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

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

manages call_make|burn|after_iso dynamically

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