calling script with dobuffer doesnt work

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

calling script with dobuffer doesnt work

Matthias Gall
hello everyone,

its me again with another simple problem. i managed to get lua run in my
vc++ implementation (thanks to terence martin, it was the extern "C" ...
stuff). now, i am trying to call a script that i read into a char* buffer.
the script looks like that
a = 1
b = a + 1
write (b)
and is read correctly into the buffer. also, the script works well with
lua.exe or luac.exe. but calling lua_dobuffer won't work. it always returns
LUA_ERRSYNTAX, but i can't find any syntax error in my small script?! the
parameters for lua_dobuffer also should be correct (my state, the buffer and
its length, and NULL as the last parameter, hope that's ok).

thanks in advance,
matt


Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

Ashley Fryer-2
> From: Matthias Gall

> the script looks like that
> a = 1
> b = a + 1
> write (b)
> and is read correctly into the buffer. also, the script works well with
> lua.exe or luac.exe. but calling lua_dobuffer won't work. it
> always returns
> LUA_ERRSYNTAX,


The problem is the last line.  The "write" function isn't built into the
core lua language, it's actually part of the add-on I/O library.  You must
initialize the standard libraries in your host "C" program before they can
be used (lua.exe and luac.exe do this for you).

See Section 6 of the manual for more info, but here's the basic idea:

extern "C"
{
#include <lualib.h>
}

....

lua_baselibopen(L);
lua_strlibopen(L);
lua_mathlibopen(L);
lua_iolibopen(L);




Reply | Threaded
Open this post in threaded view
|

Re: calling script with dobuffer doesnt work

Matthias Gall
> The problem is the last line.  The "write" function isn't built into the
> core lua language, it's actually part of the add-on I/O library.  You must
> initialize the standard libraries in your host "C" program before they can
> be used (lua.exe and luac.exe do this for you).

i already did that. the interesting thing is, that only lua_dobuffer does
not work, lua_dofile does!

thank you anyway,
matt



Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

Samson-3
In reply to this post by Matthias Gall
Have you embedded newlines into your buffer?


-----Original Message-----
From: Matthias Gall [[hidden email]]
Sent: Sunday, February 25, 2001 8:39 PM
To: Multiple recipients of list
Subject: Re: calling script with dobuffer doesnt work


> The problem is the last line.  The "write" function isn't built into the
> core lua language, it's actually part of the add-on I/O library.  You must
> initialize the standard libraries in your host "C" program before they can
> be used (lua.exe and luac.exe do this for you).

i already did that. the interesting thing is, that only lua_dobuffer does
not work, lua_dofile does!

thank you anyway,
matt


Reply | Threaded
Open this post in threaded view
|

Re: calling script with dobuffer doesnt work

Matthias Gall
> Have you embedded newlines into your buffer?

What exactly do you mean? I read the script from a file into the buffer and
don't change anything, so CR and LF are still there (I dumped the buffer and
it looked okay).

Matt


Reply | Threaded
Open this post in threaded view
|

Re: calling script with dobuffer doesnt work

Rolf Sormo
Hi!

I don't really know much about lua yet, but I just came to think one thing.
As you read the stuff from a file
(maybe to a string) and are giving the size of the buffer to dobuffer, are
you sure you're not accidently giving the size
of the string (characters + '\0') or only the data in it (characters without
the tailing '\0')?

Rolf

----- Original Message -----
From: "Matthias Gall" <[hidden email]>
To: "Multiple recipients of list" <[hidden email]>
Sent: Monday, February 26, 2001 11:43 AM
Subject: Re: calling script with dobuffer doesnt work


> > Have you embedded newlines into your buffer?
>
> What exactly do you mean? I read the script from a file into the buffer
and
> don't change anything, so CR and LF are still there (I dumped the buffer
and
> it looked okay).
>
> Matt
>
>


Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

John Batty-2
In reply to this post by Matthias Gall
Matthias

I saw a problem like this, which turned out to be because I had an EOF
character at the end of my file.
[I could load the file using lua_dofile, but not lua_dobuffer.]

I fixed it by modifying my code to add a check for a terminating EOF,
and if there is one I simply pass a smaller len value to lua_dobuffer
to truncate the EOF.

John

-----Original Message-----
From: Matthias Gall [[hidden email]]
Sent: 25 February 2001 20:39
To: Multiple recipients of list
Subject: Re: calling script with dobuffer doesnt work


> The problem is the last line.  The "write" function isn't built into the
> core lua language, it's actually part of the add-on I/O library.  You must
> initialize the standard libraries in your host "C" program before they can
> be used (lua.exe and luac.exe do this for you).

i already did that. the interesting thing is, that only lua_dobuffer does
not work, lua_dofile does!

thank you anyway,
matt




Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

Samson-3
try passing something like this to lua_dostring()

char *lua_code = "a = 1\nb = a + 1\nwrite(b)\n";



-----Original Message-----
From: Matthias Gall [[hidden email]]
Sent: Monday, February 26, 2001 10:44 AM
To: Multiple recipients of list
Subject: Re: calling script with dobuffer doesnt work


> Have you embedded newlines into your buffer?

What exactly do you mean? I read the script from a file into the buffer and
don't change anything, so CR and LF are still there (I dumped the buffer and
it looked okay).

