Re: [Chicken-users] Dynamic loading foreign C library problem

2005-11-22 Thread Matthew David Parker

On Tue, 22 Nov 2005, felix winkelmann wrote:
 Hi, Matthew!

 How do you allocate the C string, and how is it passed
 to Scheme?


Ah hah!  That was it, no fault with CHICKEN, really, I allocated the
string incorrectly.  I was doing:

AIargs = malloc(sizeof(argstring));

Which was only 4 bytes, because it was doing the size of the pointer,
argstring.  So I had to do sizeof(strlen(argstring) + 1) instead.

Now I fixed that and some other malloc'd things and the game is loading up
very well!  Thank you for your direction.  I like CHICKEN very much so
far.

Matt

PS. On a side question, is there some thing you can do in the csi chicken
interpreter to make it so you can press the up arrow and cycle through
the history of your commands, like in a bash shell?




 cheers,
 felix


 On 11/22/05, Matthew David Parker [EMAIL PROTECTED] wrote:
 
 
  Hi, I have a game that is compiled as a shared library.  It uses X11 and
  runs in a thread from SDL.
 
  So normally I would link to this library and run game_load(args) where
  args are command line options, like -join 127.0.0.1.  calling
  game_load will start the game in the thread and return control to the
  program that called game_load.  I'm making an AI interface to the game, so
  after it loads you can call functions like move(32, 4) to get the bot in
  the game to move, or something similar.
 
  I've gotten this to work dynamically linked from a C program, and I got it
  to work in Chez scheme.  I'm now trying to use CHICKEN to do it, since
  chicken is open source and chez isn't.
 
  So, in CHICKEN I have a file xpai.scm that is supposed to interface to my
  game.  Here's a simplified version of what's in it:
 
  (foreign-declare 
  #include \game_ai.h\)
 
  (define game.load
  (foreign-lambda void game_load c-string))
 
 
  I compile it like this:
   chicken xpai.scm -dynamic
 
   gcc -o xpai.so xpai.c `chicken-config -shared -libs -cflags` -lgame_ai
  -shared -fPIC `sdl-config --libs --cflags`
 
  And it compiles fine, into xpai.so
 
  Then I run csi and (load xpai.so) which it does fine.
 
  Then I run (game_load -join termite.cs.indiana.edu)
 
  the game_load function in C mallocs the arg string permanately and AIargs
  points to it.
 
  I can printf AIargs at this point and it's fine.
 
  Then, I run SDL_Init, and if I printf AIargs it's just -join termit
  instead of the full string it once was, as if SDL_Init cut into the memory
  somehow.
 
  I read SDL_Init sometimes causes problems on Mac OS X (even though I'm
  using linux 2.6.8), so I moved SDL_Init to a spot in the code before I
  malloc'd AIargs.
 
  THe program got a little further this time; it connected to the server,
  but then there as a segmentation fault, I think while it was trying to load
  the X window for the game graphics. I could have traced it down to the
  exact line, but I figure there's just some option in CHICKEN that I need
  to set so that it can properly handle this.
 
  Maybe the problem is that, while chicken properly loads my libgame_ai.so C
  library, it doesn't properly handle the memory of the dynamic libraries
  that libgame_ai.so is linked to?
 
  Matt
 
 
  ___
  Chicken-users mailing list
  Chicken-users@nongnu.org
  http://lists.nongnu.org/mailman/listinfo/chicken-users
 




___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Dynamic loading foreign C library problem

2005-11-22 Thread Zbigniew
Try the readline egg at
http://www.call-with-current-continuation.org/eggs/readline.html.

On 11/22/05, Matthew David Parker [EMAIL PROTECTED] wrote:

 PS. On a side question, is there some thing you can do in the csi chicken
 interpreter to make it so you can press the up arrow and cycle through
 the history of your commands, like in a bash shell?


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Dynamic loading foreign C library problem

2005-11-22 Thread Raffael Cavallaro
On Nov 22, 2005, at 12:04 PM, Matthew David Parker wrote:PS. On a side question, is there some thing you can do in the csi chicken interpreter to make it so you can press the "up" arrow and cycle through the history of your commands, like in a bash shell? If you use emacs with one of the scheme packages such as quack (built onĀ  cmuscheme.el and scheme.el) you get command history in csi with meta-p and meta-n.Quack is at http://www.neilvandyke.org/quack/You can do your shell stuff (such as chicken setup) from eshell and never leave emacs.regards Raffael Cavallaro, Ph.D. [EMAIL PROTECTED]  ___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Dynamic loading foreign C library problem

2005-11-22 Thread Thomas Chust
Am 22.11.2005, 17:04 Uhr, schrieb Matthew David Parker  
[EMAIL PROTECTED]:



[...] So I had to do sizeof(strlen(argstring) + 1) instead.


Hello,

what is that sizeof operator doing there? It should just be something like
  malloc(strlen(argstring) + 1)
otherwise the value of the expression will also be 4. (But probably this
was just a typo in your e-mail, so never mind)


[...]
PS. On a side question, is there some thing you can do in the csi chicken
interpreter to make it so you can press the up arrow and cycle through
the history of your commands, like in a bash shell?
[...]


Emacs and the readline egg have already been suggested. Yet another  
flexible

alternative is the rlwrap program:
  http://utopia.knoware.nl/~hlub/uck/rlwrap/

cu,
Thomas


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Dynamic loading foreign C library problem

2005-11-22 Thread felix winkelmann
On 11/22/05, Zbigniew [EMAIL PROTECTED] wrote:
 Try the readline egg at
 http://www.call-with-current-continuation.org/eggs/readline.html.

 On 11/22/05, Matthew David Parker [EMAIL PROTECTED] wrote:

  PS. On a side question, is there some thing you can do in the csi chicken
  interpreter to make it so you can press the up arrow and cycle through
  the history of your commands, like in a bash shell?


Readline would be the thing. A quick and dirty tool for these things
is rlwrap.


cheers,
felix


___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Dynamic loading foreign C library problem

2005-11-22 Thread felix winkelmann
Hm... Is there some more information available?
When exactly does the segfault occur?


cheers,
felix

On 11/22/05, Matthew David Parker [EMAIL PROTECTED] wrote:

 Alright, I got the readline egg and it works quite well.  Though, when I
 run the function to load up my game window it throws a segmentation fault.
 It only does it when I'm using readline, and not otherwise.

 On Tue, 22 Nov 2005, felix winkelmann wrote:

  On 11/22/05, Zbigniew [EMAIL PROTECTED] wrote:
   Try the readline egg at
   http://www.call-with-current-continuation.org/eggs/readline.html.
  
   On 11/22/05, Matthew David Parker [EMAIL PROTECTED] wrote:
  
PS. On a side question, is there some thing you can do in the csi 
chicken
interpreter to make it so you can press the up arrow and cycle through
the history of your commands, like in a bash shell?
  
 
  Readline would be the thing. A quick and dirty tool for these things
  is rlwrap.
 
 
  cheers,
  felix
 
 


 ___
 Chicken-users mailing list
 Chicken-users@nongnu.org
 http://lists.nongnu.org/mailman/listinfo/chicken-users



___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Dynamic loading foreign C library problem

2005-11-21 Thread felix winkelmann
Hi, Matthew!

How do you allocate the C string, and how is it passed
to Scheme?


cheers,
felix


On 11/22/05, Matthew David Parker [EMAIL PROTECTED] wrote:


 Hi, I have a game that is compiled as a shared library.  It uses X11 and
 runs in a thread from SDL.

 So normally I would link to this library and run game_load(args) where
 args are command line options, like -join 127.0.0.1.  calling
 game_load will start the game in the thread and return control to the
 program that called game_load.  I'm making an AI interface to the game, so
 after it loads you can call functions like move(32, 4) to get the bot in
 the game to move, or something similar.

 I've gotten this to work dynamically linked from a C program, and I got it
 to work in Chez scheme.  I'm now trying to use CHICKEN to do it, since
 chicken is open source and chez isn't.

 So, in CHICKEN I have a file xpai.scm that is supposed to interface to my
 game.  Here's a simplified version of what's in it:

 (foreign-declare 
 #include \game_ai.h\)

 (define game.load
 (foreign-lambda void game_load c-string))


 I compile it like this:
  chicken xpai.scm -dynamic

  gcc -o xpai.so xpai.c `chicken-config -shared -libs -cflags` -lgame_ai
 -shared -fPIC `sdl-config --libs --cflags`

 And it compiles fine, into xpai.so

 Then I run csi and (load xpai.so) which it does fine.

 Then I run (game_load -join termite.cs.indiana.edu)

 the game_load function in C mallocs the arg string permanately and AIargs
 points to it.

 I can printf AIargs at this point and it's fine.

 Then, I run SDL_Init, and if I printf AIargs it's just -join termit
 instead of the full string it once was, as if SDL_Init cut into the memory
 somehow.

 I read SDL_Init sometimes causes problems on Mac OS X (even though I'm
 using linux 2.6.8), so I moved SDL_Init to a spot in the code before I
 malloc'd AIargs.

 THe program got a little further this time; it connected to the server,
 but then there as a segmentation fault, I think while it was trying to load
 the X window for the game graphics. I could have traced it down to the
 exact line, but I figure there's just some option in CHICKEN that I need
 to set so that it can properly handle this.

 Maybe the problem is that, while chicken properly loads my libgame_ai.so C
 library, it doesn't properly handle the memory of the dynamic libraries
 that libgame_ai.so is linked to?

 Matt


 ___
 Chicken-users mailing list
 Chicken-users@nongnu.org
 http://lists.nongnu.org/mailman/listinfo/chicken-users



___
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users