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

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

r3272@localhost: bruno | 2009-07-18 01:37:19 +0200
Formating

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