File: parser.y

package info (click to toggle)
libiio 0.26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,832 kB
  • sloc: ansic: 21,880; python: 1,844; cs: 1,232; sh: 1,062; cpp: 688; yacc: 441; xml: 192; lex: 172; makefile: 40
file content (475 lines) | stat: -rw-r--r-- 11,588 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
%{
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * libiio - Library for interfacing industrial I/O (IIO) devices
 *
 * Copyright (C) 2014 Analog Devices, Inc.
 * Author: Paul Cercueil <paul.cercueil@analog.com>
 */

#include "ops.h"
#include "parser.h"

#include <errno.h>
#include <string.h>

void yyerror(yyscan_t scanner, const char *msg);
%}

%code requires {
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void *yyscan_t;
#endif

#include "../iio-config.h"
#include "../debug.h"

#include <stdbool.h>
#include <sys/socket.h>

int yylex();
int yylex_init_extra(void *d, yyscan_t *scanner);
int yylex_destroy(yyscan_t yyscanner);

void * yyget_extra(yyscan_t scanner);
ssize_t yy_input(yyscan_t scanner, char *buf, size_t max_size);

#define ECHO do { \
		struct parser_pdata *pdata = yyget_extra(yyscanner); \
		write_all(pdata, yytext, yyleng); \
	} while (0)

#define YY_INPUT(buf,result,max_size) do { \
		ssize_t res = yy_input(yyscanner, buf, max_size); \
		result = res <= 0 ? YY_NULL : (size_t) res; \
	} while (0)
}

%define api.pure
%lex-param { yyscan_t scanner }
%parse-param { yyscan_t scanner }

%union {
	char *word;
	struct iio_device *dev;
	struct iio_channel *chn;
	long value;
}

%token SPACE
%token END

%token VERSION
%token EXIT
%token HELP
%token OPEN
%token CLOSE
%token PRINT
%token ZPRINT
%token READ
%token READBUF
%token WRITEBUF
%token WRITE
%token SETTRIG
%token GETTRIG
%token TIMEOUT
%token DEBUG_ATTR
%token BUFFER_ATTR
%token IN_OUT
%token CYCLIC
%token SET
%token BUFFERS_COUNT

%token <word> WORD
%token <dev> DEVICE
%token <chn> CHANNEL
%token <value> VALUE;

%destructor { IIO_DEBUG("Freeing token \"%s\"\n", $$); free($$); } <word>

%start Line
%%

