source: MondoRescue/branches/3.3/mondo/src/common/newt-specific.c@ 3826

Last change on this file since 3826 was 3826, checked in by Bruno Cornec, 4 months ago

manages postnuke_tarball dynamically

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