source: MondoRescue/branches/2.2.7/mondo/src/common/newt-specific.c@ 1999

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