File: input.g

package info (click to toggle)
termdebug 2.2+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 456 kB
  • ctags: 685
  • sloc: ansic: 5,008; sh: 560; makefile: 48; lex: 46
file content (342 lines) | stat: -rw-r--r-- 8,631 bytes parent folder | download
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
/* Copyright (C) 2010-2013 G.P. Halkes
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3, as
   published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

%options "generate-lexer-wrapper generate-symbol-table lowercase-symbols";
%start parse, input;

{
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include "replay.h"
#include "common.h"

static td_bool start_parsed;
static td_bool first_directive;
ExpNode *script;

extern int line_number;
extern const char *file_name;
extern char *yytext;

void LLmessage(int class) {
	switch (class) {
		case LL_MISSINGEOF:
			fatal("%s:%d: Unexpected %s at end of file\n", file_name, line_number, LLgetSymbol(LLsymb));
		case LL_DELETE:
			fatal("%s:%d: Unexpected %s\n", file_name, line_number, LLgetSymbol(LLsymb));
		default:
			fatal("%s:%d: Expected %s before %s\n", file_name, line_number, LLgetSymbol(class), LLgetSymbol(LLsymb));
	}
}

void *safe_calloc(size_t size) {
	void *retval = calloc(1, size);
	if (retval == NULL)
		fatal("Out of memory while reading script\n");
	return retval;
}

char *safe_strdup_remove_quotes(const char *string) {
	size_t size;
	char *retval = strdup(string + 1);

	if (retval == NULL)
		fatal("Out of memory while reading script\n");
	if ((size = strlen(retval)) > 0)
		retval[size - 1] = 0;
	return retval;
}

/** Convert a string from the input format to an internally usable string.
	@param string A @a Token with the string to be converted.
	@param descr A description of the string to be included in error messages.
	@return The length of the resulting string.

	The use of this function processes escape characters. The converted
	characters are written in the original string.
*/
static size_t parseEscapes(char *string) {
	size_t maxReadPosition = strlen(string);
	size_t readPosition = 0, writePosition = 0;
	size_t i;

	while(readPosition < maxReadPosition) {
		if (string[readPosition] == '\\') {
			readPosition++;

			if (readPosition == maxReadPosition)
				fatal("Interal error\n");

			switch(string[readPosition++]) {
				case 'n':
					string[writePosition++] = '\n';
					break;
				case 'r':
					string[writePosition++] = '\r';
					break;
				case '\'':
					string[writePosition++] = '\'';
					break;
				case '\\':
					string[writePosition++] = '\\';
					break;
				case 't':
					string[writePosition++] = '\t';
					break;
				case 'b':
					string[writePosition++] = '\b';
					break;
				case 'f':
					string[writePosition++] = '\f';
					break;
				case 'a':
					string[writePosition++] = '\a';
					break;
				case 'v':
					string[writePosition++] = '\v';
					break;
				case '?':
					string[writePosition++] = '\?';
					break;
				case '"':
					string[writePosition++] = '"';
					break;
				case 'x': {
					/* Hexadecimal escapes */
					unsigned int value = 0;
					/* Read at most two characters, or as many as are valid. */
					for (i = 0; i < 2 && (readPosition + i) < maxReadPosition && isxdigit(string[readPosition + i]); i++) {
						value <<= 4;
						if (isdigit(string[readPosition + i]))
							value += (int) (string[readPosition + i] - '0');
						else
							value += (int) (tolower(string[readPosition + i]) - 'a') + 10;
						if (value > UCHAR_MAX)
							fatal("%s:%d: Invalid hexadecimal escape sequence\n", file_name, line_number);
					}
					readPosition += i;

					if (i == 0)
						fatal("%s:%d: Invalid hexadecimal escape sequence\n", file_name, line_number);

					string[writePosition++] = (char) value;
					break;
				}
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7': {
					/* Octal escapes */
					int value = (int)(string[readPosition - 1] - '0');
					size_t maxIdx = string[readPosition - 1] < '4' ? 2 : 1;
					for (i = 0; i < maxIdx && readPosition + i < maxReadPosition && string[readPosition + i] >= '0' && string[readPosition + i] <= '7'; i++)
						value = value * 8 + (int)(string[readPosition + i] - '0');

					readPosition += i;

					string[writePosition++] = (char) value;
					break;
				}
				default:
					string[writePosition++] = string[readPosition - 1];
					break;
			}
		} else {
			string[writePosition++] = string[readPosition++];
		}
	}
	/* Terminate string. */
	string[writePosition] = 0;
	return writePosition;
}

}
%token ENV, START, WINDOW_SIZE, EXPECT, SEND, EXPECT_EXIT, INTERACT, NUMBER, STRING, EXPECT_SUSPEND, REQUIRE_VERSION;

