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

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

gettext fixes + last and i int removed (were forgotten)

  • Property svn:keywords set to Id
File size: 42.3 KB
Line 
1/* $Id: newt-specific.c 1549 2007-07-23 23:49:52Z 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 1549 2007-07-23 23:49:52Z 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 char *blurb = NULL;
701 char *original_contents = NULL;
702 bool ret = TRUE;
703
704 assert_string_is_neither_NULL_nor_zerolength(title);
705 assert(b != NULL);
706 assert(output != NULL);
707
708 if (g_text_mode) {
709 printf
710 ("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q---\n--> ",
711 title, b);
712 (void) fgets(output, maxsize, stdin);
713 if (output[strlen(output) - 1] == '\n')
714 output[strlen(output) - 1] = '\0';
715 return(ret);
716 }
717 mr_asprintf(&blurb, b);
718 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
719 mr_asprintf(&original_contents, output);
720 output[0] = '\0';
721 type_here =
722 newtEntry(2, newtTextboxGetNumLines(text) + 2,
723 original_contents, 50,
724#ifdef __cplusplus
725 0, NEWT_FLAG_RETURNEXIT
726#else
727 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
728#endif
729 );
730 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, _(" OK "));
731 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, _("Cancel"));
732 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
733 myForm = newtForm(NULL, NULL, 0);
734 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
735 /* BERLIOS: Doesn't work on allocated chains anymore
736 center_string(blurb, 80);
737 */
738 newtPushHelpLine(blurb);
739 mr_free(blurb);
740
741 b_res = newtRunForm(myForm);
742 strcpy(output, entry_value);
743 newtPopHelpLine();
744 if (b_res == b_2) {
745 strcpy(output, original_contents);
746 ret = FALSE;
747 }
748 newtFormDestroy(myForm);
749 newtPopWindow();
750 mr_free(original_contents);
751 return(ret);
752}
753
754
755/**
756 * Pop up a dialog box with user-defined buttons.
757 * @param p The text to put in the dialog box.
758 * @param button1 The label on the first button.
759 * @param button2 The label on the second button, or "" if you only want one button.
760 * @return TRUE if @p button1 was pushed, FALSE otherwise.
761 */
762bool popup_with_buttons(char *p, char *button1, char *button2) {
763
764 /*@ buffers *********************************************************** */
765 char *prompt = NULL;
766 char *tmp = NULL;
767
768 /*@ newt ************************************************************** */
769 newtComponent myForm;
770 newtComponent b_1;
771 newtComponent b_2;
772 newtComponent b_res;
773 newtComponent text;
774
775 assert_string_is_neither_NULL_nor_zerolength(p);
776 assert(button1 != NULL);
777 assert(button2 != NULL);
778
779 if (g_text_mode) {
780 if (strlen(button2) == 0) {
781 printf("%s (%s) --> ", p, button1);
782 } else {
783 printf("%s (%s or %s) --> ", p, button1, button2);
784 }
785 malloc_string(tmp);
786 for (tmp[0] = '\0';
787 strcmp(tmp, button1) && (strlen(button2) == 0
788 || strcmp(tmp, button2));) {
789 printf("--> ");
790 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, stdin);
791 }
792 if (!strcmp(tmp, button1)) {
793 mr_free(tmp);
794 return (TRUE);
795 } else {
796 mr_free(tmp);
797 return (FALSE);
798 }
799 }
800
801 mr_asprintf(&prompt, p);
802 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
803 b_1 =
804 newtButton(20 -
805 ((button2[0] !=
806 '\0') ? strlen(button1) +
807 2 : strlen(button1) / 2),
808 newtTextboxGetNumLines(text) + 3, button1);
809 if (button2[0] != '\0') {
810 b_2 =
811 newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
812 } else {
813 b_2 = NULL;
814 }
815 // newtOpenWindow (25, 5, 46, newtTextboxGetNumLines (text) + 7, "Alert");
816 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, _("Alert"));
817 myForm = newtForm(NULL, NULL, 0);
818 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
819 /* BERLIOS: Doesn't work on allocated chains anymore
820 center_string(prompt, 80);
821 */
822 newtPushHelpLine(prompt);
823 mr_free(prompt);
824
825 b_res = newtRunForm(myForm);
826 newtPopHelpLine();
827 newtFormDestroy(myForm);
828 newtPopWindow();
829 if (b_res == b_1) {
830 return (TRUE);
831 } else {
832 return (FALSE);
833 }
834}
835
836
837/**
838 * Synchronize the log messages stored in @p err_log_lines with those shown
839 * on the screen.
840 */
841void refresh_log_screen(void) {
842
843 /*@ int *********************************************************** */
844 int i = 0;
845
846
847 if (g_text_mode || !err_log_lines) {
848 return;
849 }
850 for (i = g_noof_log_lines - 1; i >= 0; i--) {
851 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
852 " ");
853 }
854 newtRefresh();
855
856 for (i = g_noof_log_lines - 1; i >= 0; i--) {
857 err_log_lines[i][79] = '\0';
858 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
859 err_log_lines[i]);
860 }
861 newtRefresh();
862}
863
864
865/**
866 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
867 * only allocate some memory.
868 */
869void setup_newt_stuff(void) {
870
871 /*@ int *********************************************************** */
872 int i = 0;
873 int cols;
874
875 if (!g_text_mode) {
876 newtInit();
877 newtCls();
878 newtPushHelpLine
879 (_("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed."));
880 newtDrawRootText(18, 0, WELCOME_STRING);
881 newtRefresh();
882 newtGetScreenSize(&cols, &g_noof_rows);
883 g_noof_log_lines = (g_noof_rows / 5) + 1;
884 }
885
886 err_log_lines =
887 (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
888
889 for (i = 0; i < g_noof_log_lines; i++) {
890 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
891 err_log_lines[i][0] = '\0';
892 }
893}
894
895
896/**
897 * Update the evalcall form to show <tt>num</tt> %.
898 * @param num The numerator of the ratio.
899 */
900void update_evalcall_form(int num) {
901
902 /*@ long ************************************************************ */
903 long current_time = 0L;
904 long time_taken = 0L;
905 long time_total_est = 0L;
906 long time_remaining = 0L;
907
908 /*@ buffers ********************************************************** */
909 char *timeline_str = NULL;
910 char *pcline_str = NULL;
911 char *taskprogress = NULL;
912 char *tmp1 = NULL;
913
914 /*@ int ************************************************************** */
915 int percentage = 0;
916 int i = 0;
917 int j = 0;
918
919 if (num < 1) {
920 percentage = 1;
921 } else {
922 percentage = (int) trunc(num);
923 }
924
925 current_time = get_time();
926 time_taken = current_time - g_isoform_starttime;
927 if (num) {
928 time_total_est = time_taken * 100 / num;
929 time_remaining = time_total_est - time_taken;
930 } else {
931 time_remaining = 0;
932 }
933 if (!g_text_mode) {
934 newtLabelSetText(g_isoform_header, g_isoform_header_str);
935 }
936 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
937 if ((percentage < 3 && g_isoform_old_progress < 3)
938 || percentage > g_isoform_old_progress) {
939 g_isoform_old_progress = percentage;
940 mr_asprintf(&timeline_str,
941 _("%2ld:%02ld taken %2ld:%02ld remaining"),
942 time_taken / 60, time_taken % 60, time_remaining / 60,
943 time_remaining % 60);
944 if (percentage < 3) {
945 mr_asprintf(&pcline_str, " Working");
946 for (j = 0; j < g_mysterious_dot_counter; j++) {
947 mr_strcat(pcline_str, ".");
948 }
949 /* BERLIOS: 27 should be a parameter */
950 for (; j < 27; j++) {
951 mr_strcat(pcline_str, " ");
952 }
953 mr_asprintf(&tmp1, " %c", special_dot_char(g_mysterious_dot_counter));
954 mr_strcat(pcline_str, tmp1);
955 mr_free(tmp1);
956 } else {
957 mr_asprintf(&pcline_str,
958 _(" %3d%% done %3d%% to go"),
959 percentage, 100 - percentage);
960 }
961 if (g_text_mode) {
962 mr_asprintf(&taskprogress, "TASK: [");
963 for (i = 0; i < percentage; i += 5) {
964 mr_strcat(taskprogress, "*");
965 }
966 for (; i < 100; i += 5) {
967 mr_strcat(taskprogress, ".");
968 }
969 if (percentage >= 3) {
970 mr_strcat(taskprogress, _("] %3d%% done; %2ld:%02ld to go"), percentage, time_remaining / 60, time_remaining % 60);
971 printf("---evalcall------ %s\n", taskprogress);
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 strcpy(g_blurb_str_2, blurb3);
1001 update_progress_form_full(g_blurb_str_1, g_blurb_str_2,
1002 g_blurb_str_3);
1003}
1004
1005
1006/**
1007 * Update the progress form's complete blurb and show @p g_current_progress.
1008 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1009 * @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.
1010 * @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.
1011 */
1012void update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1013 /*@ long ***************************************************** */
1014 long current_time = 0L;
1015 long time_taken = 0L;
1016 long time_remaining = 0L;
1017 long time_total_est = 0L;
1018
1019 /*@ int ******************************************************* */
1020 int percentage = 0;
1021 int i = 0;
1022
1023 /*@ buffers *************************************************** */
1024 char *percentline_str = NULL;
1025 char *timeline_str = NULL;
1026 char *taskprogress = NULL;
1027
1028 if (!g_text_mode) {
1029 assert(blurb1 != NULL);
1030 assert(blurb2 != NULL);
1031 assert(blurb3 != NULL);
1032 assert(g_timeline != NULL);
1033 }
1034
1035 current_time = get_time();
1036 time_taken = current_time - g_start_time;
1037 if (g_maximum_progress == 0) {
1038 percentage = 0;
1039 } else {
1040 if (g_current_progress > g_maximum_progress) {
1041 mr_msg(0, "update_progress_form_full(%s,%s,%s) --- g_current_progress=%ld; g_maximum_progress=%ld",
1042 blurb1, blurb2, blurb3, g_current_progress,
1043 g_maximum_progress);
1044 g_current_progress = g_maximum_progress;
1045 }
1046 percentage =
1047 (int) ((g_current_progress * 100L) / g_maximum_progress);
1048 }
1049 if (percentage < 1) {
1050 percentage = 1;
1051 }
1052 if (percentage > 100) {
1053 percentage = 100;
1054 }
1055 if (g_current_progress) {
1056 time_total_est =
1057 time_taken * (long) g_maximum_progress /
1058 (long) (g_current_progress);
1059 time_remaining = time_total_est - time_taken;
1060 } else {
1061 time_remaining = 0;
1062 }
1063 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1064 mr_asprintf(&timeline_str,
1065 "%2ld:%02ld taken %2ld:%02ld remaining ",
1066 time_taken / 60, time_taken % 60, time_remaining / 60,
1067 time_remaining % 60);
1068 mr_asprintf(&percentline_str,
1069 " %3d%% done %3d%% to go", percentage,
1070 100 - percentage);
1071
1072 if (g_text_mode) {
1073 /* Not very useful
1074 printf(_("---progress-form---1--- %s\n"), blurb1);
1075 */
1076 printf(_("---progress-form---2--- %s\n"), blurb2);
1077 /* Not very useful
1078 printf(_("---progress-form---3--- %s\n"), blurb3);
1079 */
1080 mr_asprintf(&taskprogress, "TASK: [");
1081 for (i = 0; i < percentage; i += 5) {
1082 mr_strcat(taskprogress, "*");
1083 }
1084 for (; i < 100; i += 5) {
1085 mr_strcat(taskprogress, ".");
1086 }
1087 if (percentage > 100) {
1088 mr_msg(2, _("percentage = %d"), percentage);
1089 }
1090 mr_strcat(taskprogress, _("] %3d%% done; %2ld:%02ld to go"), percentage, time_remaining / 60, time_remaining % 60);
1091 printf(_("---progress-form---4--- %s\n"), taskprogress);
1092 mr_free(taskprogress);
1093 } else {
1094 /* BERLIOS: Doesn't work on allocated chains anymore
1095 center_string(blurb1, 54);
1096 */
1097 /* BERLIOS: Doesn't work on allocated chains anymore
1098 center_string(blurb2, 54);
1099 */
1100 /* BERLIOS: Doesn't work on allocated chains anymore
1101 center_string(blurb3, 54);
1102 */
1103 newtLabelSetText(g_blurb1, blurb1);
1104 newtLabelSetText(g_blurb2, blurb3);
1105 newtLabelSetText(g_blurb3, blurb2);
1106 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
1107 if (percentage >= 2) {
1108 newtLabelSetText(g_timeline, timeline_str);
1109 }
1110 newtLabelSetText(g_percentline, percentline_str);
1111 newtRefresh();
1112 }
1113 mr_free(percentline_str);
1114 mr_free(timeline_str);
1115}
1116
1117
1118/**
1119 * Ask the user which backup media type they would like to use.
1120 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1121 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs,
1122 * and @c iso.
1123 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1124 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1125 */
1126t_bkptype which_backup_media_type(bool restoring) {
1127
1128 /*@ char ************************************************************ */
1129 t_bkptype output;
1130
1131
1132 /*@ newt ************************************************************ */
1133 char *title_sz = NULL;
1134 char *minimsg_sz = NULL;
1135 static t_bkptype possible_bkptypes[] =
1136 { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso, usb };
1137 static char *possible_responses[] =
1138 { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev",
1139"nfs", "iso", "usb", NULL };
1140 char *outstr = NULL;
1141 t_bkptype backup_type;
1142 int i = 0;
1143
1144 newtComponent b1;
1145 newtComponent b2;
1146 newtComponent b3;
1147 newtComponent b4;
1148 newtComponent b5;
1149 newtComponent b6;
1150 newtComponent b7;
1151 newtComponent b8;
1152 newtComponent b_res;
1153 newtComponent myForm;
1154
1155 outstr = mr_malloc(MAX_NEWT_COMMENT_LEN);
1156 if (g_text_mode) {
1157 for (backup_type = none; backup_type == none;) {
1158 printf(_("Backup type ("));
1159 for (i = 0; possible_responses[i]; i++) {
1160 printf("%c%s", (i == 0) ? '\0' : ' ',
1161 possible_responses[i]);
1162 }
1163 printf(")\n--> ");
1164 (void) fgets(outstr, MAX_NEWT_COMMENT_LEN, stdin);
1165 mr_strip_spaces(outstr);
1166 for (i = 0; possible_responses[i]; i++) {
1167 if (!strcmp(possible_responses[i], outstr)) {
1168 backup_type = possible_bkptypes[i];
1169 }
1170 }
1171 }
1172 mr_free(outstr);
1173 return (backup_type);
1174 }
1175 newtDrawRootText(18, 0, WELCOME_STRING);
1176 if (restoring) {
1177 mr_asprintf(&title_sz,
1178 _("Please choose the backup media from which you want to read data."));
1179 mr_asprintf(&minimsg_sz, _("Read from:"));
1180 } else {
1181 mr_asprintf(&title_sz,
1182 _("Please choose the backup media to which you want to archive data."));
1183 mr_asprintf(&minimsg_sz, _("Backup to:"));
1184 }
1185 newtPushHelpLine(title_sz);
1186 mr_free(title_sz);
1187
1188 // newtOpenWindow (23, 3, 34, 17, minimsg_sz);
1189 newtCenteredWindow(34, 17, minimsg_sz);
1190 mr_free(minimsg_sz);
1191
1192 /* BERLIOS: USB ?? */
1193 b1 = newtButton(1, 1, _("CD-R disks "));
1194 b2 = newtButton(17, 1, _("CD-RW disks"));
1195 b3 = newtButton(1, 9, _("Tape drive "));
1196 b4 = newtButton(17, 5, _("CD streamer"));
1197 b5 = newtButton(1, 5, _(" DVD disks "));
1198 b6 = newtButton(17, 9, _(" NFS mount "));
1199 b7 = newtButton(1, 13, _(" Hard disk "));
1200 b8 = newtButton(17, 13, _(" Exit "));
1201 myForm = newtForm(NULL, NULL, 0);
1202 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1203 NULL);
1204 b_res = newtRunForm(myForm);
1205 newtFormDestroy(myForm);
1206 newtPopWindow();
1207 if (b_res == b1) {
1208 output = cdr;
1209 } else if (b_res == b2) {
1210 output = cdrw;
1211 } else if (b_res == b3) {
1212 output = tape;
1213 } else if (b_res == b4) {
1214 output = cdstream;
1215 } else if (b_res == b5) {
1216 output = dvd;
1217 } else if (b_res == b6) {
1218 output = nfs;
1219 } else if (b_res == b7) {
1220 output = iso;
1221 } else {
1222 output = none;
1223 }
1224 newtPopHelpLine();
1225 mr_free(outstr);
1226 return (output);
1227}
1228
1229
1230/**
1231 * Ask the user how much compression they would like to use.
1232 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1233 * @return The compression level (0-9) chosen, or -1 for "Exit".
1234 */
1235int which_compression_level(void) {
1236
1237 /*@ char ************************************************************ */
1238 int output = none;
1239
1240 /*@ newt ************************************************************ */
1241 newtComponent b1;
1242 newtComponent b2;
1243 newtComponent b3;
1244 newtComponent b4;
1245 newtComponent b5;
1246 newtComponent b_res;
1247 newtComponent myForm;
1248
1249 newtDrawRootText(18, 0, WELCOME_STRING);
1250 newtPushHelpLine
1251 (_(" Please specify the level of compression that you want."));
1252 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
1253 newtCenteredWindow(34, 13, _("How much compression?"));
1254 b1 = newtButton(4, 1, _("Maximum"));
1255 b2 = newtButton(18, 1, _("Average"));
1256 b3 = newtButton(4, 5, _("Minimum"));
1257 b4 = newtButton(18, 5, _(" None "));
1258 b5 = newtButton(4, 9, _(" Exit "));
1259 myForm = newtForm(NULL, NULL, 0);
1260 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1261 b_res = newtRunForm(myForm);
1262 newtFormDestroy(myForm);
1263 newtPopWindow();
1264 if (b_res == b1) {
1265 output = 9;
1266 } else if (b_res == b2) {
1267 output = 4;
1268 } else if (b_res == b3) {
1269 output = 1;
1270 } else if (b_res == b4) {
1271 output = 0;
1272 } else if (b_res == b5) {
1273 output = -1;
1274
1275 }
1276 newtPopHelpLine();
1277 return (output);
1278}
1279
1280
1281/**
1282 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1283 * @p ARBITRARY_MAXIMUM entries.
1284 * @param filelist The filelist structure to load @p source_file into.
1285 * @param source_file The file containing a list of filenames to load into @p filelist.
1286 */
1287int load_filelist_into_array(struct s_filelist *filelist,
1288 char *source_file) {
1289 int i;
1290 bool done;
1291 char *tmp = NULL;
1292 size_t n = 0;
1293 FILE *fin = NULL;
1294 struct s_filelist_entry dummy_fle;
1295
1296 assert(filelist != NULL);
1297 assert_string_is_neither_NULL_nor_zerolength(source_file);
1298
1299 iamhere("entering");
1300 if (!(fin = fopen(source_file, "r"))) {
1301 log_OS_error(source_file);
1302 mr_msg(2, "Can't open %s; therefore, cannot popup list",
1303 source_file);
1304 return (1);
1305 }
1306 mr_msg(2, "Loading %s", source_file);
1307 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM;
1308 filelist->entries++) {
1309 god_i_hate_gotos:
1310 if (feof(fin)) {
1311 break;
1312 }
1313 mr_getline(&tmp, &n, fin);
1314 i = (int) strlen(tmp);
1315 if (i < 2) {
1316 goto god_i_hate_gotos;
1317 }
1318 if (tmp[i - 1] < 32) {
1319 tmp[--i] = '\0';
1320 }
1321 if (i < 2) {
1322 goto god_i_hate_gotos;
1323 }
1324 if (!does_file_exist(tmp)) {
1325 goto god_i_hate_gotos;
1326 }
1327 filelist->el[filelist->entries].severity =
1328 severity_of_difference(tmp, NULL);
1329 strcpy(filelist->el[filelist->entries].filename, tmp);
1330 if (feof(fin)) {
1331 break;
1332 }
1333 }
1334 mr_free(tmp);
1335
1336 paranoid_fclose(fin);
1337 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1338 log_to_screen(_("Arbitrary limits suck, man!"));
1339 return (1);
1340 }
1341 for (done = FALSE; !done;) {
1342 done = TRUE;
1343 for (i = 0; i < filelist->entries - 1; i++) {
1344 if (filelist->el[i].severity < filelist->el[i + 1].severity
1345 || (filelist->el[i].severity ==
1346 filelist->el[i + 1].severity
1347 && strcmp(filelist->el[i].filename,
1348 filelist->el[i + 1].filename) > 0)) {
1349 memcpy((void *) &dummy_fle,
1350 (void *) &(filelist->el[i]),
1351 sizeof(struct s_filelist_entry));
1352 memcpy((void *) &(filelist->el[i]),
1353 (void *) &(filelist->el[i + 1]),
1354 sizeof(struct s_filelist_entry));
1355 memcpy((void *) &(filelist->el[i + 1]),
1356 (void *) &dummy_fle,
1357 sizeof(struct s_filelist_entry));
1358 mr_msg(2, "Swapping %s and %s",
1359 filelist->el[i].filename,
1360 filelist->el[i + 1].filename);
1361 done = FALSE;
1362 }
1363 }
1364 }
1365 iamhere("leaving");
1366 return (0);
1367}
1368
1369
1370/**
1371 * Generate a pretty string based on @p flentry.
1372 * @param flentry The filelist entry to stringify.
1373 * @return The string form of @p flentry.
1374 * @note The returned value points to static storage that will be overwritten with each call.
1375 */
1376char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1377 static char comment[100];
1378 char *tmp = NULL;
1379
1380 iamhere("entering");
1381 malloc_string(tmp);
1382 assert(flentry != NULL);
1383 if (flentry->severity == 0) {
1384 mr_asprintf(&tmp, "0 %s", flentry->filename);
1385 } else if (flentry->severity == 1) {
1386 mr_asprintf(&tmp, "low %s", flentry->filename);
1387 } else if (flentry->severity == 2) {
1388 mr_asprintf(&tmp, "med %s", flentry->filename);
1389 } else {
1390 mr_asprintf(&tmp, "high %s", flentry->filename);
1391 }
1392 if (strlen(tmp) > 98) {
1393 tmp[98] = '\0';
1394 }
1395 strcpy(comment, tmp);
1396 mr_free(tmp);
1397 iamhere("leaving");
1398 return(comment);
1399}
1400
1401
1402/**
1403 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1404 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1405 * @param source_file The file containing a list of changed files.
1406 */
1407 void popup_changelist_from_file(char *source_file) {
1408 char *reason = NULL;
1409 newtComponent myForm;
1410 newtComponent bClose;
1411 newtComponent bSelect;
1412 newtComponent b_res;
1413 newtComponent fileListbox;
1414 newtComponent headerMsg;
1415
1416 /*@ ???? ************************************************************ */
1417 void *curr_choice = NULL;
1418 void *keylist[ARBITRARY_MAXIMUM];
1419
1420 /*@ int ************************************************************* */
1421 int currline = 0;
1422 int finished = FALSE;
1423
1424 /*@ long ************************************************************ */
1425 long i = 0L;
1426 long lng = 0L;
1427
1428 /*@ buffers ********************************************************* */
1429 char *tmp = NULL;
1430 char *differ_sz = NULL;
1431 struct s_filelist *filelist = NULL;
1432
1433 assert_string_is_neither_NULL_nor_zerolength(source_file);
1434 if (g_text_mode) {
1435 mr_msg(2, "Text mode. Therefore, no popup list.");
1436 return;
1437 }
1438 mr_msg(2, "Examining file %s", source_file);
1439
1440 lng = count_lines_in_file(source_file);
1441 if (lng < 1) {
1442 mr_msg(2, "No lines in file. Therefore, no popup list.");
1443 return;
1444 } else if (lng >= ARBITRARY_MAXIMUM) {
1445 mr_msg(2, "Too many files differ for me to list.");
1446 return;
1447 }
1448
1449 filelist = (struct s_filelist *) mr_malloc(sizeof(struct s_filelist));
1450 fileListbox =
1451 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1452 newtListboxClear(fileListbox);
1453
1454 if (load_filelist_into_array(filelist, source_file)) {
1455 mr_msg(2, "Can't open %s; therefore, cannot popup list",
1456 source_file);
1457 return;
1458 }
1459 mr_msg(2, "%d files loaded into filelist array",
1460 filelist->entries);
1461 for (i = 0; i < filelist->entries; i++) {
1462 keylist[i] = (void *) i;
1463 newtListboxAppendEntry(fileListbox,
1464 filelist_entry_to_string(&
1465 (filelist->
1466 el[i])),
1467 keylist[i]);
1468 }
1469 mr_asprintf(&differ_sz,
1470 _(" %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list."),
1471 i);
1472 newtPushHelpLine(differ_sz);
1473 mr_free(differ_sz);
1474
1475 bClose = newtCompactButton(10, 15, _(" Close "));
1476 bSelect = newtCompactButton(30, 15, _(" Select "));
1477 mr_asprintf(&tmp, "%-10s %-20s", _("Priority"),
1478 _("Filename"));
1479 headerMsg = newtLabel(2, 1, tmp);
1480 mr_free(tmp);
1481
1482 newtOpenWindow(5, 4, 70, 16, _("Non-matching files"));
1483 myForm = newtForm(NULL, NULL, 0);
1484 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1485 bSelect, NULL);
1486 while (!finished) {
1487 b_res = newtRunForm(myForm);
1488 if (b_res == bClose) {
1489 finished = TRUE;
1490 } else {
1491 curr_choice = newtListboxGetCurrent(fileListbox);
1492 for (i = 0;
1493 i < filelist->entries && keylist[i] != curr_choice;
1494 i++);
1495 if (i == filelist->entries && filelist->entries > 0) {
1496 log_to_screen(_("I don't know what that button does!"));
1497 } else {
1498 currline = i;
1499 if (filelist->entries > 0) {
1500 severity_of_difference(filelist->el[currline].
1501 filename, reason);
1502 mr_asprintf(&tmp, "%s --- %s",
1503 filelist->el[currline].filename, reason);
1504 popup_and_OK(tmp);
1505 mr_free(tmp);
1506 }
1507 }
1508 }
1509 }
1510 newtFormDestroy(myForm);
1511 newtPopWindow();
1512 newtPopHelpLine();
1513 return;
1514 }
1515
1516/* @} - end of guiGroup */
1517
1518
1519#if __cplusplus
1520} /* extern "C" */
1521#endif
1522
1523
1524void wait_until_software_raids_are_prepped(int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.