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

Last change on this file since 3292 was 3292, checked in by Bruno Cornec, 10 years ago
  • Remove from mindi 2 functions replaced by perl code (ReadAllLink by mr-read-all-link and ProcessLDD by mr-process-ldd)
  • Redirect mindi execution to mondo log on the fly to try to improve percentage display with newt (not finished yet)
  • Introduce a new global var MINDI_LOGFILE and use it everywhere
  • Fix some memory free mising (thanks valgrind)
  • Remove a call to rpm to log packages - not portable
  • Works with valgrind locally to create images in dir
  • Property svn:keywords set to Id
File size: 45.6 KB
Line 
1/* newt-specific.c
2 $Id: newt-specific.c 3292 2014-05-27 15:53:14Z 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 3292 2014-05-27 15:53:14Z 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 output The string to put the user's answer in.
725 * @param maxsize The size in bytes allocated to @p output.
726 * @return TRUE if the user pressed OK, FALSE if they pressed Cancel.
727 */
728 bool popup_and_get_string(char *title, char *b, char *output, int maxsize) {
729
730 /*@ newt ************************************************************ */
731 newtComponent myForm;
732 newtComponent b_1;
733 newtComponent b_2;
734 newtComponent b_res;
735 newtComponent text;
736 newtComponent type_here;
737
738 /*@ pointers ********************************************************* */
739 char *entry_value;
740
741 /*@ buffers ********************************************************** */
742 char *blurb = NULL;
743 char *p;
744 char *original_contents = NULL;
745
746 assert_string_is_neither_NULL_nor_zerolength(title);
747 assert(b != NULL);
748 assert(output != NULL);
749
750 if (g_text_mode) {
751 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q---\n--> ", title, b);
752 p = fgets(output, maxsize, stdin);
753 if (!p) {
754 // FIXME
755 }
756 if (output[strlen(output) - 1] == '\n')
757 output[strlen(output) - 1] = '\0';
758 return (TRUE);
759 }
760
761 blurb = malloc(MAX_NEWT_COMMENT_LEN);
762 strcpy(blurb, b);
763 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
764 mr_asprintf(original_contents, "%s", output);
765 output[0] = '\0';
766 type_here = newtEntry(2, newtTextboxGetNumLines(text) + 2, original_contents, 50,
767#ifdef __cplusplus
768 0, NEWT_FLAG_RETURNEXIT
769#else
770 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
771#endif
772 );
773 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
774 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
775 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
776 myForm = newtForm(NULL, NULL, 0);
777 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
778 center_string(blurb, 80);
779 newtPushHelpLine(blurb);
780 b_res = newtRunForm(myForm);
781 strcpy(output, entry_value);
782 newtPopHelpLine();
783 newtFormDestroy(myForm);
784 newtPopWindow();
785 paranoid_free(blurb);
786
787 if (b_res == b_2) {
788 strcpy(output, original_contents);
789 mr_free(original_contents);
790 return (FALSE);
791 } else {
792 mr_free(original_contents);
793 return (TRUE);
794 }
795 }
796
797
798/**
799 * Pop up a dialog box with user-defined buttons.
800 * @param p The text to put in the dialog box.
801 * @param button1 The label on the first button.
802 * @param button2 The label on the second button, or "" if you only want one button.
803 * @return TRUE if @p button1 was pushed, FALSE otherwise.
804 */
805bool popup_with_buttons(char *p, char *button1, char *button2) {
806
807 /*@ buffers *********************************************************** */
808 char *prompt = NULL;
809
810 /*@ newt ************************************************************** */
811 newtComponent myForm;
812 newtComponent b_1;
813 newtComponent b_2;
814 newtComponent b_res;
815 newtComponent text;
816
817 assert_string_is_neither_NULL_nor_zerolength(p);
818 assert(button1 != NULL);
819 assert(button2 != NULL);
820 if (g_text_mode) {
821 if (strlen(button2) == 0) {
822 printf("%s (%s) --> ", p, button1);
823 } else {
824 printf("%s (%s or %s) --> ", p, button1, button2);
825 }
826 mr_getline(prompt, stdin);
827 while (strcmp(prompt, button1) && (strlen(button2) == 0 || strcmp(prompt, button2))) {
828 printf("--> ");
829 mr_free(prompt);
830 mr_getline(prompt, stdin);
831 }
832 if (!strcmp(prompt, button1)) {
833 mr_free(prompt);
834 return (TRUE);
835 } else {
836 mr_free(prompt);
837 return (FALSE);
838 }
839 }
840
841 prompt = malloc(MAX_NEWT_COMMENT_LEN);
842 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
843 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
844 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
845 b_1 = newtButton(20 - ((button2[0] != '\0') ? strlen(button1) + 2 : strlen(button1) / 2), newtTextboxGetNumLines(text) + 3, button1);
846 if (button2[0] != '\0') {
847 b_2 = newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
848 } else {
849 b_2 = NULL;
850 }
851 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
852 myForm = newtForm(NULL, NULL, 0);
853 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
854 center_string(prompt, 80);
855 newtPushHelpLine(prompt);
856 b_res = newtRunForm(myForm);
857 newtPopHelpLine();
858 newtFormDestroy(myForm);
859 newtPopWindow();
860 if (b_res == b_1) {
861 paranoid_free(prompt);
862 return (TRUE);
863 } else {
864 paranoid_free(prompt);
865 return (FALSE);
866 }
867 }
868
869
870
871
872/**
873 * Synchronize the log messages stored in @p err_log_lines with those shown
874 * on the screen.
875 */
876void refresh_log_screen() {
877
878 /*@ int *********************************************************** */
879 int i = 0;
880
881
882 if (g_text_mode || !err_log_lines) {
883 return;
884 }
885 for (i = g_noof_log_lines - 1; i >= 0; i--) {
886 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
887 " ");
888 }
889 newtRefresh();
890 for (i = g_noof_log_lines - 1; i >= 0; i--) {
891 err_log_lines[i][79] = '\0';
892 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
893 err_log_lines[i]);
894 }
895 newtRefresh();
896 }
897
898
899/**
900 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
901 * only allocate some memory.
902 */
903void setup_newt_stuff() {
904
905 /*@ int *********************************************************** */
906 int i = 0;
907 int cols;
908
909 if (!g_text_mode) {
910 newtInit();
911 newtCls();
912 newtPushHelpLine("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
913 newtDrawRootText(18, 0, WELCOME_STRING);
914 newtRefresh();
915 newtGetScreenSize(&cols, &g_noof_rows);
916 g_noof_log_lines = (g_noof_rows / 5) + 1;
917 }
918
919 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
920
921 for (i = 0; i < g_noof_log_lines; i++) {
922 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
923 }
924
925 for (i = 0; i < g_noof_log_lines; i++) {
926 err_log_lines[i][0] = '\0';
927 }
928 }
929
930
931/**
932 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
933 * @param num The numerator of the ratio.
934 * @param denom The denomenator of the ratio.
935 */
936void update_evalcall_form_ratio(int num, int denom) {
937
938 /*@ long ************************************************************ */
939 long current_time = 0L;
940 long time_taken = 0L;
941 long time_total_est = 0L;
942 long time_remaining = 0L;
943
944 /*@ buffers ********************************************************** */
945 char *timeline_str = NULL;
946 char *pcline_str = NULL;
947 char *taskprogress = NULL;
948
949 /*@ int ************************************************************** */
950 int percentage = 0;
951 int i = 0;
952 int j = 0;
953
954 if (num * 100 < denom) {
955 percentage = 1;
956 } else {
957 percentage = (num * 100 + denom / 2) / denom;
958 }
959
960 current_time = get_time();
961 time_taken = current_time - g_isoform_starttime;
962 if (num) {
963 time_total_est = time_taken * denom / num;
964 time_remaining = time_total_est - time_taken;
965 } else {
966 time_remaining = 0L;
967 }
968 if (!g_text_mode) {
969 if (g_isoform_header) {
970 newtLabelSetText(g_isoform_header, g_isoform_header_str);
971 }
972 }
973 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
974 if ((percentage < 3 && g_isoform_old_progress < 3) || percentage > g_isoform_old_progress) {
975 g_isoform_old_progress = percentage;
976 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);
977 if (percentage < 3) {
978 mr_asprintf(pcline_str, " Working");
979 for (j = 0; j < g_mysterious_dot_counter; j++) {
980 mr_strcat(pcline_str, ".");
981 }
982 for (; j < 27; j++) {
983 mr_strcat(pcline_str, " ");
984 }
985 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
986 } else {
987 mr_asprintf(pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
988 }
989 if (g_text_mode) {
990 mr_asprintf(taskprogress, "TASK: [");
991 for (i = 0; i < percentage; i += 5) {
992 mr_strcat(taskprogress, "*");
993 }
994 for (; i < 100; i += 5) {
995 mr_strcat(taskprogress, ".");
996 }
997 if (percentage >= 3) {
998 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
999 printf("---evalcall---1--- %s\n", g_isoform_header_str);
1000 printf("---evalcall---2--- %s\n", taskprogress);
1001 printf("---evalcall---E---\n");
1002 }
1003 mr_free(taskprogress);
1004 } else {
1005 newtScaleSet(g_isoform_scale, (unsigned long long) percentage);
1006 if (g_isoform_pcline) {
1007 newtLabelSetText(g_isoform_pcline, pcline_str);
1008 }
1009 if ((percentage >= 3) && (g_isoform_timeline)) {
1010 newtLabelSetText(g_isoform_timeline, timeline_str);
1011 }
1012 }
1013 mr_free(pcline_str);
1014 mr_free(timeline_str);
1015 }
1016 if (!g_text_mode) {
1017 newtDrawForm(g_isoform_main);
1018 newtRefresh();
1019 }
1020 }
1021
1022
1023
1024/**
1025 * Update the evalcall form to show @p curr %.
1026 * @param curr The current amount of progress (percentage) in the evalcall form.
1027 */
1028void update_evalcall_form(int curr) {
1029
1030 update_evalcall_form_ratio(curr, 100);
1031}
1032
1033
1034
1035/**
1036 * Update the progress form to show @p blurb3 and the current value of
1037 * @p g_maximum_progress.
1038 * @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.
1039 */
1040void update_progress_form(char *blurb3) {
1041
1042 if (g_current_progress == -999) {
1043 return;
1044 }
1045 strcpy(g_blurb_str_2, blurb3);
1046 update_progress_form_full(g_blurb_str_1, g_blurb_str_2, g_blurb_str_3);
1047}
1048
1049
1050/**
1051 * Update the progress form's complete blurb and show @p g_current_progress.
1052 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1053 * @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.
1054 * @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.
1055 */
1056void update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1057
1058 /*@ long ***************************************************** */
1059 long current_time = 0L;
1060 long time_taken = 0L;
1061 long time_remaining = 0L;
1062 long time_total_est = 0L;
1063
1064 /*@ int ******************************************************* */
1065 int percentage = 0;
1066 int i = 0;
1067
1068 /*@ buffers *************************************************** */
1069 char *percentline_str = NULL;
1070 char *timeline_str = NULL;
1071 char *taskprogress = NULL;
1072
1073 if (!g_text_mode) {
1074 assert(blurb1 != NULL);
1075 assert(blurb2 != NULL);
1076 assert(blurb3 != NULL);
1077 assert(g_timeline != NULL);
1078 }
1079
1080 current_time = get_time();
1081 time_taken = current_time - g_start_time;
1082 if (g_maximum_progress == 0) {
1083 percentage = 0;
1084 } else {
1085 if (g_current_progress > g_maximum_progress) {
1086 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);
1087 g_current_progress = g_maximum_progress;
1088 }
1089 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
1090 }
1091 if (percentage < 1) {
1092 percentage = 1;
1093 }
1094 if (percentage > 100) {
1095 percentage = 100;
1096 }
1097 if (g_current_progress) {
1098 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
1099 time_remaining = time_total_est - time_taken;
1100 } else {
1101 time_remaining = 0L;
1102 }
1103 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1104 if (g_text_mode) {
1105 printf("---progress-form---1--- %s\n", blurb1);
1106 printf("---progress-form---2--- %s\n", blurb2);
1107 printf("---progress-form---3--- %s\n", blurb3);
1108 printf("---progress-form---E---\n");
1109 mr_asprintf(taskprogress, "TASK: [");
1110 for (i = 0; i < percentage; i += 5) {
1111 mr_strcat(taskprogress, "*");
1112 }
1113 for (; i < 100; i += 5) {
1114 mr_strcat(taskprogress, ".");
1115 }
1116 if (percentage > 100) {
1117 log_msg(2, "percentage = %d", percentage);
1118 }
1119 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
1120 printf("---progress-form---4--- %s\n", taskprogress);
1121 paranoid_free(taskprogress);
1122 } else {
1123 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);
1124 mr_asprintf(percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
1125
1126 center_string(blurb1, 54);
1127 center_string(blurb2, 54);
1128 center_string(blurb3, 54);
1129 if (g_blurb1) {
1130 newtLabelSetText(g_blurb1, blurb1);
1131 }
1132 if (g_blurb2) {
1133 newtLabelSetText(g_blurb2, blurb3);
1134 }
1135 if (g_blurb3) {
1136 newtLabelSetText(g_blurb3, blurb2);
1137 }
1138 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
1139 if ((percentage >= 2) && (g_timeline)) {
1140 newtLabelSetText(g_timeline, timeline_str);
1141 }
1142 mr_free(timeline_str);
1143
1144 if (g_percentline) {
1145 newtLabelSetText(g_percentline, percentline_str);
1146 }
1147 mr_free(percentline_str);
1148
1149 newtDrawForm(g_progressForm);
1150 newtRefresh();
1151 }
1152 }
1153
1154
1155/**
1156 * Ask the user which backup media type they would like to use.
1157 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1158 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c netfs,
1159 * and @c iso.
1160 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1161 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1162 */
1163t_bkptype which_backup_media_type(bool restoring) {
1164
1165 /*@ char ************************************************************ */
1166 t_bkptype output;
1167
1168
1169 /*@ newt ************************************************************ */
1170 char *title_sz = NULL;
1171 char *minimsg_sz = NULL;
1172 static t_bkptype possible_bkptypes[] = { none, cdr, cdrw, dvd, tape, cdstream, udev, netfs, iso };
1173 static char *possible_responses[] = { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev", "netfs", "iso", NULL };
1174 char *outstr = NULL;
1175 t_bkptype backup_type;
1176 int i;
1177
1178 newtComponent b1;
1179 newtComponent b2;
1180 newtComponent b3;
1181 newtComponent b4;
1182 newtComponent b5;
1183 newtComponent b6;
1184 newtComponent b7;
1185 newtComponent b8;
1186 newtComponent b_res;
1187 newtComponent myForm;
1188
1189 if (g_text_mode) {
1190 for (backup_type = none; backup_type == none;) {
1191 printf("Backup type (");
1192 for (i = 0; possible_responses[i]; i++) {
1193 printf("%c%s", (i == 0) ? '\0' : ' ',
1194 possible_responses[i]);
1195 }
1196 printf(")\n--> ");
1197 mr_getline(outstr, stdin);
1198 mr_strip_spaces(outstr);
1199 for (i = 0; possible_responses[i]; i++) {
1200 if (!strcmp(possible_responses[i], outstr)) {
1201 backup_type = possible_bkptypes[i];
1202 }
1203 }
1204 mr_free(outstr);
1205 }
1206 return (backup_type);
1207 }
1208 newtDrawRootText(18, 0, WELCOME_STRING);
1209 if (restoring) {
1210 mr_asprintf(title_sz, "Please choose the backup media from which you want to read data.");
1211 mr_asprintf(minimsg_sz, "Read from:");
1212 } else {
1213 mr_asprintf(title_sz, "Please choose the backup media to which you want to archive data.");
1214 mr_asprintf(minimsg_sz, "Backup to:");
1215 }
1216 newtPushHelpLine(title_sz);
1217 newtCenteredWindow(34, 17, minimsg_sz);
1218 b1 = newtButton(1, 1, "CD-R disks ");
1219 b2 = newtButton(17, 1, "CD-RW disks");
1220 b3 = newtButton(1, 9, "Tape drive ");
1221 b4 = newtButton(17, 5, "USB Key/Disk");
1222 b5 = newtButton(1, 5, " DVD disks ");
1223 b6 = newtButton(17, 9, " Net mount ");
1224 b7 = newtButton(1, 13, " Hard disk ");
1225 b8 = newtButton(17, 13, " Exit ");
1226 myForm = newtForm(NULL, NULL, 0);
1227 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1228 NULL);
1229 b_res = newtRunForm(myForm);
1230 newtFormDestroy(myForm);
1231 newtPopWindow();
1232 if (b_res == b1) {
1233 output = cdr;
1234 } else if (b_res == b2) {
1235 output = cdrw;
1236 } else if (b_res == b3) {
1237 output = tape;
1238 } else if (b_res == b4) {
1239 output = usb;
1240 } else if (b_res == b5) {
1241 output = dvd;
1242 } else if (b_res == b6) {
1243 output = netfs;
1244 } else if (b_res == b7) {
1245 output = iso;
1246 } else {
1247 output = none;
1248 }
1249 newtPopHelpLine();
1250 mr_free(title_sz);
1251 mr_free(minimsg_sz);
1252 return (output);
1253 }
1254
1255
1256
1257
1258/**
1259 * Ask the user which compression type they would like to use.
1260 * The choices are "bzip2", "gzip", "lzo", "lzma". NULL for exit
1261 */
1262 char *which_compression_type() {
1263
1264 /*@ char ************************************************************ */
1265 char *output = NULL;
1266
1267
1268 /*@ newt ************************************************************ */
1269
1270 newtComponent b1;
1271 newtComponent b2;
1272 newtComponent b3;
1273 newtComponent b4;
1274 newtComponent b5;
1275 newtComponent b_res;
1276 newtComponent myForm;
1277
1278 newtDrawRootText(18, 0, WELCOME_STRING);
1279 newtPushHelpLine
1280 (" Please specify the type of compression that you want.");
1281 newtCenteredWindow(34, 13, "Type of compression?");
1282 b1 = newtButton(4, 1, "bzip2");
1283 b2 = newtButton(18, 1, "gzip");
1284 b3 = newtButton(4, 5, "lzo");
1285 b4 = newtButton(18, 5, "lzma");
1286 b5 = newtButton(4, 9, " Exit ");
1287 myForm = newtForm(NULL, NULL, 0);
1288 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1289 b_res = newtRunForm(myForm);
1290 newtFormDestroy(myForm);
1291 newtPopWindow();
1292 if (b_res == b1) {
1293 mr_asprintf(output, "%s", "bzip2");
1294 } else if (b_res == b2) {
1295 mr_asprintf(output, "%s", "gzip");
1296 } else if (b_res == b3) {
1297 mr_asprintf(output, "%s", "lzo");
1298 } else if (b_res == b4) {
1299 mr_asprintf(output, "%s", "lzma");
1300 }
1301 newtPopHelpLine();
1302 return(output);
1303 }
1304
1305
1306/**
1307 * Ask the user how much compression they would like to use.
1308 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1309 * @return The compression level (0-9) chosen, or -1 for "Exit".
1310 */
1311int which_compression_level() {
1312
1313 /*@ char ************************************************************ */
1314 int output = none;
1315
1316
1317 /*@ newt ************************************************************ */
1318
1319 newtComponent b1;
1320 newtComponent b2;
1321 newtComponent b3;
1322 newtComponent b4;
1323 newtComponent b5;
1324 newtComponent b_res;
1325 newtComponent myForm;
1326
1327 newtDrawRootText(18, 0, WELCOME_STRING);
1328 newtPushHelpLine(" Please specify the level of compression that you want.");
1329 newtCenteredWindow(34, 13, "How much compression?");
1330 b1 = newtButton(4, 1, "Maximum (9)");
1331 b2 = newtButton(18, 1, "Average (4)");
1332 b3 = newtButton(4, 5, "Minimum (1)");
1333 b4 = newtButton(18, 5, " None (0) ");
1334 b5 = newtButton(4, 9, " Exit ");
1335 myForm = newtForm(NULL, NULL, 0);
1336 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1337 b_res = newtRunForm(myForm);
1338 newtFormDestroy(myForm);
1339 newtPopWindow();
1340 if (b_res == b1) {
1341 output = 9;
1342 } else if (b_res == b2) {
1343 output = 4;
1344 } else if (b_res == b3) {
1345 output = 1;
1346 } else if (b_res == b4) {
1347 output = 0;
1348 } else if (b_res == b5) {
1349 output = -1;
1350 }
1351 newtPopHelpLine();
1352 return (output);
1353 }
1354
1355
1356
1357
1358
1359/**
1360 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1361 * @p ARBITRARY_MAXIMUM entries.
1362 * @param filelist The filelist structure to load @p source_file into.
1363 * @param source_file The file containing a list of filenames to load into @p filelist.
1364 */
1365int load_filelist_into_array(struct s_filelist *filelist, char *source_file) {
1366
1367 int i;
1368 int j = 0;
1369 bool done;
1370 char *tmp = NULL;
1371 char *tmp1 = NULL;
1372 FILE *fin, *fout;
1373 struct s_filelist_entry dummy_fle;
1374
1375 assert(filelist != NULL);
1376 assert_string_is_neither_NULL_nor_zerolength(source_file);
1377
1378 log_it("entering");
1379 if (!(fin = fopen(source_file, "r"))) {
1380 log_OS_error(source_file);
1381 log_msg(2, "Can't open %s; therefore, cannot popup list",
1382 source_file);
1383 return (1);
1384 }
1385
1386 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
1387 if (!(fout = fopen(tmp1, "a"))) {
1388 log_msg(2, "Can't write to %s", tmp1);
1389 mr_free(tmp1);
1390 return(1);
1391 }
1392
1393 log_msg(2, "Loading %s", source_file);
1394 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM; ) {
1395 mr_free(tmp);
1396 if (feof(fin)) {
1397 break;
1398 }
1399 mr_getline(tmp, fin);
1400 i = (int) strlen(tmp);
1401 if (i < 2) {
1402 continue;
1403 }
1404 if (tmp[i - 1] < 32) {
1405 tmp[--i] = '\0';
1406 }
1407 if (i < 2) {
1408 continue;
1409 }
1410 if (!does_file_exist(tmp) && !feof(fin)) {
1411 j++;
1412 fprintf(fout, "%s\n", tmp);
1413 continue;
1414 }
1415 filelist->el[filelist->entries].severity = severity_of_difference(tmp, NULL);
1416 strcpy(filelist->el[filelist->entries].filename, tmp);
1417 if (feof(fin)) {
1418 break;
1419 }
1420 filelist->entries++;
1421 }
1422 mr_free(tmp);
1423 paranoid_fclose(fin);
1424 paranoid_fclose(fout);
1425 if (j > 0) {
1426 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);
1427 }
1428 mr_free(tmp1);
1429 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1430 log_to_screen("Arbitrary limits suck, man!");
1431 return (1);
1432 }
1433 for (done = FALSE; !done;) {
1434 done = TRUE;
1435 for (i = 0; i < filelist->entries - 1; i++) {
1436// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
1437 if (filelist->el[i].severity < filelist->el[i + 1].severity
1438 || (filelist->el[i].severity ==
1439 filelist->el[i + 1].severity
1440 && strcmp(filelist->el[i].filename,
1441 filelist->el[i + 1].filename) > 0)) {
1442 memcpy((void *) &dummy_fle,
1443 (void *) &(filelist->el[i]),
1444 sizeof(struct s_filelist_entry));
1445 memcpy((void *) &(filelist->el[i]),
1446 (void *) &(filelist->el[i + 1]),
1447 sizeof(struct s_filelist_entry));
1448 memcpy((void *) &(filelist->el[i + 1]),
1449 (void *) &dummy_fle,
1450 sizeof(struct s_filelist_entry));
1451 log_msg(2, "Swapping %s and %s",
1452 filelist->el[i].filename,
1453 filelist->el[i + 1].filename);
1454 done = FALSE;
1455 }
1456 }
1457 }
1458 log_it("leaving");
1459 return (0);
1460 }
1461
1462
1463/**
1464 * Generate a pretty string based on @p flentry.
1465 * @param flentry The filelist entry to stringify.
1466 * @return The string form of @p flentry.
1467 * @note The returned value points to static storage that will be overwritten with each call.
1468 */
1469char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1470 static char comment[100];
1471 char *tmp = NULL;
1472
1473 log_msg(8, "entering");
1474 assert(flentry != NULL);
1475 if (flentry->severity == 0) {
1476 mr_asprintf(tmp, "0 ");
1477 } else if (flentry->severity == 1) {
1478 mr_asprintf(tmp, "low ");
1479 } else if (flentry->severity == 2) {
1480 mr_asprintf(tmp, "med ");
1481 } else {
1482 mr_asprintf(tmp, "high");
1483 }
1484 mr_strcat(tmp, " ");
1485 mr_strcat(tmp, flentry->filename);
1486 strncpy(comment, tmp, 99);
1487 mr_free(tmp);
1488
1489 log_it("leaving");
1490 return (comment);
1491 }
1492
1493
1494/**
1495 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1496 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1497 * @param source_file The file containing a list of changed files.
1498 */
1499void popup_changelist_from_file(char *source_file) {
1500
1501 char *reason = NULL;
1502 newtComponent myForm;
1503 newtComponent bClose;
1504 newtComponent bSelect;
1505 newtComponent b_res;
1506 newtComponent fileListbox;
1507 newtComponent headerMsg;
1508
1509 /*@ ???? ************************************************************ */
1510 void *curr_choice;
1511 void *keylist[ARBITRARY_MAXIMUM];
1512
1513 /*@ int ************************************************************* */
1514 int currline = 0;
1515 int finished = FALSE;
1516
1517 /*@ long ************************************************************ */
1518 long i = 0;
1519 long lng = 0;
1520
1521 /*@ buffers ********************************************************* */
1522 char *tmp = NULL;
1523 char *differ_sz = NULL;
1524
1525 struct s_filelist *filelist;
1526 assert_string_is_neither_NULL_nor_zerolength(source_file);
1527 if (g_text_mode) {
1528 log_msg(2, "Text mode. Therefore, no popup list.");
1529 return;
1530 }
1531 log_msg(2, "Examining file %s", source_file);
1532
1533 lng = count_lines_in_file(source_file);
1534 if (lng < 1) {
1535 log_msg(2, "No lines in file. Therefore, no popup list.");
1536 return;
1537 } else if (lng >= ARBITRARY_MAXIMUM) {
1538 log_msg(2, "Too many files differ for me to list.");
1539 return;
1540 }
1541
1542 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1543 fileListbox =
1544 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1545 newtListboxClear(fileListbox);
1546
1547 if (load_filelist_into_array(filelist, source_file)) {
1548 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
1549 return;
1550 }
1551 log_msg(2, "%d files loaded into filelist array", filelist->entries);
1552 for (i = 0; i < filelist->entries; i++) {
1553 keylist[i] = (void *) i;
1554 newtListboxAppendEntry(fileListbox,
1555 filelist_entry_to_string(&
1556 (filelist->
1557 el[i])),
1558 keylist[i]);
1559 }
1560 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
1561 newtPushHelpLine(differ_sz);
1562 mr_free(differ_sz);
1563
1564 bClose = newtCompactButton(10, 15, " Close ");
1565 bSelect = newtCompactButton(30, 15, " Select ");
1566 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
1567 headerMsg = newtLabel(2, 1, tmp);
1568 mr_free(tmp);
1569
1570 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1571 myForm = newtForm(NULL, NULL, 0);
1572 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose, bSelect, NULL);
1573
1574 while (!finished) {
1575 b_res = newtRunForm(myForm);
1576 if (b_res == bClose) {
1577 finished = TRUE;
1578 } else {
1579 curr_choice = newtListboxGetCurrent(fileListbox);
1580 for (i = 0;
1581 i < filelist->entries && keylist[i] != curr_choice;
1582 i++);
1583 if (i == filelist->entries && filelist->entries > 0) {
1584 log_to_screen("I don't know what that button does!");
1585 } else {
1586 currline = i;
1587 if (filelist->entries > 0) {
1588 malloc_string(reason);
1589 severity_of_difference(filelist->el[currline].filename, &reason);
1590 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
1591
1592 popup_and_OK(tmp);
1593 mr_free(tmp);
1594 paranoid_free(reason);
1595 }
1596 }
1597 }
1598 }
1599 newtFormDestroy(myForm);
1600 newtPopWindow();
1601 newtPopHelpLine();
1602 return;
1603 }
1604
1605/* @} - end of guiGroup */
1606
1607
1608#if __cplusplus
1609} /* extern "C" */
1610#endif
Note: See TracBrowser for help on using the repository browser.