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

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