source: MondoRescue/branches/3.2/mondo/src/common/newt-specific.c@ 3235

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