[gforth] / gforth / Attic / main.c  

gforth: gforth/Attic/main.c


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help