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

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

merge -r913:931 $SVN_M/branches/stable

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