source: MondoRescue/trunk/mondo/mondo/common/newt-specific.c@ 56

Last change on this file since 56 was 56, checked in by bcornec, 19 years ago

asprintf for newt-specific.c (first step)

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