-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
457 lines (413 loc) · 12 KB
/
parser.c
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
/*
* QEmacs, tiny but powerful multimode editor
*
* Copyright (c) 2000-2002 Fabrice Bellard.
* Copyright (c) 2000-2020 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
#include "variables.h"
/* config file parsing */
/* CG: error messages should go to the *error* buffer.
* displayed as a popup upon start.
*/
typedef struct QEmacsDataSource {
const char *filename;
FILE *f;
EditBuffer *b;
const char *str;
int line_num;
int pos, len;
int offset, stop;
} QEmacsDataSource;
static int expect_token(const char **pp, int tok)
{
if (skip_spaces(pp) == tok) {
++*pp;
return 1;
} else {
put_status(NULL, "'%c' expected", tok);
return 0;
}
}
static int qe_cfg_parse_string(EditState *s, const char **pp,
char *dest, int size)
{
const char *p = *pp;
int delim = *p++;
int res = 0;
int pos = 0;
for (;;) {
/* encoding issues deliberately ignored */
int c = *p;
if (c == '\n' || c == '\0') {
put_status(s, "Unterminated string");
res = -1;
break;
}
p++;
if (c == delim)
break;
if (c == '\\') {
c = *p++;
switch (c) {
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
}
}
/* XXX: silently truncate overlong string constants */
if (pos < size - 1)
dest[pos++] = c;
}
if (pos < size)
dest[pos] = '\0';
*pp = p;
return res;
}
static char *data_gets(QEmacsDataSource *ds, char *buf, int size)
{
if (ds->f) {
return fgets(buf, size, ds->f);
}
if (ds->b) {
/* EditBuffer should not be modified during parse! */
if (ds->offset < ds->stop && ds->offset < ds->b->total_size) {
// XXX: parsing will continue beyond `stop` to end of line */
eb_fgets(ds->b, buf, size, ds->offset, &ds->offset);
return buf;
}
return NULL;
}
if (ds->str) {
int i;
for (i = 0; i < size - 1; i++) {
if (ds->pos >= ds->len
|| (buf[i] = ds->str[ds->pos++]) == '\n')
break;
}
buf[i] = '\0';
return i ? buf : NULL;
}
return NULL;
}
static int qe_parse_script(EditState *s, QEmacsDataSource *ds)
{
QEmacsState *qs = s->qe_state;
QErrorContext ec;
char line[1024], str[1024];
char prompt[64], cmd[128], *q, *strp;
const char *p, *r;
int line_num;
CmdDef *d;
int nb_args, sep, i, skip, incomment;
CmdArg args[MAX_CMD_ARGS];
unsigned char args_type[MAX_CMD_ARGS];
ec = qs->ec;
incomment = skip = 0;
line_num = ds->line_num;
/* Should parse whole config file in a single read, or load it via
* a buffer */
for (;;) {
if (data_gets(ds, line, sizeof(line)) == NULL)
break;
line_num++;
qs->ec.filename = ds->filename;
qs->ec.function = NULL;
qs->ec.lineno = line_num;
p = line;
again:
if (incomment)
goto comment;
if (skip_spaces(&p) == '\0')
continue;
if (*p == '/') {
if (p[1] == '/') /* line comment */
continue;
if (p[1] == '*') { /* multiline comment */
p += 2;
incomment = 1;
comment:
while (*p) {
if (*p++ == '*' && *p == '/') {
p++;
incomment = 0;
break;
}
}
if (incomment)
continue;
goto again;
}
}
if (p[0] == '}') {
/* simplistic 1 level if block skip feature */
p++;
skip_spaces(&p);
skip = 0;
}
if (skip)
continue;
if (p[0] == '\0')
continue;
/* XXX: should parse numbers, strings and symbols */
get_str(&p, cmd, sizeof(cmd), "{}();=/");
if (*cmd == '\0') {
put_status(s, "Syntax error");
continue;
}
/* transform '_' to '-' */
q = cmd;
while (*q) {
if (*q == '_')
*q = '-';
q++;
}
/* simplistic 1 level if block skip feature */
if (strequal(cmd, "if")) {
if (!expect_token(&p, '('))
goto fail;
skip = !strtol(p, (char**)&p, 0);
if (!expect_token(&p, ')') || !expect_token(&p, '{'))
goto fail;
continue;
}
#ifndef CONFIG_TINY
{
/* search for variable */
struct VarDef *vp;
vp = qe_find_variable(cmd);
if (vp) {
if (!expect_token(&p, '='))
goto fail;
skip_spaces(&p);
if (*p == '\"' || *p == '\'') {
if (qe_cfg_parse_string(s, &p, str, countof(str)))
goto fail;
qe_set_variable(s, cmd, str, 0);
} else {
qe_set_variable(s, cmd, NULL, strtol(p, (char**)&p, 0));
}
skip_spaces(&p);
if (*p != ';' && *p != '\0')
put_status(s, "Syntax error '%s'", cmd);
continue;
}
}
#endif
if (!expect_token(&p, '('))
goto fail;
/* search for command */
d = qe_find_cmd(cmd);
if (!d) {
put_status(s, "Unknown command '%s'", cmd);
continue;
}
nb_args = 0;
/* construct argument type list */
r = d->name + strlen(d->name) + 1;
if (*r == '*') {
r++;
if (s->b->flags & BF_READONLY) {
put_status(s, "Buffer is read only");
continue;
}
}
/* first argument is always the window */
args_type[nb_args++] = CMD_ARG_WINDOW;
for (;;) {
unsigned char arg_type;
int ret;
ret = parse_arg(&r, &arg_type, prompt, countof(prompt),
NULL, 0, NULL, 0);
if (ret < 0)
goto badcmd;
if (ret == 0)
break;
if (nb_args >= MAX_CMD_ARGS) {
badcmd:
put_status(s, "Badly defined command '%s'", cmd);
goto fail;
}
args[nb_args].p = NULL;
args_type[nb_args++] = arg_type & CMD_ARG_TYPE_MASK;
}
sep = '\0';
strp = str;
for (i = 0; i < nb_args; i++) {
/* pseudo arguments: skip them */
switch (args_type[i]) {
case CMD_ARG_WINDOW:
args[i].s = s;
continue;
case CMD_ARG_INTVAL:
args[i].n = (int)(intptr_t)d->val;
continue;
case CMD_ARG_STRINGVAL:
/* CG: kludge for xxx-mode functions and named kbd macros */
args[i].p = prompt;
continue;
}
if (sep) {
/* CG: Should test for arg list too short. */
/* CG: Could supply default arguments. */
if (!expect_token(&p, sep))
goto fail;
}
sep = ',';
skip_spaces(&p);
switch (args_type[i]) {
case CMD_ARG_INT:
r = p;
args[i].n = strtol(p, (char**)&p, 0);
if (p == r) {
put_status(s, "Number expected for arg %d", i);
goto fail;
}
break;
case CMD_ARG_STRING:
if (*p != '\"' && *p != '\'') {
/* XXX: should convert number to string */
put_status(s, "String expected for arg %d", i);
goto fail;
}
if (qe_cfg_parse_string(s, &p, strp,
str + countof(str) - strp) < 0)
{
goto fail;
}
args[i].p = strp;
strp += strlen(strp) + 1;
break;
}
}
if (skip_spaces(&p) != ')') {
put_status(s, "Too many arguments for %s", d->name);
goto fail;
}
qs->this_cmd_func = d->action.func;
qs->ec.function = d->name;
call_func(d->sig, d->action, nb_args, args, args_type);
qs->last_cmd_func = qs->this_cmd_func;
if (qs->active_window)
s = qs->active_window;
check_window(&s);
continue;
fail:
;
}
qs->ec = ec;
return 0;
}
void do_eval_expression(EditState *s, const char *expression, int argval)
{
QEmacsDataSource ds = { 0 };
ds.filename = "<string>";
ds.str = expression;
ds.len = strlen(expression);
qe_parse_script(s, &ds);
}
static int do_eval_buffer_region(EditState *s, int start, int stop)
{
QEmacsDataSource ds = { 0 };
ds.filename = s->b->name;
ds.b = s->b;
if (stop < 0)
stop = INT_MAX;
if (start < 0)
start = INT_MAX;
if (start < stop) {
ds.offset = start;
ds.stop = stop;
} else {
ds.offset = stop;
ds.stop = start;
}
return qe_parse_script(s, &ds);
}
void do_eval_region(EditState *s)
{
s->region_style = 0; /* deactivate region hilite */
do_eval_buffer_region(s, s->b->mark, s->offset);
}
void do_eval_buffer(EditState *s)
{
do_eval_buffer_region(s, 0, s->b->total_size);
}
int parse_config_file(EditState *s, const char *filename)
{
QEmacsDataSource ds = { 0 };
int res;
ds.filename = filename;
ds.f = fopen(filename, "r");
if (!ds.f)
return -1;
res = qe_parse_script(s, &ds);
fclose(ds.f);
return res;
}
#ifndef CONFIG_TINY
int qe_load_session(EditState *s)
{
return parse_config_file(s, ".qesession");
}
void do_save_session(EditState *s, int popup)
{
EditBuffer *b = eb_scratch("*session*", BF_UTF8);
time_t now;
eb_printf(b, "// qemacs version: %s\n", QE_VERSION);
now = time(NULL);
eb_printf(b, "// session saved: %s\n", ctime(&now));
qe_save_variables(s, b);
qe_save_macros(s, b);
qe_save_open_files(s, b);
qe_save_window_layout(s, b);
if (popup) {
b->offset = 0;
b->flags |= BF_READONLY;
show_popup(s, b, "QEmacs session");
} else {
eb_write_buffer(b, 0, b->total_size, ".qesession");
eb_free(&b);
}
}
#endif
static CmdDef parser_commands[] = {
CMD2( KEY_META(':'), KEY_NONE,
"eval-expression", do_eval_expression, ESsi,
"s{Eval: }|expression|ui")
CMD0( KEY_NONE, KEY_NONE,
"eval-region", do_eval_region)
CMD0( KEY_NONE, KEY_NONE,
"eval-buffer", do_eval_buffer)
#ifndef CONFIG_TINY
CMD1( KEY_NONE, KEY_NONE,
"save-session", do_save_session, 1)
#endif
CMD_DEF_END,
};
static int parser_init(void)
{
qe_register_cmd_table(parser_commands, NULL);
return 0;
}
qe_module_init(parser_init);