source: MondoRescue/branches/3.0/mondo/src/common/newt-specific.c@ 2972

Last change on this file since 2972 was 2910, checked in by Bruno Cornec, 12 years ago

Fix a syntax issue in newt-specific.c

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