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