2 ** stub main for testing FICL under userland
3 ** $Id: testmain.c,v 1.13 2001/12/05 07:21:34 jsadler Exp $
6 ** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
7 ** All rights reserved.
9 ** Get the latest Ficl release at http://ficl.sourceforge.net
11 ** I am interested in hearing from anyone who uses ficl. If you have
12 ** a problem, a success story, a defect, an enhancement request, or
13 ** if you would like to contribute to the ficl release, please
14 ** contact me by email at the address above.
16 ** L I C E N S E and D I S C L A I M E R
18 ** Redistribution and use in source and binary forms, with or without
19 ** modification, are permitted provided that the following conditions
21 ** 1. Redistributions of source code must retain the above copyright
22 ** notice, this list of conditions and the following disclaimer.
23 ** 2. Redistributions in binary form must reproduce the above copyright
24 ** notice, this list of conditions and the following disclaimer in the
25 ** documentation and/or other materials provided with the distribution.
27 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * $FreeBSD: src/sys/boot/ficl/testmain.c,v 1.8 2002/04/09 17:45:11 dcs Exp $
42 * $DragonFly: src/sys/boot/ficl/testmain.c,v 1.3 2003/11/10 06:08:33 dillon Exp $
49 #include <sys/types.h>
56 ** Ficl interface to getcwd
57 ** Prints the current working directory using the VM's
60 static void ficlGetCWD(FICL_VM
*pVM
)
64 cp
= getcwd(NULL
, 80);
65 vmTextOut(pVM
, cp
, 1);
71 ** Ficl interface to chdir
72 ** Gets a newline (or NULL) delimited string from the input
73 ** and feeds it to chdir()
77 static void ficlChDir(FICL_VM
*pVM
)
79 FICL_STRING
*pFS
= (FICL_STRING
*)pVM
->pad
;
80 vmGetString(pVM
, pFS
, '\n');
83 int err
= chdir(pFS
->text
);
86 vmTextOut(pVM
, "Error: path not found", 1);
87 vmThrow(pVM
, VM_QUIT
);
92 vmTextOut(pVM
, "Warning (chdir): nothing happened", 1);
98 ** Ficl interface to system (ANSI)
99 ** Gets a newline (or NULL) delimited string from the input
100 ** and feeds it to system()
105 static void ficlSystem(FICL_VM
*pVM
)
107 FICL_STRING
*pFS
= (FICL_STRING
*)pVM
->pad
;
109 vmGetString(pVM
, pFS
, '\n');
112 int err
= system(pFS
->text
);
115 sprintf(pVM
->pad
, "System call returned %d", err
);
116 vmTextOut(pVM
, pVM
->pad
, 1);
117 vmThrow(pVM
, VM_QUIT
);
122 vmTextOut(pVM
, "Warning (system): nothing happened", 1);
128 ** Ficl add-in to load a text file and execute it...
129 ** Cheesy, but illustrative.
130 ** Line oriented... filename is newline (or NULL) delimited.
135 static void ficlLoad(FICL_VM
*pVM
)
138 char filename
[nLINEBUF
];
139 FICL_STRING
*pFilename
= (FICL_STRING
*)filename
;
147 vmGetString(pVM
, pFilename
, '\n');
149 if (pFilename
->count
<= 0)
151 vmTextOut(pVM
, "Warning (load): nothing happened", 1);
156 ** get the file's size and make sure it exists
158 result
= stat( pFilename
->text
, &buf
);
162 vmTextOut(pVM
, "Unable to stat file: ", 0);
163 vmTextOut(pVM
, pFilename
->text
, 1);
164 vmThrow(pVM
, VM_QUIT
);
167 fp
= fopen(pFilename
->text
, "r");
170 vmTextOut(pVM
, "Unable to open file ", 0);
171 vmTextOut(pVM
, pFilename
->text
, 1);
172 vmThrow(pVM
, VM_QUIT
);
176 pVM
->sourceID
.p
= (void *)fp
;
178 /* feed each line to ficlExec */
179 while (fgets(cp
, nLINEBUF
, fp
))
181 int len
= strlen(cp
) - 1;
187 result
= ficlExecC(pVM
, cp
, len
);
188 if (result
!= VM_QUIT
&& result
!= VM_USEREXIT
&& result
!= VM_OUTOFTEXT
)
192 vmThrowErr(pVM
, "Error loading file <%s> line %d", pFilename
->text
, nLine
);
197 ** Pass an empty line with SOURCE-ID == -1 to flush
198 ** any pending REFILLs (as required by FILE wordset)
200 pVM
->sourceID
.i
= -1;
206 /* handle "bye" in loaded files. --lch */
207 if (result
== VM_USEREXIT
)
208 vmThrow(pVM
, VM_USEREXIT
);
213 ** Dump a tab delimited file that summarizes the contents of the
214 ** dictionary hash table by hashcode...
216 static void spewHash(FICL_VM
*pVM
)
218 FICL_HASH
*pHash
= vmGetDict(pVM
)->pForthWords
;
222 unsigned nHash
= pHash
->size
;
224 if (!vmGetWordToPad(pVM
))
225 vmThrow(pVM
, VM_OUTOFTEXT
);
227 pOut
= fopen(pVM
->pad
, "w");
230 vmTextOut(pVM
, "unable to open file", 1);
234 for (i
=0; i
< nHash
; i
++)
238 pFW
= pHash
->table
[i
];
245 fprintf(pOut
, "%d\t%d", i
, n
);
247 pFW
= pHash
->table
[i
];
250 fprintf(pOut
, "\t%s", pFW
->name
);
261 static void ficlBreak(FICL_VM
*pVM
)
263 pVM
->state
= pVM
->state
;
267 static void ficlClock(FICL_VM
*pVM
)
269 clock_t now
= clock();
270 stackPushUNS(pVM
->pStack
, (FICL_UNS
)now
);
274 static void clocksPerSec(FICL_VM
*pVM
)
276 stackPushUNS(pVM
->pStack
, CLOCKS_PER_SEC
);
281 static void execxt(FICL_VM
*pVM
)
285 vmCheckStack(pVM
, 1, 0);
288 pFW
= stackPopPtr(pVM
->pStack
);
289 ficlExecXT(pVM
, pFW
);
295 void buildTestInterface(FICL_SYSTEM
*pSys
)
297 ficlBuild(pSys
, "break", ficlBreak
, FW_DEFAULT
);
298 ficlBuild(pSys
, "clock", ficlClock
, FW_DEFAULT
);
299 ficlBuild(pSys
, "cd", ficlChDir
, FW_DEFAULT
);
300 ficlBuild(pSys
, "execxt", execxt
, FW_DEFAULT
);
301 ficlBuild(pSys
, "load", ficlLoad
, FW_DEFAULT
);
302 ficlBuild(pSys
, "pwd", ficlGetCWD
, FW_DEFAULT
);
303 ficlBuild(pSys
, "system", ficlSystem
, FW_DEFAULT
);
304 ficlBuild(pSys
, "spewhash", spewHash
, FW_DEFAULT
);
305 ficlBuild(pSys
, "clocks/sec",
306 clocksPerSec
, FW_DEFAULT
);
312 int main(int argc
, char **argv
)
318 pSys
= ficlInitSystem(10000);
319 buildTestInterface(pSys
);
320 pVM
= ficlNewVM(pSys
);
322 ficlEvaluate(pVM
, ".ver .( " __DATE__
" ) cr quit");
325 ** load file from cmd line...
329 sprintf(in
, ".( loading %s ) cr load %s\n cr", argv
[1], argv
[1]);
330 ficlEvaluate(pVM
, in
);
336 if (fgets(in
, sizeof(in
) - 1, stdin
) == NULL
)
338 ret
= ficlExec(pVM
, in
);
339 if (ret
== VM_USEREXIT
)
341 ficlTermSystem(pSys
);