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

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