source: MondoRescue/branches/3.1/mondo/src/common/newt-specific.c@ 3147

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