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

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

remove differentiated support for cdrw

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