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

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

manages zip_exe and zip_suffix dynamically

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