source: MondoRescue/branches/2.2.10/mondo/src/common/newt-specific.c@ 2331

Last change on this file since 2331 was 2331, checked in by Bruno Cornec, 15 years ago

r3342@localhost: bruno | 2009-08-14 00:46:51 +0200

  • Another round of strcpy suppressions
  • find_home_of_exe() now allocates memory which has to be freed by the caller
  • Supress useless sz_last_suffix()
  • mr_getline now has the same interface as the other mr_mem functions
  • valgrind and compiler warnings fixes
  • Property svn:keywords set to Id
File size: 44.3 KB
RevLine 
[1]1/* newt-specific.c
[99]2 $Id: newt-specific.c 2331 2009-08-18 13:25:29Z bruno $
[1]3
4 subroutines which do display-type things
5 and use the newt library to do them
6*/
7
8
9/**
10 * @file
11 * Functions for doing display-type things with the Newt library.
12 */
13
14#define MAX_NEWT_COMMENT_LEN 200
15
16#if __cplusplus
17extern "C" {
18#endif
19
20#include "my-stuff.h"
[2211]21#include "mr_mem.h"
[1]22#include "mondostructures.h"
23#include "newt-specific.h"
24#include "libmondo-string-EXT.h"
25#include "libmondo-files-EXT.h"
26#include "libmondo-devices-EXT.h"
27#include "libmondo-tools-EXT.h"
28#include "libmondo-fork-EXT.h"
[541]29#include "libmondo-gui-EXT.h"
30#include "lib-common-externs.h"
[1]31
[1316]32extern char *MONDO_LOGFILE;
33
[1665]34extern struct s_bkpinfo *bkpinfo;
[2321]35extern void mr_free_bkpinfo();
[1665]36
[1]37/*@unused@*/
[99]38//static char cvsid[] = "$Id: newt-specific.c 2331 2009-08-18 13:25:29Z bruno $";
[1]39
[128]40 extern pid_t g_mastermind_pid;
[2030]41 /*
[128]42 extern char *g_tmpfs_mountpt;
[2030]43 */
[128]44 extern char *g_boot_mountpt;
[792]45 extern char *ps_options;
[928]46 extern char *ps_proc_id;
[1]47
[128]48 extern void set_signals(int);
[1]49
50/**
51 * @addtogroup globalGroup
52 * @{
53 */
54/**
55 * Whether we are currently in a nested call of fatal_error().
56 */
[128]57 bool g_exiting = FALSE;
[1]58
59/**
60 * Padding below the Newt components, to overcome bugs in Newt.
61 */
[128]62 char g_haharrrrr[500];
[1]63
64
[128]65 newtComponent g_timeline = NULL, ///< The line of the progress form that shows the time elapsed/remaining
66 g_percentline = NULL, ///< The line of the progress form that shows the percent completed/remaining
67 g_scale = NULL, ///< The progress bar component in the progress form
68 g_progressForm = NULL, ///< The progress form component itself
69 g_blurb1 = NULL, ///< The component for line 1 of the blurb in the progress form
70 g_blurb2 = NULL, ///< The component for line 2 of the blurb in the progress form
71 g_blurb3 = NULL, ///< The component for line 3 (updated continuously) of the blurb in the progress form
72 g_label = NULL; ///< ????? @bug ?????
[1]73
74/**
75 * Padding above the Newt components, to overcome bugs in Newt.
76 */
[128]77 char g_jim_lad_yarr[500];
78 char **err_log_lines = NULL, ///< The list of log lines to show on the screen.
79 g_blurb_str_1[MAX_NEWT_COMMENT_LEN] = "", ///< The string for line 1 of the blurb in the progress form
80 g_blurb_str_2[MAX_NEWT_COMMENT_LEN] = "", ///< The string for line 2 of the blurb in the progress form
81 g_blurb_str_3[MAX_NEWT_COMMENT_LEN] = ""; ///< The string for line 3 (updated continuously) of the blurb in the progress form
82 newtComponent g_isoform_main = NULL, ///< The evalcall form component itself
83 g_isoform_header = NULL, ///< The component for the evalcall form title
84 g_isoform_scale = NULL, ///< The progress bar component in the evalcall form
85 g_isoform_timeline = NULL, ///< The line of the evalcall form that shows the time elapsed/remaining
86 g_isoform_pcline = NULL; ///< The line of the evalcall form that shows the percent completed/remaining
87 long g_isoform_starttime; ///< The time (in seconds since the epoch) that the evalcall form was opened.
88 int g_isoform_old_progress = -1; ///< The most recent progress update of the evalcall form (percent).
89 char g_isoform_header_str[MAX_STR_LEN] = " "; ///< The string for the evalcall form title.
90 int g_mysterious_dot_counter; ///< The counter for the twirling baton (/ | \\ - ...) on percentage less than 3
91 int g_noof_log_lines = 6; ///< The number of lines to show in the log at the bottom of the screen.
92 int g_noof_rows = 25; ///< The number of rows on the screen.
[1]93
[128]94 int g_currentY = 3; ///< The row to write background progress messages to. Incremented each time a message is written.
95 extern int g_current_media_number;
96 pid_t g_main_pid = 0; ///< The PID of the main Mondo process.
97 long g_maximum_progress = 999; ///< The maximum amount of progress (100%) for the currently opened progress form.
98 long g_current_progress = -999; ///< The current amount of progress (filelist #, etc.) for the currently opened progress form.
[2277]99 long g_start_time = 0L; ///< The time (in seconds since the epoch) that the progress form was opened.
[128]100 bool g_text_mode = TRUE; ///< If FALSE, use a newt interface; if TRUE, use an ugly (but more compatible) dumb terminal interface.
101 char g_xmondo_stdin[MAX_NEWT_COMMENT_LEN], ///< ... @bug Unneeded w/current XMondo.
102 g_xmondo_stdout[MAX_NEWT_COMMENT_LEN]; ///< .... @bug Unneeded w/current XMondo.
103 bool g_called_by_xmondo = FALSE; ///< @bug Unneeded w/current XMondo.
104 char *g_selfmounted_isodir; ///< Holds the NFS mountpoint if mounted via mondoarchive.
[1]105
106/* @} - end of globalGroup */
107
108//int g_fd_in=-1, g_fd_out=-1;
109
[128]110 void popup_and_OK(char *);
[1]111
112
113/**
114 * @addtogroup guiGroup
115 * @{
116 */
117/**
118 * Ask the user a yes/no question.
119 * @param prompt The question to ask the user.
120 * @return TRUE for yes; FALSE for no.
121 */
[128]122 bool ask_me_yes_or_no(char *prompt) {
[1]123
[128]124 /*@ buffers ********************************************************** */
125 int i;
[1]126
[128]127 assert_string_is_neither_NULL_nor_zerolength(prompt);
[1]128
[128]129 if (g_text_mode) {
[2136]130
131 char *tmp;
132 tmp = malloc(MAX_NEWT_COMMENT_LEN);
133
[128]134 while (1) {
135 system("sync");
136 printf
[1899]137 ("---promptdialogYN---1--- %s\n---promptdialogYN---Q--- [yes] [no] ---\n--> ",
[128]138 prompt);
139 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, stdin);
140 if (tmp[strlen(tmp) - 1] == '\n')
141 tmp[strlen(tmp) - 1] = '\0';
[1]142
[128]143 i = (int) strlen(tmp);
144 if (i > 0 && tmp[i - 1] < 32) {
145 tmp[i - 1] = '\0';
146 }
[541]147 if (strstr("yesYES", tmp)) {
[128]148 paranoid_free(tmp);
149 return (TRUE);
[541]150 } else if (strstr("NOno", tmp)) {
[128]151 paranoid_free(tmp);
152 return (FALSE);
153 } else {
154 system("sync");
155 printf
[541]156 ("Please enter either YES or NO (or yes or no, or y or n, or...)\n");
[128]157 }
158 }
159 } else {
[541]160 return (popup_with_buttons(prompt, "Yes", "No"));
[128]161 }
162 }
[1]163
164
165/**
166 * Give the user the opportunity to continue the current operation (OK)
167 * or cancel it (Cancel).
168 * @param prompt The string to be displayed.
169 * @return TRUE for OK, FALSE for Cancel.
170 */
[128]171 bool ask_me_OK_or_cancel(char *prompt) {
[1]172
[128]173 /*@ buffer *********************************************************** */
174 char *tmp;
175 int i;
[1]176
[128]177 tmp = malloc(MAX_NEWT_COMMENT_LEN);
178 assert_string_is_neither_NULL_nor_zerolength(prompt);
179 if (g_text_mode) {
180 system("sync");
181 printf
[1899]182 ("---promptdialogOKC---1--- %s\n---promptdialogOKC---Q--- [OK] [Cancel] ---\n--> ",
[128]183 prompt);
184 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, stdin);
185 if (tmp[strlen(tmp) - 1] == '\n')
186 tmp[strlen(tmp) - 1] = '\0';
[1]187
[128]188 i = (int) strlen(tmp);
189 if (i > 0 && tmp[i - 1] < 32) {
190 tmp[i - 1] = '\0';
191 }
[541]192 if (strstr("okOKOkYESyes", tmp)) {
[128]193 paranoid_free(tmp);
194 return (TRUE);
195 } else {
196 paranoid_free(tmp);
197 return (FALSE);
198 }
199 } else {
200 paranoid_free(tmp);
[541]201 return (popup_with_buttons(prompt, " Okay ", "Cancel"));
[128]202 }
[1]203 }
204
205
206
207/**
208 * Close the currently opened evalcall form.
209 */
[128]210 void
211 close_evalcall_form(void) {
212 if (g_text_mode) {
213 return;
214 }
215 if (g_isoform_main == NULL) {
216 return;
217 }
218 update_evalcall_form(100);
219 usleep(500000);
220 if (g_text_mode) {
221 log_msg(2, "Closing evalcall form");
222 return;
223 }
224 newtPopHelpLine();
225 newtFormDestroy(g_isoform_main);
226 newtPopWindow();
[2291]227
228 /* Reset globals */
[128]229 g_isoform_main = NULL;
230 g_isoform_old_progress = -1;
[2291]231 g_isoform_header = NULL;
232 g_isoform_scale = NULL;
233 g_isoform_timeline = NULL;
234 g_isoform_pcline = NULL;
235}
[1]236
237/**
238 * Close the currently opened progress form.
239 */
[128]240 void
241 close_progress_form() {
242 if (g_text_mode) {
243 return;
244 }
245 if (g_current_progress == -999) {
246 log_msg(2,
247 "Trying to close the progress form when it ain't open!");
248 return;
249 }
250 g_current_progress = g_maximum_progress;
251 update_progress_form("Complete");
252 sleep(1);
253 if (g_text_mode) {
254 log_msg(2, "Closing progress form");
255 return;
256 }
257 newtPopHelpLine();
258 newtFormDestroy(g_progressForm);
259 newtPopWindow();
[2291]260
261 /* Reset globals */
[128]262 g_progressForm = NULL;
263 g_current_progress = -999;
[2291]264 g_timeline = NULL;
265 g_percentline = NULL;
266 g_scale = NULL;
267
[128]268 }
[1]269
[792]270/**
271 * Kill any process containing the string @p str surrounded by spaces in its commandline.
272 */
273void kill_anything_like_this(char *str) {
[1]274
[792]275char *tmp = NULL;
[1850]276char *tmp1 = NULL;
[1]277
[2323]278 mr_asprintf(tmp,"ps %s | grep \" %s \" | awk '{print %s;}' | grep -v \"grep\"", ps_options, str , ps_proc_id);
[1294]279 run_program_and_log_output(tmp, TRUE);
[1850]280 if (strlen(tmp) > 0) {
[2323]281 mr_asprintf(tmp1,"kill `%s`", tmp);
[1850]282 run_program_and_log_output(tmp1, TRUE);
[2321]283 mr_free(tmp1);
[1850]284 }
[2321]285 mr_free(tmp);
[792]286}
[1]287
[792]288
[1]289/**
290 * Exit Mondo with a fatal error.
291 * @param error_string The error message to present to the user before exiting.
292 * @note This function never returns.
293 */
[128]294 void
295 fatal_error(char *error_string) {
296 /*@ buffers ***************************************************** */
[2320]297 char *command = NULL;
[128]298 static bool already_exiting = FALSE;
[1]299
[128]300 /*@ end vars **************************************************** */
[1]301
[128]302 set_signals(FALSE); // link to external func
303 g_exiting = TRUE;
304 log_msg(1, "Fatal error received - '%s'", error_string);
305 printf("Fatal error... %s\n", error_string);
306 if (getpid() == g_mastermind_pid) {
307 log_msg(2, "mastermind %d is exiting", (int) getpid());
308 kill(g_main_pid, SIGTERM);
309 finish(1);
310 }
[1]311
[128]312 if (getpid() != g_main_pid) {
313 if (g_mastermind_pid != 0 && getpid() != g_mastermind_pid) {
314 log_msg(2, "non-m/m %d is exiting", (int) getpid());
315 kill(g_main_pid, SIGTERM);
316 finish(1);
317 }
318 }
[1]319
[128]320 log_msg(3, "OK, I think I'm the main PID.");
321 if (already_exiting) {
322 log_msg(3, "...I'm already exiting. Give me time, Julian!");
323 finish(1);
324 }
[1]325
[128]326 already_exiting = TRUE;
327 log_msg(2, "I'm going to do some cleaning up now.");
328 paranoid_system("killall mindi 2> /dev/null");
329 kill_anything_like_this("/mondo/do-not");
330 kill_anything_like_this("tmp.mondo");
[296]331 kill_anything_like_this("ntfsclone");
[128]332 sync();
[1]333
[2030]334 chdir("/");
[128]335 if (g_selfmounted_isodir) {
[2323]336 mr_asprintf(command, "umount %s", g_selfmounted_isodir);
[128]337 run_program_and_log_output(command, 5);
[2320]338 mr_free(command);
[2323]339 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
[128]340 run_program_and_log_output(command, 5);
[2320]341 mr_free(command);
[128]342 }
[99]343
[128]344 if (!g_text_mode) {
[2320]345 log_msg(0, "-------FATAL ERROR---------");
[128]346 log_msg(0, error_string);
347 newtFinished();
348 }
[1]349
[128]350 printf("---FATALERROR--- %s\n", error_string);
[2320]351 printf("If you require technical support, please contact the mailing list.\n");
[541]352 printf("See http://www.mondorescue.org for details.\n");
[2320]353 printf("The list's members can help you, if you attach that file to your e-mail.\n");
[541]354 printf("Log file: %s\n", MONDO_LOGFILE);
355 printf("Mondo has aborted.\n");
[128]356 if (!g_main_pid) {
357 log_msg(3, "FYI - g_main_pid is blank");
358 }
359 finish(254);
360 }
[1]361
[2241]362/* Free memory allocated for newt */
363void free_newt_stuff() {
[1]364
[2241]365int i;
[1]366
[2241]367for (i = 0; i < g_noof_log_lines; i++) {
368 mr_free(err_log_lines[i]);
369}
370mr_free(err_log_lines);
371}
372
373
374
[1]375/**
376 * Exit Mondo normally.
377 * @param signal The exit code (0 indicates a successful backup; 1 for Mondo means the
378 * user aborted; 254 means a fatal error occured).
379 * @note This function never returns.
380 */
[128]381 void
382 finish(int signal) {
[1708]383 char *command = NULL;
[1]384
[128]385 chdir("/");
386 run_program_and_log_output("umount " MNT_CDROM, FALSE);
387 if (g_selfmounted_isodir) {
[2323]388 mr_asprintf(command, "umount %s", g_selfmounted_isodir);
[128]389 run_program_and_log_output(command, 1);
[2320]390 mr_free(command);
391
[2323]392 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
[128]393 run_program_and_log_output(command, 1);
[2320]394 mr_free(command);
[128]395 }
396 if (!g_text_mode) {
397 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
398 log_msg(1, "Calling newtFinished()");
399 newtFinished();
400 } else {
401 log_msg(1, "Calling newtSuspend()");
402 newtSuspend();
403 }
404 }
[541]405 printf("Execution run ended; result=%d\n", signal);
406 printf("Type 'less %s' to see the output log\n", MONDO_LOGFILE);
[2241]407 if (bkpinfo->tmpdir) {
408 log_msg(8,"tempdir is %s",bkpinfo->tmpdir);
409 if (strstr(bkpinfo->tmpdir ,"mondo.tmp.") != NULL) {
410 log_msg(8,"erasing tempdir");
[2323]411 mr_asprintf(command, "rm -Rf %s", bkpinfo->tmpdir);
[2241]412 system(command);
[2318]413 mr_free(command);
[2241]414 }
[1999]415 }
[2241]416 if (bkpinfo->scratchdir) {
417 log_msg(8,"scratchdir is %s", bkpinfo->scratchdir);
418 if (strstr(bkpinfo->scratchdir ,"mondo.scratch.") != NULL) {
419 log_msg(8,"erasing scratchdir");
[2323]420 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
[2241]421 system(command);
[2318]422 mr_free(command);
[2241]423 }
424 }
425 /* Free all allocated strings in bkpinfo */
[2321]426 mr_free_bkpinfo();
[128]427 free_libmondo_global_strings();
[2241]428 free_newt_stuff();
[128]429 exit(signal);
430 }
[1]431
432
433
434
435
436/**
437 * Log the last @p g_noof_log_lines lines of @p filename that match @p
438 * grep_for_me to the screen.
439 * @param filename The file to give the end of.
440 * @param grep_for_me If not "", then only give lines in @p filename that match this regular expression.
441 */
[128]442 void
443 log_file_end_to_screen(char *filename, char *grep_for_me) {
[1]444
[128]445 /*@ buffers ********************************************************** */
446 char *command;
447 char *tmp;
[1]448
[128]449 /*@ pointers ********************************************************* */
450 FILE *fin;
[1]451
[128]452 /*@ int ************************************************************** */
453 int i = 0;
[1]454
[128]455 malloc_string(command);
456 malloc_string(tmp);
457 assert_string_is_neither_NULL_nor_zerolength(filename);
458 assert(grep_for_me != NULL);
[1]459
[128]460 if (!does_file_exist(filename)) {
461 paranoid_free(command);
462 paranoid_free(tmp);
463 return;
[1]464 }
[128]465 if (grep_for_me[0] != '\0') {
466 sprintf(command, "grep '%s' %s | tail -n%d",
467 grep_for_me, filename, g_noof_log_lines);
468 } else {
[144]469 sprintf(command, "tail -n%d %s", g_noof_log_lines,
[128]470 filename);
[1]471 }
[128]472 fin = popen(command, "r");
473 if (!fin) {
474 log_OS_error(command);
475 } else {
476 for (i = 0; i < g_noof_log_lines; i++) {
477 for (err_log_lines[i][0] = '\0';
478 strlen(err_log_lines[i]) < 2 && !feof(fin);) {
479 (void) fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN,
480 fin);
[1274]481 /* Commented to make valgrind happy and avoid crash
[128]482 strip_spaces(err_log_lines[i]);
[1274]483 */
[128]484 if (!strncmp(err_log_lines[i], "root:", 5)) {
485 strcpy(tmp, err_log_lines[i] + 6);
486 strcpy(err_log_lines[i], tmp);
487 }
488 if (feof(fin)) {
489 break;
490 }
491 }
492 }
493 paranoid_pclose(fin);
494 }
495 refresh_log_screen();
496 paranoid_free(command);
497 paranoid_free(tmp);
[1]498 }
499
500
501/**
502 * Log a message to the screen.
503 * @param fmt A printf-style format string to write. The following parameters are its arguments.
504 * @note The message is also written to the logfile.
505 */
[128]506 void
507 log_to_screen(const char *fmt, ...) {
[1]508
[128]509 /*@ int ************************************************************** */
510 int i = 0;
511 int j = 0;
512 va_list args;
[1]513
[128]514 /*@ buffers ********************************************************** */
515 char *output;
[1]516
[128]517 malloc_string(output);
[1]518
[128]519 va_start(args, fmt);
520 vsprintf(output, fmt, args);
521 log_msg(0, output);
522 output[80] = '\0';
523 va_end(args);
524 i = (int) strlen(output);
525 if (i > 0 && output[i - 1] < 32) {
526 output[i - 1] = '\0';
527 }
[1]528
[128]529 if (err_log_lines) {
530 for (i = 1; i < g_noof_log_lines; i++) {
531 strcpy(err_log_lines[i - 1],
532 " ");
533 strcpy(err_log_lines[i - 1], err_log_lines[i]);
534 }
535 }
536 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
537 output[strlen(output) - 1] = '\0';
538 }
539 for (j = 0; j < (int) strlen(output); j++) {
540 if (output[j] < 32) {
541 output[j] = ' ';
542 }
543 }
544 if (err_log_lines)
545 strcpy(err_log_lines[g_noof_log_lines - 1], output);
546 if (g_text_mode) {
547 printf("%s\n", output);
548 } else {
549 refresh_log_screen();
550 }
551 paranoid_free(output);
[1]552 }
553
554
555
556
557/**
558 * Write a string to the root window at (@p x, @p y) and also to the logfile.
559 * @param y The row to write the string to.
560 * @param x The column to write the string to.
561 * @param output The string to write.
562 */
[128]563 void
564 mvaddstr_and_log_it(int y, int x, char *output) {
565 assert_string_is_neither_NULL_nor_zerolength(output);
566 log_msg(0, output);
567 if (g_text_mode) {
568 printf("%s\n", output);
569 } else {
570 newtDrawRootText(x, y, output);
571 newtRefresh();
572 }
573 }
[1]574
575
576
577
578/**
579 * Open an evalcall form with title @p ttl.
580 * @param ttl The title to use for the evalcall form.
581 */
[128]582 void
583 open_evalcall_form(char *ttl) {
[1]584
[128]585 /*@ buffers ********************************************************* */
586 char *title;
587 char *tmp;
[1]588
[128]589 /*@ initialize ****************************************************** */
590 g_isoform_old_progress = -1;
591 g_mysterious_dot_counter = 0;
592 malloc_string(title);
593 malloc_string(tmp);
[1]594
[128]595 assert(ttl != NULL);
596 strcpy(title, ttl);
597 strcpy(g_isoform_header_str, title);
598 if (g_text_mode) {
599 log_msg(0, title);
600 } else {
601 strcpy(tmp, title);
602 center_string(tmp, 80);
603 newtPushHelpLine(tmp);
604 }
605 center_string(g_isoform_header_str, 36);
606 g_isoform_starttime = get_time();
607 if (g_text_mode) {
608 log_msg(0, g_isoform_header_str);
609 } else {
610 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
611 g_isoform_scale = newtScale(3, 3, 34, 100);
612 newtCenteredWindow(40, 7, title);
613 g_isoform_main = newtForm(NULL, NULL, 0);
614 g_isoform_timeline = newtLabel(1, 5, "This is the timeline");
615 g_isoform_pcline = newtLabel(1, 6, "This is the pcline");
616 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
617 g_isoform_pcline, g_isoform_header,
618 g_isoform_scale, NULL);
619 newtRefresh();
620 }
621 update_evalcall_form(0);
622 paranoid_free(tmp);
623 paranoid_free(title);
624 }
[1]625
626
627
628/**
629 * Open a progress form with title @p title.
630 * @param title The title to use for the progress form (will be put in the title bar on Newt).
631 * @param b1 The first line of the blurb; generally static.
632 * @param b2 The second line of the blurb; generally static.
633 * @param b3 The third line of the blurb; generally dynamic (it is passed
634 * to update_evalcall_form() every time).
635 * @param max_val The maximum amount of progress (number of filesets, etc.)
636 */
[128]637 void
638 open_progress_form(char *title, char *b1, char *b2, char *b3,
639 long max_val) {
[1]640
[128]641 /*@ buffers ********************************************************* */
642 char *b1c;
643 char *blurb1;
644 char *blurb2;
645 char *blurb3;
[1]646
[128]647 /*@ initialize ****************************************************** */
648 g_mysterious_dot_counter = 0;
[1]649
[128]650 malloc_string(b1c);
651 malloc_string(blurb1);
652 malloc_string(blurb2);
653 malloc_string(blurb3);
[1]654
[128]655 assert(title != NULL);
656 assert(b1 != NULL);
657 assert(b2 != NULL);
658 assert(b3 != NULL);
[1]659
[128]660 strcpy(blurb1, b1);
661 strcpy(blurb2, b2);
662 strcpy(blurb3, b3);
663 strcpy(b1c, b1);
664 center_string(b1c, 80);
665 if (max_val <= 0) {
666 max_val = 1;
667 }
[1]668
[128]669 g_start_time = get_time();
670 g_maximum_progress = max_val;
671 g_current_progress = 0;
672 strcpy(g_blurb_str_1, blurb1);
673 strcpy(g_blurb_str_2, blurb3);
674 strcpy(g_blurb_str_3, blurb2);
675 if (g_text_mode) {
676 log_msg(0, blurb1);
677 log_msg(0, blurb2);
678 log_msg(0, blurb3);
679 } else {
680 g_blurb1 = newtLabel(2, 1, blurb1);
681 g_blurb2 = newtLabel(2, 2, blurb3);
682 g_blurb3 = newtLabel(2, 4, blurb2);
683 // newtOpenWindow (10, 4, 60, 11, title);
684 newtCenteredWindow(60, 11, title);
685 g_scale = newtScale(3, 6, 54, g_maximum_progress);
686 g_progressForm = newtForm(NULL, NULL, 0);
687 g_percentline = newtLabel(10, 9, "This is the percentline");
688 g_timeline = newtLabel(10, 8, "This is the timeline");
689 newtFormAddComponents(g_progressForm, g_percentline,
690 g_timeline, g_scale, g_blurb1, g_blurb3,
691 g_blurb2, NULL);
692 newtPushHelpLine(b1c);
693 newtRefresh();
694 }
695 update_progress_form_full(blurb1, blurb2, blurb3);
696 paranoid_free(b1c);
697 paranoid_free(blurb1);
698 paranoid_free(blurb2);
699 paranoid_free(blurb3);
700 }
701
[1]702/**
703 * Give a message to the user in the form of a dialog box (under Newt).
704 * @param prompt The message.
705 */
[128]706 void
707 popup_and_OK(char *prompt) {
708 char ch;
[1]709
[128]710 assert_string_is_neither_NULL_nor_zerolength(prompt);
[1]711
[128]712 log_msg(0, prompt);
713 if (g_text_mode) {
714 printf
[1899]715 ("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ",
[128]716 prompt);
717 while (((ch = getchar()) != '\n') && (ch != EOF));
718 } else {
[541]719 (void) popup_with_buttons(prompt, " OK ", "");
[128]720 }
721 }
[1]722
723/**
724 * Ask the user to enter a value.
725 * @param title The title of the dialog box.
726 * @param b The blurb (e.g. what you want the user to enter).
[2316]727 * @param input The string given originaly as default
728 * @return output string if the user pressed OK, NULL if they pressed Cancel.
[1]729 */
[2316]730 char *popup_and_get_string(char *title, char *b, char *input) {
[1]731
[128]732 /*@ newt ************************************************************ */
733 newtComponent myForm;
734 newtComponent b_1;
735 newtComponent b_2;
736 newtComponent b_res;
737 newtComponent text;
738 newtComponent type_here;
[1]739
[128]740 /*@ pointers ********************************************************* */
741 char *entry_value;
[1]742
[128]743 /*@ buffers ********************************************************** */
[2316]744 char *blurb = NULL;
745 char *output = NULL;
[1]746
[128]747 assert_string_is_neither_NULL_nor_zerolength(title);
748 assert(b != NULL);
[1]749
[128]750 if (g_text_mode) {
[2316]751 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q--- [%s]\n--> ", title, b, input);
[2331]752 mr_getline(output, stdin);
[2316]753 if (! strcmp(output,"\n")) {
754 /* take default if ENTER was typed */
755 mr_free(output);
[2323]756 mr_asprintf(output, "%s", input);
[2316]757 }
[128]758 if (output[strlen(output) - 1] == '\n')
759 output[strlen(output) - 1] = '\0';
[2316]760 return (output);
[128]761 }
[2316]762
[2323]763 mr_asprintf(blurb, "%s", b);
[128]764 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
765 type_here =
766 newtEntry(2, newtTextboxGetNumLines(text) + 2,
[2316]767 input, 50,
[1]768#ifdef __cplusplus
[128]769 0, NEWT_FLAG_RETURNEXIT
[1]770#else
[128]771 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
[1]772#endif
[128]773 );
[541]774 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
775 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
[128]776 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
777 myForm = newtForm(NULL, NULL, 0);
778 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
779 center_string(blurb, 80);
780 newtPushHelpLine(blurb);
781 b_res = newtRunForm(myForm);
[2316]782
783 if (b_res == b_1) {
[2323]784 mr_asprintf(output, "%s", newtEntryGetValue(type_here));
[2316]785 }
[128]786 newtPopHelpLine();
787 newtFormDestroy(myForm);
788 newtPopWindow();
[2316]789 mr_free(blurb);
790
791 return (output);
[128]792 }
[1]793
794
795/**
796 * Pop up a dialog box with user-defined buttons.
797 * @param p The text to put in the dialog box.
798 * @param button1 The label on the first button.
799 * @param button2 The label on the second button, or "" if you only want one button.
800 * @return TRUE if @p button1 was pushed, FALSE otherwise.
801 */
[128]802 bool popup_with_buttons(char *p, char *button1, char *button2) {
[1]803
[128]804 /*@ buffers *********************************************************** */
[2136]805 char *prompt;
[1]806
[128]807 /*@ newt ************************************************************** */
808 newtComponent myForm;
809 newtComponent b_1;
810 newtComponent b_2;
811 newtComponent b_res;
812 newtComponent text;
[1]813
[128]814 assert_string_is_neither_NULL_nor_zerolength(p);
815 assert(button1 != NULL);
816 assert(button2 != NULL);
[2136]817 prompt = malloc(MAX_NEWT_COMMENT_LEN);
[128]818 if (g_text_mode) {
819 if (strlen(button2) == 0) {
820 printf("%s (%s) --> ", p, button1);
821 } else {
822 printf("%s (%s or %s) --> ", p, button1, button2);
823 }
[2136]824 for (prompt[0] = '\0';
825 strcmp(prompt, button1) && (strlen(button2) == 0
826 || strcmp(prompt, button2));) {
[128]827 printf("--> ");
[2136]828 (void) fgets(prompt, MAX_NEWT_COMMENT_LEN, stdin);
[128]829 }
[2136]830 if (!strcmp(prompt, button1)) {
[128]831 paranoid_free(prompt);
832 return (TRUE);
833 } else {
834 paranoid_free(prompt);
835 return (FALSE);
836 }
837 }
[1]838
[877]839 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
[883]840 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
[128]841 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
842 b_1 =
843 newtButton(20 -
844 ((button2[0] !=
845 '\0') ? strlen(button1) +
846 2 : strlen(button1) / 2),
847 newtTextboxGetNumLines(text) + 3, button1);
848 if (button2[0] != '\0') {
849 b_2 =
850 newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
851 } else {
852 b_2 = NULL;
853 }
854 // newtOpenWindow (25, 5, 46, newtTextboxGetNumLines (text) + 7, "Alert");
[541]855 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
[128]856 myForm = newtForm(NULL, NULL, 0);
857 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
858 center_string(prompt, 80);
859 newtPushHelpLine(prompt);
860 b_res = newtRunForm(myForm);
861 newtPopHelpLine();
862 newtFormDestroy(myForm);
863 newtPopWindow();
864 if (b_res == b_1) {
865 paranoid_free(prompt);
866 return (TRUE);
867 } else {
868 paranoid_free(prompt);
869 return (FALSE);
870 }
871 }
[1]872
873
874
[128]875
[1]876/**
877 * Synchronize the log messages stored in @p err_log_lines with those shown
878 * on the screen.
879 */
[128]880 void
881 refresh_log_screen() {
[1]882
[128]883 /*@ int *********************************************************** */
884 int i = 0;
[1]885
886
[128]887 if (g_text_mode || !err_log_lines) {
888 return;
889 }
890 for (i = g_noof_log_lines - 1; i >= 0; i--) {
891 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
892 " ");
893 }
894 newtRefresh();
895 for (i = g_noof_log_lines - 1; i >= 0; i--) {
896 err_log_lines[i][79] = '\0';
897 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
898 err_log_lines[i]);
899 }
900 newtRefresh();
901 }
[1]902
903
904/**
905 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
906 * only allocate some memory.
907 */
[128]908 void
909 setup_newt_stuff() {
[1]910
[128]911 /*@ int *********************************************************** */
912 int i = 0;
913 int cols;
[1]914
[128]915 if (!g_text_mode) {
916 newtInit();
917 newtCls();
[2321]918 newtPushHelpLine("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
[128]919 newtDrawRootText(18, 0, WELCOME_STRING);
920 newtRefresh();
921 newtGetScreenSize(&cols, &g_noof_rows);
922 g_noof_log_lines = (g_noof_rows / 5) + 1;
923 }
[1]924
[2320]925 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
[1]926
[128]927 for (i = 0; i < g_noof_log_lines; i++) {
[2320]928 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
[128]929 }
[1]930
[128]931 for (i = 0; i < g_noof_log_lines; i++) {
932 err_log_lines[i][0] = '\0';
933 }
934 }
[1]935
936
937/**
938 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
939 * @param num The numerator of the ratio.
940 * @param denom The denomenator of the ratio.
941 */
[128]942 void
943 update_evalcall_form_ratio(int num, int denom) {
[1]944
[128]945 /*@ long ************************************************************ */
[2277]946 long current_time = 0L;
947 long time_taken = 0L;
948 long time_total_est = 0L;
949 long time_remaining = 0L;
[1]950
[128]951 /*@ buffers ********************************************************** */
[2274]952 char *timeline_str = NULL;
[2211]953 char *pcline_str = NULL;
954 char *taskprogress = NULL;
[1]955
[128]956 /*@ int ************************************************************** */
957 int percentage = 0;
958 int i = 0;
959 int j = 0;
[1]960
[128]961 if (num * 100 < denom) {
962 percentage = 1;
963 } else {
964 percentage = (num * 100 + denom / 2) / denom;
965 }
966
967 current_time = get_time();
968 time_taken = current_time - g_isoform_starttime;
969 if (num) {
970 time_total_est = time_taken * denom / num;
971 time_remaining = time_total_est - time_taken;
972 } else {
[2277]973 time_remaining = 0L;
[128]974 }
975 if (!g_text_mode) {
[1899]976 if (g_isoform_header) {
977 newtLabelSetText(g_isoform_header, g_isoform_header_str);
978 }
[128]979 }
980 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
981 if ((percentage < 3 && g_isoform_old_progress < 3)
982 || percentage > g_isoform_old_progress) {
983 g_isoform_old_progress = percentage;
[2323]984 mr_asprintf(timeline_str, "%2ld:%02ld taken %2ld:%02ld remaining", (long) time_taken / 60, (long) time_taken % 60, (long) time_remaining / 60, (long) time_remaining % 60);
[128]985 if (percentage < 3) {
[2323]986 mr_asprintf(pcline_str, " Working");
[128]987 for (j = 0; j < g_mysterious_dot_counter; j++) {
[2211]988 mr_strcat(pcline_str, ".");
[128]989 }
990 for (; j < 27; j++) {
[2211]991 mr_strcat(pcline_str, " ");
[128]992 }
[2211]993 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
[128]994 } else {
[2323]995 mr_asprintf(pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
[128]996 }
997 if (g_text_mode) {
[2323]998 mr_asprintf(taskprogress, "TASK: [");
[128]999 for (i = 0; i < percentage; i += 5) {
[2211]1000 mr_strcat(taskprogress, "*");
[128]1001 }
1002 for (; i < 100; i += 5) {
[2211]1003 mr_strcat(taskprogress, ".");
[128]1004 }
1005 if (percentage >= 3) {
[2277]1006 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, time_remaining / 60, time_remaining % 60);
1007 printf("---evalcall---1--- %s\n", g_isoform_header_str);
[1899]1008 printf("---evalcall---2--- %s\n", taskprogress);
1009 printf("---evalcall---E---\n");
[128]1010 }
[2277]1011 mr_free(taskprogress);
[128]1012 } else {
1013 newtScaleSet(g_isoform_scale,
1014 (unsigned long long) percentage);
[1899]1015 if (g_isoform_pcline) {
1016 newtLabelSetText(g_isoform_pcline, pcline_str);
1017 }
1018 if ((percentage >= 3) && (g_isoform_timeline)) {
[128]1019 newtLabelSetText(g_isoform_timeline, timeline_str);
1020 }
1021 }
[2274]1022 mr_free(pcline_str);
1023 mr_free(timeline_str);
[128]1024 }
1025 if (!g_text_mode) {
1026 newtRefresh();
1027 }
[1]1028 }
1029
1030
1031
1032/**
1033 * Update the evalcall form to show @p curr %.
1034 * @param curr The current amount of progress (percentage) in the evalcall form.
1035 */
[128]1036 void
1037 update_evalcall_form(int curr) {
1038 update_evalcall_form_ratio(curr, 100);
1039 }
[1]1040
1041
1042
1043/**
1044 * Update the progress form to show @p blurb3 and the current value of
1045 * @p g_maximum_progress.
1046 * @param blurb3 The new third line of the blurb; use @p g_blurb_str_2 (no, that's not a typo) to keep it the same.
1047 */
[128]1048 void
1049 update_progress_form(char *blurb3) {
1050 if (g_current_progress == -999) {
1051 return;
1052 }
1053 strcpy(g_blurb_str_2, blurb3);
1054 update_progress_form_full(g_blurb_str_1, g_blurb_str_2,
1055 g_blurb_str_3);
1056 }
[1]1057
1058
1059/**
1060 * Update the progress form's complete blurb and show @p g_current_progress.
1061 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1062 * @param blurb2 The second line of the blurb. Use @p g_blurb_str_3 (no, that's not a typo) to keep it the same.
1063 * @param blurb3 The third line of the blurb. Use @p g_blurb_str_2 (no, that's not a typo either) to keep it the same.
1064 */
[128]1065 void
1066 update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1067 /*@ long ***************************************************** */
[2277]1068 long current_time = 0L;
1069 long time_taken = 0L;
1070 long time_remaining = 0L;
1071 long time_total_est = 0L;
[1]1072
[128]1073 /*@ int ******************************************************* */
1074 int percentage = 0;
1075 int i = 0;
[1]1076
[128]1077 /*@ buffers *************************************************** */
[2274]1078 char *percentline_str = NULL;
1079 char *timeline_str = NULL;
[2211]1080 char *taskprogress = NULL;
[1]1081
[128]1082 if (!g_text_mode) {
1083 assert(blurb1 != NULL);
1084 assert(blurb2 != NULL);
1085 assert(blurb3 != NULL);
1086 assert(g_timeline != NULL);
1087 }
[1]1088
[128]1089 current_time = get_time();
1090 time_taken = current_time - g_start_time;
1091 if (g_maximum_progress == 0) {
1092 percentage = 0;
1093 } else {
1094 if (g_current_progress > g_maximum_progress) {
[2296]1095 log_msg(0, "update_progress_form_full(%s,%s,%s) --- g_current_progress=%ld; g_maximum_progress=%ld", blurb1, blurb2, blurb3, g_current_progress, g_maximum_progress);
[128]1096 g_current_progress = g_maximum_progress;
1097 }
[2277]1098 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
[128]1099 }
1100 if (percentage < 1) {
1101 percentage = 1;
1102 }
1103 if (percentage > 100) {
1104 percentage = 100;
1105 }
1106 if (g_current_progress) {
[2277]1107 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
[128]1108 time_remaining = time_total_est - time_taken;
1109 } else {
[2277]1110 time_remaining = 0L;
[128]1111 }
1112 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1113 if (g_text_mode) {
[1899]1114 printf("---progress-form---1--- %s\n", blurb1);
1115 printf("---progress-form---2--- %s\n", blurb2);
1116 printf("---progress-form---3--- %s\n", blurb3);
[541]1117 printf("---progress-form---E---\n");
[2323]1118 mr_asprintf(taskprogress, "TASK: [");
[128]1119 for (i = 0; i < percentage; i += 5) {
[2211]1120 mr_strcat(taskprogress, "*");
[128]1121 }
1122 for (; i < 100; i += 5) {
[2211]1123 mr_strcat(taskprogress, ".");
[128]1124 }
1125 if (percentage > 100) {
[541]1126 log_msg(2, "percentage = %d", percentage);
[128]1127 }
[2277]1128 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
[1899]1129 printf("---progress-form---4--- %s\n", taskprogress);
[2211]1130 paranoid_free(taskprogress);
[128]1131 } else {
[2323]1132 mr_asprintf(timeline_str, "%2ld:%02ld taken %2ld:%02ld remaining ", (long) time_taken / 60, (long) time_taken % 60, (long) time_remaining / 60, (long) time_remaining % 60);
1133 mr_asprintf(percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
[2274]1134
[128]1135 center_string(blurb1, 54);
1136 center_string(blurb2, 54);
1137 center_string(blurb3, 54);
[1899]1138 if (g_blurb1) {
1139 newtLabelSetText(g_blurb1, blurb1);
1140 }
1141 if (g_blurb2) {
1142 newtLabelSetText(g_blurb2, blurb3);
1143 }
1144 if (g_blurb3) {
1145 newtLabelSetText(g_blurb3, blurb2);
1146 }
[128]1147 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
[1899]1148 if ((percentage >= 2) && (g_timeline)) {
[128]1149 newtLabelSetText(g_timeline, timeline_str);
1150 }
[2274]1151 mr_free(timeline_str);
1152
[1899]1153 if (g_percentline) {
1154 newtLabelSetText(g_percentline, percentline_str);
1155 }
[2274]1156 mr_free(percentline_str);
1157
[128]1158 newtRefresh();
1159 }
[1]1160 }
1161
1162
1163/**
1164 * Ask the user which backup media type they would like to use.
1165 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1166 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs,
1167 * and @c iso.
1168 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1169 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1170 */
[128]1171 t_bkptype which_backup_media_type(bool restoring) {
[1]1172
[128]1173 /*@ char ************************************************************ */
1174 t_bkptype output;
[1]1175
1176
[128]1177 /*@ newt ************************************************************ */
1178 char *title_sz;
1179 char *minimsg_sz;
1180 static t_bkptype possible_bkptypes[] =
1181 { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso };
1182 static char *possible_responses[] =
1183 { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev",
1184"nfs", "iso", NULL };
1185 char *outstr;
1186 t_bkptype backup_type;
1187 int i;
[1]1188
[128]1189 newtComponent b1;
1190 newtComponent b2;
1191 newtComponent b3;
1192 newtComponent b4;
1193 newtComponent b5;
1194 newtComponent b6;
1195 newtComponent b7;
1196 newtComponent b8;
1197 newtComponent b_res;
1198 newtComponent myForm;
[1]1199
[128]1200 title_sz = malloc(MAX_NEWT_COMMENT_LEN);
1201 minimsg_sz = malloc(MAX_NEWT_COMMENT_LEN);
1202 outstr = malloc(MAX_NEWT_COMMENT_LEN);
1203 if (g_text_mode) {
1204 for (backup_type = none; backup_type == none;) {
[541]1205 printf("Backup type (");
[128]1206 for (i = 0; possible_responses[i]; i++) {
1207 printf("%c%s", (i == 0) ? '\0' : ' ',
1208 possible_responses[i]);
1209 }
1210 printf(")\n--> ");
1211 (void) fgets(outstr, MAX_NEWT_COMMENT_LEN, stdin);
1212 strip_spaces(outstr);
1213 for (i = 0; possible_responses[i]; i++) {
1214 if (!strcmp(possible_responses[i], outstr)) {
1215 backup_type = possible_bkptypes[i];
1216 }
1217 }
1218 }
1219 paranoid_free(title_sz);
1220 paranoid_free(minimsg_sz);
1221 paranoid_free(outstr);
1222 return (backup_type);
1223 }
1224 newtDrawRootText(18, 0, WELCOME_STRING);
1225 if (restoring) {
1226 strcpy(title_sz,
[541]1227 "Please choose the backup media from which you want to read data.");
1228 strcpy(minimsg_sz, "Read from:");
[128]1229 } else {
1230 strcpy(title_sz,
[541]1231 "Please choose the backup media to which you want to archive data.");
1232 strcpy(minimsg_sz, "Backup to:");
[128]1233 }
1234 newtPushHelpLine(title_sz);
1235 newtCenteredWindow(34, 17, minimsg_sz);
[541]1236 b1 = newtButton(1, 1, "CD-R disks ");
1237 b2 = newtButton(17, 1, "CD-RW disks");
1238 b3 = newtButton(1, 9, "Tape drive ");
[1703]1239 b4 = newtButton(17, 5, "USB Key/Disk");
[541]1240 b5 = newtButton(1, 5, " DVD disks ");
1241 b6 = newtButton(17, 9, " NFS mount ");
1242 b7 = newtButton(1, 13, " Hard disk ");
1243 b8 = newtButton(17, 13, " Exit ");
[128]1244 myForm = newtForm(NULL, NULL, 0);
1245 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1246 NULL);
1247 b_res = newtRunForm(myForm);
1248 newtFormDestroy(myForm);
1249 newtPopWindow();
1250 if (b_res == b1) {
1251 output = cdr;
1252 } else if (b_res == b2) {
1253 output = cdrw;
1254 } else if (b_res == b3) {
1255 output = tape;
1256 } else if (b_res == b4) {
[1703]1257 output = usb;
[128]1258 } else if (b_res == b5) {
1259 output = dvd;
1260 } else if (b_res == b6) {
1261 output = nfs;
1262 } else if (b_res == b7) {
1263 output = iso;
1264 } else {
1265 output = none;
1266 }
1267 newtPopHelpLine();
1268 paranoid_free(title_sz);
1269 paranoid_free(minimsg_sz);
1270 paranoid_free(outstr);
1271 return (output);
1272 }
[1]1273
1274
1275
1276
1277/**
1278 * Ask the user how much compression they would like to use.
1279 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1280 * @return The compression level (0-9) chosen, or -1 for "Exit".
1281 */
[128]1282 int
1283 which_compression_level() {
[1]1284
[128]1285 /*@ char ************************************************************ */
1286 int output = none;
[1]1287
1288
[128]1289 /*@ newt ************************************************************ */
[1]1290
[128]1291 newtComponent b1;
1292 newtComponent b2;
1293 newtComponent b3;
1294 newtComponent b4;
1295 newtComponent b5;
1296 newtComponent b_res;
1297 newtComponent myForm;
[1]1298
[128]1299 newtDrawRootText(18, 0, WELCOME_STRING);
1300 newtPushHelpLine
[541]1301 (" Please specify the level of compression that you want.");
[128]1302 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
[541]1303 newtCenteredWindow(34, 13, "How much compression?");
1304 b1 = newtButton(4, 1, "Maximum");
1305 b2 = newtButton(18, 1, "Average");
[1463]1306 b3 = newtButton(4, 5, "Minimum");
[541]1307 b4 = newtButton(18, 5, " None ");
1308 b5 = newtButton(4, 9, " Exit ");
[128]1309 myForm = newtForm(NULL, NULL, 0);
1310 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1311 b_res = newtRunForm(myForm);
1312 newtFormDestroy(myForm);
1313 newtPopWindow();
1314 if (b_res == b1) {
1315 output = 9;
1316 } else if (b_res == b2) {
1317 output = 4;
1318 } else if (b_res == b3) {
1319 output = 1;
1320 } else if (b_res == b4) {
1321 output = 0;
1322 } else if (b_res == b5) {
1323 output = -1;
1324 }
1325 newtPopHelpLine();
1326 return (output);
1327 }
[1]1328
1329
1330
1331
1332
1333/**
1334 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1335 * @p ARBITRARY_MAXIMUM entries.
1336 * @param filelist The filelist structure to load @p source_file into.
1337 * @param source_file The file containing a list of filenames to load into @p filelist.
1338 */
[128]1339 int load_filelist_into_array(struct s_filelist *filelist,
1340 char *source_file) {
1341 int i;
[1662]1342 int j = 0;
[128]1343 bool done;
1344 char *tmp;
[1662]1345 char *tmp1 = NULL;
1346 FILE *fin, *fout;
[128]1347 struct s_filelist_entry dummy_fle;
[1]1348
[128]1349 assert(filelist != NULL);
1350 assert_string_is_neither_NULL_nor_zerolength(source_file);
[1]1351
[2230]1352 log_it("entering");
[128]1353 if (!(fin = fopen(source_file, "r"))) {
1354 log_OS_error(source_file);
1355 log_msg(2, "Can't open %s; therefore, cannot popup list",
1356 source_file);
1357 return (1);
1358 }
[1662]1359
[2323]1360 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
[1662]1361 if (!(fout = fopen(tmp1, "a"))) {
1362 log_msg(2, "Can't write to %s", tmp1);
1363 return(1);
1364 }
1365
1366 malloc_string(tmp);
[128]1367 log_msg(2, "Loading %s", source_file);
1368 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM;
1369 filelist->entries++) {
1370 god_i_hate_gotos:
1371 if (feof(fin)) {
1372 break;
1373 }
1374 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, fin);
1375 i = (int) strlen(tmp);
1376 if (i < 2) {
1377 goto god_i_hate_gotos;
1378 }
1379 if (tmp[i - 1] < 32) {
1380 tmp[--i] = '\0';
1381 }
1382 if (i < 2) {
1383 goto god_i_hate_gotos;
1384 }
[1662]1385 if (!does_file_exist(tmp) && !feof(fin)) {
1386 j++;
1387 fprintf(fout, "%s\n", tmp);
1388
[128]1389 goto god_i_hate_gotos;
1390 }
1391 filelist->el[filelist->entries].severity =
1392 severity_of_difference(tmp, NULL);
1393 strcpy(filelist->el[filelist->entries].filename, tmp);
1394 if (feof(fin)) {
1395 break;
1396 }
1397 }
1398 paranoid_fclose(fin);
[1662]1399 paranoid_fclose(fout);
1400 if (j > 0) {
1401 log_to_screen("%d files listed in %s/changed.files have been deleted since backup was made\nand are referenced in %s", j, bkpinfo->tmpdir,tmp1);
1402 }
1403 paranoid_free(tmp1);
[128]1404 if (filelist->entries >= ARBITRARY_MAXIMUM) {
[541]1405 log_to_screen("Arbitrary limits suck, man!");
[128]1406 paranoid_free(tmp);
1407 return (1);
1408 }
1409 for (done = FALSE; !done;) {
1410 done = TRUE;
1411 for (i = 0; i < filelist->entries - 1; i++) {
[1]1412// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
[128]1413 if (filelist->el[i].severity < filelist->el[i + 1].severity
1414 || (filelist->el[i].severity ==
1415 filelist->el[i + 1].severity
1416 && strcmp(filelist->el[i].filename,
1417 filelist->el[i + 1].filename) > 0)) {
1418 memcpy((void *) &dummy_fle,
1419 (void *) &(filelist->el[i]),
1420 sizeof(struct s_filelist_entry));
1421 memcpy((void *) &(filelist->el[i]),
1422 (void *) &(filelist->el[i + 1]),
1423 sizeof(struct s_filelist_entry));
1424 memcpy((void *) &(filelist->el[i + 1]),
1425 (void *) &dummy_fle,
1426 sizeof(struct s_filelist_entry));
1427 log_msg(2, "Swapping %s and %s",
1428 filelist->el[i].filename,
1429 filelist->el[i + 1].filename);
1430 done = FALSE;
1431 }
1432 }
1433 }
1434 paranoid_free(tmp);
[2230]1435 log_it("leaving");
[128]1436 return (0);
1437 }
[1]1438
1439
1440
1441/**
1442 * Generate a pretty string based on @p flentry.
1443 * @param flentry The filelist entry to stringify.
1444 * @return The string form of @p flentry.
1445 * @note The returned value points to static storage that will be overwritten with each call.
1446 */
[128]1447 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1448 static char comment[100];
[2211]1449 char *tmp = NULL;
[1]1450
[2230]1451 log_it("entering");
[128]1452 assert(flentry != NULL);
1453 if (flentry->severity == 0) {
[2323]1454 mr_asprintf(tmp, "0 ");
[128]1455 } else if (flentry->severity == 1) {
[2323]1456 mr_asprintf(tmp, "low ");
[128]1457 } else if (flentry->severity == 2) {
[2323]1458 mr_asprintf(tmp, "med ");
[128]1459 } else {
[2323]1460 mr_asprintf(tmp, "high");
[128]1461 }
[2211]1462 mr_strcat(tmp, " ");
1463 mr_strcat(tmp, flentry->filename);
1464 strncpy(comment, tmp, 99);
[128]1465 paranoid_free(tmp);
[2211]1466
[2230]1467 log_it("leaving");
[128]1468 return (comment);
1469 }
[1]1470
1471
1472
1473
1474
1475/**
1476 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1477 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1478 * @param source_file The file containing a list of changed files.
1479 */
[128]1480 void popup_changelist_from_file(char *source_file) {
1481 char *reason;
1482 newtComponent myForm;
1483 newtComponent bClose;
1484 newtComponent bSelect;
1485 newtComponent b_res;
1486 newtComponent fileListbox;
1487 newtComponent headerMsg;
[1]1488
[128]1489 /*@ ???? ************************************************************ */
1490 void *curr_choice;
1491 void *keylist[ARBITRARY_MAXIMUM];
[1]1492
[128]1493 /*@ int ************************************************************* */
1494 int currline = 0;
1495 int finished = FALSE;
[738]1496
1497 /*@ long ************************************************************ */
1498 long i = 0;
[128]1499 long lng = 0;
[1]1500
[128]1501 /*@ buffers ********************************************************* */
[2274]1502 char *tmp = NULL;
1503 char *differ_sz = NULL;
[1]1504
[128]1505 struct s_filelist *filelist;
1506 assert_string_is_neither_NULL_nor_zerolength(source_file);
1507 if (g_text_mode) {
1508 log_msg(2, "Text mode. Therefore, no popup list.");
[2274]1509 return;
[128]1510 }
1511 log_msg(2, "Examining file %s", source_file);
[1]1512
[128]1513 lng = count_lines_in_file(source_file);
1514 if (lng < 1) {
1515 log_msg(2, "No lines in file. Therefore, no popup list.");
[2274]1516 return;
[128]1517 } else if (lng >= ARBITRARY_MAXIMUM) {
1518 log_msg(2, "Too many files differ for me to list.");
[2274]1519 return;
[128]1520 }
[1]1521
[128]1522 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1523 fileListbox =
1524 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1525 newtListboxClear(fileListbox);
[1]1526
[128]1527 if (load_filelist_into_array(filelist, source_file)) {
[2274]1528 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
[128]1529 return;
1530 }
1531 log_msg(2, "%d files loaded into filelist array",
1532 filelist->entries);
1533 for (i = 0; i < filelist->entries; i++) {
1534 keylist[i] = (void *) i;
1535 newtListboxAppendEntry(fileListbox,
1536 filelist_entry_to_string(&
1537 (filelist->
1538 el[i])),
1539 keylist[i]);
1540 }
[2323]1541 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
[128]1542 newtPushHelpLine(differ_sz);
[2274]1543 mr_free(differ_sz);
1544
[541]1545 bClose = newtCompactButton(10, 15, " Close ");
1546 bSelect = newtCompactButton(30, 15, " Select ");
[2323]1547 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
[128]1548 headerMsg = newtLabel(2, 1, tmp);
[2274]1549 mr_free(tmp);
1550
[541]1551 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
[128]1552 myForm = newtForm(NULL, NULL, 0);
1553 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1554 bSelect, NULL);
[2274]1555
1556 malloc_string(reason);
[128]1557 while (!finished) {
1558 b_res = newtRunForm(myForm);
1559 if (b_res == bClose) {
1560 finished = TRUE;
1561 } else {
1562 curr_choice = newtListboxGetCurrent(fileListbox);
1563 for (i = 0;
1564 i < filelist->entries && keylist[i] != curr_choice;
1565 i++);
1566 if (i == filelist->entries && filelist->entries > 0) {
[541]1567 log_to_screen("I don't know what that button does!");
[128]1568 } else {
1569 currline = i;
1570 if (filelist->entries > 0) {
[2274]1571 severity_of_difference(filelist->el[currline]. filename, reason);
[2323]1572 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
[2274]1573
[128]1574 popup_and_OK(tmp);
[2274]1575 mr_free(tmp);
[128]1576 }
1577 }
1578 }
1579 }
[2274]1580 paranoid_free(reason);
1581
[128]1582 newtFormDestroy(myForm);
1583 newtPopWindow();
1584 newtPopHelpLine();
1585 return;
[1]1586 }
1587
1588/* @} - end of guiGroup */
1589
1590
1591#if __cplusplus
[128]1592} /* extern "C" */
[1]1593#endif
1594
1595
[128]1596void wait_until_software_raids_are_prepped(char *mdstat_file,
1597 int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.