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

Last change on this file since 1075 was 1075, checked in by Bruno Cornec, 17 years ago

Improvements for USB support in mindi when called from mondo
Changelogs in C files removed from common

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