Annotation of gforth/main.c, revision 1.58

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

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