/** OpenOCD runtime meat that can become single-thread in future. It parse * commandline, reads configuration, sets up the target and starts server loop. * Commandline arguments are passed into this function from openocd_main(). */ static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx) { int ret; if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK) return EXIT_FAILURE; if (server_preinit() != ERROR_OK) return EXIT_FAILURE; ret = parse_config_file(cmd_ctx); if (ret != ERROR_OK) return EXIT_FAILURE; ret = server_init(cmd_ctx); if (ERROR_OK != ret) return EXIT_FAILURE; if (init_at_startup) { ret = command_run_line(cmd_ctx, "init"); if (ERROR_OK != ret) return EXIT_FAILURE; } server_loop(cmd_ctx); server_quit(); return ret; }
/* normally this is the main() function entry, but if OpenOCD is linked * into application, then this fn will not be invoked, but rather that * application will have it's own implementation of main(). */ int openocd_main(int argc, char *argv[]) { int ret; /* initialize commandline interface */ command_context_t *cmd_ctx; cmd_ctx = setup_command_handler(); #if BUILD_IOUTIL if (ioutil_init(cmd_ctx) != ERROR_OK) { return EXIT_FAILURE; } #endif LOG_OUTPUT("\n\nBUGS? Read http://svn.berlios.de/svnroot/repos/openocd/trunk/BUGS\n\n\n"); print_version(); command_context_mode(cmd_ctx, COMMAND_CONFIG); command_set_output_handler(cmd_ctx, configuration_output_handler, NULL); if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK) return EXIT_FAILURE; ret = parse_config_file(cmd_ctx); if ( (ret != ERROR_OK) && (ret != ERROR_COMMAND_CLOSE_CONNECTION) ) return EXIT_FAILURE; #if BUILD_HTTPD if (httpd_start()!=ERROR_OK) return EXIT_FAILURE; #endif if (ret != ERROR_COMMAND_CLOSE_CONNECTION) { command_context_mode(cmd_ctx, COMMAND_EXEC); if (command_run_line(cmd_ctx, "init")!=ERROR_OK) return EXIT_FAILURE; /* handle network connections */ server_loop(cmd_ctx); } /* shut server down */ server_quit(); #if BUILD_HTTPD httpd_stop(); #endif unregister_all_commands(cmd_ctx); /* free commandline interface */ command_done(cmd_ctx); return EXIT_SUCCESS; }
int process_command_line(const char* line) { int argc = 0; char** argv = NULL; int i, rc = 0; const char* str_cmd; if (line == NULL) return 0; current_command_line = line; if (parse_cmdline_args(line, &argc, &argv) < 0) { rc = -1; goto cleanup; } if (argc == 0) goto cleanup; for (i = 0; commands[i].fn != NULL; i++) { str_cmd = res_str(commands[i].res_id); if (strcmp(str_cmd, argv[0]) == 0) { rc = commands[i].fn(argc, (const char**)argv); break; } } if (commands[i].fn == NULL) { msgf(STR_UNKNOWN_COMMAND); rc = 0; } else { switch (rc) { case CMDLINE_INVALID_PARAMETER: msgf(STR_INVALID_PARAMETERS); help(argv[0]); break; } } cleanup: if (argv != NULL) free(argv); current_command_line = NULL; return rc; }
cable* cable::factory(const char* s) { cable* cbl = NULL; int argc; char** argv = NULL; int addr; if (*s == 0) return NULL; parse_cmdline_args(s, &argc, &argv); if (argc == 0) return NULL; if (stricmp(argv[0], "xil3") == 0) { if (argc == 1) addr = 0x378; else { if (str2num(argv[1], &addr)) goto cleanup; } cbl = new parport((unsigned int)addr); } else if (stricmp(argv[0], "dusb") == 0) cbl = new digilent; else if (stricmp(argv[0],"znuhtag") == 0) { int num=0; if(argc > 1) str2num(argv[1], &num); cbl = new znuhtag(num); } else if (stricmp(argv[0],"amontec") == 0) { int speed = 0; if (argc > 1) str2num(argv[1], &speed); cbl = new amontec(speed); } else { msgf(STR_INVALID_CABLE_DEF); return NULL; } cleanup: return cbl; }
int main(int argc, char **argv) { struct cmdline_args args = {0}; int ret = 0; opdb_reset(); ctrlc_init(); args.devarg.vcc_mv = 3000; args.devarg.requested_serial = NULL; if (parse_cmdline_args(argc, argv, &args) < 0) return -1; if (sockets_init() < 0) return -1; printc_dbg("%s\n", version_text); if (setup_driver(&args) < 0) { sockets_exit(); return -1; } simio_init(); if (!args.no_rc) process_rc_file(); /* Process commands */ if (optind < argc) { while (optind < argc) { if (process_command(argv[optind++]) < 0) { ret = -1; break; } } } else { reader_loop(); } simio_exit(); stab_exit(); device_destroy(); sockets_exit(); return ret; }
/** OpenOCD runtime meat that can become single-thread in future. It parse * commandline, reads configuration, sets up the target and starts server loop. * Commandline arguments are passed into this function from openocd_main(). */ static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx) { int ret; if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK) return ERROR_FAIL; if (server_preinit() != ERROR_OK) return ERROR_FAIL; ret = parse_config_file(cmd_ctx); if (ret == ERROR_COMMAND_CLOSE_CONNECTION) return ERROR_OK; else if (ret != ERROR_OK) return ERROR_FAIL; ret = server_init(cmd_ctx); if (ERROR_OK != ret) return ERROR_FAIL; if (init_at_startup) { ret = command_run_line(cmd_ctx, "init"); if (ERROR_OK != ret) { server_quit(); return ERROR_FAIL; } } ret = server_loop(cmd_ctx); int last_signal = server_quit(); if (last_signal != ERROR_OK) return last_signal; if (ret != ERROR_OK) return ERROR_FAIL; return ERROR_OK; }
int ctx_calls2vcf(int argc, char **argv) { parse_cmdline_args(argc, argv); size_t i; // These functions call die() on error gzFile gzin = futil_gzopen(input_path, "r"); nw_aligner_setup(); // Read file header cJSON *json = read_input_header(gzin); // Get format (bubble or breakpoint file) cJSON *json_fmt = json_hdr_get(json, "file_format", cJSON_String, input_path); if(strcmp(json_fmt->valuestring,"CtxBreakpoints") == 0) input_bubble_format = false; else if(strcmp(json_fmt->valuestring,"CtxBubbles") == 0) input_bubble_format = true; else die("Unknown format: '%s'", json_fmt->valuestring); status("Reading %s in %s format", futil_inpath_str(input_path), input_bubble_format ? "bubble" : "breakpoint"); if(input_bubble_format && sam_path == NULL) cmd_print_usage("Require -F <flanks.sam> with bubble file"); // Open flank file if it exists if(sam_path) flanks_sam_open(); // Open output file FILE *fout = futil_fopen_create(out_path, "w"); // Load reference genome read_buf_alloc(&chroms, 1024); genome = kh_init(ChromHash); seq_reader_load_ref_genome(ref_paths, num_ref_paths, &chroms, genome); // convert to upper case char *s; for(i = 0; i < chroms.len; i++) for(s = chroms.b[i].seq.b; *s; s++) *s = toupper(*s); if(!input_bubble_format) brkpnt_check_refs_match(json, input_path); // Output VCF has 0 samples if bubbles file, otherwise has N where N is // number of samples/colours in the breakpoint graph size_t num_graph_samples = json_hdr_get_ncols(json, input_path); size_t num_graph_nonref = json_hdr_get_nonref_ncols(json, input_path); num_samples = 0; if(!input_bubble_format) { // If last colour has "is_ref", drop number of samples by one num_samples = num_graph_nonref < num_graph_samples ? num_graph_samples-1 : num_graph_samples; } print_vcf_header(json, !input_bubble_format, fout); status("Reading %s call file with %zu samples", input_bubble_format ? "Bubble" : "Breakpoint", num_graph_samples); status("Writing a VCF with %zu samples", num_samples); parse_entries(gzin, fout); // Print stats char num_entries_read_str[50]; char num_vars_printed_str[50]; ulong_to_str(num_entries_read, num_entries_read_str); ulong_to_str(num_vars_printed, num_vars_printed_str); status("Read %s entries, printed %s vcf entries to: %s", num_entries_read_str, num_vars_printed_str, futil_outpath_str(out_path)); if(input_bubble_format) { char msg[200]; // Bubble caller specific print_stat(num_flank5p_unmapped, num_entries_read, "flank 5p unmapped"); sprintf(msg, "flank 5p low mapq (<%zu)", min_mapq); print_stat(num_flank5p_lowqual, num_entries_read, msg); print_stat(num_flank3p_not_found, num_entries_read, "flank 3p not found"); print_stat(num_flank3p_multihits, num_entries_read, "flank 3p multiple hits"); print_stat(num_flank3p_approx_match,num_entries_read, "flank 3p approx match used"); print_stat(num_flank3p_exact_match, num_entries_read, "flank 3p exact match"); } else { // Breakpoint caller specific print_stat(num_flanks_not_uniquely_mapped, num_entries_read, "flank pairs contain one flank not mapped uniquely"); print_stat(num_flanks_diff_chroms, num_entries_read, "flank pairs map to diff chroms"); print_stat(num_flanks_diff_strands, num_entries_read, "flank pairs map to diff strands"); } print_stat(num_flanks_too_far_apart, num_entries_read, "flank pairs too far apart"); print_stat(num_flanks_overlap_too_large, num_entries_read, "flank pairs overlap too much"); print_stat(num_entries_well_mapped, num_entries_read, "flank pairs map well"); status("Aligned %zu allele pairs and %zu flanks", num_nw_allele, num_nw_flank); // Finished - clean up cJSON_Delete(json); gzclose(gzin); fclose(fout); for(i = 0; i < chroms.len; i++) seq_read_dealloc(&chroms.b[i]); read_buf_dealloc(&chroms); kh_destroy_ChromHash(genome); nw_aligner_destroy(); if(sam_path) flanks_sam_close(); // hide unused method warnings (void)kh_del_ChromHash; (void)kh_put_ChromHash; (void)kh_get_ChromHash; (void)kh_clear_ChromHash; (void)kh_destroy_ChromHash; (void)kh_init_ChromHash; return EXIT_SUCCESS; }
int load_config_file(const char *config) { int rc = -1, line_index = 0; FILE* f; char line[256]; chip_family* family = NULL; char* name; char* param; int argc; char** argv = NULL; int id, mask; int ir_length; f = fopen(config, "rt"); if (f == NULL) { msgf(STR_FAILED_TO_OPEN_CONFIG_FILE); return -1; } for (line_index = 1; !feof(f); line_index++) { if (fgets(line, sizeof(line), f) == NULL) break; strip_whitespaces(line); // Empty if (*line == 0) continue; // Comment if (line[0] == ';' || line[0] == '#') continue; if (strcasecmp(line, "BEGIN_FAMILY") == 0) { if (family != NULL) goto cleanup; family = new chip_family; if (family == NULL) goto cleanup; } else if (strcasecmp(line, "END_FAMILY") == 0) { if (family == NULL) goto cleanup; // Check if NAME, DESC, TYPE exist if (family->vars.exists(strNAME) < 0) goto cleanup; if (family->vars.exists(strDESC) < 0) goto cleanup; if (family->vars.exists(strTYPE) < 0) goto cleanup; // Add family to chip database g.chips.push_back(family); family = NULL; } else { name = line; param = strchr(line, '='); *param = 0; param++; strip_whitespaces(name); strip_whitespaces(param); if (*name == 0) goto cleanup; parse_cmdline_args(param, &argc, &argv); if (family) { if (strcasecmp(name, "CHIP") == 0) { if (argc != 4) goto cleanup; if (str2num(argv[1], &id) || str2num(argv[2], &mask) || str2num(argv[3], &ir_length)) goto cleanup; family->push_back(chip(argv[0], id, mask, ir_length, family)); } else if (argc > 0) family->vars.add(name, argv[0]); } else g.vars.add(name, param); free(argv); argv = NULL; } } rc = 0; cleanup: if (f) fclose(f); if (family) delete family; if (argv) free(argv); if (rc) msgf(STR_INVALID_CONFIG_FILE, line_index); return rc; }
bool app_init_and_loop(int& argc, char**& argv) { // glib needs this for command line args. It will be reset by Gtk::Main later. // this aborts on freebsd with "locale::facet::_S_create_c_locale name not valid", // so use the C equivalent. // std::locale::global(std::locale("")); // set locale to system LANG std::setlocale(LC_ALL, ""); // initialize GThread (for mutexes, etc... to work). Must be called before any other glib function. Glib::thread_init(); // parse command line args CmdArgs args; if (! parse_cmdline_args(args, argc, argv)) { return true; } // Note: parsing gtk option context is initializes gtk, so // gtk_disable_setlocale() won't work here. if (!args.arg_locale) { std::setlocale(LC_ALL, "C"); // set classic locale. otherwise we're already in user locale. } if (args.arg_version) { // show version information and exit app_print_version_info(); return true; } // register libdebug domains debug_register_domain("gtk"); debug_register_domain("app"); debug_register_domain("hz"); debug_register_domain("rmn"); debug_register_domain("rconfig"); // Add special debug channel to collect all libdebug output into a buffer. debug_add_channel("all", debug_level::all, app_get_debug_buf_channel()); std::vector<std::string> load_virtuals; if (args.arg_add_virtual) { const gchar* entry = 0; while ( (entry = *(args.arg_add_virtual)++) != NULL ) { load_virtuals.push_back(entry); } } std::string load_virtuals_str = hz::string_join(load_virtuals, ", "); // for display purposes only std::vector<std::string> load_devices; if (args.arg_add_device) { const gchar* entry = 0; while ( (entry = *(args.arg_add_device)++) != NULL ) { load_devices.push_back(entry); } } std::string load_devices_str = hz::string_join(load_devices, ", "); // for display purposes only // it's here because earlier there are no domains debug_out_dump("app", "Application options:\n" << "\tlocale: " << args.arg_locale << "\n" << "\tversion: " << args.arg_version << "\n" << "\thide_tabs: " << args.arg_hide_tabs << "\n" << "\tscan: " << args.arg_scan << "\n" << "\targ_add_virtual: " << (load_virtuals_str.empty() ? "[empty]" : load_virtuals_str) << "\n" << "\targ_add_device: " << (load_devices_str.empty() ? "[empty]" : load_devices_str) << "\n"); debug_out_dump("app", "LibDebug options:\n" << debug_get_cmd_args_dump()); // Load config files app_init_config(); debug_out_info("app", "Current locale: " << std::setlocale(LC_ALL, NULL) << "\n"); // Initialize GTK+ // Gtk::Main m(argc, argv, args.arg_locale); Gtk::Main m(argc, argv); // Redirect all GTK+/Glib and related messages to libdebug static const char* const gtkdomains[] = { // no atk or cairo, they don't log. libgnomevfs may be loaded by gtk file chooser. "GLib", "GModule", "GLib-GObject", "GLib-GRegex", "GLib-GIO", "GThread", "Pango", "Gtk", "Gdk", "GdkPixbuf", "libglade", "libgnomevfs", "glibmm", "giomm", "atkmm", "pangomm", "gdkmm", "gtkmm", "libglademm" }; for (unsigned int i = 0; i < G_N_ELEMENTS(gtkdomains); ++i) { g_log_set_handler(gtkdomains[i], GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION), glib_message_handler, NULL); } // This shows up in About dialog gtk. Glib::set_application_name("GSmartControl"); // should be localized // Add data file search paths #ifdef _WIN32 hz::data_file_add_search_directory("."); // the program is bundled with all the files in the same directory #else #ifdef DEBUG_BUILD hz::data_file_add_search_directory(std::string(TOP_SRC_DIR) + "/src/res"); // application data resources hz::data_file_add_search_directory(std::string(TOP_SRC_DIR) + "/data"); // application data resources #else hz::data_file_add_search_directory(PACKAGE_PKGDATA_DIR); // /usr/share/program_name #endif #endif // set default icon for all windows. // set_default_icon_name is available since 2.12 in gtkmm, but since 2.6 in gtk. #ifndef _WIN32 // win32 version has its icon compiled-in. { // we load it via icontheme to provide multi-size version. GtkIconTheme* default_icon_theme = gtk_icon_theme_get_default(); // application-installed, /usr/share/icons/<theme_name>/apps/<size> if (gtk_icon_theme_has_icon(default_icon_theme, "gsmartcontrol")) { gtk_window_set_default_icon_name("gsmartcontrol"); // try the gnome icon, it's higher quality / resolution } else if (gtk_icon_theme_has_icon(default_icon_theme, "gnome-dev-harddisk")) { gtk_window_set_default_icon_name("gnome-dev-harddisk"); // gtk built-in, always available } else { gtk_window_set_default_icon_name("gtk-harddisk"); } } #endif // Export some command line arguments to rmn // obey the command line option for no-scan on startup rconfig::set_data("/runtime/gui/force_no_scan_on_startup", !bool(args.arg_scan)); // load virtual drives on startup if specified. rconfig::set_data("/runtime/gui/add_virtuals_on_startup", load_virtuals); // add devices to the list on startup if specified. rconfig::set_data("/runtime/gui/add_devices_on_startup", load_devices); // hide tabs if SMART is disabled rconfig::set_data("/runtime/gui/hide_tabs_on_smart_disabled", bool(args.arg_hide_tabs)); // Create executor log window, but don't show it. // It will track all command executor outputs. GscExecutorLogWindow::create(); // Open the main window GscMainWindow* win = GscMainWindow::create(); if (!win) { debug_out_fatal("app", "Cannot create the main window. Exiting.\n"); return false; // cannot create main window } // first-boot message app_show_first_boot_message(win); // The Main Loop (tm) debug_out_info("app", "Entering main loop.\n"); m.run(); debug_out_info("app", "Main loop exited.\n"); // close the main window and delete its object GscMainWindow::destroy(); GscExecutorLogWindow::destroy(); // std::cerr << app_get_debug_buffer_str(); // this will output everything that went through libdebug. return true; }
int main(int argc, char **argv) { struct cmdline_args args = {0}; int ret = 0; setvbuf(stderr, NULL, _IOFBF, 0); setvbuf(stdout, NULL, _IOFBF, 0); opdb_reset(); ctrlc_init(); args.devarg.vcc_mv = 3000; args.devarg.requested_serial = NULL; if (parse_cmdline_args(argc, argv, &args) < 0) goto fail_parse; if (args.flags & OPT_EMBEDDED) input_module = &input_async; if (input_module->init() < 0) goto fail_input; output_set_embedded(args.flags & OPT_EMBEDDED); if (sockets_init() < 0) { ret = -1; goto fail_sockets; } printc_dbg("%s", version_text); printc_dbg("%s\n", chipinfo_copyright()); if (setup_driver(&args) < 0) { ret = -1; goto fail_driver; } if (device_probe_id(device_default, args.devarg.forced_chip_id) < 0) printc_err("warning: device ID probe failed\n"); simio_init(); if (!(args.flags & OPT_NO_RC)) process_rc_file(args.alt_config); /* Process commands */ if (optind < argc) { while (optind < argc) { if (process_command(argv[optind++]) < 0) { ret = -1; break; } } } else { reader_loop(); } simio_exit(); device_destroy(); stab_exit(); fail_driver: sockets_exit(); fail_sockets: input_module->exit(); fail_input: fail_parse: /* We need to do this on Windows, because in embedded mode we * may still have a running background thread for input. If so, * returning from main() won't cause the process to terminate. */ #if defined(__CYGWIN__) cygwin_internal(CW_EXIT_PROCESS, (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE, 1); #elif defined(__Windows__) ExitProcess(ret); #endif return ret; }