File:  [gforth] / gforth / engine / main.c
Revision 1.23: download - view: text, annotated - select for diffs
Sun Feb 28 08:37:45 1999 UTC (25 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
moved signal handling into a new file signals.c
minor changes to window size handling

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

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