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

Last change on this file since 837 was 837, checked in by Bruno Cornec, 18 years ago

Compilation Fix

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