source: MondoRescue/branches/2.2.5/mondo/src/common/newt-specific.c@ 1662

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

Fix bug #197 (based on an initial patch of Scott Cummings)

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