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

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

r3369@localhost: bruno | 2009-08-18 16:57:27 +0200

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