File:  [gforth] / gforth / Attic / main.c
Revision 1.46: download - view: text, annotated - select for diffs
Thu Nov 7 22:31:33 1996 UTC (24 years, 10 months ago) by pazsan
Branches: MAIN
CVS tags: v0-2-0, HEAD
Fixed some things with DOS

    1: /* command line interpretation, image loading etc. for Gforth
    2: 
    3: 
    4:   Copyright (C) 1995 Free Software Foundation, Inc.
    5: 
    6:   This file is part of Gforth.
    7: 
    8:   Gforth is free software; you can redistribute it and/or
    9:   modify it under the terms of the GNU General Public License
   10:   as published by the Free Software Foundation; either version 2
   11:   of the License, or (at your option) any later version.
   12: 
   13:   This program is distributed in the hope that it will be useful,
   14:   but WITHOUT ANY WARRANTY; without even the implied warranty of
   15:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16:   GNU General Public License for more details.
   17: 
   18:   You should have received a copy of the GNU General Public License
   19:   along with this program; if not, write to the Free Software
   20:   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   21: */
   22: 
   23: #include "config.h"
   24: #include <errno.h>
   25: #include <ctype.h>
   26: #include <stdio.h>
   27: #include <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>
   34: #include "forth.h"
   35: #include "io.h"
   36: #include "getopt.h"
   37: #include "version.h"
   38: 
   39: #ifdef MSDOS
   40: jmp_buf throw_jmp_buf;
   41: #endif
   42: 
   43: #ifndef DEFAULTPATH
   44: #  define DEFAULTPATH "/usr/local/lib/gforth:."
   45: #endif
   46: 
   47: #ifdef DIRECT_THREADED
   48: #  define CA(n)	(symbols[(n)])
   49: #else
   50: #  define CA(n)	((Cell)(symbols+(n)))
   51: #endif
   52: 
   53: #define maxaligned(n)	(typeof(n))((((Cell)n)+sizeof(Float)-1)&-sizeof(Float))
   54: 
   55: static Cell dictsize=0;
   56: static Cell dsize=0;
   57: static Cell rsize=0;
   58: static Cell fsize=0;
   59: static Cell lsize=0;
   60: char *progname;
   61: 
   62: /* image file format:
   63:  *  "#! binary-path -i\n" (e.g., "#! /usr/local/bin/gforth-0.2.0 -i\n")
   64:  *   padding to a multiple of 8
   65:  *   magic: "Gforth1x" means format 0.2,
   66:  *              where x is even for big endian and odd for little endian
   67:  *              and x & ~1 is the size of the cell in bytes.
   68:  *  padding to max alignment (no padding necessary on current machines)
   69:  *  ImageHeader structure (see below)
   70:  *  data (size in ImageHeader.image_size)
   71:  *  tags ((if relocatable, 1 bit/data cell)
   72:  *
   73:  * tag==1 means that the corresponding word is an address;
   74:  * If the word is >=0, the address is within the image;
   75:  * addresses within the image are given relative to the start of the image.
   76:  * If the word =-1 (CF_NIL), the address is NIL,
   77:  * If the word is <CF_NIL and >CF(DODOES), it's a CFA (:, Create, ...)
   78:  * If the word =CF(DODOES), it's a DOES> CFA
   79:  * If the word =CF(DOESJUMP), it's a DOES JUMP (2 Cells after DOES>,
   80:  *					possibly containing a jump to dodoes)
   81:  * If the word is <CF(DOESJUMP), it's a primitive
   82:  */
   83: 
   84: typedef struct {
   85:   Address base;		/* base address of image (0 if relocatable) */
   86:   UCell checksum;	/* checksum of ca's to protect against some
   87: 			   incompatible	binary/executable combinations
   88: 			   (0 if relocatable) */
   89:   UCell image_size;	/* all sizes in bytes */
   90:   UCell dict_size;
   91:   UCell data_stack_size;
   92:   UCell fp_stack_size;
   93:   UCell return_stack_size;
   94:   UCell locals_stack_size;
   95:   Xt *boot_entry;	/* initial ip for booting (in BOOT) */
   96:   Xt *throw_entry;	/* ip after signal (in THROW) */
   97: } ImageHeader;
   98: /* the image-header is created in main.fs */
   99: 
  100: void relocate(Cell *image, char *bitstring, int size, Label symbols[])
  101: {
  102:   int i=0, j, k, steps=(size/sizeof(Cell))/8;
  103:   char bits;
  104: /*   static char bits[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};*/
  105: 
  106: /*  printf("relocating %x[%x]\n", image, size); */
  107:    
  108:   for(k=0; k<=steps; k++)
  109:     for(j=0, bits=bitstring[k]; j<8; j++, i++, bits<<=1)
  110:       if(bits & 0x80)
  111: 	if(image[i]<0)
  112: 	  switch(image[i])
  113: 	    {
  114: 	    case CF_NIL      : image[i]=0; break;
  115: 	    case CF(DOCOL)   :
  116: 	    case CF(DOVAR)   :
  117: 	    case CF(DOCON)   :
  118: 	    case CF(DOUSER)  : 
  119: 	    case CF(DODEFER) : 
  120: 	    case CF(DOFIELD) : MAKE_CF(image+i,symbols[CF(image[i])]); break;
  121: 	    case CF(DODOES)  : MAKE_DOES_CF(image+i,image[i+1]+((Cell)image));
  122: 	      break;
  123: 	    case CF(DOESJUMP): MAKE_DOES_HANDLER(image+i); break;
  124: 	    default          :
  125: /*	      printf("Code field generation image[%x]:=CA(%x)\n",
  126: 		     i, CF(image[i]));
  127: */	      image[i]=(Cell)CA(CF(image[i]));
  128: 	    }
  129: 	else
  130: 	  image[i]+=(Cell)image;
  131: }
  132: 
  133: UCell checksum(Label symbols[])
  134: {
  135:   UCell r=0;
  136:   Cell i;
  137: 
  138:   for (i=DOCOL; i<=DOESJUMP; i++) {
  139:     r ^= (UCell)(symbols[i]);
  140:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  141:   }
  142: #ifdef DIRECT_THREADED
  143:   /* we have to consider all the primitives */
  144:   for (; symbols[i]!=(Label)0; i++) {
  145:     r ^= (UCell)(symbols[i]);
  146:     r = (r << 5) | (r >> (8*sizeof(Cell)-5));
  147:   }
  148: #else
  149:   /* in indirect threaded code all primitives are accessed through the
  150:      symbols table, so we just have to put the base address of symbols
  151:      in the checksum */
  152:   r ^= (UCell)symbols;
  153: #endif
  154:   return r;
  155: }
  156: 
  157: Address loader(FILE *imagefile, char* filename)
  158: /* returns the address of the image proper (after the preamble) */
  159: {
  160:   ImageHeader header;
  161:   Address image;
  162:   Address imp; /* image+preamble */
  163:   Char magic[8];
  164:   Cell wholesize;
  165:   Cell imagesize; /* everything needed by the image */
  166:   Cell preamblesize=0;
  167:   Label *symbols=engine(0,0,0,0,0);
  168:   UCell check_sum=checksum(symbols);
  169: 
  170:   static char* endianstring[]= { "big","little" };
  171: 
  172:   do
  173:     {
  174:       if(fread(magic,sizeof(Char),8,imagefile) < 8) {
  175: 	fprintf(stderr,"%s: image %s doesn't seem to be a Gforth (>=0.2) image.\n",
  176: 		progname, filename);
  177: 	exit(1);
  178:       }
  179:       preamblesize+=8;
  180: #ifdef DEBUG
  181:       fprintf(stderr,"Magic found: %-8s\n",magic);
  182: #endif
  183:     }
  184:   while(memcmp(magic,"Gforth1",7));
  185:   
  186:   if(magic[7] != sizeof(Cell) +
  187: #ifdef WORDS_BIGENDIAN
  188:        '0'
  189: #else
  190:        '1'
  191: #endif
  192:        )
  193:     { fprintf(stderr,"This image is %d bit %s-endian, whereas the machine is %d bit %s-endian.\n", 
  194: 	      ((magic[7]-'0')&~1)*8, endianstring[magic[7]&1],
  195: 	      sizeof(Cell)*8, endianstring[
  196: #ifdef WORDS_BIGENDIAN
  197: 		      0
  198: #else
  199: 		      1
  200: #endif
  201: 		      ]);
  202:       exit(-2);
  203:     };
  204: 
  205:   fread((void *)&header,sizeof(ImageHeader),1,imagefile);
  206:   if (dictsize==0)
  207:     dictsize = header.dict_size;
  208:   if (dsize==0)
  209:     dsize=header.data_stack_size;
  210:   if (rsize==0)
  211:     rsize=header.return_stack_size;
  212:   if (fsize==0)
  213:     fsize=header.fp_stack_size;
  214:   if (lsize==0)
  215:     lsize=header.locals_stack_size;
  216:   dictsize=maxaligned(dictsize);
  217:   dsize=maxaligned(dsize);
  218:   rsize=maxaligned(rsize);
  219:   lsize=maxaligned(lsize);
  220:   fsize=maxaligned(fsize);
  221:   
  222:   wholesize = preamblesize+dictsize+dsize+rsize+fsize+lsize;
  223:   imagesize = preamblesize+header.image_size+((header.image_size-1)/sizeof(Cell))/8+1;
  224:   image=malloc((wholesize>imagesize?wholesize:imagesize)
  225: #ifdef FUZZ
  226: 	       +FUZZ
  227: #endif
  228: 	       );
  229:   /*image = maxaligned(image);*/
  230:   /* memset(image,0,wholesize); */
  231: 
  232: #ifdef FUZZ
  233:   if(header.base==0) image += FUZZ/2;
  234:   else if((UCell)(header.base - (Cell)image + preamblesize) < FUZZ)
  235:     image = header.base - preamblesize;
  236: #endif  
  237:   rewind(imagefile);  /* fseek(imagefile,0L,SEEK_SET); */
  238:   fread(image,1,imagesize,imagefile);
  239:   fclose(imagefile);
  240:   imp=image+preamblesize;
  241:   
  242:   if(header.base==0) {
  243:     relocate((Cell *)imp,imp+header.image_size,header.image_size,symbols);
  244:     ((ImageHeader *)imp)->checksum=check_sum;
  245:   }
  246:   else if(header.base!=imp) {
  247:     fprintf(stderr,"%s: Cannot load nonrelocatable image (compiled for address $%lx) at address $%lx\nThe Gforth installer should look into the INSTALL file\n",
  248: 	    progname, (unsigned long)header.base, (unsigned long)imp);
  249:     exit(1);
  250:   } else if (header.checksum != check_sum) {
  251:     fprintf(stderr,"%s: Checksum of image ($%lx) does not match the executable ($%lx)\nThe Gforth installer should look into the INSTALL file\n",
  252: 	    progname, (unsigned long)(header.checksum),(unsigned long)check_sum);
  253:     exit(1);
  254:   }
  255: 
  256:   ((ImageHeader *)imp)->dict_size=dictsize;
  257:   ((ImageHeader *)imp)->data_stack_size=dsize;
  258:   ((ImageHeader *)imp)->return_stack_size=rsize;
  259:   ((ImageHeader *)imp)->fp_stack_size=fsize;
  260:   ((ImageHeader *)imp)->locals_stack_size=lsize;
  261: 
  262:   CACHE_FLUSH(imp, header.image_size);
  263: 
  264:   return imp;
  265: }
  266: 
  267: int go_forth(Address image, int stack, Cell *entries)
  268: {
  269:   Cell *sp=(Cell*)(image+dictsize+dsize);
  270:   Address lp=(Address)((void *)sp+lsize);
  271:   Float *fp=(Float *)((void *)lp+fsize);
  272:   Cell *rp=(Cell*)((void *)fp+rsize);
  273:   Xt *ip=(Xt *)(((ImageHeader *)image)->boot_entry);
  274:   int throw_code;
  275:   
  276:   for(;stack>0;stack--)
  277:     *--sp=entries[stack-1];
  278: 
  279: #if !defined(MSDOS) && !defined(_WIN32) && !defined(__EMX__)
  280:   get_winsize();
  281: #endif
  282:    
  283:   install_signal_handlers(); /* right place? */
  284:   
  285:   if ((throw_code=setjmp(throw_jmp_buf))) {
  286:     static Cell signal_data_stack[8];
  287:     static Cell signal_return_stack[8];
  288:     static Float signal_fp_stack[1];
  289:     
  290:     signal_data_stack[7]=throw_code;
  291:     
  292:     return((int)engine(((ImageHeader *)image)->throw_entry,signal_data_stack+7,
  293: 		       signal_return_stack+8,signal_fp_stack,0));
  294:   }
  295: 
  296:   return((int)engine(ip,sp,rp,fp,lp));
  297: }
  298: 
  299: int convsize(char *s, int elemsize)
  300: /* converts s of the format #+u (e.g. 25k) into the number of bytes.
  301:    the unit u can be one of bekM, where e stands for the element
  302:    size. default is e */
  303: {
  304:   char *endp;
  305:   int n,m;
  306: 
  307:   m = elemsize;
  308:   n = strtoul(s,&endp,0);
  309:   if (endp!=NULL) {
  310:     if (strcmp(endp,"b")==0)
  311:       m=1;
  312:     else if (strcmp(endp,"k")==0)
  313:       m=1024;
  314:     else if (strcmp(endp,"M")==0)
  315:       m=1024*1024;
  316:     else if (strcmp(endp,"e")!=0 && strcmp(endp,"")!=0) {
  317:       fprintf(stderr,"%s: cannot grok size specification %s: invalid unit \"%s\"\n", progname, s, endp);
  318:       exit(1);
  319:     }
  320:   }
  321:   return n*m;
  322: }
  323: 
  324: int main(int argc, char **argv, char **env)
  325: {
  326:   char *path, *path1;
  327:   char *imagename="gforth.fi";
  328:   FILE *image_file;
  329:   int c, retvalue;
  330: 	  
  331: #if defined(i386) && defined(ALIGNMENT_CHECK) && !defined(DIRECT_THREADED)
  332:   /* turn on alignment checks on the 486.
  333:    * on the 386 this should have no effect. */
  334:   __asm__("pushfl; popl %eax; orl $0x40000, %eax; pushl %eax; popfl;");
  335:   /* this is unusable with Linux' libc.4.6.27, because this library is
  336:      not alignment-clean; we would have to replace some library
  337:      functions (e.g., memcpy) to make it work */
  338: #endif
  339: 
  340:   progname = argv[0];
  341:   if ((path1=getenv("GFORTHPATH"))==NULL)
  342:     path1 = DEFAULTPATH;
  343:   
  344:   opterr=0;
  345:   while (1) {
  346:     int option_index=0;
  347:     static struct option opts[] = {
  348:       {"image-file", required_argument, NULL, 'i'},
  349:       {"dictionary-size", required_argument, NULL, 'm'},
  350:       {"data-stack-size", required_argument, NULL, 'd'},
  351:       {"return-stack-size", required_argument, NULL, 'r'},
  352:       {"fp-stack-size", required_argument, NULL, 'f'},
  353:       {"locals-stack-size", required_argument, NULL, 'l'},
  354:       {"path", required_argument, NULL, 'p'},
  355:       {"version", no_argument, NULL, 'v'},
  356:       {"help", no_argument, NULL, 'h'},
  357:       {0,0,0,0}
  358:       /* no-init-file, no-rc? */
  359:     };
  360:     
  361:     c = getopt_long(argc, argv, "+i:m:d:r:f:l:p:vh", opts, &option_index);
  362:     
  363:     if (c==EOF)
  364:       break;
  365:     if (c=='?') {
  366:       optind--;
  367:       break;
  368:     }
  369:     switch (c) {
  370:     case 'i': imagename = optarg; break;
  371:     case 'm': dictsize = convsize(optarg,sizeof(Cell)); break;
  372:     case 'd': dsize = convsize(optarg,sizeof(Cell)); break;
  373:     case 'r': rsize = convsize(optarg,sizeof(Cell)); break;
  374:     case 'f': fsize = convsize(optarg,sizeof(Float)); break;
  375:     case 'l': lsize = convsize(optarg,sizeof(Cell)); break;
  376:     case 'p': path1 = optarg; break;
  377:     case 'v': fprintf(stderr, "gforth %s\n", gforth_version); exit(0);
  378:     case 'h': 
  379:       fprintf(stderr, "Usage: %s [engine options] [image arguments]\n\
  380: Engine Options:\n\
  381:  -d SIZE, --data-stack-size=SIZE    Specify data stack size\n\
  382:  -f SIZE, --fp-stack-size=SIZE	    Specify floating point stack size\n\
  383:  -h, --help			    Print this message and exit\n\
  384:  -i FILE, --image-file=FILE	    Use image FILE instead of `gforth.fi'\n\
  385:  -l SIZE, --locals-stack-size=SIZE  Specify locals stack size\n\
  386:  -m SIZE, --dictionary-size=SIZE    Specify Forth dictionary size\n\
  387:  -p PATH, --path=PATH		    Search path for finding image and sources\n\
  388:  -r SIZE, --return-stack-size=SIZE  Specify return stack size\n\
  389:  -v, --version			    Print version and exit\n\
  390: SIZE arguments consists of an integer followed by a unit. The unit can be\n\
  391:   `b' (bytes), `e' (elements), `k' (kilobytes), or `M' (Megabytes).\n\
  392: \n\
  393: Arguments of default image `gforth.fi':\n\
  394:  FILE				    load FILE (with `require')\n\
  395:  -e STRING, --evaluate STRING       interpret STRING (with `EVALUATE')\n",
  396: 	      argv[0]); exit(0);
  397:     }
  398:   }
  399:   path=path1;
  400:   
  401:   if(strchr(imagename, '/')==NULL)
  402:     {
  403:       do {
  404: 	char *pend=strchr(path, PATHSEP);
  405: 	if (pend==NULL)
  406: 	  pend=path+strlen(path);
  407: 	if (strlen(path)==0) {
  408: 	  fprintf(stderr,"%s: cannot open image file %s in path %s for reading\n",
  409: 		  progname, imagename, path);
  410: 	  exit(1);
  411: 	}
  412: 	{
  413: 	  int dirlen=pend-path;
  414: 	  char fullfilename[dirlen+strlen(imagename)+2];
  415: 	  memcpy(fullfilename, path, dirlen);
  416: 	  if (fullfilename[dirlen-1]!='/')
  417: 	    fullfilename[dirlen++]='/';
  418: 	  strcpy(fullfilename+dirlen,imagename);
  419: 	  image_file=fopen(fullfilename,"rb");
  420: 	}
  421: 	path=pend+(*pend==PATHSEP);
  422:       } while (image_file==NULL);
  423:     }
  424:   else
  425:     {
  426:       image_file=fopen(imagename,"rb");
  427:     }
  428: 
  429:   {
  430:     char path2[strlen(path1)+1];
  431:     char *p1, *p2;
  432:     Cell environ[]= {
  433:       (Cell)argc-(optind-1),
  434:       (Cell)(argv+(optind-1)),
  435:       (Cell)strlen(path1),
  436:       (Cell)path2};
  437:     argv[optind-1] = progname;
  438:     /*
  439:        for (i=0; i<environ[0]; i++)
  440:        printf("%s\n", ((char **)(environ[1]))[i]);
  441:        */
  442:     /* make path OS-independent by replacing path separators with NUL */
  443:     for (p1=path1, p2=path2; *p1!='\0'; p1++, p2++)
  444:       if (*p1==PATHSEP)
  445: 	*p2 = '\0';
  446:       else
  447: 	*p2 = *p1;
  448:     *p2='\0';
  449:     retvalue=go_forth(loader(image_file, imagename),4,environ);
  450:     deprep_terminal();
  451:     exit(retvalue);
  452:   }
  453: }

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