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

Last change on this file since 3834 was 3834, checked in by Bruno Cornec, 4 months ago

manages scratchdir dynamically

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