Annotation of gforth/main.c, revision 1.55

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

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