Line:
	END {
		YYACCEPT;
	}
	| EXIT END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		pdata->stop = true;
		YYACCEPT;
	}
	| HELP END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		output(pdata, "Available commands:\n\n"
		"\tHELP\n"
		"\t\tPrint this help message\n"
		"\tEXIT\n"
		"\t\tClose the current session\n"
		"\tPRINT\n"
		"\t\tDisplays a XML string corresponding to the current IIO context\n"
		"\tZPRINT\n"
		"\t\tGet a compressed XML string corresponding to the current IIO context\n"
		"\tVERSION\n"
		"\t\tGet the version of libiio in use\n"
		"\tTIMEOUT <timeout_ms>\n"
		"\t\tSet the timeout (in ms) for I/O operations\n"
		"\tOPEN <device> <samples_count> <mask> [CYCLIC]\n"
		"\t\tOpen the specified device with the given mask of channels\n"
		"\tCLOSE <device>\n"
		"\t\tClose the specified device\n"
		"\tREAD <device> DEBUG|BUFFER|[INPUT|OUTPUT <channel>] [<attribute>]\n"
		"\t\tRead the value of an attribute\n"
		"\tWRITE <device> DEBUG|BUFFER|[INPUT|OUTPUT <channel>] [<attribute>] <bytes_count>\n"
		"\t\tSet the value of an attribute\n"
		"\tREADBUF <device> <bytes_count>\n"
		"\t\tRead raw data from the specified device\n"
		"\tWRITEBUF <device> <bytes_count>\n"
		"\t\tWrite raw data to the specified device\n"
		"\tGETTRIG <device>\n"
		"\t\tGet the name of the trigger used by the specified device\n"
		"\tSETTRIG <device> [<trigger>]\n"
		"\t\tSet the trigger to use for the specified device\n"
		"\tSET <device> BUFFERS_COUNT <count>\n"
		"\t\tSet the number of kernel buffers for the specified device\n");
		YYACCEPT;
	}
	| VERSION END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		char buf[128];
		snprintf(buf, sizeof(buf), "%u.%u.%-7.7s\n", LIBIIO_VERSION_MAJOR,
						LIBIIO_VERSION_MINOR, LIBIIO_VERSION_GIT);
		output(pdata, buf);
		YYACCEPT;
	}
	| PRINT END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		const char *xml = iio_context_get_xml(pdata->ctx);
		if (!pdata->verbose) {
			char buf[128];
			snprintf(buf, sizeof(buf), "%lu\n", (unsigned long) strlen(xml));
			output(pdata, buf);
		}
		output(pdata, xml);
		output(pdata, "\n");
		YYACCEPT;
	}
	| ZPRINT END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (pdata->xml_zstd) {
			if (!pdata->verbose) {
				char buf[128];
				snprintf(buf, sizeof(buf), "%lu\n", (unsigned long)pdata->xml_zstd_len);
				output(pdata, buf);
			}
			if (write_all(pdata, pdata->xml_zstd, pdata->xml_zstd_len) <= 0)
				pdata->stop = true;
			output(pdata, "\n");
			YYACCEPT;
		} else {
			char buf[128];
			snprintf(buf, sizeof(buf), "%d\n", -EINVAL);
			output(pdata, buf);
			YYABORT;
		}
	}
	| TIMEOUT SPACE WORD END {
		char *word = $3;
		struct parser_pdata *pdata = yyget_extra(scanner);
		unsigned int timeout = (unsigned int) atoi(word);
		int ret = set_timeout(pdata, timeout);
		free(word);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| OPEN SPACE DEVICE SPACE WORD SPACE WORD SPACE CYCLIC END {
		char *nb = $5, *mask = $7;
		struct parser_pdata *pdata = yyget_extra(scanner);
		unsigned long samples_count = atol(nb);
		int ret = open_dev(pdata, $3, samples_count, mask, true);
		free(nb);
		free(mask);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| OPEN SPACE DEVICE SPACE WORD SPACE WORD END {
		char *nb = $5, *mask = $7;
		struct parser_pdata *pdata = yyget_extra(scanner);
		unsigned long samples_count = atol(nb);
		int ret = open_dev(pdata, $3, samples_count, mask, false);
		free(nb);
		free(mask);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| CLOSE SPACE DEVICE END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		int ret = close_dev(pdata, $3);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (read_dev_attr(pdata, $3, NULL, IIO_ATTR_TYPE_DEVICE) < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE SPACE WORD END {
		char *attr = $5;
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = read_dev_attr(pdata, $3, attr, IIO_ATTR_TYPE_DEVICE);
		free(attr);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE SPACE DEBUG_ATTR END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (read_dev_attr(pdata, $3, NULL, IIO_ATTR_TYPE_DEBUG) < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE SPACE DEBUG_ATTR SPACE WORD END {
		char *attr = $7;
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = read_dev_attr(pdata, $3, attr, IIO_ATTR_TYPE_DEBUG);
		free(attr);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE SPACE BUFFER_ATTR END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (read_dev_attr(pdata, $3, NULL, IIO_ATTR_TYPE_BUFFER) < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE SPACE BUFFER_ATTR SPACE WORD END {
		char *attr = $7;
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = read_dev_attr(pdata, $3, attr, IIO_ATTR_TYPE_BUFFER);
		free(attr);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE SPACE IN_OUT SPACE CHANNEL END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (read_chn_attr(pdata, $7, NULL) < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READ SPACE DEVICE SPACE IN_OUT SPACE CHANNEL SPACE WORD END {
		char *attr = $9;
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = read_chn_attr(pdata, $7, attr);
		free(attr);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| READBUF SPACE DEVICE SPACE WORD END {
		char *len = $5;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = rw_dev(pdata, $3, nb, false);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITEBUF SPACE DEVICE SPACE WORD END {
		char *len = $5;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = rw_dev(pdata, $3, nb, true);

		/* Discard additional data */
		yyclearin;

		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE WORD END {
		char *len = $5;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_dev_attr(pdata, $3, NULL, nb, IIO_ATTR_TYPE_DEVICE);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE WORD SPACE WORD END {
		char *attr = $5, *len = $7;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_dev_attr(pdata, $3, attr, nb, IIO_ATTR_TYPE_DEVICE);
		free(attr);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE DEBUG_ATTR SPACE WORD END {
		char *len = $7;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_dev_attr(pdata, $3, NULL, nb, IIO_ATTR_TYPE_DEBUG);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE DEBUG_ATTR SPACE WORD SPACE WORD END {
		char *attr = $7, *len = $9;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_dev_attr(pdata, $3, attr, nb, IIO_ATTR_TYPE_DEBUG);
		free(attr);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE BUFFER_ATTR SPACE WORD END {
		char *len = $7;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_dev_attr(pdata, $3, NULL, nb, IIO_ATTR_TYPE_BUFFER);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE BUFFER_ATTR SPACE WORD SPACE WORD END {
		char *attr = $7, *len = $9;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_dev_attr(pdata, $3, attr, nb, IIO_ATTR_TYPE_BUFFER);
		free(attr);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE IN_OUT SPACE CHANNEL SPACE WORD END {
		char *len = $9;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_chn_attr(pdata, $7, NULL, nb);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| WRITE SPACE DEVICE SPACE IN_OUT SPACE CHANNEL SPACE WORD SPACE WORD END {
		char *attr = $9, *len = $11;
		unsigned long nb = atol(len);
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = write_chn_attr(pdata, $7, attr, nb);
		free(attr);
		free(len);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| SETTRIG SPACE DEVICE SPACE WORD END {
		char *trig = $5;
		struct parser_pdata *pdata = yyget_extra(scanner);
		ssize_t ret = set_trigger(pdata, $3, trig);
		free(trig);
		if (ret < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| SETTRIG SPACE DEVICE END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (set_trigger(pdata, $3, NULL) < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| GETTRIG SPACE DEVICE END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (get_trigger(pdata, $3) < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| SET SPACE DEVICE SPACE BUFFERS_COUNT SPACE VALUE END {
		struct parser_pdata *pdata = yyget_extra(scanner);
		if (set_buffers_count(pdata, $3, $7) < 0)
			YYABORT;
		else
			YYACCEPT;
	}
	| error END {
		yyclearin;
		yyerrok;
		YYACCEPT;
	}
	;

%%

void yyerror(yyscan_t scanner, const char *msg)
{
	struct parser_pdata *pdata = yyget_extra(scanner);
	if (pdata->verbose) {
		output(pdata, "ERROR: ");
		output(pdata, msg);
		output(pdata, "\n");
	} else {
		char buf[128];
		snprintf(buf, sizeof(buf), "%i\n", -EINVAL);
		output(pdata, buf);
	}
}

ssize_t yy_input(yyscan_t scanner, char *buf, size_t max_size)
{
	struct parser_pdata *pdata = yyget_extra(scanner);
	ssize_t ret;

	ret = read_line(pdata, buf, max_size);
	if (ret < 0)
		return ret;
	if (ret == 0)
		return -EIO;

	if ((size_t) ret == max_size)
		buf[max_size - 1] = '\0';

	return ret;
}