source: MondoRescue/branches/stable/mondo/src/common/newt-specific.c@ 1594

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

Use of conf file entries (iso_burning_*, media_device, media_size)
and adapatation of the rest of the code to that (including bkpinfo)

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