[gambit-list] gsc-script

Christian christian at pflanze.mine.nu
Thu Aug 11 12:25:26 EDT 2005


Hello

Since I couldn't see any way for gsc running scripts as a command 
line argument (it can however -e '(include "thefile.scm")') I wrote 
the below C program to act as an interpreter. Why not use gsi-script? 
Because I need the compiler at runtime. This can also be extended to 
load any arbitrary code before the actual script (see comment below).

To be used like:
#!/usr/local/bin/gsc-script
...

To be installed like:
  gcc -O -o gsc-script gsc-script.c
  install -s gsc-script /usr/local/bin/gsc-script

Cheers
Christian.

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define CMDPATH "/usr/local/Gambit-C/bin/gsc"
#define MAXCMDLEN 100
#define MAXSTRLEN 2000

int main(int argc, char*argv[],char*envp[]) {
     char*newcmd[MAXCMDLEN+1];
     int newcmd_i=0;
     newcmd[newcmd_i++]= CMDPATH;
     newcmd[newcmd_i++]="-:tE";
     newcmd[newcmd_i++]="-i";
     newcmd[newcmd_i++]="-e";

     if (argc < 2){
         fprintf(stderr,"not enough arguments\n");
         exit(1);
     }
     {
#define STR "(include \""
// for loading some other code into every program, just add it here:
// #define STR "(load \"~~/my-load\")(include \""
         char includestr[MAXSTRLEN+1];
         size_t si=strlen(STR);

         strcpy(includestr,STR);

         { // escaping copy:
#undef STR
#define STR argv[1]
             int i;
             int len=strlen(STR);
             for(i=0;i<len;i++){
                 int c= STR[i];
                 if ((c=='\\')||(c=='"')) {
                     includestr[si++]='\\';
                     if (si>=MAXSTRLEN) {
                         fprintf(stderr,"pathstring too long\n");
                         exit(1);
                     }
                 }
                 includestr[si++]=c;
                 if (si>=MAXSTRLEN) {
                     fprintf(stderr,"pathstring too long\n");
                     exit(1);
                 }
             }
             {
#undef STR
#define STR "\")"
                 int len=strlen(STR);
                 if((si+len) >= MAXSTRLEN) {
                     fprintf(stderr,"pathstring too long\n");
                     exit(1);
                 }
                 strcpy(&(includestr[si]),STR);
                 si+= len;
                 includestr[si++]=0;

                 newcmd[newcmd_i++]=includestr;

                 /* finish parameters and exec: */
                 newcmd[newcmd_i++]= 0;
                 execve(CMDPATH,newcmd,envp);
                 fprintf(stderr,"could not execute '%s': 
%s\n",CMDPATH,strerror(errno));
                 exit(127);
             }
         }
     }
}



More information about the Gambit-list mailing list