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

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

Some define cleanup in my-stuff.h

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