- if (!(input_format = determine_input_file_format(opt_input_file,
- fmtspec))) {
- /* The exact cause was already logged. */
- return;
- }
-
- if (fmtargs)
- g_hash_table_remove(fmtargs, "sigrok_key");
-
- if (stat(opt_input_file, &st) == -1) {
- g_critical("Failed to load %s: %s", opt_input_file,
- strerror(errno));
- exit(1);
- }
-
- /* Initialize the input module. */
- if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
- g_critical("Failed to allocate input module.");
- exit(1);
- }
- in->format = input_format;
- in->param = fmtargs;
- if (in->format->init) {
- if (in->format->init(in, opt_input_file) != SR_OK) {
- g_critical("Input format init failed.");
- exit(1);
+ buf = g_string_sized_new(BUFSIZE);
+ if (mod_id) {
+ /* User specified an input module to use. */
+ if (!(imod = sr_input_find(mod_id)))
+ g_critical("Error: unknown input module '%s'.", mod_id);
+ g_hash_table_remove(mod_args, "sigrok_key");
+ if ((options = sr_input_options_get(imod))) {
+ mod_opts = generic_arg_to_opt(options, mod_args);
+ sr_output_options_free(options);
+ } else
+ mod_opts = NULL;
+ if (!(in = sr_input_new(imod, mod_opts)))
+ g_critical("Error: failed to initialize input module.");
+ if (mod_opts)
+ g_hash_table_destroy(mod_opts);
+ if (mod_args)
+ g_hash_table_destroy(mod_args);
+ if ((fd = open(opt_input_file, O_RDONLY)) == -1)
+ g_critical("Failed to load %s: %s.", opt_input_file,
+ strerror(errno));
+ } else {
+ if (strcmp(opt_input_file, "-")) {
+ /*
+ * An actual filename: let the input modules try to
+ * identify the file.
+ */
+ in = sr_input_scan_file(opt_input_file);
+ /* That worked, reopen the file for reading. */
+ fd = open(opt_input_file, O_RDONLY);
+ } else {
+ /*
+ * Taking input from a pipe: let the input modules try
+ * to identify the stream content.
+ */
+ if (!strcmp(opt_input_file, "-")) {
+ /* stdin */
+ fd = 0;
+ } else {
+ if ((fd = open(opt_input_file, O_RDONLY)) == -1)
+ g_critical("Failed to load %s: %s.", opt_input_file,
+ strerror(errno));
+ }
+ if ((len = read(fd, buf->str, BUFSIZE)) < 1)
+ g_critical("Failed to read %s: %s.", opt_input_file,
+ strerror(errno));
+ buf->len = len;
+ in = sr_input_scan_buffer(buf);