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

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

memory management continues:

  • mondoarchive handled completely
  • bkpinfo, begining of dyn. alloc.
  • lot of changes around memory everywhere

=> even if it compiles, i'm pretty sure it doesn't work yet (even not tried)

  • Property svn:keywords set to Id
File size: 44.6 KB
Line 
1/* $Id: newt-specific.c 171 2005-12-08 16:20:29Z 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 171 2005-12-08 16:20:29Z 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 ("gzip -9c "MONDO_LOGFILE" > /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, "grep '%s' %s | tail -n%d",
430 grep_for_me, filename, g_noof_log_lines);
431 } else {
432 asprintf(&command, "tail -n%d %s", g_noof_log_lines, filename);
433 }
434 fin = popen(command, "r");
435 if (!fin) {
436 log_OS_error(command);
437 } else {
438 for (i = 0; i < g_noof_log_lines; i++) {
439 for (err_log_lines[i][0] = '\0';
440 strlen(err_log_lines[i]) < 2 && !feof(fin);) {
441 (void) fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN,
442 fin);
443 strip_spaces(err_log_lines[i]);
444 if (!strncmp(err_log_lines[i], "root:", 5)) {
445 asprintf(&tmp, "%s", err_log_lines[i] + 6);
446 strcpy(err_log_lines[i], tmp);
447 paranoid_free(tmp);
448 }
449 if (feof(fin)) {
450 break;
451 }
452 }
453 }
454 paranoid_pclose(fin);
455 }
456 refresh_log_screen();
457 paranoid_free(command);
458 }
459
460
461/**
462 * Log a message to the screen.
463 * @param fmt A printf-style format string to write. The following parameters are its arguments.
464 * @note The message is also written to the logfile.
465 */
466 void
467 log_to_screen(const char *fmt, ...) {
468
469 /*@ int ************************************************************** */
470 int i = 0;
471 int j = 0;
472 va_list args;
473
474 /*@ buffers ********************************************************** */
475 char *output;
476
477
478 va_start(args, fmt);
479 vasprintf(&output, fmt, args);
480 log_msg(0, output);
481 if (strlen(output) > 80) {
482 output[80] = '\0';
483 }
484 va_end(args);
485 i = (int) strlen(output);
486 if (i > 0 && output[i - 1] < 32) {
487 output[i - 1] = '\0';
488 }
489
490 if (err_log_lines) {
491 for (i = 1; i < g_noof_log_lines; i++) {
492 strcpy(err_log_lines[i - 1],
493 " ");
494 strcpy(err_log_lines[i - 1], err_log_lines[i]);
495 }
496 }
497 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
498 output[strlen(output) - 1] = '\0';
499 }
500 for (j = 0; j < (int) strlen(output); j++) {
501 if (output[j] < 32) {
502 output[j] = ' ';
503 }
504 }
505 if (err_log_lines)
506 strcpy(err_log_lines[g_noof_log_lines - 1], output);
507 if (g_text_mode) {
508 printf("%s\n", output);
509 } else {
510 refresh_log_screen();
511 }
512 paranoid_free(output);
513 }
514
515
516
517
518/**
519 * Write a string to the root window at (@p x, @p y) and also to the logfile.
520 * @param y The row to write the string to.
521 * @param x The column to write the string to.
522 * @param output The string to write.
523 */
524 void
525 mvaddstr_and_log_it(int y, int x, char *output) {
526 assert_string_is_neither_NULL_nor_zerolength(output);
527 log_msg(0, output);
528 if (g_text_mode) {
529 printf("%s\n", output);
530 } else {
531 newtDrawRootText(x, y, output);
532 newtRefresh();
533 }
534 }
535
536
537
538
539/**
540 * Open an evalcall form with title @p ttl.
541 * @param ttl The title to use for the evalcall form.
542 */
543 void
544 open_evalcall_form(char *ttl) {
545
546 /*@ buffers ********************************************************* */
547 char *title;
548 char *tmp;
549
550 /*@ initialize ****************************************************** */
551 g_isoform_old_progress = -1;
552 g_mysterious_dot_counter = 0;
553
554 assert(ttl != NULL);
555 asprintf(&title, ttl);
556 // BERLIOS: We need to unallocate it somewhere
557 asprintf(&g_isoform_header_str, title);
558 // center_string (title, 80);
559 if (g_text_mode) {
560 log_msg(0, title);
561 } else {
562 asprintf(&tmp, title);
563 /* BERLIOS: center_string is now broken replace it ! */
564 //center_string(tmp, 80);
565 newtPushHelpLine(tmp);
566 paranoid_free(tmp);
567 }
568 /* BERLIOS: center_string is now broken replace it ! */
569 //center_string(g_isoform_header_str, 36);
570 g_isoform_starttime = get_time();
571 if (g_text_mode) {
572 log_msg(0, g_isoform_header_str);
573 } else {
574 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
575 g_isoform_scale = newtScale(3, 3, 34, 100);
576 // newtOpenWindow (20, 6, 40, 7, title); // "Please Wait");
577 newtCenteredWindow(40, 7, title);
578 g_isoform_main = newtForm(NULL, NULL, 0);
579 g_isoform_timeline = newtLabel(1, 5, "This is the timeline");
580 g_isoform_pcline = newtLabel(1, 6, "This is the pcline");
581 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
582 g_isoform_pcline, g_isoform_header,
583 g_isoform_scale, NULL);
584 newtRefresh();
585 }
586 update_evalcall_form(0);
587 paranoid_free(title);
588 }
589
590
591
592/**
593 * Open a progress form with title @p title.
594 * @param title The title to use for the progress form (will be put in the title bar on Newt).
595 * @param b1 The first line of the blurb; generally static.
596 * @param b2 The second line of the blurb; generally static.
597 * @param b3 The third line of the blurb; generally dynamic (it is passed
598 * to update_evalcall_form() every time).
599 * @param max_val The maximum amount of progress (number of filesets, etc.)
600 */
601 void
602 open_progress_form(char *title, char *b1, char *b2, char *b3,
603 long max_val) {
604
605 /*@ buffers ********************************************************* */
606 char *b1c;
607 char *blurb1;
608 char *blurb2;
609 char *blurb3;
610
611 /*@ initialize ****************************************************** */
612 g_mysterious_dot_counter = 0;
613
614 assert(title != NULL);
615 assert(b1 != NULL);
616 assert(b2 != NULL);
617 assert(b3 != NULL);
618
619 asprintf(&blurb1, b1);
620 asprintf(&blurb2, b2);
621 asprintf(&blurb3, b3);
622 asprintf(&b1c, b1);
623 /* BERLIOS: center_string is now broken replace it ! */
624 //center_string(b1c, 80);
625 if (max_val <= 0) {
626 max_val = 1;
627 }
628
629 g_start_time = get_time();
630 g_maximum_progress = max_val;
631 g_current_progress = 0;
632 // BERLIOS: We need to unallocate them
633 asprintf(&g_blurb_str_1, blurb1);
634 asprintf(&g_blurb_str_2, blurb3);
635 asprintf(&g_blurb_str_3, blurb2);
636 if (g_text_mode) {
637 log_msg(0, blurb1);
638 log_msg(0, blurb2);
639 log_msg(0, blurb3);
640 } else {
641 g_blurb1 = newtLabel(2, 1, blurb1);
642 g_blurb2 = newtLabel(2, 2, blurb3);
643 g_blurb3 = newtLabel(2, 4, blurb2);
644 // newtOpenWindow (10, 4, 60, 11, title);
645 newtCenteredWindow(60, 11, title);
646 g_scale = newtScale(3, 6, 54, g_maximum_progress);
647 g_progressForm = newtForm(NULL, NULL, 0);
648 g_percentline = newtLabel(10, 9, "This is the percentline");
649 g_timeline = newtLabel(10, 8, "This is the timeline");
650 newtFormAddComponents(g_progressForm, g_percentline,
651 g_timeline, g_scale, g_blurb1, g_blurb3,
652 g_blurb2, NULL);
653 newtPushHelpLine(b1c);
654 newtRefresh();
655 }
656 update_progress_form_full(blurb1, blurb2, blurb3);
657 paranoid_free(b1c);
658 paranoid_free(blurb1);
659 paranoid_free(blurb2);
660 paranoid_free(blurb3);
661 }
662
663/**
664 * Give a message to the user in the form of a dialog box (under Newt).
665 * @param prompt The message.
666 */
667 void
668 popup_and_OK(char *prompt) {
669 char ch;
670
671 assert_string_is_neither_NULL_nor_zerolength(prompt);
672
673 log_msg(0, prompt);
674 if (g_text_mode) {
675 printf
676 ("---promptpopup---1--- %s\r\n---promptpopup---Q--- [OK] ---\r\n--> ",
677 prompt);
678 while (((ch = getchar()) != '\n') && (ch != EOF));
679 } else {
680 (void) popup_with_buttons(prompt, " OK ", "");
681 }
682 }
683
684/**
685 * Ask the user to enter a value.
686 * @param title The title of the dialog box.
687 * @param b The blurb (e.g. what you want the user to enter).
688 * @param output The string to put the user's answer in. It has to be freed by the caller
689 * @param maxsize The size in bytes allocated to @p output.
690 * @return TRUE if the user pressed OK, FALSE if they pressed Cancel.
691 */
692 bool popup_and_get_string(char *title, char *b, char *output,
693 int maxsize) {
694
695 /*@ newt ************************************************************ */
696 newtComponent myForm;
697 newtComponent b_1;
698 newtComponent b_2;
699 newtComponent b_res;
700 newtComponent text;
701 newtComponent type_here;
702
703 /*@ pointers ********************************************************* */
704 char *entry_value;
705
706 /*@ buffers ********************************************************** */
707 char *blurb;
708 char *original_contents;
709 size_t n = 0;
710
711 assert_string_is_neither_NULL_nor_zerolength(title);
712 assert(b != NULL);
713
714 if (g_text_mode) {
715 printf
716 ("---promptstring---1--- %s\r\n---promptstring---2--- %s\r\n---promptstring---Q---\r\n--> ",
717 title, b);
718 (void) getline(&output, &n, stdin);
719 if (output[strlen(output) - 1] == '\n')
720 output[strlen(output) - 1] = '\0';
721 return (TRUE);
722 }
723 asprintf(&blurb, b);
724 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
725 original_contents = output;
726
727 type_here =
728 newtEntry(2, newtTextboxGetNumLines(text) + 2,
729 original_contents, 50,
730#ifdef __cplusplus
731 0, NEWT_FLAG_RETURNEXIT
732#else
733 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
734#endif
735 );
736 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
737 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
738 // newtOpenWindow (8, 5, 54, newtTextboxGetNumLines (text) + 9, title);
739 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
740 myForm = newtForm(NULL, NULL, 0);
741 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
742 /* BERLIOS: center_string is now broken replace it ! */
743 //center_string(blurb, 80);
744 newtPushHelpLine(blurb);
745 paranoid_free(blurb);
746 b_res = newtRunForm(myForm);
747 output = entry_value;
748 newtPopHelpLine();
749 newtFormDestroy(myForm);
750 newtPopWindow();
751 if (b_res == b_2) {
752 paranoid_free(output);
753 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 if (feof(fin)) {
1382 break;
1383 }
1384 (void) getline(&tmp, &n, fin);
1385 i = (int) strlen(tmp);
1386 if (i < 2) {
1387 if (feof(fin)) {
1388 break;
1389 }
1390 }
1391 if (tmp[i - 1] < 32) {
1392 tmp[--i] = '\0';
1393 }
1394 if (i < 2) {
1395 if (feof(fin)) {
1396 break;
1397 }
1398 }
1399 if (!does_file_exist(tmp)) {
1400 if (feof(fin)) {
1401 break;
1402 }
1403 }
1404 filelist->el[filelist->entries].severity =
1405 severity_of_difference(tmp, reason);
1406 paranoid_free(reason);
1407 strcpy(filelist->el[filelist->entries].filename, tmp);
1408 if (feof(fin)) {
1409 break;
1410 }
1411 }
1412 paranoid_fclose(fin);
1413 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1414 log_to_screen("Arbitrary limits suck, man!");
1415 paranoid_free(tmp);
1416 return (1);
1417 }
1418 paranoid_free(tmp);
1419
1420 for (done = FALSE; !done;) {
1421 done = TRUE;
1422 for (i = 0; i < filelist->entries - 1; i++) {
1423// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
1424 if (filelist->el[i].severity < filelist->el[i + 1].severity
1425 || (filelist->el[i].severity ==
1426 filelist->el[i + 1].severity
1427 && strcmp(filelist->el[i].filename,
1428 filelist->el[i + 1].filename) > 0)) {
1429 memcpy((void *) &dummy_fle,
1430 (void *) &(filelist->el[i]),
1431 sizeof(struct s_filelist_entry));
1432 memcpy((void *) &(filelist->el[i]),
1433 (void *) &(filelist->el[i + 1]),
1434 sizeof(struct s_filelist_entry));
1435 memcpy((void *) &(filelist->el[i + 1]),
1436 (void *) &dummy_fle,
1437 sizeof(struct s_filelist_entry));
1438 log_msg(2, "Swapping %s and %s",
1439 filelist->el[i].filename,
1440 filelist->el[i + 1].filename);
1441 done = FALSE;
1442 }
1443 }
1444 }
1445 iamhere("leaving");
1446 return (0);
1447 }
1448
1449
1450
1451/**
1452 * Generate a pretty string based on @p flentry.
1453 * @param flentry The filelist entry to stringify.
1454 * @return The string form of @p flentry.
1455 * @note The returned value points to static storage that will be overwritten with each call.
1456 */
1457 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1458 char *comment;
1459
1460 iamhere("entering");
1461 assert(flentry != NULL);
1462 if (flentry->severity == 0) {
1463 asprintf(&comment, "0 %93s", flentry->filename);
1464 } else if (flentry->severity == 1) {
1465 asprintf(&comment, "low %93s", flentry->filename);
1466 } else if (flentry->severity == 2) {
1467 asprintf(&comment, "med %93s", flentry->filename);
1468 } else {
1469 asprintf(&comment, "high %93s", flentry->filename);
1470 }
1471 iamhere("leaving");
1472 return (comment);
1473 }
1474
1475
1476
1477
1478
1479/**
1480 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1481 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1482 * @param source_file The file containing a list of changed files.
1483 */
1484 void popup_changelist_from_file(char *source_file) {
1485 char *reason = NULL;
1486 newtComponent myForm;
1487 newtComponent bClose;
1488 newtComponent bSelect;
1489 newtComponent b_res;
1490 newtComponent fileListbox;
1491 newtComponent headerMsg;
1492
1493 /*@ ???? ************************************************************ */
1494 void *curr_choice;
1495 void *keylist[ARBITRARY_MAXIMUM];
1496
1497 /*@ int ************************************************************* */
1498 int i = 0;
1499 int currline = 0;
1500 int finished = FALSE;
1501 long lng = 0;
1502
1503 /*@ buffers ********************************************************* */
1504 char *tmp;
1505 char *differ_sz;
1506
1507 struct s_filelist *filelist;
1508 assert_string_is_neither_NULL_nor_zerolength(source_file);
1509 if (g_text_mode) {
1510 log_msg(2, "Text mode. Therefore, no popup list.");
1511 return;
1512 }
1513 log_msg(2, "Examining file %s", source_file);
1514
1515 lng = count_lines_in_file(source_file);
1516 if (lng < 1) {
1517 log_msg(2, "No lines in file. Therefore, no popup list.");
1518 return;
1519 } else if (lng >= ARBITRARY_MAXIMUM) {
1520 log_msg(2, "Too many files differ for me to list.");
1521 return;
1522 }
1523
1524 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1525 fileListbox =
1526 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1527 newtListboxClear(fileListbox);
1528
1529 if (load_filelist_into_array(filelist, source_file)) {
1530 log_msg(2, "Can't open %s; therefore, cannot popup list",
1531 source_file);
1532 return;
1533 }
1534 log_msg(2, "%d files loaded into filelist array",
1535 filelist->entries);
1536 for (i = 0; i < filelist->entries; i++) {
1537 keylist[i] = (void *) i;
1538 newtListboxAppendEntry(fileListbox,
1539 filelist_entry_to_string(&
1540 (filelist->
1541 el[i])),
1542 keylist[i]);
1543 }
1544 asprintf(&differ_sz,
1545 " %d files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.",
1546 i);
1547 newtPushHelpLine(differ_sz);
1548 paranoid_free(differ_sz);
1549
1550 bClose = newtCompactButton(10, 15, " Close ");
1551 bSelect = newtCompactButton(30, 15, " Select ");
1552 asprintf(&tmp, "%-10s %-20s", "Priority",
1553 "Filename");
1554 headerMsg = newtLabel(2, 1, tmp);
1555 paranoid_free(tmp);
1556
1557 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1558 myForm = newtForm(NULL, NULL, 0);
1559 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1560 bSelect, NULL);
1561 while (!finished) {
1562 b_res = newtRunForm(myForm);
1563 if (b_res == bClose) {
1564 finished = TRUE;
1565 } else {
1566 curr_choice = newtListboxGetCurrent(fileListbox);
1567 for (i = 0;
1568 i < filelist->entries && keylist[i] != curr_choice;
1569 i++);
1570 if (i == filelist->entries && filelist->entries > 0) {
1571 log_to_screen("I don't know what that button does!");
1572 } else {
1573 currline = i;
1574 if (filelist->entries > 0) {
1575 severity_of_difference(filelist->el[currline].
1576 filename, reason);
1577 asprintf(&tmp, "%s --- %s",
1578 filelist->el[currline].filename, reason);
1579 popup_and_OK(tmp);
1580 paranoid_free(tmp);
1581 paranoid_free(reason);
1582 }
1583 }
1584 }
1585 }
1586 newtFormDestroy(myForm);
1587 newtPopWindow();
1588 newtPopHelpLine();
1589 }
1590
1591/* @} - end of guiGroup */
1592
1593
1594#if __cplusplus
1595} /* extern "C" */
1596#endif
1597
1598
1599void wait_until_software_raids_are_prepped(char *mdstat_file,
1600 int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.