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

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

merge -r793:807 $SVN_M/branches/stable
src => common for the moment it's easier to manage merges

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