source: MondoRescue/branches/3.0/mondo/src/common/newt-specific.c@ 3188

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