source: MondoRescue/branches/3.1/mondo/src/common/newt-specific.c@ 3148

Last change on this file since 3148 was 3148, checked in by Bruno Cornec, 11 years ago

2nd phase for svn merge -r 2935:3146 ../3.0

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