source: MondoRescue/branches/3.3/mondo/src/common/newt-specific.c@ 3828

Last change on this file since 3828 was 3828, checked in by Bruno Cornec, 3 months ago

manages restore_path dynamically

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