input {
	ExpNode **current = &script;
	start_parsed = td_false;
	first_directive = td_true;
} :
	[
		command
		{
			*current = command;
			current = &(*current)->next;
			first_directive = td_false;
		}
	]+
	{
		check_script(file_name, script);
	}
;

command<ExpNode *> {
	StringListNode **next;
	LLretval = safe_calloc(sizeof(ExpNode));
	next = &LLretval->un.args;
} :
	ENV
	STRING
	{
		if (start_parsed)
			fatal("%s:%d: env directive after start directive\n", file_name, line_number);
		LLretval->type = EXP_ENV;
		LLretval->line = line_number;
		*next = safe_calloc(sizeof(StringListNode));
		(*next)->string = safe_strdup_remove_quotes(yytext);
		next = &(*next)->next;
	}
	STRING
	{
		*next = safe_calloc(sizeof(StringListNode));
		(*next)->string = safe_strdup_remove_quotes(yytext);
		(*next)->length = parseEscapes((*next)->string);
	}
|
	REQUIRE_VERSION
	NUMBER
	{
		if (!first_directive)
			fatal("%s:%d: require_version directive should be the first directive in the recording\n", file_name, line_number);
		LLretval->type = EXP_REQUIRE_VERSION;
		LLretval->line = line_number;
		LLretval->un.number = atoi(yytext);
		if (LLretval->un.number < 1)
			fatal("%s:%d: lowest possible require_version is 1\n", file_name, line_number);
	}
|
	START
	{
		if (start_parsed)
			fatal("%s:%d: Multiple start directives specified\n", file_name, line_number);
		start_parsed = td_true;
		LLretval->type = EXP_START;
		LLretval->line = line_number;
	}
	[
		STRING
		{
			*next = safe_calloc(sizeof(StringListNode));
			(*next)->string = safe_strdup_remove_quotes(yytext);
			(*next)->length = parseEscapes((*next)->string);
			next = &(*next)->next;
		}
	]+
|
	WINDOW_SIZE
	NUMBER
	{
		LLretval->type = EXP_WINDOW_SIZE;
		LLretval->line = line_number;
		LLretval->un.size.columns = atoi(yytext);
	}
	NUMBER
	{
		LLretval->un.size.rows = atoi(yytext);
	}
	[
		NUMBER
		{
			LLretval->un.size.delay = atoi(yytext);
		}
		[
			'>'
			NUMBER
			{
				LLretval->un.size.min_delay = atoi(yytext);
				if (LLretval->un.size.min_delay > LLretval->un.size.delay)
					LLretval->un.size.min_delay = LLretval->un.size.delay;
			}
		]?
	]?
|
	EXPECT
	{
		if (!start_parsed)
			fatal("%s:%d: expect directive before start directive\n", file_name, line_number);
		LLretval->type = EXP_EXPECT;
		LLretval->line = line_number;
	}
	[
		STRING
		{
			*next = safe_calloc(sizeof(StringListNode));
			(*next)->string = safe_strdup_remove_quotes(yytext);
			(*next)->length = parseEscapes((*next)->string);
			next = &(*next)->next;
		}
	]+
|
	SEND
	{
		if (!start_parsed)
			fatal("%s:%d: send directive before start directive\n", file_name, line_number);
		LLretval->type = EXP_SEND;
		LLretval->line = line_number;
	}
	[
		NUMBER
		{
			*next = safe_calloc(sizeof(StringListNode));
			(*next)->delay = atoi(yytext);
		}
		[
			'>'
			NUMBER
			{
				(*next)->min_delay = atoi(yytext);
				if ((*next)->delay < (*next)->min_delay)
					(*next)->min_delay = (*next)->delay;
			}
		]?
		STRING
		{
			(*next)->string = safe_strdup_remove_quotes(yytext);
			(*next)->length = parseEscapes((*next)->string);
			next = &(*next)->next;
		}
	]+
|
	EXPECT_EXIT
	NUMBER
	{
		if (!start_parsed)
			fatal("%s:%d: expect_exit directive before start directive\n", file_name, line_number);
		LLretval->type = EXP_EXPECT_EXIT;
		LLretval->line = line_number;
		LLretval->un.number = atoi(yytext);
	}
|
	EXPECT_SUSPEND
	{
		if (!start_parsed)
			fatal("%s:%d: expect_suspend directive before start directive\n", file_name, line_number);
		LLretval->type = EXP_EXPECT_SUSPEND;
		LLretval->line = line_number;
	}
|
	INTERACT
	{
		if (!start_parsed)
			fatal("%s:%d: interact directive before start directive\n", file_name, line_number);
		LLretval->type = EXP_INTERACT;
		LLretval->line = line_number;
	}
;