source: MondoRescue/branches/stable/mondo/src/common/newt-specific.c@ 1080

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