source: MondoRescue/branches/2.2.9/mondo/src/common/newt-specific.c@ 2211

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

r3089@localhost: bruno | 2009-05-18 06:41:05 +0200

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