source: MondoRescue/branches/3.0/mondo/src/common/newt-specific.c@ 3191

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