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

Last change on this file since 2340 was 2340, checked in by Bruno Cornec, 15 years ago
  • New compiler flogs introduce for memory check
  • Other dyn. allocation added replacing static ones
  • Option -Y is added for LZMA support
  • Property svn:keywords set to Id
File size: 43.9 KB
Line 
1/* newt-specific.c
2 $Id: newt-specific.c 2340 2009-08-20 23:53:49Z 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 2340 2009-08-20 23:53:49Z 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 }
348
349 printf("---FATALERROR--- %s\n", error_string);
350 printf("If you require technical support, please contact the mailing list.\n");
351 printf("See http://www.mondorescue.org for details.\n");
352 printf("The list's members can help you, if you attach that file to your e-mail.\n");
353 printf("Log file: %s\n", MONDO_LOGFILE);
354 printf("Mondo has aborted.\n");
355 if (!g_main_pid) {
356 log_msg(3, "FYI - g_main_pid is blank");
357 }
358 finish(254);
359 }
360
361/* Free memory allocated for newt */
362void free_newt_stuff() {
363
364int i;
365
366for (i = 0; i < g_noof_log_lines; i++) {
367 mr_free(err_log_lines[i]);
368}
369mr_free(err_log_lines);
370}
371
372
373
374/**
375 * Exit Mondo normally.
376 * @param signal The exit code (0 indicates a successful backup; 1 for Mondo means the
377 * user aborted; 254 means a fatal error occured).
378 * @note This function never returns.
379 */
380 void finish(int signal) {
381
382 char *command = NULL;
383
384 chdir("/");
385 run_program_and_log_output("umount " MNT_CDROM, FALSE);
386 if (g_selfmounted_isodir) {
387 mr_asprintf(command, "umount %s", g_selfmounted_isodir);
388 run_program_and_log_output(command, 1);
389 mr_free(command);
390
391 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
392 run_program_and_log_output(command, 1);
393 mr_free(command);
394 }
395 if (bkpinfo->tmpdir) {
396 log_msg(8,"tempdir is %s",bkpinfo->tmpdir);
397 if (strstr(bkpinfo->tmpdir ,"mondo.tmp.") != NULL) {
398 log_msg(8,"erasing tempdir");
399 mr_asprintf(command, "rm -Rf %s", bkpinfo->tmpdir);
400 system(command);
401 mr_free(command);
402 }
403 }
404 if (bkpinfo->scratchdir) {
405 log_msg(8,"scratchdir is %s", bkpinfo->scratchdir);
406 if (strstr(bkpinfo->scratchdir ,"mondo.scratch.") != NULL) {
407 log_msg(8,"erasing scratchdir");
408 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
409 system(command);
410 mr_free(command);
411 }
412 }
413 /* Free all allocated strings in bkpinfo */
414 mr_free_bkpinfo();
415 free_libmondo_global_strings();
416 free_newt_stuff();
417 if (!g_text_mode) {
418 log_msg(1, "Calling newtFinished()");
419 newtFinished();
420 }
421 printf("Execution run ended; result=%d\n", signal);
422 printf("Type 'less %s' to see the output log\n", MONDO_LOGFILE);
423 exit(signal);
424 }
425
426
427/**
428 * Log the last @p g_noof_log_lines lines of @p filename that match @p
429 * grep_for_me to the screen.
430 * @param filename The file to give the end of.
431 * @param grep_for_me If not "", then only give lines in @p filename that match this regular expression.
432 */
433 void
434 log_file_end_to_screen(char *filename, char *grep_for_me) {
435
436 /*@ buffers ********************************************************** */
437 char *command = NULL;
438 char *tmp = NULL;
439
440 /*@ pointers ********************************************************* */
441 FILE *fin;
442
443 /*@ int ************************************************************** */
444 int i = 0;
445
446 assert_string_is_neither_NULL_nor_zerolength(filename);
447 assert(grep_for_me != NULL);
448
449 if (!does_file_exist(filename)) {
450 return;
451 }
452 if (grep_for_me[0] != '\0') {
453 mr_asprintf(command, "grep '%s' %s | tail -n%d", grep_for_me, filename, g_noof_log_lines);
454 } else {
455 mr_asprintf(command, "tail -n%d %s", g_noof_log_lines, filename);
456 }
457 fin = popen(command, "r");
458 if (!fin) {
459 log_OS_error(command);
460 } else {
461 for (i = 0; i < g_noof_log_lines; i++) {
462 for (err_log_lines[i][0] = '\0';
463 strlen(err_log_lines[i]) < 2 && !feof(fin);) {
464 (void) fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN,
465 fin);
466 /* Commented to make valgrind happy and avoid crash
467 strip_spaces(err_log_lines[i]);
468 */
469 if (!strncmp(err_log_lines[i], "root:", 5)) {
470 mr_asprintf(tmp, "%s", err_log_lines[i] + 6);
471 strcpy(err_log_lines[i], tmp);
472 mr_free(tmp);
473 }
474 if (feof(fin)) {
475 break;
476 }
477 }
478 }
479 paranoid_pclose(fin);
480 }
481 mr_free(command);
482 refresh_log_screen();
483 }
484
485
486/**
487 * Log a message to the screen.
488 * @param fmt A printf-style format string to write. The following parameters are its arguments.
489 * @note The message is also written to the logfile.
490 */
491 void
492 log_to_screen(const char *fmt, ...) {
493
494 /*@ int ************************************************************** */
495 int i = 0;
496 int j = 0;
497 va_list args;
498
499 /*@ buffers ********************************************************** */
500 char *output;
501
502 malloc_string(output);
503
504 va_start(args, fmt);
505 vsprintf(output, fmt, args);
506 log_msg(0, output);
507 output[80] = '\0';
508 va_end(args);
509 i = (int) strlen(output);
510 if (i > 0 && output[i - 1] < 32) {
511 output[i - 1] = '\0';
512 }
513
514 if (err_log_lines) {
515 for (i = 1; i < g_noof_log_lines; i++) {
516 strcpy(err_log_lines[i - 1],
517 " ");
518 strcpy(err_log_lines[i - 1], err_log_lines[i]);
519 }
520 }
521 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
522 output[strlen(output) - 1] = '\0';
523 }
524 for (j = 0; j < (int) strlen(output); j++) {
525 if (output[j] < 32) {
526 output[j] = ' ';
527 }
528 }
529 if (err_log_lines)
530 strcpy(err_log_lines[g_noof_log_lines - 1], output);
531 if (g_text_mode) {
532 printf("%s\n", output);
533 } else {
534 refresh_log_screen();
535 }
536 paranoid_free(output);
537 }
538
539
540
541
542/**
543 * Write a string to the root window at (@p x, @p y) and also to the logfile.
544 * @param y The row to write the string to.
545 * @param x The column to write the string to.
546 * @param output The string to write.
547 */
548 void
549 mvaddstr_and_log_it(int y, int x, char *output) {
550 assert_string_is_neither_NULL_nor_zerolength(output);
551 log_msg(0, output);
552 if (g_text_mode) {
553 printf("%s\n", output);
554 } else {
555 newtDrawRootText(x, y, output);
556 newtRefresh();
557 }
558 }
559
560
561
562
563/**
564 * Open an evalcall form with title @p ttl.
565 * @param ttl The title to use for the evalcall form.
566 */
567 void
568 open_evalcall_form(char *ttl) {
569
570 /*@ buffers ********************************************************* */
571 char *title = NULL;
572 char *tmp = NULL;
573
574 /*@ initialize ****************************************************** */
575 g_isoform_old_progress = -1;
576 g_mysterious_dot_counter = 0;
577
578 assert(ttl != NULL);
579 malloc_string(tmp);
580 mr_asprintf(title, "%s", ttl);
581 strcpy(g_isoform_header_str, title);
582 if (g_text_mode) {
583 log_msg(0, title);
584 } else {
585 strcpy(tmp, title);
586 center_string(tmp, 80);
587 newtPushHelpLine(tmp);
588 }
589 center_string(g_isoform_header_str, 36);
590 g_isoform_starttime = get_time();
591 if (g_text_mode) {
592 log_msg(0, g_isoform_header_str);
593 } else {
594 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
595 g_isoform_scale = newtScale(3, 3, 34, 100);
596 newtCenteredWindow(40, 7, title);
597 g_isoform_main = newtForm(NULL, NULL, 0);
598 g_isoform_timeline = newtLabel(1, 5, "This is the timeline");
599 g_isoform_pcline = newtLabel(1, 6, "This is the pcline");
600 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
601 g_isoform_pcline, g_isoform_header,
602 g_isoform_scale, NULL);
603 newtRefresh();
604 }
605 update_evalcall_form(0);
606 mr_free(title);
607 paranoid_free(tmp);
608 }
609
610
611
612/**
613 * Open a progress form with title @p title.
614 * @param title The title to use for the progress form (will be put in the title bar on Newt).
615 * @param b1 The first line of the blurb; generally static.
616 * @param b2 The second line of the blurb; generally static.
617 * @param b3 The third line of the blurb; generally dynamic (it is passed
618 * to update_evalcall_form() every time).
619 * @param max_val The maximum amount of progress (number of filesets, etc.)
620 */
621 void
622 open_progress_form(char *title, char *b1, char *b2, char *b3,
623 long max_val) {
624
625 /*@ buffers ********************************************************* */
626 char *b1c;
627 char *blurb1 = NULL;
628 char *blurb2 = NULL;
629 char *blurb3 = NULL;
630
631 /*@ initialize ****************************************************** */
632 g_mysterious_dot_counter = 0;
633
634 malloc_string(blurb1);
635 malloc_string(blurb2);
636 malloc_string(blurb3);
637 malloc_string(b1c);
638
639 assert(title != NULL);
640 assert(b1 != NULL);
641 assert(b2 != NULL);
642 assert(b3 != NULL);
643
644 strcpy(blurb1, b1);
645 strcpy(blurb2, b2);
646 strcpy(blurb3, b3);
647 strcpy(b1c, b1);
648 center_string(b1c, 80);
649 if (max_val <= 0) {
650 max_val = 1;
651 }
652
653 g_start_time = get_time();
654 g_maximum_progress = max_val;
655 g_current_progress = 0;
656 strcpy(g_blurb_str_1, blurb1);
657 strcpy(g_blurb_str_2, blurb3);
658 strcpy(g_blurb_str_3, blurb2);
659 if (g_text_mode) {
660 log_msg(0, blurb1);
661 log_msg(0, blurb2);
662 log_msg(0, blurb3);
663 } else {
664 g_blurb1 = newtLabel(2, 1, blurb1);
665 g_blurb2 = newtLabel(2, 2, blurb3);
666 g_blurb3 = newtLabel(2, 4, blurb2);
667 // newtOpenWindow (10, 4, 60, 11, title);
668 newtCenteredWindow(60, 11, title);
669 g_scale = newtScale(3, 6, 54, g_maximum_progress);
670 g_progressForm = newtForm(NULL, NULL, 0);
671 g_percentline = newtLabel(10, 9, "This is the percentline");
672 g_timeline = newtLabel(10, 8, "This is the timeline");
673 newtFormAddComponents(g_progressForm, g_percentline,
674 g_timeline, g_scale, g_blurb1, g_blurb3,
675 g_blurb2, NULL);
676 newtPushHelpLine(b1c);
677 newtRefresh();
678 }
679 update_progress_form_full(blurb1, blurb2, blurb3);
680 paranoid_free(b1c);
681 mr_free(blurb1);
682 mr_free(blurb2);
683 mr_free(blurb3);
684 }
685
686/**
687 * Give a message to the user in the form of a dialog box (under Newt).
688 * @param prompt The message.
689 */
690 void
691 popup_and_OK(char *prompt) {
692 char ch;
693
694 assert_string_is_neither_NULL_nor_zerolength(prompt);
695
696 log_msg(0, prompt);
697 if (g_text_mode) {
698 printf
699 ("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ",
700 prompt);
701 while (((ch = getchar()) != '\n') && (ch != EOF));
702 } else {
703 (void) popup_with_buttons(prompt, " OK ", "");
704 }
705 }
706
707/**
708 * Ask the user to enter a value.
709 * @param title The title of the dialog box.
710 * @param b The blurb (e.g. what you want the user to enter).
711 * @param input The string given originaly as default
712 * @return output string if the user pressed OK, NULL if they pressed Cancel.
713 */
714 char *popup_and_get_string(char *title, char *b, char *input) {
715
716 /*@ newt ************************************************************ */
717 newtComponent myForm;
718 newtComponent b_1;
719 newtComponent b_2;
720 newtComponent b_res;
721 newtComponent text;
722 newtComponent type_here;
723
724 /*@ pointers ********************************************************* */
725 char *entry_value;
726
727 /*@ buffers ********************************************************** */
728 char *blurb = NULL;
729 char *output = NULL;
730
731 assert_string_is_neither_NULL_nor_zerolength(title);
732 assert(b != NULL);
733
734 if (g_text_mode) {
735 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q--- [%s]\n--> ", title, b, input);
736 mr_getline(output, stdin);
737 if (! strcmp(output,"\n")) {
738 /* take default if ENTER was typed */
739 mr_free(output);
740 mr_asprintf(output, "%s", input);
741 }
742 if (output[strlen(output) - 1] == '\n')
743 output[strlen(output) - 1] = '\0';
744 return (output);
745 }
746
747 mr_asprintf(blurb, "%s", b);
748 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
749 type_here =
750 newtEntry(2, newtTextboxGetNumLines(text) + 2,
751 input, 50,
752#ifdef __cplusplus
753 0, NEWT_FLAG_RETURNEXIT
754#else
755 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
756#endif
757 );
758 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
759 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
760 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
761 myForm = newtForm(NULL, NULL, 0);
762 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
763 center_string(blurb, 80);
764 newtPushHelpLine(blurb);
765 b_res = newtRunForm(myForm);
766
767 if (b_res == b_1) {
768 mr_asprintf(output, "%s", newtEntryGetValue(type_here));
769 }
770 newtPopHelpLine();
771 newtFormDestroy(myForm);
772 newtPopWindow();
773 mr_free(blurb);
774
775 return (output);
776 }
777
778
779/**
780 * Pop up a dialog box with user-defined buttons.
781 * @param p The text to put in the dialog box.
782 * @param button1 The label on the first button.
783 * @param button2 The label on the second button, or "" if you only want one button.
784 * @return TRUE if @p button1 was pushed, FALSE otherwise.
785 */
786 bool popup_with_buttons(char *p, char *button1, char *button2) {
787
788 /*@ buffers *********************************************************** */
789 char *prompt;
790
791 /*@ newt ************************************************************** */
792 newtComponent myForm;
793 newtComponent b_1;
794 newtComponent b_2;
795 newtComponent b_res;
796 newtComponent text;
797
798 assert_string_is_neither_NULL_nor_zerolength(p);
799 assert(button1 != NULL);
800 assert(button2 != NULL);
801 prompt = malloc(MAX_NEWT_COMMENT_LEN);
802 if (g_text_mode) {
803 if (strlen(button2) == 0) {
804 printf("%s (%s) --> ", p, button1);
805 } else {
806 printf("%s (%s or %s) --> ", p, button1, button2);
807 }
808 for (prompt[0] = '\0';
809 strcmp(prompt, button1) && (strlen(button2) == 0
810 || strcmp(prompt, button2));) {
811 printf("--> ");
812 (void) fgets(prompt, MAX_NEWT_COMMENT_LEN, stdin);
813 }
814 if (!strcmp(prompt, button1)) {
815 paranoid_free(prompt);
816 return (TRUE);
817 } else {
818 paranoid_free(prompt);
819 return (FALSE);
820 }
821 }
822
823 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
824 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
825 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
826 b_1 =
827 newtButton(20 -
828 ((button2[0] !=
829 '\0') ? strlen(button1) +
830 2 : strlen(button1) / 2),
831 newtTextboxGetNumLines(text) + 3, button1);
832 if (button2[0] != '\0') {
833 b_2 =
834 newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
835 } else {
836 b_2 = NULL;
837 }
838 // newtOpenWindow (25, 5, 46, newtTextboxGetNumLines (text) + 7, "Alert");
839 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
840 myForm = newtForm(NULL, NULL, 0);
841 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
842 center_string(prompt, 80);
843 newtPushHelpLine(prompt);
844 b_res = newtRunForm(myForm);
845 newtPopHelpLine();
846 newtFormDestroy(myForm);
847 newtPopWindow();
848 if (b_res == b_1) {
849 paranoid_free(prompt);
850 return (TRUE);
851 } else {
852 paranoid_free(prompt);
853 return (FALSE);
854 }
855 }
856
857
858
859
860/**
861 * Synchronize the log messages stored in @p err_log_lines with those shown
862 * on the screen.
863 */
864 void
865 refresh_log_screen() {
866
867 /*@ int *********************************************************** */
868 int i = 0;
869
870
871 if (g_text_mode || !err_log_lines) {
872 return;
873 }
874 for (i = g_noof_log_lines - 1; i >= 0; i--) {
875 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
876 " ");
877 }
878 newtRefresh();
879 for (i = g_noof_log_lines - 1; i >= 0; i--) {
880 err_log_lines[i][79] = '\0';
881 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
882 err_log_lines[i]);
883 }
884 newtRefresh();
885 }
886
887
888/**
889 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
890 * only allocate some memory.
891 */
892 void
893 setup_newt_stuff() {
894
895 /*@ int *********************************************************** */
896 int i = 0;
897 int cols;
898
899 if (!g_text_mode) {
900 newtInit();
901 newtCls();
902 newtPushHelpLine("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
903 newtDrawRootText(18, 0, WELCOME_STRING);
904 newtRefresh();
905 newtGetScreenSize(&cols, &g_noof_rows);
906 g_noof_log_lines = (g_noof_rows / 5) + 1;
907 }
908
909 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
910
911 for (i = 0; i < g_noof_log_lines; i++) {
912 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
913 }
914
915 for (i = 0; i < g_noof_log_lines; i++) {
916 err_log_lines[i][0] = '\0';
917 }
918 }
919
920
921/**
922 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
923 * @param num The numerator of the ratio.
924 * @param denom The denomenator of the ratio.
925 */
926 void
927 update_evalcall_form_ratio(int num, int denom) {
928
929 /*@ long ************************************************************ */
930 long current_time = 0L;
931 long time_taken = 0L;
932 long time_total_est = 0L;
933 long time_remaining = 0L;
934
935 /*@ buffers ********************************************************** */
936 char *timeline_str = NULL;
937 char *pcline_str = NULL;
938 char *taskprogress = NULL;
939
940 /*@ int ************************************************************** */
941 int percentage = 0;
942 int i = 0;
943 int j = 0;
944
945 if (num * 100 < denom) {
946 percentage = 1;
947 } else {
948 percentage = (num * 100 + denom / 2) / denom;
949 }
950
951 current_time = get_time();
952 time_taken = current_time - g_isoform_starttime;
953 if (num) {
954 time_total_est = time_taken * denom / num;
955 time_remaining = time_total_est - time_taken;
956 } else {
957 time_remaining = 0L;
958 }
959 if (!g_text_mode) {
960 if (g_isoform_header) {
961 newtLabelSetText(g_isoform_header, g_isoform_header_str);
962 }
963 }
964 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
965 if ((percentage < 3 && g_isoform_old_progress < 3)
966 || percentage > g_isoform_old_progress) {
967 g_isoform_old_progress = percentage;
968 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);
969 if (percentage < 3) {
970 mr_asprintf(pcline_str, " Working");
971 for (j = 0; j < g_mysterious_dot_counter; j++) {
972 mr_strcat(pcline_str, ".");
973 }
974 for (; j < 27; j++) {
975 mr_strcat(pcline_str, " ");
976 }
977 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
978 } else {
979 mr_asprintf(pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
980 }
981 if (g_text_mode) {
982 mr_asprintf(taskprogress, "TASK: [");
983 for (i = 0; i < percentage; i += 5) {
984 mr_strcat(taskprogress, "*");
985 }
986 for (; i < 100; i += 5) {
987 mr_strcat(taskprogress, ".");
988 }
989 if (percentage >= 3) {
990 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, time_remaining / 60, time_remaining % 60);
991 printf("---evalcall---1--- %s\n", g_isoform_header_str);
992 printf("---evalcall---2--- %s\n", taskprogress);
993 printf("---evalcall---E---\n");
994 }
995 mr_free(taskprogress);
996 } else {
997 newtScaleSet(g_isoform_scale, (unsigned long long) percentage);
998 if (g_isoform_pcline) {
999 newtLabelSetText(g_isoform_pcline, pcline_str);
1000 }
1001 if ((percentage >= 3) && (g_isoform_timeline)) {
1002 newtLabelSetText(g_isoform_timeline, timeline_str);
1003 }
1004 }
1005 mr_free(pcline_str);
1006 mr_free(timeline_str);
1007 }
1008 if (!g_text_mode) {
1009 newtRefresh();
1010 }
1011 }
1012
1013
1014
1015/**
1016 * Update the evalcall form to show @p curr %.
1017 * @param curr The current amount of progress (percentage) in the evalcall form.
1018 */
1019 void
1020 update_evalcall_form(int curr) {
1021 update_evalcall_form_ratio(curr, 100);
1022 }
1023
1024
1025
1026/**
1027 * Update the progress form to show @p blurb3 and the current value of
1028 * @p g_maximum_progress.
1029 * @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.
1030 */
1031 void
1032 update_progress_form(char *blurb3) {
1033 if (g_current_progress == -999) {
1034 return;
1035 }
1036 strcpy(g_blurb_str_2, blurb3);
1037 update_progress_form_full(g_blurb_str_1, g_blurb_str_2,
1038 g_blurb_str_3);
1039 }
1040
1041
1042/**
1043 * Update the progress form's complete blurb and show @p g_current_progress.
1044 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1045 * @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.
1046 * @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.
1047 */
1048 void
1049 update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1050 /*@ long ***************************************************** */
1051 long current_time = 0L;
1052 long time_taken = 0L;
1053 long time_remaining = 0L;
1054 long time_total_est = 0L;
1055
1056 /*@ int ******************************************************* */
1057 int percentage = 0;
1058 int i = 0;
1059
1060 /*@ buffers *************************************************** */
1061 char *percentline_str = NULL;
1062 char *timeline_str = NULL;
1063 char *taskprogress = NULL;
1064
1065 if (!g_text_mode) {
1066 assert(blurb1 != NULL);
1067 assert(blurb2 != NULL);
1068 assert(blurb3 != NULL);
1069 assert(g_timeline != NULL);
1070 }
1071
1072 current_time = get_time();
1073 time_taken = current_time - g_start_time;
1074 if (g_maximum_progress == 0) {
1075 percentage = 0;
1076 } else {
1077 if (g_current_progress > g_maximum_progress) {
1078 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);
1079 g_current_progress = g_maximum_progress;
1080 }
1081 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
1082 }
1083 if (percentage < 1) {
1084 percentage = 1;
1085 }
1086 if (percentage > 100) {
1087 percentage = 100;
1088 }
1089 if (g_current_progress) {
1090 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
1091 time_remaining = time_total_est - time_taken;
1092 } else {
1093 time_remaining = 0L;
1094 }
1095 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1096 if (g_text_mode) {
1097 printf("---progress-form---1--- %s\n", blurb1);
1098 printf("---progress-form---2--- %s\n", blurb2);
1099 printf("---progress-form---3--- %s\n", blurb3);
1100 printf("---progress-form---E---\n");
1101 mr_asprintf(taskprogress, "TASK: [");
1102 for (i = 0; i < percentage; i += 5) {
1103 mr_strcat(taskprogress, "*");
1104 }
1105 for (; i < 100; i += 5) {
1106 mr_strcat(taskprogress, ".");
1107 }
1108 if (percentage > 100) {
1109 log_msg(2, "percentage = %d", percentage);
1110 }
1111 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
1112 printf("---progress-form---4--- %s\n", taskprogress);
1113 paranoid_free(taskprogress);
1114 } else {
1115 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);
1116 mr_asprintf(percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
1117
1118 center_string(blurb1, 54);
1119 center_string(blurb2, 54);
1120 center_string(blurb3, 54);
1121 if (g_blurb1) {
1122 newtLabelSetText(g_blurb1, blurb1);
1123 }
1124 if (g_blurb2) {
1125 newtLabelSetText(g_blurb2, blurb3);
1126 }
1127 if (g_blurb3) {
1128 newtLabelSetText(g_blurb3, blurb2);
1129 }
1130 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
1131 if ((percentage >= 2) && (g_timeline)) {
1132 newtLabelSetText(g_timeline, timeline_str);
1133 }
1134 mr_free(timeline_str);
1135
1136 if (g_percentline) {
1137 newtLabelSetText(g_percentline, percentline_str);
1138 }
1139 mr_free(percentline_str);
1140
1141 newtRefresh();
1142 }
1143 }
1144
1145
1146/**
1147 * Ask the user which backup media type they would like to use.
1148 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1149 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs,
1150 * and @c iso.
1151 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1152 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1153 */
1154 t_bkptype which_backup_media_type(bool restoring) {
1155
1156 /*@ char ************************************************************ */
1157 t_bkptype output;
1158
1159
1160 /*@ newt ************************************************************ */
1161 char *title_sz = NULL;
1162 char *minimsg_sz = NULL;
1163 static t_bkptype possible_bkptypes[] =
1164 { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso };
1165 static char *possible_responses[] =
1166 { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev",
1167"nfs", "iso", NULL };
1168 char *outstr;
1169 t_bkptype backup_type;
1170 int i;
1171
1172 newtComponent b1;
1173 newtComponent b2;
1174 newtComponent b3;
1175 newtComponent b4;
1176 newtComponent b5;
1177 newtComponent b6;
1178 newtComponent b7;
1179 newtComponent b8;
1180 newtComponent b_res;
1181 newtComponent myForm;
1182
1183 outstr = malloc(MAX_NEWT_COMMENT_LEN);
1184 if (g_text_mode) {
1185 for (backup_type = none; backup_type == none;) {
1186 printf("Backup type (");
1187 for (i = 0; possible_responses[i]; i++) {
1188 printf("%c%s", (i == 0) ? '\0' : ' ',
1189 possible_responses[i]);
1190 }
1191 printf(")\n--> ");
1192 (void) fgets(outstr, MAX_NEWT_COMMENT_LEN, stdin);
1193 strip_spaces(outstr);
1194 for (i = 0; possible_responses[i]; i++) {
1195 if (!strcmp(possible_responses[i], outstr)) {
1196 backup_type = possible_bkptypes[i];
1197 }
1198 }
1199 }
1200 paranoid_free(outstr);
1201 return (backup_type);
1202 }
1203 newtDrawRootText(18, 0, WELCOME_STRING);
1204 if (restoring) {
1205 mr_asprintf(title_sz, "Please choose the backup media from which you want to read data.");
1206 mr_asprintf(minimsg_sz, "Read from:");
1207 } else {
1208 mr_asprintf(title_sz, "Please choose the backup media to which you want to archive data.");
1209 mr_asprintf(minimsg_sz, "Backup to:");
1210 }
1211 newtPushHelpLine(title_sz);
1212 newtCenteredWindow(34, 17, minimsg_sz);
1213 b1 = newtButton(1, 1, "CD-R disks ");
1214 b2 = newtButton(17, 1, "CD-RW disks");
1215 b3 = newtButton(1, 9, "Tape drive ");
1216 b4 = newtButton(17, 5, "USB Key/Disk");
1217 b5 = newtButton(1, 5, " DVD disks ");
1218 b6 = newtButton(17, 9, " NFS mount ");
1219 b7 = newtButton(1, 13, " Hard disk ");
1220 b8 = newtButton(17, 13, " Exit ");
1221 myForm = newtForm(NULL, NULL, 0);
1222 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1223 NULL);
1224 b_res = newtRunForm(myForm);
1225 newtFormDestroy(myForm);
1226 newtPopWindow();
1227 if (b_res == b1) {
1228 output = cdr;
1229 } else if (b_res == b2) {
1230 output = cdrw;
1231 } else if (b_res == b3) {
1232 output = tape;
1233 } else if (b_res == b4) {
1234 output = usb;
1235 } else if (b_res == b5) {
1236 output = dvd;
1237 } else if (b_res == b6) {
1238 output = nfs;
1239 } else if (b_res == b7) {
1240 output = iso;
1241 } else {
1242 output = none;
1243 }
1244 newtPopHelpLine();
1245 mr_free(title_sz);
1246 mr_free(minimsg_sz);
1247 paranoid_free(outstr);
1248 return (output);
1249 }
1250
1251
1252
1253
1254/**
1255 * Ask the user how much compression they would like to use.
1256 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1257 * @return The compression level (0-9) chosen, or -1 for "Exit".
1258 */
1259 int
1260 which_compression_level() {
1261
1262 /*@ char ************************************************************ */
1263 int output = none;
1264
1265
1266 /*@ newt ************************************************************ */
1267
1268 newtComponent b1;
1269 newtComponent b2;
1270 newtComponent b3;
1271 newtComponent b4;
1272 newtComponent b5;
1273 newtComponent b_res;
1274 newtComponent myForm;
1275
1276 newtDrawRootText(18, 0, WELCOME_STRING);
1277 newtPushHelpLine
1278 (" Please specify the level of compression that you want.");
1279 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
1280 newtCenteredWindow(34, 13, "How much compression?");
1281 b1 = newtButton(4, 1, "Maximum");
1282 b2 = newtButton(18, 1, "Average");
1283 b3 = newtButton(4, 5, "Minimum");
1284 b4 = newtButton(18, 5, " None ");
1285 b5 = newtButton(4, 9, " Exit ");
1286 myForm = newtForm(NULL, NULL, 0);
1287 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1288 b_res = newtRunForm(myForm);
1289 newtFormDestroy(myForm);
1290 newtPopWindow();
1291 if (b_res == b1) {
1292 output = 9;
1293 } else if (b_res == b2) {
1294 output = 4;
1295 } else if (b_res == b3) {
1296 output = 1;
1297 } else if (b_res == b4) {
1298 output = 0;
1299 } else if (b_res == b5) {
1300 output = -1;
1301 }
1302 newtPopHelpLine();
1303 return (output);
1304 }
1305
1306
1307
1308
1309
1310/**
1311 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1312 * @p ARBITRARY_MAXIMUM entries.
1313 * @param filelist The filelist structure to load @p source_file into.
1314 * @param source_file The file containing a list of filenames to load into @p filelist.
1315 */
1316 int load_filelist_into_array(struct s_filelist *filelist,
1317 char *source_file) {
1318 int i;
1319 int j = 0;
1320 bool done;
1321 char *tmp;
1322 char *tmp1 = NULL;
1323 FILE *fin, *fout;
1324 struct s_filelist_entry dummy_fle;
1325
1326 assert(filelist != NULL);
1327 assert_string_is_neither_NULL_nor_zerolength(source_file);
1328
1329 log_it("entering");
1330 if (!(fin = fopen(source_file, "r"))) {
1331 log_OS_error(source_file);
1332 log_msg(2, "Can't open %s; therefore, cannot popup list",
1333 source_file);
1334 return (1);
1335 }
1336
1337 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
1338 if (!(fout = fopen(tmp1, "a"))) {
1339 log_msg(2, "Can't write to %s", tmp1);
1340 return(1);
1341 }
1342
1343 malloc_string(tmp);
1344 log_msg(2, "Loading %s", source_file);
1345 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM;
1346 filelist->entries++) {
1347 god_i_hate_gotos:
1348 if (feof(fin)) {
1349 break;
1350 }
1351 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, fin);
1352 i = (int) strlen(tmp);
1353 if (i < 2) {
1354 goto god_i_hate_gotos;
1355 }
1356 if (tmp[i - 1] < 32) {
1357 tmp[--i] = '\0';
1358 }
1359 if (i < 2) {
1360 goto god_i_hate_gotos;
1361 }
1362 if (!does_file_exist(tmp) && !feof(fin)) {
1363 j++;
1364 fprintf(fout, "%s\n", tmp);
1365
1366 goto god_i_hate_gotos;
1367 }
1368 filelist->el[filelist->entries].severity =
1369 severity_of_difference(tmp, NULL);
1370 strcpy(filelist->el[filelist->entries].filename, tmp);
1371 if (feof(fin)) {
1372 break;
1373 }
1374 }
1375 paranoid_fclose(fin);
1376 paranoid_fclose(fout);
1377 if (j > 0) {
1378 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);
1379 }
1380 paranoid_free(tmp1);
1381 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1382 log_to_screen("Arbitrary limits suck, man!");
1383 paranoid_free(tmp);
1384 return (1);
1385 }
1386 for (done = FALSE; !done;) {
1387 done = TRUE;
1388 for (i = 0; i < filelist->entries - 1; i++) {
1389// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
1390 if (filelist->el[i].severity < filelist->el[i + 1].severity
1391 || (filelist->el[i].severity ==
1392 filelist->el[i + 1].severity
1393 && strcmp(filelist->el[i].filename,
1394 filelist->el[i + 1].filename) > 0)) {
1395 memcpy((void *) &dummy_fle,
1396 (void *) &(filelist->el[i]),
1397 sizeof(struct s_filelist_entry));
1398 memcpy((void *) &(filelist->el[i]),
1399 (void *) &(filelist->el[i + 1]),
1400 sizeof(struct s_filelist_entry));
1401 memcpy((void *) &(filelist->el[i + 1]),
1402 (void *) &dummy_fle,
1403 sizeof(struct s_filelist_entry));
1404 log_msg(2, "Swapping %s and %s",
1405 filelist->el[i].filename,
1406 filelist->el[i + 1].filename);
1407 done = FALSE;
1408 }
1409 }
1410 }
1411 paranoid_free(tmp);
1412 log_it("leaving");
1413 return (0);
1414 }
1415
1416
1417
1418/**
1419 * Generate a pretty string based on @p flentry.
1420 * @param flentry The filelist entry to stringify.
1421 * @return The string form of @p flentry.
1422 * @note The returned value points to static storage that will be overwritten with each call.
1423 */
1424 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1425 static char comment[100];
1426 char *tmp = NULL;
1427
1428 log_it("entering");
1429 assert(flentry != NULL);
1430 if (flentry->severity == 0) {
1431 mr_asprintf(tmp, "0 ");
1432 } else if (flentry->severity == 1) {
1433 mr_asprintf(tmp, "low ");
1434 } else if (flentry->severity == 2) {
1435 mr_asprintf(tmp, "med ");
1436 } else {
1437 mr_asprintf(tmp, "high");
1438 }
1439 mr_strcat(tmp, " ");
1440 mr_strcat(tmp, flentry->filename);
1441 strncpy(comment, tmp, 99);
1442 paranoid_free(tmp);
1443
1444 log_it("leaving");
1445 return (comment);
1446 }
1447
1448
1449
1450
1451
1452/**
1453 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1454 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1455 * @param source_file The file containing a list of changed files.
1456 */
1457 void popup_changelist_from_file(char *source_file) {
1458 char *reason;
1459 newtComponent myForm;
1460 newtComponent bClose;
1461 newtComponent bSelect;
1462 newtComponent b_res;
1463 newtComponent fileListbox;
1464 newtComponent headerMsg;
1465
1466 /*@ ???? ************************************************************ */
1467 void *curr_choice;
1468 void *keylist[ARBITRARY_MAXIMUM];
1469
1470 /*@ int ************************************************************* */
1471 int currline = 0;
1472 int finished = FALSE;
1473
1474 /*@ long ************************************************************ */
1475 long i = 0;
1476 long lng = 0;
1477
1478 /*@ buffers ********************************************************* */
1479 char *tmp = NULL;
1480 char *differ_sz = NULL;
1481
1482 struct s_filelist *filelist;
1483 assert_string_is_neither_NULL_nor_zerolength(source_file);
1484 if (g_text_mode) {
1485 log_msg(2, "Text mode. Therefore, no popup list.");
1486 return;
1487 }
1488 log_msg(2, "Examining file %s", source_file);
1489
1490 lng = count_lines_in_file(source_file);
1491 if (lng < 1) {
1492 log_msg(2, "No lines in file. Therefore, no popup list.");
1493 return;
1494 } else if (lng >= ARBITRARY_MAXIMUM) {
1495 log_msg(2, "Too many files differ for me to list.");
1496 return;
1497 }
1498
1499 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1500 fileListbox =
1501 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1502 newtListboxClear(fileListbox);
1503
1504 if (load_filelist_into_array(filelist, source_file)) {
1505 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
1506 return;
1507 }
1508 log_msg(2, "%d files loaded into filelist array",
1509 filelist->entries);
1510 for (i = 0; i < filelist->entries; i++) {
1511 keylist[i] = (void *) i;
1512 newtListboxAppendEntry(fileListbox,
1513 filelist_entry_to_string(&
1514 (filelist->
1515 el[i])),
1516 keylist[i]);
1517 }
1518 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
1519 newtPushHelpLine(differ_sz);
1520 mr_free(differ_sz);
1521
1522 bClose = newtCompactButton(10, 15, " Close ");
1523 bSelect = newtCompactButton(30, 15, " Select ");
1524 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
1525 headerMsg = newtLabel(2, 1, tmp);
1526 mr_free(tmp);
1527
1528 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1529 myForm = newtForm(NULL, NULL, 0);
1530 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1531 bSelect, NULL);
1532
1533 malloc_string(reason);
1534 while (!finished) {
1535 b_res = newtRunForm(myForm);
1536 if (b_res == bClose) {
1537 finished = TRUE;
1538 } else {
1539 curr_choice = newtListboxGetCurrent(fileListbox);
1540 for (i = 0;
1541 i < filelist->entries && keylist[i] != curr_choice;
1542 i++);
1543 if (i == filelist->entries && filelist->entries > 0) {
1544 log_to_screen("I don't know what that button does!");
1545 } else {
1546 currline = i;
1547 if (filelist->entries > 0) {
1548 severity_of_difference(filelist->el[currline]. filename, reason);
1549 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
1550
1551 popup_and_OK(tmp);
1552 mr_free(tmp);
1553 }
1554 }
1555 }
1556 }
1557 paranoid_free(reason);
1558
1559 newtFormDestroy(myForm);
1560 newtPopWindow();
1561 newtPopHelpLine();
1562 return;
1563 }
1564
1565/* @} - end of guiGroup */
1566
1567
1568#if __cplusplus
1569} /* extern "C" */
1570#endif
1571
1572
1573void wait_until_software_raids_are_prepped(char *mdstat_file,
1574 int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.