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

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

miising extern bkpinfo in newt-specific.c

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