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

Last change on this file since 3161 was 3161, checked in by Bruno Cornec, 12 years ago
  • Apply fixes to 3.1 tree so that at least mondo compiles again (but that branch probably doesn't work at all again).
  • Property svn:keywords set to Id
File size: 44.8 KB
RevLine 
[1]1/* newt-specific.c
[99]2 $Id: newt-specific.c 3161 2013-06-25 20:53:14Z 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 3161 2013-06-25 20:53:14Z 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;
[128]173 int i;
[1]174
[3147]175 if (g_fail_immediately) {
176 // We consider the user aborted by using the -F option
177 log_msg(3, "Exiting at first interaction request due to -F");
178 finish(1);
179 }
[2357]180
[128]181 if (g_text_mode) {
[2376]182 sync();
[2357]183 printf("---promptdialogOKC---1--- %s\n---promptdialogOKC---Q--- [OK] [Cancel] ---\n--> ", prompt);
184 mr_getline(tmp, stdin);
[128]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)) {
[2357]193 mr_free(tmp);
[128]194 return (TRUE);
195 } else {
[2357]196 mr_free(tmp);
[128]197 return (FALSE);
198 }
199 } else {
[541]200 return (popup_with_buttons(prompt, " Okay ", "Cancel"));
[128]201 }
[1]202 }
203
204
205
206/**
207 * Close the currently opened evalcall form.
208 */
[128]209 void
210 close_evalcall_form(void) {
211 if (g_text_mode) {
212 return;
213 }
214 if (g_isoform_main == NULL) {
215 return;
216 }
217 update_evalcall_form(100);
218 usleep(500000);
219 if (g_text_mode) {
220 log_msg(2, "Closing evalcall form");
221 return;
222 }
223 newtPopHelpLine();
224 newtFormDestroy(g_isoform_main);
225 newtPopWindow();
[2291]226
227 /* Reset globals */
[128]228 g_isoform_main = NULL;
229 g_isoform_old_progress = -1;
[2291]230 g_isoform_header = NULL;
231 g_isoform_scale = NULL;
232 g_isoform_timeline = NULL;
233 g_isoform_pcline = NULL;
234}
[1]235
236/**
237 * Close the currently opened progress form.
238 */
[128]239 void
240 close_progress_form() {
241 if (g_text_mode) {
242 return;
243 }
244 if (g_current_progress == -999) {
[2377]245 log_msg(2, "Trying to close the progress form when it ain't open!");
[128]246 return;
247 }
248 g_current_progress = g_maximum_progress;
249 update_progress_form("Complete");
250 sleep(1);
251 if (g_text_mode) {
252 log_msg(2, "Closing progress form");
253 return;
254 }
255 newtPopHelpLine();
256 newtFormDestroy(g_progressForm);
257 newtPopWindow();
[2291]258
259 /* Reset globals */
[128]260 g_progressForm = NULL;
261 g_current_progress = -999;
[2291]262 g_timeline = NULL;
263 g_percentline = NULL;
264 g_scale = NULL;
265
[128]266 }
[1]267
[792]268/**
269 * Kill any process containing the string @p str surrounded by spaces in its commandline.
270 */
271void kill_anything_like_this(char *str) {
[1]272
[792]273char *tmp = NULL;
[1850]274char *tmp1 = NULL;
[1]275
[2937]276 mr_asprintf(tmp,"ps %s | grep \" %s \" | grep -v \"grep\" | awk '{print %s;}'", ps_options, str , ps_proc_id);
[1294]277 run_program_and_log_output(tmp, TRUE);
[1850]278 if (strlen(tmp) > 0) {
[2323]279 mr_asprintf(tmp1,"kill `%s`", tmp);
[1850]280 run_program_and_log_output(tmp1, TRUE);
[2321]281 mr_free(tmp1);
[1850]282 }
[2321]283 mr_free(tmp);
[792]284}
[1]285
[792]286
[1]287/**
288 * Exit Mondo with a fatal error.
289 * @param error_string The error message to present to the user before exiting.
290 * @note This function never returns.
291 */
[2377]292void fatal_error(char *error_string) {
293
[128]294 /*@ buffers ***************************************************** */
[2320]295 char *command = NULL;
[128]296 static bool already_exiting = FALSE;
[1]297
[128]298 /*@ end vars **************************************************** */
[1]299
[128]300 set_signals(FALSE); // link to external func
301 g_exiting = TRUE;
302 log_msg(1, "Fatal error received - '%s'", error_string);
303 printf("Fatal error... %s\n", error_string);
304 if (getpid() == g_mastermind_pid) {
305 log_msg(2, "mastermind %d is exiting", (int) getpid());
306 kill(g_main_pid, SIGTERM);
307 finish(1);
308 }
[1]309
[128]310 if (getpid() != g_main_pid) {
311 if (g_mastermind_pid != 0 && getpid() != g_mastermind_pid) {
312 log_msg(2, "non-m/m %d is exiting", (int) getpid());
313 kill(g_main_pid, SIGTERM);
314 finish(1);
315 }
316 }
[1]317
[128]318 log_msg(3, "OK, I think I'm the main PID.");
319 if (already_exiting) {
320 log_msg(3, "...I'm already exiting. Give me time, Julian!");
321 finish(1);
322 }
[1]323
[128]324 already_exiting = TRUE;
325 log_msg(2, "I'm going to do some cleaning up now.");
326 paranoid_system("killall mindi 2> /dev/null");
327 kill_anything_like_this("/mondo/do-not");
328 kill_anything_like_this("tmp.mondo");
[296]329 kill_anything_like_this("ntfsclone");
[128]330 sync();
[1]331
[3147]332 if (chdir("/")) {
333 // FIXME
334 }
[128]335 if (g_selfmounted_isodir) {
[2937]336 mr_asprintf(command, "umount -d %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 */
[2377]380void finish(int signal) {
[2340]381
[1708]382 char *command = NULL;
[1]383
[2937]384 /* Before removing dir, make sure we're out of them */
[3147]385 if (chdir("/tmp")) {
386 // FIXME
387 }
[2937]388 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
[128]389 if (g_selfmounted_isodir) {
[2937]390 mr_asprintf(command, "umount -d %s", g_selfmounted_isodir);
[128]391 run_program_and_log_output(command, 1);
[2320]392 mr_free(command);
393
[2323]394 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
[128]395 run_program_and_log_output(command, 1);
[2320]396 mr_free(command);
[128]397 }
[2241]398 if (bkpinfo->tmpdir) {
399 log_msg(8,"tempdir is %s",bkpinfo->tmpdir);
400 if (strstr(bkpinfo->tmpdir ,"mondo.tmp.") != NULL) {
[2937]401 log_msg(8,"erasing tempdir %s",bkpinfo->tmpdir);
[2323]402 mr_asprintf(command, "rm -Rf %s", bkpinfo->tmpdir);
[3147]403 paranoid_system(command);
[2318]404 mr_free(command);
[2241]405 }
[1999]406 }
[2241]407 if (bkpinfo->scratchdir) {
408 log_msg(8,"scratchdir is %s", bkpinfo->scratchdir);
409 if (strstr(bkpinfo->scratchdir ,"mondo.scratch.") != NULL) {
[2937]410 log_msg(8,"erasing scratchdir %s",bkpinfo->scratchdir);
[2323]411 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
[3147]412 paranoid_system(command);
[2318]413 mr_free(command);
[2241]414 }
415 }
416 /* Free all allocated strings in bkpinfo */
[2321]417 mr_free_bkpinfo();
[2816]418
[128]419 free_libmondo_global_strings();
[2241]420 free_newt_stuff();
[2340]421 if (!g_text_mode) {
422 log_msg(1, "Calling newtFinished()");
423 newtFinished();
424 }
425 printf("Execution run ended; result=%d\n", signal);
426 printf("Type 'less %s' to see the output log\n", MONDO_LOGFILE);
[128]427 exit(signal);
428 }
[1]429
430
431/**
432 * Log the last @p g_noof_log_lines lines of @p filename that match @p
433 * grep_for_me to the screen.
434 * @param filename The file to give the end of.
435 * @param grep_for_me If not "", then only give lines in @p filename that match this regular expression.
436 */
[2357]437void 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);) {
[3161]467 if (fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN, fin)) {
468 //FIXME
469 }
[1274]470 /* Commented to make valgrind happy and avoid crash
[128]471 strip_spaces(err_log_lines[i]);
[1274]472 */
[128]473 if (!strncmp(err_log_lines[i], "root:", 5)) {
[2334]474 mr_asprintf(tmp, "%s", err_log_lines[i] + 6);
[2703]475 strncpy(err_log_lines[i], tmp, (size_t)MAX_NEWT_COMMENT_LEN);
[2334]476 mr_free(tmp);
[128]477 }
478 if (feof(fin)) {
479 break;
480 }
481 }
482 }
483 paranoid_pclose(fin);
484 }
[2334]485 mr_free(command);
[128]486 refresh_log_screen();
[1]487 }
488
489
490/**
491 * Log a message to the screen.
492 * @param fmt A printf-style format string to write. The following parameters are its arguments.
493 * @note The message is also written to the logfile.
494 */
[2377]495void 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);
[2703]508 vsnprintf(output, MAX_STR_LEN-1, fmt, args);
[128]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 */
[2377]551void mvaddstr_and_log_it(int y, int x, char *output) {
552
[128]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 */
[2377]570void open_evalcall_form(char *ttl) {
[1]571
[128]572 /*@ buffers ********************************************************* */
[2334]573 char *title = NULL;
574 char *tmp = NULL;
[1]575
[128]576 /*@ initialize ****************************************************** */
577 g_isoform_old_progress = -1;
578 g_mysterious_dot_counter = 0;
[1]579
[128]580 assert(ttl != NULL);
[2334]581 malloc_string(tmp);
582 mr_asprintf(title, "%s", ttl);
[128]583 strcpy(g_isoform_header_str, title);
584 if (g_text_mode) {
585 log_msg(0, title);
586 } else {
587 strcpy(tmp, title);
588 center_string(tmp, 80);
589 newtPushHelpLine(tmp);
590 }
591 center_string(g_isoform_header_str, 36);
592 g_isoform_starttime = get_time();
593 if (g_text_mode) {
594 log_msg(0, g_isoform_header_str);
595 } else {
596 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
597 g_isoform_scale = newtScale(3, 3, 34, 100);
598 newtCenteredWindow(40, 7, title);
599 g_isoform_main = newtForm(NULL, NULL, 0);
[2937]600 g_isoform_timeline = newtLabel(1, 5, " ");
601 g_isoform_pcline = newtLabel(1, 6, " ");
[128]602 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
603 g_isoform_pcline, g_isoform_header,
604 g_isoform_scale, NULL);
[2937]605 newtDrawForm(g_isoform_main);
[128]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 */
[2377]624void open_progress_form(char *title, char *b1, char *b2, char *b3, long max_val) {
[1]625
[128]626 /*@ buffers ********************************************************* */
627 char *b1c;
[2334]628 char *blurb1 = NULL;
629 char *blurb2 = NULL;
630 char *blurb3 = NULL;
[1]631
[128]632 /*@ initialize ****************************************************** */
633 g_mysterious_dot_counter = 0;
[1]634
[2334]635 malloc_string(b1c);
[1]636
[128]637 assert(title != NULL);
638 assert(b1 != NULL);
639 assert(b2 != NULL);
640 assert(b3 != NULL);
[1]641
[2377]642 mr_asprintf(blurb1, "%s", b1);
643 mr_asprintf(blurb2, "%s", b2);
644 mr_asprintf(blurb3, "%s", b3);
645
[128]646 strcpy(b1c, b1);
647 center_string(b1c, 80);
648 if (max_val <= 0) {
649 max_val = 1;
650 }
[1]651
[128]652 g_start_time = get_time();
653 g_maximum_progress = max_val;
654 g_current_progress = 0;
655 strcpy(g_blurb_str_1, blurb1);
656 strcpy(g_blurb_str_2, blurb3);
657 strcpy(g_blurb_str_3, blurb2);
658 if (g_text_mode) {
659 log_msg(0, blurb1);
660 log_msg(0, blurb2);
661 log_msg(0, blurb3);
662 } else {
663 g_blurb1 = newtLabel(2, 1, blurb1);
664 g_blurb2 = newtLabel(2, 2, blurb3);
665 g_blurb3 = newtLabel(2, 4, blurb2);
666 newtCenteredWindow(60, 11, title);
[2421]667 g_scale = newtScale(3, 6, 54, (long long)g_maximum_progress);
[128]668 g_progressForm = newtForm(NULL, NULL, 0);
[2937]669 g_percentline = newtLabel(10, 9, " ");
670 g_timeline = newtLabel(10, 8, " ");
[2377]671 newtFormAddComponents(g_progressForm, g_percentline, g_timeline, g_scale, g_blurb1, g_blurb3, g_blurb2, NULL);
[128]672 newtPushHelpLine(b1c);
[2937]673 newtDrawForm(g_progressForm);
[128]674 newtRefresh();
675 }
676 update_progress_form_full(blurb1, blurb2, blurb3);
677 paranoid_free(b1c);
[2334]678 mr_free(blurb1);
679 mr_free(blurb2);
680 mr_free(blurb3);
[128]681 }
682
[1]683/**
684 * Give a message to the user in the form of a dialog box (under Newt).
685 * @param prompt The message.
686 */
[2377]687void popup_and_OK(char *prompt) {
688
[128]689 char ch;
[1]690
[128]691 assert_string_is_neither_NULL_nor_zerolength(prompt);
[1]692
[128]693 log_msg(0, prompt);
694 if (g_text_mode) {
[2377]695 printf("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ", prompt);
[128]696 while (((ch = getchar()) != '\n') && (ch != EOF));
697 } else {
[541]698 (void) popup_with_buttons(prompt, " OK ", "");
[128]699 }
700 }
[1]701
702/**
703 * Ask the user to enter a value.
704 * @param title The title of the dialog box.
705 * @param b The blurb (e.g. what you want the user to enter).
[2316]706 * @param input The string given originaly as default
707 * @return output string if the user pressed OK, NULL if they pressed Cancel.
[1]708 */
[2377]709char *popup_and_get_string(char *title, char *b, char *input) {
[1]710
[128]711 /*@ newt ************************************************************ */
712 newtComponent myForm;
713 newtComponent b_1;
714 newtComponent b_2;
715 newtComponent b_res;
716 newtComponent text;
717 newtComponent type_here;
[1]718
[128]719 /*@ pointers ********************************************************* */
720 char *entry_value;
[1]721
[128]722 /*@ buffers ********************************************************** */
[2316]723 char *blurb = NULL;
724 char *output = NULL;
[1]725
[128]726 assert_string_is_neither_NULL_nor_zerolength(title);
727 assert(b != NULL);
[1]728
[128]729 if (g_text_mode) {
[2316]730 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q--- [%s]\n--> ", title, b, input);
[2331]731 mr_getline(output, stdin);
[2316]732 if (! strcmp(output,"\n")) {
733 /* take default if ENTER was typed */
734 mr_free(output);
[2323]735 mr_asprintf(output, "%s", input);
[2316]736 }
[128]737 if (output[strlen(output) - 1] == '\n')
738 output[strlen(output) - 1] = '\0';
[2316]739 return (output);
[128]740 }
[2316]741
[2323]742 mr_asprintf(blurb, "%s", b);
[128]743 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
[2377]744 type_here = newtEntry(2, newtTextboxGetNumLines(text) + 2, input, 50,
[1]745#ifdef __cplusplus
[128]746 0, NEWT_FLAG_RETURNEXIT
[1]747#else
[128]748 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
[1]749#endif
[128]750 );
[541]751 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
752 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
[128]753 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
754 myForm = newtForm(NULL, NULL, 0);
755 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
756 center_string(blurb, 80);
757 newtPushHelpLine(blurb);
758 b_res = newtRunForm(myForm);
[2316]759
760 if (b_res == b_1) {
[2323]761 mr_asprintf(output, "%s", newtEntryGetValue(type_here));
[2316]762 }
[128]763 newtPopHelpLine();
764 newtFormDestroy(myForm);
765 newtPopWindow();
[2316]766 mr_free(blurb);
767
768 return (output);
[128]769 }
[1]770
771
772/**
773 * Pop up a dialog box with user-defined buttons.
774 * @param p The text to put in the dialog box.
775 * @param button1 The label on the first button.
776 * @param button2 The label on the second button, or "" if you only want one button.
777 * @return TRUE if @p button1 was pushed, FALSE otherwise.
778 */
[2377]779bool popup_with_buttons(char *p, char *button1, char *button2) {
[1]780
[128]781 /*@ buffers *********************************************************** */
[2357]782 char *prompt = NULL;
[1]783
[128]784 /*@ newt ************************************************************** */
785 newtComponent myForm;
786 newtComponent b_1;
787 newtComponent b_2;
788 newtComponent b_res;
789 newtComponent text;
[1]790
[128]791 assert_string_is_neither_NULL_nor_zerolength(p);
792 assert(button1 != NULL);
793 assert(button2 != NULL);
794 if (g_text_mode) {
795 if (strlen(button2) == 0) {
796 printf("%s (%s) --> ", p, button1);
797 } else {
798 printf("%s (%s or %s) --> ", p, button1, button2);
799 }
[2357]800 mr_getline(prompt, stdin);
801 while (strcmp(prompt, button1) && (strlen(button2) == 0 || strcmp(prompt, button2))) {
[128]802 printf("--> ");
[2357]803 mr_free(prompt);
804 mr_getline(prompt, stdin);
[128]805 }
[2136]806 if (!strcmp(prompt, button1)) {
[2357]807 mr_free(prompt);
[128]808 return (TRUE);
809 } else {
[2357]810 mr_free(prompt);
[128]811 return (FALSE);
812 }
813 }
[1]814
[2357]815 prompt = malloc(MAX_NEWT_COMMENT_LEN);
[877]816 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
[883]817 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
[128]818 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
[2377]819 b_1 = newtButton(20 - ((button2[0] != '\0') ? strlen(button1) + 2 : strlen(button1) / 2), newtTextboxGetNumLines(text) + 3, button1);
[128]820 if (button2[0] != '\0') {
[2377]821 b_2 = newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
[128]822 } else {
823 b_2 = NULL;
824 }
[541]825 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
[128]826 myForm = newtForm(NULL, NULL, 0);
827 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
828 center_string(prompt, 80);
829 newtPushHelpLine(prompt);
830 b_res = newtRunForm(myForm);
831 newtPopHelpLine();
832 newtFormDestroy(myForm);
833 newtPopWindow();
834 if (b_res == b_1) {
835 paranoid_free(prompt);
836 return (TRUE);
837 } else {
838 paranoid_free(prompt);
839 return (FALSE);
840 }
841 }
[1]842
843
844
[128]845
[1]846/**
847 * Synchronize the log messages stored in @p err_log_lines with those shown
848 * on the screen.
849 */
[2377]850void refresh_log_screen() {
[1]851
[128]852 /*@ int *********************************************************** */
853 int i = 0;
[1]854
855
[128]856 if (g_text_mode || !err_log_lines) {
857 return;
858 }
859 for (i = g_noof_log_lines - 1; i >= 0; i--) {
860 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
861 " ");
862 }
863 newtRefresh();
864 for (i = g_noof_log_lines - 1; i >= 0; i--) {
865 err_log_lines[i][79] = '\0';
866 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
867 err_log_lines[i]);
868 }
869 newtRefresh();
870 }
[1]871
872
873/**
874 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
875 * only allocate some memory.
876 */
[2377]877void setup_newt_stuff() {
[1]878
[128]879 /*@ int *********************************************************** */
880 int i = 0;
881 int cols;
[1]882
[128]883 if (!g_text_mode) {
884 newtInit();
885 newtCls();
[2321]886 newtPushHelpLine("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
[128]887 newtDrawRootText(18, 0, WELCOME_STRING);
888 newtRefresh();
889 newtGetScreenSize(&cols, &g_noof_rows);
890 g_noof_log_lines = (g_noof_rows / 5) + 1;
891 }
[1]892
[2320]893 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
[1]894
[128]895 for (i = 0; i < g_noof_log_lines; i++) {
[2320]896 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
[128]897 }
[1]898
[128]899 for (i = 0; i < g_noof_log_lines; i++) {
900 err_log_lines[i][0] = '\0';
901 }
902 }
[1]903
904
905/**
906 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
907 * @param num The numerator of the ratio.
908 * @param denom The denomenator of the ratio.
909 */
[2377]910void update_evalcall_form_ratio(int num, int denom) {
[1]911
[128]912 /*@ long ************************************************************ */
[2277]913 long current_time = 0L;
914 long time_taken = 0L;
915 long time_total_est = 0L;
916 long time_remaining = 0L;
[1]917
[128]918 /*@ buffers ********************************************************** */
[2274]919 char *timeline_str = NULL;
[2211]920 char *pcline_str = NULL;
921 char *taskprogress = NULL;
[1]922
[128]923 /*@ int ************************************************************** */
924 int percentage = 0;
925 int i = 0;
926 int j = 0;
[1]927
[128]928 if (num * 100 < denom) {
929 percentage = 1;
930 } else {
931 percentage = (num * 100 + denom / 2) / denom;
932 }
933
934 current_time = get_time();
935 time_taken = current_time - g_isoform_starttime;
936 if (num) {
937 time_total_est = time_taken * denom / num;
938 time_remaining = time_total_est - time_taken;
939 } else {
[2277]940 time_remaining = 0L;
[128]941 }
942 if (!g_text_mode) {
[1899]943 if (g_isoform_header) {
944 newtLabelSetText(g_isoform_header, g_isoform_header_str);
945 }
[128]946 }
947 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
948 if ((percentage < 3 && g_isoform_old_progress < 3)
949 || percentage > g_isoform_old_progress) {
950 g_isoform_old_progress = percentage;
[2323]951 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]952 if (percentage < 3) {
[2323]953 mr_asprintf(pcline_str, " Working");
[128]954 for (j = 0; j < g_mysterious_dot_counter; j++) {
[2211]955 mr_strcat(pcline_str, ".");
[128]956 }
957 for (; j < 27; j++) {
[2211]958 mr_strcat(pcline_str, " ");
[128]959 }
[2211]960 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
[128]961 } else {
[2323]962 mr_asprintf(pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
[128]963 }
964 if (g_text_mode) {
[2323]965 mr_asprintf(taskprogress, "TASK: [");
[128]966 for (i = 0; i < percentage; i += 5) {
[2211]967 mr_strcat(taskprogress, "*");
[128]968 }
969 for (; i < 100; i += 5) {
[2211]970 mr_strcat(taskprogress, ".");
[128]971 }
972 if (percentage >= 3) {
[2277]973 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, time_remaining / 60, time_remaining % 60);
974 printf("---evalcall---1--- %s\n", g_isoform_header_str);
[1899]975 printf("---evalcall---2--- %s\n", taskprogress);
976 printf("---evalcall---E---\n");
[128]977 }
[2277]978 mr_free(taskprogress);
[128]979 } else {
[2340]980 newtScaleSet(g_isoform_scale, (unsigned long long) percentage);
[1899]981 if (g_isoform_pcline) {
982 newtLabelSetText(g_isoform_pcline, pcline_str);
983 }
984 if ((percentage >= 3) && (g_isoform_timeline)) {
[128]985 newtLabelSetText(g_isoform_timeline, timeline_str);
986 }
987 }
[2274]988 mr_free(pcline_str);
989 mr_free(timeline_str);
[128]990 }
991 if (!g_text_mode) {
[2937]992 newtDrawForm(g_isoform_main);
[128]993 newtRefresh();
994 }
[1]995 }
996
997
998
999/**
1000 * Update the evalcall form to show @p curr %.
1001 * @param curr The current amount of progress (percentage) in the evalcall form.
1002 */
[2377]1003void update_evalcall_form(int curr) {
1004
[128]1005 update_evalcall_form_ratio(curr, 100);
[2377]1006}
[1]1007
1008
1009
1010/**
1011 * Update the progress form to show @p blurb3 and the current value of
1012 * @p g_maximum_progress.
1013 * @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.
1014 */
[2377]1015void update_progress_form(char *blurb3) {
1016
[128]1017 if (g_current_progress == -999) {
1018 return;
1019 }
1020 strcpy(g_blurb_str_2, blurb3);
[2377]1021 update_progress_form_full(g_blurb_str_1, g_blurb_str_2, g_blurb_str_3);
1022}
[1]1023
1024
1025/**
1026 * Update the progress form's complete blurb and show @p g_current_progress.
1027 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1028 * @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.
1029 * @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.
1030 */
[2377]1031void update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1032
[128]1033 /*@ long ***************************************************** */
[2277]1034 long current_time = 0L;
1035 long time_taken = 0L;
1036 long time_remaining = 0L;
1037 long time_total_est = 0L;
[1]1038
[128]1039 /*@ int ******************************************************* */
1040 int percentage = 0;
1041 int i = 0;
[1]1042
[128]1043 /*@ buffers *************************************************** */
[2274]1044 char *percentline_str = NULL;
1045 char *timeline_str = NULL;
[2211]1046 char *taskprogress = NULL;
[1]1047
[128]1048 if (!g_text_mode) {
1049 assert(blurb1 != NULL);
1050 assert(blurb2 != NULL);
1051 assert(blurb3 != NULL);
1052 assert(g_timeline != NULL);
1053 }
[1]1054
[128]1055 current_time = get_time();
1056 time_taken = current_time - g_start_time;
1057 if (g_maximum_progress == 0) {
1058 percentage = 0;
1059 } else {
1060 if (g_current_progress > g_maximum_progress) {
[2296]1061 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]1062 g_current_progress = g_maximum_progress;
1063 }
[2277]1064 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
[128]1065 }
1066 if (percentage < 1) {
1067 percentage = 1;
1068 }
1069 if (percentage > 100) {
1070 percentage = 100;
1071 }
1072 if (g_current_progress) {
[2277]1073 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
[128]1074 time_remaining = time_total_est - time_taken;
1075 } else {
[2277]1076 time_remaining = 0L;
[128]1077 }
1078 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1079 if (g_text_mode) {
[1899]1080 printf("---progress-form---1--- %s\n", blurb1);
1081 printf("---progress-form---2--- %s\n", blurb2);
1082 printf("---progress-form---3--- %s\n", blurb3);
[541]1083 printf("---progress-form---E---\n");
[2323]1084 mr_asprintf(taskprogress, "TASK: [");
[128]1085 for (i = 0; i < percentage; i += 5) {
[2211]1086 mr_strcat(taskprogress, "*");
[128]1087 }
1088 for (; i < 100; i += 5) {
[2211]1089 mr_strcat(taskprogress, ".");
[128]1090 }
1091 if (percentage > 100) {
[541]1092 log_msg(2, "percentage = %d", percentage);
[128]1093 }
[2277]1094 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
[1899]1095 printf("---progress-form---4--- %s\n", taskprogress);
[2211]1096 paranoid_free(taskprogress);
[128]1097 } else {
[2323]1098 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);
1099 mr_asprintf(percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
[2274]1100
[128]1101 center_string(blurb1, 54);
1102 center_string(blurb2, 54);
1103 center_string(blurb3, 54);
[1899]1104 if (g_blurb1) {
1105 newtLabelSetText(g_blurb1, blurb1);
1106 }
1107 if (g_blurb2) {
1108 newtLabelSetText(g_blurb2, blurb3);
1109 }
1110 if (g_blurb3) {
1111 newtLabelSetText(g_blurb3, blurb2);
1112 }
[128]1113 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
[1899]1114 if ((percentage >= 2) && (g_timeline)) {
[128]1115 newtLabelSetText(g_timeline, timeline_str);
1116 }
[2274]1117 mr_free(timeline_str);
1118
[1899]1119 if (g_percentline) {
1120 newtLabelSetText(g_percentline, percentline_str);
1121 }
[2274]1122 mr_free(percentline_str);
1123
[2937]1124 newtDrawForm(g_progressForm);
[128]1125 newtRefresh();
1126 }
[1]1127 }
1128
1129
1130/**
1131 * Ask the user which backup media type they would like to use.
1132 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
[2382]1133 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c netfs,
[1]1134 * and @c iso.
1135 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1136 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1137 */
[2377]1138t_bkptype which_backup_media_type(bool restoring) {
[1]1139
[128]1140 /*@ char ************************************************************ */
1141 t_bkptype output;
[1]1142
1143
[128]1144 /*@ newt ************************************************************ */
[2334]1145 char *title_sz = NULL;
1146 char *minimsg_sz = NULL;
[2382]1147 static t_bkptype possible_bkptypes[] = { none, cdr, cdrw, dvd, tape, cdstream, udev, netfs, iso };
1148 static char *possible_responses[] = { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev", "netfs", "iso", NULL };
[2357]1149 char *outstr = NULL;
[128]1150 t_bkptype backup_type;
1151 int i;
[1]1152
[128]1153 newtComponent b1;
1154 newtComponent b2;
1155 newtComponent b3;
1156 newtComponent b4;
1157 newtComponent b5;
1158 newtComponent b6;
1159 newtComponent b7;
1160 newtComponent b8;
1161 newtComponent b_res;
1162 newtComponent myForm;
[1]1163
[128]1164 if (g_text_mode) {
1165 for (backup_type = none; backup_type == none;) {
[541]1166 printf("Backup type (");
[128]1167 for (i = 0; possible_responses[i]; i++) {
1168 printf("%c%s", (i == 0) ? '\0' : ' ',
1169 possible_responses[i]);
1170 }
1171 printf(")\n--> ");
[2357]1172 mr_getline(outstr, stdin);
1173 mr_strip_spaces(outstr);
[128]1174 for (i = 0; possible_responses[i]; i++) {
1175 if (!strcmp(possible_responses[i], outstr)) {
1176 backup_type = possible_bkptypes[i];
1177 }
1178 }
[2357]1179 mr_free(outstr);
[128]1180 }
1181 return (backup_type);
1182 }
1183 newtDrawRootText(18, 0, WELCOME_STRING);
1184 if (restoring) {
[2334]1185 mr_asprintf(title_sz, "Please choose the backup media from which you want to read data.");
1186 mr_asprintf(minimsg_sz, "Read from:");
[128]1187 } else {
[2334]1188 mr_asprintf(title_sz, "Please choose the backup media to which you want to archive data.");
1189 mr_asprintf(minimsg_sz, "Backup to:");
[128]1190 }
1191 newtPushHelpLine(title_sz);
1192 newtCenteredWindow(34, 17, minimsg_sz);
[541]1193 b1 = newtButton(1, 1, "CD-R disks ");
1194 b2 = newtButton(17, 1, "CD-RW disks");
1195 b3 = newtButton(1, 9, "Tape drive ");
[1703]1196 b4 = newtButton(17, 5, "USB Key/Disk");
[541]1197 b5 = newtButton(1, 5, " DVD disks ");
[2382]1198 b6 = newtButton(17, 9, " Net mount ");
[541]1199 b7 = newtButton(1, 13, " Hard disk ");
1200 b8 = newtButton(17, 13, " Exit ");
[128]1201 myForm = newtForm(NULL, NULL, 0);
1202 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1203 NULL);
1204 b_res = newtRunForm(myForm);
1205 newtFormDestroy(myForm);
1206 newtPopWindow();
1207 if (b_res == b1) {
1208 output = cdr;
1209 } else if (b_res == b2) {
1210 output = cdrw;
1211 } else if (b_res == b3) {
1212 output = tape;
1213 } else if (b_res == b4) {
[1703]1214 output = usb;
[128]1215 } else if (b_res == b5) {
1216 output = dvd;
1217 } else if (b_res == b6) {
[2382]1218 output = netfs;
[128]1219 } else if (b_res == b7) {
1220 output = iso;
1221 } else {
1222 output = none;
1223 }
1224 newtPopHelpLine();
[2334]1225 mr_free(title_sz);
1226 mr_free(minimsg_sz);
[128]1227 paranoid_free(outstr);
1228 return (output);
1229 }
[1]1230
1231
1232
1233
1234/**
[2816]1235 * Ask the user which compression type they would like to use.
1236 * The choices are "bzip2", "gzip", "lzo". NULL for exit
1237 */
1238 char *which_compression_type() {
1239
1240 /*@ char ************************************************************ */
1241 char *output = NULL;
1242
1243
1244 /*@ newt ************************************************************ */
1245
1246 newtComponent b1;
1247 newtComponent b2;
1248 newtComponent b3;
[2874]1249 newtComponent b4;
[2816]1250 newtComponent b5;
1251 newtComponent b_res;
1252 newtComponent myForm;
1253
1254 newtDrawRootText(18, 0, WELCOME_STRING);
1255 newtPushHelpLine
1256 (" Please specify the type of compression that you want.");
1257 newtCenteredWindow(34, 13, "Type of compression?");
1258 b1 = newtButton(4, 1, "bzip2");
1259 b2 = newtButton(18, 1, "gzip");
1260 b3 = newtButton(4, 5, "lzo");
[2874]1261 b4 = newtButton(18, 5, "lzma");
[2816]1262 b5 = newtButton(4, 9, " Exit ");
1263 myForm = newtForm(NULL, NULL, 0);
1264 newtFormAddComponents(myForm, b1, b3, b2, b5, NULL);
1265 b_res = newtRunForm(myForm);
1266 newtFormDestroy(myForm);
1267 newtPopWindow();
1268 if (b_res == b1) {
1269 mr_asprintf(output, "%s", "bzip2");
1270 } else if (b_res == b2) {
1271 mr_asprintf(output, "%s", "gzip");
1272 } else if (b_res == b3) {
1273 mr_asprintf(output, "%s", "lzo");
1274 } else if (b_res == b4) {
1275 mr_asprintf(output, "%s", "lzma");
1276 }
1277 newtPopHelpLine();
1278 return(output);
1279 }
1280
1281
1282/**
[1]1283 * Ask the user how much compression they would like to use.
1284 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1285 * @return The compression level (0-9) chosen, or -1 for "Exit".
1286 */
[2377]1287int which_compression_level() {
[1]1288
[128]1289 /*@ char ************************************************************ */
1290 int output = none;
[1]1291
1292
[128]1293 /*@ newt ************************************************************ */
[1]1294
[128]1295 newtComponent b1;
1296 newtComponent b2;
1297 newtComponent b3;
1298 newtComponent b4;
1299 newtComponent b5;
1300 newtComponent b_res;
1301 newtComponent myForm;
[1]1302
[128]1303 newtDrawRootText(18, 0, WELCOME_STRING);
[2377]1304 newtPushHelpLine(" Please specify the level of compression that you want.");
[541]1305 newtCenteredWindow(34, 13, "How much compression?");
[3147]1306 b1 = newtButton(4, 1, "Maximum (9)");
1307 b2 = newtButton(18, 1, "Average (4)");
1308 b3 = newtButton(4, 5, "Minimum (1)");
1309 b4 = newtButton(18, 5, " None (0) ");
[541]1310 b5 = newtButton(4, 9, " Exit ");
[128]1311 myForm = newtForm(NULL, NULL, 0);
1312 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1313 b_res = newtRunForm(myForm);
1314 newtFormDestroy(myForm);
1315 newtPopWindow();
1316 if (b_res == b1) {
1317 output = 9;
1318 } else if (b_res == b2) {
1319 output = 4;
1320 } else if (b_res == b3) {
1321 output = 1;
1322 } else if (b_res == b4) {
1323 output = 0;
1324 } else if (b_res == b5) {
1325 output = -1;
1326 }
1327 newtPopHelpLine();
1328 return (output);
1329 }
[1]1330
1331
1332
1333
1334
1335/**
1336 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1337 * @p ARBITRARY_MAXIMUM entries.
1338 * @param filelist The filelist structure to load @p source_file into.
1339 * @param source_file The file containing a list of filenames to load into @p filelist.
1340 */
[2377]1341int load_filelist_into_array(struct s_filelist *filelist, char *source_file) {
1342
[128]1343 int i;
[1662]1344 int j = 0;
[128]1345 bool done;
[2357]1346 char *tmp = NULL;
[1662]1347 char *tmp1 = NULL;
1348 FILE *fin, *fout;
[128]1349 struct s_filelist_entry dummy_fle;
[1]1350
[128]1351 assert(filelist != NULL);
1352 assert_string_is_neither_NULL_nor_zerolength(source_file);
[1]1353
[2230]1354 log_it("entering");
[128]1355 if (!(fin = fopen(source_file, "r"))) {
1356 log_OS_error(source_file);
1357 log_msg(2, "Can't open %s; therefore, cannot popup list",
1358 source_file);
1359 return (1);
1360 }
[1662]1361
[2323]1362 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
[1662]1363 if (!(fout = fopen(tmp1, "a"))) {
1364 log_msg(2, "Can't write to %s", tmp1);
[2357]1365 mr_free(tmp1);
[1662]1366 return(1);
1367 }
1368
[128]1369 log_msg(2, "Loading %s", source_file);
[2377]1370 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM; ) {
[2357]1371 if (tmp != NULL) {
1372 mr_free(tmp);
1373 }
[128]1374 if (feof(fin)) {
1375 break;
1376 }
[2357]1377 mr_getline(tmp, fin);
[128]1378 i = (int) strlen(tmp);
1379 if (i < 2) {
[2377]1380 continue;
[128]1381 }
1382 if (tmp[i - 1] < 32) {
1383 tmp[--i] = '\0';
1384 }
1385 if (i < 2) {
[2377]1386 continue;
[128]1387 }
[1662]1388 if (!does_file_exist(tmp) && !feof(fin)) {
1389 j++;
1390 fprintf(fout, "%s\n", tmp);
[2377]1391 continue;
[128]1392 }
[2357]1393 filelist->el[filelist->entries].severity = severity_of_difference(tmp, NULL);
[128]1394 strcpy(filelist->el[filelist->entries].filename, tmp);
1395 if (feof(fin)) {
1396 break;
1397 }
[2377]1398 filelist->entries++;
[128]1399 }
[2357]1400 mr_free(tmp);
[128]1401 paranoid_fclose(fin);
[1662]1402 paranoid_fclose(fout);
1403 if (j > 0) {
1404 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);
1405 }
[2357]1406 mr_free(tmp1);
[128]1407 if (filelist->entries >= ARBITRARY_MAXIMUM) {
[541]1408 log_to_screen("Arbitrary limits suck, man!");
[128]1409 return (1);
1410 }
1411 for (done = FALSE; !done;) {
1412 done = TRUE;
1413 for (i = 0; i < filelist->entries - 1; i++) {
[1]1414// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
[128]1415 if (filelist->el[i].severity < filelist->el[i + 1].severity
1416 || (filelist->el[i].severity ==
1417 filelist->el[i + 1].severity
1418 && strcmp(filelist->el[i].filename,
1419 filelist->el[i + 1].filename) > 0)) {
1420 memcpy((void *) &dummy_fle,
1421 (void *) &(filelist->el[i]),
1422 sizeof(struct s_filelist_entry));
1423 memcpy((void *) &(filelist->el[i]),
1424 (void *) &(filelist->el[i + 1]),
1425 sizeof(struct s_filelist_entry));
1426 memcpy((void *) &(filelist->el[i + 1]),
1427 (void *) &dummy_fle,
1428 sizeof(struct s_filelist_entry));
1429 log_msg(2, "Swapping %s and %s",
1430 filelist->el[i].filename,
1431 filelist->el[i + 1].filename);
1432 done = FALSE;
1433 }
1434 }
1435 }
[2230]1436 log_it("leaving");
[128]1437 return (0);
1438 }
[1]1439
1440
1441/**
1442 * Generate a pretty string based on @p flentry.
1443 * @param flentry The filelist entry to stringify.
1444 * @return The string form of @p flentry.
1445 * @note The returned value points to static storage that will be overwritten with each call.
1446 */
[2377]1447char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
[2211]1448 char *tmp = NULL;
[1]1449
[2377]1450 log_msg(8, "entering");
[128]1451 assert(flentry != NULL);
1452 if (flentry->severity == 0) {
[2323]1453 mr_asprintf(tmp, "0 ");
[128]1454 } else if (flentry->severity == 1) {
[2323]1455 mr_asprintf(tmp, "low ");
[128]1456 } else if (flentry->severity == 2) {
[2323]1457 mr_asprintf(tmp, "med ");
[128]1458 } else {
[2323]1459 mr_asprintf(tmp, "high");
[128]1460 }
[2377]1461 mr_strcat(tmp, " %s", flentry->filename);
[2211]1462
[2377]1463 log_msg(8, "leaving");
1464 return (tmp);
[128]1465 }
[1]1466
1467
1468/**
1469 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1470 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1471 * @param source_file The file containing a list of changed files.
1472 */
[2377]1473void popup_changelist_from_file(char *source_file) {
1474
[2414]1475 char *reason = NULL;
[128]1476 newtComponent myForm;
1477 newtComponent bClose;
1478 newtComponent bSelect;
1479 newtComponent b_res;
1480 newtComponent fileListbox;
1481 newtComponent headerMsg;
[1]1482
[128]1483 /*@ ???? ************************************************************ */
1484 void *curr_choice;
1485 void *keylist[ARBITRARY_MAXIMUM];
[1]1486
[128]1487 /*@ int ************************************************************* */
1488 int currline = 0;
1489 int finished = FALSE;
[738]1490
1491 /*@ long ************************************************************ */
1492 long i = 0;
[128]1493 long lng = 0;
[1]1494
[128]1495 /*@ buffers ********************************************************* */
[2274]1496 char *tmp = NULL;
1497 char *differ_sz = NULL;
[1]1498
[128]1499 struct s_filelist *filelist;
1500 assert_string_is_neither_NULL_nor_zerolength(source_file);
1501 if (g_text_mode) {
1502 log_msg(2, "Text mode. Therefore, no popup list.");
[2274]1503 return;
[128]1504 }
1505 log_msg(2, "Examining file %s", source_file);
[1]1506
[128]1507 lng = count_lines_in_file(source_file);
1508 if (lng < 1) {
1509 log_msg(2, "No lines in file. Therefore, no popup list.");
[2274]1510 return;
[128]1511 } else if (lng >= ARBITRARY_MAXIMUM) {
1512 log_msg(2, "Too many files differ for me to list.");
[2274]1513 return;
[128]1514 }
[1]1515
[128]1516 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1517 fileListbox =
1518 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1519 newtListboxClear(fileListbox);
[1]1520
[128]1521 if (load_filelist_into_array(filelist, source_file)) {
[2274]1522 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
[128]1523 return;
1524 }
[2377]1525 log_msg(2, "%d files loaded into filelist array", filelist->entries);
[128]1526 for (i = 0; i < filelist->entries; i++) {
1527 keylist[i] = (void *) i;
[2382]1528 tmp = filelist_entry_to_string(&(filelist->el[i]));
1529 newtListboxAppendEntry(fileListbox, tmp, keylist[i]);
[2377]1530 mr_free(tmp);
[128]1531 }
[2323]1532 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
[128]1533 newtPushHelpLine(differ_sz);
[2274]1534 mr_free(differ_sz);
1535
[541]1536 bClose = newtCompactButton(10, 15, " Close ");
1537 bSelect = newtCompactButton(30, 15, " Select ");
[2323]1538 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
[128]1539 headerMsg = newtLabel(2, 1, tmp);
[2274]1540 mr_free(tmp);
1541
[541]1542 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
[128]1543 myForm = newtForm(NULL, NULL, 0);
[2377]1544 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose, bSelect, NULL);
[2274]1545
[128]1546 while (!finished) {
1547 b_res = newtRunForm(myForm);
1548 if (b_res == bClose) {
1549 finished = TRUE;
1550 } else {
1551 curr_choice = newtListboxGetCurrent(fileListbox);
1552 for (i = 0;
1553 i < filelist->entries && keylist[i] != curr_choice;
1554 i++);
1555 if (i == filelist->entries && filelist->entries > 0) {
[541]1556 log_to_screen("I don't know what that button does!");
[128]1557 } else {
1558 currline = i;
1559 if (filelist->entries > 0) {
[2414]1560 severity_of_difference(filelist->el[currline]. filename, &reason);
[2323]1561 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
[2274]1562
[128]1563 popup_and_OK(tmp);
[2274]1564 mr_free(tmp);
[2414]1565 mr_free(reason);
[128]1566 }
1567 }
1568 }
1569 }
[2274]1570
[128]1571 newtFormDestroy(myForm);
1572 newtPopWindow();
1573 newtPopHelpLine();
1574 return;
[1]1575 }
1576
1577/* @} - end of guiGroup */
1578
1579
1580#if __cplusplus
[128]1581} /* extern "C" */
[1]1582#endif
Note: See TracBrowser for help on using the repository browser.