-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmemfile.c
426 lines (386 loc) · 10.4 KB
/
memfile.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
#include <stddef.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include "ipc.h"
#include "memfile.h"
/* used to identify metatable */
#define NAME "ipc.memfile"
#define OBJECT_INDEX 1
#define CLOSE_INDEX 2
#define FLUSH_INDEX 3
typedef struct {
char* addr;
size_t n;
size_t p;
int flags;
} memfile;
static memfile* tomemfile( lua_State* L, int idx ) {
memfile* mf = luaL_checkudata( L, idx, NAME );
if( mf->addr == NULL )
luaL_error( L, "attempt to use closed in-memory file" );
return mf;
}
static int read_all( lua_State* L, memfile* mf ) {
lua_pushlstring( L, mf->addr + mf->p, mf->n - mf->p );
mf->p = mf->n;
return LUA_TSTRING;
}
static int read_n( lua_State* L, memfile* mf, size_t n ) {
if( mf->p == mf->n )
lua_pushnil( L );
else {
if( mf->n - mf->p < n )
n = mf->n - mf->p;
lua_pushlstring( L, mf->addr + mf->p, n );
mf->p += n;
}
return lua_type( L, -1 );
}
static int read_line( lua_State* L, memfile* mf,
int store_nl ) {
if( mf->p == mf->n )
lua_pushnil( L );
else {
luaL_Buffer b;
int oldc = '\0';
int c = '\0';
luaL_buffinit( L, &b );
while( mf->p != mf->n &&
(c = mf->addr[ mf->p++ ]) != '\n' ) {
if( oldc == '\r' ) {
luaL_addchar( &b, oldc );
oldc = '\0';
}
if( c == '\r' ) {
oldc = '\r';
} else
luaL_addchar( &b, c );
}
if( oldc == '\r' && (c != '\n' || store_nl) )
luaL_addchar( &b, oldc );
if( c == '\n' && store_nl )
luaL_addchar( &b, c );
luaL_pushresult( &b );
}
return lua_type( L, -1 );
}
static int lines_iter( lua_State* L ) {
memfile* mf = tomemfile( L, lua_upvalueindex( 1 ) );
int nupvals = lua_tointeger( L, lua_upvalueindex( 2 ) );
int i = 3;
lua_settop( L, 0 );
luaL_checkstack( L, nupvals-2, "not enough stack space" );
for( i = 3; i <= nupvals; ++i ) {
if( lua_type( L, lua_upvalueindex( i ) ) == LUA_TNUMBER ) {
lua_Number n = lua_tonumber( L, lua_upvalueindex( i ) );
lua_Integer x = lua_tointeger( L, lua_upvalueindex( i ) );
if( x < 0 || (lua_Number)x != n )
luaL_error( L, "invalid format" );
if( LUA_TNIL == read_n( L, mf, x ) )
return i-2;
} else {
char const* fmt = lua_tostring( L, lua_upvalueindex( i ) );
if( !fmt )
luaL_error( L, "invalid format" );
if( *fmt == '*' ) /* '*' is optional in later Lua versions */
fmt++;
switch( *fmt ) {
case 'a':
if( LUA_TNIL == read_all( L, mf ) )
return i-2;
break;
case 'l':
if( LUA_TNIL == read_line( L, mf, 0 ) )
return i-2;
break;
case 'L':
if( LUA_TNIL == read_line( L, mf, 1 ) )
return i-2;
break;
default:
luaL_error( L, "invalid format" );
break;
}
}
}
return nupvals-2;
}
static int memfile_flush( lua_State* L ) {
memfile* mf = tomemfile( L, 1 );
int top;
if( (mf->flags & MEMFILE_W) == 0 ) {
lua_pushnil( L );
lua_pushliteral( L, "permission denied" );
return 2;
}
lua_getuservalue( L, 1 );
top = lua_gettop( L );
lua_rawgeti( L, -1, FLUSH_INDEX );
lua_rawgeti( L, -2, OBJECT_INDEX );
lua_pushinteger( L, (lua_Integer)mf->p );
if( lua_type( L, -3 ) == LUA_TFUNCTION )
lua_call( L, 2, LUA_MULTRET );
else {
lua_pop( L, 3 );
lua_pushboolean( L, 1 );
}
return lua_gettop( L )-top;
}
static int memfile_lines( lua_State* L ) {
memfile* mf = tomemfile( L, 1 );
int top = lua_gettop( L );
if( (mf->flags & MEMFILE_R) == 0 )
luaL_error( L, "permission denied" );
if( top == 1 ) {
lua_pushliteral( L, "l" );
top = 2;
};
lua_pushinteger( L, top+1 );
lua_insert( L, 2 ); /* memfile, nupvals, arg1, ... */
lua_pushcclosure( L, lines_iter, top+1 );
return 1;
}
static int memfile_read( lua_State* L ) {
memfile* mf = tomemfile( L, 1 );
int top = lua_gettop( L );
int i = 2;
if( (mf->flags & MEMFILE_R) == 0 ) {
lua_pushnil( L );
lua_pushliteral( L, "permission denied" );
return 2;
}
if( top == 1 ) {
lua_pushliteral( L, "l" );
top = 2;
}
luaL_checkstack( L, top-1, "not enough stack space" );
for( i = 2; i <= top; ++i ) {
if( lua_type( L, i ) == LUA_TNUMBER ) {
lua_Number n = lua_tonumber( L, i );
lua_Integer x = lua_tointeger( L, i );
if( x < 0 || (lua_Number)x != n )
luaL_argerror( L, i, "invalid format" );
if( LUA_TNIL == read_n( L, mf, x ) )
return i-1;
} else {
char const* fmt = lua_tostring( L, i );
if( !fmt )
luaL_argerror( L, i, "invalid format" );
if( *fmt == '*' ) /* '*' is optional in later Lua versions */
fmt++;
switch( *fmt ) {
case 'a':
if( LUA_TNIL == read_all( L, mf ) )
return i-1;
break;
case 'l':
if( LUA_TNIL == read_line( L, mf, 0 ) )
return i-1;
break;
case 'L':
if( LUA_TNIL == read_line( L, mf, 1 ) )
return i-1;
break;
default:
luaL_argerror( L, i, "invalid format" );
break;
}
}
}
return top-1;
}
static int memfile_seek( lua_State* L ) {
static char const* const modenames[] = { "set", "cur", "end", NULL };
memfile* mf = tomemfile( L, 1 );
int op = luaL_checkoption( L, 2, "cur", modenames );
lua_Integer offset = luaL_optinteger( L, 3, 0 );
int ok = 1;
switch( op ) {
case 0: /* SEEK_SET */
if( offset < 0 || offset > mf->n )
ok = 0;
else
mf->p = offset;
break;
case 1: /* SEEK_CUR */
if( (offset < 0 && -(offset+1) >= mf->p ) ||
(offset >= 0 && offset > mf->n-mf->p) )
ok = 0;
else
mf->p += offset;
break;
default: /* SEEK_END */
if( offset > 0 ||
(offset < 0 && -(offset+1) >= mf->n) )
ok = 0;
else
mf->p = mf->n + offset;
break;
};
if( ok ) {
lua_pushinteger( L, (lua_Integer)mf->p );
return 1;
} else {
lua_pushnil( L );
lua_pushliteral( L, "invalid offset" );
return 2;
}
}
static int memfile_setvbuf( lua_State* L ) {
static char const* const modenames[] = { "no", "full", "line", NULL };
(void)tomemfile( L, 1 );
(void)luaL_checkoption(L, 2, NULL, modenames );
(void)luaL_optinteger( L, 3, 0 );
/* there are no caches for memory buffers */
lua_pushboolean( L, 1 );
return 1;
}
static int memfile_write( lua_State* L ) {
memfile* mf = luaL_checkudata( L, 1, NAME );
int ok = 1;
int arg = 2;
int top = lua_gettop( L );
if( (mf->flags & MEMFILE_W) == 0 ) {
lua_pushnil( L );
lua_pushliteral( L, "permission denied" );
return 2;
}
for( arg = 2; arg <= top && ok; ++arg ) {
size_t len = 0;
char const* s = NULL;
if( NULL == (s = lua_tolstring( L, arg, &len ) ) )
s = luaL_checklstring( L, arg, &len );
if( mf->p+len > mf->n ) {
len = mf->n - mf->p;
ok = 0;
}
memcpy( mf->addr + mf->p, s, len );
mf->p += len;
}
lua_settop( L, 1 );
if( ok )
return 1;
else {
lua_pushnil( L );
lua_pushliteral( L, "not enough space" );
return 2;
}
}
static int memfile_addr( lua_State* L ) {
memfile* mf = tomemfile( L, 1 );
lua_pushlightuserdata( L, mf->addr );
return 1;
}
static int memfile_size( lua_State* L ) {
memfile* mf = tomemfile( L, 1 );
lua_pushinteger( L, mf->n );
return 1;
}
static int memfile_truncate( lua_State* L ) {
memfile* mf = tomemfile( L, 1 );
size_t sz = luaL_checkinteger( L, 2 );
if( sz > mf->n ) {
lua_pushnil( L );
lua_pushliteral( L, "new size too large" );
return 2;
}
mf->n = sz;
if( mf->p > sz )
mf->p = sz;
lua_pushboolean( L, 1 );
return 1;
}
static int memfile_close( lua_State* L ) {
memfile* mf = tomemfile( L, 1 );
int top;
lua_getuservalue( L, 1 );
top = lua_gettop( L );
lua_rawgeti( L, -1, CLOSE_INDEX );
lua_rawgeti( L, -2, OBJECT_INDEX );
if( lua_type( L, -2 ) == LUA_TFUNCTION )
lua_call( L, 1, LUA_MULTRET );
else {
lua_pop( L, 2 );
lua_pushboolean( L, 1 );
}
mf->addr = NULL;
luaL_checkstack( L, 1, "not enough stack space" );
#if LUA_VERSION_NUM > 501
lua_pushnil( L );
#else
lua_pushvalue( L, LUA_GLOBALSINDEX );
#endif
lua_setuservalue( L, 1 );
return lua_gettop( L )-top;
}
static int memfile_gc( lua_State* L ) {
memfile* mf = lua_touserdata( L, 1 );
if( mf->addr != NULL ) {
lua_getuservalue( L, 1 );
lua_rawgeti( L, -1, CLOSE_INDEX );
lua_rawgeti( L, -2, OBJECT_INDEX );
if( lua_type( L, -2 ) == LUA_TFUNCTION )
lua_call( L, 1, 0 );
}
return 0;
}
IPC_LOCAL void* memfile_udata( lua_State* L, int idx,
char const* name ) {
int i = lua_absindex( L, idx );
(void)tomemfile( L, i );
lua_getuservalue( L, i );
lua_rawgeti( L, -1, OBJECT_INDEX );
lua_replace( L, i ); /* replace memfile object */
lua_pop( L, 1 ); /* remove uservalue table */
return luaL_checkudata( L, i, name );
}
IPC_LOCAL void memfile_new( lua_State* L, void* addr, size_t n,
int perms, int oidx, int closeidx,
int flushidx ) {
memfile* mf = NULL;
oidx = lua_absindex( L, oidx );
closeidx = lua_absindex( L, closeidx );
flushidx = lua_absindex( L, flushidx );
mf = lua_newuserdata( L, sizeof( *mf ) );
mf->addr = addr;
mf->n = n;
mf->p = 0;
mf->flags = perms;
lua_newtable( L );
if( oidx ) {
lua_pushvalue( L, oidx );
lua_rawseti( L, -2, OBJECT_INDEX );
}
if( closeidx ) {
lua_pushvalue( L, closeidx );
lua_rawseti( L, -2, CLOSE_INDEX );
}
if( flushidx ) {
lua_pushvalue(L, flushidx );
lua_rawseti( L, -2, FLUSH_INDEX );
}
lua_setuservalue( L, -2 );
if( luaL_newmetatable( L, NAME ) ) {
luaL_Reg const methods[] = {
{ "flush", memfile_flush },
{ "lines", memfile_lines },
{ "read", memfile_read },
{ "seek", memfile_seek },
{ "setvbuf", memfile_setvbuf },
{ "write", memfile_write },
{ "addr", memfile_addr },
{ "size", memfile_size },
{ "truncate", memfile_truncate },
{ "close", memfile_close },
{ NULL, NULL }
};
luaL_newlib( L, methods );
lua_setfield( L, -2, "__index" );
lua_pushcfunction( L, memfile_gc );
lua_setfield( L, -2, "__gc" );
lua_pushboolean( L, 0 );
lua_setfield( L, -2, "__metatable" );
}
lua_setmetatable( L, -2 );
}