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

Last change on this file since 1176 was 1106, checked in by Bruno Cornec, 17 years ago

merge -r1082:1105 $SVN_M/branches/stable

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