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

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

strip_spaces => mr_strip_spaces in mr_str.c and corrected at the same time :-)

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