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

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

Merge from trun for memory mngt for newt-specific.c

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