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

Last change on this file since 30 was 30, checked in by bcornec, 19 years ago

Id property added on files to allow for better conf. management

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