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

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

r3327@localhost: bruno | 2009-08-04 00:17:46 +0200
popup_and_get_string and build_partition_name now return an allocated string

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