source: MondoRescue/trunk/mondo/mondo/common/newt-specific.c@ 45

Last change on this file since 45 was 45, checked in by bcornec, 19 years ago

asprint and getline now added

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