]> sigrok.org Git - sigrok-cli.git/blame - sigrok-cli.c
cli: Make --protocol-decoder-annotations plural.
[sigrok-cli.git] / sigrok-cli.c
CommitLineData
43e5747a
UH
1/*
2 * This file is part of the sigrok project.
3 *
110aa72a 4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
43e5747a
UH
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 <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <string.h>
25#include <time.h>
26#include <sys/time.h>
27#include <inttypes.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <errno.h>
31#include <glib.h>
32#include <glib/gstdio.h>
33#include <sigrok.h>
34#include "sigrok-cli.h"
35#include "config.h"
36
37#define DEFAULT_OUTPUT_FORMAT "bits:width=64"
38
39extern struct sr_hwcap_option sr_hwcap_options[];
40
1999ae06
UH
41static uint64_t limit_samples = 0;
42static struct sr_output_format *output_format = NULL;
43static int default_output_format = FALSE;
44static char *output_format_param = NULL;
45static char *input_format_param = NULL;
120f9ee7 46static GHashTable *pd_ann_visible = NULL;
43e5747a
UH
47
48static gboolean opt_version = FALSE;
49static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
1e0f9ed9 50static gboolean opt_list_devs = FALSE;
43e5747a
UH
51static gboolean opt_wait_trigger = FALSE;
52static gchar *opt_input_file = NULL;
53static gchar *opt_output_file = NULL;
1e0f9ed9 54static gchar *opt_dev = NULL;
43e5747a
UH
55static gchar *opt_probes = NULL;
56static gchar *opt_triggers = NULL;
57static gchar *opt_pds = NULL;
9f4a898e 58static gchar *opt_pd_stack = NULL;
b6bd032d 59static gchar *opt_pd_annotations = NULL;
43e5747a 60static gchar *opt_input_format = NULL;
76ae913d 61static gchar *opt_output_format = NULL;
43e5747a
UH
62static gchar *opt_time = NULL;
63static gchar *opt_samples = NULL;
64static gchar *opt_continuous = NULL;
65
66static GOptionEntry optargs[] = {
03996962
BV
67 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
68 "Show version and support list", NULL},
69 {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
70 "Select libsigrok/libsigrokdecode loglevel", NULL},
71 {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devs,
72 "Scan for devices", NULL},
73 {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_dev,
74 "Use specified device", NULL},
75 {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
76 "Load input from file", NULL},
77 {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
78 "Input format", NULL},
79 {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
80 "Save output to file", NULL},
81 {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
82 "Output format", NULL},
83 {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
84 "Probes to use", NULL},
85 {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
86 "Trigger configuration", NULL},
87 {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
88 "Wait for trigger", NULL},
89 {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds,
90 "Protocol decoders to run", NULL},
91 {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
92 "Protocol decoder stack", NULL},
b6bd032d
UH
93 {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations,
94 "Protocol decoder annotation(s) to show", NULL},
03996962
BV
95 {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time,
96 "How long to sample (ms)", NULL},
97 {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples,
98 "Number of samples to acquire", NULL},
99 {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
100 "Sample continuously", NULL},
43e5747a
UH
101 {NULL, 0, 0, 0, NULL, NULL, NULL}
102};
103
104static void show_version(void)
105{
bc8e2400 106 GSList *l;
a210c4cc 107 struct sr_dev_driver **drivers;
43e5747a
UH
108 struct sr_input_format **inputs;
109 struct sr_output_format **outputs;
110 struct srd_decoder *dec;
111 int i;
112
113 printf("sigrok-cli %s\n\n", VERSION);
15d920a9 114
4d167240
UH
115 printf("Using libsigrok %s (lib version %s).\n",
116 sr_package_version_string_get(), sr_lib_version_string_get());
117 printf("Using libsigrokdecode %s (lib version %s).\n\n",
118 srd_package_version_string_get(), srd_lib_version_string_get());
119
43e5747a 120 printf("Supported hardware drivers:\n");
a214a2eb 121 drivers = sr_driver_list();
a210c4cc
UH
122 for (i = 0; drivers[i]; i++) {
123 printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname);
43e5747a
UH
124 }
125 printf("\n");
126
127 printf("Supported input formats:\n");
128 inputs = sr_input_list();
129 for (i = 0; inputs[i]; i++)
130 printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description);
131 printf("\n");
132
133 printf("Supported output formats:\n");
134 outputs = sr_output_list();
135 for (i = 0; outputs[i]; i++)
136 printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description);
137 printf("\n");
138
15d920a9
BV
139 if (srd_init(NULL) == SRD_OK) {
140 printf("Supported protocol decoders:\n");
1b34803d
UH
141 srd_decoder_load_all();
142 for (l = srd_decoder_list(); l; l = l->next) {
15d920a9
BV
143 dec = l->data;
144 printf(" %-20s %s\n", dec->id, dec->longname);
20622036
UH
145 /* Print protocol description upon "-l 3" or higher. */
146 if (opt_loglevel >= SR_LOG_INFO)
147 printf(" %-20s %s\n", "", dec->desc);
15d920a9
BV
148 }
149 srd_exit();
43e5747a
UH
150 }
151 printf("\n");
43e5747a
UH
152}
153
1e0f9ed9 154static void print_dev_line(const struct sr_dev *dev)
43e5747a 155{
1e0f9ed9 156 const struct sr_dev_inst *sdi;
43e5747a 157
b9210275 158 sr_dev_info_get(dev, SR_DI_INST, (const void **)&sdi);
43e5747a
UH
159
160 if (sdi->vendor && sdi->vendor[0])
161 printf("%s ", sdi->vendor);
162 if (sdi->model && sdi->model[0])
163 printf("%s ", sdi->model);
164 if (sdi->version && sdi->version[0])
165 printf("%s ", sdi->version);
1e0f9ed9
UH
166 if (dev->probes)
167 printf("with %d probes", g_slist_length(dev->probes));
43e5747a
UH
168 printf("\n");
169}
170
1e0f9ed9 171static void show_dev_list(void)
43e5747a 172{
1e0f9ed9
UH
173 struct sr_dev *dev, *demo_dev;
174 GSList *devs, *l;
43e5747a
UH
175 int devcnt;
176
177 devcnt = 0;
1e0f9ed9 178 devs = sr_dev_list();
43e5747a 179
1e0f9ed9 180 if (g_slist_length(devs) == 0)
43e5747a
UH
181 return;
182
183 printf("The following devices were found:\nID Device\n");
1e0f9ed9
UH
184 demo_dev = NULL;
185 for (l = devs; l; l = l->next) {
186 dev = l->data;
187 if (sr_dev_has_hwcap(dev, SR_HWCAP_DEMO_DEV)) {
188 demo_dev = dev;
43e5747a
UH
189 continue;
190 }
191 printf("%-3d ", devcnt++);
1e0f9ed9 192 print_dev_line(dev);
43e5747a 193 }
1e0f9ed9 194 if (demo_dev) {
43e5747a 195 printf("demo ");
1e0f9ed9 196 print_dev_line(demo_dev);
43e5747a
UH
197 }
198}
199
1e0f9ed9 200static void show_dev_detail(void)
43e5747a 201{
1e0f9ed9 202 struct sr_dev *dev;
43e5747a 203 struct sr_hwcap_option *hwo;
9c9c1080 204 const struct sr_samplerates *samplerates;
edd79b3f 205 int cap, *hwcaps, i;
9c9c1080
AS
206 char *s, *title;
207 const char *charopts, **stropts;
43e5747a 208
1e0f9ed9
UH
209 dev = parse_devstring(opt_dev);
210 if (!dev) {
43e5747a
UH
211 printf("No such device. Use -D to list all devices.\n");
212 return;
213 }
214
1e0f9ed9 215 print_dev_line(dev);
43e5747a 216
1e0f9ed9 217 if (sr_dev_info_get(dev, SR_DI_TRIGGER_TYPES,
fa230beb 218 (const void **)&charopts) == SR_OK) {
43e5747a
UH
219 printf("Supported triggers: ");
220 while (*charopts) {
221 printf("%c ", *charopts);
222 charopts++;
223 }
224 printf("\n");
225 }
226
227 title = "Supported options:\n";
a210c4cc 228 hwcaps = dev->driver->hwcap_get_all();
edd79b3f
UH
229 for (cap = 0; hwcaps[cap]; cap++) {
230 if (!(hwo = sr_hw_hwcap_get(hwcaps[cap])))
43e5747a
UH
231 continue;
232
233 if (title) {
234 printf("%s", title);
235 title = NULL;
236 }
237
edd79b3f 238 if (hwo->hwcap == SR_HWCAP_PATTERN_MODE) {
43e5747a 239 printf(" %s", hwo->shortname);
0a56f4ec 240 if (sr_dev_info_get(dev, SR_DI_PATTERNS,
fa230beb 241 (const void **)&stropts) == SR_OK) {
0a56f4ec 242 printf(" - supported patterns:\n");
9c9c1080
AS
243 for (i = 0; stropts[i]; i++)
244 printf(" %s\n", stropts[i]);
245 } else {
43e5747a 246 printf("\n");
43e5747a 247 }
edd79b3f 248 } else if (hwo->hwcap == SR_HWCAP_SAMPLERATE) {
43e5747a
UH
249 printf(" %s", hwo->shortname);
250 /* Supported samplerates */
1e0f9ed9 251 if (sr_dev_info_get(dev, SR_DI_SAMPLERATES,
fa230beb 252 (const void **)&samplerates) != SR_OK) {
43e5747a
UH
253 printf("\n");
254 continue;
255 }
256
257 if (samplerates->step) {
258 /* low */
259 if (!(s = sr_samplerate_string(samplerates->low)))
260 continue;
261 printf(" (%s", s);
c2c4a0de 262 g_free(s);
43e5747a
UH
263 /* high */
264 if (!(s = sr_samplerate_string(samplerates->high)))
265 continue;
266 printf(" - %s", s);
c2c4a0de 267 g_free(s);
43e5747a
UH
268 /* step */
269 if (!(s = sr_samplerate_string(samplerates->step)))
270 continue;
271 printf(" in steps of %s)\n", s);
c2c4a0de 272 g_free(s);
43e5747a
UH
273 } else {
274 printf(" - supported samplerates:\n");
275 for (i = 0; samplerates->list[i]; i++) {
9da0b05b 276 printf(" %s\n", sr_samplerate_string(samplerates->list[i]));
43e5747a
UH
277 }
278 }
279 } else {
280 printf(" %s\n", hwo->shortname);
281 }
282 }
283}
284
71b1ea4e
BV
285static void show_pd_detail(void)
286{
287 GSList *l;
288 struct srd_decoder *dec;
289 char **pdtokens, **pdtok, **ann, *doc;
e9ff6974 290 struct srd_probe *p;
71b1ea4e
BV
291
292 pdtokens = g_strsplit(opt_pds, ",", -1);
293 for (pdtok = pdtokens; *pdtok; pdtok++) {
2358775c 294 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
03996962 295 printf("Protocol decoder %s not found.\n", *pdtok);
71b1ea4e
BV
296 return;
297 }
298 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
299 dec->id, dec->name, dec->longname, dec->desc);
300 printf("License: %s\n", dec->license);
e9ff6974 301 printf("Annotations:\n");
71b1ea4e 302 if (dec->annotations) {
71b1ea4e
BV
303 for (l = dec->annotations; l; l = l->next) {
304 ann = l->data;
305 printf("- %s\n %s\n", ann[0], ann[1]);
306 }
e9ff6974
UH
307 } else {
308 printf("None.\n");
309 }
310 /* TODO: Print supported decoder options. */
311 printf("Required probes:\n");
312 if (dec->probes) {
313 for (l = dec->probes; l; l = l->next) {
314 p = l->data;
315 printf("- %s (%s): %s\n",
316 p->name, p->id, p->desc);
317 }
318 } else {
319 printf("None.\n");
320 }
321 printf("Optional probes:\n");
322 if (dec->opt_probes) {
323 for (l = dec->opt_probes; l; l = l->next) {
324 p = l->data;
325 printf("- %s (%s): %s\n",
326 p->name, p->id, p->desc);
327 }
328 } else {
329 printf("None.\n");
71b1ea4e 330 }
4359a4da 331 if ((doc = srd_decoder_doc_get(dec))) {
e9ff6974
UH
332 printf("Documentation:\n%s\n",
333 doc[0] == '\n' ? doc + 1 : doc);
71b1ea4e
BV
334 g_free(doc);
335 }
336 }
337
338 g_strfreev(pdtokens);
71b1ea4e
BV
339}
340
1e0f9ed9 341static void datafeed_in(struct sr_dev *dev, struct sr_datafeed_packet *packet)
43e5747a
UH
342{
343 static struct sr_output *o = NULL;
8364461d 344 static int probelist[SR_MAX_NUM_PROBES + 1] = { 0 };
43e5747a
UH
345 static uint64_t received_samples = 0;
346 static int unitsize = 0;
347 static int triggered = 0;
348 static FILE *outfile = NULL;
349 struct sr_probe *probe;
350 struct sr_datafeed_header *header;
351 struct sr_datafeed_logic *logic;
352 int num_enabled_probes, sample_size, ret, i;
353 uint64_t output_len, filter_out_len;
8de01efe 354 uint8_t *output_buf, *filter_out;
43e5747a
UH
355
356 /* If the first packet to come in isn't a header, don't even try. */
357 if (packet->type != SR_DF_HEADER && o == NULL)
358 return;
359
360 sample_size = -1;
361 switch (packet->type) {
362 case SR_DF_HEADER:
8170b8ea 363 g_debug("cli: Received SR_DF_HEADER");
43e5747a 364 /* Initialize the output module. */
c2c4a0de 365 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
8170b8ea 366 g_critical("Output module malloc failed.");
43e5747a
UH
367 exit(1);
368 }
369 o->format = output_format;
1e0f9ed9 370 o->dev = dev;
43e5747a
UH
371 o->param = output_format_param;
372 if (o->format->init) {
373 if (o->format->init(o) != SR_OK) {
8170b8ea 374 g_critical("Output format initialization failed.");
43e5747a
UH
375 exit(1);
376 }
377 }
378
379 header = packet->payload;
380 num_enabled_probes = 0;
381 for (i = 0; i < header->num_logic_probes; i++) {
1e0f9ed9 382 probe = g_slist_nth_data(dev->probes, i);
43e5747a
UH
383 if (probe->enabled)
384 probelist[num_enabled_probes++] = probe->index;
385 }
386 /* How many bytes we need to store num_enabled_probes bits */
387 unitsize = (num_enabled_probes + 7) / 8;
388
389 outfile = stdout;
390 if (opt_output_file) {
391 if (default_output_format) {
392 /* output file is in session format, which means we'll
393 * dump everything in the datastore as it comes in,
394 * and save from there after the session. */
395 outfile = NULL;
1e0f9ed9 396 ret = sr_datastore_new(unitsize, &(dev->datastore));
43e5747a 397 if (ret != SR_OK) {
8170b8ea 398 g_critical("Failed to create datastore.");
43e5747a
UH
399 exit(1);
400 }
401 } else {
402 /* saving to a file in whatever format was set
76ae913d 403 * with -O, so all we need is a filehandle */
43e5747a
UH
404 outfile = g_fopen(opt_output_file, "wb");
405 }
406 }
3a999920
BV
407 if (opt_pds) {
408 if (srd_session_start(num_enabled_probes, unitsize,
409 header->samplerate) != SRD_OK)
410 sr_session_halt();
411 }
43e5747a
UH
412 break;
413 case SR_DF_END:
8170b8ea 414 g_debug("cli: Received SR_DF_END");
43e5747a 415 if (!o) {
8170b8ea 416 g_debug("cli: double end!");
43e5747a
UH
417 break;
418 }
419 if (o->format->event) {
420 o->format->event(o, SR_DF_END, &output_buf, &output_len);
421 if (output_len) {
422 if (outfile)
423 fwrite(output_buf, 1, output_len, outfile);
c2c4a0de 424 g_free(output_buf);
43e5747a
UH
425 output_len = 0;
426 }
427 }
428 if (limit_samples && received_samples < limit_samples)
8170b8ea 429 g_warning("Device only sent %" PRIu64 " samples.",
43e5747a
UH
430 received_samples);
431 if (opt_continuous)
8170b8ea 432 g_warning("Device stopped after %" PRIu64 " samples.",
43e5747a
UH
433 received_samples);
434 sr_session_halt();
435 if (outfile && outfile != stdout)
436 fclose(outfile);
c2c4a0de 437 g_free(o);
43e5747a
UH
438 o = NULL;
439 break;
440 case SR_DF_TRIGGER:
8170b8ea 441 g_debug("cli: received SR_DF_TRIGGER");
43e5747a
UH
442 if (o->format->event)
443 o->format->event(o, SR_DF_TRIGGER, &output_buf,
444 &output_len);
445 triggered = 1;
446 break;
447 case SR_DF_LOGIC:
448 logic = packet->payload;
449 sample_size = logic->unitsize;
b46c4414 450 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
43e5747a 451 break;
43e5747a
UH
452 }
453
454 /* not supporting anything but SR_DF_LOGIC for now */
455
456 if (sample_size == -1 || logic->length == 0)
457 return;
458
459 /* Don't store any samples until triggered. */
460 if (opt_wait_trigger && !triggered)
461 return;
462
463 if (limit_samples && received_samples >= limit_samples)
464 return;
465
466 /* TODO: filters only support SR_DF_LOGIC */
467 ret = sr_filter_probes(sample_size, unitsize, probelist,
468 logic->data, logic->length,
469 &filter_out, &filter_out_len);
470 if (ret != SR_OK)
471 return;
472
473 /* what comes out of the filter is guaranteed to be packed into the
474 * minimum size needed to support the number of samples at this sample
475 * size. however, the driver may have submitted too much -- cut off
476 * the buffer of the last packet according to the sample limit.
477 */
478 if (limit_samples && (received_samples + logic->length / sample_size >
479 limit_samples * sample_size))
480 filter_out_len = limit_samples * sample_size - received_samples;
481
1e0f9ed9
UH
482 if (dev->datastore)
483 sr_datastore_put(dev->datastore, filter_out,
43e5747a
UH
484 filter_out_len, sample_size, probelist);
485
486 if (opt_output_file && default_output_format)
487 /* saving to a session file, don't need to do anything else
488 * to this data for now. */
489 goto cleanup;
490
491 if (opt_pds) {
37aeb8c5 492 if (srd_session_send(received_samples, (uint8_t*)filter_out,
838285aa 493 filter_out_len) != SRD_OK)
305661ad 494 sr_session_halt();
43e5747a
UH
495 } else {
496 output_len = 0;
497 if (o->format->data && packet->type == o->format->df_type)
498 o->format->data(o, filter_out, filter_out_len, &output_buf, &output_len);
499 if (output_len) {
500 fwrite(output_buf, 1, output_len, outfile);
c2c4a0de 501 g_free(output_buf);
43e5747a
UH
502 }
503 }
504
505cleanup:
506 g_free(filter_out);
507 received_samples += logic->length / sample_size;
43e5747a
UH
508}
509
9f4a898e
BV
510/* Register the given PDs for this session.
511 * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5"
43e5747a
UH
512 * That will instantiate two SPI decoders on the clock but different data
513 * lines.
514 */
1e0f9ed9 515static int register_pds(struct sr_dev *dev, const char *pdstring)
43e5747a 516{
9720f23a 517 GHashTable *pd_opthash;
878e90d9 518 struct srd_decoder_inst *di;
445950d3 519 int ret;
a1418b73 520 char **pdtokens, **pdtok, *pd_name;
43e5747a
UH
521
522 /* Avoid compiler warnings. */
1e0f9ed9 523 (void)dev;
43e5747a 524
445950d3 525 ret = 0;
120f9ee7
BV
526 pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal,
527 g_free, NULL);
a1418b73 528 pd_name = NULL;
120f9ee7
BV
529 pd_opthash = NULL;
530 pdtokens = g_strsplit(pdstring, ",", 0);
43e5747a 531 for (pdtok = pdtokens; *pdtok; pdtok++) {
9720f23a 532 if (!(pd_opthash = parse_generic_arg(*pdtok))) {
8170b8ea 533 g_critical("Invalid protocol decoder option '%s'.", *pdtok);
a1418b73 534 goto err_out;
43e5747a 535 }
a1418b73 536
9720f23a
BV
537 pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key"));
538 g_hash_table_remove(pd_opthash, "sigrok_key");
2358775c 539 if (srd_decoder_load(pd_name) != SRD_OK) {
8170b8ea 540 g_critical("Failed to load protocol decoder %s.", pd_name);
445950d3 541 ret = 1;
15d920a9
BV
542 goto err_out;
543 }
878e90d9 544 if (!(di = srd_inst_new(pd_name, pd_opthash))) {
8170b8ea 545 g_critical("Failed to instantiate protocol decoder %s.", pd_name);
445950d3 546 ret = 1;
a1418b73 547 goto err_out;
43e5747a 548 }
120f9ee7
BV
549
550 /* If no annotation list was specified, add them all in now.
551 * This will be pared down later to leave only the last PD
552 * in the stack.
553 */
b6bd032d
UH
554 if (!opt_pd_annotations)
555 g_hash_table_insert(pd_ann_visible,
556 g_strdup(di->inst_id), NULL);
a1418b73 557
67ce0d27
BV
558 /* Any keys left in the options hash are probes, where the key
559 * is the probe name as specified in the decoder class, and the
560 * value is the probe number i.e. the order in which the PD's
561 * incoming samples are arranged. */
445950d3
BV
562 if (srd_inst_probe_set_all(di, pd_opthash) != SRD_OK) {
563 ret = 1;
67ce0d27 564 goto err_out;
445950d3 565 }
67ce0d27
BV
566 g_hash_table_destroy(pd_opthash);
567 pd_opthash = NULL;
568 }
43e5747a 569
a1418b73 570err_out:
43e5747a 571 g_strfreev(pdtokens);
9720f23a
BV
572 if (pd_opthash)
573 g_hash_table_destroy(pd_opthash);
a1418b73
BV
574 if (pd_name)
575 g_free(pd_name);
43e5747a 576
445950d3 577 return ret;
43e5747a
UH
578}
579
120f9ee7
BV
580int setup_pd_stack(void)
581{
582 struct srd_decoder_inst *di_from, *di_to;
583 int ret, i;
584 char **pds;
585
586 /* Set up the protocol decoder stack. */
587 pds = g_strsplit(opt_pds, ",", 0);
588 if (g_strv_length(pds) > 1) {
589 if (opt_pd_stack) {
590 /* A stack setup was specified, use that. */
591 g_strfreev(pds);
592 pds = g_strsplit(opt_pd_stack, ",", 0);
593 if (g_strv_length(pds) < 2) {
594 g_strfreev(pds);
595 g_critical("Specify at least two protocol decoders to stack.");
596 return 1;
597 }
598 }
599
600 if (!(di_from = srd_inst_find_by_id(pds[0]))) {
601 g_critical("Cannot stack protocol decoder '%s': "
602 "instance not found.", pds[0]);
603 return 1;
604 }
605 for (i = 1; pds[i]; i++) {
606 if (!(di_to = srd_inst_find_by_id(pds[i]))) {
607 g_critical("Cannot stack protocol decoder '%s': "
608 "instance not found.", pds[i]);
609 return 1;
610 }
611 if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK)
612 return 1;
613
614 /* Don't show annotation from this PD. Only the last PD in
615 * the stack will be left on the annotation list (unless
616 * the annotation list was specifically provided).
617 */
618 if (!opt_pd_annotation)
b6bd032d
UH
619 g_hash_table_remove(pd_ann_visible,
620 di_from->inst_id);
120f9ee7
BV
621
622 di_from = di_to;
623 }
624 }
625 g_strfreev(pds);
626
627 return 0;
628}
629
630int setup_pd_annotations(void)
631{
632 GSList *l;
633 struct srd_decoder *dec;
634 int ann;
635 char **pds, **pdtok, **keyval, **ann_descr;
636
637 /* Set up custom list of PDs and annotations to show. */
b6bd032d
UH
638 if (opt_pd_annotations) {
639 pds = g_strsplit(opt_pd_annotations, ",", 0);
120f9ee7
BV
640 for (pdtok = pds; *pdtok && **pdtok; pdtok++) {
641 ann = 0;
642 keyval = g_strsplit(*pdtok, "=", 0);
643 if (!(dec = srd_decoder_get_by_id(keyval[0]))) {
644 g_critical("Protocol decoder '%s' not found.", keyval[0]);
645 return 1;
646 }
647 if (!dec->annotations) {
648 g_critical("Protocol decoder '%s' has no annotations.", keyval[0]);
649 return 1;
650 }
651 if (g_strv_length(keyval) == 2) {
652 for (l = dec->annotations; l; l = l->next, ann++) {
653 ann_descr = l->data;
654 if (!canon_cmp(ann_descr[0], keyval[1]))
655 /* Found it. */
656 break;
657 }
658 if (!l) {
659 g_critical("Annotation '%s' not found "
660 "for protocol decoder '%s'.", keyval[1], keyval[0]);
661 return 1;
662 }
663 }
664 g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]);
665 g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann));
666 g_strfreev(keyval);
667 }
668 g_strfreev(pds);
669 }
670
671 return 0;
672}
673
ad2bc491
BV
674int setup_output_format(void)
675{
676 GHashTable *fmtargs;
677 GHashTableIter iter;
678 gpointer key, value;
679 struct sr_output_format **outputs;
680 int i;
681 char *fmtspec;
682
683 if (!opt_output_format) {
684 opt_output_format = DEFAULT_OUTPUT_FORMAT;
685 /* we'll need to remember this so when saving to a file
686 * later, sigrok session format will be used.
687 */
688 default_output_format = TRUE;
689 }
690 fmtargs = parse_generic_arg(opt_output_format);
691 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
692 if (!fmtspec) {
693 g_critical("Invalid output format.");
694 return 1;
695 }
696 outputs = sr_output_list();
697 for (i = 0; outputs[i]; i++) {
698 if (strcmp(outputs[i]->id, fmtspec))
699 continue;
700 g_hash_table_remove(fmtargs, "sigrok_key");
701 output_format = outputs[i];
702 g_hash_table_iter_init(&iter, fmtargs);
703 while (g_hash_table_iter_next(&iter, &key, &value)) {
704 /* only supporting one parameter per output module
705 * for now, and only its value */
706 output_format_param = g_strdup(value);
707 break;
708 }
709 break;
710 }
711 if (!output_format) {
712 g_critical("Invalid output format %s.", opt_output_format);
713 return 1;
714 }
715 g_hash_table_destroy(fmtargs);
716
717 return 0;
718}
719
68cdf174 720void show_pd_annotation(struct srd_proto_data *pdata, void *cb_data)
213c6cea
BV
721{
722 int i;
ca07c711 723 char **annotations;
120f9ee7 724 gpointer ann_format;
213c6cea 725
68cdf174
UH
726 /* 'cb_data' is not used in this specific callback. */
727 (void)cb_data;
7cb9889f 728
120f9ee7 729 if (!pd_ann_visible)
213c6cea 730 return;
213c6cea 731
120f9ee7
BV
732 if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id,
733 NULL, &ann_format))
9720f23a 734 /* Not in the list of PDs whose annotations we're showing. */
9f4a898e 735 return;
120f9ee7
BV
736
737 if (pdata->ann_format != GPOINTER_TO_INT(ann_format))
738 /* We don't want this particular format from the PD. */
739 return;
9f4a898e 740
9720f23a 741 annotations = pdata->data;
41bc9e4f
BV
742 if (opt_loglevel > SR_LOG_WARN)
743 printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample);
ca07c711
UH
744 printf("%s: ", pdata->pdo->proto_id);
745 for (i = 0; annotations[i]; i++)
746 printf("\"%s\" ", annotations[i]);
213c6cea 747 printf("\n");
213c6cea
BV
748}
749
1e0f9ed9 750static int select_probes(struct sr_dev *dev)
43e5747a
UH
751{
752 struct sr_probe *probe;
753 char **probelist;
754 int max_probes, i;
755
756 if (!opt_probes)
757 return SR_OK;
758
759 /*
760 * This only works because a device by default initializes
761 * and enables all its probes.
762 */
1e0f9ed9 763 max_probes = g_slist_length(dev->probes);
43e5747a
UH
764 probelist = parse_probestring(max_probes, opt_probes);
765 if (!probelist) {
766 return SR_ERR;
767 }
768
769 for (i = 0; i < max_probes; i++) {
770 if (probelist[i]) {
2df65e9f 771 sr_dev_probe_name_set(dev, i + 1, probelist[i]);
43e5747a
UH
772 g_free(probelist[i]);
773 } else {
1e0f9ed9 774 probe = sr_dev_probe_find(dev, i + 1);
43e5747a
UH
775 probe->enabled = FALSE;
776 }
777 }
778 g_free(probelist);
779
780 return SR_OK;
781}
782
783/**
784 * Return the input file format which the CLI tool should use.
785 *
786 * If the user specified -I / --input-format, use that one. Otherwise, try to
787 * autodetect the format as good as possible. Failing that, return NULL.
788 *
789 * @param filename The filename of the input file. Must not be NULL.
790 * @param opt The -I / --input-file option the user specified (or NULL).
791 *
792 * @return A pointer to the 'struct sr_input_format' that should be used,
793 * or NULL if no input format was selected or auto-detected.
794 */
795static struct sr_input_format *determine_input_file_format(
796 const char *filename, const char *opt)
797{
798 int i;
799 struct sr_input_format **inputs;
800
801 /* If there are no input formats, return NULL right away. */
802 inputs = sr_input_list();
803 if (!inputs) {
8170b8ea 804 g_critical("No supported input formats available.");
43e5747a
UH
805 return NULL;
806 }
807
808 /* If the user specified -I / --input-format, use that one. */
809 if (opt) {
810 for (i = 0; inputs[i]; i++) {
811 if (strcasecmp(inputs[i]->id, opt_input_format))
812 continue;
8170b8ea
BV
813 g_debug("Using user-specified input file format '%s'.",
814 inputs[i]->id);
43e5747a
UH
815 return inputs[i];
816 }
817
818 /* The user specified an unknown input format, return NULL. */
8170b8ea
BV
819 g_critical("Error: specified input file format '%s' is "
820 "unknown.", opt_input_format);
43e5747a
UH
821 return NULL;
822 }
823
824 /* Otherwise, try to find an input module that can handle this file. */
825 for (i = 0; inputs[i]; i++) {
826 if (inputs[i]->format_match(filename))
827 break;
828 }
829
830 /* Return NULL if no input module wanted to touch this. */
831 if (!inputs[i]) {
8170b8ea 832 g_critical("Error: no matching input module found.");
43e5747a
UH
833 return NULL;
834 }
78912cc1
UH
835
836 g_debug("cli: Autodetected '%s' input format for file '%s'.",
837 inputs[i]->id, filename);
43e5747a 838
43e5747a
UH
839 return inputs[i];
840}
841
842static void load_input_file_format(void)
843{
844 struct stat st;
845 struct sr_input *in;
846 struct sr_input_format *input_format;
847
8170b8ea
BV
848 if (!(input_format = determine_input_file_format(opt_input_file,
849 opt_input_format)))
850 /* The exact cause was already logged. */
43e5747a 851 return;
43e5747a
UH
852
853 if (stat(opt_input_file, &st) == -1) {
8170b8ea 854 g_critical("Failed to load %s: %s", opt_input_file,
43e5747a
UH
855 strerror(errno));
856 exit(1);
857 }
858
859 /* Initialize the input module. */
c2c4a0de 860 if (!(in = g_try_malloc(sizeof(struct sr_input)))) {
8170b8ea 861 g_critical("Failed to allocate input module.");
43e5747a
UH
862 exit(1);
863 }
864 in->format = input_format;
865 in->param = input_format_param;
866 if (in->format->init) {
867 if (in->format->init(in) != SR_OK) {
8170b8ea 868 g_critical("Input format init failed.");
43e5747a
UH
869 exit(1);
870 }
871 }
872
1e0f9ed9 873 if (select_probes(in->vdev) > 0)
43e5747a
UH
874 return;
875
876 sr_session_new();
877 sr_session_datafeed_callback_add(datafeed_in);
1e0f9ed9 878 if (sr_session_dev_add(in->vdev) != SR_OK) {
8170b8ea 879 g_critical("Failed to use device.");
43e5747a
UH
880 sr_session_destroy();
881 return;
882 }
883
884 input_format->loadfile(in, opt_input_file);
885 if (opt_output_file && default_output_format) {
886 if (sr_session_save(opt_output_file) != SR_OK)
8170b8ea 887 g_critical("Failed to save session.");
43e5747a
UH
888 }
889 sr_session_destroy();
43e5747a
UH
890}
891
892static void load_input_file(void)
893{
894
895 if (sr_session_load(opt_input_file) == SR_OK) {
896 /* sigrok session file */
897 sr_session_datafeed_callback_add(datafeed_in);
898 sr_session_start();
899 sr_session_run();
900 sr_session_stop();
901 }
902 else {
903 /* fall back on input modules */
904 load_input_file_format();
905 }
43e5747a
UH
906}
907
1e0f9ed9 908int num_real_devs(void)
43e5747a 909{
1e0f9ed9
UH
910 struct sr_dev *dev;
911 GSList *devs, *l;
912 int num_devs;
913
914 num_devs = 0;
915 devs = sr_dev_list();
916 for (l = devs; l; l = l->next) {
917 dev = l->data;
918 if (!sr_dev_has_hwcap(dev, SR_HWCAP_DEMO_DEV))
919 num_devs++;
43e5747a
UH
920 }
921
1e0f9ed9 922 return num_devs;
43e5747a
UH
923}
924
1e0f9ed9 925static int set_dev_options(struct sr_dev *dev, GHashTable *args)
43e5747a
UH
926{
927 GHashTableIter iter;
928 gpointer key, value;
929 int ret, i;
930 uint64_t tmp_u64;
931 gboolean found;
932 gboolean tmp_bool;
933
934 g_hash_table_iter_init(&iter, args);
935 while (g_hash_table_iter_next(&iter, &key, &value)) {
936 found = FALSE;
edd79b3f 937 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
43e5747a
UH
938 if (strcmp(sr_hwcap_options[i].shortname, key))
939 continue;
940 if ((value == NULL) &&
941 (sr_hwcap_options[i].type != SR_T_BOOL)) {
8170b8ea 942 g_critical("Option '%s' needs a value.", (char *)key);
43e5747a
UH
943 return SR_ERR;
944 }
945 found = TRUE;
946 switch (sr_hwcap_options[i].type) {
947 case SR_T_UINT64:
948 ret = sr_parse_sizestring(value, &tmp_u64);
949 if (ret != SR_OK)
950 break;
a210c4cc 951 ret = dev->driver->dev_config_set(dev->driver_index,
8d145d46 952 sr_hwcap_options[i].hwcap, &tmp_u64);
43e5747a
UH
953 break;
954 case SR_T_CHAR:
a210c4cc 955 ret = dev->driver->dev_config_set(dev->driver_index,
8d145d46 956 sr_hwcap_options[i].hwcap, value);
43e5747a
UH
957 break;
958 case SR_T_BOOL:
959 if (!value)
960 tmp_bool = TRUE;
961 else
962 tmp_bool = sr_parse_boolstring(value);
a210c4cc 963 ret = dev->driver->dev_config_set(dev->driver_index,
edd79b3f 964 sr_hwcap_options[i].hwcap,
43e5747a
UH
965 GINT_TO_POINTER(tmp_bool));
966 break;
967 default:
968 ret = SR_ERR;
969 }
970
971 if (ret != SR_OK) {
8170b8ea 972 g_critical("Failed to set device option '%s'.", (char *)key);
43e5747a
UH
973 return ret;
974 }
975 else
976 break;
977 }
978 if (!found) {
8170b8ea 979 g_critical("Unknown device option '%s'.", (char *) key);
43e5747a
UH
980 return SR_ERR;
981 }
982 }
983
984 return SR_OK;
985}
986
987static void run_session(void)
988{
1e0f9ed9 989 struct sr_dev *dev;
43e5747a 990 GHashTable *devargs;
1e0f9ed9 991 int num_devs, max_probes, i;
9c9c1080 992 uint64_t time_msec;
43e5747a
UH
993 char **probelist, *devspec;
994
995 devargs = NULL;
1e0f9ed9
UH
996 if (opt_dev) {
997 devargs = parse_generic_arg(opt_dev);
43e5747a 998 devspec = g_hash_table_lookup(devargs, "sigrok_key");
1e0f9ed9
UH
999 dev = parse_devstring(devspec);
1000 if (!dev) {
8170b8ea 1001 g_critical("Device not found.");
43e5747a
UH
1002 return;
1003 }
1004 g_hash_table_remove(devargs, "sigrok_key");
1005 } else {
1e0f9ed9
UH
1006 num_devs = num_real_devs();
1007 if (num_devs == 1) {
43e5747a
UH
1008 /* No device specified, but there is only one. */
1009 devargs = NULL;
1e0f9ed9
UH
1010 dev = parse_devstring("0");
1011 } else if (num_devs == 0) {
8170b8ea 1012 g_critical("No devices found.");
43e5747a
UH
1013 return;
1014 } else {
8170b8ea 1015 g_critical("%d devices found, please select one.", num_devs);
43e5747a
UH
1016 return;
1017 }
1018 }
1019
1020 sr_session_new();
1021 sr_session_datafeed_callback_add(datafeed_in);
1022
1e0f9ed9 1023 if (sr_session_dev_add(dev) != SR_OK) {
8170b8ea 1024 g_critical("Failed to use device.");
43e5747a
UH
1025 sr_session_destroy();
1026 return;
1027 }
1028
1029 if (devargs) {
1e0f9ed9 1030 if (set_dev_options(dev, devargs) != SR_OK) {
43e5747a
UH
1031 sr_session_destroy();
1032 return;
1033 }
1034 g_hash_table_destroy(devargs);
1035 }
1036
1e0f9ed9 1037 if (select_probes(dev) != SR_OK)
43e5747a
UH
1038 return;
1039
1040 if (opt_continuous) {
a214a2eb 1041 if (!sr_driver_hwcap_exists(dev->driver, SR_HWCAP_CONTINUOUS)) {
8170b8ea 1042 g_critical("This device does not support continuous sampling.");
43e5747a
UH
1043 sr_session_destroy();
1044 return;
1045 }
1046 }
1047
1048 if (opt_triggers) {
1e0f9ed9 1049 probelist = sr_parse_triggerstring(dev, opt_triggers);
43e5747a
UH
1050 if (!probelist) {
1051 sr_session_destroy();
1052 return;
1053 }
1054
1e0f9ed9 1055 max_probes = g_slist_length(dev->probes);
43e5747a
UH
1056 for (i = 0; i < max_probes; i++) {
1057 if (probelist[i]) {
1e0f9ed9 1058 sr_dev_trigger_set(dev, i + 1, probelist[i]);
43e5747a
UH
1059 g_free(probelist[i]);
1060 }
1061 }
1062 g_free(probelist);
1063 }
1064
1065 if (opt_time) {
1066 time_msec = sr_parse_timestring(opt_time);
1067 if (time_msec == 0) {
8170b8ea 1068 g_critical("Invalid time '%s'", opt_time);
43e5747a
UH
1069 sr_session_destroy();
1070 return;
1071 }
1072
a214a2eb 1073 if (sr_driver_hwcap_exists(dev->driver, SR_HWCAP_LIMIT_MSEC)) {
a210c4cc 1074 if (dev->driver->dev_config_set(dev->driver_index,
8d145d46 1075 SR_HWCAP_LIMIT_MSEC, &time_msec) != SR_OK) {
8170b8ea 1076 g_critical("Failed to configure time limit.");
43e5747a
UH
1077 sr_session_destroy();
1078 return;
1079 }
1080 }
1081 else {
1082 /* time limit set, but device doesn't support this...
1083 * convert to samples based on the samplerate.
1084 */
1085 limit_samples = 0;
1e0f9ed9 1086 if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) {
9c9c1080
AS
1087 const uint64_t *samplerate;
1088
1e0f9ed9 1089 sr_dev_info_get(dev, SR_DI_CUR_SAMPLERATE,
fa230beb
UH
1090 (const void **)&samplerate);
1091 limit_samples = (*samplerate) * time_msec / (uint64_t)1000;
43e5747a
UH
1092 }
1093 if (limit_samples == 0) {
8170b8ea 1094 g_critical("Not enough time at this samplerate.");
43e5747a
UH
1095 sr_session_destroy();
1096 return;
1097 }
1098
a210c4cc 1099 if (dev->driver->dev_config_set(dev->driver_index,
8d145d46 1100 SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK) {
8170b8ea 1101 g_critical("Failed to configure time-based sample limit.");
43e5747a
UH
1102 sr_session_destroy();
1103 return;
1104 }
1105 }
1106 }
1107
1108 if (opt_samples) {
1109 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)
a210c4cc 1110 || (dev->driver->dev_config_set(dev->driver_index,
8d145d46 1111 SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK)) {
8170b8ea 1112 g_critical("Failed to configure sample limit.");
43e5747a
UH
1113 sr_session_destroy();
1114 return;
1115 }
1116 }
1117
a210c4cc 1118 if (dev->driver->dev_config_set(dev->driver_index,
1e0f9ed9 1119 SR_HWCAP_PROBECONFIG, (char *)dev->probes) != SR_OK) {
8170b8ea 1120 g_critical("Failed to configure probes.");
43e5747a
UH
1121 sr_session_destroy();
1122 return;
1123 }
1124
1125 if (sr_session_start() != SR_OK) {
8170b8ea 1126 g_critical("Failed to start session.");
43e5747a
UH
1127 sr_session_destroy();
1128 return;
1129 }
1130
1131 if (opt_continuous)
1132 add_anykey();
1133
1134 sr_session_run();
1135
1136 if (opt_continuous)
1137 clear_anykey();
1138
1139 if (opt_output_file && default_output_format) {
1140 if (sr_session_save(opt_output_file) != SR_OK)
8170b8ea 1141 g_critical("Failed to save session.");
43e5747a
UH
1142 }
1143 sr_session_destroy();
43e5747a
UH
1144}
1145
1146static void logger(const gchar *log_domain, GLogLevelFlags log_level,
68cdf174 1147 const gchar *message, gpointer cb_data)
43e5747a
UH
1148{
1149 /* Avoid compiler warnings. */
1150 (void)log_domain;
68cdf174 1151 (void)cb_data;
43e5747a
UH
1152
1153 /*
1154 * All messages, warnings, errors etc. go to stderr (not stdout) in
1155 * order to not mess up the CLI tool data output, e.g. VCD output.
1156 */
2d6ff326 1157 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
d740e6ac 1158 || opt_loglevel > SR_LOG_WARN) {
43e5747a
UH
1159 fprintf(stderr, "%s\n", message);
1160 fflush(stderr);
43e5747a
UH
1161 }
1162}
1163
1164int main(int argc, char **argv)
1165{
43e5747a
UH
1166 GOptionContext *context;
1167 GError *error;
43e5747a
UH
1168
1169 g_log_set_default_handler(logger, NULL);
43e5747a
UH
1170
1171 error = NULL;
1172 context = g_option_context_new(NULL);
1173 g_option_context_add_main_entries(context, optargs, NULL);
1174
1175 if (!g_option_context_parse(context, &argc, &argv, &error)) {
8170b8ea 1176 g_critical("%s", error->message);
43e5747a
UH
1177 return 1;
1178 }
1179
1180 /* Set the loglevel (amount of messages to output) for libsigrok. */
120f9ee7 1181 if (sr_log_loglevel_set(opt_loglevel) != SR_OK)
43e5747a 1182 return 1;
43e5747a 1183
6de7ec06 1184 /* Set the loglevel (amount of messages to output) for libsigrokdecode. */
120f9ee7 1185 if (srd_log_loglevel_set(opt_loglevel) != SRD_OK)
6de7ec06 1186 return 1;
6de7ec06 1187
43e5747a
UH
1188 if (sr_init() != SR_OK)
1189 return 1;
1190
1191 if (opt_pds) {
120f9ee7 1192 if (srd_init(NULL) != SRD_OK)
213c6cea 1193 return 1;
120f9ee7 1194 if (register_pds(NULL, opt_pds) != 0)
213c6cea 1195 return 1;
4359a4da 1196 if (srd_pd_output_callback_add(SRD_OUTPUT_ANN,
b6bd032d 1197 show_pd_annotations, NULL) != SRD_OK)
120f9ee7
BV
1198 return 1;
1199 if (setup_pd_stack() != 0)
1200 return 1;
1201 if (setup_pd_annotations() != 0)
213c6cea 1202 return 1;
9f4a898e
BV
1203 }
1204
ad2bc491 1205 if (setup_output_format() != 0)
43e5747a 1206 return 1;
43e5747a
UH
1207
1208 if (opt_version)
1209 show_version();
1e0f9ed9
UH
1210 else if (opt_list_devs)
1211 show_dev_list();
43e5747a
UH
1212 else if (opt_input_file)
1213 load_input_file();
1214 else if (opt_samples || opt_time || opt_continuous)
1215 run_session();
1e0f9ed9
UH
1216 else if (opt_dev)
1217 show_dev_detail();
71b1ea4e
BV
1218 else if (opt_pds)
1219 show_pd_detail();
43e5747a
UH
1220 else
1221 printf("%s", g_option_context_get_help(context, TRUE, NULL));
1222
1223 if (opt_pds)
1224 srd_exit();
1225
1226 g_option_context_free(context);
43e5747a
UH
1227 sr_exit();
1228
1229 return 0;
1230}