source: MondoRescue/branches/2.2.10/mondo/src/common/newt-specific.c@ 2357

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