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

Last change on this file since 94 was 94, checked in by bcornec, 18 years ago

x86_64 support (begining)
find_and_store_mondoarchives_home interface change
libmondo-tools.c modified with asprintf

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