-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
zen_parse.c
271 lines (243 loc) · 6.94 KB
/
zen_parse.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
/* This file is part of Zenroom (https://zenroom.dyne.org)
*
* Copyright (C) 2017-2019 Dyne.org foundation
* designed, written and maintained by Denis Roio <jaromil@dyne.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
// auxiliary functions for parsing Zencode, used inside zencode.lua
// optimizations also happen here
// #include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include <zenroom.h>
#include <zen_memory.h>
#include <zen_error.h>
#include <lualib.h>
#include <lauxlib.h>
#define MAX_DEPTH 4096
static char low[MAX_LINE]; // 1KB max for a single zencode line
// parse the first word until the first space, returns a new string
static int lua_parse_prefix(lua_State* L) {
const char *line;
size_t size;
line = luaL_checklstring(L,1,&size); SAFE(line);
register unsigned short int c;
unsigned short fspace = 0;
// skip space in front
for(c=0; c<size && c<MAX_LINE && c<USHRT_MAX; c++) {
if( !isspace(line[c]) ) break;
fspace++; }
for(; c<size && c<MAX_LINE && c<USHRT_MAX; c++) {
if( isspace(line[c]) ) {
low[c] = '\0'; break; }
low[c] = tolower(line[c]);
}
if(c>size || c==MAX_LINE) lua_pushnil(L);
else lua_pushlstring(L,&low[fspace],c-fspace);
return 1;
}
// internal use, trims the string to a provided destination which is
// pre-allocated
static size_t trimto(char *dest, const char *src, const size_t len) {
register unsigned short int c;
register unsigned short int d;
for(c=0; c<len && isspace(src[c]); c++); // skip front space
for(d=0; c<len; c++, d++) dest[d] = src[c];
dest[d] = '\0'; // null termination
return(d);
}
static int lua_strcasecmp(lua_State *L) {
const char *a, *b;
size_t la, lb;
char *ta, *tb;
a = luaL_checklstring(L,1,&la); SAFE(a);
b = luaL_checklstring(L,2,&lb); SAFE(b);
if(la>MAX_LINE) lerror(L, "strcasecmp: arg #1 MAX_LINE limit hit");
if(lb>MAX_LINE) lerror(L, "strcasecmp: arg #2 MAX_LINE limit hit");
ta = malloc(la+1);
tb = malloc(lb+1);
la = trimto(ta, a, la);
lb = trimto(tb, b, lb);
if(la != lb) { lua_pushboolean(L,0); goto end; }
if( strcasecmp(ta,tb) == 0 ) { lua_pushboolean(L,1); goto end; }
// else
lua_pushboolean(L,0);
end:
free(ta);
free(tb);
return 1;
}
// trim whitespace in front and at end of string
static int lua_trim_spaces(lua_State* L) {
const char* front;
const char* end;
size_t size;
front = luaL_checklstring(L,1,&size);
end = &front[size - 1];
while (size && isspace(*front)) {
size--;
front++;
}
while (size && isspace(*end)) {
size--;
end--;
}
lua_pushlstring(L,front,(size_t)(end - front) + 1);
return 1;
}
// trim whitespace or single quote in front and at end of string
static int lua_trim_quotes(lua_State* L) {
const char* front;
const char* end;
size_t size;
front = luaL_checklstring(L,1,&size);
end = &front[size - 1];
while (size && (isspace(*front) || *front == '\'')) {
size--;
front++;
}
while (size && (isspace(*end) || *end == '\'')) {
size--;
end--;
}
lua_pushlstring(L,front,(size_t)(end - front) + 1);
return 1;
}
static int lua_unserialize_json(lua_State* L) {
const char *in;
size_t size;
register int level = 0;
register char *p;
register char in_literal_str = 0;
in = luaL_checklstring(L, 1, &size);
char brakets[MAX_DEPTH];
p = (char*)in;
while (size && isspace(*p) ) { size--; p++; } // first char
while (size && (*p == 0x0) ) { size--; p++; } // first char
if(!size) { lua_pushnil(L); return 1; }
if (*p == '{' || *p == '[') {
brakets[level] = *p == '{' ? '}' : ']';
size--;
level++;
} else {
func(L, "JSON doesn't starts with '{' nor '[', char found: %c (%02x)", *p, *p);
lua_pushnil(L);
return 1;
} // ok, level is 1
for( p++ ; size>0 ; size--, p++ ) {
if(in_literal_str) {
// a string literal end with a " which is not escaped, i.e. \"
// in case a string literal ends with \\", it ends
// p-1 and p-2 cannot be outside buffer because a JSON dictionary
// starts at least with {"
if(*p == '"' && (*(p-1) != '\\' || *(p-2) == '\\')) {
in_literal_str = 0;
}
} else {
if(*p=='"') in_literal_str = 1;
else {
if(*p=='{' || *p=='[') {
if(level < MAX_DEPTH){
brakets[level] = *p == '{' ? '}' : ']';
}
level++;
}
if(*p=='}' || *p==']') {
level--;
if(level < MAX_DEPTH && brakets[level] != *p){
lerror(L, "JSON format error, expected: %c, found %c at position %d", brakets[level], *p, (size_t)(p - in)+1);
lua_pushnil(L);
return 1;
}
}
if(level==0) { // end of first block
lua_pushlstring(L, in, (size_t)(p - in)+1);
p++;
while (size && isspace(*p) ) { size--; p++; } // remove final spaces
while (size && (*p == 0x0) ) { size--; p++; } // remove final char
lua_pushlstring(L, p, size);
return 2;
}
}
}
}
// should never be here
lerror(L, "JSON has malformed beginning or end");
return 0;
}
// removed because of unexplained segfault when used inside pcall to
// parse zencode: set_rule and set_scenario will explode, also seems
// to perform worse than pure Lua (see PR #709)
#if 0
char* strtok_single(char* str, char const* delims)
{
static char* src = NULL;
char *p, *ret = NULL;
if (str != NULL)
src = str;
if (src == NULL)
return NULL;
if ((p = strpbrk(src, delims)) != NULL) {
*p = 0;
ret = src;
src = ++p;
} else {
ret = src;
src = NULL;
}
return ret;
}
static int lua_strtok(lua_State* L) {
const char DEFAULT_SEP[] = " ";
char copy[MAX_FILE];
const char *sep = DEFAULT_SEP;
const char *in;
size_t size;
register int i = 1;
register char *token;
in = luaL_checklstring(L, 1, &size);
if(!in) {
lua_pushnil(L);
return 1;
}
if (lua_gettop(L) > 1) {
sep = luaL_checklstring(L, 2, NULL);
}
lua_newtable(L);
memcpy(copy, in, size+1);
token = strtok_single(copy, sep);
while(token != NULL) {
lua_pushlstring(L, token, strlen(token));
lua_rawseti(L, -2, i);
token = strtok_single(NULL, sep);
i = i + 1;
}
return 1;
}
#endif
void zen_add_parse(lua_State *L) {
// override print() and io.write()
static const struct luaL_Reg custom_parser [] =
{ {"parse_prefix", lua_parse_prefix},
{"strcasecmp", lua_strcasecmp},
{"trim", lua_trim_spaces},
{"trimq", lua_trim_quotes},
{"jsontok", lua_unserialize_json},
{NULL, NULL} };
lua_getglobal(L, "_G");
luaL_setfuncs(L, custom_parser, 0); // for Lua versions 5.2 or greater
lua_pop(L, 1);
}