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

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

Still trying to fix the compilation issue

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