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

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

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

  • Change mr_asprintf interface to pass only the char * (makes bkpinfo usage more easy)
  • Property svn:keywords set to Id
File size: 44.3 KB
Line 
1/* newt-specific.c
2 $Id: newt-specific.c 2323 2009-08-18 13:05:43Z bruno $
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"
21#include "mr_mem.h"
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"
29#include "libmondo-gui-EXT.h"
30#include "lib-common-externs.h"
31
32extern char *MONDO_LOGFILE;
33
34extern struct s_bkpinfo *bkpinfo;
35extern void mr_free_bkpinfo();
36
37/*@unused@*/
38//static char cvsid[] = "$Id: newt-specific.c 2323 2009-08-18 13:05:43Z bruno $";
39
40 extern pid_t g_mastermind_pid;
41 /*
42 extern char *g_tmpfs_mountpt;
43 */
44 extern char *g_boot_mountpt;
45 extern char *ps_options;
46 extern char *ps_proc_id;
47
48 extern void set_signals(int);
49
50/**
51 * @addtogroup globalGroup
52 * @{
53 */
54/**
55 * Whether we are currently in a nested call of fatal_error().
56 */
57 bool g_exiting = FALSE;
58
59/**
60 * Padding below the Newt components, to overcome bugs in Newt.
61 */
62 char g_haharrrrr[500];
63
64
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 ?????
73
74/**
75 * Padding above the Newt components, to overcome bugs in Newt.
76 */
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.
93
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.
99 long g_start_time = 0L; ///< The time (in seconds since the epoch) that the progress form was opened.
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.
105
106/* @} - end of globalGroup */
107
108//int g_fd_in=-1, g_fd_out=-1;
109
110 void popup_and_OK(char *);
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 */
122 bool ask_me_yes_or_no(char *prompt) {
123
124 /*@ buffers ********************************************************** */
125 int i;
126
127 assert_string_is_neither_NULL_nor_zerolength(prompt);
128
129 if (g_text_mode) {
130
131 char *tmp;
132 tmp = malloc(MAX_NEWT_COMMENT_LEN);
133
134 while (1) {
135 system("sync");
136 printf
137 ("---promptdialogYN---1--- %s\n---promptdialogYN---Q--- [yes] [no] ---\n--> ",
138 prompt);
139 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, stdin);
140 if (tmp[strlen(tmp) - 1] == '\n')
141 tmp[strlen(tmp) - 1] = '\0';
142
143 i = (int) strlen(tmp);
144 if (i > 0 && tmp[i - 1] < 32) {
145 tmp[i - 1] = '\0';
146 }
147 if (strstr("yesYES", tmp)) {
148 paranoid_free(tmp);
149 return (TRUE);
150 } else if (strstr("NOno", tmp)) {
151 paranoid_free(tmp);
152 return (FALSE);
153 } else {
154 system("sync");
155 printf
156 ("Please enter either YES or NO (or yes or no, or y or n, or...)\n");
157 }
158 }
159 } else {
160 return (popup_with_buttons(prompt, "Yes", "No"));
161 }
162 }
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 */
171 bool ask_me_OK_or_cancel(char *prompt) {
172
173 /*@ buffer *********************************************************** */
174 char *tmp;
175 int i;
176
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
182 ("---promptdialogOKC---1--- %s\n---promptdialogOKC---Q--- [OK] [Cancel] ---\n--> ",
183 prompt);
184 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, stdin);
185 if (tmp[strlen(tmp) - 1] == '\n')
186 tmp[strlen(tmp) - 1] = '\0';
187
188 i = (int) strlen(tmp);
189 if (i > 0 && tmp[i - 1] < 32) {
190 tmp[i - 1] = '\0';
191 }
192 if (strstr("okOKOkYESyes", tmp)) {
193 paranoid_free(tmp);
194 return (TRUE);
195 } else {
196 paranoid_free(tmp);
197 return (FALSE);
198 }
199 } else {
200 paranoid_free(tmp);
201 return (popup_with_buttons(prompt, " Okay ", "Cancel"));
202 }
203 }
204
205
206
207/**
208 * Close the currently opened evalcall form.
209 */
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();
227
228 /* Reset globals */
229 g_isoform_main = NULL;
230 g_isoform_old_progress = -1;
231 g_isoform_header = NULL;
232 g_isoform_scale = NULL;
233 g_isoform_timeline = NULL;
234 g_isoform_pcline = NULL;
235}
236
237/**
238 * Close the currently opened progress form.
239 */
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();
260
261 /* Reset globals */
262 g_progressForm = NULL;
263 g_current_progress = -999;
264 g_timeline = NULL;
265 g_percentline = NULL;
266 g_scale = NULL;
267
268 }
269
270/**
271 * Kill any process containing the string @p str surrounded by spaces in its commandline.
272 */
273void kill_anything_like_this(char *str) {
274
275char *tmp = NULL;
276char *tmp1 = NULL;
277
278 mr_asprintf(tmp,"ps %s | grep \" %s \" | awk '{print %s;}' | grep -v \"grep\"", ps_options, str , ps_proc_id);
279 run_program_and_log_output(tmp, TRUE);
280 if (strlen(tmp) > 0) {
281 mr_asprintf(tmp1,"kill `%s`", tmp);
282 run_program_and_log_output(tmp1, TRUE);
283 mr_free(tmp1);
284 }
285 mr_free(tmp);
286}
287
288
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 */
294 void
295 fatal_error(char *error_string) {
296 /*@ buffers ***************************************************** */
297 char *command = NULL;
298 static bool already_exiting = FALSE;
299
300 /*@ end vars **************************************************** */
301
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 }
311
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 }
319
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 }
325
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");
331 kill_anything_like_this("ntfsclone");
332 sync();
333
334 chdir("/");
335 if (g_selfmounted_isodir) {
336 mr_asprintf(command, "umount %s", g_selfmounted_isodir);
337 run_program_and_log_output(command, 5);
338 mr_free(command);
339 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
340 run_program_and_log_output(command, 5);
341 mr_free(command);
342 }
343
344 if (!g_text_mode) {
345 log_msg(0, "-------FATAL ERROR---------");
346 log_msg(0, error_string);
347 newtFinished();
348 }
349
350 printf("---FATALERROR--- %s\n", error_string);
351 printf("If you require technical support, please contact the mailing list.\n");
352 printf("See http://www.mondorescue.org for details.\n");
353 printf("The list's members can help you, if you attach that file to your e-mail.\n");
354 printf("Log file: %s\n", MONDO_LOGFILE);
355 printf("Mondo has aborted.\n");
356 if (!g_main_pid) {
357 log_msg(3, "FYI - g_main_pid is blank");
358 }
359 finish(254);
360 }
361
362/* Free memory allocated for newt */
363void free_newt_stuff() {
364
365int i;
366
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
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 */
381 void
382 finish(int signal) {
383 char *command = NULL;
384
385 chdir("/");
386 run_program_and_log_output("umount " MNT_CDROM, FALSE);
387 if (g_selfmounted_isodir) {
388 mr_asprintf(command, "umount %s", g_selfmounted_isodir);
389 run_program_and_log_output(command, 1);
390 mr_free(command);
391
392 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
393 run_program_and_log_output(command, 1);
394 mr_free(command);
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 }
405 printf("Execution run ended; result=%d\n", signal);
406 printf("Type 'less %s' to see the output log\n", MONDO_LOGFILE);
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");
411 mr_asprintf(command, "rm -Rf %s", bkpinfo->tmpdir);
412 system(command);
413 mr_free(command);
414 }
415 }
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");
420 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
421 system(command);
422 mr_free(command);
423 }
424 }
425 /* Free all allocated strings in bkpinfo */
426 mr_free_bkpinfo();
427 free_libmondo_global_strings();
428 free_newt_stuff();
429 exit(signal);
430 }
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 */
442 void
443 log_file_end_to_screen(char *filename, char *grep_for_me) {
444
445 /*@ buffers ********************************************************** */
446 char *command;
447 char *tmp;
448
449 /*@ pointers ********************************************************* */
450 FILE *fin;
451
452 /*@ int ************************************************************** */
453 int i = 0;
454
455 malloc_string(command);
456 malloc_string(tmp);
457 assert_string_is_neither_NULL_nor_zerolength(filename);
458 assert(grep_for_me != NULL);
459
460 if (!does_file_exist(filename)) {
461 paranoid_free(command);
462 paranoid_free(tmp);
463 return;
464 }
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 {
469 sprintf(command, "tail -n%d %s", g_noof_log_lines,
470 filename);
471 }
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);
481 /* Commented to make valgrind happy and avoid crash
482 strip_spaces(err_log_lines[i]);
483 */
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);
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 */
506 void
507 log_to_screen(const char *fmt, ...) {
508
509 /*@ int ************************************************************** */
510 int i = 0;
511 int j = 0;
512 va_list args;
513
514 /*@ buffers ********************************************************** */
515 char *output;
516
517 malloc_string(output);
518
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 }
528
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);
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 */
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 }
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 */
582 void
583 open_evalcall_form(char *ttl) {
584
585 /*@ buffers ********************************************************* */
586 char *title;
587 char *tmp;
588
589 /*@ initialize ****************************************************** */
590 g_isoform_old_progress = -1;
591 g_mysterious_dot_counter = 0;
592 malloc_string(title);
593 malloc_string(tmp);
594
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 }
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 */
637 void
638 open_progress_form(char *title, char *b1, char *b2, char *b3,
639 long max_val) {
640
641 /*@ buffers ********************************************************* */
642 char *b1c;
643 char *blurb1;
644 char *blurb2;
645 char *blurb3;
646
647 /*@ initialize ****************************************************** */
648 g_mysterious_dot_counter = 0;
649
650 malloc_string(b1c);
651 malloc_string(blurb1);
652 malloc_string(blurb2);
653 malloc_string(blurb3);
654
655 assert(title != NULL);
656 assert(b1 != NULL);
657 assert(b2 != NULL);
658 assert(b3 != NULL);
659
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 }
668
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
702/**
703 * Give a message to the user in the form of a dialog box (under Newt).
704 * @param prompt The message.
705 */
706 void
707 popup_and_OK(char *prompt) {
708 char ch;
709
710 assert_string_is_neither_NULL_nor_zerolength(prompt);
711
712 log_msg(0, prompt);
713 if (g_text_mode) {
714 printf
715 ("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ",
716 prompt);
717 while (((ch = getchar()) != '\n') && (ch != EOF));
718 } else {
719 (void) popup_with_buttons(prompt, " OK ", "");
720 }
721 }
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).
727 * @param input The string given originaly as default
728 * @return output string if the user pressed OK, NULL if they pressed Cancel.
729 */
730 char *popup_and_get_string(char *title, char *b, char *input) {
731
732 /*@ newt ************************************************************ */
733 newtComponent myForm;
734 newtComponent b_1;
735 newtComponent b_2;
736 newtComponent b_res;
737 newtComponent text;
738 newtComponent type_here;
739
740 /*@ pointers ********************************************************* */
741 char *entry_value;
742
743 /*@ buffers ********************************************************** */
744 char *blurb = NULL;
745 char *output = NULL;
746
747 assert_string_is_neither_NULL_nor_zerolength(title);
748 assert(b != NULL);
749
750 if (g_text_mode) {
751 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q--- [%s]\n--> ", title, b, input);
752 mr_getline(&output, stdin);
753 if (! strcmp(output,"\n")) {
754 /* take default if ENTER was typed */
755 mr_free(output);
756 mr_asprintf(output, "%s", input);
757 }
758 if (output[strlen(output) - 1] == '\n')
759 output[strlen(output) - 1] = '\0';
760 return (output);
761 }
762
763 mr_asprintf(blurb, "%s", b);
764 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
765 type_here =
766 newtEntry(2, newtTextboxGetNumLines(text) + 2,
767 input, 50,
768#ifdef __cplusplus
769 0, NEWT_FLAG_RETURNEXIT
770#else
771 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
772#endif
773 );
774 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
775 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
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);
782
783 if (b_res == b_1) {
784 mr_asprintf(output, "%s", newtEntryGetValue(type_here));
785 }
786 newtPopHelpLine();
787 newtFormDestroy(myForm);
788 newtPopWindow();
789 mr_free(blurb);
790
791 return (output);
792 }
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 */
802 bool popup_with_buttons(char *p, char *button1, char *button2) {
803
804 /*@ buffers *********************************************************** */
805 char *prompt;
806
807 /*@ newt ************************************************************** */
808 newtComponent myForm;
809 newtComponent b_1;
810 newtComponent b_2;
811 newtComponent b_res;
812 newtComponent text;
813
814 assert_string_is_neither_NULL_nor_zerolength(p);
815 assert(button1 != NULL);
816 assert(button2 != NULL);
817 prompt = malloc(MAX_NEWT_COMMENT_LEN);
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 }
824 for (prompt[0] = '\0';
825 strcmp(prompt, button1) && (strlen(button2) == 0
826 || strcmp(prompt, button2));) {
827 printf("--> ");
828 (void) fgets(prompt, MAX_NEWT_COMMENT_LEN, stdin);
829 }
830 if (!strcmp(prompt, button1)) {
831 paranoid_free(prompt);
832 return (TRUE);
833 } else {
834 paranoid_free(prompt);
835 return (FALSE);
836 }
837 }
838
839 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
840 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
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");
855 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
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 }
872
873
874
875
876/**
877 * Synchronize the log messages stored in @p err_log_lines with those shown
878 * on the screen.
879 */
880 void
881 refresh_log_screen() {
882
883 /*@ int *********************************************************** */
884 int i = 0;
885
886
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 }
902
903
904/**
905 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
906 * only allocate some memory.
907 */
908 void
909 setup_newt_stuff() {
910
911 /*@ int *********************************************************** */
912 int i = 0;
913 int cols;
914
915 if (!g_text_mode) {
916 newtInit();
917 newtCls();
918 newtPushHelpLine("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
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 }
924
925 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
926
927 for (i = 0; i < g_noof_log_lines; i++) {
928 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
929 }
930
931 for (i = 0; i < g_noof_log_lines; i++) {
932 err_log_lines[i][0] = '\0';
933 }
934 }
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 */
942 void
943 update_evalcall_form_ratio(int num, int denom) {
944
945 /*@ long ************************************************************ */
946 long current_time = 0L;
947 long time_taken = 0L;
948 long time_total_est = 0L;
949 long time_remaining = 0L;
950
951 /*@ buffers ********************************************************** */
952 char *timeline_str = NULL;
953 char *pcline_str = NULL;
954 char *taskprogress = NULL;
955
956 /*@ int ************************************************************** */
957 int percentage = 0;
958 int i = 0;
959 int j = 0;
960
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 {
973 time_remaining = 0L;
974 }
975 if (!g_text_mode) {
976 if (g_isoform_header) {
977 newtLabelSetText(g_isoform_header, g_isoform_header_str);
978 }
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;
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);
985 if (percentage < 3) {
986 mr_asprintf(pcline_str, " Working");
987 for (j = 0; j < g_mysterious_dot_counter; j++) {
988 mr_strcat(pcline_str, ".");
989 }
990 for (; j < 27; j++) {
991 mr_strcat(pcline_str, " ");
992 }
993 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
994 } else {
995 mr_asprintf(pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
996 }
997 if (g_text_mode) {
998 mr_asprintf(taskprogress, "TASK: [");
999 for (i = 0; i < percentage; i += 5) {
1000 mr_strcat(taskprogress, "*");
1001 }
1002 for (; i < 100; i += 5) {
1003 mr_strcat(taskprogress, ".");
1004 }
1005 if (percentage >= 3) {
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);
1008 printf("---evalcall---2--- %s\n", taskprogress);
1009 printf("---evalcall---E---\n");
1010 }
1011 mr_free(taskprogress);
1012 } else {
1013 newtScaleSet(g_isoform_scale,
1014 (unsigned long long) percentage);
1015 if (g_isoform_pcline) {
1016 newtLabelSetText(g_isoform_pcline, pcline_str);
1017 }
1018 if ((percentage >= 3) && (g_isoform_timeline)) {
1019 newtLabelSetText(g_isoform_timeline, timeline_str);
1020 }
1021 }
1022 mr_free(pcline_str);
1023 mr_free(timeline_str);
1024 }
1025 if (!g_text_mode) {
1026 newtRefresh();
1027 }
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 */
1036 void
1037 update_evalcall_form(int curr) {
1038 update_evalcall_form_ratio(curr, 100);
1039 }
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 */
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 }
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 */
1065 void
1066 update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1067 /*@ long ***************************************************** */
1068 long current_time = 0L;
1069 long time_taken = 0L;
1070 long time_remaining = 0L;
1071 long time_total_est = 0L;
1072
1073 /*@ int ******************************************************* */
1074 int percentage = 0;
1075 int i = 0;
1076
1077 /*@ buffers *************************************************** */
1078 char *percentline_str = NULL;
1079 char *timeline_str = NULL;
1080 char *taskprogress = NULL;
1081 char *tmp = NULL;
1082
1083 if (!g_text_mode) {
1084 assert(blurb1 != NULL);
1085 assert(blurb2 != NULL);
1086 assert(blurb3 != NULL);
1087 assert(g_timeline != NULL);
1088 }
1089
1090 current_time = get_time();
1091 time_taken = current_time - g_start_time;
1092 if (g_maximum_progress == 0) {
1093 percentage = 0;
1094 } else {
1095 if (g_current_progress > g_maximum_progress) {
1096 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);
1097 g_current_progress = g_maximum_progress;
1098 }
1099 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
1100 }
1101 if (percentage < 1) {
1102 percentage = 1;
1103 }
1104 if (percentage > 100) {
1105 percentage = 100;
1106 }
1107 if (g_current_progress) {
1108 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
1109 time_remaining = time_total_est - time_taken;
1110 } else {
1111 time_remaining = 0L;
1112 }
1113 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1114 if (g_text_mode) {
1115 printf("---progress-form---1--- %s\n", blurb1);
1116 printf("---progress-form---2--- %s\n", blurb2);
1117 printf("---progress-form---3--- %s\n", blurb3);
1118 printf("---progress-form---E---\n");
1119 mr_asprintf(taskprogress, "TASK: [");
1120 for (i = 0; i < percentage; i += 5) {
1121 mr_strcat(taskprogress, "*");
1122 }
1123 for (; i < 100; i += 5) {
1124 mr_strcat(taskprogress, ".");
1125 }
1126 if (percentage > 100) {
1127 log_msg(2, "percentage = %d", percentage);
1128 }
1129 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
1130 printf("---progress-form---4--- %s\n", taskprogress);
1131 paranoid_free(taskprogress);
1132 } else {
1133 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);
1134 mr_asprintf(percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
1135
1136 center_string(blurb1, 54);
1137 center_string(blurb2, 54);
1138 center_string(blurb3, 54);
1139 if (g_blurb1) {
1140 newtLabelSetText(g_blurb1, blurb1);
1141 }
1142 if (g_blurb2) {
1143 newtLabelSetText(g_blurb2, blurb3);
1144 }
1145 if (g_blurb3) {
1146 newtLabelSetText(g_blurb3, blurb2);
1147 }
1148 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
1149 if ((percentage >= 2) && (g_timeline)) {
1150 newtLabelSetText(g_timeline, timeline_str);
1151 }
1152 mr_free(timeline_str);
1153
1154 if (g_percentline) {
1155 newtLabelSetText(g_percentline, percentline_str);
1156 }
1157 mr_free(percentline_str);
1158
1159 newtRefresh();
1160 }
1161 }
1162
1163
1164/**
1165 * Ask the user which backup media type they would like to use.
1166 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1167 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs,
1168 * and @c iso.
1169 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1170 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1171 */
1172 t_bkptype which_backup_media_type(bool restoring) {
1173
1174 /*@ char ************************************************************ */
1175 t_bkptype output;
1176
1177
1178 /*@ newt ************************************************************ */
1179 char *title_sz;
1180 char *minimsg_sz;
1181 static t_bkptype possible_bkptypes[] =
1182 { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso };
1183 static char *possible_responses[] =
1184 { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev",
1185"nfs", "iso", NULL };
1186 char *outstr;
1187 t_bkptype backup_type;
1188 int i;
1189
1190 newtComponent b1;
1191 newtComponent b2;
1192 newtComponent b3;
1193 newtComponent b4;
1194 newtComponent b5;
1195 newtComponent b6;
1196 newtComponent b7;
1197 newtComponent b8;
1198 newtComponent b_res;
1199 newtComponent myForm;
1200
1201 title_sz = malloc(MAX_NEWT_COMMENT_LEN);
1202 minimsg_sz = malloc(MAX_NEWT_COMMENT_LEN);
1203 outstr = malloc(MAX_NEWT_COMMENT_LEN);
1204 if (g_text_mode) {
1205 for (backup_type = none; backup_type == none;) {
1206 printf("Backup type (");
1207 for (i = 0; possible_responses[i]; i++) {
1208 printf("%c%s", (i == 0) ? '\0' : ' ',
1209 possible_responses[i]);
1210 }
1211 printf(")\n--> ");
1212 (void) fgets(outstr, MAX_NEWT_COMMENT_LEN, stdin);
1213 strip_spaces(outstr);
1214 for (i = 0; possible_responses[i]; i++) {
1215 if (!strcmp(possible_responses[i], outstr)) {
1216 backup_type = possible_bkptypes[i];
1217 }
1218 }
1219 }
1220 paranoid_free(title_sz);
1221 paranoid_free(minimsg_sz);
1222 paranoid_free(outstr);
1223 return (backup_type);
1224 }
1225 newtDrawRootText(18, 0, WELCOME_STRING);
1226 if (restoring) {
1227 strcpy(title_sz,
1228 "Please choose the backup media from which you want to read data.");
1229 strcpy(minimsg_sz, "Read from:");
1230 } else {
1231 strcpy(title_sz,
1232 "Please choose the backup media to which you want to archive data.");
1233 strcpy(minimsg_sz, "Backup to:");
1234 }
1235 newtPushHelpLine(title_sz);
1236 newtCenteredWindow(34, 17, minimsg_sz);
1237 b1 = newtButton(1, 1, "CD-R disks ");
1238 b2 = newtButton(17, 1, "CD-RW disks");
1239 b3 = newtButton(1, 9, "Tape drive ");
1240 b4 = newtButton(17, 5, "USB Key/Disk");
1241 b5 = newtButton(1, 5, " DVD disks ");
1242 b6 = newtButton(17, 9, " NFS mount ");
1243 b7 = newtButton(1, 13, " Hard disk ");
1244 b8 = newtButton(17, 13, " Exit ");
1245 myForm = newtForm(NULL, NULL, 0);
1246 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1247 NULL);
1248 b_res = newtRunForm(myForm);
1249 newtFormDestroy(myForm);
1250 newtPopWindow();
1251 if (b_res == b1) {
1252 output = cdr;
1253 } else if (b_res == b2) {
1254 output = cdrw;
1255 } else if (b_res == b3) {
1256 output = tape;
1257 } else if (b_res == b4) {
1258 output = usb;
1259 } else if (b_res == b5) {
1260 output = dvd;
1261 } else if (b_res == b6) {
1262 output = nfs;
1263 } else if (b_res == b7) {
1264 output = iso;
1265 } else {
1266 output = none;
1267 }
1268 newtPopHelpLine();
1269 paranoid_free(title_sz);
1270 paranoid_free(minimsg_sz);
1271 paranoid_free(outstr);
1272 return (output);
1273 }
1274
1275
1276
1277
1278/**
1279 * Ask the user how much compression they would like to use.
1280 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1281 * @return The compression level (0-9) chosen, or -1 for "Exit".
1282 */
1283 int
1284 which_compression_level() {
1285
1286 /*@ char ************************************************************ */
1287 int output = none;
1288
1289
1290 /*@ newt ************************************************************ */
1291
1292 newtComponent b1;
1293 newtComponent b2;
1294 newtComponent b3;
1295 newtComponent b4;
1296 newtComponent b5;
1297 newtComponent b_res;
1298 newtComponent myForm;
1299
1300 newtDrawRootText(18, 0, WELCOME_STRING);
1301 newtPushHelpLine
1302 (" Please specify the level of compression that you want.");
1303 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
1304 newtCenteredWindow(34, 13, "How much compression?");
1305 b1 = newtButton(4, 1, "Maximum");
1306 b2 = newtButton(18, 1, "Average");
1307 b3 = newtButton(4, 5, "Minimum");
1308 b4 = newtButton(18, 5, " None ");
1309 b5 = newtButton(4, 9, " Exit ");
1310 myForm = newtForm(NULL, NULL, 0);
1311 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1312 b_res = newtRunForm(myForm);
1313 newtFormDestroy(myForm);
1314 newtPopWindow();
1315 if (b_res == b1) {
1316 output = 9;
1317 } else if (b_res == b2) {
1318 output = 4;
1319 } else if (b_res == b3) {
1320 output = 1;
1321 } else if (b_res == b4) {
1322 output = 0;
1323 } else if (b_res == b5) {
1324 output = -1;
1325 }
1326 newtPopHelpLine();
1327 return (output);
1328 }
1329
1330
1331
1332
1333
1334/**
1335 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1336 * @p ARBITRARY_MAXIMUM entries.
1337 * @param filelist The filelist structure to load @p source_file into.
1338 * @param source_file The file containing a list of filenames to load into @p filelist.
1339 */
1340 int load_filelist_into_array(struct s_filelist *filelist,
1341 char *source_file) {
1342 int i;
1343 int j = 0;
1344 bool done;
1345 char *tmp;
1346 char *tmp1 = NULL;
1347 FILE *fin, *fout;
1348 struct s_filelist_entry dummy_fle;
1349
1350 assert(filelist != NULL);
1351 assert_string_is_neither_NULL_nor_zerolength(source_file);
1352
1353 log_it("entering");
1354 if (!(fin = fopen(source_file, "r"))) {
1355 log_OS_error(source_file);
1356 log_msg(2, "Can't open %s; therefore, cannot popup list",
1357 source_file);
1358 return (1);
1359 }
1360
1361 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
1362 if (!(fout = fopen(tmp1, "a"))) {
1363 log_msg(2, "Can't write to %s", tmp1);
1364 return(1);
1365 }
1366
1367 malloc_string(tmp);
1368 log_msg(2, "Loading %s", source_file);
1369 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM;
1370 filelist->entries++) {
1371 god_i_hate_gotos:
1372 if (feof(fin)) {
1373 break;
1374 }
1375 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, fin);
1376 i = (int) strlen(tmp);
1377 if (i < 2) {
1378 goto god_i_hate_gotos;
1379 }
1380 if (tmp[i - 1] < 32) {
1381 tmp[--i] = '\0';
1382 }
1383 if (i < 2) {
1384 goto god_i_hate_gotos;
1385 }
1386 if (!does_file_exist(tmp) && !feof(fin)) {
1387 j++;
1388 fprintf(fout, "%s\n", tmp);
1389
1390 goto god_i_hate_gotos;
1391 }
1392 filelist->el[filelist->entries].severity =
1393 severity_of_difference(tmp, NULL);
1394 strcpy(filelist->el[filelist->entries].filename, tmp);
1395 if (feof(fin)) {
1396 break;
1397 }
1398 }
1399 paranoid_fclose(fin);
1400 paranoid_fclose(fout);
1401 if (j > 0) {
1402 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);
1403 }
1404 paranoid_free(tmp1);
1405 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1406 log_to_screen("Arbitrary limits suck, man!");
1407 paranoid_free(tmp);
1408 return (1);
1409 }
1410 for (done = FALSE; !done;) {
1411 done = TRUE;
1412 for (i = 0; i < filelist->entries - 1; i++) {
1413// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
1414 if (filelist->el[i].severity < filelist->el[i + 1].severity
1415 || (filelist->el[i].severity ==
1416 filelist->el[i + 1].severity
1417 && strcmp(filelist->el[i].filename,
1418 filelist->el[i + 1].filename) > 0)) {
1419 memcpy((void *) &dummy_fle,
1420 (void *) &(filelist->el[i]),
1421 sizeof(struct s_filelist_entry));
1422 memcpy((void *) &(filelist->el[i]),
1423 (void *) &(filelist->el[i + 1]),
1424 sizeof(struct s_filelist_entry));
1425 memcpy((void *) &(filelist->el[i + 1]),
1426 (void *) &dummy_fle,
1427 sizeof(struct s_filelist_entry));
1428 log_msg(2, "Swapping %s and %s",
1429 filelist->el[i].filename,
1430 filelist->el[i + 1].filename);
1431 done = FALSE;
1432 }
1433 }
1434 }
1435 paranoid_free(tmp);
1436 log_it("leaving");
1437 return (0);
1438 }
1439
1440
1441
1442/**
1443 * Generate a pretty string based on @p flentry.
1444 * @param flentry The filelist entry to stringify.
1445 * @return The string form of @p flentry.
1446 * @note The returned value points to static storage that will be overwritten with each call.
1447 */
1448 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1449 static char comment[100];
1450 char *tmp = NULL;
1451
1452 log_it("entering");
1453 assert(flentry != NULL);
1454 if (flentry->severity == 0) {
1455 mr_asprintf(tmp, "0 ");
1456 } else if (flentry->severity == 1) {
1457 mr_asprintf(tmp, "low ");
1458 } else if (flentry->severity == 2) {
1459 mr_asprintf(tmp, "med ");
1460 } else {
1461 mr_asprintf(tmp, "high");
1462 }
1463 mr_strcat(tmp, " ");
1464 mr_strcat(tmp, flentry->filename);
1465 strncpy(comment, tmp, 99);
1466 paranoid_free(tmp);
1467
1468 log_it("leaving");
1469 return (comment);
1470 }
1471
1472
1473
1474
1475
1476/**
1477 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1478 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1479 * @param source_file The file containing a list of changed files.
1480 */
1481 void popup_changelist_from_file(char *source_file) {
1482 char *reason;
1483 newtComponent myForm;
1484 newtComponent bClose;
1485 newtComponent bSelect;
1486 newtComponent b_res;
1487 newtComponent fileListbox;
1488 newtComponent headerMsg;
1489
1490 /*@ ???? ************************************************************ */
1491 void *curr_choice;
1492 void *keylist[ARBITRARY_MAXIMUM];
1493
1494 /*@ int ************************************************************* */
1495 int currline = 0;
1496 int finished = FALSE;
1497
1498 /*@ long ************************************************************ */
1499 long i = 0;
1500 long lng = 0;
1501
1502 /*@ buffers ********************************************************* */
1503 char *tmp = NULL;
1504 char *differ_sz = NULL;
1505
1506 struct s_filelist *filelist;
1507 assert_string_is_neither_NULL_nor_zerolength(source_file);
1508 if (g_text_mode) {
1509 log_msg(2, "Text mode. Therefore, no popup list.");
1510 return;
1511 }
1512 log_msg(2, "Examining file %s", source_file);
1513
1514 lng = count_lines_in_file(source_file);
1515 if (lng < 1) {
1516 log_msg(2, "No lines in file. Therefore, no popup list.");
1517 return;
1518 } else if (lng >= ARBITRARY_MAXIMUM) {
1519 log_msg(2, "Too many files differ for me to list.");
1520 return;
1521 }
1522
1523 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1524 fileListbox =
1525 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1526 newtListboxClear(fileListbox);
1527
1528 if (load_filelist_into_array(filelist, source_file)) {
1529 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
1530 return;
1531 }
1532 log_msg(2, "%d files loaded into filelist array",
1533 filelist->entries);
1534 for (i = 0; i < filelist->entries; i++) {
1535 keylist[i] = (void *) i;
1536 newtListboxAppendEntry(fileListbox,
1537 filelist_entry_to_string(&
1538 (filelist->
1539 el[i])),
1540 keylist[i]);
1541 }
1542 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
1543 newtPushHelpLine(differ_sz);
1544 mr_free(differ_sz);
1545
1546 bClose = newtCompactButton(10, 15, " Close ");
1547 bSelect = newtCompactButton(30, 15, " Select ");
1548 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
1549 headerMsg = newtLabel(2, 1, tmp);
1550 mr_free(tmp);
1551
1552 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1553 myForm = newtForm(NULL, NULL, 0);
1554 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1555 bSelect, NULL);
1556
1557 malloc_string(reason);
1558 while (!finished) {
1559 b_res = newtRunForm(myForm);
1560 if (b_res == bClose) {
1561 finished = TRUE;
1562 } else {
1563 curr_choice = newtListboxGetCurrent(fileListbox);
1564 for (i = 0;
1565 i < filelist->entries && keylist[i] != curr_choice;
1566 i++);
1567 if (i == filelist->entries && filelist->entries > 0) {
1568 log_to_screen("I don't know what that button does!");
1569 } else {
1570 currline = i;
1571 if (filelist->entries > 0) {
1572 severity_of_difference(filelist->el[currline]. filename, reason);
1573 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
1574
1575 popup_and_OK(tmp);
1576 mr_free(tmp);
1577 }
1578 }
1579 }
1580 }
1581 paranoid_free(reason);
1582
1583 newtFormDestroy(myForm);
1584 newtPopWindow();
1585 newtPopHelpLine();
1586 return;
1587 }
1588
1589/* @} - end of guiGroup */
1590
1591
1592#if __cplusplus
1593} /* extern "C" */
1594#endif
1595
1596
1597void wait_until_software_raids_are_prepped(char *mdstat_file,
1598 int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.