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

Last change on this file was 3892, checked in by Bruno Cornec, 6 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
RevLine 
[1]1/* newt-specific.c
[99]2 $Id: newt-specific.c 3892 2024-03-12 02:10:24Z bruno $
[1]3
4 subroutines which do display-type things
5 and use the newt library to do them
6*/
7
8
9/**
10 * @file
11 * Functions for doing display-type things with the Newt library.
12 */
13
14#define MAX_NEWT_COMMENT_LEN 200
15
[3846]16#ifdef __cplusplus
[1]17extern "C" {
18#endif
19
20#include "my-stuff.h"
[2211]21#include "mr_mem.h"
[3194]22#include "mr_str.h"
[1]23#include "mondostructures.h"
24#include "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"
[541]29#include "libmondo-gui-EXT.h"
[1]30
[3871]31
[1316]32extern char *MONDO_LOGFILE;
33
[1665]34extern struct s_bkpinfo *bkpinfo;
35
[1]36/*@unused@*/
[99]37//static char cvsid[] = "$Id: newt-specific.c 3892 2024-03-12 02:10:24Z bruno $";
[1]38
[128]39 extern pid_t g_mastermind_pid;
[2030]40 /*
[128]41 extern char *g_tmpfs_mountpt;
[2030]42 */
[128]43 extern char *g_boot_mountpt;
[792]44 extern char *ps_options;
[928]45 extern char *ps_proc_id;
[1]46
[128]47 extern void set_signals(int);
[1]48
49/**
50 * @addtogroup globalGroup
51 * @{
52 */
53/**
54 * Whether we are currently in a nested call of fatal_error().
55 */
[128]56 bool g_exiting = FALSE;
[1]57
[3141]58// Decide whether we should continue to ask questions or exit (cron use case)
59extern bool g_fail_immediately;
60
[128]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 ?????
[1]69
[128]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).
[3892]81 char *g_isoform_header_str = NULL; ///< The string for the evalcall form title.
[128]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.
[1]85
[128]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.
[3191]89 long g_maximum_progress = 999L; ///< The maximum amount of progress (100%) for the currently opened progress form.
[128]90 long g_current_progress = -999; ///< The current amount of progress (filelist #, etc.) for the currently opened progress form.
[3191]91 long g_start_time = 0L; ///< The time (in seconds since the epoch) that the progress form was opened.
[128]92 bool g_text_mode = TRUE; ///< If FALSE, use a newt interface; if TRUE, use an ugly (but more compatible) dumb terminal interface.
[2380]93 char *g_selfmounted_isodir; ///< Holds the NETFS mountpoint if mounted via mondoarchive.
[1]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 */
[128]106 bool ask_me_yes_or_no(char *prompt) {
[1]107
[128]108 /*@ buffers ********************************************************** */
[3191]109 int i = 0;
110 char *tmp = NULL;
[1]111
[3141]112 assert_string_is_neither_NULL_nor_zerolength(prompt);
[1]113
[3141]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 }
[128]119 if (g_text_mode) {
120 while (1) {
[3191]121 sync();
122 printf("---promptdialogYN---1--- %s\n---promptdialogYN---Q--- [yes] [no] ---\n--> ", prompt);
123 mr_getline(tmp, stdin);
[128]124 if (tmp[strlen(tmp) - 1] == '\n')
125 tmp[strlen(tmp) - 1] = '\0';
[1]126
[128]127 i = (int) strlen(tmp);
128 if (i > 0 && tmp[i - 1] < 32) {
129 tmp[i - 1] = '\0';
130 }
[541]131 if (strstr("yesYES", tmp)) {
[3191]132 mr_free(tmp);
[128]133 return (TRUE);
[541]134 } else if (strstr("NOno", tmp)) {
[3191]135 mr_free(tmp);
[128]136 return (FALSE);
137 } else {
[3191]138 sync();
139 printf("Please enter either YES or NO (or yes or no, or y or n, or...)\n");
[128]140 }
141 }
142 } else {
[541]143 return (popup_with_buttons(prompt, "Yes", "No"));
[128]144 }
145 }
[1]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 */
[128]154 bool ask_me_OK_or_cancel(char *prompt) {
[1]155
[128]156 /*@ buffer *********************************************************** */
[3191]157 char *tmp = NULL;
[128]158 int i;
[1]159
[3141]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 }
[3191]166
[128]167 if (g_text_mode) {
[3191]168 sync();
169 printf("---promptdialogOKC---1--- %s\n---promptdialogOKC---Q--- [OK] [Cancel] ---\n--> ", prompt);
170 mr_getline(tmp, stdin);
[128]171 if (tmp[strlen(tmp) - 1] == '\n')
172 tmp[strlen(tmp) - 1] = '\0';
[1]173
[128]174 i = (int) strlen(tmp);
175 if (i > 0 && tmp[i - 1] < 32) {
176 tmp[i - 1] = '\0';
177 }
[541]178 if (strstr("okOKOkYESyes", tmp)) {
[3191]179 mr_free(tmp);
[128]180 return (TRUE);
181 } else {
[3191]182 mr_free(tmp);
[128]183 return (FALSE);
184 }
185 } else {
[541]186 return (popup_with_buttons(prompt, " Okay ", "Cancel"));
[128]187 }
[1]188 }
189
190
191
192/**
193 * Close the currently opened evalcall form.
194 */
[128]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();
[2290]212
213 /* Reset globals */
[128]214 g_isoform_main = NULL;
215 g_isoform_old_progress = -1;
[2290]216 g_isoform_header = NULL;
217 g_isoform_scale = NULL;
218 g_isoform_timeline = NULL;
219 g_isoform_pcline = NULL;
220}
[1]221
222/**
223 * Close the currently opened progress form.
224 */
[128]225 void
226 close_progress_form() {
227 if (g_text_mode) {
228 return;
229 }
230 if (g_current_progress == -999) {
[3191]231 log_msg(2, "Trying to close the progress form when it ain't open!");
[128]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();
[2290]244
245 /* Reset globals */
[128]246 g_progressForm = NULL;
247 g_current_progress = -999;
[2290]248 g_timeline = NULL;
249 g_percentline = NULL;
250 g_scale = NULL;
251
[128]252 }
[1]253
[792]254/**
255 * Kill any process containing the string @p str surrounded by spaces in its commandline.
256 */
[3854]257void kill_anything_like_this(const char *str) {
[1]258
[792]259char *tmp = NULL;
[1850]260char *tmp1 = NULL;
[1]261
[3185]262 mr_asprintf(tmp,"ps %s | grep \" %s \" | grep -v \"grep\" | awk '{print %s;}'", ps_options, str , ps_proc_id);
[1294]263 run_program_and_log_output(tmp, TRUE);
[1850]264 if (strlen(tmp) > 0) {
[3185]265 mr_asprintf(tmp1,"kill `%s`", tmp);
[1850]266 run_program_and_log_output(tmp1, TRUE);
[3191]267 mr_free(tmp1);
[1850]268 }
[3191]269 mr_free(tmp);
[792]270}
[1]271
[792]272
[1]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 */
[3846]278void fatal_error(const char *error_string) {
[3191]279
[128]280 /*@ buffers ***************************************************** */
[3191]281 char *command = NULL;
[128]282 static bool already_exiting = FALSE;
[1]283
[128]284 /*@ end vars **************************************************** */
[1]285
[128]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 }
[1]295
[128]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 }
[1]303
[128]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 }
[1]309
[128]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");
[3154]314 kill_anything_like_this("mondo.tmp");
[296]315 kill_anything_like_this("ntfsclone");
[128]316 sync();
[1]317
[3060]318 if (chdir("/")) {
319 // FIXME
320 }
[128]321 if (g_selfmounted_isodir) {
[3191]322 mr_asprintf(command, "umount -d %s", g_selfmounted_isodir);
[128]323 run_program_and_log_output(command, 5);
[3191]324 mr_free(command);
325 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
[128]326 run_program_and_log_output(command, 5);
[3191]327 mr_free(command);
[128]328 }
[99]329
[128]330 if (!g_text_mode) {
[3191]331 log_msg(0, "-------FATAL ERROR---------");
[128]332 log_msg(0, error_string);
333 }
[1]334
[128]335 printf("---FATALERROR--- %s\n", error_string);
[3191]336 printf("If you require technical support, please contact the mailing list.\n");
[541]337 printf("See http://www.mondorescue.org for details.\n");
[3191]338 printf("The list's members can help you, if you attach that file to your e-mail.\n");
[541]339 printf("Log file: %s\n", MONDO_LOGFILE);
340 printf("Mondo has aborted.\n");
[128]341 if (!g_main_pid) {
342 log_msg(3, "FYI - g_main_pid is blank");
343 }
344 finish(254);
345 }
[1]346
[2241]347/* Free memory allocated for newt */
348void free_newt_stuff() {
[1]349
[2241]350int i;
[1]351
[3508]352if (err_log_lines == NULL) {
353 return;
354}
355
[2241]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
[1]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 */
[3191]370void finish(int signal) {
371
[1708]372 char *command = NULL;
[1]373
[2910]374 /* Before removing dir, make sure we're out of them */
[3060]375 if (chdir("/tmp")) {
376 // FIXME
377 }
[2878]378 run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
[128]379 if (g_selfmounted_isodir) {
[3185]380 mr_asprintf(command, "umount -d %s", g_selfmounted_isodir);
[128]381 run_program_and_log_output(command, 1);
[3191]382 mr_free(command);
383
[3185]384 mr_asprintf(command, "rmdir %s", g_selfmounted_isodir);
[128]385 run_program_and_log_output(command, 1);
[3191]386 mr_free(command);
[128]387 }
[3833]388 if (bkpinfo->tmpdir != NULL) {
[2241]389 log_msg(8,"tempdir is %s",bkpinfo->tmpdir);
390 if (strstr(bkpinfo->tmpdir ,"mondo.tmp.") != NULL) {
[2907]391 log_msg(8,"erasing tempdir %s",bkpinfo->tmpdir);
[3185]392 mr_asprintf(command, "rm -Rf %s", bkpinfo->tmpdir);
[3060]393 paranoid_system(command);
[2697]394 mr_free(command);
[2241]395 }
[1999]396 }
[3834]397 if (bkpinfo->scratchdir != NULL) {
[2241]398 log_msg(8,"scratchdir is %s", bkpinfo->scratchdir);
399 if (strstr(bkpinfo->scratchdir ,"mondo.scratch.") != NULL) {
[2907]400 log_msg(8,"erasing scratchdir %s",bkpinfo->scratchdir);
[3185]401 mr_asprintf(command, "rm -Rf %s", bkpinfo->scratchdir);
[3060]402 paranoid_system(command);
[2697]403 mr_free(command);
[2241]404 }
405 }
406 /* Free all allocated strings in bkpinfo */
[2380]407 mr_free(bkpinfo->netfs_user);
408 mr_free(bkpinfo->netfs_proto);
[3208]409 mr_free(bkpinfo->netfs_remote_dir);
[2424]410 mr_free(bkpinfo->exclude_devs);
[2697]411 mr_free(bkpinfo->exclude_paths);
[3292]412 mr_free(bkpinfo->include_paths);
[2769]413 mr_free(bkpinfo->subdir);
[3821]414 mr_free(bkpinfo->image_devs);
[3822]415 mr_free(bkpinfo->media_device);
[3826]416 mr_free(bkpinfo->postnuke_tarball);
[3827]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);
[3828]421 mr_free(bkpinfo->restore_path);
[3829]422 mr_free(bkpinfo->kernel_path);
[3830]423 mr_free(bkpinfo->zip_exe);
424 mr_free(bkpinfo->zip_suffix);
[3833]425 mr_free(bkpinfo->tmpdir);
[3834]426 mr_free(bkpinfo->scratchdir);
[3835]427 mr_free(bkpinfo->prefix);
[3836]428 mr_free(bkpinfo->isodir);
[3856]429 mr_free(bkpinfo->serial_string);
[2241]430 /* Then free the structure */
[1703]431 paranoid_free(bkpinfo);
[3191]432
[3858]433 /* free global vars */
434 mr_free(g_isoform_header_str);
435
[128]436 free_libmondo_global_strings();
[2241]437 free_newt_stuff();
[3191]438 if (!g_text_mode) {
439 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
440 log_msg(1, "Calling newtFinished()");
441 newtFinished();
442 } else {
443 log_msg(1, "Calling newtSuspend()");
444 newtSuspend();
445 }
446 }
447 printf("Execution run ended; result=%d\n", signal);
448 printf("Type 'less %s' to see the output log\n", MONDO_LOGFILE);
[128]449 exit(signal);
450 }
[1]451
452
453/**
454 * Log the last @p g_noof_log_lines lines of @p filename that match @p
455 * grep_for_me to the screen.
456 * @param filename The file to give the end of.
457 * @param grep_for_me If not "", then only give lines in @p filename that match this regular expression.
458 */
[3191]459void log_file_end_to_screen(char *filename, char *grep_for_me) {
[1]460
[128]461 /*@ buffers ********************************************************** */
[2700]462 char *command = NULL;
463 char *tmp = NULL;
[3060]464 char *p = NULL;
[1]465
[128]466 /*@ pointers ********************************************************* */
467 FILE *fin;
[1]468
[128]469 /*@ int ************************************************************** */
470 int i = 0;
[1]471
[128]472 assert_string_is_neither_NULL_nor_zerolength(filename);
473 assert(grep_for_me != NULL);
[1]474
[128]475 if (!does_file_exist(filename)) {
476 return;
[1]477 }
[128]478 if (grep_for_me[0] != '\0') {
[3191]479 mr_asprintf(command, "grep '%s' %s | tail -n%d", grep_for_me, filename, g_noof_log_lines);
[128]480 } else {
[3191]481 mr_asprintf(command, "tail -n%d %s", g_noof_log_lines, filename);
[1]482 }
[128]483 fin = popen(command, "r");
484 if (!fin) {
485 log_OS_error(command);
486 } else {
487 for (i = 0; i < g_noof_log_lines; i++) {
488 for (err_log_lines[i][0] = '\0';
489 strlen(err_log_lines[i]) < 2 && !feof(fin);) {
[3060]490 p = fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN, fin);
491 if (!p) {
492 // FIXME
493 }
[1274]494 /* Commented to make valgrind happy and avoid crash
[128]495 strip_spaces(err_log_lines[i]);
[1274]496 */
[128]497 if (!strncmp(err_log_lines[i], "root:", 5)) {
[3185]498 mr_asprintf(tmp, "%s", err_log_lines[i] + 6);
[2700]499 strncpy(err_log_lines[i], tmp, (size_t)MAX_NEWT_COMMENT_LEN);
500 mr_free(tmp);
[128]501 }
502 if (feof(fin)) {
503 break;
504 }
505 }
506 }
507 paranoid_pclose(fin);
508 }
[2700]509 mr_free(command);
[128]510 refresh_log_screen();
[1]511 }
512
513
514/**
515 * Log a message to the screen.
516 * @param fmt A printf-style format string to write. The following parameters are its arguments.
517 * @note The message is also written to the logfile.
518 */
[3191]519void log_to_screen(const char *fmt, ...) {
[1]520
[128]521 /*@ int ************************************************************** */
522 int i = 0;
523 int j = 0;
524 va_list args;
[1]525
[128]526 /*@ buffers ********************************************************** */
527 char *output;
[1]528
[128]529 malloc_string(output);
[1]530
[128]531 va_start(args, fmt);
[2700]532 vsnprintf(output, MAX_STR_LEN-1, fmt, args);
[128]533 log_msg(0, output);
534 output[80] = '\0';
535 va_end(args);
536 i = (int) strlen(output);
537 if (i > 0 && output[i - 1] < 32) {
538 output[i - 1] = '\0';
539 }
[1]540
[128]541 if (err_log_lines) {
542 for (i = 1; i < g_noof_log_lines; i++) {
543 strcpy(err_log_lines[i - 1],
544 " ");
545 strcpy(err_log_lines[i - 1], err_log_lines[i]);
546 }
547 }
548 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
549 output[strlen(output) - 1] = '\0';
550 }
551 for (j = 0; j < (int) strlen(output); j++) {
552 if (output[j] < 32) {
553 output[j] = ' ';
554 }
555 }
556 if (err_log_lines)
557 strcpy(err_log_lines[g_noof_log_lines - 1], output);
558 if (g_text_mode) {
559 printf("%s\n", output);
560 } else {
561 refresh_log_screen();
562 }
563 paranoid_free(output);
[1]564 }
565
566
567
568
569/**
570 * Write a string to the root window at (@p x, @p y) and also to the logfile.
571 * @param y The row to write the string to.
572 * @param x The column to write the string to.
573 * @param output The string to write.
574 */
[3861]575void mvaddstr_and_log_it(int y, int x, const char *output) {
[3191]576
[128]577 assert_string_is_neither_NULL_nor_zerolength(output);
578 log_msg(0, output);
579 if (g_text_mode) {
580 printf("%s\n", output);
581 } else {
582 newtDrawRootText(x, y, output);
583 newtRefresh();
584 }
585 }
[1]586
587
588
589
590/**
591 * Open an evalcall form with title @p ttl.
592 * @param ttl The title to use for the evalcall form.
593 */
[3191]594void open_evalcall_form(char *ttl) {
[1]595
[128]596 /*@ buffers ********************************************************* */
[3191]597 char *tmp = NULL;
[1]598
[128]599 /*@ initialize ****************************************************** */
600 g_isoform_old_progress = -1;
601 g_mysterious_dot_counter = 0;
[1]602
[128]603 assert(ttl != NULL);
604 if (g_text_mode) {
[3191]605 log_msg(0, ttl);
[128]606 } else {
[3858]607 tmp = mr_center_string(ttl, 80);
[128]608 newtPushHelpLine(tmp);
[3858]609 mr_free(tmp);
[128]610 }
[3858]611 mr_free(g_isoform_header_str);
612 g_isoform_header_str = mr_center_string(ttl, 36);
[128]613 g_isoform_starttime = get_time();
614 if (g_text_mode) {
[3892]615 log_msg(0, "g_isoform_header_str = %s", g_isoform_header_str);
[128]616 } else {
617 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
618 g_isoform_scale = newtScale(3, 3, 34, 100);
[3191]619 newtCenteredWindow(40, 7, ttl);
[3292]620 if (g_isoform_main != NULL) {
621 newtFormDestroy(g_isoform_main);
622 }
[128]623 g_isoform_main = newtForm(NULL, NULL, 0);
[2881]624 g_isoform_timeline = newtLabel(1, 5, " ");
625 g_isoform_pcline = newtLabel(1, 6, " ");
[128]626 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
627 g_isoform_pcline, g_isoform_header,
628 g_isoform_scale, NULL);
[2879]629 newtDrawForm(g_isoform_main);
[128]630 newtRefresh();
631 }
632 update_evalcall_form(0);
633 }
[1]634
635
636
637/**
[3871]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/**
[1]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 */
[3191]760void open_progress_form(char *title, char *b1, char *b2, char *b3, long max_val) {
[1]761
[128]762 /*@ buffers ********************************************************* */
[3858]763 char *b1c = NULL;
[3191]764 char *blurb1 = NULL;
765 char *blurb2 = NULL;
766 char *blurb3 = NULL;
[1]767
[128]768 /*@ initialize ****************************************************** */
769 g_mysterious_dot_counter = 0;
[1]770
[128]771 assert(title != NULL);
772 assert(b1 != NULL);
773 assert(b2 != NULL);
774 assert(b3 != NULL);
[1]775
[3235]776 malloc_string(blurb1);
777 malloc_string(blurb2);
778 malloc_string(blurb3);
779 strcpy(blurb1, b1);
780 strcpy(blurb2, b2);
781 strcpy(blurb3, b3);
[3191]782
[3858]783 b1c = mr_center_string(b1c, 80);
[128]784 if (max_val <= 0) {
785 max_val = 1;
786 }
[1]787
[128]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);
[3191]803 g_scale = newtScale(3, 6, 54, (long long)g_maximum_progress);
[128]804 g_progressForm = newtForm(NULL, NULL, 0);
[2886]805 g_percentline = newtLabel(10, 9, " ");
806 g_timeline = newtLabel(10, 8, " ");
[3191]807 newtFormAddComponents(g_progressForm, g_percentline, g_timeline, g_scale, g_blurb1, g_blurb3, g_blurb2, NULL);
[128]808 newtPushHelpLine(b1c);
[2879]809 newtDrawForm(g_progressForm);
[128]810 newtRefresh();
811 }
812 update_progress_form_full(blurb1, blurb2, blurb3);
[3858]813 mr_free(b1c);
[3235]814 paranoid_free(blurb1);
815 paranoid_free(blurb2);
816 paranoid_free(blurb3);
[128]817 }
818
[1]819/**
820 * Give a message to the user in the form of a dialog box (under Newt).
821 * @param prompt The message.
822 */
[3864]823void popup_and_OK(const char *prompt) {
[3191]824
[128]825 char ch;
[1]826
[128]827 assert_string_is_neither_NULL_nor_zerolength(prompt);
[1]828
[128]829 log_msg(0, prompt);
830 if (g_text_mode) {
[3191]831 printf("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ", prompt);
[128]832 while (((ch = getchar()) != '\n') && (ch != EOF));
833 } else {
[541]834 (void) popup_with_buttons(prompt, " OK ", "");
[128]835 }
836 }
[1]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).
[3377]842 * @param input The string to initialize the user's answer with.
843 * @return value needs to be freed up by caller
844 */
[3854]845char *mr_popup_and_get_string(const char *title, const char *b, const char *input) {
[3377]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
[3618]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 }
[3377]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).
[1]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 */
[3191]902 bool popup_and_get_string(char *title, char *b, char *output, int maxsize) {
[1]903
[128]904 /*@ newt ************************************************************ */
905 newtComponent myForm;
906 newtComponent b_1;
907 newtComponent b_2;
908 newtComponent b_res;
909 newtComponent text;
910 newtComponent type_here;
[1]911
[128]912 /*@ pointers ********************************************************* */
913 char *entry_value;
[1]914
[128]915 /*@ buffers ********************************************************** */
[3191]916 char *blurb = NULL;
[3060]917 char *p;
[3191]918 char *original_contents = NULL;
[1]919
[128]920 assert_string_is_neither_NULL_nor_zerolength(title);
921 assert(b != NULL);
922 assert(output != NULL);
[1]923
[128]924 if (g_text_mode) {
[3191]925 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q---\n--> ", title, b);
[3060]926 p = fgets(output, maxsize, stdin);
927 if (!p) {
928 // FIXME
929 }
[128]930 if (output[strlen(output) - 1] == '\n')
931 output[strlen(output) - 1] = '\0';
932 return (TRUE);
933 }
[3191]934
[3858]935 text = newtTextboxReflowed(2, 1, b, 48, 5, 5, 0);
[3191]936 mr_asprintf(original_contents, "%s", output);
[128]937 output[0] = '\0';
[3191]938 type_here = newtEntry(2, newtTextboxGetNumLines(text) + 2, original_contents, 50,
[1]939#ifdef __cplusplus
[128]940 0, NEWT_FLAG_RETURNEXIT
[1]941#else
[128]942 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
[1]943#endif
[128]944 );
[541]945 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
946 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
[128]947 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
948 myForm = newtForm(NULL, NULL, 0);
949 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
[3858]950 blurb = mr_center_string(b, 80);
[128]951 newtPushHelpLine(blurb);
[3858]952 mr_free(blurb);
[128]953 b_res = newtRunForm(myForm);
954 strcpy(output, entry_value);
955 newtPopHelpLine();
956 newtFormDestroy(myForm);
957 newtPopWindow();
[3191]958
[128]959 if (b_res == b_2) {
960 strcpy(output, original_contents);
[3191]961 mr_free(original_contents);
[128]962 return (FALSE);
963 } else {
[3191]964 mr_free(original_contents);
[128]965 return (TRUE);
966 }
967 }
[1]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 */
[3191]977bool popup_with_buttons(char *p, char *button1, char *button2) {
[1]978
[128]979 /*@ buffers *********************************************************** */
[3191]980 char *prompt = NULL;
[1]981
[128]982 /*@ newt ************************************************************** */
983 newtComponent myForm;
984 newtComponent b_1;
985 newtComponent b_2;
986 newtComponent b_res;
987 newtComponent text;
[1]988
[128]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 }
[3191]998 mr_getline(prompt, stdin);
999 while (strcmp(prompt, button1) && (strlen(button2) == 0 || strcmp(prompt, button2))) {
[128]1000 printf("--> ");
[3191]1001 mr_free(prompt);
1002 mr_getline(prompt, stdin);
[128]1003 }
[2136]1004 if (!strcmp(prompt, button1)) {
[3191]1005 mr_free(prompt);
[128]1006 return (TRUE);
1007 } else {
[3191]1008 mr_free(prompt);
[128]1009 return (FALSE);
1010 }
1011 }
[1]1012
[3858]1013 text = newtTextboxReflowed(1, 1, p, 40, 5, 5, 0);
[3191]1014 b_1 = newtButton(20 - ((button2[0] != '\0') ? strlen(button1) + 2 : strlen(button1) / 2), newtTextboxGetNumLines(text) + 3, button1);
[128]1015 if (button2[0] != '\0') {
[3191]1016 b_2 = newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
[128]1017 } else {
1018 b_2 = NULL;
1019 }
[541]1020 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
[128]1021 myForm = newtForm(NULL, NULL, 0);
1022 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
[3858]1023 prompt = mr_center_string(p, 80);
[128]1024 newtPushHelpLine(prompt);
[3858]1025 mr_free(prompt);
[128]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 }
[1]1036
1037
1038
[128]1039
[1]1040/**
1041 * Synchronize the log messages stored in @p err_log_lines with those shown
1042 * on the screen.
1043 */
[3191]1044void refresh_log_screen() {
[1]1045
[128]1046 /*@ int *********************************************************** */
1047 int i = 0;
[1]1048
1049
[128]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 }
[1]1065
1066
1067/**
1068 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
1069 * only allocate some memory.
1070 */
[3191]1071void setup_newt_stuff() {
[1]1072
[128]1073 /*@ int *********************************************************** */
1074 int i = 0;
1075 int cols;
[1]1076
[128]1077 if (!g_text_mode) {
1078 newtInit();
1079 newtCls();
[3191]1080 newtPushHelpLine("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
[128]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 }
[1]1086
[3191]1087 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
[1]1088
[128]1089 for (i = 0; i < g_noof_log_lines; i++) {
[3191]1090 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
[128]1091 }
[1]1092
[128]1093 for (i = 0; i < g_noof_log_lines; i++) {
1094 err_log_lines[i][0] = '\0';
1095 }
1096 }
[1]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 */
[3191]1104void update_evalcall_form_ratio(int num, int denom) {
[1]1105
[128]1106 /*@ long ************************************************************ */
[3191]1107 long current_time = 0L;
1108 long time_taken = 0L;
1109 long time_total_est = 0L;
1110 long time_remaining = 0L;
[1]1111
[128]1112 /*@ buffers ********************************************************** */
[3191]1113 char *timeline_str = NULL;
[2211]1114 char *pcline_str = NULL;
1115 char *taskprogress = NULL;
[1]1116
[128]1117 /*@ int ************************************************************** */
1118 int percentage = 0;
1119 int i = 0;
1120 int j = 0;
[1]1121
[128]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 {
[3191]1134 time_remaining = 0L;
[128]1135 }
1136 if (!g_text_mode) {
[1899]1137 if (g_isoform_header) {
1138 newtLabelSetText(g_isoform_header, g_isoform_header_str);
1139 }
[128]1140 }
1141 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
[3205]1142 if ((percentage < 3 && g_isoform_old_progress < 3) || percentage > g_isoform_old_progress) {
[128]1143 g_isoform_old_progress = percentage;
[3191]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);
[128]1145 if (percentage < 3) {
[3185]1146 mr_asprintf(pcline_str, " Working");
[128]1147 for (j = 0; j < g_mysterious_dot_counter; j++) {
[2211]1148 mr_strcat(pcline_str, ".");
[128]1149 }
1150 for (; j < 27; j++) {
[2211]1151 mr_strcat(pcline_str, " ");
[128]1152 }
[2211]1153 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
[128]1154 } else {
[3191]1155 mr_asprintf(pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
[128]1156 }
1157 if (g_text_mode) {
[3185]1158 mr_asprintf(taskprogress, "TASK: [");
[128]1159 for (i = 0; i < percentage; i += 5) {
[2211]1160 mr_strcat(taskprogress, "*");
[128]1161 }
1162 for (; i < 100; i += 5) {
[2211]1163 mr_strcat(taskprogress, ".");
[128]1164 }
1165 if (percentage >= 3) {
[3191]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);
[1899]1168 printf("---evalcall---2--- %s\n", taskprogress);
1169 printf("---evalcall---E---\n");
[128]1170 }
[3191]1171 mr_free(taskprogress);
[128]1172 } else {
[3191]1173 newtScaleSet(g_isoform_scale, (unsigned long long) percentage);
[1899]1174 if (g_isoform_pcline) {
1175 newtLabelSetText(g_isoform_pcline, pcline_str);
1176 }
1177 if ((percentage >= 3) && (g_isoform_timeline)) {
[128]1178 newtLabelSetText(g_isoform_timeline, timeline_str);
1179 }
1180 }
[3191]1181 mr_free(pcline_str);
1182 mr_free(timeline_str);
[128]1183 }
1184 if (!g_text_mode) {
[2879]1185 newtDrawForm(g_isoform_main);
[128]1186 newtRefresh();
1187 }
[1]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 */
[3191]1196void update_evalcall_form(int curr) {
1197
[128]1198 update_evalcall_form_ratio(curr, 100);
[3191]1199}
[1]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 */
[3191]1208void update_progress_form(char *blurb3) {
1209
[128]1210 if (g_current_progress == -999) {
1211 return;
1212 }
1213 strcpy(g_blurb_str_2, blurb3);
[3191]1214 update_progress_form_full(g_blurb_str_1, g_blurb_str_2, g_blurb_str_3);
1215}
[1]1216
1217
1218/**
1219 * Ask the user which backup media type they would like to use.
[3881]1220 * The choices are @p none (exit to shell), @c optical,
[3882]1221 * @c tape, @c udev (only when @p g_text_mode is TRUE), @c netfs,
[1]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 */
[3191]1226t_bkptype which_backup_media_type(bool restoring) {
[1]1227
[128]1228 /*@ char ************************************************************ */
1229 t_bkptype output;
[1]1230
1231
[128]1232 /*@ newt ************************************************************ */
[3191]1233 char *title_sz = NULL;
1234 char *minimsg_sz = NULL;
[3882]1235 static t_bkptype possible_bkptypes[] = { none, optical, tape, udev, netfs, iso };
1236 static char *possible_responses[] = { "none", "optical", "tape", "udev", "netfs", "iso", NULL };
[3191]1237 char *outstr = NULL;
[3614]1238 char *instr = NULL;
[128]1239 t_bkptype backup_type;
1240 int i;
[1]1241
[128]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;
[1]1250
[128]1251 if (g_text_mode) {
1252 for (backup_type = none; backup_type == none;) {
[541]1253 printf("Backup type (");
[128]1254 for (i = 0; possible_responses[i]; i++) {
1255 printf("%c%s", (i == 0) ? '\0' : ' ',
1256 possible_responses[i]);
1257 }
1258 printf(")\n--> ");
[3614]1259 mr_getline(instr, stdin);
1260 outstr = mr_strip_spaces(instr);
1261 mr_free(instr);
[128]1262 for (i = 0; possible_responses[i]; i++) {
1263 if (!strcmp(possible_responses[i], outstr)) {
1264 backup_type = possible_bkptypes[i];
1265 }
1266 }
[3191]1267 mr_free(outstr);
[128]1268 }
1269 return (backup_type);
1270 }
1271 newtDrawRootText(18, 0, WELCOME_STRING);
1272 if (restoring) {
[3191]1273 mr_asprintf(title_sz, "Please choose the backup media from which you want to read data.");
1274 mr_asprintf(minimsg_sz, "Read from:");
[128]1275 } else {
[3191]1276 mr_asprintf(title_sz, "Please choose the backup media to which you want to archive data.");
1277 mr_asprintf(minimsg_sz, "Backup to:");
[128]1278 }
1279 newtPushHelpLine(title_sz);
1280 newtCenteredWindow(34, 17, minimsg_sz);
[3881]1281 b1 = newtButton(1, 1, "Optical disks ");
[541]1282 b3 = newtButton(1, 9, "Tape drive ");
[1703]1283 b4 = newtButton(17, 5, "USB Key/Disk");
[2380]1284 b6 = newtButton(17, 9, " Net mount ");
[541]1285 b7 = newtButton(1, 13, " Hard disk ");
1286 b8 = newtButton(17, 13, " Exit ");
[128]1287 myForm = newtForm(NULL, NULL, 0);
[3881]1288 newtFormAddComponents(myForm, b1, b3, b7, b4, b6, b8, NULL);
[128]1289 b_res = newtRunForm(myForm);
1290 newtFormDestroy(myForm);
1291 newtPopWindow();
1292 if (b_res == b1) {
[3881]1293 output = optical;
[128]1294 } else if (b_res == b3) {
1295 output = tape;
1296 } else if (b_res == b4) {
[1703]1297 output = usb;
[128]1298 } else if (b_res == b6) {
[2380]1299 output = netfs;
[128]1300 } else if (b_res == b7) {
1301 output = iso;
1302 } else {
1303 output = none;
1304 }
1305 newtPopHelpLine();
[3191]1306 mr_free(title_sz);
1307 mr_free(minimsg_sz);
[128]1308 return (output);
1309 }
[1]1310
1311
1312
1313
1314/**
[2771]1315 * Ask the user which compression type they would like to use.
[3207]1316 * The choices are "bzip2", "gzip", "lzo", "lzma". NULL for exit
[2771]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;
[3191]1329 newtComponent b4;
[2771]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");
[3191]1341 b4 = newtButton(18, 5, "lzma");
[2771]1342 b5 = newtButton(4, 9, " Exit ");
1343 myForm = newtForm(NULL, NULL, 0);
[3207]1344 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
[2771]1345 b_res = newtRunForm(myForm);
1346 newtFormDestroy(myForm);
1347 newtPopWindow();
1348 if (b_res == b1) {
[3185]1349 mr_asprintf(output, "%s", "bzip2");
[2771]1350 } else if (b_res == b2) {
[3185]1351 mr_asprintf(output, "%s", "gzip");
[2771]1352 } else if (b_res == b3) {
[3185]1353 mr_asprintf(output, "%s", "lzo");
[3191]1354 } else if (b_res == b4) {
1355 mr_asprintf(output, "%s", "lzma");
[2771]1356 }
1357 newtPopHelpLine();
1358 return(output);
1359 }
1360
1361
1362/**
[1]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 */
[3191]1367int which_compression_level() {
[1]1368
[128]1369 /*@ char ************************************************************ */
1370 int output = none;
[1]1371
1372
[128]1373 /*@ newt ************************************************************ */
[1]1374
[128]1375 newtComponent b1;
1376 newtComponent b2;
1377 newtComponent b3;
1378 newtComponent b4;
1379 newtComponent b5;
1380 newtComponent b_res;
1381 newtComponent myForm;
[1]1382
[128]1383 newtDrawRootText(18, 0, WELCOME_STRING);
[3191]1384 newtPushHelpLine(" Please specify the level of compression that you want.");
[541]1385 newtCenteredWindow(34, 13, "How much compression?");
[3114]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) ");
[541]1390 b5 = newtButton(4, 9, " Exit ");
[128]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 }
[1]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 */
[3191]1421int load_filelist_into_array(struct s_filelist *filelist, char *source_file) {
1422
[128]1423 int i;
[1662]1424 int j = 0;
[128]1425 bool done;
[3191]1426 char *tmp = NULL;
[1662]1427 char *tmp1 = NULL;
1428 FILE *fin, *fout;
[128]1429 struct s_filelist_entry dummy_fle;
[1]1430
[128]1431 assert(filelist != NULL);
1432 assert_string_is_neither_NULL_nor_zerolength(source_file);
[1]1433
[2230]1434 log_it("entering");
[128]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 }
[1662]1441
[3185]1442 mr_asprintf(tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
[1662]1443 if (!(fout = fopen(tmp1, "a"))) {
1444 log_msg(2, "Can't write to %s", tmp1);
[3191]1445 mr_free(tmp1);
[1662]1446 return(1);
1447 }
1448
[128]1449 log_msg(2, "Loading %s", source_file);
[3191]1450 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM; ) {
1451 mr_free(tmp);
[128]1452 if (feof(fin)) {
1453 break;
1454 }
[3191]1455 mr_getline(tmp, fin);
[128]1456 i = (int) strlen(tmp);
1457 if (i < 2) {
[3191]1458 continue;
[128]1459 }
1460 if (tmp[i - 1] < 32) {
1461 tmp[--i] = '\0';
1462 }
1463 if (i < 2) {
[3191]1464 continue;
[128]1465 }
[1662]1466 if (!does_file_exist(tmp) && !feof(fin)) {
1467 j++;
1468 fprintf(fout, "%s\n", tmp);
[3191]1469 continue;
[128]1470 }
[3191]1471 filelist->el[filelist->entries].severity = severity_of_difference(tmp, NULL);
[128]1472 strcpy(filelist->el[filelist->entries].filename, tmp);
1473 if (feof(fin)) {
1474 break;
1475 }
[3191]1476 filelist->entries++;
[128]1477 }
[3191]1478 mr_free(tmp);
[128]1479 paranoid_fclose(fin);
[1662]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 }
[3191]1484 mr_free(tmp1);
[128]1485 if (filelist->entries >= ARBITRARY_MAXIMUM) {
[541]1486 log_to_screen("Arbitrary limits suck, man!");
[128]1487 return (1);
1488 }
1489 for (done = FALSE; !done;) {
1490 done = TRUE;
1491 for (i = 0; i < filelist->entries - 1; i++) {
[1]1492// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
[128]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 }
[2230]1514 log_it("leaving");
[128]1515 return (0);
1516 }
[1]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.
[3892]1523 * @note The returned value points to a dynamically allocated string that the caller needs to free.
[1]1524 */
[3191]1525char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
[3892]1526 char *comment = NULL;
[1]1527
[3191]1528 log_msg(8, "entering");
[128]1529 assert(flentry != NULL);
1530 if (flentry->severity == 0) {
[3892]1531 mr_asprintf(comment, "0 ");
[128]1532 } else if (flentry->severity == 1) {
[3892]1533 mr_asprintf(comment, "low ");
[128]1534 } else if (flentry->severity == 2) {
[3892]1535 mr_asprintf(comment, "med ");
[128]1536 } else {
[3892]1537 mr_asprintf(comment, "high");
[128]1538 }
[3892]1539 mr_strcat(comment, " ");
1540 mr_strcat(comment, flentry->filename);
[2230]1541 log_it("leaving");
[128]1542 return (comment);
1543 }
[1]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 */
[3191]1551void popup_changelist_from_file(char *source_file) {
1552
1553 char *reason = NULL;
[128]1554 newtComponent myForm;
1555 newtComponent bClose;
1556 newtComponent bSelect;
1557 newtComponent b_res;
1558 newtComponent fileListbox;
1559 newtComponent headerMsg;
[1]1560
[128]1561 /*@ ???? ************************************************************ */
1562 void *curr_choice;
1563 void *keylist[ARBITRARY_MAXIMUM];
[1]1564
[128]1565 /*@ int ************************************************************* */
1566 int currline = 0;
1567 int finished = FALSE;
[738]1568
1569 /*@ long ************************************************************ */
1570 long i = 0;
[128]1571 long lng = 0;
[1]1572
[128]1573 /*@ buffers ********************************************************* */
[3191]1574 char *tmp = NULL;
1575 char *differ_sz = NULL;
[1]1576
[128]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.");
[3191]1581 return;
[128]1582 }
1583 log_msg(2, "Examining file %s", source_file);
[1]1584
[128]1585 lng = count_lines_in_file(source_file);
1586 if (lng < 1) {
1587 log_msg(2, "No lines in file. Therefore, no popup list.");
[3191]1588 return;
[128]1589 } else if (lng >= ARBITRARY_MAXIMUM) {
1590 log_msg(2, "Too many files differ for me to list.");
[3191]1591 return;
[128]1592 }
[1]1593
[128]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);
[1]1598
[128]1599 if (load_filelist_into_array(filelist, source_file)) {
[3191]1600 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
[128]1601 return;
1602 }
[3191]1603 log_msg(2, "%d files loaded into filelist array", filelist->entries);
[128]1604 for (i = 0; i < filelist->entries; i++) {
1605 keylist[i] = (void *) i;
[3892]1606 tmp = filelist_entry_to_string(&(filelist->el[i]));
[128]1607 newtListboxAppendEntry(fileListbox,
[3892]1608 tmp,
[128]1609 keylist[i]);
[3892]1610 mr_free(tmp);
[128]1611 }
[3191]1612 mr_asprintf(differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
[128]1613 newtPushHelpLine(differ_sz);
[3191]1614 mr_free(differ_sz);
1615
[541]1616 bClose = newtCompactButton(10, 15, " Close ");
1617 bSelect = newtCompactButton(30, 15, " Select ");
[3191]1618 mr_asprintf(tmp, "%-10s %-20s", "Priority", "Filename");
[128]1619 headerMsg = newtLabel(2, 1, tmp);
[3191]1620 mr_free(tmp);
1621
[541]1622 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
[128]1623 myForm = newtForm(NULL, NULL, 0);
[3191]1624 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose, bSelect, NULL);
1625
[128]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) {
[541]1636 log_to_screen("I don't know what that button does!");
[128]1637 } else {
1638 currline = i;
1639 if (filelist->entries > 0) {
[3191]1640 malloc_string(reason);
[3194]1641 severity_of_difference(filelist->el[currline].filename, &reason);
[3191]1642 mr_asprintf(tmp, "%s --- %s", filelist->el[currline].filename, reason);
1643
[128]1644 popup_and_OK(tmp);
[3191]1645 mr_free(tmp);
1646 paranoid_free(reason);
[128]1647 }
1648 }
1649 }
1650 }
1651 newtFormDestroy(myForm);
1652 newtPopWindow();
1653 newtPopHelpLine();
1654 return;
[1]1655 }
1656
1657/* @} - end of guiGroup */
1658
1659
[3846]1660#ifdef __cplusplus
[128]1661} /* extern "C" */
[1]1662#endif
Note: See TracBrowser for help on using the repository browser.