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

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

Fix mr_popup_and_get_string proto

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