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