1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : NT_main.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "NT_local.h" |
---|
12 | |
---|
13 | #include <mg_merge.hxx> |
---|
14 | #include <awti_import.hxx> |
---|
15 | #include <insdel.h> |
---|
16 | #include <macros.hxx> |
---|
17 | #include <mod_rlimit.h> |
---|
18 | #include <TreeDisplay.hxx> |
---|
19 | |
---|
20 | #include <awt.hxx> |
---|
21 | #include <awt_misc.hxx> |
---|
22 | #include <awt_sel_boxes.hxx> |
---|
23 | #include <awt_TreeAwars.hxx> |
---|
24 | |
---|
25 | #include <aw_advice.hxx> |
---|
26 | #include <aw_question.hxx> |
---|
27 | #include <aw_awars.hxx> |
---|
28 | #include <aw_edit.hxx> |
---|
29 | #include <aw_file.hxx> |
---|
30 | #include <aw_msg.hxx> |
---|
31 | #include <aw_root.hxx> |
---|
32 | #include <aw_global_awars.hxx> |
---|
33 | |
---|
34 | #include <adGene.h> |
---|
35 | |
---|
36 | #include <arb_version.h> |
---|
37 | #include <arb_progress.h> |
---|
38 | #include <arb_file.h> |
---|
39 | |
---|
40 | #include <signal.h> |
---|
41 | |
---|
42 | using namespace std; |
---|
43 | |
---|
44 | AW_HEADER_MAIN |
---|
45 | |
---|
46 | NT_global GLOBAL; |
---|
47 | |
---|
48 | // NT_format_all_alignments may be called after any operation which causes |
---|
49 | // unformatted alignments (e.g importing sequences) |
---|
50 | // |
---|
51 | // It tests all alignments whether they need to be formatted |
---|
52 | // and asks the user if they should be formatted. |
---|
53 | |
---|
54 | GB_ERROR NT_format_all_alignments(GBDATA *gb_main) { |
---|
55 | GB_ERROR err = NULp; |
---|
56 | GB_transaction ta(gb_main); |
---|
57 | GB_topSecurityLevel unsecured(gb_main); |
---|
58 | |
---|
59 | long ali_count = GBT_count_alignments(gb_main); |
---|
60 | if (ali_count) { |
---|
61 | arb_progress progress("Formatting alignments", ali_count); |
---|
62 | err = GBT_check_data(gb_main, NULp); |
---|
63 | |
---|
64 | AW_repeated_question question; |
---|
65 | question.add_help("prompt/format_alignments.hlp"); |
---|
66 | |
---|
67 | GBDATA *gb_presets = GBT_get_presets(gb_main); |
---|
68 | for (GBDATA *gb_ali = GB_entry(gb_presets, "alignment"); |
---|
69 | gb_ali && !err; |
---|
70 | gb_ali = GB_nextEntry(gb_ali)) |
---|
71 | { |
---|
72 | GBDATA *gb_aligned = GB_search(gb_ali, "aligned", GB_INT); |
---|
73 | |
---|
74 | if (GB_read_int(gb_aligned) == 0) { // sequences in alignment are not formatted |
---|
75 | enum FormatAction { |
---|
76 | FA_ASK_USER = 0, // ask user |
---|
77 | FA_FORMAT_ALL = 1, // format automatically w/o asking |
---|
78 | FA_SKIP_ALL = 2, // skip automatically w/o asking |
---|
79 | }; |
---|
80 | FormatAction format_action = FA_ASK_USER; |
---|
81 | GBDATA *gb_ali_name = GB_entry(gb_ali, "alignment_name"); |
---|
82 | const char *ali_name = GB_read_char_pntr(gb_ali_name); |
---|
83 | |
---|
84 | { |
---|
85 | bool is_ali_genom = strcmp(ali_name, GENOM_ALIGNMENT) == 0; |
---|
86 | GBDATA *gb_auto_format = GB_entry(gb_ali, "auto_format"); |
---|
87 | |
---|
88 | if (gb_auto_format) { |
---|
89 | format_action = FormatAction(GB_read_int(gb_auto_format)); |
---|
90 | if (is_ali_genom) { |
---|
91 | if (format_action != FA_SKIP_ALL) { |
---|
92 | format_action = FA_SKIP_ALL; // always skip ali_genom |
---|
93 | err = GB_write_int(gb_auto_format, FA_SKIP_ALL); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | else if (is_ali_genom) { |
---|
98 | format_action = FA_SKIP_ALL; |
---|
99 | err = GBT_write_int(gb_ali, "auto_format", FA_SKIP_ALL); // always skip |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | bool perform_format = false; |
---|
104 | if (!err) { |
---|
105 | switch (format_action) { |
---|
106 | case FA_FORMAT_ALL: perform_format = true; break; |
---|
107 | case FA_SKIP_ALL: perform_format = false; break; |
---|
108 | default: { |
---|
109 | char *qtext = GBS_global_string_copy("Alignment '%s' is not formatted. Format?", ali_name); |
---|
110 | int answer = question.get_answer("format_alignments", qtext, "Format,Skip,Always format,Always skip", "all", false); |
---|
111 | |
---|
112 | switch (answer) { |
---|
113 | case 2: |
---|
114 | err = GBT_write_int(gb_ali, "auto_format", FA_FORMAT_ALL); |
---|
115 | // fall-through |
---|
116 | case 0: |
---|
117 | perform_format = true; |
---|
118 | break; |
---|
119 | |
---|
120 | case 3: |
---|
121 | err = GBT_write_int(gb_ali, "auto_format", FA_SKIP_ALL); |
---|
122 | break; |
---|
123 | } |
---|
124 | |
---|
125 | free(qtext); |
---|
126 | break; |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | if (!err && perform_format) { |
---|
131 | err = ARB_format_alignment(gb_main, ali_name); |
---|
132 | } |
---|
133 | } |
---|
134 | progress.inc_and_check_user_abort(err); |
---|
135 | } |
---|
136 | |
---|
137 | if (err) progress.done(); |
---|
138 | } |
---|
139 | |
---|
140 | err = ta.close(err); |
---|
141 | return err; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | // -------------------------------------------------------------------------------- |
---|
146 | |
---|
147 | |
---|
148 | static GB_ERROR nt_check_database_consistency() { |
---|
149 | // called once on ARB_NTREE startup |
---|
150 | arb_progress cons_progress("Checking consistency"); |
---|
151 | |
---|
152 | GB_ERROR err = NT_format_all_alignments(GLOBAL.gb_main); |
---|
153 | if (!err) err = NT_repair_DB(GLOBAL.gb_main); |
---|
154 | |
---|
155 | return err; |
---|
156 | } |
---|
157 | |
---|
158 | __ATTR__USERESULT static GB_ERROR startup_mainwindow_and_dbserver(AW_root *aw_root, const char *autorun_macro, TREE_canvas*& result_ntw) { |
---|
159 | AWT_initTreeAwarRegistry(GLOBAL.gb_main); |
---|
160 | |
---|
161 | GB_ERROR error = configure_macro_recording(aw_root, "ARB_NT", GLOBAL.gb_main); // @@@ problematic if called from startup-importer |
---|
162 | if (!error) { |
---|
163 | result_ntw = NT_create_main_window(aw_root); |
---|
164 | if (GB_is_server(GLOBAL.gb_main)) { |
---|
165 | error = nt_check_database_consistency(); |
---|
166 | if (!error) NT_repair_userland_problems(); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | if (!error) error = GBK_system("arb_macro_compatibility.sh &"); // run async to avoid (DB) lock problems |
---|
171 | |
---|
172 | if (!error && autorun_macro) execute_macro(aw_root, autorun_macro); // @@@ triggering execution here is ok, but its a bad place to pass 'autorun_macro'. Should be handled more generally |
---|
173 | |
---|
174 | return error; |
---|
175 | } |
---|
176 | |
---|
177 | static ARB_ERROR load_and_startup_main_window(AW_root *aw_root, const char *autorun_macro) { |
---|
178 | char *db_server = aw_root->awar(AWAR_DB_PATH)->read_string(); |
---|
179 | GLOBAL.gb_main = GBT_open(db_server, "rw"); |
---|
180 | |
---|
181 | ARB_ERROR error; |
---|
182 | if (!GLOBAL.gb_main) { |
---|
183 | error = GB_await_error(); |
---|
184 | } |
---|
185 | else { |
---|
186 | aw_root->awar(AWAR_DB_PATH)->write_string(db_server); |
---|
187 | aw_root->awar(AWAR_SAVED_DB_PATH)->write_string(db_server); |
---|
188 | |
---|
189 | #define MAXNAMELEN 35 |
---|
190 | int len = strlen(db_server); |
---|
191 | if (len>MAXNAMELEN) { |
---|
192 | char *nameOnly = strrchr(db_server, '/'); |
---|
193 | if (nameOnly) { |
---|
194 | nameOnly++; |
---|
195 | len -= (nameOnly-db_server); |
---|
196 | memmove(db_server, nameOnly, len+1); |
---|
197 | if (len>MAXNAMELEN) { |
---|
198 | strcpy(db_server+MAXNAMELEN-3, "..."); |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | #if defined(DEBUG) |
---|
203 | AWT_announce_db_to_browser(GLOBAL.gb_main, GBS_global_string("ARB database (%s)", db_server)); |
---|
204 | #endif // DEBUG |
---|
205 | |
---|
206 | TREE_canvas *dummy = NULp; |
---|
207 | GB_ERROR problem = startup_mainwindow_and_dbserver(aw_root, autorun_macro, dummy); |
---|
208 | aw_message_if(problem); // no need to terminate ARB |
---|
209 | } |
---|
210 | |
---|
211 | free(db_server); |
---|
212 | return error; |
---|
213 | } |
---|
214 | |
---|
215 | #define AWAR_DB_FILTER AWAR_DBBASE "/filter" |
---|
216 | #define AWAR_DB_DIRECTORY AWAR_DBBASE "/directory" |
---|
217 | |
---|
218 | static void nt_delete_database(AW_window *aww) { |
---|
219 | AW_root *aw_root = aww->get_root(); |
---|
220 | char *db_server = aw_root->awar(AWAR_DB_PATH)->read_string(); |
---|
221 | |
---|
222 | if (strlen(db_server)) { |
---|
223 | if (aw_ask_sure(NULp, GBS_global_string("Are you sure to delete database %s\nNote: there is no way to undelete it afterwards", db_server))) { |
---|
224 | GB_ERROR error = GB_delete_database(db_server); |
---|
225 | if (error) { |
---|
226 | aw_message(error); |
---|
227 | } |
---|
228 | else { |
---|
229 | aw_root->awar(AWAR_DB_FILTER)->touch(); |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | else { |
---|
234 | aw_message("No database selected"); |
---|
235 | } |
---|
236 | free(db_server); |
---|
237 | } |
---|
238 | |
---|
239 | static void start_main_window_after_import(AW_root *aw_root) { |
---|
240 | GLOBAL.aw_root = aw_root; |
---|
241 | |
---|
242 | GBDATA *gb_imported = AWTI_acquire_imported_DB_and_cleanup_importer(); |
---|
243 | nt_assert(gb_imported == GLOBAL.gb_main); // import-DB should already be used as main-DB |
---|
244 | GLOBAL.gb_main = gb_imported; |
---|
245 | |
---|
246 | TREE_canvas *ntw = NULp; |
---|
247 | aw_message_if(startup_mainwindow_and_dbserver(aw_root, NULp, ntw)); |
---|
248 | |
---|
249 | if (aw_root->awar(AWAR_IMPORT_AUTOCONF)->read_int()) { |
---|
250 | NT_create_config_after_import(ntw, true); |
---|
251 | } |
---|
252 | |
---|
253 | aw_root->awar(AWAR_TREE_REFRESH)->touch(); |
---|
254 | } |
---|
255 | |
---|
256 | static void nt_intro_start_existing(AW_window *aw_intro) { |
---|
257 | aw_intro->hide(); |
---|
258 | ARB_ERROR error = load_and_startup_main_window(aw_intro->get_root(), NULp); |
---|
259 | nt_assert(contradicted(error, got_macro_ability(aw_intro->get_root()))); |
---|
260 | if (error) { |
---|
261 | aw_intro->show(); |
---|
262 | aw_popup_ok(error.deliver()); |
---|
263 | } |
---|
264 | else { |
---|
265 | error.expect_no_error(); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | static void nt_intro_start_merge(AW_window *aw_intro) { |
---|
270 | AW_root *aw_root = aw_intro->get_root(); |
---|
271 | const char *dir = aw_root->awar(AWAR_DB_DIRECTORY)->read_char_pntr(); |
---|
272 | char *merge_args = GBS_global_string_copy("'%s' '%s'", dir, dir); |
---|
273 | |
---|
274 | NT_restart(aw_root, merge_args); // call arb_ntree as merge-tool on exit |
---|
275 | } |
---|
276 | |
---|
277 | static void nt_intro_start_import(AW_window *aw_intro) { |
---|
278 | aw_intro->hide(); |
---|
279 | |
---|
280 | AW_root *aw_root = aw_intro->get_root(); |
---|
281 | aw_root->awar_string(AWAR_DB_PATH)->write_string("noname.arb"); |
---|
282 | |
---|
283 | AWTI_open_import_window(aw_root, NULp, true, NULp, makeRootCallback(start_main_window_after_import)); |
---|
284 | AWTI_set_importDB_pointer(GLOBAL.gb_main); |
---|
285 | |
---|
286 | nt_assert(got_macro_ability(aw_root)); |
---|
287 | } |
---|
288 | |
---|
289 | static AW_window *nt_create_intro_window(AW_root *awr) { |
---|
290 | AW_window_simple *aws = new AW_window_simple; |
---|
291 | aws->init(awr, "ARB_INTRO", "ARB INTRO"); |
---|
292 | aws->load_xfig("arb_intro.fig"); |
---|
293 | |
---|
294 | aws->callback(makeWindowCallback(NT_exit, EXIT_SUCCESS)); |
---|
295 | aws->at("close"); |
---|
296 | aws->create_button("EXIT", "Exit", "x"); |
---|
297 | |
---|
298 | aws->at("help"); |
---|
299 | aws->callback(makeHelpCallback("arb_intro.hlp")); |
---|
300 | aws->create_button("HELP", "HELP", "H"); |
---|
301 | |
---|
302 | AW_create_standard_fileselection(aws, AWAR_DBBASE); |
---|
303 | |
---|
304 | aws->button_length(0); |
---|
305 | |
---|
306 | aws->at("logo"); |
---|
307 | aws->create_button(NULp, "#logo.xpm"); |
---|
308 | |
---|
309 | aws->at("version"); |
---|
310 | aws->create_button(NULp, GBS_global_string("Version " ARB_VERSION "\n(" ARB_BUILD_DATE ")")); // version + date |
---|
311 | |
---|
312 | aws->at("copyright"); |
---|
313 | aws->create_button(NULp, GBS_global_string("(C) 1993-" ARB_BUILD_YEAR)); |
---|
314 | |
---|
315 | aws->at("old"); |
---|
316 | aws->callback(nt_intro_start_existing); |
---|
317 | aws->create_autosize_button("OPEN_SELECTED", "OPEN SELECTED", "O"); |
---|
318 | |
---|
319 | aws->at("del"); |
---|
320 | aws->callback(nt_delete_database); |
---|
321 | aws->create_autosize_button("DELETE_SELECTED", "DELETE SELECTED"); |
---|
322 | |
---|
323 | aws->at("new_complex"); |
---|
324 | aws->callback(nt_intro_start_import); |
---|
325 | aws->create_autosize_button("CREATE_AND_IMPORT", "CREATE AND IMPORT", "I"); |
---|
326 | |
---|
327 | aws->at("merge"); |
---|
328 | aws->callback(nt_intro_start_merge); |
---|
329 | aws->create_autosize_button("MERGE_TWO_DATABASES", "MERGE TWO ARB DATABASES", "O"); |
---|
330 | |
---|
331 | aws->at("expert"); |
---|
332 | aws->create_toggle(AWAR_EXPERT); |
---|
333 | |
---|
334 | return aws; |
---|
335 | } |
---|
336 | |
---|
337 | static void AWAR_DB_PATH_changed_cb(AW_root *awr) { |
---|
338 | static bool avoid_recursion = false; |
---|
339 | |
---|
340 | if (!avoid_recursion) { |
---|
341 | LocallyModify<bool> flag(avoid_recursion, true); |
---|
342 | |
---|
343 | char *value = awr->awar(AWAR_SAVED_DB_PATH)->read_string(); |
---|
344 | char *lslash = strrchr(value, '/'); |
---|
345 | |
---|
346 | char *name = lslash ? lslash+1 : value; |
---|
347 | #if defined(DEBUG) |
---|
348 | printf("writing '%s' to AWAR_DB_NAME\n", name); |
---|
349 | #endif // DEBUG |
---|
350 | awr->awar(AWAR_DB_NAME)->write_string(name); |
---|
351 | |
---|
352 | if (lslash) { // update value of directory |
---|
353 | lslash[0] = 0; |
---|
354 | awr->awar(AWAR_DB_DIRECTORY)->write_string(value); |
---|
355 | lslash[0] = '/'; |
---|
356 | } |
---|
357 | |
---|
358 | free(value); |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | class NtreeCommandLine : virtual Noncopyable { |
---|
363 | int arg_count; |
---|
364 | char const*const* args; |
---|
365 | |
---|
366 | bool help_requested; |
---|
367 | bool do_import; |
---|
368 | |
---|
369 | const char *macro_name; |
---|
370 | |
---|
371 | public: |
---|
372 | NtreeCommandLine(int argc_, char const*const* argv_) |
---|
373 | : arg_count(argc_-1), |
---|
374 | args(argv_+1), |
---|
375 | help_requested(false), |
---|
376 | do_import(false), |
---|
377 | macro_name(NULp) |
---|
378 | {} |
---|
379 | |
---|
380 | void shift() { ++args; --arg_count; } |
---|
381 | |
---|
382 | int free_args() const { return arg_count; } |
---|
383 | const char *get_arg(int num) const { return num<arg_count ? args[num] : NULp; } |
---|
384 | |
---|
385 | bool wants_help() const { return help_requested; } |
---|
386 | bool wants_import() const { return do_import; } |
---|
387 | bool wants_merge() const { return arg_count == 2; } |
---|
388 | |
---|
389 | const char *autorun_macro() const { return macro_name; } |
---|
390 | |
---|
391 | void print_help(FILE *out) const { |
---|
392 | fprintf(out, |
---|
393 | "\n" |
---|
394 | "arb_ntree version " ARB_VERSION_DETAILED "\n" |
---|
395 | "(C) 1993-" ARB_BUILD_YEAR " The ARB-project\n" |
---|
396 | "http://www.arb-home.de/\n" |
---|
397 | "(version build by: " ARB_BUILD_USER "@" ARB_BUILD_HOST ")\n" |
---|
398 | "\n" |
---|
399 | "Possible usage:\n" |
---|
400 | " arb_ntree => start ARB (intro)\n" |
---|
401 | " arb_ntree DB => start ARB with DB\n" |
---|
402 | " arb_ntree DB1 DB2 => merge from DB1 into DB2\n" |
---|
403 | " arb_ntree --import FILE => import FILE into new database (FILE may be a filemask)\n" |
---|
404 | "\n" |
---|
405 | "Additional arguments possible with command lines above:\n" |
---|
406 | " --execute macroname => execute macro 'macroname' after startup\n" |
---|
407 | "\n" |
---|
408 | "Each DB argument may be one of the following:\n" |
---|
409 | " database.arb -> use existing or new database\n" |
---|
410 | " \":\" -> connect to database of a running instance of ARB\n" |
---|
411 | " directory -> prompt for databases inside directory\n" |
---|
412 | " filemask -> also prompt for DB, but more specific (e.g. \"prot*.arb\")\n" |
---|
413 | "\n" |
---|
414 | ); |
---|
415 | } |
---|
416 | |
---|
417 | ARB_ERROR parse() { |
---|
418 | ARB_ERROR error; |
---|
419 | |
---|
420 | while (!error && arg_count>0 && args[0][0] == '-') { |
---|
421 | const char *opt = args[0]+1; |
---|
422 | if (opt[0] == '-') ++opt; // accept '-' or '--' |
---|
423 | if (strcmp(opt, "help") == 0 || strcmp(opt, "h") == 0) { help_requested = true; } |
---|
424 | else if (strcmp(opt, "execute") == 0) { shift(); macro_name = *args; } |
---|
425 | else if (strcmp(opt, "import") == 0) { do_import = true; } |
---|
426 | |
---|
427 | // bunch of test switches to provoke various ways to terminate (see also http://bugs.arb-home.de/ticket/538) |
---|
428 | else if (strcmp(opt, "crash") == 0) { GBK_terminate("crash requested"); } |
---|
429 | else if (strcmp(opt, "trap") == 0) { arb_assert(0); } |
---|
430 | else if (strcmp(opt, "sighup") == 0) { raise(SIGHUP); } |
---|
431 | else if (strcmp(opt, "sigsegv") == 0) { raise(SIGSEGV); } |
---|
432 | else if (strcmp(opt, "sigterm") == 0) { raise(SIGTERM); } |
---|
433 | else if (strcmp(opt, "fail") == 0) { exit(EXIT_FAILURE); } |
---|
434 | else if (strcmp(opt, "exit") == 0) { exit(EXIT_SUCCESS); } |
---|
435 | // end of test switches ---------------------------------------- |
---|
436 | |
---|
437 | else error = GBS_global_string("Unknown switch '%s'", args[0]); |
---|
438 | shift(); |
---|
439 | } |
---|
440 | // non-switch arguments remain in arg_count/args |
---|
441 | if (!error) { |
---|
442 | if (do_import && arg_count != 1) error = "expected exactly one file-name or file-mask behind --import"; |
---|
443 | else if (arg_count>2) error = "too many stray arguments given (max. 2 accepted)"; |
---|
444 | } |
---|
445 | return error; |
---|
446 | } |
---|
447 | }; |
---|
448 | |
---|
449 | enum ArgType { // args that might be specified on command line (for DB or FILE; see above) |
---|
450 | RUNNING_DB, |
---|
451 | DIRECTORY, |
---|
452 | EXISTING_DB, |
---|
453 | NEW_DB, |
---|
454 | FILEMASK, |
---|
455 | EXISTING_FILE, |
---|
456 | UNKNOWN_ARG, |
---|
457 | }; |
---|
458 | |
---|
459 | inline bool has_arb_suffix(const char *arg) { |
---|
460 | const char *suffix = strstr(arg, ".arb"); |
---|
461 | if (suffix) { |
---|
462 | return strcmp(suffix, ".arb") == 0; |
---|
463 | } |
---|
464 | return false; |
---|
465 | } |
---|
466 | |
---|
467 | static ArgType detectArgType(const char *arg) { |
---|
468 | if (strcmp(arg, ":") == 0) return RUNNING_DB; |
---|
469 | if (GB_is_directory(arg)) return DIRECTORY; |
---|
470 | if (strpbrk(arg, "*?")) return FILEMASK; |
---|
471 | if (has_arb_suffix(arg)) { |
---|
472 | return GB_is_regularfile(arg) ? EXISTING_DB : NEW_DB; |
---|
473 | } |
---|
474 | |
---|
475 | GB_ERROR load_file_err = GBT_check_arb_file(arg); |
---|
476 | if (!load_file_err) return EXISTING_DB; |
---|
477 | |
---|
478 | return GB_is_regularfile(arg) ? EXISTING_FILE : UNKNOWN_ARG; |
---|
479 | } |
---|
480 | |
---|
481 | enum RunMode { NORMAL, IMPORT, MERGE, BROWSE }; |
---|
482 | |
---|
483 | #define ABORTED_BY_USER "aborted by user" |
---|
484 | |
---|
485 | static ARB_ERROR check_argument_for_mode(const char *database, char *&browser_startdir, RunMode& mode) { |
---|
486 | // Check whether 'database' is a |
---|
487 | // - ARB database |
---|
488 | // - directory name |
---|
489 | // - file to import |
---|
490 | // |
---|
491 | // Modify 'mode' occordingly. |
---|
492 | // Set 'browser_startdir' |
---|
493 | |
---|
494 | ARB_ERROR error; |
---|
495 | if (mode != IMPORT) { |
---|
496 | if (!database) mode = BROWSE; |
---|
497 | else { |
---|
498 | GB_ERROR load_file_err = GBT_check_arb_file(database); |
---|
499 | if (load_file_err) { |
---|
500 | char *full_path = AW_unfold_path("PWD", database); |
---|
501 | |
---|
502 | enum Action { LOAD_DB, CONVERT_DB, BROWSE_DB, EXIT, NOIDEA }; |
---|
503 | Action action = NOIDEA; |
---|
504 | if (GB_is_directory(full_path)) { |
---|
505 | action = BROWSE_DB; |
---|
506 | } |
---|
507 | else { |
---|
508 | if (!GB_is_regularfile(full_path)) { |
---|
509 | const char *msg = GBS_global_string("'%s' is neither a known option nor a legal file- or directory-name.\n(Error: %s)", |
---|
510 | full_path, load_file_err); |
---|
511 | |
---|
512 | int ans = aw_question(NULp, msg, "Browser,Exit"); |
---|
513 | action = ans ? EXIT : BROWSE_DB; |
---|
514 | } |
---|
515 | else { |
---|
516 | const char *msg = GBS_global_string("Your file is not an original arb file\n(%s)", load_file_err); |
---|
517 | action = (Action)aw_question(NULp, msg, "Continue (dangerous),Start Converter,Browser,Exit"); |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | switch (action) { |
---|
522 | case CONVERT_DB: mode = IMPORT; break; |
---|
523 | case LOAD_DB: break; |
---|
524 | case NOIDEA: nt_assert(0); FALLTHROUGH; // in NDEBUG |
---|
525 | case EXIT: error = ABORTED_BY_USER; break; |
---|
526 | case BROWSE_DB: { |
---|
527 | char *dir = nulldup(full_path); |
---|
528 | while (dir && !GB_is_directory(dir)) freeset(dir, AW_extract_directory(dir)); |
---|
529 | |
---|
530 | if (dir) { |
---|
531 | nt_assert(GB_is_directory(dir)); |
---|
532 | reassign(browser_startdir, dir); |
---|
533 | mode = BROWSE; |
---|
534 | } |
---|
535 | free(dir); |
---|
536 | break; |
---|
537 | } |
---|
538 | |
---|
539 | } |
---|
540 | free(full_path); |
---|
541 | } |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | return error; |
---|
546 | } |
---|
547 | |
---|
548 | class SelectedDatabase : virtual Noncopyable { |
---|
549 | GBDATA*& gb_main; |
---|
550 | char *name; |
---|
551 | ArgType type; |
---|
552 | char *role; |
---|
553 | |
---|
554 | void fix_name() { |
---|
555 | if (type != RUNNING_DB && name[0]) { |
---|
556 | const char *changed; |
---|
557 | |
---|
558 | if (type == NEW_DB) { |
---|
559 | changed = GB_unfold_in_directory(GB_getcwd(), name); |
---|
560 | } |
---|
561 | else { |
---|
562 | changed = GB_canonical_path(name); |
---|
563 | } |
---|
564 | |
---|
565 | if (strcmp(name, changed) != 0) { |
---|
566 | reselect_file(changed); |
---|
567 | } |
---|
568 | } |
---|
569 | } |
---|
570 | |
---|
571 | public: |
---|
572 | SelectedDatabase(GBDATA*& gb_main_, const char *name_, const char *role_) |
---|
573 | : gb_main(gb_main_), |
---|
574 | name(ARB_strdup(name_)), |
---|
575 | type(detectArgType(name)), |
---|
576 | role(ARB_strdup(role_)) |
---|
577 | { |
---|
578 | fix_name(); |
---|
579 | } |
---|
580 | ~SelectedDatabase() { |
---|
581 | free(role); |
---|
582 | free(name); |
---|
583 | } |
---|
584 | |
---|
585 | ArgType arg_type() const { return type; } |
---|
586 | |
---|
587 | bool needs_to_prompt() const { |
---|
588 | return type == DIRECTORY || type == FILEMASK || type == UNKNOWN_ARG; |
---|
589 | } |
---|
590 | |
---|
591 | const char *get_fullname() const { return name; } |
---|
592 | const char *get_nameonly() const { |
---|
593 | const char *slash = strrchr(name, '/'); |
---|
594 | return slash ? slash+1 : name; |
---|
595 | } |
---|
596 | const char *get_role() const { return role; } |
---|
597 | const char *get_description() const { return GBS_global_string("%s (%s)", get_role(), get_nameonly()); } |
---|
598 | |
---|
599 | ArgType get_type() const { return type; } |
---|
600 | |
---|
601 | const char *get_dir() const { |
---|
602 | const char *dir = NULp; |
---|
603 | switch (type) { |
---|
604 | case DIRECTORY: |
---|
605 | dir = get_fullname(); |
---|
606 | break; |
---|
607 | |
---|
608 | case NEW_DB: |
---|
609 | case FILEMASK: |
---|
610 | case EXISTING_FILE: |
---|
611 | case EXISTING_DB: { |
---|
612 | char *dir_copy; |
---|
613 | GB_split_full_path(name, &dir_copy, NULp, NULp, NULp); |
---|
614 | |
---|
615 | static SmartCharPtr dir_store; |
---|
616 | dir_store = dir_copy; |
---|
617 | |
---|
618 | dir = dir_copy; |
---|
619 | break; |
---|
620 | } |
---|
621 | case UNKNOWN_ARG: |
---|
622 | case RUNNING_DB: |
---|
623 | dir = "."; |
---|
624 | break; |
---|
625 | } |
---|
626 | return dir; |
---|
627 | } |
---|
628 | |
---|
629 | const char *get_mask() const { |
---|
630 | const char *mask; |
---|
631 | switch (type) { |
---|
632 | case FILEMASK: { |
---|
633 | char *mask_copy; |
---|
634 | GB_split_full_path(name, NULp, &mask_copy, NULp, NULp); |
---|
635 | |
---|
636 | static SmartCharPtr mask_store; |
---|
637 | mask_store = mask_copy; |
---|
638 | |
---|
639 | mask = mask_copy; |
---|
640 | break; |
---|
641 | } |
---|
642 | default: |
---|
643 | mask = ".arb"; |
---|
644 | break; |
---|
645 | } |
---|
646 | return mask; |
---|
647 | } |
---|
648 | |
---|
649 | void reselect_file(const char *file) { |
---|
650 | freedup(name, file); |
---|
651 | type = detectArgType(name); |
---|
652 | fix_name(); |
---|
653 | } |
---|
654 | void reselect_from_awar(AW_root *aw_root, const char *awar_name) { |
---|
655 | if (awar_name) { |
---|
656 | const char *file = aw_root->awar(awar_name)->read_char_pntr(); |
---|
657 | if (file) reselect_file(file); |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | GB_ERROR open_db_for_merge(bool is_source_db); |
---|
662 | void close_db() { if (gb_main) GB_close(gb_main); } |
---|
663 | }; |
---|
664 | |
---|
665 | GB_ERROR SelectedDatabase::open_db_for_merge(bool is_source_db) { |
---|
666 | GB_ERROR error = NULp; |
---|
667 | const char *openMode = "rw"; |
---|
668 | |
---|
669 | switch (get_type()) { |
---|
670 | case DIRECTORY: |
---|
671 | case FILEMASK: |
---|
672 | GBK_terminate("Program logic error (should have been prompted for explicit DB name)"); |
---|
673 | break; |
---|
674 | |
---|
675 | case UNKNOWN_ARG: |
---|
676 | case EXISTING_FILE: |
---|
677 | error = GBS_global_string("'%s' is no arb-database", get_fullname()); |
---|
678 | break; |
---|
679 | |
---|
680 | case NEW_DB: |
---|
681 | if (is_source_db) { |
---|
682 | error = GBS_global_string("'%s' has to exist", get_fullname()); |
---|
683 | break; |
---|
684 | } |
---|
685 | openMode = "crw"; // allow to create DB |
---|
686 | break; |
---|
687 | |
---|
688 | case EXISTING_DB: |
---|
689 | case RUNNING_DB: |
---|
690 | break; |
---|
691 | } |
---|
692 | |
---|
693 | if (!error) { |
---|
694 | gb_main = GBT_open(get_fullname(), openMode); |
---|
695 | if (!gb_main) error = GB_await_error(); |
---|
696 | else { |
---|
697 | IF_DEBUG(AWT_announce_db_to_browser(gb_main, get_description())); |
---|
698 | } |
---|
699 | } |
---|
700 | |
---|
701 | return error; |
---|
702 | } |
---|
703 | |
---|
704 | struct merge_scheme : virtual Noncopyable { |
---|
705 | SelectedDatabase *src; |
---|
706 | SelectedDatabase *dst; |
---|
707 | |
---|
708 | char *awar_src; |
---|
709 | char *awar_dst; |
---|
710 | |
---|
711 | GB_ERROR error; |
---|
712 | |
---|
713 | merge_scheme(SelectedDatabase *src_, SelectedDatabase *dst_) |
---|
714 | : src(src_), |
---|
715 | dst(dst_), |
---|
716 | awar_src(NULp), |
---|
717 | awar_dst(NULp), |
---|
718 | error(NULp) |
---|
719 | {} |
---|
720 | ~merge_scheme() { |
---|
721 | if (error) { |
---|
722 | src->close_db(); |
---|
723 | dst->close_db(); |
---|
724 | } |
---|
725 | delete src; |
---|
726 | delete dst; |
---|
727 | free(awar_src); |
---|
728 | free(awar_dst); |
---|
729 | } |
---|
730 | |
---|
731 | |
---|
732 | void open_dbs() { |
---|
733 | if (!error) error = src->open_db_for_merge(true); |
---|
734 | if (!error) error = dst->open_db_for_merge(false); |
---|
735 | } |
---|
736 | |
---|
737 | void fix_dst(AW_root *aw_root) { dst->reselect_from_awar(aw_root, awar_dst); } |
---|
738 | void fix_src(AW_root *aw_root) { src->reselect_from_awar(aw_root, awar_src); } |
---|
739 | |
---|
740 | bool knows_dbs() const { return !src->needs_to_prompt() && !dst->needs_to_prompt(); } |
---|
741 | }; |
---|
742 | |
---|
743 | static bool merge_tool_running_as_client = true; // default to safe state (true avoids call of 'arb_clean' at exit) |
---|
744 | static void exit_from_merge(const char *restart_args) { |
---|
745 | if (merge_tool_running_as_client) { // there is a main ARB running |
---|
746 | if (restart_args) NT_start(restart_args, true); |
---|
747 | exit(EXIT_SUCCESS); // exit w/o killing clients (in contrast to NT_exit()) |
---|
748 | } |
---|
749 | else { |
---|
750 | NT_restart(AW_root::SINGLETON, null2empty(restart_args)); |
---|
751 | nt_assert(0); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | static void merge_startup_abort_cb(AW_window*) { |
---|
756 | fputs("Error: merge aborted by user\n", stderr); |
---|
757 | exit_from_merge(NULp); |
---|
758 | } |
---|
759 | |
---|
760 | static AW_window *merge_startup_error_window(AW_root *aw_root, GB_ERROR error) { |
---|
761 | AW_window_simple *aw_msg = new AW_window_simple; |
---|
762 | |
---|
763 | aw_msg->init(aw_root, "arb_merge_error", "ARB merge error"); |
---|
764 | aw_msg->recalc_size_atShow(AW_RESIZE_DEFAULT); // force size recalc (ignores user size) |
---|
765 | |
---|
766 | aw_msg->at(10, 10); |
---|
767 | aw_msg->auto_space(10, 10); |
---|
768 | |
---|
769 | aw_msg->create_autosize_button(NULp, error, "", 2); |
---|
770 | aw_msg->at_newline(); |
---|
771 | |
---|
772 | aw_msg->callback(merge_startup_abort_cb); |
---|
773 | aw_msg->create_autosize_button("OK", "Ok", "O", 2); |
---|
774 | |
---|
775 | aw_msg->window_fit(); |
---|
776 | |
---|
777 | return aw_msg; |
---|
778 | } |
---|
779 | static AW_window *startup_merge_main_window(AW_root *aw_root, merge_scheme *ms) { |
---|
780 | ms->fix_src(aw_root); |
---|
781 | ms->fix_dst(aw_root); |
---|
782 | |
---|
783 | if (ms->knows_dbs()) { |
---|
784 | ms->open_dbs(); |
---|
785 | } |
---|
786 | else { |
---|
787 | ms->error = "Need two databases to merge"; |
---|
788 | } |
---|
789 | |
---|
790 | AW_window *aw_result; |
---|
791 | if (!ms->error) { |
---|
792 | MERGE_create_db_file_awars(aw_root, AW_ROOT_DEFAULT, ms->src->get_fullname(), ms->dst->get_fullname()); |
---|
793 | bool dst_is_new = ms->dst->arg_type() == NEW_DB; |
---|
794 | aw_result = MERGE_create_main_window(aw_root, dst_is_new, exit_from_merge); |
---|
795 | |
---|
796 | nt_assert(got_macro_ability(aw_root)); |
---|
797 | } |
---|
798 | else { |
---|
799 | aw_result = merge_startup_error_window(aw_root, ms->error); |
---|
800 | } |
---|
801 | delete ms; // was allocated in startup_gui() |
---|
802 | return aw_result; |
---|
803 | } |
---|
804 | |
---|
805 | static AW_window *startup_merge_prompting_for_nonexplicit_dst_db(AW_root *aw_root, merge_scheme *ms) { |
---|
806 | AW_window *aw_result; |
---|
807 | if (ms->dst->needs_to_prompt()) { |
---|
808 | aw_result = awt_create_load_box(aw_root, "Select", ms->dst->get_role(), |
---|
809 | ms->dst->get_dir(), ms->dst->get_mask(), |
---|
810 | &(ms->awar_dst), |
---|
811 | AW_window::makeWindowReplacer(makeCreateWindowCallback(startup_merge_main_window, ms)), |
---|
812 | makeWindowCallback(merge_startup_abort_cb), "Cancel"); |
---|
813 | } |
---|
814 | else { |
---|
815 | aw_result = startup_merge_main_window(aw_root, ms); |
---|
816 | } |
---|
817 | return aw_result; |
---|
818 | } |
---|
819 | |
---|
820 | static AW_window *startup_merge_prompting_for_nonexplicit_dbs(AW_root *aw_root, merge_scheme *ms) { |
---|
821 | // if src_spec or dst_spec needs to be prompted for -> startup prompters with callbacks bound to continue |
---|
822 | // otherwise just startup merge |
---|
823 | |
---|
824 | AW_window *aw_result; |
---|
825 | if (ms->src->needs_to_prompt()) { |
---|
826 | aw_result = awt_create_load_box(aw_root, "Select", ms->src->get_role(), |
---|
827 | ms->src->get_dir(), ms->src->get_mask(), |
---|
828 | &(ms->awar_src), |
---|
829 | AW_window::makeWindowReplacer(makeCreateWindowCallback(startup_merge_prompting_for_nonexplicit_dst_db, ms)), |
---|
830 | makeWindowCallback(merge_startup_abort_cb), "Cancel"); |
---|
831 | } |
---|
832 | else { |
---|
833 | aw_result = startup_merge_prompting_for_nonexplicit_dst_db(aw_root, ms); |
---|
834 | } |
---|
835 | return aw_result; |
---|
836 | } |
---|
837 | |
---|
838 | static void startup_gui(NtreeCommandLine& cl, ARB_ERROR& error) { |
---|
839 | { |
---|
840 | char *message = ARB_strdup(GB_path_in_ARBLIB("message")); |
---|
841 | char *stamp = ARB_strdup(GB_path_in_arbprop("msgtime")); |
---|
842 | if (GB_time_of_file(message)>GB_time_of_file(stamp)) { |
---|
843 | AW_edit(message); |
---|
844 | AWT_system_cb(GBS_global_string("touch %s", stamp)); |
---|
845 | } |
---|
846 | free(stamp); |
---|
847 | free(message); |
---|
848 | } |
---|
849 | |
---|
850 | // create some early awars |
---|
851 | // Note: normally you don't like to add your awar-init-function here, but into nt_create_all_awars() |
---|
852 | |
---|
853 | AW_root* aw_root = GLOBAL.aw_root; |
---|
854 | |
---|
855 | AW_create_fileselection_awars(aw_root, AWAR_DBBASE, "", ".arb", "noname.arb"); // creates AWAR_DB_PATH |
---|
856 | aw_root->awar_string(AWAR_DB_NAME, "noname.arb"); |
---|
857 | |
---|
858 | aw_root->awar_int(AWAR_EXPERT, 0); |
---|
859 | |
---|
860 | AWT_install_cb_guards(); |
---|
861 | |
---|
862 | ARB_declare_global_awars(aw_root, AW_ROOT_DEFAULT); |
---|
863 | aw_root->awar(AWAR_SAVED_DB_PATH)->add_callback(AWAR_DB_PATH_changed_cb); |
---|
864 | |
---|
865 | aw_root->setUserActionTracker(new NullTracker); // no macro recording inside prompters that may popup |
---|
866 | if (!error) { |
---|
867 | nt_assert(!cl.wants_help()); |
---|
868 | |
---|
869 | RunMode mode = NORMAL; |
---|
870 | |
---|
871 | if (cl.wants_merge()) mode = MERGE; |
---|
872 | else if (cl.wants_import()) mode = IMPORT; |
---|
873 | |
---|
874 | if (mode == MERGE) { |
---|
875 | MERGE_create_all_awars(aw_root, AW_ROOT_DEFAULT); |
---|
876 | |
---|
877 | merge_scheme *ms = new merge_scheme( // freed in startup_merge_main_window() |
---|
878 | new SelectedDatabase(GLOBAL_gb_src, cl.get_arg(0), "Source DB for merge"), |
---|
879 | new SelectedDatabase(GLOBAL_gb_dst, cl.get_arg(1), "Destination DB for merge")); |
---|
880 | |
---|
881 | merge_tool_running_as_client = ms->src->arg_type() == RUNNING_DB || ms->dst->arg_type() == RUNNING_DB; |
---|
882 | |
---|
883 | if (ms->src->arg_type() == RUNNING_DB && ms->dst->arg_type() == RUNNING_DB) { |
---|
884 | error = "You cannot merge from running to running DB"; |
---|
885 | } |
---|
886 | else { |
---|
887 | aw_root->setUserActionTracker(new NullTracker); // no macro recording during startup of merge tool (file prompts) |
---|
888 | AW_window *aww = startup_merge_prompting_for_nonexplicit_dbs(aw_root, ms); |
---|
889 | |
---|
890 | nt_assert(contradicted(aww, error)); |
---|
891 | |
---|
892 | if (!error) { |
---|
893 | aww->show(); |
---|
894 | aw_root->main_loop(); |
---|
895 | nt_assert(0); |
---|
896 | } |
---|
897 | } |
---|
898 | } |
---|
899 | else { |
---|
900 | const char *database = NULp; |
---|
901 | char *browser_startdir = ARB_strdup("."); |
---|
902 | |
---|
903 | if (cl.free_args() > 0) database = cl.get_arg(0); |
---|
904 | |
---|
905 | error = check_argument_for_mode(database, browser_startdir, mode); |
---|
906 | if (!error) { |
---|
907 | if (mode == IMPORT) { |
---|
908 | AWTI_open_import_window(aw_root, database, true, NULp, makeRootCallback(start_main_window_after_import)); |
---|
909 | AWTI_set_importDB_pointer(GLOBAL.gb_main); |
---|
910 | |
---|
911 | nt_assert(got_macro_ability(aw_root)); |
---|
912 | aw_root->main_loop(); |
---|
913 | } |
---|
914 | else if (mode == NORMAL) { |
---|
915 | aw_root->awar(AWAR_DB_PATH)->write_string(database); |
---|
916 | error = load_and_startup_main_window(aw_root, cl.autorun_macro()); |
---|
917 | if (!error) { |
---|
918 | nt_assert(got_macro_ability(aw_root)); |
---|
919 | aw_root->main_loop(); |
---|
920 | } |
---|
921 | } |
---|
922 | else if (mode == BROWSE) { |
---|
923 | aw_root->awar(AWAR_DB_DIRECTORY)->write_string(browser_startdir); |
---|
924 | char *latest = GB_find_latest_file(browser_startdir, "/\\.(arb|a[0-9]{2})$/"); |
---|
925 | if (latest) { |
---|
926 | int l = strlen(latest); |
---|
927 | strcpy(latest+l-3, "arb"); |
---|
928 | aw_root->awar(AWAR_DB_PATH)->write_string(latest); |
---|
929 | free(latest); |
---|
930 | } |
---|
931 | nt_create_intro_window(aw_root)->show(); |
---|
932 | aw_root->setUserActionTracker(new NullTracker); // no macro recording inside intro window |
---|
933 | aw_root->main_loop(); |
---|
934 | } |
---|
935 | } |
---|
936 | free(browser_startdir); |
---|
937 | } |
---|
938 | } |
---|
939 | |
---|
940 | if (error) { |
---|
941 | const char *msg = error.preserve(); |
---|
942 | if (strcmp(msg, ABORTED_BY_USER) != 0) aw_popup_ok(msg); |
---|
943 | } |
---|
944 | } |
---|
945 | |
---|
946 | int ARB_main(int argc, char *argv[]) { |
---|
947 | { |
---|
948 | // i really dont want 'arb_ntree --help' to startup the GUI |
---|
949 | // hack: parse CL twice |
---|
950 | NtreeCommandLine cl(argc, argv); |
---|
951 | bool cl_ok = !cl.parse().deliver(); |
---|
952 | if (cl_ok) { |
---|
953 | if (cl.wants_help()) { |
---|
954 | cl.print_help(stderr); |
---|
955 | return EXIT_SUCCESS; |
---|
956 | } |
---|
957 | } |
---|
958 | } |
---|
959 | |
---|
960 | aw_initstatus(); |
---|
961 | GB_set_verbose(); |
---|
962 | |
---|
963 | GB_shell shell; |
---|
964 | AW_root *aw_root = AWT_create_root("ntree.arb", "ARB_NT", need_macro_ability()); |
---|
965 | |
---|
966 | GLOBAL.aw_root = aw_root; |
---|
967 | |
---|
968 | NtreeCommandLine cl(argc, argv); |
---|
969 | ARB_ERROR error = cl.parse(); |
---|
970 | |
---|
971 | if (!error) { |
---|
972 | if (cl.wants_help()) { |
---|
973 | cl.print_help(stderr); |
---|
974 | } |
---|
975 | else { |
---|
976 | ModRLimit increase_stacksize(RLIMIT_STACK, TREEDISP_STACKSIZE); |
---|
977 | startup_gui(cl, error); |
---|
978 | } |
---|
979 | } |
---|
980 | |
---|
981 | int exitcode = EXIT_SUCCESS; |
---|
982 | if (error) { |
---|
983 | exitcode = EXIT_FAILURE; |
---|
984 | fprintf(stderr, "arb_ntree: Error: %s\n", error.deliver()); |
---|
985 | } |
---|
986 | else { |
---|
987 | error.expect_no_error(); |
---|
988 | } |
---|
989 | |
---|
990 | delete aw_root; |
---|
991 | |
---|
992 | return exitcode; |
---|
993 | } |
---|
994 | |
---|