source: MondoRescue/branches/2.2.10/mondo/src/common/newt-specific.c@ 2316

Last change on this file since 2316 was 2316, checked in by Bruno Cornec, 15 years ago

r3327@localhost: bruno | 2009-08-04 00:17:46 +0200
popup_and_get_string and build_partition_name now return an allocated string

  • Property svn:keywords set to Id
File size: 45.4 KB
RevLine 
[1]1/* newt-specific.c
[99]2 $Id: newt-specific.c 2316 2009-08-18 12:34:29Z 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 2316 2009-08-18 12:34:29Z 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.
[2277]98 long g_start_time = 0L; ///< The time (in seconds since the epoch) that the progress form was opened.
[128]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.
103 char *g_selfmounted_isodir; ///< Holds the NFS 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();
[2291]226
227 /* Reset globals */
[128]228 g_isoform_main = NULL;
229 g_isoform_old_progress = -1;
[2291]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();
[2291]259
260 /* Reset globals */
[128]261 g_progressForm = NULL;
262 g_current_progress = -999;
[2291]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 newtFinished();
361 }
[1]362
[128]363 printf("---FATALERROR--- %s\n", error_string);
[213]364 printf
[541]365 ("If you require technical support, please contact the mailing list.\n");
366 printf("See http://www.mondorescue.org for details.\n");
[213]367 printf
[541]368 ("The list's members can help you, if you attach that file to your e-mail.\n");
369 printf("Log file: %s\n", MONDO_LOGFILE);
370 printf("Mondo has aborted.\n");
[128]371 if (!g_main_pid) {
372 log_msg(3, "FYI - g_main_pid is blank");
373 }
374 paranoid_free(tmp);
375 paranoid_free(command);
376 finish(254);
377 }
[1]378
[2241]379/* Free memory allocated for newt */
380void free_newt_stuff() {
[1]381
[2241]382int i;
[1]383
[2241]384for (i = 0; i < g_noof_log_lines; i++) {
385 mr_free(err_log_lines[i]);
386}
387mr_free(err_log_lines);
388}
389
390
391
[1]392/**
393 * Exit Mondo normally.
394 * @param signal The exit code (0 indicates a successful backup; 1 for Mondo means the
395 * user aborted; 254 means a fatal error occured).
396 * @note This function never returns.
397 */
[128]398 void
399 finish(int signal) {
[1708]400 char *command = NULL;
[1]401
[128]402 chdir("/");
403 run_program_and_log_output("umount " MNT_CDROM, FALSE);
404 if (g_selfmounted_isodir) {
[2211]405 mr_asprintf(&command, "umount %s", g_selfmounted_isodir);
[128]406 run_program_and_log_output(command, 1);
[1708]407 paranoid_free(command);
[2211]408 mr_asprintf(&command, "rmdir %s", g_selfmounted_isodir);
[128]409 run_program_and_log_output(command, 1);
[1708]410 paranoid_free(command);
[128]411 }
412 if (!g_text_mode) {
413 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
414 log_msg(1, "Calling newtFinished()");
415 newtFinished();
416 } else {
417 log_msg(1, "Calling newtSuspend()");
418 newtSuspend();
419 }
420 }
[541]421 printf("Execution run ended; result=%d\n", signal);
422 printf("Type 'less %s' to see the output log\n", MONDO_LOGFILE);
[2241]423 if (bkpinfo->tmpdir) {
424 log_msg(8,"tempdir is %s",bkpinfo->tmpdir);
425 if (strstr(bkpinfo->tmpdir ,"mondo.tmp.") != NULL) {
426 log_msg(8,"erasing tempdir");
427 mr_asprintf(&command, "rm -Rf %s", bkpinfo->tmpdir);
428 system(command);
429 paranoid_free(command);
430 }
[1999]431 }
[2241]432 if (bkpinfo->scratchdir) {
433 log_msg(8,"scratchdir is %s", bkpinfo->scratchdir);
434 if (strstr(bkpinfo->scratchdir ,"mondo.scratch.") != NULL) {
435 log_msg(8,"erasing scratchdir");
436 mr_asprintf(&command, "rm -Rf %s", bkpinfo->scratchdir);
437 system(command);
438 paranoid_free(command);
439 }
440 }
441 /* Free all allocated strings in bkpinfo */
442 mr_free(bkpinfo->nfs_user);
443
444 /* Then free the structure */
[1703]445 paranoid_free(bkpinfo);
[128]446 free_libmondo_global_strings();
[2241]447 free_newt_stuff();
[128]448 exit(signal);
449 }
[1]450
451
452
453
454
455/**
456 * Log the last @p g_noof_log_lines lines of @p filename that match @p
457 * grep_for_me to the screen.
458 * @param filename The file to give the end of.
459 * @param grep_for_me If not "", then only give lines in @p filename that match this regular expression.
460 */
[128]461 void
462 log_file_end_to_screen(char *filename, char *grep_for_me) {
[1]463
[128]464 /*@ buffers ********************************************************** */
465 char *command;
466 char *tmp;
[1]467
[128]468 /*@ pointers ********************************************************* */
469 FILE *fin;
[1]470
[128]471 /*@ int ************************************************************** */
472 int i = 0;
[1]473
[128]474 malloc_string(command);
475 malloc_string(tmp);
476 assert_string_is_neither_NULL_nor_zerolength(filename);
477 assert(grep_for_me != NULL);
[1]478
[128]479 if (!does_file_exist(filename)) {
480 paranoid_free(command);
481 paranoid_free(tmp);
482 return;
[1]483 }
[128]484 if (grep_for_me[0] != '\0') {
485 sprintf(command, "grep '%s' %s | tail -n%d",
486 grep_for_me, filename, g_noof_log_lines);
487 } else {
[144]488 sprintf(command, "tail -n%d %s", g_noof_log_lines,
[128]489 filename);
[1]490 }
[128]491 fin = popen(command, "r");
492 if (!fin) {
493 log_OS_error(command);
494 } else {
495 for (i = 0; i < g_noof_log_lines; i++) {
496 for (err_log_lines[i][0] = '\0';
497 strlen(err_log_lines[i]) < 2 && !feof(fin);) {
498 (void) fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN,
499 fin);
[1274]500 /* Commented to make valgrind happy and avoid crash
[128]501 strip_spaces(err_log_lines[i]);
[1274]502 */
[128]503 if (!strncmp(err_log_lines[i], "root:", 5)) {
504 strcpy(tmp, err_log_lines[i] + 6);
505 strcpy(err_log_lines[i], tmp);
506 }
507 if (feof(fin)) {
508 break;
509 }
510 }
511 }
512 paranoid_pclose(fin);
513 }
514 refresh_log_screen();
515 paranoid_free(command);
516 paranoid_free(tmp);
[1]517 }
518
519
520/**
521 * Log a message to the screen.
522 * @param fmt A printf-style format string to write. The following parameters are its arguments.
523 * @note The message is also written to the logfile.
524 */
[128]525 void
526 log_to_screen(const char *fmt, ...) {
[1]527
[128]528 /*@ int ************************************************************** */
529 int i = 0;
530 int j = 0;
531 va_list args;
[1]532
[128]533 /*@ buffers ********************************************************** */
534 char *output;
[1]535
[128]536 malloc_string(output);
[1]537
[128]538 va_start(args, fmt);
539 vsprintf(output, fmt, args);
540 log_msg(0, output);
541 output[80] = '\0';
542 va_end(args);
543 i = (int) strlen(output);
544 if (i > 0 && output[i - 1] < 32) {
545 output[i - 1] = '\0';
546 }
[1]547
[128]548 if (err_log_lines) {
549 for (i = 1; i < g_noof_log_lines; i++) {
550 strcpy(err_log_lines[i - 1],
551 " ");
552 strcpy(err_log_lines[i - 1], err_log_lines[i]);
553 }
554 }
555 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
556 output[strlen(output) - 1] = '\0';
557 }
558 for (j = 0; j < (int) strlen(output); j++) {
559 if (output[j] < 32) {
560 output[j] = ' ';
561 }
562 }
563 if (err_log_lines)
564 strcpy(err_log_lines[g_noof_log_lines - 1], output);
565 if (g_text_mode) {
566 printf("%s\n", output);
567 } else {
568 refresh_log_screen();
569 }
570 paranoid_free(output);
[1]571 }
572
573
574
575
576/**
577 * Write a string to the root window at (@p x, @p y) and also to the logfile.
578 * @param y The row to write the string to.
579 * @param x The column to write the string to.
580 * @param output The string to write.
581 */
[128]582 void
583 mvaddstr_and_log_it(int y, int x, char *output) {
584 assert_string_is_neither_NULL_nor_zerolength(output);
585 log_msg(0, output);
586 if (g_text_mode) {
587 printf("%s\n", output);
588 } else {
589 newtDrawRootText(x, y, output);
590 newtRefresh();
591 }
592 }
[1]593
594
595
596
597/**
598 * Open an evalcall form with title @p ttl.
599 * @param ttl The title to use for the evalcall form.
600 */
[128]601 void
602 open_evalcall_form(char *ttl) {
[1]603
[128]604 /*@ buffers ********************************************************* */
605 char *title;
606 char *tmp;
[1]607
[128]608 /*@ initialize ****************************************************** */
609 g_isoform_old_progress = -1;
610 g_mysterious_dot_counter = 0;
611 malloc_string(title);
612 malloc_string(tmp);
[1]613
[128]614 assert(ttl != NULL);
615 strcpy(title, ttl);
616 strcpy(g_isoform_header_str, title);
[1]617// center_string (title, 80);
[128]618 if (g_text_mode) {
619 log_msg(0, title);
620 } else {
621 strcpy(tmp, title);
622 center_string(tmp, 80);
623 newtPushHelpLine(tmp);
624 }
625 center_string(g_isoform_header_str, 36);
626 g_isoform_starttime = get_time();
627 if (g_text_mode) {
628 log_msg(0, g_isoform_header_str);
629 } else {
630 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
631 g_isoform_scale = newtScale(3, 3, 34, 100);
632 newtCenteredWindow(40, 7, title);
633 g_isoform_main = newtForm(NULL, NULL, 0);
634 g_isoform_timeline = newtLabel(1, 5, "This is the timeline");
635 g_isoform_pcline = newtLabel(1, 6, "This is the pcline");
636 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
637 g_isoform_pcline, g_isoform_header,
638 g_isoform_scale, NULL);
639 newtRefresh();
640 }
641 update_evalcall_form(0);
642 paranoid_free(tmp);
643 paranoid_free(title);
644 }
[1]645
646
647
648/**
649 * Open a progress form with title @p title.
650 * @param title The title to use for the progress form (will be put in the title bar on Newt).
651 * @param b1 The first line of the blurb; generally static.
652 * @param b2 The second line of the blurb; generally static.
653 * @param b3 The third line of the blurb; generally dynamic (it is passed
654 * to update_evalcall_form() every time).
655 * @param max_val The maximum amount of progress (number of filesets, etc.)
656 */
[128]657 void
658 open_progress_form(char *title, char *b1, char *b2, char *b3,
659 long max_val) {
[1]660
[128]661 /*@ buffers ********************************************************* */
662 char *b1c;
663 char *blurb1;
664 char *blurb2;
665 char *blurb3;
[1]666
[128]667 /*@ initialize ****************************************************** */
668 g_mysterious_dot_counter = 0;
[1]669
[128]670 malloc_string(b1c);
671 malloc_string(blurb1);
672 malloc_string(blurb2);
673 malloc_string(blurb3);
[1]674
[128]675 assert(title != NULL);
676 assert(b1 != NULL);
677 assert(b2 != NULL);
678 assert(b3 != NULL);
[1]679
[128]680 strcpy(blurb1, b1);
681 strcpy(blurb2, b2);
682 strcpy(blurb3, b3);
683 strcpy(b1c, b1);
684 center_string(b1c, 80);
685 if (max_val <= 0) {
686 max_val = 1;
687 }
[1]688
[128]689 g_start_time = get_time();
690 g_maximum_progress = max_val;
691 g_current_progress = 0;
692 strcpy(g_blurb_str_1, blurb1);
693 strcpy(g_blurb_str_2, blurb3);
694 strcpy(g_blurb_str_3, blurb2);
695 if (g_text_mode) {
696 log_msg(0, blurb1);
697 log_msg(0, blurb2);
698 log_msg(0, blurb3);
699 } else {
700 g_blurb1 = newtLabel(2, 1, blurb1);
701 g_blurb2 = newtLabel(2, 2, blurb3);
702 g_blurb3 = newtLabel(2, 4, blurb2);
703 // newtOpenWindow (10, 4, 60, 11, title);
704 newtCenteredWindow(60, 11, title);
705 g_scale = newtScale(3, 6, 54, g_maximum_progress);
706 g_progressForm = newtForm(NULL, NULL, 0);
707 g_percentline = newtLabel(10, 9, "This is the percentline");
708 g_timeline = newtLabel(10, 8, "This is the timeline");
709 newtFormAddComponents(g_progressForm, g_percentline,
710 g_timeline, g_scale, g_blurb1, g_blurb3,
711 g_blurb2, NULL);
712 newtPushHelpLine(b1c);
713 newtRefresh();
714 }
715 update_progress_form_full(blurb1, blurb2, blurb3);
716 paranoid_free(b1c);
717 paranoid_free(blurb1);
718 paranoid_free(blurb2);
719 paranoid_free(blurb3);
720 }
721
[1]722/**
723 * Give a message to the user in the form of a dialog box (under Newt).
724 * @param prompt The message.
725 */
[128]726 void
727 popup_and_OK(char *prompt) {
728 char ch;
[1]729
[128]730 assert_string_is_neither_NULL_nor_zerolength(prompt);
[1]731
[128]732 log_msg(0, prompt);
733 if (g_text_mode) {
734 printf
[1899]735 ("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ",
[128]736 prompt);
737 while (((ch = getchar()) != '\n') && (ch != EOF));
738 } else {
[541]739 (void) popup_with_buttons(prompt, " OK ", "");
[128]740 }
741 }
[1]742
743/**
744 * Ask the user to enter a value.
745 * @param title The title of the dialog box.
746 * @param b The blurb (e.g. what you want the user to enter).
[2316]747 * @param input The string given originaly as default
748 * @return output string if the user pressed OK, NULL if they pressed Cancel.
[1]749 */
[2316]750 char *popup_and_get_string(char *title, char *b, char *input) {
[1]751
[128]752 /*@ newt ************************************************************ */
753 newtComponent myForm;
754 newtComponent b_1;
755 newtComponent b_2;
756 newtComponent b_res;
757 newtComponent text;
758 newtComponent type_here;
[1]759
[128]760 /*@ pointers ********************************************************* */
761 char *entry_value;
[1]762
[128]763 /*@ buffers ********************************************************** */
[2316]764 char *blurb = NULL;
765 char *output = NULL;
[1]766
[128]767 assert_string_is_neither_NULL_nor_zerolength(title);
768 assert(b != NULL);
[1]769
[128]770 if (g_text_mode) {
[2316]771 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q--- [%s]\n--> ", title, b, input);
772 mr_getline(&output, stdin);
773 if (! strcmp(output,"\n")) {
774 /* take default if ENTER was typed */
775 mr_free(output);
776 mr_asprintf(&output, "%s", input);
777 }
[128]778 if (output[strlen(output) - 1] == '\n')
779 output[strlen(output) - 1] = '\0';
[2316]780 return (output);
[128]781 }
[2316]782
783 mr_asprintf(&blurb, "%s", b);
[128]784 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
785 type_here =
786 newtEntry(2, newtTextboxGetNumLines(text) + 2,
[2316]787 input, 50,
[1]788#ifdef __cplusplus
[128]789 0, NEWT_FLAG_RETURNEXIT
[1]790#else
[128]791 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
[1]792#endif
[128]793 );
[541]794 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
795 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
[128]796 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
797 myForm = newtForm(NULL, NULL, 0);
798 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
799 center_string(blurb, 80);
800 newtPushHelpLine(blurb);
801 b_res = newtRunForm(myForm);
[2316]802
803 if (b_res == b_1) {
804 mr_asprintf(&output, "%s", newtEntryGetValue(type_here));
805 }
[128]806 newtPopHelpLine();
807 newtFormDestroy(myForm);
808 newtPopWindow();
[2316]809 mr_free(blurb);
810
811 return (output);
[128]812 }
[1]813
814
815/**
816 * Pop up a dialog box with user-defined buttons.
817 * @param p The text to put in the dialog box.
818 * @param button1 The label on the first button.
819 * @param button2 The label on the second button, or "" if you only want one button.
820 * @return TRUE if @p button1 was pushed, FALSE otherwise.
821 */
[128]822 bool popup_with_buttons(char *p, char *button1, char *button2) {
[1]823
[128]824 /*@ buffers *********************************************************** */
[2136]825 char *prompt;
[1]826
[128]827 /*@ newt ************************************************************** */
828 newtComponent myForm;
829 newtComponent b_1;
830 newtComponent b_2;
831 newtComponent b_res;
832 newtComponent text;
[1]833
[128]834 assert_string_is_neither_NULL_nor_zerolength(p);
835 assert(button1 != NULL);
836 assert(button2 != NULL);
[2136]837 prompt = malloc(MAX_NEWT_COMMENT_LEN);
[128]838 if (g_text_mode) {
839 if (strlen(button2) == 0) {
840 printf("%s (%s) --> ", p, button1);
841 } else {
842 printf("%s (%s or %s) --> ", p, button1, button2);
843 }
[2136]844 for (prompt[0] = '\0';
845 strcmp(prompt, button1) && (strlen(button2) == 0
846 || strcmp(prompt, button2));) {
[128]847 printf("--> ");
[2136]848 (void) fgets(prompt, MAX_NEWT_COMMENT_LEN, stdin);
[128]849 }
[2136]850 if (!strcmp(prompt, button1)) {
[128]851 paranoid_free(prompt);
852 return (TRUE);
853 } else {
854 paranoid_free(prompt);
855 return (FALSE);
856 }
857 }
[1]858
[877]859 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
[883]860 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
[128]861 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
862 b_1 =
863 newtButton(20 -
864 ((button2[0] !=
865 '\0') ? strlen(button1) +
866 2 : strlen(button1) / 2),
867 newtTextboxGetNumLines(text) + 3, button1);
868 if (button2[0] != '\0') {
869 b_2 =
870 newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
871 } else {
872 b_2 = NULL;
873 }
874 // newtOpenWindow (25, 5, 46, newtTextboxGetNumLines (text) + 7, "Alert");
[541]875 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
[128]876 myForm = newtForm(NULL, NULL, 0);
877 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
878 center_string(prompt, 80);
879 newtPushHelpLine(prompt);
880 b_res = newtRunForm(myForm);
881 newtPopHelpLine();
882 newtFormDestroy(myForm);
883 newtPopWindow();
884 if (b_res == b_1) {
885 paranoid_free(prompt);
886 return (TRUE);
887 } else {
888 paranoid_free(prompt);
889 return (FALSE);
890 }
891 }
[1]892
893
894
[128]895
[1]896/**
897 * Synchronize the log messages stored in @p err_log_lines with those shown
898 * on the screen.
899 */
[128]900 void
901 refresh_log_screen() {
[1]902
[128]903 /*@ int *********************************************************** */
904 int i = 0;
[1]905
906
[128]907 if (g_text_mode || !err_log_lines) {
908 return;
909 }
910 for (i = g_noof_log_lines - 1; i >= 0; i--) {
911 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
912 " ");
913 }
914 newtRefresh();
915 for (i = g_noof_log_lines - 1; i >= 0; i--) {
916 err_log_lines[i][79] = '\0';
917 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
918 err_log_lines[i]);
919 }
920 newtRefresh();
921 }
[1]922
923
924/**
925 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
926 * only allocate some memory.
927 */
[128]928 void
929 setup_newt_stuff() {
[1]930
[128]931 /*@ int *********************************************************** */
932 int i = 0;
933 int cols;
[1]934
[128]935 if (!g_text_mode) {
936 newtInit();
937 newtCls();
938 newtPushHelpLine
[585]939 ("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
[128]940 /* newtDrawRootText(28,0,"Welcome to Mondo Rescue"); */
941 newtDrawRootText(18, 0, WELCOME_STRING);
942 newtRefresh();
943 newtGetScreenSize(&cols, &g_noof_rows);
944 g_noof_log_lines = (g_noof_rows / 5) + 1;
945 }
[1]946
[128]947 err_log_lines =
948 (char **) malloc(sizeof(char *) * g_noof_log_lines);
949 if (!err_log_lines) {
950 fatal_error("Out of memory");
951 }
[1]952
[128]953 for (i = 0; i < g_noof_log_lines; i++) {
954 err_log_lines[i] = (char *) malloc(MAX_NEWT_COMMENT_LEN);
955 if (!err_log_lines[i]) {
956 fatal_error("Out of memory");
957 }
958 }
[1]959
[128]960 for (i = 0; i < g_noof_log_lines; i++) {
961 err_log_lines[i][0] = '\0';
962 }
963 }
[1]964
965
966/**
967 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
968 * @param num The numerator of the ratio.
969 * @param denom The denomenator of the ratio.
970 */
[128]971 void
972 update_evalcall_form_ratio(int num, int denom) {
[1]973
[128]974 /*@ long ************************************************************ */
[2277]975 long current_time = 0L;
976 long time_taken = 0L;
977 long time_total_est = 0L;
978 long time_remaining = 0L;
[1]979
[128]980 /*@ buffers ********************************************************** */
[2274]981 char *timeline_str = NULL;
[2211]982 char *pcline_str = NULL;
983 char *taskprogress = NULL;
[1]984
[128]985 /*@ int ************************************************************** */
986 int percentage = 0;
987 int i = 0;
988 int j = 0;
[1]989
[128]990 if (num * 100 < denom) {
991 percentage = 1;
992 } else {
993 percentage = (num * 100 + denom / 2) / denom;
994 }
995
996 current_time = get_time();
997 time_taken = current_time - g_isoform_starttime;
998 if (num) {
999 time_total_est = time_taken * denom / num;
1000 time_remaining = time_total_est - time_taken;
1001 } else {
[2277]1002 time_remaining = 0L;
[128]1003 }
1004 if (!g_text_mode) {
[1899]1005 if (g_isoform_header) {
1006 newtLabelSetText(g_isoform_header, g_isoform_header_str);
1007 }
[128]1008 }
1009 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1010 if ((percentage < 3 && g_isoform_old_progress < 3)
1011 || percentage > g_isoform_old_progress) {
1012 g_isoform_old_progress = percentage;
[2296]1013 mr_asprintf(&timeline_str, "%2ld:%02ld taken %2ld:%02ld remaining", (long) time_taken / 60, (long) time_taken % 60, (long) time_remaining / 60, (long) time_remaining % 60);
[128]1014 if (percentage < 3) {
[2211]1015 mr_asprintf(&pcline_str, " Working");
[128]1016 for (j = 0; j < g_mysterious_dot_counter; j++) {
[2211]1017 mr_strcat(pcline_str, ".");
[128]1018 }
1019 for (; j < 27; j++) {
[2211]1020 mr_strcat(pcline_str, " ");
[128]1021 }
[2211]1022 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
[128]1023 } else {
[2296]1024 mr_asprintf(&pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
[128]1025 }
1026 if (g_text_mode) {
[2211]1027 mr_asprintf(&taskprogress, "TASK: [");
[128]1028 for (i = 0; i < percentage; i += 5) {
[2211]1029 mr_strcat(taskprogress, "*");
[128]1030 }
1031 for (; i < 100; i += 5) {
[2211]1032 mr_strcat(taskprogress, ".");
[128]1033 }
1034 if (percentage >= 3) {
[2277]1035 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, time_remaining / 60, time_remaining % 60);
1036 printf("---evalcall---1--- %s\n", g_isoform_header_str);
[1899]1037 printf("---evalcall---2--- %s\n", taskprogress);
1038 printf("---evalcall---E---\n");
[128]1039 }
[2277]1040 mr_free(taskprogress);
[128]1041 } else {
1042 newtScaleSet(g_isoform_scale,
1043 (unsigned long long) percentage);
[1899]1044 if (g_isoform_pcline) {
1045 newtLabelSetText(g_isoform_pcline, pcline_str);
1046 }
1047 if ((percentage >= 3) && (g_isoform_timeline)) {
[128]1048 newtLabelSetText(g_isoform_timeline, timeline_str);
1049 }
1050 }
[2274]1051 mr_free(pcline_str);
1052 mr_free(timeline_str);
[128]1053 }
1054 if (!g_text_mode) {
1055// log_it("refreshing");
1056 newtRefresh();
1057 }
[1]1058 }
1059
1060
1061
1062/**
1063 * Update the evalcall form to show @p curr %.
1064 * @param curr The current amount of progress (percentage) in the evalcall form.
1065 */
[128]1066 void
1067 update_evalcall_form(int curr) {
1068 update_evalcall_form_ratio(curr, 100);
1069 }
[1]1070
1071
1072
1073/**
1074 * Update the progress form to show @p blurb3 and the current value of
1075 * @p g_maximum_progress.
1076 * @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.
1077 */
[128]1078 void
1079 update_progress_form(char *blurb3) {
1080 /* log_it("update_progress_form --- called"); */
1081 if (g_current_progress == -999) {
1082 /* 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."); */
1083 return;
1084 }
1085 strcpy(g_blurb_str_2, blurb3);
1086 update_progress_form_full(g_blurb_str_1, g_blurb_str_2,
1087 g_blurb_str_3);
1088 }
[1]1089
1090
1091/**
1092 * Update the progress form's complete blurb and show @p g_current_progress.
1093 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1094 * @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.
1095 * @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.
1096 */
[128]1097 void
1098 update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1099 /*@ long ***************************************************** */
[2277]1100 long current_time = 0L;
1101 long time_taken = 0L;
1102 long time_remaining = 0L;
1103 long time_total_est = 0L;
[1]1104
[128]1105 /*@ int ******************************************************* */
1106 int percentage = 0;
1107 int i = 0;
[1]1108
[128]1109 /*@ buffers *************************************************** */
[2274]1110 char *percentline_str = NULL;
1111 char *timeline_str = NULL;
[2211]1112 char *taskprogress = NULL;
[2274]1113 char *tmp = NULL;
[1]1114
[128]1115 if (!g_text_mode) {
1116 assert(blurb1 != NULL);
1117 assert(blurb2 != NULL);
1118 assert(blurb3 != NULL);
1119 assert(g_timeline != NULL);
1120 }
[1]1121
[128]1122 current_time = get_time();
1123 time_taken = current_time - g_start_time;
1124 if (g_maximum_progress == 0) {
1125 percentage = 0;
1126 } else {
1127 if (g_current_progress > g_maximum_progress) {
[2296]1128 log_msg(0, "update_progress_form_full(%s,%s,%s) --- g_current_progress=%ld; g_maximum_progress=%ld", blurb1, blurb2, blurb3, g_current_progress, g_maximum_progress);
[128]1129 g_current_progress = g_maximum_progress;
1130 }
[2277]1131 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
[128]1132 }
1133 if (percentage < 1) {
1134 percentage = 1;
1135 }
1136 if (percentage > 100) {
1137 percentage = 100;
1138 }
1139 if (g_current_progress) {
[2277]1140 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
[128]1141 time_remaining = time_total_est - time_taken;
1142 } else {
[2277]1143 time_remaining = 0L;
[128]1144 }
1145 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1146 if (g_text_mode) {
[1899]1147 printf("---progress-form---1--- %s\n", blurb1);
1148 printf("---progress-form---2--- %s\n", blurb2);
1149 printf("---progress-form---3--- %s\n", blurb3);
[541]1150 printf("---progress-form---E---\n");
[2211]1151 mr_asprintf(&taskprogress, "TASK: [");
[128]1152 for (i = 0; i < percentage; i += 5) {
[2211]1153 mr_strcat(taskprogress, "*");
[128]1154 }
1155 for (; i < 100; i += 5) {
[2211]1156 mr_strcat(taskprogress, ".");
[128]1157 }
1158 if (percentage > 100) {
[541]1159 log_msg(2, "percentage = %d", percentage);
[128]1160 }
[2277]1161 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
[1899]1162 printf("---progress-form---4--- %s\n", taskprogress);
[2211]1163 paranoid_free(taskprogress);
[128]1164 } else {
[2296]1165 mr_asprintf(&timeline_str, "%2ld:%02ld taken %2ld:%02ld remaining ", (long) time_taken / 60, (long) time_taken % 60, (long) time_remaining / 60, (long) time_remaining % 60);
[2316]1166 mr_asprintf(&percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
[2274]1167
[128]1168 center_string(blurb1, 54);
1169 center_string(blurb2, 54);
1170 center_string(blurb3, 54);
[1899]1171 if (g_blurb1) {
1172 newtLabelSetText(g_blurb1, blurb1);
1173 }
1174 if (g_blurb2) {
1175 newtLabelSetText(g_blurb2, blurb3);
1176 }
1177 if (g_blurb3) {
1178 newtLabelSetText(g_blurb3, blurb2);
1179 }
[128]1180 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
[1899]1181 if ((percentage >= 2) && (g_timeline)) {
[128]1182 newtLabelSetText(g_timeline, timeline_str);
1183 }
[2274]1184 mr_free(timeline_str);
1185
[1899]1186 if (g_percentline) {
1187 newtLabelSetText(g_percentline, percentline_str);
1188 }
[2274]1189 mr_free(percentline_str);
1190
[128]1191 newtRefresh();
1192 }
[1]1193 }
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208/**
1209 * Ask the user which backup media type they would like to use.
1210 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1211 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs,
1212 * and @c iso.
1213 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1214 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1215 */
[128]1216 t_bkptype which_backup_media_type(bool restoring) {
[1]1217
[128]1218 /*@ char ************************************************************ */
1219 t_bkptype output;
[1]1220
1221
[128]1222 /*@ newt ************************************************************ */
1223 char *title_sz;
1224 char *minimsg_sz;
1225 static t_bkptype possible_bkptypes[] =
1226 { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso };
1227 static char *possible_responses[] =
1228 { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev",
1229"nfs", "iso", NULL };
1230 char *outstr;
1231 t_bkptype backup_type;
1232 int i;
[1]1233
[128]1234 newtComponent b1;
1235 newtComponent b2;
1236 newtComponent b3;
1237 newtComponent b4;
1238 newtComponent b5;
1239 newtComponent b6;
1240 newtComponent b7;
1241 newtComponent b8;
1242 newtComponent b_res;
1243 newtComponent myForm;
[1]1244
[128]1245 title_sz = malloc(MAX_NEWT_COMMENT_LEN);
1246 minimsg_sz = malloc(MAX_NEWT_COMMENT_LEN);
1247 outstr = malloc(MAX_NEWT_COMMENT_LEN);
1248 if (g_text_mode) {
1249 for (backup_type = none; backup_type == none;) {
[541]1250 printf("Backup type (");
[128]1251 for (i = 0; possible_responses[i]; i++) {
1252 printf("%c%s", (i == 0) ? '\0' : ' ',
1253 possible_responses[i]);
1254 }
1255 printf(")\n--> ");
1256 (void) fgets(outstr, MAX_NEWT_COMMENT_LEN, stdin);
1257 strip_spaces(outstr);
1258 for (i = 0; possible_responses[i]; i++) {
1259 if (!strcmp(possible_responses[i], outstr)) {
1260 backup_type = possible_bkptypes[i];
1261 }
1262 }
1263 }
1264 paranoid_free(title_sz);
1265 paranoid_free(minimsg_sz);
1266 paranoid_free(outstr);
1267 return (backup_type);
1268 }
1269 newtDrawRootText(18, 0, WELCOME_STRING);
1270 if (restoring) {
1271 strcpy(title_sz,
[541]1272 "Please choose the backup media from which you want to read data.");
1273 strcpy(minimsg_sz, "Read from:");
[128]1274 } else {
1275 strcpy(title_sz,
[541]1276 "Please choose the backup media to which you want to archive data.");
1277 strcpy(minimsg_sz, "Backup to:");
[128]1278 }
1279 newtPushHelpLine(title_sz);
1280 // newtOpenWindow (23, 3, 34, 17, minimsg_sz);
1281 newtCenteredWindow(34, 17, minimsg_sz);
[541]1282 b1 = newtButton(1, 1, "CD-R disks ");
1283 b2 = newtButton(17, 1, "CD-RW disks");
1284 b3 = newtButton(1, 9, "Tape drive ");
[1703]1285 b4 = newtButton(17, 5, "USB Key/Disk");
[541]1286 b5 = newtButton(1, 5, " DVD disks ");
1287 b6 = newtButton(17, 9, " NFS mount ");
1288 b7 = newtButton(1, 13, " Hard disk ");
1289 b8 = newtButton(17, 13, " Exit ");
[128]1290 myForm = newtForm(NULL, NULL, 0);
1291 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1292 NULL);
1293 b_res = newtRunForm(myForm);
1294 newtFormDestroy(myForm);
1295 newtPopWindow();
1296 if (b_res == b1) {
1297 output = cdr;
1298 } else if (b_res == b2) {
1299 output = cdrw;
1300 } else if (b_res == b3) {
1301 output = tape;
1302 } else if (b_res == b4) {
[1703]1303 output = usb;
[128]1304 } else if (b_res == b5) {
1305 output = dvd;
1306 } else if (b_res == b6) {
1307 output = nfs;
1308 } else if (b_res == b7) {
1309 output = iso;
1310 } else {
1311 output = none;
1312 }
1313 newtPopHelpLine();
1314 paranoid_free(title_sz);
1315 paranoid_free(minimsg_sz);
1316 paranoid_free(outstr);
1317 return (output);
1318 }
[1]1319
1320
1321
1322
1323/**
1324 * Ask the user how much compression they would like to use.
1325 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1326 * @return The compression level (0-9) chosen, or -1 for "Exit".
1327 */
[128]1328 int
1329 which_compression_level() {
[1]1330
[128]1331 /*@ char ************************************************************ */
1332 int output = none;
[1]1333
1334
[128]1335 /*@ newt ************************************************************ */
[1]1336
[128]1337 newtComponent b1;
1338 newtComponent b2;
1339 newtComponent b3;
1340 newtComponent b4;
1341 newtComponent b5;
1342 newtComponent b_res;
1343 newtComponent myForm;
[1]1344
[128]1345 newtDrawRootText(18, 0, WELCOME_STRING);
1346 newtPushHelpLine
[541]1347 (" Please specify the level of compression that you want.");
[128]1348 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
[541]1349 newtCenteredWindow(34, 13, "How much compression?");
1350 b1 = newtButton(4, 1, "Maximum");
1351 b2 = newtButton(18, 1, "Average");
[1463]1352 b3 = newtButton(4, 5, "Minimum");
[541]1353 b4 = newtButton(18, 5, " None ");
1354 b5 = newtButton(4, 9, " Exit ");
[128]1355 myForm = newtForm(NULL, NULL, 0);
1356 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1357 b_res = newtRunForm(myForm);
1358 newtFormDestroy(myForm);
1359 newtPopWindow();
1360 if (b_res == b1) {
1361 output = 9;
1362 } else if (b_res == b2) {
1363 output = 4;
1364 } else if (b_res == b3) {
1365 output = 1;
1366 } else if (b_res == b4) {
1367 output = 0;
1368 } else if (b_res == b5) {
1369 output = -1;
1370 }
1371 newtPopHelpLine();
1372 return (output);
1373 }
[1]1374
1375
1376
1377
1378
1379/**
1380 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1381 * @p ARBITRARY_MAXIMUM entries.
1382 * @param filelist The filelist structure to load @p source_file into.
1383 * @param source_file The file containing a list of filenames to load into @p filelist.
1384 */
[128]1385 int load_filelist_into_array(struct s_filelist *filelist,
1386 char *source_file) {
1387 int i;
[1662]1388 int j = 0;
[128]1389 bool done;
1390 char *tmp;
[1662]1391 char *tmp1 = NULL;
1392 FILE *fin, *fout;
[128]1393 struct s_filelist_entry dummy_fle;
[1]1394
[128]1395 assert(filelist != NULL);
1396 assert_string_is_neither_NULL_nor_zerolength(source_file);
[1]1397
[2230]1398 log_it("entering");
[128]1399 if (!(fin = fopen(source_file, "r"))) {
1400 log_OS_error(source_file);
1401 log_msg(2, "Can't open %s; therefore, cannot popup list",
1402 source_file);
1403 return (1);
1404 }
[1662]1405
[2211]1406 mr_asprintf(&tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
[1662]1407 if (!(fout = fopen(tmp1, "a"))) {
1408 log_msg(2, "Can't write to %s", tmp1);
1409 return(1);
1410 }
1411
1412 malloc_string(tmp);
[128]1413 log_msg(2, "Loading %s", source_file);
1414 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM;
1415 filelist->entries++) {
1416 god_i_hate_gotos:
1417 if (feof(fin)) {
1418 break;
1419 }
1420 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, fin);
1421 i = (int) strlen(tmp);
1422 if (i < 2) {
1423 goto god_i_hate_gotos;
1424 }
1425 if (tmp[i - 1] < 32) {
1426 tmp[--i] = '\0';
1427 }
1428 if (i < 2) {
1429 goto god_i_hate_gotos;
1430 }
[1662]1431 if (!does_file_exist(tmp) && !feof(fin)) {
1432 j++;
1433 fprintf(fout, "%s\n", tmp);
1434
[128]1435 goto god_i_hate_gotos;
1436 }
1437 filelist->el[filelist->entries].severity =
1438 severity_of_difference(tmp, NULL);
1439 strcpy(filelist->el[filelist->entries].filename, tmp);
1440 if (feof(fin)) {
1441 break;
1442 }
1443 }
1444 paranoid_fclose(fin);
[1662]1445 paranoid_fclose(fout);
1446 if (j > 0) {
1447 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);
1448 }
1449 paranoid_free(tmp1);
[128]1450 if (filelist->entries >= ARBITRARY_MAXIMUM) {
[541]1451 log_to_screen("Arbitrary limits suck, man!");
[128]1452 paranoid_free(tmp);
1453 return (1);
1454 }
1455 for (done = FALSE; !done;) {
1456 done = TRUE;
1457 for (i = 0; i < filelist->entries - 1; i++) {
[1]1458// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
[128]1459 if (filelist->el[i].severity < filelist->el[i + 1].severity
1460 || (filelist->el[i].severity ==
1461 filelist->el[i + 1].severity
1462 && strcmp(filelist->el[i].filename,
1463 filelist->el[i + 1].filename) > 0)) {
1464 memcpy((void *) &dummy_fle,
1465 (void *) &(filelist->el[i]),
1466 sizeof(struct s_filelist_entry));
1467 memcpy((void *) &(filelist->el[i]),
1468 (void *) &(filelist->el[i + 1]),
1469 sizeof(struct s_filelist_entry));
1470 memcpy((void *) &(filelist->el[i + 1]),
1471 (void *) &dummy_fle,
1472 sizeof(struct s_filelist_entry));
1473 log_msg(2, "Swapping %s and %s",
1474 filelist->el[i].filename,
1475 filelist->el[i + 1].filename);
1476 done = FALSE;
1477 }
1478 }
1479 }
1480 paranoid_free(tmp);
[2230]1481 log_it("leaving");
[128]1482 return (0);
1483 }
[1]1484
1485
1486
1487/**
1488 * Generate a pretty string based on @p flentry.
1489 * @param flentry The filelist entry to stringify.
1490 * @return The string form of @p flentry.
1491 * @note The returned value points to static storage that will be overwritten with each call.
1492 */
[128]1493 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1494 static char comment[100];
[2211]1495 char *tmp = NULL;
[1]1496
[2230]1497 log_it("entering");
[128]1498 assert(flentry != NULL);
1499 if (flentry->severity == 0) {
[2211]1500 mr_asprintf(&tmp, "0 ");
[128]1501 } else if (flentry->severity == 1) {
[2211]1502 mr_asprintf(&tmp, "low ");
[128]1503 } else if (flentry->severity == 2) {
[2211]1504 mr_asprintf(&tmp, "med ");
[128]1505 } else {
[2211]1506 mr_asprintf(&tmp, "high");
[128]1507 }
[2211]1508 mr_strcat(tmp, " ");
1509 mr_strcat(tmp, flentry->filename);
1510 strncpy(comment, tmp, 99);
[128]1511 paranoid_free(tmp);
[2211]1512
[2230]1513 log_it("leaving");
[128]1514 return (comment);
1515 }
[1]1516
1517
1518
1519
1520
1521/**
1522 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1523 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1524 * @param source_file The file containing a list of changed files.
1525 */
[128]1526 void popup_changelist_from_file(char *source_file) {
1527 char *reason;
1528 newtComponent myForm;
1529 newtComponent bClose;
1530 newtComponent bSelect;
1531 newtComponent b_res;
1532 newtComponent fileListbox;
1533 newtComponent headerMsg;
[1]1534
[128]1535 /*@ ???? ************************************************************ */
1536 void *curr_choice;
1537 void *keylist[ARBITRARY_MAXIMUM];
[1]1538
[128]1539 /*@ int ************************************************************* */
1540 int currline = 0;
1541 int finished = FALSE;
[738]1542
1543 /*@ long ************************************************************ */
1544 long i = 0;
[128]1545 long lng = 0;
[1]1546
[128]1547 /*@ buffers ********************************************************* */
[2274]1548 char *tmp = NULL;
1549 char *differ_sz = NULL;
[1]1550
[128]1551 struct s_filelist *filelist;
1552 assert_string_is_neither_NULL_nor_zerolength(source_file);
1553 if (g_text_mode) {
1554 log_msg(2, "Text mode. Therefore, no popup list.");
[2274]1555 return;
[128]1556 }
1557 log_msg(2, "Examining file %s", source_file);
[1]1558
[128]1559 lng = count_lines_in_file(source_file);
1560 if (lng < 1) {
1561 log_msg(2, "No lines in file. Therefore, no popup list.");
[2274]1562 return;
[128]1563 } else if (lng >= ARBITRARY_MAXIMUM) {
1564 log_msg(2, "Too many files differ for me to list.");
[2274]1565 return;
[128]1566 }
[1]1567
[128]1568 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1569 fileListbox =
1570 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1571 newtListboxClear(fileListbox);
[1]1572
[128]1573 if (load_filelist_into_array(filelist, source_file)) {
[2274]1574 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
[128]1575 return;
1576 }
1577 log_msg(2, "%d files loaded into filelist array",
1578 filelist->entries);
1579 for (i = 0; i < filelist->entries; i++) {
1580 keylist[i] = (void *) i;
1581 newtListboxAppendEntry(fileListbox,
1582 filelist_entry_to_string(&
1583 (filelist->
1584 el[i])),
1585 keylist[i]);
1586 }
[2296]1587 mr_asprintf(&differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
[128]1588 newtPushHelpLine(differ_sz);
[2274]1589 mr_free(differ_sz);
1590
[541]1591 bClose = newtCompactButton(10, 15, " Close ");
1592 bSelect = newtCompactButton(30, 15, " Select ");
[2274]1593 mr_asprintf(&tmp, "%-10s %-20s", "Priority", "Filename");
[128]1594 headerMsg = newtLabel(2, 1, tmp);
[2274]1595 mr_free(tmp);
1596
[541]1597 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
[128]1598 myForm = newtForm(NULL, NULL, 0);
1599 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1600 bSelect, NULL);
[2274]1601
1602 malloc_string(reason);
[128]1603 while (!finished) {
1604 b_res = newtRunForm(myForm);
1605 if (b_res == bClose) {
1606 finished = TRUE;
1607 } else {
1608 curr_choice = newtListboxGetCurrent(fileListbox);
1609 for (i = 0;
1610 i < filelist->entries && keylist[i] != curr_choice;
1611 i++);
1612 if (i == filelist->entries && filelist->entries > 0) {
[541]1613 log_to_screen("I don't know what that button does!");
[128]1614 } else {
1615 currline = i;
1616 if (filelist->entries > 0) {
[2274]1617 severity_of_difference(filelist->el[currline]. filename, reason);
1618 mr_asprintf(&tmp, "%s --- %s", filelist->el[currline].filename, reason);
1619
[128]1620 popup_and_OK(tmp);
[2274]1621 mr_free(tmp);
[128]1622 }
1623 }
1624 }
1625 }
[2274]1626 paranoid_free(reason);
1627
[128]1628 newtFormDestroy(myForm);
1629 newtPopWindow();
1630 newtPopHelpLine();
1631 return;
[1]1632 }
1633
1634/* @} - end of guiGroup */
1635
1636
1637#if __cplusplus
[128]1638} /* extern "C" */
[1]1639#endif
1640
1641
[128]1642void wait_until_software_raids_are_prepped(char *mdstat_file,
1643 int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.