Clean Guile code from C program

2008-03-07 Thread Jose Martin

Hi all, I need to use a program in C that is designed to be used in Guile. I'm 
not a Guile user myself so I don't know well how to remove the Guile code from 
the C program, so that I can run it without Guile (Please, don't be ofended for 
that ;) ).



I looked at the program's code and it looks a bit difficult to change it, and I 
must say I'm a beginner in C. Could anyone give me the steps of the changes I'd 
need to remove Guile-dependent code from C code?



The program has about 10 files, so here I'm pasting only one of the files. The 
code is open-source so I could share it if anyone wants to see the other files.



I'd massively appreciate any help. Thanks,



Jose



-- ATTACHED CODE --



#include 

...



#define INFO contain

#define TEXTMGRP(x) (SCM_NIMP(x) && SCM_CAR(x) == (SCM)scm_tc16_textmgr)

#define TEXTMGR(x) ((TextFile*) SCM_CDR(x))



long scm_tc16_textmgr;



SCM mark_textmgr(SCM obj)

{

  SCM_SETGC8MARK(obj);

  return SCM_BOOL_F;

}



size_t free_textmgr(SCM obj)

{

  if (TEXTMGR(obj)) {

TEXTMGR(obj)->foreach(markref);

TEXTMGR(obj)->hashtable()->foreach(decref);

delete TEXTMGR(obj);

  }

  return 0;

}



int print_textmgr(SCM obj, SCM port, scm_print_state * pstate)

