source: MondoRescue/trunk/mondo/mondo/common/newt-specific.c@ 219

Last change on this file since 219 was 219, checked in by bcornec, 18 years ago

merge -r176:218 $SVN_M/branches/2.05

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