source: MondoRescue/branches/2.2.10/mondo/src/common/newt-specific.c@ 2380

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