-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfg-parse.y
463 lines (384 loc) · 9.29 KB
/
cfg-parse.y
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
%code requires {
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <assert.h>
#include <limits.h>
#include "types.h"
#include "misc.h"
extern int cfg_debug;
struct cfg_pstate {
struct config* cfg;
struct remote* nextrmt;
};
int parse_cfg(FILE* cfgfile, struct config* cfg);
}
%union {
char* str;
int64_t i;
double d;
direction_t dir;
struct noderef noderef;
struct action action;
struct focus_hint focushint;
struct mouse_switch mouseswitch;
struct focus_target focus_target;
struct link link;
struct logfile logfile;
struct {
float duration;
int numsteps;
} dim_fade;
};
%code {
#include "misc.h"
#include "cfg-lex.yy.h"
static void cfg_error(struct cfg_pstate* st, char const*);
}
%{
#define fail_parse(st, msg) do { \
cfg_error(st, msg); \
YYABORT; \
} while (0)
static struct remote* new_uninit_remote(void)
{
struct remote* rmt = xcalloc(sizeof(*rmt));
rmt->sshpid = -1;
rmt->msgchan.send.fd = rmt->msgchan.recv.fd = -1;
rmt->state = CS_NEW;
rmt->params = new_kvmap();
rmt->node.remote = rmt;
rmt->scrollmult = 1;
return rmt;
}
%}
%token EQ
%token LBRACE RBRACE
%token LBRACKET RBRACKET
%token KW_YES KW_NO
%token <i> INTEGER
%token <d> DECIMAL
%token <str> STRING
%token KW_MASTER KW_REMOTE KW_TOPOLOGY
%token KW_REMOTESHELL KW_BINDADDR KW_HOTKEY KW_FOCUS KW_RECONNECT KW_SLIDE
%token KW_IDENTITYFILE KW_PARAM KW_SHOWFOCUS KW_DIMINACTIVE KW_FLASHACTIVE
%token KW_NONE KW_MOUSESWITCH KW_MULTITAP KW_SHOWNULLSWITCH KW_HOTKEYONLY KW_QUIT
%token KW_PREVIOUS KW_RECONMAXINT KW_RECONMAXTRIES KW_CLEARCLIPBOARD
%token KW_USEPRIVATEAGENT KW_SCROLLMULT KW_HALT_RECONNECTS KW_STEP_LOGLEVEL
%token KW_USER KW_HOSTNAME KW_PORT KW_REMOTECMD
%token KW_LEFT KW_RIGHT KW_UP KW_DOWN
%token KW_LOGFILE KW_LOGLEVEL KW_SYSLOG KW_STDERR
%token KW_ERROR KW_WARN KW_INFO KW_VERBOSE KW_DEBUG KW_DEBUG2
%token END 0 "EOF"
%type <d> realnum
%type <noderef> node
%type <action> action
%type <focushint> focushint
%type <mouseswitch> mouseswitch
%type <dim_fade> dim_fade
%type <focus_target> focus_target
%type <link> link
%type <dir> direction opt_direction
%type <str> opt_string
%type <i> loglevel
%type <logfile> logfile
%type <i> port_setting fade_steps show_nullswitch yesno_bool
%type <str> bindaddr_setting user_setting remotecmd_setting remoteshell_setting
%type <str> identityfile_setting
%type <i> scrollmult_setting
%debug
%name-prefix "cfg_"
%parse-param {struct cfg_pstate* st}
%error-verbose
%%
input: blocks;
/*
* Bison compat hack: Bison 3.x warns about empty rules not explicitly marked
* with '%empty', but earlier versions don't support the '%empty' marker, and
* also don't support -Wno-empty-rule to avoid the warning. This is a
* best-effort kludge at supporting both with as few warnings as possible
* (just the one empty-rule warning for this, which serves as kind of a
* pseudo-%empty marker in other rules).
*/
EMPTY: /* empty -- this would be %empty if older bisons supported it. */;
blocks: EMPTY
| blocks block {
};
block: master_block {
}
| topology_block {
}
| remote_block {
};
opt_string: EMPTY { $$ = NULL; }
| STRING { $$ = $1; };
master_block: KW_MASTER opt_string LBRACE master_opts RBRACE {
st->cfg->master.name = $2;
};
master_opts: EMPTY
| master_opt master_opts {
};
realnum: INTEGER { $$ = (double)$1; }
| DECIMAL { $$ = $1; };
yesno_bool: KW_YES { $$ = 1; }
| KW_NO { $$ = 0; };
port_setting: KW_PORT EQ INTEGER {
$$ = $3;
if ($$ < 1 || $$ > USHRT_MAX)
fail_parse(st, "invalid port number");
};
user_setting: KW_USER EQ STRING { $$ = $3; };
bindaddr_setting: KW_BINDADDR EQ STRING { $$ = $3; };
remotecmd_setting: KW_REMOTECMD EQ STRING { $$ = $3; };
scrollmult_setting: KW_SCROLLMULT EQ INTEGER { $$ = $3; };
remoteshell_setting: KW_REMOTESHELL EQ STRING {
$$ = expand_word($3);
if (!$$)
fail_parse(st, "bad syntax in remote-shell");
};
identityfile_setting: KW_IDENTITYFILE EQ STRING {
$$ = expand_word($3);
if (!$$)
fail_parse(st, "bad syntax in identity-file");
};
fade_steps: EMPTY { $$ = 1; }
| INTEGER { $$ = $1; };
dim_fade: EMPTY {
$$.duration = 0.0;
$$.numsteps = 1;
}
| realnum INTEGER {
$$.duration = $1;
$$.numsteps = $2;
};
focushint: KW_DIMINACTIVE realnum dim_fade {
$$.type = FH_DIM_INACTIVE;
$$.brightness = $2;
$$.duration = (uint64_t)($3.duration * 1000000);
$$.fade_steps = $3.numsteps;
if ($$.brightness < 0.0 || $3.duration < 0.0)
fail_parse(st, "dim-inactive arguments must be >= 0");
if ($$.fade_steps < 1)
fail_parse(st, "dim-inactive fade-steps must be >= 1");
}
| KW_FLASHACTIVE realnum realnum fade_steps {
$$.type = FH_FLASH_ACTIVE;
$$.brightness = $2;
$$.duration = (uint64_t)($3 * 1000000);
$$.fade_steps = $4;
if ($$.brightness < 0.0 || $3 < 0.0)
fail_parse(st, "flash-active arguments must be >= 0");
if ($$.fade_steps < 1)
fail_parse(st, "flash-active fade-steps must be >= 1");
}
| KW_NONE {
$$.type = FH_NONE;
};
mouseswitch: KW_MULTITAP INTEGER realnum {
$$.type = MS_MULTITAP;
$$.num = $2;
$$.window = (uint64_t)($3 * 1000000);
if ($$.num <= 0 || $$.num > ((EDGESTATE_HISTLEN+1) / 2))
fail_parse(st, "invalid multi-tap count");
if ($3 < 0.0)
fail_parse(st, "multi-tap time window must be >= 0");
}
| KW_SLIDE {
/* 'slide' is just shorthand for 'multi-tap 1 ...' */
$$.type = MS_MULTITAP;
$$.num = 1;
$$.window = 0;
}
| KW_NONE {
$$.type = MS_NONE;
};
show_nullswitch: KW_YES { $$ = NS_YES; }
| KW_NO { $$ = NS_NO; }
| KW_HOTKEYONLY { $$ = NS_HOTKEYONLY; };
loglevel: KW_ERROR { $$ = LL_ERROR; }
| KW_WARN { $$ = LL_WARN; }
| KW_INFO { $$ = LL_INFO; }
| KW_VERBOSE { $$ = LL_VERBOSE; }
| KW_DEBUG { $$ = LL_DEBUG; }
| KW_DEBUG2 { $$ = LL_DEBUG2; };
logfile: KW_SYSLOG { $$.type = LF_SYSLOG; }
| KW_STDERR { $$.type = LF_STDERR; }
| KW_NONE { $$.type = LF_NONE; }
| STRING {
$$.type = LF_FILE;
$$.path = expand_word($1);
if (!$$.path)
fail_parse(st, "bad syntax in log-file");
};
master_opt: remoteshell_setting {
st->cfg->ssh_defaults.remoteshell = $1;
}
| port_setting {
st->cfg->ssh_defaults.port = $1;
}
| bindaddr_setting {
st->cfg->ssh_defaults.bindaddr = $1;
}
| identityfile_setting {
st->cfg->ssh_defaults.identityfile = $1;
}
| user_setting {
st->cfg->ssh_defaults.username = $1;
}
| remotecmd_setting {
st->cfg->ssh_defaults.remotecmd = $1;
}
| KW_SHOWFOCUS EQ focushint {
st->cfg->focus_hint = $3;
}
| KW_MOUSESWITCH EQ mouseswitch {
st->cfg->mouseswitch = $3;
}
| KW_SHOWNULLSWITCH EQ show_nullswitch {
st->cfg->show_nullswitch = $3;
}
| KW_RECONMAXTRIES EQ INTEGER {
st->cfg->reconnect.max_tries = $3;
}
| KW_RECONMAXINT EQ realnum {
st->cfg->reconnect.max_interval = (uint64_t)($3 * 1000000);
}
| KW_USEPRIVATEAGENT EQ yesno_bool {
st->cfg->use_private_ssh_agent = $3;
}
| KW_LOGFILE EQ logfile {
st->cfg->log.file = $3;
}
| KW_LOGLEVEL EQ loglevel {
st->cfg->log.level = $3;
}
| KW_HOTKEY LBRACKET STRING RBRACKET EQ action {
struct hotkey* hk = xmalloc(sizeof(*hk));
hk->key_string = $3;
hk->action = $6;
hk->next = st->cfg->hotkeys;
st->cfg->hotkeys = hk;
};
focus_target: direction {
$$.type = FT_DIRECTION;
$$.dir = $1;
}
| node {
$$.type = FT_NODE;
$$.nr = $1;
}
| KW_PREVIOUS {
$$.type = FT_PREVIOUS;
};
action: KW_FOCUS focus_target {
$$.type = AT_FOCUS;
$$.target = $2;
}
| KW_CLEARCLIPBOARD {
$$.type = AT_CLEARCLIPBOARD;
}
| KW_RECONNECT {
$$.type = AT_RECONNECT;
}
| KW_HALT_RECONNECTS {
$$.type = AT_HALT_RECONNECTS;
}
| KW_QUIT {
$$.type = AT_QUIT;
}
| KW_STEP_LOGLEVEL INTEGER {
$$.type = AT_STEP_LOGLEVEL;
$$.loglevel_delta = $2;
};
topology_block: KW_TOPOLOGY LBRACE links RBRACE {
};
links: EMPTY {
}
| links link {
struct link* ln = xmalloc(sizeof(*ln));
*ln = $2;
ln->next = st->cfg->topology;
st->cfg->topology = ln;
};
direction: KW_LEFT { $$ = LEFT; }
| KW_RIGHT { $$ = RIGHT; }
| KW_UP { $$ = UP; }
| KW_DOWN { $$ = DOWN; };
opt_direction: EMPTY { $$ = NO_DIR; }
| direction { $$ = $1; };
link: node direction EQ node opt_direction {
$$.a.nr = $1;
$$.a.dir = $2;
$$.b.nr = $4;
$$.b.dir = $5;
};
node: STRING {
$$.type = NT_TMPNAME;
$$.name = $1;
}
| KW_MASTER {
$$.type = NT_TMPNAME;
$$.name = NULL;
};
remote_block: KW_REMOTE STRING remote_opts {
struct remote* rmt = st->nextrmt;
rmt->node.name = $2;
rmt->next = st->cfg->remotes;
if (!rmt->hostname)
rmt->hostname = xstrdup(rmt->node.name);
st->cfg->remotes = rmt;
st->nextrmt = new_uninit_remote();
};
remote_opts: EMPTY
| LBRACE remote_optlist RBRACE {
};
remote_optlist: EMPTY
| remote_optlist remote_opt {
};
remote_opt: KW_HOSTNAME EQ STRING {
st->nextrmt->hostname = $3;
}
| remoteshell_setting {
st->nextrmt->sshcfg.remoteshell = $1;
}
| port_setting {
st->nextrmt->sshcfg.port = $1;
}
| bindaddr_setting {
st->nextrmt->sshcfg.bindaddr = $1;
}
| identityfile_setting {
st->nextrmt->sshcfg.identityfile = $1;
}
| user_setting {
st->nextrmt->sshcfg.username = $1;
}
| remotecmd_setting {
st->nextrmt->sshcfg.remotecmd = $1;
}
| scrollmult_setting {
st->nextrmt->scrollmult = $1;
}
| KW_PARAM LBRACKET STRING RBRACKET EQ STRING {
kvmap_put(st->nextrmt->params, $3, $6);
xfree($3);
xfree($6);
};
%%
int parse_cfg(FILE* cfgfile, struct config* cfg)
{
int status;
struct cfg_pstate pstate;
cfg_restart(cfgfile);
pstate.cfg = cfg;
pstate.nextrmt = new_uninit_remote();
status = cfg_parse(&pstate);
cfg_lex_destroy();
xfree(pstate.nextrmt->params);
xfree(pstate.nextrmt);
return status;
}
static void cfg_error(struct cfg_pstate* st, char const* s)
{
initerr("config parse error: %s\n", s);
}