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