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

Last change on this file since 2276 was 2274, checked in by Bruno Cornec, 17 years ago

r3260@localhost: bruno | 2009-07-12 20:27:08 +0200
Replace sprintf by mr_asprintf in newt-specific.c

  • Property svn:keywords set to Id
File size: 45.9 KB
RevLine 
[1]1/* newt-specific.c
[99]2 $Id: newt-specific.c 2274 2009-07-13 00:34:28Z 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 2274 2009-07-13 00:34:28Z bruno $";
[1]38
[128]39 extern pid_t g_mastermind_pid;
[2030]40 /*
[128]41 extern char *g_tmpfs_mountpt;
[2030]42 */
[128]43 extern char *g_boot_mountpt;
[792]44 extern char *ps_options;
[928]45 extern char *ps_proc_id;
[1]46
[128]47 extern void set_signals(int);
[1]48
49/**
50 * @addtogroup globalGroup
51 * @{
52 */
53/**
54 * Whether we are currently in a nested call of fatal_error().
55 */
[128]56 bool g_exiting = FALSE;
[1]57
58/**
59 * Padding below the Newt components, to overcome bugs in Newt.
60 */
[128]61 char g_haharrrrr[500];
[1]62
63
[128]64 newtComponent g_timeline = NULL, ///< The line of the progress form that shows the time elapsed/remaining
65 g_percentline = NULL, ///< The line of the progress form that shows the percent completed/remaining
66 g_scale = NULL, ///< The progress bar component in the progress form
67 g_progressForm = NULL, ///< The progress form component itself
68 g_blurb1 = NULL, ///< The component for line 1 of the blurb in the progress form
69 g_blurb2 = NULL, ///< The component for line 2 of the blurb in the progress form
70 g_blurb3 = NULL, ///< The component for line 3 (updated continuously) of the blurb in the progress form
71 g_label = NULL; ///< ????? @bug ?????
[1]72
73/**
74 * Padding above the Newt components, to overcome bugs in Newt.
75 */
[128]76 char g_jim_lad_yarr[500];
77 char **err_log_lines = NULL, ///< The list of log lines to show on the screen.
78 g_blurb_str_1[MAX_NEWT_COMMENT_LEN] = "", ///< The string for line 1 of the blurb in the progress form
79 g_blurb_str_2[MAX_NEWT_COMMENT_LEN] = "", ///< The string for line 2 of the blurb in the progress form
80 g_blurb_str_3[MAX_NEWT_COMMENT_LEN] = ""; ///< The string for line 3 (updated continuously) of the blurb in the progress form
81 newtComponent g_isoform_main = NULL, ///< The evalcall form component itself
82 g_isoform_header = NULL, ///< The component for the evalcall form title
83 g_isoform_scale = NULL, ///< The progress bar component in the evalcall form
84 g_isoform_timeline = NULL, ///< The line of the evalcall form that shows the time elapsed/remaining
85 g_isoform_pcline = NULL; ///< The line of the evalcall form that shows the percent completed/remaining
86 long g_isoform_starttime; ///< The time (in seconds since the epoch) that the evalcall form was opened.
87 int g_isoform_old_progress = -1; ///< The most recent progress update of the evalcall form (percent).
88 char g_isoform_header_str[MAX_STR_LEN] = " "; ///< The string for the evalcall form title.
89 int g_mysterious_dot_counter; ///< The counter for the twirling baton (/ | \\ - ...) on percentage less than 3
90 int g_noof_log_lines = 6; ///< The number of lines to show in the log at the bottom of the screen.
91 int g_noof_rows = 25; ///< The number of rows on the screen.
[1]92
[128]93 int g_currentY = 3; ///< The row to write background progress messages to. Incremented each time a message is written.
94 extern int g_current_media_number;
95 pid_t g_main_pid = 0; ///< The PID of the main Mondo process.
96 long g_maximum_progress = 999; ///< The maximum amount of progress (100%) for the currently opened progress form.
97 long g_current_progress = -999; ///< The current amount of progress (filelist #, etc.) for the currently opened progress form.
98 long g_start_time = 0; ///< The time (in seconds since the epoch) that the progress form was opened.
99 bool g_text_mode = TRUE; ///< If FALSE, use a newt interface; if TRUE, use an ugly (but more compatible) dumb terminal interface.
100 char g_xmondo_stdin[MAX_NEWT_COMMENT_LEN], ///< ... @bug Unneeded w/current XMondo.
101 g_xmondo_stdout[MAX_NEWT_COMMENT_LEN]; ///< .... @bug Unneeded w/current XMondo.
102 bool g_called_by_xmondo = FALSE; ///< @bug Unneeded w/current XMondo.
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 // newtOpenWindow (20, 6, 40, 7, title); // "Please Wait");
628 newtCenteredWindow(40, 7, title);
629 g_isoform_main = newtForm(NULL, NULL, 0);
630 g_isoform_timeline = newtLabel(1, 5, "This is the timeline");
631 g_isoform_pcline = newtLabel(1, 6, "This is the pcline");
632 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
633 g_isoform_pcline, g_isoform_header,
634 g_isoform_scale, NULL);
635 newtRefresh();
636 }
637 update_evalcall_form(0);
638 paranoid_free(tmp);
639 paranoid_free(title);
640 }
[1]641
642
643
644/**
645 * Open a progress form with title @p title.
646 * @param title The title to use for the progress form (will be put in the title bar on Newt).
647 * @param b1 The first line of the blurb; generally static.
648 * @param b2 The second line of the blurb; generally static.
649 * @param b3 The third line of the blurb; generally dynamic (it is passed
650 * to update_evalcall_form() every time).
651 * @param max_val The maximum amount of progress (number of filesets, etc.)
652 */
[128]653 void
654 open_progress_form(char *title, char *b1, char *b2, char *b3,
655 long max_val) {
[1]656
[128]657 /*@ buffers ********************************************************* */
658 char *b1c;
659 char *blurb1;
660 char *blurb2;
661 char *blurb3;
[1]662
[128]663 /*@ initialize ****************************************************** */
664 g_mysterious_dot_counter = 0;
[1]665
[128]666 malloc_string(b1c);
667 malloc_string(blurb1);
668 malloc_string(blurb2);
669 malloc_string(blurb3);
[1]670
[128]671 assert(title != NULL);
672 assert(b1 != NULL);
673 assert(b2 != NULL);
674 assert(b3 != NULL);
[1]675
[128]676 strcpy(blurb1, b1);
677 strcpy(blurb2, b2);
678 strcpy(blurb3, b3);
679 strcpy(b1c, b1);
680 center_string(b1c, 80);
681 if (max_val <= 0) {
682 max_val = 1;
683 }
[1]684
[128]685 g_start_time = get_time();
686 g_maximum_progress = max_val;
687 g_current_progress = 0;
688 strcpy(g_blurb_str_1, blurb1);
689 strcpy(g_blurb_str_2, blurb3);
690 strcpy(g_blurb_str_3, blurb2);
691 if (g_text_mode) {
692 log_msg(0, blurb1);
693 log_msg(0, blurb2);
694 log_msg(0, blurb3);
695 } else {
696 g_blurb1 = newtLabel(2, 1, blurb1);
697 g_blurb2 = newtLabel(2, 2, blurb3);
698 g_blurb3 = newtLabel(2, 4, blurb2);
699 // newtOpenWindow (10, 4, 60, 11, title);
700 newtCenteredWindow(60, 11, title);
701 g_scale = newtScale(3, 6, 54, g_maximum_progress);
702 g_progressForm = newtForm(NULL, NULL, 0);
703 g_percentline = newtLabel(10, 9, "This is the percentline");
704 g_timeline = newtLabel(10, 8, "This is the timeline");
705 newtFormAddComponents(g_progressForm, g_percentline,
706 g_timeline, g_scale, g_blurb1, g_blurb3,
707 g_blurb2, NULL);
708 newtPushHelpLine(b1c);
709 newtRefresh();
710 }
711 update_progress_form_full(blurb1, blurb2, blurb3);
712 paranoid_free(b1c);
713 paranoid_free(blurb1);
714 paranoid_free(blurb2);
715 paranoid_free(blurb3);
716 }
717
[1]718/**
719 * Give a message to the user in the form of a dialog box (under Newt).
720 * @param prompt The message.
721 */
[128]722 void
723 popup_and_OK(char *prompt) {
724 char ch;
[1]725
[128]726 assert_string_is_neither_NULL_nor_zerolength(prompt);
[1]727
[128]728 log_msg(0, prompt);
729 if (g_text_mode) {
730 printf
[1899]731 ("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ",
[128]732 prompt);
733 while (((ch = getchar()) != '\n') && (ch != EOF));
734 } else {
[541]735 (void) popup_with_buttons(prompt, " OK ", "");
[128]736 }
737 }
[1]738
739/**
740 * Ask the user to enter a value.
741 * @param title The title of the dialog box.
742 * @param b The blurb (e.g. what you want the user to enter).
743 * @param output The string to put the user's answer in.
744 * @param maxsize The size in bytes allocated to @p output.
745 * @return TRUE if the user pressed OK, FALSE if they pressed Cancel.
746 */
[128]747 bool popup_and_get_string(char *title, char *b, char *output,
748 int maxsize) {
[1]749
[128]750 /*@ newt ************************************************************ */
751 newtComponent myForm;
752 newtComponent b_1;
753 newtComponent b_2;
754 newtComponent b_res;
755 newtComponent text;
756 newtComponent type_here;
[1]757
[128]758 /*@ pointers ********************************************************* */
759 char *entry_value;
[1]760
[128]761 /*@ buffers ********************************************************** */
762 char *blurb;
763 char *original_contents;
[1]764
[128]765 blurb = malloc(MAX_NEWT_COMMENT_LEN);
766 original_contents = malloc(MAX_NEWT_COMMENT_LEN);
767 assert_string_is_neither_NULL_nor_zerolength(title);
768 assert(b != NULL);
769 assert(output != NULL);
[1]770
[128]771 if (g_text_mode) {
772 printf
[1899]773 ("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q---\n--> ",
[128]774 title, b);
775 (void) fgets(output, maxsize, stdin);
776 if (output[strlen(output) - 1] == '\n')
777 output[strlen(output) - 1] = '\0';
778 paranoid_free(blurb);
779 paranoid_free(original_contents);
780 return (TRUE);
781 }
782 strcpy(blurb, b);
783 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
784 strcpy(original_contents, output);
785 output[0] = '\0';
786 type_here =
787 newtEntry(2, newtTextboxGetNumLines(text) + 2,
788 original_contents, 50,
[1]789#ifdef __cplusplus
[128]790 0, NEWT_FLAG_RETURNEXIT
[1]791#else
[128]792 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
[1]793#endif
[128]794 );
[541]795 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
796 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
[128]797 // newtOpenWindow (8, 5, 54, newtTextboxGetNumLines (text) + 9, title);
798 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
799 myForm = newtForm(NULL, NULL, 0);
800 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
801 center_string(blurb, 80);
802 newtPushHelpLine(blurb);
803 b_res = newtRunForm(myForm);
804 strcpy(output, entry_value);
805 newtPopHelpLine();
806 newtFormDestroy(myForm);
807 newtPopWindow();
808 if (b_res == b_2) {
809 strcpy(output, original_contents);
810 paranoid_free(blurb);
811 paranoid_free(original_contents);
812 return (FALSE);
813 } else {
814 paranoid_free(blurb);
815 paranoid_free(original_contents);
816 return (TRUE);
817 }
818 }
[1]819
820
821/**
822 * Pop up a dialog box with user-defined buttons.
823 * @param p The text to put in the dialog box.
824 * @param button1 The label on the first button.
825 * @param button2 The label on the second button, or "" if you only want one button.
826 * @return TRUE if @p button1 was pushed, FALSE otherwise.
827 */
[128]828 bool popup_with_buttons(char *p, char *button1, char *button2) {
[1]829
[128]830 /*@ buffers *********************************************************** */
[2136]831 char *prompt;
[1]832
[128]833 /*@ newt ************************************************************** */
834 newtComponent myForm;
835 newtComponent b_1;
836 newtComponent b_2;
837 newtComponent b_res;
838 newtComponent text;
[1]839
[128]840 assert_string_is_neither_NULL_nor_zerolength(p);
841 assert(button1 != NULL);
842 assert(button2 != NULL);
[2136]843 prompt = malloc(MAX_NEWT_COMMENT_LEN);
[128]844 if (g_text_mode) {
845 if (strlen(button2) == 0) {
846 printf("%s (%s) --> ", p, button1);
847 } else {
848 printf("%s (%s or %s) --> ", p, button1, button2);
849 }
[2136]850 for (prompt[0] = '\0';
851 strcmp(prompt, button1) && (strlen(button2) == 0
852 || strcmp(prompt, button2));) {
[128]853 printf("--> ");
[2136]854 (void) fgets(prompt, MAX_NEWT_COMMENT_LEN, stdin);
[128]855 }
[2136]856 if (!strcmp(prompt, button1)) {
[128]857 paranoid_free(prompt);
858 return (TRUE);
859 } else {
860 paranoid_free(prompt);
861 return (FALSE);
862 }
863 }
[1]864
[877]865 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
[883]866 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
[128]867 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
868 b_1 =
869 newtButton(20 -
870 ((button2[0] !=
871 '\0') ? strlen(button1) +
872 2 : strlen(button1) / 2),
873 newtTextboxGetNumLines(text) + 3, button1);
874 if (button2[0] != '\0') {
875 b_2 =
876 newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
877 } else {
878 b_2 = NULL;
879 }
880 // newtOpenWindow (25, 5, 46, newtTextboxGetNumLines (text) + 7, "Alert");
[541]881 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
[128]882 myForm = newtForm(NULL, NULL, 0);
883 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
884 center_string(prompt, 80);
885 newtPushHelpLine(prompt);
886 b_res = newtRunForm(myForm);
887 newtPopHelpLine();
888 newtFormDestroy(myForm);
889 newtPopWindow();
890 if (b_res == b_1) {
891 paranoid_free(prompt);
892 return (TRUE);
893 } else {
894 paranoid_free(prompt);
895 return (FALSE);
896 }
897 }
[1]898
899
900
[128]901
[1]902/**
903 * Synchronize the log messages stored in @p err_log_lines with those shown
904 * on the screen.
905 */
[128]906 void
907 refresh_log_screen() {
[1]908
[128]909 /*@ int *********************************************************** */
910 int i = 0;
[1]911
912
[128]913 if (g_text_mode || !err_log_lines) {
914 return;
915 }
916 for (i = g_noof_log_lines - 1; i >= 0; i--) {
917 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
918 " ");
919 }
920 newtRefresh();
921 for (i = g_noof_log_lines - 1; i >= 0; i--) {
922 err_log_lines[i][79] = '\0';
923 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
924 err_log_lines[i]);
925 }
926 newtRefresh();
927 }
[1]928
929
930/**
931 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
932 * only allocate some memory.
933 */
[128]934 void
935 setup_newt_stuff() {
[1]936
[128]937 /*@ int *********************************************************** */
938 int i = 0;
939 int cols;
[1]940
[128]941 if (!g_text_mode) {
942 newtInit();
943 newtCls();
944 newtPushHelpLine
[585]945 ("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
[128]946 /* newtDrawRootText(28,0,"Welcome to Mondo Rescue"); */
947 newtDrawRootText(18, 0, WELCOME_STRING);
948 newtRefresh();
949 newtGetScreenSize(&cols, &g_noof_rows);
950 g_noof_log_lines = (g_noof_rows / 5) + 1;
951 }
[1]952
[128]953 err_log_lines =
954 (char **) malloc(sizeof(char *) * g_noof_log_lines);
955 if (!err_log_lines) {
956 fatal_error("Out of memory");
957 }
[1]958
[128]959 for (i = 0; i < g_noof_log_lines; i++) {
960 err_log_lines[i] = (char *) malloc(MAX_NEWT_COMMENT_LEN);
961 if (!err_log_lines[i]) {
962 fatal_error("Out of memory");
963 }
964 }
[1]965
[128]966 for (i = 0; i < g_noof_log_lines; i++) {
967 err_log_lines[i][0] = '\0';
968 }
969 }
[1]970
971
972/**
973 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
974 * @param num The numerator of the ratio.
975 * @param denom The denomenator of the ratio.
976 */
[128]977 void
978 update_evalcall_form_ratio(int num, int denom) {
[1]979
[128]980 /*@ long ************************************************************ */
981 long current_time = 0;
982 long time_taken = 0;
983 long time_total_est = 0;
984 long time_remaining = 0;
[1]985
[128]986 /*@ buffers ********************************************************** */
[2274]987 char *timeline_str = NULL;
[2211]988 char *pcline_str = NULL;
989 char *taskprogress = NULL;
[1]990
[128]991 /*@ int ************************************************************** */
992 int percentage = 0;
993 int i = 0;
994 int j = 0;
[1]995
[128]996 if (num * 100 < denom) {
997 percentage = 1;
998 } else {
999 percentage = (num * 100 + denom / 2) / denom;
1000 }
1001
1002 current_time = get_time();
1003 time_taken = current_time - g_isoform_starttime;
1004 if (num) {
1005 time_total_est = time_taken * denom / num;
1006 time_remaining = time_total_est - time_taken;
1007 } else {
1008 time_remaining = 0;
1009 }
1010 if (!g_text_mode) {
[1899]1011 if (g_isoform_header) {
1012 newtLabelSetText(g_isoform_header, g_isoform_header_str);
1013 }
[128]1014 }
1015 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1016 if ((percentage < 3 && g_isoform_old_progress < 3)
1017 || percentage > g_isoform_old_progress) {
1018 g_isoform_old_progress = percentage;
[2274]1019 mr_asprintf(&timeline_str,
[541]1020 "%2ld:%02ld taken %2ld:%02ld remaining",
[128]1021 time_taken / 60, time_taken % 60, time_remaining / 60,
1022 time_remaining % 60);
1023 if (percentage < 3) {
[2211]1024 mr_asprintf(&pcline_str, " Working");
[128]1025 for (j = 0; j < g_mysterious_dot_counter; j++) {
[2211]1026 mr_strcat(pcline_str, ".");
[128]1027 }
1028 for (; j < 27; j++) {
[2211]1029 mr_strcat(pcline_str, " ");
[128]1030 }
[2211]1031 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
[128]1032 } else {
[2211]1033 mr_asprintf(&pcline_str, " %3d%% done %3d%% to go",
[128]1034 percentage, 100 - percentage);
1035 }
1036 if (g_text_mode) {
[2211]1037 mr_asprintf(&taskprogress, "TASK: [");
[128]1038 for (i = 0; i < percentage; i += 5) {
[2211]1039 mr_strcat(taskprogress, "*");
[128]1040 }
1041 for (; i < 100; i += 5) {
[2211]1042 mr_strcat(taskprogress, ".");
[128]1043 }
1044 if (percentage >= 3) {
[2211]1045 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage,
[128]1046 time_remaining / 60, time_remaining % 60);
[1899]1047 printf("---evalcall---1--- %s\n",
[128]1048 g_isoform_header_str);
[1899]1049 printf("---evalcall---2--- %s\n", taskprogress);
1050 printf("---evalcall---E---\n");
[128]1051 }
[2211]1052 paranoid_free(taskprogress);
[128]1053 } else {
1054 newtScaleSet(g_isoform_scale,
1055 (unsigned long long) percentage);
[1899]1056 if (g_isoform_pcline) {
1057 newtLabelSetText(g_isoform_pcline, pcline_str);
1058 }
1059 if ((percentage >= 3) && (g_isoform_timeline)) {
[128]1060 newtLabelSetText(g_isoform_timeline, timeline_str);
1061 }
1062 }
[2274]1063 mr_free(pcline_str);
1064 mr_free(timeline_str);
[128]1065 }
1066 if (!g_text_mode) {
1067// log_it("refreshing");
1068 newtRefresh();
1069 }
[1]1070 }
1071
1072
1073
1074/**
1075 * Update the evalcall form to show @p curr %.
1076 * @param curr The current amount of progress (percentage) in the evalcall form.
1077 */
[128]1078 void
1079 update_evalcall_form(int curr) {
1080 update_evalcall_form_ratio(curr, 100);
1081 }
[1]1082
1083
1084
1085/**
1086 * Update the progress form to show @p blurb3 and the current value of
1087 * @p g_maximum_progress.
1088 * @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.
1089 */
[128]1090 void
1091 update_progress_form(char *blurb3) {
1092 /* log_it("update_progress_form --- called"); */
1093 if (g_current_progress == -999) {
1094 /* 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."); */
1095 return;
1096 }
1097 strcpy(g_blurb_str_2, blurb3);
1098 update_progress_form_full(g_blurb_str_1, g_blurb_str_2,
1099 g_blurb_str_3);
1100 }
[1]1101
1102
1103/**
1104 * Update the progress form's complete blurb and show @p g_current_progress.
1105 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1106 * @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.
1107 * @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.
1108 */
[128]1109 void
1110 update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1111 /*@ long ***************************************************** */
1112 long current_time = 0;
1113 long time_taken = 0;
1114 long time_remaining = 0;
1115 long time_total_est = 0;
[1]1116
[128]1117 /*@ int ******************************************************* */
1118 int percentage = 0;
1119 int i = 0;
[1]1120
[128]1121 /*@ buffers *************************************************** */
[2274]1122 char *percentline_str = NULL;
1123 char *timeline_str = NULL;
[2211]1124 char *taskprogress = NULL;
[2274]1125 char *tmp = NULL;
[1]1126
[128]1127 if (!g_text_mode) {
1128 assert(blurb1 != NULL);
1129 assert(blurb2 != NULL);
1130 assert(blurb3 != NULL);
1131 assert(g_timeline != NULL);
1132 }
[1]1133
[128]1134 current_time = get_time();
1135 time_taken = current_time - g_start_time;
1136 if (g_maximum_progress == 0) {
1137 percentage = 0;
1138 } else {
1139 if (g_current_progress > g_maximum_progress) {
[2274]1140 mr_asprintf(&tmp,
[128]1141 "update_progress_form_full(%s,%s,%s) --- g_current_progress=%ld; g_maximum_progress=%ld",
1142 blurb1, blurb2, blurb3, g_current_progress,
1143 g_maximum_progress);
1144 log_msg(0, tmp);
[2274]1145 mr_free(tmp);
[128]1146 g_current_progress = g_maximum_progress;
1147 }
1148 percentage =
1149 (int) ((g_current_progress * 100L) / g_maximum_progress);
1150 }
1151 if (percentage < 1) {
1152 percentage = 1;
1153 }
1154 if (percentage > 100) {
1155 percentage = 100;
1156 }
1157 if (g_current_progress) {
1158 time_total_est =
1159 time_taken * (long) g_maximum_progress /
1160 (long) (g_current_progress);
1161 time_remaining = time_total_est - time_taken;
1162 } else {
1163 time_remaining = 0;
1164 }
1165 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1166 if (g_text_mode) {
[1899]1167 printf("---progress-form---1--- %s\n", blurb1);
1168 printf("---progress-form---2--- %s\n", blurb2);
1169 printf("---progress-form---3--- %s\n", blurb3);
[541]1170 printf("---progress-form---E---\n");
[2211]1171 mr_asprintf(&taskprogress, "TASK: [");
[128]1172 for (i = 0; i < percentage; i += 5) {
[2211]1173 mr_strcat(taskprogress, "*");
[128]1174 }
1175 for (; i < 100; i += 5) {
[2211]1176 mr_strcat(taskprogress, ".");
[128]1177 }
1178 if (percentage > 100) {
[541]1179 log_msg(2, "percentage = %d", percentage);
[128]1180 }
[2211]1181 mr_strcat(taskprogress, "] %3d%c", percentage, '%');
1182 mr_strcat(taskprogress, " done; %2ld:%02ld to go",
[128]1183 time_remaining / 60, time_remaining % 60);
[1899]1184 printf("---progress-form---4--- %s\n", taskprogress);
[2211]1185 paranoid_free(taskprogress);
[128]1186 } else {
[2274]1187 mr_asprintf(&timeline_str,
1188 "%2ld:%02ld taken %2ld:%02ld remaining ",
1189 time_taken / 60, time_taken % 60, time_remaining / 60,
1190 time_remaining % 60);
1191 mr_asprintf(percentline_str, " %3d%% done %3d%% to go",
1192 percentage, 100 - percentage);
1193
[128]1194 center_string(blurb1, 54);
1195 center_string(blurb2, 54);
1196 center_string(blurb3, 54);
[1899]1197 if (g_blurb1) {
1198 newtLabelSetText(g_blurb1, blurb1);
1199 }
1200 if (g_blurb2) {
1201 newtLabelSetText(g_blurb2, blurb3);
1202 }
1203 if (g_blurb3) {
1204 newtLabelSetText(g_blurb3, blurb2);
1205 }
[128]1206 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
[1899]1207 if ((percentage >= 2) && (g_timeline)) {
[128]1208 newtLabelSetText(g_timeline, timeline_str);
1209 }
[2274]1210 mr_free(timeline_str);
1211
[1899]1212 if (g_percentline) {
1213 newtLabelSetText(g_percentline, percentline_str);
1214 }
[2274]1215 mr_free(percentline_str);
1216
[128]1217 newtRefresh();
1218 }
[1]1219 }
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234/**
1235 * Ask the user which backup media type they would like to use.
1236 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1237 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs,
1238 * and @c iso.
1239 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1240 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1241 */
[128]1242 t_bkptype which_backup_media_type(bool restoring) {
[1]1243
[128]1244 /*@ char ************************************************************ */
1245 t_bkptype output;
[1]1246
1247
[128]1248 /*@ newt ************************************************************ */
1249 char *title_sz;
1250 char *minimsg_sz;
1251 static t_bkptype possible_bkptypes[] =
1252 { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso };
1253 static char *possible_responses[] =
1254 { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev",
1255"nfs", "iso", NULL };
1256 char *outstr;
1257 t_bkptype backup_type;
1258 int i;
[1]1259
[128]1260 newtComponent b1;
1261 newtComponent b2;
1262 newtComponent b3;
1263 newtComponent b4;
1264 newtComponent b5;
1265 newtComponent b6;
1266 newtComponent b7;
1267 newtComponent b8;
1268 newtComponent b_res;
1269 newtComponent myForm;
[1]1270
[128]1271 title_sz = malloc(MAX_NEWT_COMMENT_LEN);
1272 minimsg_sz = malloc(MAX_NEWT_COMMENT_LEN);
1273 outstr = malloc(MAX_NEWT_COMMENT_LEN);
1274 if (g_text_mode) {
1275 for (backup_type = none; backup_type == none;) {
[541]1276 printf("Backup type (");
[128]1277 for (i = 0; possible_responses[i]; i++) {
1278 printf("%c%s", (i == 0) ? '\0' : ' ',
1279 possible_responses[i]);
1280 }
1281 printf(")\n--> ");
1282 (void) fgets(outstr, MAX_NEWT_COMMENT_LEN, stdin);
1283 strip_spaces(outstr);
1284 for (i = 0; possible_responses[i]; i++) {
1285 if (!strcmp(possible_responses[i], outstr)) {
1286 backup_type = possible_bkptypes[i];
1287 }
1288 }
1289 }
1290 paranoid_free(title_sz);
1291 paranoid_free(minimsg_sz);
1292 paranoid_free(outstr);
1293 return (backup_type);
1294 }
1295 newtDrawRootText(18, 0, WELCOME_STRING);
1296 if (restoring) {
1297 strcpy(title_sz,
[541]1298 "Please choose the backup media from which you want to read data.");
1299 strcpy(minimsg_sz, "Read from:");
[128]1300 } else {
1301 strcpy(title_sz,
[541]1302 "Please choose the backup media to which you want to archive data.");
1303 strcpy(minimsg_sz, "Backup to:");
[128]1304 }
1305 newtPushHelpLine(title_sz);
1306 // newtOpenWindow (23, 3, 34, 17, minimsg_sz);
1307 newtCenteredWindow(34, 17, minimsg_sz);
[541]1308 b1 = newtButton(1, 1, "CD-R disks ");
1309 b2 = newtButton(17, 1, "CD-RW disks");
1310 b3 = newtButton(1, 9, "Tape drive ");
[1703]1311 b4 = newtButton(17, 5, "USB Key/Disk");
[541]1312 b5 = newtButton(1, 5, " DVD disks ");
1313 b6 = newtButton(17, 9, " NFS mount ");
1314 b7 = newtButton(1, 13, " Hard disk ");
1315 b8 = newtButton(17, 13, " Exit ");
[128]1316 myForm = newtForm(NULL, NULL, 0);
1317 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1318 NULL);
1319 b_res = newtRunForm(myForm);
1320 newtFormDestroy(myForm);
1321 newtPopWindow();
1322 if (b_res == b1) {
1323 output = cdr;
1324 } else if (b_res == b2) {
1325 output = cdrw;
1326 } else if (b_res == b3) {
1327 output = tape;
1328 } else if (b_res == b4) {
[1703]1329 output = usb;
[128]1330 } else if (b_res == b5) {
1331 output = dvd;
1332 } else if (b_res == b6) {
1333 output = nfs;
1334 } else if (b_res == b7) {
1335 output = iso;
1336 } else {
1337 output = none;
1338 }
1339 newtPopHelpLine();
1340 paranoid_free(title_sz);
1341 paranoid_free(minimsg_sz);
1342 paranoid_free(outstr);
1343 return (output);
1344 }
[1]1345
1346
1347
1348
1349/**
1350 * Ask the user how much compression they would like to use.
1351 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1352 * @return The compression level (0-9) chosen, or -1 for "Exit".
1353 */
[128]1354 int
1355 which_compression_level() {
[1]1356
[128]1357 /*@ char ************************************************************ */
1358 int output = none;
[1]1359
1360
[128]1361 /*@ newt ************************************************************ */
[1]1362
[128]1363 newtComponent b1;
1364 newtComponent b2;
1365 newtComponent b3;
1366 newtComponent b4;
1367 newtComponent b5;
1368 newtComponent b_res;
1369 newtComponent myForm;
[1]1370
[128]1371 newtDrawRootText(18, 0, WELCOME_STRING);
1372 newtPushHelpLine
[541]1373 (" Please specify the level of compression that you want.");
[128]1374 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
[541]1375 newtCenteredWindow(34, 13, "How much compression?");
1376 b1 = newtButton(4, 1, "Maximum");
1377 b2 = newtButton(18, 1, "Average");
[1463]1378 b3 = newtButton(4, 5, "Minimum");
[541]1379 b4 = newtButton(18, 5, " None ");
1380 b5 = newtButton(4, 9, " Exit ");
[128]1381 myForm = newtForm(NULL, NULL, 0);
1382 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1383 b_res = newtRunForm(myForm);
1384 newtFormDestroy(myForm);
1385 newtPopWindow();
1386 if (b_res == b1) {
1387 output = 9;
1388 } else if (b_res == b2) {
1389 output = 4;
1390 } else if (b_res == b3) {
1391 output = 1;
1392 } else if (b_res == b4) {
1393 output = 0;
1394 } else if (b_res == b5) {
1395 output = -1;
1396 }
1397 newtPopHelpLine();
1398 return (output);
1399 }
[1]1400
1401
1402
1403
1404
1405/**
1406 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1407 * @p ARBITRARY_MAXIMUM entries.
1408 * @param filelist The filelist structure to load @p source_file into.
1409 * @param source_file The file containing a list of filenames to load into @p filelist.
1410 */
[128]1411 int load_filelist_into_array(struct s_filelist *filelist,
1412 char *source_file) {
1413 int i;
[1662]1414 int j = 0;
[128]1415 bool done;
1416 char *tmp;
[1662]1417 char *tmp1 = NULL;
1418 FILE *fin, *fout;
[128]1419 struct s_filelist_entry dummy_fle;
[1]1420
[128]1421 assert(filelist != NULL);
1422 assert_string_is_neither_NULL_nor_zerolength(source_file);
[1]1423
[2230]1424 log_it("entering");
[128]1425 if (!(fin = fopen(source_file, "r"))) {
1426 log_OS_error(source_file);
1427 log_msg(2, "Can't open %s; therefore, cannot popup list",
1428 source_file);
1429 return (1);
1430 }
[1662]1431
[2211]1432 mr_asprintf(&tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
[1662]1433 if (!(fout = fopen(tmp1, "a"))) {
1434 log_msg(2, "Can't write to %s", tmp1);
1435 return(1);
1436 }
1437
1438 malloc_string(tmp);
[128]1439 log_msg(2, "Loading %s", source_file);
1440 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM;
1441 filelist->entries++) {
1442 god_i_hate_gotos:
1443 if (feof(fin)) {
1444 break;
1445 }
1446 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, fin);
1447 i = (int) strlen(tmp);
1448 if (i < 2) {
1449 goto god_i_hate_gotos;
1450 }
1451 if (tmp[i - 1] < 32) {
1452 tmp[--i] = '\0';
1453 }
1454 if (i < 2) {
1455 goto god_i_hate_gotos;
1456 }
[1662]1457 if (!does_file_exist(tmp) && !feof(fin)) {
1458 j++;
1459 fprintf(fout, "%s\n", tmp);
1460
[128]1461 goto god_i_hate_gotos;
1462 }
1463 filelist->el[filelist->entries].severity =
1464 severity_of_difference(tmp, NULL);
1465 strcpy(filelist->el[filelist->entries].filename, tmp);
1466 if (feof(fin)) {
1467 break;
1468 }
1469 }
1470 paranoid_fclose(fin);
[1662]1471 paranoid_fclose(fout);
1472 if (j > 0) {
1473 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);
1474 }
1475 paranoid_free(tmp1);
[128]1476 if (filelist->entries >= ARBITRARY_MAXIMUM) {
[541]1477 log_to_screen("Arbitrary limits suck, man!");
[128]1478 paranoid_free(tmp);
1479 return (1);
1480 }
1481 for (done = FALSE; !done;) {
1482 done = TRUE;
1483 for (i = 0; i < filelist->entries - 1; i++) {
[1]1484// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
[128]1485 if (filelist->el[i].severity < filelist->el[i + 1].severity
1486 || (filelist->el[i].severity ==
1487 filelist->el[i + 1].severity
1488 && strcmp(filelist->el[i].filename,
1489 filelist->el[i + 1].filename) > 0)) {
1490 memcpy((void *) &dummy_fle,
1491 (void *) &(filelist->el[i]),
1492 sizeof(struct s_filelist_entry));
1493 memcpy((void *) &(filelist->el[i]),
1494 (void *) &(filelist->el[i + 1]),
1495 sizeof(struct s_filelist_entry));
1496 memcpy((void *) &(filelist->el[i + 1]),
1497 (void *) &dummy_fle,
1498 sizeof(struct s_filelist_entry));
1499 log_msg(2, "Swapping %s and %s",
1500 filelist->el[i].filename,
1501 filelist->el[i + 1].filename);
1502 done = FALSE;
1503 }
1504 }
1505 }
1506 paranoid_free(tmp);
[2230]1507 log_it("leaving");
[128]1508 return (0);
1509 }
[1]1510
1511
1512
1513/**
1514 * Generate a pretty string based on @p flentry.
1515 * @param flentry The filelist entry to stringify.
1516 * @return The string form of @p flentry.
1517 * @note The returned value points to static storage that will be overwritten with each call.
1518 */
[128]1519 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1520 static char comment[100];
[2211]1521 char *tmp = NULL;
[1]1522
[2230]1523 log_it("entering");
[128]1524 assert(flentry != NULL);
1525 if (flentry->severity == 0) {
[2211]1526 mr_asprintf(&tmp, "0 ");
[128]1527 } else if (flentry->severity == 1) {
[2211]1528 mr_asprintf(&tmp, "low ");
[128]1529 } else if (flentry->severity == 2) {
[2211]1530 mr_asprintf(&tmp, "med ");
[128]1531 } else {
[2211]1532 mr_asprintf(&tmp, "high");
[128]1533 }
[2211]1534 mr_strcat(tmp, " ");
1535 mr_strcat(tmp, flentry->filename);
1536 strncpy(comment, tmp, 99);
[128]1537 paranoid_free(tmp);
[2211]1538
[2230]1539 log_it("leaving");
[128]1540 return (comment);
1541 }
[1]1542
1543
1544
1545
1546
1547/**
1548 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1549 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1550 * @param source_file The file containing a list of changed files.
1551 */
[128]1552 void popup_changelist_from_file(char *source_file) {
1553 char *reason;
1554 newtComponent myForm;
1555 newtComponent bClose;
1556 newtComponent bSelect;
1557 newtComponent b_res;
1558 newtComponent fileListbox;
1559 newtComponent headerMsg;
[1]1560
[128]1561 /*@ ???? ************************************************************ */
1562 void *curr_choice;
1563 void *keylist[ARBITRARY_MAXIMUM];
[1]1564
[128]1565 /*@ int ************************************************************* */
1566 int currline = 0;
1567 int finished = FALSE;
[738]1568
1569 /*@ long ************************************************************ */
1570 long i = 0;
[128]1571 long lng = 0;
[1]1572
[128]1573 /*@ buffers ********************************************************* */
[2274]1574 char *tmp = NULL;
1575 char *differ_sz = NULL;
[1]1576
[128]1577 struct s_filelist *filelist;
1578 assert_string_is_neither_NULL_nor_zerolength(source_file);
1579 if (g_text_mode) {
1580 log_msg(2, "Text mode. Therefore, no popup list.");
[2274]1581 return;
[128]1582 }
1583 log_msg(2, "Examining file %s", source_file);
[1]1584
[128]1585 lng = count_lines_in_file(source_file);
1586 if (lng < 1) {
1587 log_msg(2, "No lines in file. Therefore, no popup list.");
[2274]1588 return;
[128]1589 } else if (lng >= ARBITRARY_MAXIMUM) {
1590 log_msg(2, "Too many files differ for me to list.");
[2274]1591 return;
[128]1592 }
[1]1593
[128]1594 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1595 fileListbox =
1596 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1597 newtListboxClear(fileListbox);
[1]1598
[128]1599 if (load_filelist_into_array(filelist, source_file)) {
[2274]1600 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
[128]1601 return;
1602 }
1603 log_msg(2, "%d files loaded into filelist array",
1604 filelist->entries);
1605 for (i = 0; i < filelist->entries; i++) {
1606 keylist[i] = (void *) i;
1607 newtListboxAppendEntry(fileListbox,
1608 filelist_entry_to_string(&
1609 (filelist->
1610 el[i])),
1611 keylist[i]);
1612 }
[2274]1613 mr_asprintf(&differ_sz,
[738]1614 " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.",
[128]1615 i);
1616 newtPushHelpLine(differ_sz);
[2274]1617 mr_free(differ_sz);
1618
[541]1619 bClose = newtCompactButton(10, 15, " Close ");
1620 bSelect = newtCompactButton(30, 15, " Select ");
[2274]1621 mr_asprintf(&tmp, "%-10s %-20s", "Priority", "Filename");
[128]1622 headerMsg = newtLabel(2, 1, tmp);
[2274]1623 mr_free(tmp);
1624
[541]1625 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
[128]1626 myForm = newtForm(NULL, NULL, 0);
1627 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1628 bSelect, NULL);
[2274]1629
1630 malloc_string(reason);
[128]1631 while (!finished) {
1632 b_res = newtRunForm(myForm);
1633 if (b_res == bClose) {
1634 finished = TRUE;
1635 } else {
1636 curr_choice = newtListboxGetCurrent(fileListbox);
1637 for (i = 0;
1638 i < filelist->entries && keylist[i] != curr_choice;
1639 i++);
1640 if (i == filelist->entries && filelist->entries > 0) {
[541]1641 log_to_screen("I don't know what that button does!");
[128]1642 } else {
1643 currline = i;
1644 if (filelist->entries > 0) {
[2274]1645 severity_of_difference(filelist->el[currline]. filename, reason);
1646 mr_asprintf(&tmp, "%s --- %s", filelist->el[currline].filename, reason);
1647
[128]1648 popup_and_OK(tmp);
[2274]1649 mr_free(tmp);
[128]1650 }
1651 }
1652 }
1653 }
[2274]1654 paranoid_free(reason);
1655
[128]1656 newtFormDestroy(myForm);
1657 newtPopWindow();
1658 newtPopHelpLine();
1659 return;
[1]1660 }
1661
1662/* @} - end of guiGroup */
1663
1664
1665#if __cplusplus
[128]1666} /* extern "C" */
[1]1667#endif
1668
1669
[128]1670void wait_until_software_raids_are_prepped(char *mdstat_file,
1671 int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.