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

Last change on this file since 3870 was 3869, checked in by Bruno Cornec, 3 months ago

Errors fixed

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