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

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