int main (int argc, char **argv) { int prog_arg = 0; const char *config_file = NULL; const char *dbus_daemon = NULL; char bus_address[MAX_ADDR_LEN] = { 0 }; const char *prev_arg = NULL; int i = 1; int requires_arg = 0; while (i < argc) { const char *arg = argv[i]; if (requires_arg) { const char **arg_dest; assert (prev_arg != NULL); if (strcmp (prev_arg, "--config-file") == 0) { arg_dest = &config_file; } else if (strcmp (prev_arg, "--dbus-daemon") == 0) { arg_dest = &dbus_daemon; } else { /* shouldn't happen */ fprintf (stderr, "%s: internal error: %s not fully implemented\n", me, prev_arg); return 127; } if (*arg_dest != NULL) { fprintf (stderr, "%s: %s given twice\n", me, prev_arg); return 127; } *arg_dest = arg; requires_arg = 0; prev_arg = arg; ++i; continue; } if (strcmp (arg, "--help") == 0 || strcmp (arg, "-h") == 0 || strcmp (arg, "-?") == 0) { usage (0); } else if (strcmp (arg, "--version") == 0) { version (); } else if (strstr (arg, "--config-file=") == arg) { const char *file; if (config_file != NULL) { fprintf (stderr, "%s: --config-file given twice\n", me); return 127; } file = strchr (arg, '='); ++file; config_file = file; } else if (strstr (arg, "--dbus-daemon=") == arg) { const char *file; if (dbus_daemon != NULL) { fprintf (stderr, "%s: --dbus-daemon given twice\n", me); return 127; } file = strchr (arg, '='); ++file; dbus_daemon = file; } else if (strcmp (arg, "--config-file") == 0 || strcmp (arg, "--dbus-daemon") == 0) { requires_arg = 1; } else if (arg[0] == '-') { if (strcmp (arg, "--") != 0) { fprintf (stderr, "%s: option '%s' is unknown\n", me, arg); return 127; } else { prog_arg = i + 1; break; } } else { prog_arg = i; break; } prev_arg = arg; ++i; } /* "dbus-run-session" and "dbus-run-session ... --" are not allowed: * there must be something to run */ if (prog_arg < 1 || prog_arg >= argc) { fprintf (stderr, "%s: a non-option argument is required\n", me); return 127; } if (requires_arg) { fprintf (stderr, "%s: option '%s' requires an argument\n", me, prev_arg); return 127; } if (dbus_daemon == NULL) dbus_daemon = "dbus-daemon"; return run_session (dbus_daemon, config_file, bus_address, argv, prog_arg); }
int main(int argc, char **argv) { g_log_set_default_handler(logger, NULL); if (parse_options(argc, argv)) { return 1; } /* Set the loglevel (amount of messages to output) for libsigrok. */ if (sr_log_loglevel_set(opt_loglevel) != SR_OK) goto done; if (sr_init(&sr_ctx) != SR_OK) goto done; #ifdef HAVE_SRD /* Set the loglevel (amount of messages to output) for libsigrokdecode. */ if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) goto done; if (opt_pds) { if (srd_init(NULL) != SRD_OK) goto done; if (srd_session_new(&srd_sess) != SRD_OK) { g_critical("Failed to create new decode session."); goto done; } if (register_pds(opt_pds, opt_pd_annotations) != 0) goto done; if (setup_pd_stack(opt_pds, opt_pd_stack, opt_pd_annotations) != 0) goto done; /* Only one output type is ever shown. */ if (opt_pd_binary) { if (setup_pd_binary(opt_pd_binary) != 0) goto done; if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_BINARY, show_pd_binary, NULL) != SRD_OK) goto done; } else if (opt_pd_meta) { if (setup_pd_meta(opt_pd_meta) != 0) goto done; if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_META, show_pd_meta, NULL) != SRD_OK) goto done; } else { if (opt_pd_annotations) if (setup_pd_annotations(opt_pd_annotations) != 0) goto done; if (srd_pd_output_callback_add(srd_sess, SRD_OUTPUT_ANN, show_pd_annotations, NULL) != SRD_OK) goto done; } } #endif if (opt_version) show_version(); else if (opt_input_format && opt_show) show_input(); else if (opt_output_format && opt_show) show_output(); else if (opt_transform_module && opt_show) show_transform(); else if (opt_scan_devs) show_dev_list(); #ifdef HAVE_SRD else if (opt_pds && opt_show) show_pd_detail(); #endif else if (opt_show) show_dev_detail(); else if (opt_input_file) load_input_file(); else if (opt_get) get_option(); else if (opt_set) set_options(); else if (opt_samples || opt_time || opt_frames || opt_continuous) run_session(); else show_help(); #ifdef HAVE_SRD if (opt_pds) srd_exit(); #endif done: if (sr_ctx) sr_exit(sr_ctx); return 0; }
int main(int argc, char **argv) { GOptionContext *context; GError *error; g_log_set_default_handler(logger, NULL); error = NULL; context = g_option_context_new(NULL); g_option_context_add_main_entries(context, optargs, NULL); if (!g_option_context_parse(context, &argc, &argv, &error)) { g_critical("%s", error->message); return 1; } /* Set the loglevel (amount of messages to output) for libsigrok. */ if (sr_log_loglevel_set(opt_loglevel) != SR_OK) return 1; /* Set the loglevel (amount of messages to output) for libsigrokdecode. */ if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) return 1; if (sr_init() != SR_OK) return 1; if (opt_pds) { if (srd_init(NULL) != SRD_OK) return 1; if (register_pds(NULL, opt_pds) != 0) return 1; if (srd_pd_output_callback_add(SRD_OUTPUT_ANN, show_pd_annotations, NULL) != SRD_OK) return 1; if (setup_pd_stack() != 0) return 1; if (setup_pd_annotations() != 0) return 1; } if (setup_output_format() != 0) return 1; if (opt_version) show_version(); else if (opt_list_devs) show_dev_list(); else if (opt_input_file) load_input_file(); else if (opt_samples || opt_time || opt_frames || opt_continuous) run_session(); else if (opt_dev) show_dev_detail(); else if (opt_pds) show_pd_detail(); else printf("%s", g_option_context_get_help(context, TRUE, NULL)); if (opt_pds) srd_exit(); g_option_context_free(context); sr_exit(); return 0; }