{

  scm_puts("#filename(), port);

  scm_puts(">", port);

  return 1;

}



SCM textmgr2scm(TextFile* text)

{

  SCM result;

  SCM_NEWCELL(result);

  SCM_SETCAR(result, (SCM) scm_tc16_textmgr);

  SCM_SETCDR(result, (SCM) text);

  return result;

}





SCM_PROC(s_open_text, "open-text", 1, 0, 0, open_text);

SCM open_text(SCM name)

{

  SCM txt;

  if (scm_string_p(name) != SCM_BOOL_T)

scm_wrong_type_arg(s_open_text, 1, name);

  txt = textmgr2scm(new TextFile(SCM_CHARS(name)));

  TEXTMGR(txt)->foreach(markref);

  TEXTMGR(txt)->hashtable()->foreach(incref);

  return txt;

}



SCM_PROC(s_append_text, "append-text", 2, 0, 0, append_text);

SCM append_text(SCM obj, SCM name)

{

  if (!TEXTMGRP(obj))

scm_wrong_type_arg(s_append_text, 1, obj);

  if (!TEXTMGR(obj))

scm_misc_error(s_append_text, "Text object is empty", SCM_EOL);

  if (scm_string_p(name) != SCM_BOOL_T)

scm_wrong_type_arg(s_append_text, 2, name);

  TEXTMGR(obj)->foreach(markref);

  TEXTMGR(obj)->hashtable()->foreach(decref);

  TEXTMGR(obj)->append(SCM_CHARS(name));

  TEXTMGR(obj)->foreach(markref);

  TEXTMGR(obj)->hashtable()->foreach(incref);

  return obj;

}



SCM_PROC(s_close_text, "close-text", 1, 0, 0, close_text);

SCM close_text(SCM obj)

{

  if (!TEXTMGRP(obj))

scm_wrong_type_arg(s_close_text, 1, obj);

  if (TEXTMGR(obj)) {

scm_gc();

TEXTMGR(obj)->foreach(markref);

TEXTMGR(obj)->hashtable()->foreach(decref);

TEXTMGR(obj)->hashtable()->removeif(delrefp);

delete TEXTMGR(obj);

SCM_SETCDR(obj, 0);

  }

  return SCM_UNSPECIFIED;

}



SCM_PROC(s_rewind_text, "rewind-text", 1, 0, 0, rewind_text);

SCM rewind_text(SCM obj)

{

  if (!TEXTMGRP(obj))

scm_wrong_type_arg(s_rewind_text, 1, obj);

  if (!TEXTMGR(obj))

scm_misc_error(s_rewind_text, "Text object is empty", SCM_EOL);

  TEXTMGR(obj)->rewind();

  return SCM_UNSPECIFIED;

}



SCM_PROC(s_get_next_word, "get-next-word", 1, 0, 0, get_next_word);

SCM get_next_word(SCM obj)

{

  contain* cn;

  if (!TEXTMGRP(obj))

scm_wrong_type_arg(s_get_next_word, 1, obj);

  if (!TEXTMGR(obj))

scm_misc_error(s_get_next_word, "Text object is empty", SCM_EOL);

  cn = TEXTMGR(obj)->getnext();

  if (cn)

return cn->content;

  else

return SCM_BOOL_F;

}



SCM_PROC(s_get_word, "get-word", 2, 0, 0, get_word);

SCM get_word(SCM obj, SCM n)

{

  contain* cn;

  if (!TEXTMGRP(obj))

scm_wrong_type_arg(s_get_word, 1, obj);

  if (!TEXTMGR(obj))

scm_misc_error(s_get_word, "Text object is empty", SCM_EOL);

  if (scm_integer_p(n) != SCM_BOOL_T)

scm_wrong_type_arg(s_get_word, 2, n);

  cn = TEXTMGR(obj)->get(gh_scm2int(n));

  if (cn)

return cn->content;

  else

return SCM_BOOL_F;

}



SCM_PROC(s_scramble_text, "scramble-text", 1, 0, 0, scramble_text);

SCM scramble_text(SCM obj)

{

  if (!TEXTMGRP(obj))

scm_wrong_type_arg(s_scramble_text, 1, obj);

  if (!TEXTMGR(obj))

scm_misc_error(s_scramble_text, "Text object is empty", SCM_EOL);

  return textmgr2scm(TEXTMGR(obj)->scramble());

}



static SCM currentproc = 0;



void iterproc(contain* cn)

{

  gh_call1(currentproc, cn->content);

}



SCM_PROC(s_for_each_word, "for-each-word", 2, 0, 0, for_each_word);

SCM for_each_word(SCM obj, SCM proc)

{

  SCM lastproc = currentproc;

  if (!TEXTMGRP(obj))

scm_wrong_type_arg(s_for_each_word, 1, obj);

  if (!TEXTMGR(obj))

scm_misc_error(s_for_each_word, "Text object is empty", SCM_EOL);

  currentproc = proc;

  TEXTMGR(obj)->foreach(iterproc);

  currentproc = lastproc;

  return SCM_UNSPECIFIED;

}



SCM_PROC(s_for_each_unique_word, "for-each-unique-word", 1, 0, 0, 
for_each_unique_word);

SCM for_each_unique_word(SCM proc)

{

  SCM lastproc = currentpro

Re: Clean Guile code from C program

2008-03-07 Thread Mike Gran
--- Jose Martin <[EMAIL PROTECTED]> wrote:

> I looked at the program's code and it looks a bit
> difficult to change it, and I must say I'm a
> beginner in C. Could anyone give me the steps of the
> changes I'd need to remove Guile-dependent code from
> C code?

Wow.  The Guile stuff is pretty well entangled.  If
you don't know enough C (and I think I saw some C++ in
there as well) you might be in over your head.

But, FWIW, here's how you'd do it.

1.  For every variable and function declared as type
SCM, figure out what type of information actually goes
in that variable.  Choose an appropriate C type
instead.

1a.  This code creates a special Guile type textmgr
which is, underneath, a C++ class textmgr.  Wherever
the textmgr stuff is used, call the C++ class's
procecures instead.

2.  For every function that begins with scm_, check
the docs for its purpose and recode them in C.  You
can usually ignore the scm__p functions because
they check the type of the variable, which won't be
necessary once you convert everything into appropriate
C types.

3.  The scm_shell creates a loop that displays a
prompt, takes user-typed commands, executes them, and
displays the output.  Invent some sort of input scheme
or input language and write a parser for it.  Have it
execute the commands that the user typed in.  (The
"readline" calls invoke a library called readline
which does command history stuff.  Your parser could
use the C API of readline instead.)

4.  Make sure that your C datatypes are freed
appropriately.

5. ???

6. Profit!

Good luck!

--
Mike Gran






Re: Clean Guile code from C program

2008-03-07 Thread Luigi Semenzato
It's not clear that Mike's asking the right question here.
If he wants to get rid of Guile from his program, first he
needs to check if the program contains any Guile code
which uses this code.  It's possible that a substantial
amount of the program's functionality has been
written in Guile.  In that case converting these
functions, which are Guile-specific, to C won't help much.

A better approach is to understand what the Guile
part of the program is doing and figuring out how
to convert that part to C.  Most likely, the best way
of doing that will not use any of the C-Guile interface.

It's possible (but quite unlikely) that all calls into this code
come from other C code.  Then the advice applies, although
even in this case it may be easier to rewrite the C code
from scratch.

Also, the code is a little suspicious.  Take a look at this
fragment below:

SCM check_text_sections(SCM tag)

{

 if (tag == SCM_BOOL_F) {

   TextFile::CheckNewline = 0;

 } else if (tag == SCM_BOOL_F) {

   TextFile::CheckNewline = 1;

...


Before asking Guile-specific questions, Mike should backtrack
and get advice on how to solve his problem (probably on a different
forum).  He may very well be going down the wrong path.

Luigi


On 3/7/08, Mike Gran <[EMAIL PROTECTED]> wrote:
> --- Jose Martin <[EMAIL PROTECTED]> wrote:
>
>  > I looked at the program's code and it looks a bit
>  > difficult to change it, and I must say I'm a
>  > beginner in C. Could anyone give me the steps of the
>  > changes I'd need to remove Guile-dependent code from
>  > C code?
>
>
> Wow.  The Guile stuff is pretty well entangled.  If
>  you don't know enough C (and I think I saw some C++ in
>  there as well) you might be in over your head.
>
>  But, FWIW, here's how you'd do it.




Re: Clean Guile code from C program

2008-03-07 Thread Neil Jerram
"Jose Martin" <[EMAIL PROTECTED]> writes:

> Hi all, I need to use a program in C that is designed to be used in
> Guile. I'm not a Guile user myself so I don't know well how to remove
> the Guile code from the C program, so that I can run it without Guile
> (Please, don't be ofended for that ;) ).

Hi Jose,

Others are already giving useful advice, so I won't duplicate that.
I'd be interested to hear a bit more, though, about what the program
is, how it came to use Guile until now, and why you are interested now
in taking Guile out.

(I'm not offended; but curious, and wondering if there are any other
options that we could help you with.)

Best wishes,
 Neil