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

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