Matt

Reply | Threaded
Open this post in threaded view
|

Re: calling script with dobuffer doesnt work

Matthias Gall
I was checking my file for this EOF marker, but there was none. Next, I
removed the CR (0x0D), and this worked fine. I never thought this could be a
problem, so it took some time until I finally tried what first seemed
improbable to me. Is this a bug, or a feature?! A real "new-line" needs both
LF AND CR. However, do you know a nice editor for Windows that uses LF only
as a new-line-marker?

Matt


Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

Philippe Lhoste-2
> I was checking my file for this EOF marker, but there was none. Next, I
> removed the CR (0x0D), and this worked fine. I never thought this
> could be a
> problem, so it took some time until I finally tried what first seemed
> improbable to me. Is this a bug, or a feature?!

I would put that in the bug list... A really portable application should
handle EOL as CR, LF or CR+LF.

> A real "new-line"
> needs both
> LF AND CR.

"Real" in Windows world... CR in Mac world, LF in Unix world.

> However, do you know a nice editor for Windows that
> uses LF only
> as a new-line-marker?
>
> Matt

An opportunity for a plug I shouldn't let slip...
SciTE (www.Scintilla.org) manages the three formats.
By default, it saves in the same format as read. You can also choose a
line-ending format and convert the file before saving it. A bit too much,
but you can also type different line-ends in the same file. Useful to test
the robustness of some applications, though...
And it has Lua syntax highlighting!

Now, to be honest, there are a number of good freeware text editors that
correctly manage these issues.

Regards.

--._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`--


Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

Luiz Henrique de Figueiredo
In reply to this post by Matthias Gall
>> I was checking my file for this EOF marker, but there was none. Next, I
>> removed the CR (0x0D), and this worked fine. I never thought this
>> could be a
>> problem, so it took some time until I finally tried what first seemed
>> improbable to me. Is this a bug, or a feature?!
>
>I would put that in the bug list... A really portable application should
>handle EOL as CR, LF or CR+LF.

This is not a bug. See below.
To me, it's still not clear what happened, but here are some facts:

- The lexer simply ignores CR. It uses LF for signalling change of line (and
  thus for keeping track of line numbers) but otherwise, LF is just whitespace.
  So, the problem is not the presence or absence of CR.

- If the string contains a DOS EOF marker (control-Z), then you'll get an
  syntax error. This is correct: control-Z has no meaning for Lua because it
  has no meaning for ANSI C.

- It is possible that the same file works ok when run with lua_dofile and not
  work ok when run with lua_dobuffer after it's read into a buffer: stdio takes
  care of CR+LF and control-Z before Lua sees it. So, if you open the file 
  in BINARY mode, read it into a buffer and then call lua_dobuffer, anything
  can happen... (probably a control-Z will be read and sent to Lua).
  So, make sure you open the file in TEXT mode.

Someone suggested that the fault is probably in how lua_dobuffer was called:
the length of the buffer was probably wrong: if buffer is declared as
	char buffer[MAXB]
the correct code is NOT lua_dobuffer(L,buffer,MAXB,"a name"); it's likely that
what you want is lua_dobuffer(L,buffer,strlen(buffer),"a name"), in which case
it's easier to use lua_dostring instead.

I think this was the problem, but we'd have to see the code to be sure.
--lhf

Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

Philippe Lhoste-2
> >> I was checking my file for this EOF marker, but there was none. Next, I
> >> removed the CR (0x0D), and this worked fine. I never thought this
> >> could be a
> >> problem, so it took some time until I finally tried what first seemed
> >> improbable to me. Is this a bug, or a feature?!
> >
> >I would put that in the bug list... A really portable application should
> >handle EOL as CR, LF or CR+LF.
>
> This is not a bug. See below.
> To me, it's still not clear what happened, but here are some facts:
>
> - The lexer simply ignores CR. It uses LF for signalling change
> of line (and
>   thus for keeping track of line numbers) but otherwise, LF is
> just whitespace.
>   So, the problem is not the presence or absence of CR.

OK, but I wonder how Lua behaves on Macintoshes and others systems using CR
as end of line.
I suppose a Mac port may need some adjustment here, unless the C runtime is
taking care of the needed conversions when opening a file in text mode.
I am not a regular Mac user (at least on the programming side), so it is a
rather theoritical thought.
Just wondering: as MacOS X is Unix based (I think), does it choose to adhere
to the LF EOL?

-- PL


Reply | Threaded
Open this post in threaded view
|

RE: calling script with dobuffer doesnt work

Luiz Henrique de Figueiredo
In reply to this post by Luiz Henrique de Figueiredo
>OK, but I wonder how Lua behaves on Macintoshes and others systems using CR
>as end of line.

ANSI C requires that stdio takes care of that. So, yes, it's the C runtime.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: RE: calling script with dobuffer doesnt work

Terence Martin-2
In reply to this post by Luiz Henrique de Figueiredo
We used it before they abandoned x86, and it used unix conventions then. Not sure about now though. I would assume they didn't change it.

Philippe Lhoste wrote on 3/1/01 2:38 am:

Just wondering: as MacOS X is Unix based (I think), does it choose to adhere to the LF EOL?

-- PL