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