Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Embedded interpreter woes?

10 views
Skip to first unread message

Antti Järvinen

unread,
Nov 15, 2003, 2:23:37 PM11/15/03
to
Dear Sirs,

here is a c-code:
-------------------------------
#include <stdio.h>
#include <wchar.h>
#include <tcl.h>
#include <stdlib.h>

Tcl_Interp *tcl_interp ;
char *the_program = "puts stdout $FooBar\nset a_len [ string length $FooBar ]\nputs stdout $a_len" ;

int main(int argc, char **argv)
{
tcl_interp = Tcl_CreateInterp() ;
if(tcl_interp == NULL) {
fprintf(stderr,"problem") ;
return(-1) ;
}
if(Tcl_Init(tcl_interp) == TCL_ERROR) {
fprintf(stderr,"Tcl_init returns TCL_ERROR?\n") ;
return(-2) ;
}
Tcl_FindExecutable(argv[0]) ;

if(Tcl_SetVar(tcl_interp, "FooBar","Value of FooBar",0)==NULL) {
printf("Tcl_SetVar returns null\n") ;
return(-4) ;
}
printf("The_Program->%s<-\n", the_program) ;
printf("\neval=%d\n",Tcl_Eval(tcl_interp, the_program)) ;
return(0) ;
}

-------------------------------

when run on linux box like this:

$ cc unicodeexample2.c -o unicodeexample2 -ltcl -Wall
$ ./unicodeexample2
The_Program->puts stdout $FooBar
set a_len [ string length $FooBar ]
puts stdout $a_len<-

It prints out:
Value of FooBar
Segmentation fault

gdb points here:

Program received signal SIGSEGV, Segmentation fault.
0x4007f7cc in Tcl_ParseCommand () from /usr/lib/libtcl.so.0
(gdb) where
#0 0x4007f7cc in Tcl_ParseCommand () from /usr/lib/libtcl.so.0
#1 0x400807cb in Tcl_EvalEx () from /usr/lib/libtcl.so.0
#2 0x4008051a in Tcl_EvalTokens () from /usr/lib/libtcl.so.0
#3 0x40080848 in Tcl_EvalEx () from /usr/lib/libtcl.so.0
#4 0x40080b89 in Tcl_Eval () from /usr/lib/libtcl.so.0
#5 0x080487c6 in main ()

now:
what the heck I'm doing wrong?

This very same program works wonders when I start tcl interpreter and
copy-paste this code there. If I remove this statement in [] marks and
replace it with some static value, it works. Seems like spawning of
another interpreter breaks something ; what have I broken and where?

This is tried in redhat linuxen 7.2 and 9, having tcl versions
8.3.3 and 8.3.5.

--
Antti Järvinen, cost...@iki.fi
"concerto for two faggots and orchestra"

miguel sofer

unread,
Nov 15, 2003, 3:06:03 PM11/15/03
to
Antti Järvinen wrote:
> <snip>

> what the heck I'm doing wrong?

Not sure if this is the source of your problem, but ... the call to
Tcl_FindExecutable() should occur BEFORE the first Tcl_CreateInterp().

Tcl_FindExecutable() does more initialization than its name implies :(

Miguel

Benjamin Riefenstahl

unread,
Nov 15, 2003, 3:06:02 PM11/15/03
to
Hi Antti,

cost...@iki.fi (Antti Järvinen) writes:
> char *the_program = "puts stdout $FooBar\nset a_len [ string length
> $FooBar ]\nputs stdout $a_len" ;

> [...]


> printf("\neval=%d\n",Tcl_Eval(tcl_interp, the_program)) ;


With older Tcl versions, the actual programs that are passed to
Tcl_Eval() must be in modifiable memory (the code temporarily sets
characters to 0). Try this variable declaration instead:

static char the_program[] =
"puts stdout $FooBar\n"
"set a_len [ string length $FooBar ]\n"
"puts stdout $a_len\n"
;

benny

Antti Järvinen

unread,
Nov 16, 2003, 5:37:24 AM11/16/03
to
Benjamin Riefenstahl <Benjamin.R...@epost.de> writes:
> With older Tcl versions, the actual programs that are passed to
> Tcl_Eval() must be in modifiable memory (the code temporarily sets
> characters to 0). Try this variable declaration instead:
>
> static char the_program[] =
> "puts stdout $FooBar\n"
> "set a_len [ string length $FooBar ]\n"
> "puts stdout $a_len\n"
> ;

Yes,

first I was wondering that I had some end-of-line issues (\r versus \n)
so I did put the tcl-program into a file that I then did read at runtime and
whoop: the program did what expected. And I was confused as content of the
file then really was exactly the same as I had in my memory buffer, including
end-of-line marks.

0 new messages