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

Last change on this file was 3892, checked in by Bruno Cornec, 2 months ago

More compiler fixes

  • Fix unused vars
  • Fix FreeBSD #if alone
  • Use MDSTAT_FILE everywhere
  • Fix missing break
  • Fix some strncpy. mr_strncpy used when safe
  • Fix wrong g_isoform_header_str proto !
  • find-cd & find-dvd => find-optical
  • Property svn:keywords set to Id
File size: 47.3 KB
Line 
1/* newt-specific.c
2 $Id: newt-specific.c 3892 2024-03-12 02:10:24Z 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 3892 2024-03-12 02:10:24Z 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 if (g_text_mode) {
605 log_msg(0, ttl);
606 } else {
607 tmp = mr_center_string(ttl, 80);
608 newtPushHelpLine(tmp);
609 mr_free(tmp);
610 }
611 mr_free(g_isoform_header_str);
612 g_isoform_header_str = mr_center_string(ttl, 36);
613 g_isoform_starttime = get_time();
614 if (g_text_mode) {
615 log_msg(0, "g_isoform_header_str = %s", g_isoform_header_str);
616 } else {
617 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
618 g_isoform_scale = newtScale(3, 3, 34, 100);
619 newtCenteredWindow(40, 7, ttl);
620 if (g_isoform_main != NULL) {
621 newtFormDestroy(g_isoform_main);
622 }
623 g_isoform_main = newtForm(NULL, NULL, 0);
624 g_isoform_timeline = newtLabel(1, 5, " ");
625 g_isoform_pcline = newtLabel(1, 6, " ");
626 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
627 g_isoform_pcline, g_isoform_header,
628 g_isoform_scale, NULL);
629 newtDrawForm(g_isoform_main);
630 newtRefresh();
631 }
632 update_evalcall_form(0);
633 }
634
635
636
637/**
638 * Update the progress form's complete blurb and show @p g_current_progress.
639 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
640 * @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.
641 * @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.
642 */
643void update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
644
645 /*@ long ***************************************************** */
646 long current_time = 0L;
647 long time_taken = 0L;
648 long time_remaining = 0L;
649 long time_total_est = 0L;
650
651 char *blurb1_str = NULL;
652 char *blurb2_str = NULL;
653 char *blurb3_str = NULL;
654
655 /*@ int ******************************************************* */
656 int percentage = 0;
657 int i = 0;
658
659 /*@ buffers *************************************************** */
660 char *percentline_str = NULL;
661 char *timeline_str = NULL;
662 char *taskprogress = NULL;
663
664 if (!g_text_mode) {
665 assert(blurb1 != NULL);
666 assert(blurb2 != NULL);
667 assert(blurb3 != NULL);
668 assert(g_timeline != NULL);
669 }
670
671 current_time = get_time();
672 time_taken = current_time - g_start_time;
673 if (g_maximum_progress == 0) {
674 percentage = 0;
675 } else {
676 if (g_current_progress > g_maximum_progress) {
677 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);
678 g_current_progress = g_maximum_progress;
679 }
680 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
681 }
682 if (percentage < 1) {
683 percentage = 1;
684 }
685 if (percentage > 100) {
686 percentage = 100;
687 }
688 if (g_current_progress) {
689 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
690 time_remaining = time_total_est - time_taken;
691 } else {
692 time_remaining = 0L;
693 }
694 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
695 if (g_text_mode) {
696 printf("---progress-form---1--- %s\n", blurb1);
697 printf("---progress-form---2--- %s\n", blurb2);
698 printf("---progress-form---3--- %s\n", blurb3);
699 printf("---progress-form---E---\n");
700 mr_asprintf(taskprogress, "TASK: [");
701 for (i = 0; i < percentage; i += 5) {
702 mr_strcat(taskprogress, "*");
703 }
704 for (; i < 100; i += 5) {
705 mr_strcat(taskprogress, ".");
706 }
707 if (percentage > 100) {
708 log_msg(2, "percentage = %d", percentage);
709 }
710 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
711 printf("---progress-form---4--- %s\n", taskprogress);
712 paranoid_free(taskprogress);
713 } else {
714 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);
715 mr_asprintf(percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
716
717 blurb1_str = mr_center_string(blurb1, 54);
718 blurb2_str = mr_center_string(blurb2, 54);
719 blurb3_str = mr_center_string(blurb3, 54);
720 if (g_blurb1) {
721 newtLabelSetText(g_blurb1, blurb1_str);
722 }
723 if (g_blurb2) {
724 // Not a typo but ridiculous !
725 newtLabelSetText(g_blurb2, blurb3_str);
726 }
727 if (g_blurb3) {
728 // Not a typo but ridiculous !
729 newtLabelSetText(g_blurb3, blurb2_str);
730 }
731 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
732 if ((percentage >= 2) && (g_timeline)) {
733 newtLabelSetText(g_timeline, timeline_str);
734 }
735 mr_free(timeline_str);
736
737 if (g_percentline) {
738 newtLabelSetText(g_percentline, percentline_str);
739 }
740 mr_free(percentline_str);
741 mr_free(blurb1_str);
742 mr_free(blurb2_str);
743 mr_free(blurb3_str);
744
745 newtDrawForm(g_progressForm);
746 newtRefresh();
747 }
748 }
749
750
751/**
752 * Open a progress form with title @p title.
753 * @param title The title to use for the progress form (will be put in the title bar on Newt).
754 * @param b1 The first line of the blurb; generally static.
755 * @param b2 The second line of the blurb; generally static.
756 * @param b3 The third line of the blurb; generally dynamic (it is passed
757 * to update_evalcall_form() every time).
758 * @param max_val The maximum amount of progress (number of filesets, etc.)
759 */
760void open_progress_form(char *title, char *b1, char *b2, char *b3, long max_val) {
761
762 /*@ buffers ********************************************************* */
763 char *b1c = NULL;
764 char *blurb1 = NULL;
765 char *blurb2 = NULL;
766 char *blurb3 = NULL;
767
768 /*@ initialize ****************************************************** */
769 g_mysterious_dot_counter = 0;
770
771 assert(title != NULL);
772 assert(b1 != NULL);
773 assert(b2 != NULL);
774 assert(b3 != NULL);
775
776 malloc_string(blurb1);
777 malloc_string(blurb2);
778 malloc_string(blurb3);
779 strcpy(blurb1, b1);
780 strcpy(blurb2, b2);
781 strcpy(blurb3, b3);
782
783 b1c = mr_center_string(b1c, 80);
784 if (max_val <= 0) {
785 max_val = 1;
786 }
787
788 g_start_time = get_time();
789 g_maximum_progress = max_val;
790 g_current_progress = 0;
791 strcpy(g_blurb_str_1, blurb1);
792 strcpy(g_blurb_str_2, blurb3);
793 strcpy(g_blurb_str_3, blurb2);
794 if (g_text_mode) {
795 log_msg(0, blurb1);
796 log_msg(0, blurb2);
797 log_msg(0, blurb3);
798 } else {
799 g_blurb1 = newtLabel(2, 1, blurb1);
800 g_blurb2 = newtLabel(2, 2, blurb3);
801 g_blurb3 = newtLabel(2, 4, blurb2);
802 newtCenteredWindow(60, 11, title);
803 g_scale = newtScale(3, 6, 54, (long long)g_maximum_progress);
804 g_progressForm = newtForm(NULL, NULL, 0);
805 g_percentline = newtLabel(10, 9, " ");
806 g_timeline = newtLabel(10, 8, " ");
807 newtFormAddComponents(g_progressForm, g_percentline, g_timeline, g_scale, g_blurb1, g_blurb3, g_blurb2, NULL);
808 newtPushHelpLine(b1c);
809 newtDrawForm(g_progressForm);
810 newtRefresh();
811 }
812 update_progress_form_full(blurb1, blurb2, blurb3);
813 mr_free(b1c);
814 paranoid_free(blurb1);
815 paranoid_free(blurb2);
816 paranoid_free(blurb3);
817 }
818
819/**
820 * Give a message to the user in the form of a dialog box (under Newt).
821 * @param prompt The message.
822 */
823void popup_and_OK(const char *prompt) {
824
825 char ch;
826
827 assert_string_is_neither_NULL_nor_zerolength(prompt);
828
829 log_msg(0, prompt);
830 if (g_text_mode) {
831 printf("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ", prompt);
832 while (((ch = getchar()) != '\n') && (ch != EOF));
833 } else {
834 (void) popup_with_buttons(prompt, " OK ", "");
835 }
836 }
837
838/**
839 * Ask the user to enter a value.
840 * @param title The title of the dialog box.
841 * @param b The blurb (e.g. what you want the user to enter).
842 * @param input The string to initialize the user's answer with.
843 * @return value needs to be freed up by caller
844 */
845char *mr_popup_and_get_string(const char *title, const char *b, const char *input) {
846
847 /*@ newt ************************************************************ */
848 newtComponent myForm;
849 newtComponent b_1;
850 newtComponent b_2;
851 newtComponent b_res;
852 newtComponent text;
853 newtComponent type_here;
854
855 char *entry_value = NULL;
856 char *blurb = NULL;
857 char *out = NULL;
858
859 assert_string_is_neither_NULL_nor_zerolength(title);
860 assert(b != NULL);
861
862 if (g_text_mode) {
863 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q---\n--> ", title, b);
864 mr_getline(out, stdin);
865 if (out[strlen(out) - 1] == '\n')
866 out[strlen(out) - 1] = '\0';
867 return(out);
868 }
869
870 assert(input != NULL);
871 text = newtTextboxReflowed(2, 1, b, 48, 5, 5, 0);
872 type_here = newtEntry(2, newtTextboxGetNumLines(text) + 2, input, 50, (void *) &entry_value, NEWT_FLAG_RETURNEXIT);
873 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
874 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
875 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
876 myForm = newtForm(NULL, NULL, 0);
877 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
878 blurb = mr_center_string(b, 80);
879 newtPushHelpLine(blurb);
880 b_res = newtRunForm(myForm);
881
882 if (b_res != b_2) {
883 /* We need to copy the value as newtFormDestroy will clear entry_value */
884 mr_asprintf(out, "%s", entry_value);
885 }
886 newtPopHelpLine();
887 newtFormDestroy(myForm);
888 newtPopWindow();
889 mr_free(blurb);
890
891 return(out);
892}
893
894/**
895 * Ask the user to enter a value.
896 * @param title The title of the dialog box.
897 * @param b The blurb (e.g. what you want the user to enter).
898 * @param output The string to put the user's answer in.
899 * @param maxsize The size in bytes allocated to @p output.
900 * @return TRUE if the user pressed OK, FALSE if they pressed Cancel.
901 */
902 bool popup_and_get_string(char *title, char *b, char *output, int maxsize) {
903
904 /*@ newt ************************************************************ */
905 newtComponent myForm;
906 newtComponent b_1;
907 newtComponent b_2;
908 newtComponent b_res;
909 newtComponent text;
910 newtComponent type_here;
911
912 /*@ pointers ********************************************************* */
913 char *entry_value;
914
915 /*@ buffers ********************************************************** */
916 char *blurb = NULL;
917 char *p;
918 char *original_contents = NULL;
919
920 assert_string_is_neither_NULL_nor_zerolength(title);
921 assert(b != NULL);
922 assert(output != NULL);
923
924 if (g_text_mode) {
925 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q---\n--> ", title, b);
926 p = fgets(output, maxsize, stdin);
927 if (!p) {
928 // FIXME
929 }
930 if (output[strlen(output) - 1] == '\n')
931 output[strlen(output) - 1] = '\0';
932 return (TRUE);
933 }
934
935 text = newtTextboxReflowed(2, 1, b, 48, 5, 5, 0);
936 mr_asprintf(original_contents, "%s", output);
937 output[0] = '\0';
938 type_here = newtEntry(2, newtTextboxGetNumLines(text) + 2, original_contents, 50,
939#ifdef __cplusplus
940 0, NEWT_FLAG_RETURNEXIT
941#else
942 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
943#endif
944 );
945 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
946 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
947 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
948 myForm = newtForm(NULL, NULL, 0);
949 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
950 blurb = mr_center_string(b, 80);
951 newtPushHelpLine(blurb);
952 mr_free(blurb);
953 b_res = newtRunForm(myForm);
954 strcpy(output, entry_value);
955 newtPopHelpLine();
956 newtFormDestroy(myForm);
957 newtPopWindow();
958
959 if (b_res == b_2) {
960 strcpy(output, original_contents);
961 mr_free(original_contents);
962 return (FALSE);
963 } else {
964 mr_free(original_contents);
965 return (TRUE);
966 }
967 }
968
969
970/**
971 * Pop up a dialog box with user-defined buttons.
972 * @param p The text to put in the dialog box.
973 * @param button1 The label on the first button.
974 * @param button2 The label on the second button, or "" if you only want one button.
975 * @return TRUE if @p button1 was pushed, FALSE otherwise.
976 */
977bool popup_with_buttons(char *p, char *button1, char *button2) {
978
979 /*@ buffers *********************************************************** */
980 char *prompt = NULL;
981
982 /*@ newt ************************************************************** */
983 newtComponent myForm;
984 newtComponent b_1;
985 newtComponent b_2;
986 newtComponent b_res;
987 newtComponent text;
988
989 assert_string_is_neither_NULL_nor_zerolength(p);
990 assert(button1 != NULL);
991 assert(button2 != NULL);
992 if (g_text_mode) {
993 if (strlen(button2) == 0) {
994 printf("%s (%s) --> ", p, button1);
995 } else {
996 printf("%s (%s or %s) --> ", p, button1, button2);
997 }
998 mr_getline(prompt, stdin);
999 while (strcmp(prompt, button1) && (strlen(button2) == 0 || strcmp(prompt, button2))) {
1000 printf("--> ");
1001 mr_free(prompt);
1002 mr_getline(prompt, stdin);
1003 }
1004 if (!strcmp(prompt, button1)) {
1005 mr_free(prompt);
1006 return (TRUE);
1007 } else {
1008 mr_free(prompt);
1009 return (FALSE);
1010 }
1011 }
1012
1013 text = newtTextboxReflowed(1, 1, p, 40, 5, 5, 0);
1014 b_1 = newtButton(20 - ((button2[0] != '\0') ? strlen(button1) + 2 : strlen(button1) / 2), newtTextboxGetNumLines(text) + 3, button1);
1015 if (button2[0] != '\0') {
1016 b_2 = newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
1017 } else {
1018 b_2 = NULL;
1019 }
1020 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
1021 myForm = newtForm(NULL, NULL, 0);
1022 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
1023 prompt = mr_center_string(p, 80);
1024 newtPushHelpLine(prompt);
1025 mr_free(prompt);
1026 b_res = newtRunForm(myForm);
1027 newtPopHelpLine();
1028 newtFormDestroy(myForm);
1029 newtPopWindow();
1030 if (b_res == b_1) {
1031 return (TRUE);
1032 } else {
1033 return (FALSE);
1034 }
1035 }
1036
1037
1038
1039
1040/**
1041 * Synchronize the log messages stored in @p err_log_lines with those shown
1042 * on the screen.
1043 */
1044void refresh_log_screen() {
1045
1046 /*@ int *********************************************************** */
1047 int i = 0;
1048
1049
1050 if (g_text_mode || !err_log_lines) {
1051 return;
1052 }
1053 for (i = g_noof_log_lines - 1; i >= 0; i--) {
1054 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
1055 " ");
1056 }
1057 newtRefresh();
1058 for (i = g_noof_log_lines - 1; i >= 0; i--) {
1059 err_log_lines[i][79] = '\0';
1060 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
1061 err_log_lines[i]);
1062 }
1063 newtRefresh();
1064 }
1065
1066
1067/**
1068 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
1069 * only allocate some memory.
1070 */
1071void setup_newt_stuff() {
1072
1073 /*@ int *********************************************************** */
1074 int i = 0;
1075 int cols;
1076
1077 if (!g_text_mode) {
1078 newtInit();
1079 newtCls();
1080 newtPushHelpLine("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
1081 newtDrawRootText(18, 0, WELCOME_STRING);
1082 newtRefresh();
1083 newtGetScreenSize(&cols, &g_noof_rows);
1084 g_noof_log_lines = (g_noof_rows / 5) + 1;
1085 }
1086
1087 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
1088
1089 for (i = 0; i < g_noof_log_lines; i++) {
1090 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
1091 }
1092
1093 for (i = 0; i < g_noof_log_lines; i++) {
1094 err_log_lines[i][0] = '\0';
1095 }
1096 }
1097
1098
1099/**
1100 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
1101 * @param num The numerator of the ratio.
1102 * @param denom The denomenator of the ratio.
1103 */
1104void update_evalcall_form_ratio(int num, int denom) {
1105
1106 /*@ long ************************************************************ */
1107 long current_time = 0L;
1108 long time_taken = 0L;
1109 long time_total_est = 0L;
1110 long time_remaining = 0L;
1111
1112 /*@ buffers ********************************************************** */
1113 char *timeline_str = NULL;
1114 char *pcline_str = NULL;
1115 char *taskprogress = NULL;
1116
1117 /*@ int ************************************************************** */
1118 int percentage = 0;
1119 int i = 0;
1120 int j = 0;
1121
1122 if (num * 100 < denom) {
1123 percentage = 1;
1124 } else {
1125 percentage = (num * 100 + denom / 2) / denom;
1126 }
1127
1128 current_time = get_time();
1129 time_taken = current_time - g_isoform_starttime;
1130 if (num) {
1131 time_total_est = time_taken * denom / num;
1132 time_remaining = time_total_est - time_taken;
1133 } else {
1134 time_remaining = 0L;
1135 }
1136 if (!g_text_mode) {
1137 if (g_isoform_header) {
1138 newtLabelSetText(g_isoform_header, g_isoform_header_str);
1139 }
1140 }
1141 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1142 if ((percentage < 3 && g_isoform_old_progress < 3) || percentage > g_isoform_old_progress) {
1143 g_isoform_old_progress = percentage;
1144 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);
1145 if (percentage < 3) {
1146 mr_asprintf(pcline_str, " Working");
1147 for (j = 0; j < g_mysterious_dot_counter; j++) {
1148 mr_strcat(pcline_str, ".");
1149 }
1150 for (; j < 27; j++) {
1151 mr_strcat(pcline_str, " ");
1152 }
1153 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
1154 } else {
1155 mr_asprintf(pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
1156 }
1157 if (g_text_mode) {
1158 mr_asprintf(taskprogress, "TASK: [");
1159 for (i = 0; i < percentage; i += 5) {
1160 mr_strcat(taskprogress, "*");
1161 }
1162 for (; i < 100; i += 5) {
1163 mr_strcat(taskprogress, ".");
1164 }
1165 if (percentage >= 3) {
1166 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
1167 printf("---evalcall---1--- %s\n", g_isoform_header_str);
1168 printf("---evalcall---2--- %s\n", taskprogress);
1169 printf("---evalcall---E---\n");
1170 }
1171 mr_free(taskprogress);
1172 } else {
1173 newtScaleSet(g_isoform_scale, (unsigned long long) percentage);
1174 if (g_isoform_pcline) {
1175 newtLabelSetText(g_isoform_pcline, pcline_str);
1176 }
1177 if ((percentage >= 3) && (g_isoform_timeline)) {
1178 newtLabelSetText(g_isoform_timeline, timeline_str);
1179 }
1180 }
1181 mr_free(pcline_str);
1182 mr_free(timeline_str);
1183 }
1184 if (!g_text_mode) {
1185 newtDrawForm(g_isoform_main);
1186 newtRefresh();
1187 }
1188 }
1189
1190
1191
1192/**
1193 * Update the evalcall form to show @p curr %.
1194 * @param curr The current amount of progress (percentage) in the evalcall form.
1195 */
1196void update_evalcall_form(int curr) {
1197
1198 update_evalcall_form_ratio(curr, 100);
1199}
1200
1201
1202
1203/**
1204 * Update the progress form to show @p blurb3 and the current value of
1205 * @p g_maximum_progress.
1206 * @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.
1207 */
1208void update_progress_form(char *blurb3) {
1209
1210 if (g_current_progress == -999) {
1211 return;
1212 }
1213 strcpy(g_blurb_str_2, blurb3);
1214 update_progress_form_full(g_blurb_str_1, g_blurb_str_2, g_blurb_str_3);
1215}
1216
1217
1218/**
1219 * Ask the user which backup media type they would like to use.
1220 * The choices are @p none (exit to shell), @c optical,
1221 * @c tape, @c udev (only when @p g_text_mode is TRUE), @c netfs,
1222 * and @c iso.
1223 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1224 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1225 */
1226t_bkptype which_backup_media_type(bool restoring) {
1227
1228 /*@ char ************************************************************ */
1229 t_bkptype output;
1230
1231
1232 /*@ newt ************************************************************ */
1233 char *title_sz = NULL;
1234 char *minimsg_sz = NULL;
1235 static t_bkptype possible_bkptypes[] = { none, optical, tape, udev, netfs, iso };
1236 static char *possible_responses[] = { "none", "optical", "tape", "udev", "netfs", "iso", NULL };
1237 char *outstr = NULL;
1238 char *instr = NULL;
1239 t_bkptype backup_type;
1240 int i;
1241
1242 newtComponent b1;
1243 newtComponent b3;
1244 newtComponent b4;
1245 newtComponent b6;
1246 newtComponent b7;
1247 newtComponent b8;
1248 newtComponent b_res;
1249 newtComponent myForm;
1250
1251 if (g_text_mode) {
1252 for (backup_type = none; backup_type == none;) {
1253 printf("Backup type (");
1254 for (i = 0; possible_responses[i]; i++) {
1255 printf("%c%s", (i == 0) ? '\0' : ' ',
1256 possible_responses[i]);
1257 }
1258 printf(")\n--> ");
1259 mr_getline(instr, stdin);
1260 outstr = mr_strip_spaces(instr);
1261 mr_free(instr);
1262 for (i = 0; possible_responses[i]; i++) {
1263 if (!strcmp(possible_responses[i], outstr)) {
1264 backup_type = possible_bkptypes[i];
1265 }
1266 }
1267 mr_free(outstr);
1268 }
1269 return (backup_type);
1270 }
1271 newtDrawRootText(18, 0, WELCOME_STRING);
1272 if (restoring) {
1273 mr_asprintf(title_sz, "Please choose the backup media from which you want to read data.");
1274 mr_asprintf(minimsg_sz, "Read from:");
1275 } else {
1276 mr_asprintf(title_sz, "Please choose the backup media to which you want to archive data.");
1277 mr_asprintf(minimsg_sz, "Backup to:");
1278 }
1279 newtPushHelpLine(title_sz);
1280 newtCenteredWindow(34, 17, minimsg_sz);
1281 b1 = newtButton(1, 1, "Optical disks ");
1282 b3 = newtButton(1, 9, "Tape drive ");
1283 b4 = newtButton(17, 5, "USB Key/Disk");
1284 b6 = newtButton(17, 9, " Net mount ");
1285 b7 = newtButton(1, 13, " Hard disk ");
1286 b8 = newtButton(17, 13, " Exit ");
1287 myForm = newtForm(NULL, NULL, 0);
1288 newtFormAddComponents(myForm, b1, b3, b7, b4, b6, b8, NULL);
1289 b_res = newtRunForm(myForm);
1290 newtFormDestroy(myForm);
1291 newtPopWindow();
1292 if (b_res == b1) {
1293 output = optical;
1294 } else if (b_res == b3) {
1295 output = tape;
1296 } else if (b_res == b4) {
1297 output = usb;
1298 } else if (b_res == b6) {
1299 output = netfs;
1300 } else if (b_res == b7) {
1301 output = iso;
1302 } else {
1303 output = none;
1304 }
1305 newtPopHelpLine();
1306 mr_free(title_sz);
1307 mr_free(minimsg_sz);
1308 return (output);
1309 }
1310
1311
1312
1313
1314/**
1315 * Ask the user which compression type they would like to use.
1316 * The choices are "bzip2", "gzip", "lzo", "lzma". NULL for exit
1317 */
1318 char *which_compression_type() {
1319
1320 /*@ char ************************************************************ */
1321 char *output = NULL;
1322
1323
1324 /*@ newt ************************************************************ */
1325
1326 newtComponent b1;
1327 newtComponent b2;
1328 newtComponent b3;
1329 newtComponent b4;
1330 newtComponent b5;
1331 newtComponent b_res;
1332 newtComponent myForm;
1333
1334 newtDrawRootText(18, 0, WELCOME_STRING);
1335 newtPushHelpLine
1336 (" Please specify the type of compression that you want.");
1337 newtCenteredWindow(34, 13, "Type of compression?");
1338 b1 = newtButton(4, 1, "bzip2");
1339 b2 = newtButton(18, 1, "gzip");
1340 b3 = newtButton(4, 5, "lzo");
1341 b4 = newtButton(18, 5, "lzma");
1342 b5 = newtButton(4, 9, " Exit ");
1343 myForm = newtForm(NULL, NULL, 0);
1344 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1345 b_res = newtRunForm(myForm);
1346 newtFormDestroy(myForm);
1347 newtPopWindow();
1348 if (b_res == b1) {
1349 mr_asprintf(output, "%s", "bzip2");
1350 } else if (b_res == b2) {
1351 mr_asprintf(output, "%s", "gzip");
1352 } else if (b_res == b3) {
1353 mr_asprintf(output, "%s", "lzo");
1354 } else if (b_res == b4) {
1355 mr_asprintf(output, "%s", "lzma");
1356 }
1357 newtPopHelpLine();
1358 return(output);
1359 }
1360
1361
1362/**
1363 * Ask the user how much compression they would like to use.
1364 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1365 * @return The compression level (0-9) chosen, or -1 for "Exit".
1366 */
1367int which_compression_level() {
1368
1369 /*@ char ************************************************************ */
1370 int output = none;
1371
1372
1373 /*@ newt ************************************************************ */
1374
1375 newtComponent b1;
1376 newtComponent b2;
1377 newtComponent b3;
1378 newtComponent b4;
1379 newtComponent b5;
1380 newtComponent b_res;
1381 newtComponent myForm;
1382
1383 newtDrawRootText(18, 0, WELCOME_STRING);
1384 newtPushHelpLine(" Please specify the level of compression that you want.");
1385 newtCenteredWindow(34, 13, "How much compression?");
1386 b1 = newtButton(4, 1, "Maximum (9)");
1387 b2 = newtButton(18, 1, "Average (4)");
1388 b3 = newtButton(4, 5, "Minimum (1)");
1389 b4 = newtButton(18, 5, " None (0) ");
1390 b5 = newtButton(4, 9, " Exit ");
1391 myForm = newtForm(NULL, NULL, 0);
1392 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1393 b_res = newtRunForm(myForm);
1394 newtFormDestroy(myForm);
1395 newtPopWindow();
1396 if (b_res == b1) {
1397 output = 9;
1398 } else if (b_res == b2) {
1399 output = 4;
1400 } else if (b_res == b3) {
1401 output = 1;
1402 } else if (b_res == b4) {
1403 output = 0;
1404 } else if (b_res == b5) {
1405 output = -1;
1406 }
1407 newtPopHelpLine();
1408 return (output);
1409 }
1410
1411
1412
1413
1414
1415/**
1416 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1417 * @p ARBITRARY_MAXIMUM entries.
1418 * @param filelist The filelist structure to load @p source_file into.
1419 * @param source_file The file containing a list of filenames to load into @p filelist.
1420 */
1421int load_filelist_into_array(struct s_filelist *filelist, char *source_file) {
1422
1423 int i;
1424 int j = 0;
1425 bool done;
1426 char *tmp = NULL;
1427 char *tmp1 = NULL;
1428 FILE *fin, *fout;
1429 struct s_filelist_entry dummy_fle;
1430
1431 assert(filelist != NULL);
1432 assert_string_is_neither_NULL_nor_zerolength(source_file);
1433
1434 log_it("entering");
1435 if (!(fin = fopen(source_file, "r"))) {
1436 log_OS_error(source_file);
1437 log_msg(2, "Can't open %s; therefore, cannot popup list",
1438 source_file);
1439 return (1);
1440 }
1441
1442 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
1443 if (!(fout = fopen(tmp1, "a"))) {
1444 log_msg(2, "Can't write to %s", tmp1);
1445 mr_free(tmp1);
1446 return(1);
1447 }
1448
1449 log_msg(2, "Loading %s", source_file);
1450 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM; ) {
1451 mr_free(tmp);
1452 if (feof(fin)) {
1453 break;
1454 }
1455 mr_getline(tmp, fin);
1456 i = (int) strlen(tmp);
1457 if (i < 2) {
1458 continue;
1459 }
1460 if (tmp[i - 1] < 32) {
1461 tmp[--i] = '\0';
1462 }
1463 if (i < 2) {
1464 continue;
1465 }
1466 if (!does_file_exist(tmp) && !feof(fin)) {
1467 j++;
1468 fprintf(fout, "%s\n", tmp);
1469 continue;
1470 }
1471 filelist->el[filelist->entries].severity = severity_of_difference(tmp, NULL);
1472 strcpy(filelist->el[filelist->entries].filename, tmp);
1473 if (feof(fin)) {
1474 break;
1475 }
1476 filelist->entries++;
1477 }
1478 mr_free(tmp);
1479 paranoid_fclose(fin);
1480 paranoid_fclose(fout);
1481 if (j > 0) {
1482 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);
1483 }
1484 mr_free(tmp1);
1485 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1486 log_to_screen("Arbitrary limits suck, man!");
1487 return (1);
1488 }
1489 for (done = FALSE; !done;) {
1490 done = TRUE;
1491 for (i = 0; i < filelist->entries - 1; i++) {
1492// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
1493 if (filelist->el[i].severity < filelist->el[i + 1].severity
1494 || (filelist->el[i].severity ==
1495 filelist->el[i + 1].severity
1496 && strcmp(filelist->el[i].filename,
1497 filelist->el[i + 1].filename) > 0)) {
1498 memcpy((void *) &dummy_fle,
1499 (void *) &(filelist->el[i]),
1500 sizeof(struct s_filelist_entry));
1501 memcpy((void *) &(filelist->el[i]),
1502 (void *) &(filelist->el[i + 1]),
1503 sizeof(struct s_filelist_entry));
1504 memcpy((void *) &(filelist->el[i + 1]),
1505 (void *) &dummy_fle,
1506 sizeof(struct s_filelist_entry));
1507 log_msg(2, "Swapping %s and %s",
1508 filelist->el[i].filename,
1509 filelist->el[i + 1].filename);
1510 done = FALSE;
1511 }
1512 }
1513 }
1514 log_it("leaving");
1515 return (0);
1516 }
1517
1518
1519/**
1520 * Generate a pretty string based on @p flentry.
1521 * @param flentry The filelist entry to stringify.
1522 * @return The string form of @p flentry.
1523 * @note The returned value points to a dynamically allocated string that the caller needs to free.
1524 */
1525char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1526 char *comment = NULL;
1527
1528 log_msg(8, "entering");
1529 assert(flentry != NULL);
1530 if (flentry->severity == 0) {
1531 mr_asprintf(comment, "0 ");
1532 } else if (flentry->severity == 1) {
1533 mr_asprintf(comment, "low ");
1534 } else if (flentry->severity == 2) {
1535 mr_asprintf(comment, "med ");
1536 } else {
1537 mr_asprintf(comment, "high");
1538 }
1539 mr_strcat(comment, " ");
1540 mr_strcat(comment, flentry->filename);
1541 log_it("leaving");
1542 return (comment);
1543 }
1544
1545
1546/**
1547 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1548 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1549 * @param source_file The file containing a list of changed files.
1550 */
1551void popup_changelist_from_file(char *source_file) {
1552
1553 char *reason = NULL;
1554 newtComponent myForm;
1555 newtComponent bClose;
1556 newtComponent bSelect;
1557 newtComponent b_res;
1558 newtComponent fileListbox;
1559 newtComponent headerMsg;
1560
1561 /*@ ???? ************************************************************ */
1562 void *curr_choice;
1563 void *keylist[ARBITRARY_MAXIMUM];
1564
1565 /*@ int ************************************************************* */
1566 int currline = 0;
1567 int finished = FALSE;
1568
1569 /*@ long ************************************************************ */
1570 long i = 0;
1571 long lng = 0;
1572
1573 /*@ buffers ********************************************************* */
1574 char *tmp = NULL;
1575 char *differ_sz = NULL;
1576
1577 struct s_filelist *filelist;
1578 assert_string_is_neither_NULL_nor_zerolength(source_file);
1579 if (g_text_mode) {
1580 log_msg(2, "Text mode. Therefore, no popup list.");
1581 return;
1582 }
1583 log_msg(2, "Examining file %s", source_file);
1584
1585 lng = count_lines_in_file(source_file);
1586 if (lng < 1) {
1587 log_msg(2, "No lines in file. Therefore, no popup list.");
1588 return;
1589 } else if (lng >= ARBITRARY_MAXIMUM) {
1590 log_msg(2, "Too many files differ for me to list.");
1591 return;
1592 }
1593
1594 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1595 fileListbox =
1596 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1597 newtListboxClear(fileListbox);
1598
1599 if (load_filelist_into_array(filelist, source_file)) {
1600 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
1601 return;
1602 }
1603 log_msg(2, "%d files loaded into filelist array", filelist->entries);
1604 for (i = 0; i < filelist->entries; i++) {
1605 keylist[i] = (void *) i;
1606 tmp = filelist_entry_to_string(&(filelist->el[i]));
1607 newtListboxAppendEntry(fileListbox,
1608 tmp,
1609 keylist[i]);
1610 mr_free(tmp);
1611 }
1612 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
1613 newtPushHelpLine(differ_sz);
1614 mr_free(differ_sz);
1615
1616 bClose = newtCompactButton(10, 15, " Close ");
1617 bSelect = newtCompactButton(30, 15, " Select ");
1618 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
1619 headerMsg = newtLabel(2, 1, tmp);
1620 mr_free(tmp);
1621
1622 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1623 myForm = newtForm(NULL, NULL, 0);
1624 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose, bSelect, NULL);
1625
1626 while (!finished) {
1627 b_res = newtRunForm(myForm);
1628 if (b_res == bClose) {
1629 finished = TRUE;
1630 } else {
1631 curr_choice = newtListboxGetCurrent(fileListbox);
1632 for (i = 0;
1633 i < filelist->entries && keylist[i] != curr_choice;
1634 i++);
1635 if (i == filelist->entries && filelist->entries > 0) {
1636 log_to_screen("I don't know what that button does!");
1637 } else {
1638 currline = i;
1639 if (filelist->entries > 0) {
1640 malloc_string(reason);
1641 severity_of_difference(filelist->el[currline].filename, &reason);
1642 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
1643
1644 popup_and_OK(tmp);
1645 mr_free(tmp);
1646 paranoid_free(reason);
1647 }
1648 }
1649 }
1650 }
1651 newtFormDestroy(myForm);
1652 newtPopWindow();
1653 newtPopHelpLine();
1654 return;
1655 }
1656
1657/* @} - end of guiGroup */
1658
1659
1660#ifdef __cplusplus
1661} /* extern "C" */
1662#endif
Note: See TracBrowser for help on using the repository browser.