[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: [CODE] Requesting help with lua and C
- From: "Leonardo Maciel" <leonardo@...>
- Date: Sun, 17 Jun 2007 02:46:37 -0300
> however this is the place I would call "lua_to_user" with the
You mean "c_to_user", right?
If I understand your problem correctly, one of many ways to do it. Not the
most elegant, but the easiest to explain that I can think of is:
/*
* Redirect the output to a temporary file.
* Run this (not tested) code snippet once during initialization.
*/
lua_getglobal(L, "io") /* get the io table */
lua_getfield(L, -1, "output") /* pushes the io.output function onto the
stack */
lua_getfield(L, -2, "tmpfile") /* pushes the io.tmpfile function onto the
stack */
lua_remove(L, -3) /* remove the io table from the stack */
lua_call(L, 0, 1) /* call io.tmpfile, returns a file descriptor
*/
lua_call(L, 1, 0) /* call the io.output function with fd as
argument */
/*
* Now you may call luaL_dostring anywhere in your program.
* The stdout is now written to a temporary file.
*/
luaL_dostring(L, txt);
/*
* Finally write the contents of the temporary file to your user_data
structure
*/
lua_getglobal(L, "io") /* get the io table */
lua_getfield(L, -1, "read") /* pushes the io.read function onto the stack
*/
lua_remove(L, -2) /* remove the io table from the stack */
lua_pushstring("*a") /* reads the whole file */
lua_call(L, 1, 1) /* call io.tmpfile, leaves file contents on
stack */
txt = lua_tostring(L, -1); /* get file contents from the top of the
stack */
c_to_user(usr, txt);
lua_remove(L, -1); /* remove the string only when finished using
it */
Hope this helps. And don't forget to rewrite your c_to_user function to
check for the string length in order to avoid an overflow.
--
Leonardo Maciel
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Shane Lee
Sent: domingo, 17 de junho de 2007 00:12
To: Lua
Subject: [CODE] Requesting help with lua and C
Generally, I would hesitate to unlurk on a mailing list with what seems to
be a newbie problem. However, I am relatively new to Lua and even even
though I have the manual and have read quite a bit of the "Programming in
Lua" online help I am still unable to find the aswer to my problem. Perhaps
there isn't a real answer. What I am hoping to avoid is a generic answer or
RTFM, which I was able to find all over the internet via google searches and
wiki studies.
Here is what I have thus far, and the comments should explain my problem:
-------------Begin Psuedo-Code--------------------
Lua:
--Simple string stored in a table
t = { [[And this is from the Lua script!]] }
print(t[1])
--End of script
C code:
struct user_data {
int uuid; /* User's unique ID num */
char inbuf[8192]; /* Users's input buffer control */
char outbuf[8192]; /* Socket's output buffer control */
};
/*
* c_to_user:
* Psuedo function that takes a variable (txt) and stores it in the
* "user's" outbuf for later processing.
*/
void c_to_user(struct user_data *usr, char *txt) {
/* Do some basic error checking... */
if (!usr || !txt || txt[0] == '\0') { return; }
/* Use 'sprintf' to store the input to the user's outbuf */
sprintf(usr->outbuf, "%s", txt);
return;
}
/*
* lua_to_user:
* Run a lua script and send the output to c_to_user.
*/
void lua_to_user(struct user_data *usr, char *txt, lua_State *L) {
const char *s = NULL;
char buf[8192];
memset(buf, '\0', 8192);
c_to_user(usr, "This is stored in C.");
/* "txt" would be a !validated! path to the Lua script */
luaL_dostring(L, txt);
/* At this point my knowledge breaks down and I run out of ideas,
* however this is the place I would call "lua_to_user" with the
* script's output.
*/
return;
}
------------- End Psuedo-Code--------------------
If anyone out there is able to figure out my problem, I would greatly
appreciate their response.
Thanks in advance,
-Shane