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

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