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

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

More cleanup

  • Remove support for cd stream never really tested/useful/... (-C option removed for mondoarchive)
  • Remove dvdrecord specificities as obsolete
  • Remove types cdr and dvd to keep a single optical
  • Remove cdrw_speed param as obsolete now it's automatically detected
  • mondoarchive -c has no parameter anymore
  • Remove some useless global variables
  • Replaces some remaining /mnt/cdrom values by the define MNT_CDROM
  • Property svn:keywords set to Id
File size: 47.6 KB
Line 
1/* newt-specific.c
2 $Id: newt-specific.c 3882 2024-03-09 23:46:53Z 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 3882 2024-03-09 23:46:53Z 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 optical,
1228 * @c tape, @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, optical, tape, udev, netfs, iso };
1243 static char *possible_responses[] = { "none", "optical", "tape", "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 b3;
1251 newtComponent b4;
1252 newtComponent b6;
1253 newtComponent b7;
1254 newtComponent b8;
1255 newtComponent b_res;
1256 newtComponent myForm;
1257
1258 if (g_text_mode) {
1259 for (backup_type = none; backup_type == none;) {
1260 printf("Backup type (");
1261 for (i = 0; possible_responses[i]; i++) {
1262 printf("%c%s", (i == 0) ? '\0' : ' ',
1263 possible_responses[i]);
1264 }
1265 printf(")\n--> ");
1266 mr_getline(instr, stdin);
1267 outstr = mr_strip_spaces(instr);
1268 mr_free(instr);
1269 for (i = 0; possible_responses[i]; i++) {
1270 if (!strcmp(possible_responses[i], outstr)) {
1271 backup_type = possible_bkptypes[i];
1272 }
1273 }
1274 mr_free(outstr);
1275 }
1276 return (backup_type);
1277 }
1278 newtDrawRootText(18, 0, WELCOME_STRING);
1279 if (restoring) {
1280 mr_asprintf(title_sz, "Please choose the backup media from which you want to read data.");
1281 mr_asprintf(minimsg_sz, "Read from:");
1282 } else {
1283 mr_asprintf(title_sz, "Please choose the backup media to which you want to archive data.");
1284 mr_asprintf(minimsg_sz, "Backup to:");
1285 }
1286 newtPushHelpLine(title_sz);
1287 newtCenteredWindow(34, 17, minimsg_sz);
1288 b1 = newtButton(1, 1, "Optical disks ");
1289 b3 = newtButton(1, 9, "Tape drive ");
1290 b4 = newtButton(17, 5, "USB Key/Disk");
1291 b6 = newtButton(17, 9, " Net mount ");
1292 b7 = newtButton(1, 13, " Hard disk ");
1293 b8 = newtButton(17, 13, " Exit ");
1294 myForm = newtForm(NULL, NULL, 0);
1295 newtFormAddComponents(myForm, b1, b3, b7, b4, b6, b8, NULL);
1296 b_res = newtRunForm(myForm);
1297 newtFormDestroy(myForm);
1298 newtPopWindow();
1299 if (b_res == b1) {
1300 output = optical;
1301 } else if (b_res == b3) {
1302 output = tape;
1303 } else if (b_res == b4) {
1304 output = usb;
1305 } else if (b_res == b6) {
1306 output = netfs;
1307 } else if (b_res == b7) {
1308 output = iso;
1309 } else {
1310 output = none;
1311 }
1312 newtPopHelpLine();
1313 mr_free(title_sz);
1314 mr_free(minimsg_sz);
1315 return (output);
1316 }
1317
1318
1319
1320
1321/**
1322 * Ask the user which compression type they would like to use.
1323 * The choices are "bzip2", "gzip", "lzo", "lzma". NULL for exit
1324 */
1325 char *which_compression_type() {
1326
1327 /*@ char ************************************************************ */
1328 char *output = NULL;
1329
1330
1331 /*@ newt ************************************************************ */
1332
1333 newtComponent b1;
1334 newtComponent b2;
1335 newtComponent b3;
1336 newtComponent b4;
1337 newtComponent b5;
1338 newtComponent b_res;
1339 newtComponent myForm;
1340
1341 newtDrawRootText(18, 0, WELCOME_STRING);
1342 newtPushHelpLine
1343 (" Please specify the type of compression that you want.");
1344 newtCenteredWindow(34, 13, "Type of compression?");
1345 b1 = newtButton(4, 1, "bzip2");
1346 b2 = newtButton(18, 1, "gzip");
1347 b3 = newtButton(4, 5, "lzo");
1348 b4 = newtButton(18, 5, "lzma");
1349 b5 = newtButton(4, 9, " Exit ");
1350 myForm = newtForm(NULL, NULL, 0);
1351 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1352 b_res = newtRunForm(myForm);
1353 newtFormDestroy(myForm);
1354 newtPopWindow();
1355 if (b_res == b1) {
1356 mr_asprintf(output, "%s", "bzip2");
1357 } else if (b_res == b2) {
1358 mr_asprintf(output, "%s", "gzip");
1359 } else if (b_res == b3) {
1360 mr_asprintf(output, "%s", "lzo");
1361 } else if (b_res == b4) {
1362 mr_asprintf(output, "%s", "lzma");
1363 }
1364 newtPopHelpLine();
1365 return(output);
1366 }
1367
1368
1369/**
1370 * Ask the user how much compression they would like to use.
1371 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1372 * @return The compression level (0-9) chosen, or -1 for "Exit".
1373 */
1374int which_compression_level() {
1375
1376 /*@ char ************************************************************ */
1377 int output = none;
1378
1379
1380 /*@ newt ************************************************************ */
1381
1382 newtComponent b1;
1383 newtComponent b2;
1384 newtComponent b3;
1385 newtComponent b4;
1386 newtComponent b5;
1387 newtComponent b_res;
1388 newtComponent myForm;
1389
1390 newtDrawRootText(18, 0, WELCOME_STRING);
1391 newtPushHelpLine(" Please specify the level of compression that you want.");
1392 newtCenteredWindow(34, 13, "How much compression?");
1393 b1 = newtButton(4, 1, "Maximum (9)");
1394 b2 = newtButton(18, 1, "Average (4)");
1395 b3 = newtButton(4, 5, "Minimum (1)");
1396 b4 = newtButton(18, 5, " None (0) ");
1397 b5 = newtButton(4, 9, " Exit ");
1398 myForm = newtForm(NULL, NULL, 0);
1399 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1400 b_res = newtRunForm(myForm);
1401 newtFormDestroy(myForm);
1402 newtPopWindow();
1403 if (b_res == b1) {
1404 output = 9;
1405 } else if (b_res == b2) {
1406 output = 4;
1407 } else if (b_res == b3) {
1408 output = 1;
1409 } else if (b_res == b4) {
1410 output = 0;
1411 } else if (b_res == b5) {
1412 output = -1;
1413 }
1414 newtPopHelpLine();
1415 return (output);
1416 }
1417
1418
1419
1420
1421
1422/**
1423 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1424 * @p ARBITRARY_MAXIMUM entries.
1425 * @param filelist The filelist structure to load @p source_file into.
1426 * @param source_file The file containing a list of filenames to load into @p filelist.
1427 */
1428int load_filelist_into_array(struct s_filelist *filelist, char *source_file) {
1429
1430 int i;
1431 int j = 0;
1432 bool done;
1433 char *tmp = NULL;
1434 char *tmp1 = NULL;
1435 FILE *fin, *fout;
1436 struct s_filelist_entry dummy_fle;
1437
1438 assert(filelist != NULL);
1439 assert_string_is_neither_NULL_nor_zerolength(source_file);
1440
1441 log_it("entering");
1442 if (!(fin = fopen(source_file, "r"))) {
1443 log_OS_error(source_file);
1444 log_msg(2, "Can't open %s; therefore, cannot popup list",
1445 source_file);
1446 return (1);
1447 }
1448
1449 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
1450 if (!(fout = fopen(tmp1, "a"))) {
1451 log_msg(2, "Can't write to %s", tmp1);
1452 mr_free(tmp1);
1453 return(1);
1454 }
1455
1456 log_msg(2, "Loading %s", source_file);
1457 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM; ) {
1458 mr_free(tmp);
1459 if (feof(fin)) {
1460 break;
1461 }
1462 mr_getline(tmp, fin);
1463 i = (int) strlen(tmp);
1464 if (i < 2) {
1465 continue;
1466 }
1467 if (tmp[i - 1] < 32) {
1468 tmp[--i] = '\0';
1469 }
1470 if (i < 2) {
1471 continue;
1472 }
1473 if (!does_file_exist(tmp) && !feof(fin)) {
1474 j++;
1475 fprintf(fout, "%s\n", tmp);
1476 continue;
1477 }
1478 filelist->el[filelist->entries].severity = severity_of_difference(tmp, NULL);
1479 strcpy(filelist->el[filelist->entries].filename, tmp);
1480 if (feof(fin)) {
1481 break;
1482 }
1483 filelist->entries++;
1484 }
1485 mr_free(tmp);
1486 paranoid_fclose(fin);
1487 paranoid_fclose(fout);
1488 if (j > 0) {
1489 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);
1490 }
1491 mr_free(tmp1);
1492 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1493 log_to_screen("Arbitrary limits suck, man!");
1494 return (1);
1495 }
1496 for (done = FALSE; !done;) {
1497 done = TRUE;
1498 for (i = 0; i < filelist->entries - 1; i++) {
1499// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
1500 if (filelist->el[i].severity < filelist->el[i + 1].severity
1501 || (filelist->el[i].severity ==
1502 filelist->el[i + 1].severity
1503 && strcmp(filelist->el[i].filename,
1504 filelist->el[i + 1].filename) > 0)) {
1505 memcpy((void *) &dummy_fle,
1506 (void *) &(filelist->el[i]),
1507 sizeof(struct s_filelist_entry));
1508 memcpy((void *) &(filelist->el[i]),
1509 (void *) &(filelist->el[i + 1]),
1510 sizeof(struct s_filelist_entry));
1511 memcpy((void *) &(filelist->el[i + 1]),
1512 (void *) &dummy_fle,
1513 sizeof(struct s_filelist_entry));
1514 log_msg(2, "Swapping %s and %s",
1515 filelist->el[i].filename,
1516 filelist->el[i + 1].filename);
1517 done = FALSE;
1518 }
1519 }
1520 }
1521 log_it("leaving");
1522 return (0);
1523 }
1524
1525
1526/**
1527 * Generate a pretty string based on @p flentry.
1528 * @param flentry The filelist entry to stringify.
1529 * @return The string form of @p flentry.
1530 * @note The returned value points to static storage that will be overwritten with each call.
1531 */
1532char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1533 static char comment[100];
1534 char *tmp = NULL;
1535
1536 log_msg(8, "entering");
1537 assert(flentry != NULL);
1538 if (flentry->severity == 0) {
1539 mr_asprintf(tmp, "0 ");
1540 } else if (flentry->severity == 1) {
1541 mr_asprintf(tmp, "low ");
1542 } else if (flentry->severity == 2) {
1543 mr_asprintf(tmp, "med ");
1544 } else {
1545 mr_asprintf(tmp, "high");
1546 }
1547 mr_strcat(tmp, " ");
1548 mr_strcat(tmp, flentry->filename);
1549 strncpy(comment, tmp, 99);
1550 mr_free(tmp);
1551
1552 log_it("leaving");
1553 return (comment);
1554 }
1555
1556
1557/**
1558 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1559 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1560 * @param source_file The file containing a list of changed files.
1561 */
1562void popup_changelist_from_file(char *source_file) {
1563
1564 char *reason = NULL;
1565 newtComponent myForm;
1566 newtComponent bClose;
1567 newtComponent bSelect;
1568 newtComponent b_res;
1569 newtComponent fileListbox;
1570 newtComponent headerMsg;
1571
1572 /*@ ???? ************************************************************ */
1573 void *curr_choice;
1574 void *keylist[ARBITRARY_MAXIMUM];
1575
1576 /*@ int ************************************************************* */
1577 int currline = 0;
1578 int finished = FALSE;
1579
1580 /*@ long ************************************************************ */
1581 long i = 0;
1582 long lng = 0;
1583
1584 /*@ buffers ********************************************************* */
1585 char *tmp = NULL;
1586 char *differ_sz = NULL;
1587
1588 struct s_filelist *filelist;
1589 assert_string_is_neither_NULL_nor_zerolength(source_file);
1590 if (g_text_mode) {
1591 log_msg(2, "Text mode. Therefore, no popup list.");
1592 return;
1593 }
1594 log_msg(2, "Examining file %s", source_file);
1595
1596 lng = count_lines_in_file(source_file);
1597 if (lng < 1) {
1598 log_msg(2, "No lines in file. Therefore, no popup list.");
1599 return;
1600 } else if (lng >= ARBITRARY_MAXIMUM) {
1601 log_msg(2, "Too many files differ for me to list.");
1602 return;
1603 }
1604
1605 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1606 fileListbox =
1607 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1608 newtListboxClear(fileListbox);
1609
1610 if (load_filelist_into_array(filelist, source_file)) {
1611 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
1612 return;
1613 }
1614 log_msg(2, "%d files loaded into filelist array", filelist->entries);
1615 for (i = 0; i < filelist->entries; i++) {
1616 keylist[i] = (void *) i;
1617 newtListboxAppendEntry(fileListbox,
1618 filelist_entry_to_string(&
1619 (filelist->
1620 el[i])),
1621 keylist[i]);
1622 }
1623 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
1624 newtPushHelpLine(differ_sz);
1625 mr_free(differ_sz);
1626
1627 bClose = newtCompactButton(10, 15, " Close ");
1628 bSelect = newtCompactButton(30, 15, " Select ");
1629 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
1630 headerMsg = newtLabel(2, 1, tmp);
1631 mr_free(tmp);
1632
1633 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1634 myForm = newtForm(NULL, NULL, 0);
1635 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose, bSelect, NULL);
1636
1637 while (!finished) {
1638 b_res = newtRunForm(myForm);
1639 if (b_res == bClose) {
1640 finished = TRUE;
1641 } else {
1642 curr_choice = newtListboxGetCurrent(fileListbox);
1643 for (i = 0;
1644 i < filelist->entries && keylist[i] != curr_choice;
1645 i++);
1646 if (i == filelist->entries && filelist->entries > 0) {
1647 log_to_screen("I don't know what that button does!");
1648 } else {
1649 currline = i;
1650 if (filelist->entries > 0) {
1651 malloc_string(reason);
1652 severity_of_difference(filelist->el[currline].filename, &reason);
1653 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
1654
1655 popup_and_OK(tmp);
1656 mr_free(tmp);
1657 paranoid_free(reason);
1658 }
1659 }
1660 }
1661 }
1662 newtFormDestroy(myForm);
1663 newtPopWindow();
1664 newtPopHelpLine();
1665 return;
1666 }
1667
1668/* @} - end of guiGroup */
1669
1670
1671#ifdef __cplusplus
1672} /* extern "C" */
1673#endif
Note: See TracBrowser for help on using the repository browser.