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

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

r3330@localhost: bruno | 2009-08-05 00:48:36 +0200
bkpinfo->include_paths is now dynamically allocated

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