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

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

merge -r1078:1080 $SVN_M/branches/stable

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