source: MondoRescue/branches/3.2/mondo/src/common/newt-specific.c@ 3508

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