13 #include "lixp_debug.h"
14 #include "lixp_util.h"
15 #include "lixp_instance.h"
18 /* ------------------------------------------------------------------------
22 struct ixp
*lixp_checkixp (lua_State
*L
, int narg
)
24 void *ud
= luaL_checkudata (L
, narg
, L_IXP_MT
);
25 luaL_argcheck (L
, ud
!= NULL
, 1, "`ixp' expected");
26 return (struct ixp
*)ud
;
29 int l_ixp_tostring (lua_State
*L
)
31 struct ixp
*ixp
= lixp_checkixp (L
, 1);
32 lua_pushfstring (L
, "ixp instance %p", ixp
);
36 /* ------------------------------------------------------------------------
37 * lua: write(file, data) -- writes data to a file
40 int l_ixp_write (lua_State
*L
)
49 ixp
= lixp_checkixp (L
, 1);
50 file
= luaL_checkstring (L
, 2);
51 data
= luaL_checklstring (L
, 3, &data_len
);
53 fid
= ixp_open(ixp
->client
, file
, P9_OWRITE
);
55 return lixp_pusherror (L
, "count not open p9 file");
57 DBGF("** ixp.write (%s,%s) **\n", file
, data
);
59 rc
= lixp_write_data (fid
, data
, data_len
);
62 return lixp_pusherror (L
, "failed to write to p9 file");
69 /* ------------------------------------------------------------------------
70 * lua: data = read(file) -- returns all contents (upto 4k)
72 int l_ixp_read (lua_State
*L
)
78 size_t buf_ofs
, buf_size
, buf_left
;
80 ixp
= lixp_checkixp (L
, 1);
81 file
= luaL_checkstring (L
, 2);
83 fid
= ixp_open(ixp
->client
, file
, P9_OREAD
);
85 return lixp_pusherror (L
, "count not open p9 file");
87 buf
= malloc (fid
->iounit
);
90 return lixp_pusherror (L
, "count not allocate memory");
93 buf_size
= buf_left
= fid
->iounit
;
95 DBGF("** ixp.read (%s) **\n", file
);
98 int rc
= ixp_read (fid
, buf
+buf_ofs
, buf_left
);
103 return lixp_pusherror (L
, "failed to read from p9 file");
109 if (buf_ofs
>= buf_size
)
110 return lixp_pusherror (L
, "internal error while reading");
112 if (buf_size
>= 4096)
115 buf
= realloc (buf
, 4096);
118 return lixp_pusherror (L
, "count not allocate memory");
125 lua_pushstring (L
, buf
);
129 /* ------------------------------------------------------------------------
130 * lua: create(file, [data]) -- create a file, optionally write data to it
132 int l_ixp_create (lua_State
*L
)
140 ixp
= lixp_checkixp (L
, 1);
141 file
= luaL_checkstring (L
, 2);
142 data
= luaL_optlstring (L
, 3, NULL
, &data_len
);
144 DBGF("** ixp.create (%s) **\n", file
);
146 fid
= ixp_create (ixp
->client
, file
, 0777, P9_OWRITE
);
148 return lixp_pusherror (L
, "count not create file");
151 && !(fid
->qid
.type
& P9_DMDIR
)) {
152 int rc
= lixp_write_data (fid
, data
, data_len
);
155 return lixp_pusherror (L
, "failed to write to p9 file");
163 /* ------------------------------------------------------------------------
164 * lua: remove(file) -- remove a file
166 int l_ixp_remove (lua_State
*L
)
172 ixp
= lixp_checkixp (L
, 1);
173 file
= luaL_checkstring (L
, 2);
175 DBGF("** ixp.remove (%s) **\n", file
);
177 rc
= ixp_remove (ixp
->client
, file
);
179 return lixp_pusherror (L
, "failed to remove p9 file");
184 /* ------------------------------------------------------------------------
185 * lua: itr = iread(file) -- returns a line iterator
188 struct l_ixp_iread_s
{
196 static int iread_iter (lua_State
*L
);
198 int l_ixp_iread (lua_State
*L
)
202 struct l_ixp_iread_s
*ctx
;
204 ixp
= lixp_checkixp (L
, 1);
205 file
= luaL_checkstring (L
, 2);
207 ctx
= (struct l_ixp_iread_s
*)lua_newuserdata (L
, sizeof(*ctx
));
209 return lixp_pusherror (L
, "count not allocate context");
210 memset (ctx
, 0, sizeof (*ctx
));
212 // set the metatable for the new userdata
213 luaL_getmetatable (L
, L_IXP_IREAD_MT
);
214 lua_setmetatable (L
, -2);
216 ctx
->fid
= ixp_open(ixp
->client
, file
, P9_OREAD
);
217 if(ctx
->fid
== NULL
) {
218 return lixp_pusherror (L
, "count not open p9 file");
221 DBGF("** ixp.iread (%s) **\n", file
);
223 // create and return the iterator function
224 // the only argument is the userdata
225 lua_pushcclosure (L
, iread_iter
, 1);
229 static int iread_iter (lua_State
*L
)
231 struct l_ixp_iread_s
*ctx
;
234 ctx
= (struct l_ixp_iread_s
*)lua_touserdata (L
, lua_upvalueindex(1));
236 DBGF("** ixp.iread - iter **\n");
239 ctx
->buf
= malloc (ctx
->fid
->iounit
);
241 return lixp_pusherror (L
, "count not allocate memory");
242 ctx
->buf_size
= ctx
->fid
->iounit
;
249 rc
= ixp_read (ctx
->fid
, ctx
->buf
, ctx
->buf_size
);
251 return 0; // we are done
256 s
= ctx
->buf
+ ctx
->buf_pos
;
257 e
= s
+ ctx
->buf_len
;
259 cr
= strchr (s
, '\n');
261 // no match, just return the whole thing
262 // TODO: should read more upto a cr or some limit
263 lua_pushstring (L
, s
);
268 // we have a match s..cr is our sub string
269 int len
= (cr
-s
) + 1;
271 lua_pushstring (L
, s
);
278 static int iread_gc (lua_State
*L
)
280 struct l_ixp_iread_s
*ctx
;
282 ctx
= (struct l_ixp_iread_s
*)lua_touserdata (L
, 1);
284 DBGF("** ixp.iread - gc **\n");
286 ixp_close (ctx
->fid
);
294 void lixp_init_iread_mt (lua_State
*L
)
296 luaL_newmetatable(L
, L_IXP_IREAD_MT
);
298 // setup the __gc field
299 lua_pushstring (L
, "__gc");
300 lua_pushcfunction (L
, iread_gc
);
301 lua_settable (L
, -3);
304 /* ------------------------------------------------------------------------
305 * lua: stat = stat(file) -- returns a status table
308 int l_ixp_stat (lua_State
*L
)
311 struct IxpStat
*stat
;
315 ixp
= lixp_checkixp (L
, 1);
316 file
= luaL_checkstring (L
, 2);
318 DBGF("** ixp.stat (%s) **\n", file
);
320 stat
= ixp_stat(ixp
->client
, file
);
322 return lixp_pusherror(L
, "cannot stat file");
324 rc
= lixp_pushstat (L
, stat
);
331 /* ------------------------------------------------------------------------
332 * lua: itr = idir(dir) -- returns a file name iterator
335 struct l_ixp_idir_s
{
341 static int idir_iter (lua_State
*L
);
343 int l_ixp_idir (lua_State
*L
)
347 struct l_ixp_idir_s
*ctx
;
349 ixp
= lixp_checkixp (L
, 1);
350 file
= luaL_checkstring (L
, 2);
352 ctx
= (struct l_ixp_idir_s
*)lua_newuserdata (L
, sizeof(*ctx
));
354 return lixp_pusherror (L
, "count not allocate context");
355 memset(ctx
, 0, sizeof (*ctx
));
357 // set the metatable for the new userdata
358 luaL_getmetatable (L
, L_IXP_IDIR_MT
);
359 lua_setmetatable (L
, -2);
361 ctx
->fid
= ixp_open(ixp
->client
, file
, P9_OREAD
);
362 if(ctx
->fid
== NULL
) {
363 return lixp_pusherror (L
, "count not open p9 file");
366 ctx
->buf
= malloc (ctx
->fid
->iounit
);
368 ixp_close (ctx
->fid
);
370 return lixp_pusherror (L
, "count not allocate memory");
373 DBGF("** ixp.idir (%s) **\n", file
);
375 // create and return the iterator function
376 // the only argument is the userdata
377 lua_pushcclosure (L
, idir_iter
, 1);
381 static int idir_iter (lua_State
*L
)
383 struct l_ixp_idir_s
*ctx
;
386 ctx
= (struct l_ixp_idir_s
*)lua_touserdata (L
, lua_upvalueindex(1));
388 DBGF("** ixp.idir - iter **\n");
390 if (ctx
->m
.pos
>= ctx
->m
.end
) {
391 int rc
= ixp_read (ctx
->fid
, ctx
->buf
, ctx
->fid
->iounit
);
396 ctx
->m
= ixp_message(ctx
->buf
, rc
, MsgUnpack
);
397 if (ctx
->m
.pos
>= ctx
->m
.end
)
401 ixp_pstat(&ctx
->m
, &stat
);
403 return lixp_pushstat (L
, &stat
);
406 static int idir_gc (lua_State
*L
)
408 struct l_ixp_idir_s
*ctx
;
410 ctx
= (struct l_ixp_idir_s
*)lua_touserdata (L
, 1);
412 DBGF("** ixp.idir - gc **\n");
416 ixp_close (ctx
->fid
);
421 void lixp_init_idir_mt (lua_State
*L
)
423 luaL_newmetatable(L
, L_IXP_IDIR_MT
);
425 // setup the __gc field
426 lua_pushstring (L
, "__gc");
427 lua_pushcfunction (L
, idir_gc
);
428 lua_settable (L
, -3);