source: MondoRescue/branches/2.2.9/mondo/src/common/newt-specific.c@ 2713

Last change on this file since 2713 was 2700, checked in by Bruno Cornec, 13 years ago

r4176@localhost: bruno | 2011-01-26 18:11:02 +0100

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