Annotation of gforth/engine/main.c, revision 1.8

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

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