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

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

r3263@localhost: bruno | 2009-07-13 02:25:52 +0200

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