source: MondoRescue/branches/2.2.9/mondo/src/common/newt-specific.c@ 2388

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