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

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

remove center_string function to keep only mr_center_string

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