File:  [gforth] / gforth / engine / main.c
Revision 1.4: download - view: text, annotated - select for diffs
Thu Jul 31 16:17:26 1997 UTC (26 years, 8 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Added documentation for structures and object.fs
Changed representation of structures from "size align" to "align size",
   and renamed 1 cells: to cell% etc.
added %size and %alignment
fixed search bug
added command-line option --die-on-signal

    1: /* command line interpretation, image loading etc. for Gforth
    2: 
    3: 
    4:   Copyright (C) 1995 Free Software Foundation, Inc.
    5: 
    6:   This file is part of Gforth.
    7: 
    8:   Gforth is free software; you can redistribute it and/or
    9:   modify it under the terms of the GNU General Public License
   10:   as published by the Free Software Foundation; either version 2
   11:   of the License, or (at your option) any later version.
   12: 
   13:   This program is distributed in the hope that it will be useful,
   14:   but WITHOUT ANY WARRANTY; without even the implied warranty of
   15:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16:   GNU General Public License for more details.
   17: 
   18:   You should have received a copy of the GNU General Public License
   19:   along with this program; if not, write to the Free Software
   20:   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   21: */
   22: 
   23: #include "config.h"
   24: #include <errno.h>
   25: #include <ctype.h>
   26: #include <stdio.h>
   27: #include <unistd.h>
   28: #include <string.h>
   29: #include <math.h>
   30: #include <sys/types.h>
   31: #include <sys/stat.h>
   32: #include <fcntl.h>
   33: #include <assert.h>
   34: #include <stdlib.h>
   35: #if HAVE_SYS_MMAN_H
   36: #include <sys/mman.h>
   37: #endif
   38: #include "forth.h"
   39: #include "io.h"
   40: #include "getopt.h"
   41: #include "version.h"
   42: 
   43: #define PRIM_VERSION 1
   44: /* increment this whenever the primitives change in an incompatible way */
   45: 
   46: #ifdef MSDOS
   47: jmp_buf throw_jmp_buf;
   48: #  ifndef DEFAULTPATH
   49: #    define DEFAULTPATH "."
   50: #  endif
   51: #endif
   52: 
   53: #if defined(DIRECT_THREADED) 
   54: #  define CA(n)	(symbols[(n)])
   55: #else
   56: #  define CA(n)	((Cell)(symbols+(n)))
   57: #endif
   58: 
   59: #define maxaligned(n)	(typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
   60: 
   61: static UCell dictsize=0;
   62: static UCell dsize=0;
   63: static UCell rsize=0;
   64: static UCell fsize=0;
   65: static UCell lsize=0;
   66: int offset_image=0;
   67: int die_on_signal=0;
   68: static int clear_dictionary=0;
   69: static int debug=0;
   70: static size_t pagesize=0;
   71: char *progname;
   72: 
   73: /* image file format:
   74:  *  "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.3.0 -i\n")
   75:  *   padding to a multiple of 8
   76:  *   magic: "Gforth1x" means format 0.2,
   77:  *              where x is even for big endian and odd for little endian
   78:  *              and x & ~1 is the size of the cell in bytes.
   79:  *  padding to max alignment (no padding necessary on current machines)
   80:  *  ImageHeader structure (see below)
   81:  *  data (size in ImageHeader.image_size)
   82:  *  tags ((if relocatable, 1 bit/data cell)
   83:  *
   84:  * tag==1 means that the corresponding word is an address;
   85:  * If the word is >=0, the address is within the image;
   86:  * addresses within the image are given relative to the start of the image.
   87:  * If the word =-1 (CF_NIL), the address is NIL,
   88:  * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
   89:  * If the word =CF(DODOES), it's a DOES> CFA
   90:  * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
   91:  *					possibly containing a jump to dodoes)
   92:  * If the word is <CF(DOESJUMP), it's a primitive
   93:  */
   94: 
   95: typedef struct {
   96:   Address base;		/* base address of image (0 if relocatable) */
   97:   UCell checksum;	/* checksum of ca's to protect against some
   98: 			   incompatible	binary/executable combinations
   99: 			   (0 if relocatable) */
  100:   UCell image_size;	/* all sizes in bytes */
  101:   UCell dict_size;
  102:   UCell data_stack_size;
  103:   UCell fp_stack_size;
  104:   UCell return_stack_size;
  105:   UCell locals_stack_size;
  106:   Xt *boot_entry;	/* initial ip for booting (in BOOT) */
  107:   Xt *throw_entry;	/* ip after signal (in THROW) */
  108:   Cell unused1;		/* possibly tib stack size */
  109:   Cell unused2;
  110:   Address data_stack_base; /* this and the following fields are initialized by the loader */
  111:   Address fp_stack_base;
  112:   Address return_stack_base;
  113:   Address locals_stack_base;
  114: } ImageHeader;
  115: /* the image-header is created in main.fs */
  116: 
  117: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
  118: {
  119:   int i=0, j, k, steps=(size/sizeof(Cell))/8;
  120:   char bits;
  121: /*   static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
  122: 
  123: /*  printf("relocating %x[%x]\n", image, size); */
  124:    
  125:   for(k=0; k<=steps; k++)
  126:     for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1) {
  127:       /*      fprintf(stderr,"relocate: image[%d]\n", i);*/
  128:       if(bits & 0x80) {
  129: 	/* fprintf(stderr,"relocate: image[%d]=%d\n", i, image[i]);*/
  130: 	if(image[i]<0)
  131: 	  switch(image[i])
  132: 	    {
  133: 	    case CF_NIL      : image[i]=0; break;
  134: #if !defined(DOUBLY_INDIRECT)
  135: 	    case CF(DOCOL)   :
  136: 	    case CF(DOVAR)   :
  137: 	    case CF(DOCON)   :
  138: 	    case CF(DOUSER)  : 
  139: 	    case CF(DODEFER) : 
  140: 	    case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
  141: 	    case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
  142: #endif /* !defined(DOUBLY_INDIRECT) */
  143: 	    case CF(DODOES)  :
  144: 	      MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
  145: 	      break;
  146: 	    default          :
  147: /*	      printf("Code field generation image[%x]:=CA(%x)\n",
  148: 		     i, CF(image[i])); */
  149: 	      image[i]=(Cell)CA(CF(image[i]));
  150: 	    }
  151: 	else
  152: 	  image[i]+=(Cell)image;
  153:       }
  154:     }
  155: }
  156: 
  157: UCell checksum(Label symbols[])
  158: {
  159:   UCell r=PRIM_VERSION;
  160:   Cell i;
  161: 
  162:   for (i=DOCOL; i<=DOESJUMP; i++) {
  163:     r ^= (UCell)(symbols[i]);
  164:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  165:   }
  166: #ifdef DIRECT_THREADED
  167:   /* we have to consider all the primitives */
  168:   for (; symbols[i]!=(Label)0; i++) {
  169:     r ^= (UCell)(symbols[i]);
  170:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  171:   }
  172: #else
  173:   /* in indirect threaded code all primitives are accessed through the
  174:      symbols table, so we just have to put the base address of symbols
  175:      in the checksum */
  176:   r ^= (UCell)symbols;
  177: #endif
  178:   return r;
  179: }
  180: 
  181: Address verbose_malloc(Cell size)
  182: {
  183:   Address r;
  184:   /* leave a little room (64B) for stack underflows */
  185:   if ((r = malloc(size+64))==NULL) {
  186:     perror(progname);
  187:     exit(1);
  188:   }
  189:   r = (Address)((((Cell)r)+(sizeof(Float)-1))&(-sizeof(Float)));
  190:   if (debug)
  191:     fprintf(stderr, "malloc succeeds, address=$%lx\n", (long)r);
  192:   return r;
  193: }
  194: 
  195: Address my_alloc(Cell size)
  196: {
  197:   static Address next_address=0;
  198:   Address r;
  199: 
  200: #if HAVE_MMAP
  201: #if defined(MAP_ANON)
  202:   if (debug)
  203:     fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_ANON, ...); ", (long)next_address, (long)size);
  204:   r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
  205: #else /* !defined(MAP_ANON) */
  206:   /* Ultrix (at least does not define MAP_FILE and MAP_PRIVATE (both are
  207:      apparently defaults*/
  208: #ifndef MAP_FILE
  209: # define MAP_FILE 0
  210: #endif
  211: #ifndef MAP_PRIVATE
  212: # define MAP_PRIVATE 0
  213: #endif
  214:   static int dev_zero=-1;
  215: 
  216:   if (dev_zero == -1)
  217:     dev_zero = open("/dev/zero", O_RDONLY);
  218:   if (dev_zero == -1) {
  219:     r = (Address)-1;
  220:     if (debug)
  221:       fprintf(stderr, "open(\"/dev/zero\"...) failed (%s), no mmap; ", 
  222: 	      strerror(errno));
  223:   } else {
  224:     if (debug)
  225:       fprintf(stderr,"try mmap($%lx, $%lx, ..., MAP_FILE, dev_zero, ...); ", (long)next_address, (long)size);
  226:     r=mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, dev_zero, 0);
  227:   }
  228: #endif /* !defined(MAP_ANON) */
  229: 
  230:   if (r != (Address)-1) {
  231:     if (debug)
  232:       fprintf(stderr, "success, address=$%lx\n", (long) r);
  233:     if (pagesize != 0)
  234:       next_address = (Address)(((((Cell)r)+size-1)&-pagesize)+2*pagesize); /* leave one page unmapped */
  235:     return r;
  236:   }
  237:   if (debug)
  238:     fprintf(stderr, "failed: %s\n", strerror(errno));
  239: #endif /* HAVE_MMAP */
  240:   /* use malloc as fallback */
  241:   return verbose_malloc(size);
  242: }
  243: 
  244: #if (defined(mips) && !defined(INDIRECT_THREADED))
  245: /* the 256MB jump restriction on the MIPS architecture makes the
  246:    combination of direct threading and mmap unsafe. */
  247: #define dict_alloc(size) verbose_malloc(size)
  248: #else
  249: #define dict_alloc(size) my_alloc(size)
  250: #endif
  251: 
  252: Address loader(FILE *imagefile, char* filename)
  253: /* returns the address of the image proper (after the preamble) */
  254: {
  255:   ImageHeader header;
  256:   Address image;
  257:   Address imp; /* image+preamble */
  258:   Char magic[9];
  259:   Cell preamblesize=0;
  260:   Label *symbols = engine(0,0,0,0,0);
  261:   Cell data_offset = offset_image ? 28*sizeof(Cell) : 0;
  262:   UCell check_sum;
  263:   static char* endianstring[]= { "big","little" };
  264: 
  265: #ifndef DOUBLY_INDIRECT
  266:   check_sum = checksum(symbols);
  267: #else /* defined(DOUBLY_INDIRECT) */
  268:   check_sum = (UCell)symbols;
  269: #endif /* defined(DOUBLY_INDIRECT) */
  270: 
  271:   do
  272:     {
  273:       if(fread(magic,sizeof(Char),8,imagefile) < 8) {
  274: 	fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.2) image.\n",
  275: 		progname, filename);
  276: 	exit(1);
  277:       }
  278:       preamblesize+=8;
  279:     }
  280:   while(memcmp(magic,"Gforth1",7));
  281:   if (debug) {
  282:     magic[8]='\0';
  283:     fprintf(stderr,"Magic found: %s\n", magic);
  284:   }
  285: 
  286:   if(magic[7] != sizeof(Cell) +
  287: #ifdef WORDS_BIGENDIAN
  288:        '0'
  289: #else
  290:        '1'
  291: #endif
  292:        )
  293:     { fprintf(stderr,"This image is %d bit %s-endian, whereas the machine is %d bit %s-endian.\n", 
  294: 	      ((magic[7]-'0')&~1)*8, endianstring[magic[7]&1],
  295: 	      sizeof(Cell)*8, endianstring[
  296: #ifdef WORDS_BIGENDIAN
  297: 		      0
  298: #else
  299: 		      1
  300: #endif
  301: 		      ]);
  302:       exit(-2);
  303:     };
  304: 
  305:   fread((void *)&header,sizeof(ImageHeader),1,imagefile);
  306:   if (dictsize==0)
  307:     dictsize = header.dict_size;
  308:   if (dsize==0)
  309:     dsize=header.data_stack_size;
  310:   if (rsize==0)
  311:     rsize=header.return_stack_size;
  312:   if (fsize==0)
  313:     fsize=header.fp_stack_size;
  314:   if (lsize==0)
  315:     lsize=header.locals_stack_size;
  316:   dictsize=maxaligned(dictsize);
  317:   dsize=maxaligned(dsize);
  318:   rsize=maxaligned(rsize);
  319:   lsize=maxaligned(lsize);
  320:   fsize=maxaligned(fsize);
  321:   
  322: #if HAVE_GETPAGESIZE
  323:   pagesize=getpagesize(); /* Linux/GNU libc offers this */
  324: #elif HAVE_SYSCONF && defined(_SC_PAGESIZE)
  325:   pagesize=sysconf(_SC_PAGESIZE); /* POSIX.4 */
  326: #elif PAGESIZE
  327:   pagesize=PAGESIZE; /* in limits.h according to Gallmeister's POSIX.4 book */
  328: #endif
  329:   if (debug)
  330:     fprintf(stderr,"pagesize=%d\n",pagesize);
  331: 
  332:   image = dict_alloc(preamblesize+dictsize+data_offset)+data_offset;
  333:   rewind(imagefile);  /* fseek(imagefile,0L,SEEK_SET); */
  334:   if (clear_dictionary)
  335:     memset(image,0,dictsize);
  336:   fread(image,1,preamblesize+header.image_size,imagefile);
  337:   imp=image+preamblesize;
  338:   if(header.base==0) {
  339:     Cell reloc_size=((header.image_size-1)/sizeof(Cell))/8+1;
  340:     char reloc_bits[reloc_size];
  341:     fread(reloc_bits,1,reloc_size,imagefile);
  342:     relocate((Cell *)imp,reloc_bits,header.image_size,symbols);
  343: #if 0
  344:     { /* let's see what the relocator did */
  345:       FILE *snapshot=fopen("snapshot.fi","wb");
  346:       fwrite(image,1,imagesize,snapshot);
  347:       fclose(snapshot);
  348:     }
  349: #endif
  350:   }
  351:   else if(header.base!=imp) {
  352:     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\n",
  353: 	    progname, (unsigned long)header.base, (unsigned long)imp);
  354:     exit(1);
  355:   }
  356:   if (header.checksum==0)
  357:     ((ImageHeader *)imp)->checksum=check_sum;
  358:   else if (header.checksum != check_sum) {
  359:     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\n",
  360: 	    progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
  361:     exit(1);
  362:   }
  363:   fclose(imagefile);
  364: 
  365:   ((ImageHeader *)imp)->dict_size=dictsize;
  366:   ((ImageHeader *)imp)->data_stack_size=dsize;
  367:   ((ImageHeader *)imp)->fp_stack_size=fsize;
  368:   ((ImageHeader *)imp)->return_stack_size=rsize;
  369:   ((ImageHeader *)imp)->locals_stack_size=lsize;
  370: 
  371:   ((ImageHeader *)imp)->data_stack_base=my_alloc(dsize);
  372:   ((ImageHeader *)imp)->fp_stack_base=my_alloc(fsize);
  373:   ((ImageHeader *)imp)->return_stack_base=my_alloc(rsize);
  374:   ((ImageHeader *)imp)->locals_stack_base=my_alloc(lsize);
  375: 
  376:   CACHE_FLUSH(imp, header.image_size);
  377: 
  378:   return imp;
  379: }
  380: 
  381: int go_forth(Address image, int stack, Cell *entries)
  382: {
  383:   Cell *sp=(Cell*)(((ImageHeader *)image)->data_stack_base + dsize);
  384:   Float *fp=(Float *)(((ImageHeader *)image)->fp_stack_base + fsize);
  385:   Cell *rp=(Cell *)(((ImageHeader *)image)->return_stack_base + rsize);
  386:   Address lp=((ImageHeader *)image)->locals_stack_base + lsize;
  387:   Xt *ip=(Xt *)(((ImageHeader *)image)->boot_entry);
  388:   int throw_code;
  389: 
  390:   /* ensure that the cached elements (if any) are accessible */
  391:   IF_TOS(sp--);
  392:   IF_FTOS(fp--);
  393:   
  394:   for(;stack>0;stack--)
  395:     *--sp=entries[stack-1];
  396: 
  397: #if !defined(MSDOS) && !defined(_WIN32) && !defined(__EMX__)
  398:   get_winsize();
  399: #endif
  400:    
  401:   install_signal_handlers(); /* right place? */
  402:   
  403:   if ((throw_code=setjmp(throw_jmp_buf))) {
  404:     static Cell signal_data_stack[8];
  405:     static Cell signal_return_stack[8];
  406:     static Float signal_fp_stack[1];
  407:     
  408:     signal_data_stack[7]=throw_code;
  409:     
  410:     return((int)engine(((ImageHeader *)image)->throw_entry,signal_data_stack+7,
  411: 		       signal_return_stack+8,signal_fp_stack,0));
  412:   }
  413: 
  414:   return((int)engine(ip,sp,rp,fp,lp));
  415: }
  416: 
  417: UCell convsize(char *s, UCell elemsize)
  418: /* converts s of the format [0-9]+[bekMGT]? (e.g. 25k) into the number
  419:    of bytes.  the letter at the end indicates the unit, where e stands
  420:    for the element size. default is e */
  421: {
  422:   char *endp;
  423:   UCell n,m;
  424: 
  425:   m = elemsize;
  426:   n = strtoul(s,&endp,0);
  427:   if (endp!=NULL) {
  428:     if (strcmp(endp,"b")==0)
  429:       m=1;
  430:     else if (strcmp(endp,"k")==0)
  431:       m=1024;
  432:     else if (strcmp(endp,"M")==0)
  433:       m=1024*1024;
  434:     else if (strcmp(endp,"G")==0)
  435:       m=1024*1024*1024;
  436: #if (SIZEOF_CHAR_P > 4)
  437:     else if (strcmp(endp,"T")==0)
  438:       m=1024*1024*1024*1024;
  439: #endif
  440:     else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
  441:       fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
  442:       exit(1);
  443:     }
  444:   }
  445:   return n*m;
  446: }
  447: 
  448: int onlypath(char *file)
  449: { int i;
  450:   i=strlen(file);
  451:   while (i) { 	if (file[i]=='\\' || file[i]=='/') break;
  452:   		i--; }
  453:   return (i);
  454: }
  455: 
  456: FILE *openimage(char *fullfilename)
  457: { FILE *image_file;
  458:   image_file=fopen(fullfilename,"rb");
  459:   if (image_file!=NULL && debug)
  460:      fprintf(stderr, "Opened image file: %s\n", fullfilename);
  461:   return (image_file);
  462: }
  463: 
  464: FILE *checkimage(char *path, int len, char *imagename)
  465: { int dirlen=len;
  466:   char fullfilename[dirlen+strlen(imagename)+2];
  467:   memcpy(fullfilename, path, dirlen);
  468:   if (fullfilename[dirlen-1]!='/')
  469:     fullfilename[dirlen++]='/';
  470:   strcpy(fullfilename+dirlen,imagename);
  471:   return (openimage(fullfilename));
  472: }
  473: 
  474: int main(int argc, char **argv, char **env)
  475: {
  476:   char *path, *path1;
  477:   char *imagename="gforth.fi";
  478:   FILE *image_file;
  479:   int c, retvalue;
  480: 	  
  481: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
  482:   /* turn on alignment checks on the 486.
  483:    * on the 386 this should have no effect. */
  484:   __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
  485:   /* this is unusable with Linux' libc.4.6.27, because this library is
  486:      not alignment-clean; we would have to replace some library
  487:      functions (e.g., memcpy) to make it work */
  488: #endif
  489: 
  490:   progname = argv[0];
  491:   if ((path1=getenv("GFORTHPATH"))==NULL)
  492:     path1 = DEFAULTPATH;
  493:   
  494:   opterr=0;
  495:   while (1) {
  496:     int option_index=0;
  497:     static struct option opts[] = {
  498:       {"image-file", required_argument, NULL, 'i'},
  499:       {"dictionary-size", required_argument, NULL, 'm'},
  500:       {"data-stack-size", required_argument, NULL, 'd'},
  501:       {"return-stack-size", required_argument, NULL, 'r'},
  502:       {"fp-stack-size", required_argument, NULL, 'f'},
  503:       {"locals-stack-size", required_argument, NULL, 'l'},
  504:       {"path", required_argument, NULL, 'p'},
  505:       {"version", no_argument, NULL, 'v'},
  506:       {"help", no_argument, NULL, 'h'},
  507:       /* put something != 0 into offset_image */
  508:       {"offset-image", no_argument, &offset_image, 1},
  509:       {"no-offset-im", no_argument, &offset_image, 0},
  510:       {"clear-dictionary", no_argument, &clear_dictionary, 1},
  511:       {"die-on-signal", no_argument, &die_on_signal, 1},
  512:       {"debug", no_argument, &debug, 1},
  513:       {0,0,0,0}
  514:       /* no-init-file, no-rc? */
  515:     };
  516:     
  517:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vh", opts, &option_index);
  518:     
  519:     if (c==EOF)
  520:       break;
  521:     if (c=='?') {
  522:       optind--;
  523:       break;
  524:     }
  525:     switch (c) {
  526:     case 'i': imagename = optarg; break;
  527:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
  528:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
  529:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
  530:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
  531:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
  532:     case 'p': path1 = optarg; break;
  533:     case 'v': fprintf(stderr, "gforth %s\n", gforth_version); exit(0);
  534:     case 'h': 
  535:       fprintf(stderr, "Usage: %s [engine options] [image arguments]\n\
  536: Engine Options:\n\
  537:  -c, --clear-dictionary		    Initialize the dictionary with 0 bytes\n\
  538:  -d SIZE, --data-stack-size=SIZE    Specify data stack size\n\
  539:  --debug			    Print debugging information during startup\n\
  540:  --die-on-signal		    exit instead of CATCHing some signals\n\
  541:  -f SIZE, --fp-stack-size=SIZE	    Specify floating point stack size\n\
  542:  -h, --help			    Print this message and exit\n\
  543:  -i FILE, --image-file=FILE	    Use image FILE instead of `gforth.fi'\n\
  544:  -l SIZE, --locals-stack-size=SIZE  Specify locals stack size\n\
  545:  -m SIZE, --dictionary-size=SIZE    Specify Forth dictionary size\n\
  546:  --no-offset-im			    Load image at normal position\n\
  547:  --offset-image			    Load image at a different position\n\
  548:  -p PATH, --path=PATH		    Search path for finding image and sources\n\
  549:  -r SIZE, --return-stack-size=SIZE  Specify return stack size\n\
  550:  -v, --version			    Print version and exit\n\
  551: SIZE arguments consist of an integer followed by a unit. The unit can be\n\
  552:   `b' (bytes), `e' (elements), `k' (kilobytes), or `M' (Megabytes).\n\
  553: \n\
  554: Arguments of default image `gforth.fi':\n\
  555:  FILE				    load FILE (with `require')\n\
  556:  -e STRING, --evaluate STRING       interpret STRING (with `EVALUATE')\n",
  557: 	      argv[0]); exit(0);
  558:     }
  559:   }
  560:   path=path1;
  561:   image_file=NULL;
  562:   
  563:   if(strchr(imagename, '/')==NULL)
  564:   {
  565:     /* first check the directory where the exe file is in !! 01may97jaw */
  566:     if (onlypath(progname))
  567:       image_file=checkimage(progname, onlypath(progname), imagename);
  568:     if (!image_file)
  569:       do {
  570: 	char *pend=strchr(path, PATHSEP);
  571: 	if (pend==NULL)
  572: 	  pend=path+strlen(path);
  573: 	if (strlen(path)==0) break;
  574: 	image_file=checkimage(path, pend-path, imagename);
  575: 	path=pend+(*pend==PATHSEP);
  576:       } while (image_file==NULL);
  577:   }
  578:   else
  579:   {  image_file=openimage(imagename);
  580:   }
  581: 
  582:   if (!image_file)
  583:   { fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
  584: 		  progname, imagename, path1);
  585:     exit(1);
  586:   }
  587: 
  588:   {
  589:     char path2[strlen(path1)+1];
  590:     char *p1, *p2;
  591:     Cell environ[]= {
  592:       (Cell)argc-(optind-1),
  593:       (Cell)(argv+(optind-1)),
  594:       (Cell)strlen(path1),
  595:       (Cell)path2};
  596:     argv[optind-1] = progname;
  597:     /*
  598:        for (i=0; i<environ[0]; i++)
  599:        printf("%s\n", ((char **)(environ[1]))[i]);
  600:        */
  601:     /* make path OS-independent by replacing path separators with NUL */
  602:     for (p1=path1, p2=path2; *p1!='\0'; p1++, p2++)
  603:       if (*p1==PATHSEP)
  604: 	*p2 = '\0';
  605:       else
  606: 	*p2 = *p1;
  607:     *p2='\0';
  608:     retvalue=go_forth(loader(image_file, imagename),4,environ);
  609:     deprep_terminal();
  610:     exit(retvalue);
  611:   }
  612: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>