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

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

merge -r 125:126 $SVN_M/branches/2.05

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