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

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

r3144@localhost: bruno | 2009-06-26 12:18:08 +0200

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