int gt_lua_set_modules_path(lua_State *L, GtError *err) { GtStr *modules_path = NULL, *external_modules_path = NULL, *package_path = NULL; int had_err = 0; gt_error_check(err); gt_assert(L); if (!(modules_path = gt_get_gtdata_path(gt_error_get_progname(err), err))) had_err = -1; if (!had_err) { external_modules_path = gt_str_clone(modules_path); gt_str_append_cstr(modules_path, "/modules/?.lua"); gt_str_append_cstr(external_modules_path, "/modules/external/?.lua"); lua_getglobal(L, "package"); gt_assert(lua_istable(L, -1)); lua_getfield(L, -1, "path"); gt_assert(lua_isstring(L, -1)); package_path = gt_str_new_cstr(lua_tostring(L, -1)); lua_pop(L, 1); gt_str_append_char(package_path, ';'); gt_str_append_str(package_path, modules_path); gt_str_append_char(package_path, ';'); gt_str_append_str(package_path, external_modules_path); lua_pushstring(L, gt_str_get(package_path)); lua_setfield(L, -2, "path"); lua_pop(L, 1); } gt_str_delete(package_path); gt_str_delete(modules_path); gt_str_delete(external_modules_path); return had_err; }
static int gt_condenser_runner(int argc, const char **argv, int parsed_args, void *tool_arguments, GT_UNUSED GtError *err) { GtToolbox *condenser_toolbox = tool_arguments; GtTool *tool; int had_err = 0; char **nargv = NULL; gt_error_check(err); gt_assert(condenser_toolbox); /* get condenser tools */ tool = gt_toolbox_get_tool(condenser_toolbox, argv[parsed_args]); if (!tool) { gt_error_set(err, "condenser tool '%s' not found; option -help lists " "possible tools", argv[parsed_args]); had_err = -1; } /* call condenser tool */ if (!had_err) { gt_assert(tool); nargv = gt_cstr_array_prefix_first(argv + parsed_args, gt_error_get_progname(err)); gt_error_set_progname(err, nargv[0]); had_err = gt_tool_run(tool, argc - parsed_args, (const char**) nargv, err); } /* free */ gt_cstr_array_delete(nargv); return had_err; }
static GtStr* get_xrf_path(GtError *err) { const char *progname; GtStr *xrf_path, *prog; gt_error_check(err); progname = gt_error_get_progname(err); gt_assert(progname != NULL); prog = gt_str_new(); gt_str_append_cstr_nt(prog, progname, gt_cstr_length_up_to_char(progname, ' ')); xrf_path = gt_get_gtdata_path(gt_str_get(prog), err); if (xrf_path) gt_str_append_cstr(xrf_path, "/xrf_abbr/"); gt_str_delete(prog); return xrf_path; }
GtAlphabet* gt_alphabet_new_from_file_no_suffix(const char *filename, GtError *err) { GtStr *transpath = NULL; bool haserr = false; GtAlphabet *alpha; gt_assert(filename); gt_error_check(err); alpha = gt_alphabet_new_empty(); if (!gt_file_exists(filename)) { GtStr *prog; const char *progname = gt_error_get_progname(err); gt_assert(progname != NULL); prog = gt_str_new(); gt_str_append_cstr_nt(prog, progname, gt_cstr_length_up_to_char(progname, ' ')); transpath = gt_get_gtdata_path(gt_str_get(prog), err); gt_str_delete(prog); gt_str_append_cstr(transpath, "/trans/"); gt_str_append_cstr(transpath, filename); } if (read_symbolmap(alpha, transpath == NULL ? filename : gt_str_get(transpath), err) != 0) { haserr = true; } gt_str_delete(transpath); if (alpha == NULL) { haserr = true; } if (haserr) { gt_alphabet_delete((GtAlphabet*) alpha); return NULL; } return alpha; }
int gt_tooldriver(int(*tool)(int argc, const char **argv, GtError*), int argc, char *argv[]) { GtError *err; int had_err; gt_allocators_init(); err = gt_error_new(); gt_error_set_progname(err, argv[0]); had_err = tool(argc, (const char**) argv, err); if (gt_error_is_set(err)) { fprintf(stderr, "%s: error: %s\n", gt_error_get_progname(err), gt_error_get(err)); gt_assert(had_err); } gt_error_delete(err); if (gt_allocators_clean()) return 2; /* programmer error */ if (had_err) return EXIT_FAILURE; return EXIT_SUCCESS; }
static int gt_cge_runner(int argc, const char **argv, int parsed_args, void *tool_arguments, GtError *err) { GtToolbox *index_toolbox = tool_arguments; GtToolfunc toolfunc; GtTool *tool = NULL; char **nargv = NULL; int had_err = 0; gt_error_check(err); gt_assert(index_toolbox != NULL); /* determine tool */ if (!gt_toolbox_has_tool(index_toolbox, argv[parsed_args])) { gt_error_set(err, "congruence tool '%s' not found; option -help lists " "possible tools", argv[parsed_args]); had_err = -1; } /* call sub-tool */ if (!had_err) { if (!(toolfunc = gt_toolbox_get(index_toolbox, argv[parsed_args]))) { tool = gt_toolbox_get_tool(index_toolbox, argv[parsed_args]); gt_assert(tool != NULL); } nargv = gt_cstr_array_prefix_first(argv + parsed_args, gt_error_get_progname(err)); gt_error_set_progname(err, nargv[0]); if (toolfunc != NULL) had_err = toolfunc(argc - parsed_args, (const char**) nargv, err); else had_err = gt_tool_run(tool, argc - parsed_args, (const char**) nargv, err); } gt_cstr_array_delete(nargv); return had_err; }
static int gt_dev_runner(int argc, const char **argv, int parsed_args, void *tool_arguments, GtError *err) { GtToolbox *dev_toolbox = tool_arguments; GtToolfunc toolfunc; GtTool *tool = NULL; int had_err = 0; char **nargv = NULL; gt_error_check(err); gt_assert(dev_toolbox); /* get development tools */ if (!gt_toolbox_has_tool(dev_toolbox, argv[parsed_args])) { gt_error_set(err, "development tool '%s' not found; option -help lists " "possible tools", argv[parsed_args]); had_err = -1; } /* call development tool */ if (!had_err) { if (!(toolfunc = gt_toolbox_get(dev_toolbox, argv[parsed_args]))) { tool = gt_toolbox_get_tool(dev_toolbox, argv[parsed_args]); gt_assert(tool); } nargv = gt_cstr_array_prefix_first(argv + parsed_args, gt_error_get_progname(err)); gt_error_set_progname(err, nargv[0]); if (toolfunc) had_err = toolfunc(argc - parsed_args, (const char**) nargv, err); else had_err = gt_tool_run(tool, argc - parsed_args, (const char**) nargv, err); } /* free */ gt_cstr_array_delete(nargv); return had_err; }
int gt_toolobjdriver(GtToolConstructor tool_constructor, int argc, char *argv[]) { GtTool *tool; GtError *err; int had_err; gt_allocators_init(); err = gt_error_new(); gt_error_set_progname(err, argv[0]); tool = tool_constructor(); had_err = gt_tool_run(tool, argc, (const char**) argv, err); gt_tool_delete(tool); if (gt_error_is_set(err)) { fprintf(stderr, "%s: error: %s\n", gt_error_get_progname(err), gt_error_get(err)); gt_assert(had_err); } gt_error_delete(err); if (gt_allocators_clean()) return 2; /* programmer error */ if (had_err) return EXIT_FAILURE; return EXIT_SUCCESS; }
GthBSSMParam* gth_bssm_param_load(const char *filename, GtError *err) { GthBSSMParam *bssm_param = NULL; GtStr *path = gt_str_new(); int had_err = 0; gt_error_check(err); if (gt_file_exists(filename)) gt_str_append_cstr(path, filename); else { if (strchr(filename, GT_PATH_SEPARATOR)) { gt_error_set(err, "filename \"%s\" contains illegal symbol '%c': the " "path list specified by environment variable \"%s\" " "cannot be searched for it", filename, GT_PATH_SEPARATOR, BSSMENVNAME); had_err = -1; } /* check for file path in environment variable */ if (!had_err) had_err = gt_file_find_in_env(path, filename, BSSMENVNAME, err); if (!had_err && !gt_str_length(path)) { gt_error_set(err, "file \"%s\" not found in directory list specified " "by environment variable %s", filename, BSSMENVNAME); had_err = -1; } if (!had_err) { /* path found -> append filename */ gt_str_append_char(path, GT_PATH_SEPARATOR); gt_str_append_cstr(path, filename); } else { /* check for file path relative to binary */ int new_err = gt_file_find_exec_in_path(path, gt_error_get_progname(err), NULL); if (!new_err) { gt_assert(gt_str_length(path)); gt_str_append_char(path, GT_PATH_SEPARATOR); gt_str_append_cstr(path, "bssm"); gt_str_append_char(path, GT_PATH_SEPARATOR); gt_str_append_cstr(path, filename); if (gt_file_exists(gt_str_get(path))) { gt_error_unset(err); had_err = 0; } } } } if (!had_err) { if (!(bssm_param = bssm_param_plain_read(gt_str_get(path), err))) had_err = -1; if (had_err) { /* loading new plain text format didn't work -> try old binary format */ if ((bssm_param = load_old_binary_format(path, filename, NULL))) { /* loading binary format worked -> unset error */ gt_error_unset(err); had_err = 0; } } } gt_str_delete(path); if (had_err) { gth_bssm_param_delete(bssm_param); return NULL; } return bssm_param; }
static int gt_speck_runner(int argc, const char **argv, int parsed_args, void *tool_arguments, GtError *err) { GtNodeStream *gff3_in_stream = NULL, *checker_stream = NULL, *a_in_stream = NULL, *a_out_stream = NULL, *feature_stream = NULL, *sort_stream = NULL, *last_stream = NULL; GtNodeVisitor *spec_visitor = NULL; GtSpecResults *res = NULL; GtFeatureIndex *fi = NULL; GtTypeChecker *type_checker = NULL; GtTimer *t = NULL; GtRegionMapping *rm = NULL; GtArray *arr = gt_array_new(sizeof (GtFeatureNode*)); GtStr *prog, *speclib; SpeccheckArguments *arguments = tool_arguments; int had_err = 0; gt_error_check(err); res = gt_spec_results_new(); gt_assert(res); if (gt_file_exists(gt_str_get(arguments->format))) { speclib = gt_str_ref(arguments->format); } else { prog = gt_str_new(); gt_str_append_cstr_nt(prog, gt_error_get_progname(err), gt_cstr_length_up_to_char(gt_error_get_progname(err), ' ')); speclib = gt_get_gtdata_path(gt_str_get(prog), NULL); gt_str_delete(prog); gt_str_append_cstr(speclib, "/spec/output_drivers/"); gt_str_append_str(speclib, arguments->format); if (!gt_file_exists(gt_str_get(speclib))) { gt_error_set(err, "output driver file \"%s\" does not exist", gt_str_get(speclib)); had_err = -1; } } if (!had_err) { spec_visitor = gt_spec_visitor_new(gt_str_get(arguments->specfile), res, err); if (!spec_visitor) { gt_spec_results_delete(res); return -1; } } t = gt_timer_new(); gt_assert(t); /* add region mapping if given */ if (!had_err && gt_seqid2file_option_used(arguments->s2fi)) { rm = gt_seqid2file_region_mapping_new(arguments->s2fi, err); if (!rm) had_err = -1; if (!had_err) gt_spec_visitor_add_region_mapping((GtSpecVisitor*) spec_visitor, rm); } /* set type checker if necessary */ if (!had_err && gt_typecheck_info_option_used(arguments->tci)) { type_checker = gt_typecheck_info_create_type_checker(arguments->tci, err); if (!type_checker) had_err = -1; if (!had_err) gt_spec_visitor_add_type_checker((GtSpecVisitor*) spec_visitor, type_checker); } if (!had_err) { /* set runtime error behaviour */ if (arguments->fail_hard) gt_spec_visitor_fail_on_runtime_error((GtSpecVisitor*) spec_visitor); else gt_spec_visitor_report_runtime_errors((GtSpecVisitor*) spec_visitor); /* redirect warnings */ gt_warning_set_handler(gt_speck_record_warning, res); last_stream = gff3_in_stream = gt_gff3_in_stream_new_unsorted( argc - parsed_args, argv + parsed_args); gt_assert(gff3_in_stream); gt_gff3_in_stream_enable_tidy_mode((GtGFF3InStream*) gff3_in_stream); /* insert sort stream if requested */ if (arguments->sort) { last_stream = sort_stream = gt_sort_stream_new(last_stream); } /* if -provideindex is given, collect input features and index them first */ if (arguments->provideindex) { fi = gt_feature_index_memory_new(); gt_assert(fi); last_stream = feature_stream = gt_feature_stream_new(last_stream, fi); gt_assert(feature_stream); last_stream = a_out_stream = gt_array_out_stream_all_new(last_stream, arr, err); if (!a_out_stream) had_err = -1; gt_timer_start(t); if (!had_err) had_err = gt_node_stream_pull(last_stream, err); if (!had_err) { gt_spec_visitor_add_feature_index((GtSpecVisitor*) spec_visitor, gt_feature_index_ref(fi)); last_stream = a_in_stream = gt_array_in_stream_new(arr, NULL, err); if (!a_in_stream) had_err = -1; } } else { gt_timer_start(t); } if (!had_err) { checker_stream = gt_visitor_stream_new(last_stream, spec_visitor); gt_assert(checker_stream); } /* perform checking */ if (!had_err) had_err = gt_node_stream_pull(checker_stream, err); gt_timer_stop(t); /* reset warnings output */ gt_warning_set_handler(gt_warning_default_handler, NULL); /* output results */ if (!had_err) { GtStr *runtime = gt_str_new(); gt_timer_get_formatted(t, GT_WD ".%06ld", runtime); had_err = gt_spec_results_render_template(res, gt_str_get(speclib), arguments->outfp, gt_str_get(arguments->specfile), arguments->verbose, arguments->colored, gt_str_get(runtime), err); gt_str_delete(runtime); } } /* free */ gt_node_stream_delete(gff3_in_stream); gt_node_stream_delete(a_in_stream); gt_node_stream_delete(a_out_stream); gt_node_stream_delete(checker_stream); gt_node_stream_delete(feature_stream); gt_node_stream_delete(sort_stream); gt_spec_results_delete(res); gt_feature_index_delete(fi); gt_type_checker_delete(type_checker); gt_timer_delete(t); gt_array_delete(arr); gt_str_delete(speclib); return had_err; }
GtR* gtr_new(GtError *err) { GtR *gtr; char *seedstr = NULL; int had_err = 0; #ifndef WITHOUT_CAIRO GtStr *style_file = NULL; #endif gtr = gt_calloc(1, sizeof (GtR)); if ((seedstr = getenv("GT_SEED"))) { if (gt_parse_uint(>r->seed, seedstr) != 0) { gt_error_set(err, "invalid seed in GT_SEED environment variable: %s", seedstr); had_err = -1; } } else gtr->seed = 0; if (!had_err) { gtr->debugfp = gt_str_new(); gtr->testspacepeak = gt_str_new(); gtr->test_only = gt_str_new(); gtr->manoutdir = gt_str_new(); gtr->L = luaL_newstate(); if (!gtr->L) { gt_error_set(err, "out of memory (cannot create new lua state)"); had_err = -1; } } if (!had_err) { luaL_openlibs(gtr->L); /* open the standard libraries */ gt_lua_open_lib(gtr->L); /* open the GenomeTools library */ lua_pushcfunction(gtr->L, luaopen_lpeg); lua_pushstring(gtr->L, "lpeg"); lua_call(gtr->L, 1, 0); /* open LPeg library */ lua_pushcfunction(gtr->L, luaopen_md5_core); lua_pushstring(gtr->L, "md5"); lua_call(gtr->L, 1, 0); /* open MD5 library */ lua_pushcfunction(gtr->L, luaopen_lfs); lua_pushstring(gtr->L, "lfs"); lua_call(gtr->L, 1, 0); /* open Lua filesystem */ lua_pushcfunction(gtr->L, luaopen_des56); lua_pushstring(gtr->L, "des56"); lua_call(gtr->L, 1, 0); /* open DES56 library */ had_err = gt_lua_set_modules_path(gtr->L, err); } #ifndef WITHOUT_CAIRO if (!had_err) { lua_settop(gtr->L, 0); if (!(gtr->style = gt_style_new_with_state(gtr->L))) had_err = -1; } if (!had_err) { if (!(style_file = gt_get_gtdata_path(gt_error_get_progname(err), err))) had_err = -1; } if (!had_err) { gt_str_append_cstr(style_file, "/sketch/default.style"); if (gt_file_exists(gt_str_get(style_file))) { if (gt_style_load_file(gtr->style, gt_str_get(style_file), err)) had_err = -1; else gt_lua_put_style_in_registry(gtr->L, gtr->style); } } gt_str_delete(style_file); #endif if (had_err) { gt_free(gtr); return NULL; } return gtr; }
int gtr_run(GtR *gtr, int argc, const char **argv, GtError *err) { GtToolfunc toolfunc; GtTool *tool = NULL; char **nargv = NULL; void *mem, *map; int had_err = 0; gt_error_check(err); gt_assert(gtr); if (gtr->debug) enable_logging(gt_str_get(gtr->debugfp), >r->logfp); if (gtr->quiet) gt_warning_disable(); gtr->seed = gt_ya_rand_init(gtr->seed); gt_log_log("seed=%u", gtr->seed); if (gtr->list) return list_tools(gtr); if (gt_str_length(gtr->manoutdir) > 0) return create_manpages(gtr, gt_str_get(gtr->manoutdir), err); if (gtr->check64bit) return check64bit(); if (gtr->test) return run_tests(gtr, err); if (gt_str_length(gtr->testspacepeak)) { mem = gt_malloc(1 << 26); /* alloc 64 MB */; map = gt_fa_xmmap_read(gt_str_get(gtr->testspacepeak), NULL); gt_fa_xmunmap(map); gt_free(mem); } if (argc == 0 && !gtr->interactive) { gt_error_set(err, "neither tool nor script specified; option -help lists " "possible tools"); had_err = -1; } if (!had_err && argc) { if (!gtr->tools || !gt_toolbox_has_tool(gtr->tools, argv[0])) { /* no tool found -> try to open script */ if (gt_file_exists(argv[0])) { /* export script */ gt_lua_set_script_dir(gtr->L, argv[0]); /* run script */ nargv = gt_cstr_array_prefix_first(argv, gt_error_get_progname(err)); gt_lua_set_arg(gtr->L, nargv[0], (const char**) nargv+1); if (luaL_dofile(gtr->L, argv[0])) { /* error */ gt_assert(lua_isstring(gtr->L, -1)); /* error message on top */ gt_error_set(err, "could not execute script %s", lua_tostring(gtr->L, -1)); had_err = -1; lua_pop(gtr->L, 1); /* pop error message */ } } else { /* neither tool nor script found */ gt_error_set(err, "neither tool nor script '%s' found; option -help " "lists possible tools", argv[0]); had_err = -1; } } else { /* run tool */ if (!(toolfunc = gt_toolbox_get(gtr->tools, argv[0]))) { tool = gt_toolbox_get_tool(gtr->tools, argv[0]); gt_assert(tool); } nargv = gt_cstr_array_prefix_first(argv, gt_error_get_progname(err)); gt_error_set_progname(err, nargv[0]); if (toolfunc) had_err = toolfunc(argc, (const char**) nargv, err); else had_err = gt_tool_run(tool, argc, (const char**) nargv, err); } } gt_cstr_array_delete(nargv); if (!had_err && gtr->interactive) { gt_showshortversion(gt_error_get_progname(err)); gt_lua_set_arg(gtr->L, gt_error_get_progname(err), argv); run_interactive_lua_interpreter(gtr->L); } if (had_err) return EXIT_FAILURE; return EXIT_SUCCESS; }