source: MondoRescue/branches/2.2.10/mondo/src/common/newt-specific.c@ 2334

Last change on this file since 2334 was 2334, checked in by Bruno Cornec, 15 years ago

r3369@localhost: bruno | 2009-08-18 16:57:27 +0200

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