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

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

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

  • Change mr_asprintf to avoid the need of pointer and be consistent with the other mr_mem functions
  • Property svn:keywords set to Id
File size: 44.3 KB
Line 
1/* newt-specific.c
2 $Id: newt-specific.c 2324 2009-08-18 13:13:54Z 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 2324 2009-08-18 13:13:54Z 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
1082 if (!g_text_mode) {
1083 assert(blurb1 != NULL);
1084 assert(blurb2 != NULL);
1085 assert(blurb3 != NULL);
1086 assert(g_timeline != NULL);
1087 }
1088
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) {
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);
1096 g_current_progress = g_maximum_progress;
1097 }
1098 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
1099 }
1100 if (percentage < 1) {
1101 percentage = 1;
1102 }
1103 if (percentage > 100) {
1104 percentage = 100;
1105 }
1106 if (g_current_progress) {
1107 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
1108 time_remaining = time_total_est - time_taken;
1109 } else {
1110 time_remaining = 0L;
1111 }
1112 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1113 if (g_text_mode) {
1114 printf("---progress-form---1--- %s\n", blurb1);
1115 printf("---progress-form---2--- %s\n", blurb2);
1116 printf("---progress-form---3--- %s\n", blurb3);
1117 printf("---progress-form---E---\n");
1118 mr_asprintf(taskprogress, "TASK: [");
1119 for (i = 0; i < percentage; i += 5) {
1120 mr_strcat(taskprogress, "*");
1121 }
1122 for (; i < 100; i += 5) {
1123 mr_strcat(taskprogress, ".");
1124 }
1125 if (percentage > 100) {
1126 log_msg(2, "percentage = %d", percentage);
1127 }
1128 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
1129 printf("---progress-form---4--- %s\n", taskprogress);
1130 paranoid_free(taskprogress);
1131 } else {
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);
1134
1135 center_string(blurb1, 54);
1136 center_string(blurb2, 54);
1137 center_string(blurb3, 54);
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 }
1147 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
1148 if ((percentage >= 2) && (g_timeline)) {
1149 newtLabelSetText(g_timeline, timeline_str);
1150 }
1151 mr_free(timeline_str);
1152
1153 if (g_percentline) {
1154 newtLabelSetText(g_percentline, percentline_str);
1155 }
1156 mr_free(percentline_str);
1157
1158 newtRefresh();
1159 }
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 */
1171 t_bkptype which_backup_media_type(bool restoring) {
1172
1173 /*@ char ************************************************************ */
1174 t_bkptype output;
1175
1176
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;
1188
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;
1199
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;) {
1205 printf("Backup type (");
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,
1227 "Please choose the backup media from which you want to read data.");
1228 strcpy(minimsg_sz, "Read from:");
1229 } else {
1230 strcpy(title_sz,
1231 "Please choose the backup media to which you want to archive data.");
1232 strcpy(minimsg_sz, "Backup to:");
1233 }
1234 newtPushHelpLine(title_sz);
1235 newtCenteredWindow(34, 17, minimsg_sz);
1236 b1 = newtButton(1, 1, "CD-R disks ");
1237 b2 = newtButton(17, 1, "CD-RW disks");
1238 b3 = newtButton(1, 9, "Tape drive ");
1239 b4 = newtButton(17, 5, "USB Key/Disk");
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 ");
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) {
1257 output = usb;
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 }
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 */
1282 int
1283 which_compression_level() {
1284
1285 /*@ char ************************************************************ */
1286 int output = none;
1287
1288
1289 /*@ newt ************************************************************ */
1290
1291 newtComponent b1;
1292 newtComponent b2;
1293 newtComponent b3;
1294 newtComponent b4;
1295 newtComponent b5;
1296 newtComponent b_res;
1297 newtComponent myForm;
1298
1299 newtDrawRootText(18, 0, WELCOME_STRING);
1300 newtPushHelpLine
1301 (" Please specify the level of compression that you want.");
1302 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
1303 newtCenteredWindow(34, 13, "How much compression?");
1304 b1 = newtButton(4, 1, "Maximum");
1305 b2 = newtButton(18, 1, "Average");
1306 b3 = newtButton(4, 5, "Minimum");
1307 b4 = newtButton(18, 5, " None ");
1308 b5 = newtButton(4, 9, " Exit ");
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 }
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 */
1339 int load_filelist_into_array(struct s_filelist *filelist,
1340 char *source_file) {
1341 int i;
1342 int j = 0;
1343 bool done;
1344 char *tmp;
1345 char *tmp1 = NULL;
1346 FILE *fin, *fout;
1347 struct s_filelist_entry dummy_fle;
1348
1349 assert(filelist != NULL);
1350 assert_string_is_neither_NULL_nor_zerolength(source_file);
1351
1352 log_it("entering");
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 }
1359
1360 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
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);
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 }
1385 if (!does_file_exist(tmp) && !feof(fin)) {
1386 j++;
1387 fprintf(fout, "%s\n", tmp);
1388
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);
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);
1404 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1405 log_to_screen("Arbitrary limits suck, man!");
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++) {
1412// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
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);
1435 log_it("leaving");
1436 return (0);
1437 }
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 */
1447 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1448 static char comment[100];
1449 char *tmp = NULL;
1450
1451 log_it("entering");
1452 assert(flentry != NULL);
1453 if (flentry->severity == 0) {
1454 mr_asprintf(tmp, "0 ");
1455 } else if (flentry->severity == 1) {
1456 mr_asprintf(tmp, "low ");
1457 } else if (flentry->severity == 2) {
1458 mr_asprintf(tmp, "med ");
1459 } else {
1460 mr_asprintf(tmp, "high");
1461 }
1462 mr_strcat(tmp, " ");
1463 mr_strcat(tmp, flentry->filename);
1464 strncpy(comment, tmp, 99);
1465 paranoid_free(tmp);
1466
1467 log_it("leaving");
1468 return (comment);
1469 }
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 */
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;
1488
1489 /*@ ???? ************************************************************ */
1490 void *curr_choice;
1491 void *keylist[ARBITRARY_MAXIMUM];
1492
1493 /*@ int ************************************************************* */
1494 int currline = 0;
1495 int finished = FALSE;
1496
1497 /*@ long ************************************************************ */
1498 long i = 0;
1499 long lng = 0;
1500
1501 /*@ buffers ********************************************************* */
1502 char *tmp = NULL;
1503 char *differ_sz = NULL;
1504
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.");
1509 return;
1510 }
1511 log_msg(2, "Examining file %s", source_file);
1512
1513 lng = count_lines_in_file(source_file);
1514 if (lng < 1) {
1515 log_msg(2, "No lines in file. Therefore, no popup list.");
1516 return;
1517 } else if (lng >= ARBITRARY_MAXIMUM) {
1518 log_msg(2, "Too many files differ for me to list.");
1519 return;
1520 }
1521
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);
1526
1527 if (load_filelist_into_array(filelist, source_file)) {
1528 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
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 }
1541 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
1542 newtPushHelpLine(differ_sz);
1543 mr_free(differ_sz);
1544
1545 bClose = newtCompactButton(10, 15, " Close ");
1546 bSelect = newtCompactButton(30, 15, " Select ");
1547 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
1548 headerMsg = newtLabel(2, 1, tmp);
1549 mr_free(tmp);
1550
1551 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1552 myForm = newtForm(NULL, NULL, 0);
1553 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1554 bSelect, NULL);
1555
1556 malloc_string(reason);
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) {
1567 log_to_screen("I don't know what that button does!");
1568 } else {
1569 currline = i;
1570 if (filelist->entries > 0) {
1571 severity_of_difference(filelist->el[currline]. filename, reason);
1572 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
1573
1574 popup_and_OK(tmp);
1575 mr_free(tmp);
1576 }
1577 }
1578 }
1579 }
1580 paranoid_free(reason);
1581
1582 newtFormDestroy(myForm);
1583 newtPopWindow();
1584 newtPopHelpLine();
1585 return;
1586 }
1587
1588/* @} - end of guiGroup */
1589
1590
1591#if __cplusplus
1592} /* extern "C" */
1593#endif
1594
1595
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.