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