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

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

Use of conf file entries (iso_burning_*, media_device, media_size)
and adapatation of the rest of the code to that (including bkpinfo)

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