[gforth] / gforth / engine / main.c  

gforth: gforth/engine/main.c


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help