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

Last change on this file since 820 was 797, checked in by andree, 18 years ago

newt-specific.c:
Fixed compile error by removing superfluous quotes in
kill_anything_like_this().

libmondo-fork.c:
Fixed compiler warning on 64bit platforms regarding the output of the
result of ftello() by casting it to long long (and it's actually
signed).

libmondo-fifo.c:
Added %s for ps_options in kill_buffer(). Also fixes compiler warning.

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