-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.rl
117 lines (104 loc) · 2.66 KB
/
parse.rl
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <task.h>
#include "dreadlock.h"
enum CMDLIST {
CMD_LOCK,
CMD_UNLOCK
};
%%{
machine command;
timeout = (digit+)
@{
if (!timeout_s)
timeout_s = p;
};
key = (alnum+)
@{
if (!key) {
key = p;
keylen = 1;
}
else {
keylen++;
}
};
lock =
( [lL][oO][cC][kK] ' '+ key ' '+ timeout ' '*)
@{cmd = CMD_LOCK;};
unlock =
( [uU][nN][lL][oO][cC][kK] ' '+ key ' '*)
@{cmd = CMD_UNLOCK;};
crlf = '\r\n';
main := |*
( lock | unlock ) crlf
@{
int timeout_ms = -1;
if (timeout_s) {
timeout_ms = atoi(timeout_s);
timeout_s = NULL;
}
switch(cmd) {
case CMD_LOCK: do_lock(st, key, keylen, timeout_ms); break;
case CMD_UNLOCK: do_unlock(st, key, keylen); break;
default: assert(0); break;
}
key = NULL;
};
*|;
}%%
%% write data;
#define PARSE_BUFSIZ 10000
void ragel_parse(dreadlock_client_state *st) {
int cs;
char *key = NULL;
int keylen = -1;
int cmd = -1;
char *timeout_s = NULL;
char buf[PARSE_BUFSIZ];
char *writeptr = buf;
char *eof = NULL;
char *ts = NULL, *te = NULL, *p = NULL, *pe = NULL;
int act;
(void)act;
int canwrite = PARSE_BUFSIZ;
%% write init;
while (1) {
int bread = fdread(st->fd, writeptr, canwrite);
if (bread == 0)
eof = writeptr;
else if (bread < 0) {
switch (errno) {
case EINTR: continue;
default:
perror("dreadlock client read");
eof = writeptr;
bread = 0;
}
}
p = writeptr;
pe = writeptr + bread;
%% write exec;
if (cs == command_error) {
fprintf(stderr, "syntax error from client\n");
break;
}
if (eof)
break;
if (ts) {
int have = te - ts;
memmove(buf, ts, have);
ts = buf;
te = buf + have;
canwrite = PARSE_BUFSIZ - have;
writeptr = buf + have;
}
else {
writeptr = buf;
canwrite = PARSE_BUFSIZ;
}
}
}