]> sigrok.org Git - sigrok-cli.git/blob - input.c
Fix hardcoded home directory (pass -DHOME=$HOME to makensis).
[sigrok-cli.git] / input.c
1 /*
2  * This file is part of the sigrok-cli project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "sigrok-cli.h"
21 #include "config.h"
22 #include <glib.h>
23
24 extern gchar *opt_input_file;
25 extern gchar *opt_input_format;
26 extern gchar *opt_probes;
27
28
29 /**
30  * Return the input file format which the CLI tool should use.
31  *
32  * If the user specified -I / --input-format, use that one. Otherwise, try to
33  * autodetect the format as good as possible. Failing that, return NULL.
34  *
35  * @param filename The filename of the input file. Must not be NULL.
36  * @param opt The -I / --input-file option the user specified (or NULL).
37  *
38  * @return A pointer to the 'struct sr_input_format' that should be used,
39  *         or NULL if no input format was selected or auto-detected.
40  */
41 static struct sr_input_format *determine_input_file_format(
42                         const char *filename, const char *opt)
43 {
44         int i;
45         struct sr_input_format **inputs;
46
47         /* If there are no input formats, return NULL right away. */
48         inputs = sr_input_list();
49         if (!inputs) {
50                 g_critical("No supported input formats available.");
51                 return NULL;
52         }
53
54         /* If the user specified -I / --input-format, use that one. */
55         if (opt) {
56                 for (i = 0; inputs[i]; i++) {
57                         if (strcasecmp(inputs[i]->id, opt))
58                                 continue;
59                         g_debug("Using user-specified input file format '%s'.",
60                                         inputs[i]->id);
61                         return inputs[i];
62                 }
63
64                 /* The user specified an unknown input format, return NULL. */
65                 g_critical("Error: specified input file format '%s' is "
66                         "unknown.", opt);
67                 return NULL;
68         }
69
70         /* Otherwise, try to find an input module that can handle this file. */
71         for (i = 0; inputs[i]; i++) {
72                 if (inputs[i]->format_match(filename))
73                         break;
74         }
75
76         /* Return NULL if no input module wanted to touch this. */
77         if (!inputs[i]) {
78                 g_critical("Error: no matching input module found.");
79                 return NULL;
80         }
81
82         g_debug("cli: Autodetected '%s' input format for file '%s'.",
83                 inputs[i]->id, filename);
84
85         return inputs[i];
86 }
87
88 static void load_input_file_format(void)
89 {
90         GHashTable *fmtargs = NULL;
91         struct stat st;
92         struct sr_input *in;
93         struct sr_input_format *input_format;
94         char *fmtspec = NULL;
95
96         if (opt_input_format) {
97                 fmtargs = parse_generic_arg(opt_input_format, TRUE);
98                 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
99         }
100
101         if (!(input_format = determine_input_file_format(opt_input_file,
102                                                    fmtspec))) {
103                 /* The exact cause was already logged. */
104                 return;
105         }
106
107         if (fmtargs)
108                 g_hash_table_remove(fmtargs, "sigrok_key");
109
110         if (stat(opt_input_file, &st) == -1) {
111                 g_critical("Failed to load %s: %s", opt_input_file,
112                         strerror(errno));
113                 exit(1);
114         }
115
116         /* Initialize the input module. */
117         if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
118                 g_critical("Failed to allocate input module.");
119                 exit(1);
120         }
121         in->format = input_format;
122         in->param = fmtargs;
123         if (in->format->init) {
124                 if (in->format->init(in, opt_input_file) != SR_OK) {
125                         g_critical("Input format init failed.");
126                         exit(1);
127                 }
128         }
129
130         if (select_probes(in->sdi) > 0)
131                 return;
132
133         sr_session_new();
134         sr_session_datafeed_callback_add(datafeed_in, NULL);
135         if (sr_session_dev_add(in->sdi) != SR_OK) {
136                 g_critical("Failed to use device.");
137                 sr_session_destroy();
138                 return;
139         }
140
141         input_format->loadfile(in, opt_input_file);
142
143         sr_session_destroy();
144
145         if (fmtargs)
146                 g_hash_table_destroy(fmtargs);
147 }
148
149 void load_input_file(void)
150 {
151
152         if (sr_session_load(opt_input_file) == SR_OK) {
153                 /* sigrok session file */
154                 sr_session_datafeed_callback_add(datafeed_in, NULL);
155                 sr_session_start();
156                 sr_session_run();
157                 sr_session_stop();
158         }
159         else {
160                 /* fall back on input modules */
161                 load_input_file_format();
162         }
163 }
164