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

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

merge r87:88 of the 2.04_berlios branch
indent some files

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