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

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

Merges in trunk modifications coming from stable (common)

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