1*ca987d46SWarner Losh /*
2*ca987d46SWarner Losh ** stub main for testing FICL under userland
3*ca987d46SWarner Losh ** $Id: testmain.c,v 1.13 2001/12/05 07:21:34 jsadler Exp $
4*ca987d46SWarner Losh */
5*ca987d46SWarner Losh /*
6*ca987d46SWarner Losh ** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
7*ca987d46SWarner Losh ** All rights reserved.
8*ca987d46SWarner Losh **
9*ca987d46SWarner Losh ** Get the latest Ficl release at http://ficl.sourceforge.net
10*ca987d46SWarner Losh **
11*ca987d46SWarner Losh ** I am interested in hearing from anyone who uses ficl. If you have
12*ca987d46SWarner Losh ** a problem, a success story, a defect, an enhancement request, or
13*ca987d46SWarner Losh ** if you would like to contribute to the ficl release, please
14*ca987d46SWarner Losh ** contact me by email at the address above.
15*ca987d46SWarner Losh **
16*ca987d46SWarner Losh ** L I C E N S E and D I S C L A I M E R
17*ca987d46SWarner Losh **
18*ca987d46SWarner Losh ** Redistribution and use in source and binary forms, with or without
19*ca987d46SWarner Losh ** modification, are permitted provided that the following conditions
20*ca987d46SWarner Losh ** are met:
21*ca987d46SWarner Losh ** 1. Redistributions of source code must retain the above copyright
22*ca987d46SWarner Losh ** notice, this list of conditions and the following disclaimer.
23*ca987d46SWarner Losh ** 2. Redistributions in binary form must reproduce the above copyright
24*ca987d46SWarner Losh ** notice, this list of conditions and the following disclaimer in the
25*ca987d46SWarner Losh ** documentation and/or other materials provided with the distribution.
26*ca987d46SWarner Losh **
27*ca987d46SWarner Losh ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28*ca987d46SWarner Losh ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29*ca987d46SWarner Losh ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30*ca987d46SWarner Losh ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31*ca987d46SWarner Losh ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32*ca987d46SWarner Losh ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33*ca987d46SWarner Losh ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34*ca987d46SWarner Losh ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35*ca987d46SWarner Losh ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36*ca987d46SWarner Losh ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37*ca987d46SWarner Losh ** SUCH DAMAGE.
38*ca987d46SWarner Losh */
39*ca987d46SWarner Losh
40*ca987d46SWarner Losh
41*ca987d46SWarner Losh #include <stdlib.h>
42*ca987d46SWarner Losh #include <stdio.h>
43*ca987d46SWarner Losh #include <string.h>
44*ca987d46SWarner Losh #include <time.h>
45*ca987d46SWarner Losh #include <sys/types.h>
46*ca987d46SWarner Losh #include <sys/stat.h>
47*ca987d46SWarner Losh #include <unistd.h>
48*ca987d46SWarner Losh
49*ca987d46SWarner Losh #include "ficl.h"
50*ca987d46SWarner Losh
51*ca987d46SWarner Losh /*
52*ca987d46SWarner Losh ** Ficl interface to getcwd
53*ca987d46SWarner Losh ** Prints the current working directory using the VM's
54*ca987d46SWarner Losh ** textOut method...
55*ca987d46SWarner Losh */
ficlGetCWD(FICL_VM * pVM)56*ca987d46SWarner Losh static void ficlGetCWD(FICL_VM *pVM)
57*ca987d46SWarner Losh {
58*ca987d46SWarner Losh char *cp;
59*ca987d46SWarner Losh
60*ca987d46SWarner Losh cp = getcwd(NULL, 80);
61*ca987d46SWarner Losh vmTextOut(pVM, cp, 1);
62*ca987d46SWarner Losh free(cp);
63*ca987d46SWarner Losh return;
64*ca987d46SWarner Losh }
65*ca987d46SWarner Losh
66*ca987d46SWarner Losh /*
67*ca987d46SWarner Losh ** Ficl interface to chdir
68*ca987d46SWarner Losh ** Gets a newline (or NULL) delimited string from the input
69*ca987d46SWarner Losh ** and feeds it to chdir()
70*ca987d46SWarner Losh ** Example:
71*ca987d46SWarner Losh ** cd c:\tmp
72*ca987d46SWarner Losh */
ficlChDir(FICL_VM * pVM)73*ca987d46SWarner Losh static void ficlChDir(FICL_VM *pVM)
74*ca987d46SWarner Losh {
75*ca987d46SWarner Losh FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
76*ca987d46SWarner Losh vmGetString(pVM, pFS, '\n');
77*ca987d46SWarner Losh if (pFS->count > 0)
78*ca987d46SWarner Losh {
79*ca987d46SWarner Losh int err = chdir(pFS->text);
80*ca987d46SWarner Losh if (err)
81*ca987d46SWarner Losh {
82*ca987d46SWarner Losh vmTextOut(pVM, "Error: path not found", 1);
83*ca987d46SWarner Losh vmThrow(pVM, VM_QUIT);
84*ca987d46SWarner Losh }
85*ca987d46SWarner Losh }
86*ca987d46SWarner Losh else
87*ca987d46SWarner Losh {
88*ca987d46SWarner Losh vmTextOut(pVM, "Warning (chdir): nothing happened", 1);
89*ca987d46SWarner Losh }
90*ca987d46SWarner Losh return;
91*ca987d46SWarner Losh }
92*ca987d46SWarner Losh
93*ca987d46SWarner Losh /*
94*ca987d46SWarner Losh ** Ficl interface to system (ANSI)
95*ca987d46SWarner Losh ** Gets a newline (or NULL) delimited string from the input
96*ca987d46SWarner Losh ** and feeds it to system()
97*ca987d46SWarner Losh ** Example:
98*ca987d46SWarner Losh ** system rm -rf /
99*ca987d46SWarner Losh ** \ ouch!
100*ca987d46SWarner Losh */
ficlSystem(FICL_VM * pVM)101*ca987d46SWarner Losh static void ficlSystem(FICL_VM *pVM)
102*ca987d46SWarner Losh {
103*ca987d46SWarner Losh FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
104*ca987d46SWarner Losh
105*ca987d46SWarner Losh vmGetString(pVM, pFS, '\n');
106*ca987d46SWarner Losh if (pFS->count > 0)
107*ca987d46SWarner Losh {
108*ca987d46SWarner Losh int err = system(pFS->text);
109*ca987d46SWarner Losh if (err)
110*ca987d46SWarner Losh {
111*ca987d46SWarner Losh sprintf(pVM->pad, "System call returned %d", err);
112*ca987d46SWarner Losh vmTextOut(pVM, pVM->pad, 1);
113*ca987d46SWarner Losh vmThrow(pVM, VM_QUIT);
114*ca987d46SWarner Losh }
115*ca987d46SWarner Losh }
116*ca987d46SWarner Losh else
117*ca987d46SWarner Losh {
118*ca987d46SWarner Losh vmTextOut(pVM, "Warning (system): nothing happened", 1);
119*ca987d46SWarner Losh }
120*ca987d46SWarner Losh return;
121*ca987d46SWarner Losh }
122*ca987d46SWarner Losh
123*ca987d46SWarner Losh /*
124*ca987d46SWarner Losh ** Ficl add-in to load a text file and execute it...
125*ca987d46SWarner Losh ** Cheesy, but illustrative.
126*ca987d46SWarner Losh ** Line oriented... filename is newline (or NULL) delimited.
127*ca987d46SWarner Losh ** Example:
128*ca987d46SWarner Losh ** load test.ficl
129*ca987d46SWarner Losh */
130*ca987d46SWarner Losh #define nLINEBUF 256
ficlLoad(FICL_VM * pVM)131*ca987d46SWarner Losh static void ficlLoad(FICL_VM *pVM)
132*ca987d46SWarner Losh {
133*ca987d46SWarner Losh char cp[nLINEBUF];
134*ca987d46SWarner Losh char filename[nLINEBUF];
135*ca987d46SWarner Losh FICL_STRING *pFilename = (FICL_STRING *)filename;
136*ca987d46SWarner Losh int nLine = 0;
137*ca987d46SWarner Losh FILE *fp;
138*ca987d46SWarner Losh int result;
139*ca987d46SWarner Losh CELL id;
140*ca987d46SWarner Losh struct stat buf;
141*ca987d46SWarner Losh
142*ca987d46SWarner Losh
143*ca987d46SWarner Losh vmGetString(pVM, pFilename, '\n');
144*ca987d46SWarner Losh
145*ca987d46SWarner Losh if (pFilename->count <= 0)
146*ca987d46SWarner Losh {
147*ca987d46SWarner Losh vmTextOut(pVM, "Warning (load): nothing happened", 1);
148*ca987d46SWarner Losh return;
149*ca987d46SWarner Losh }
150*ca987d46SWarner Losh
151*ca987d46SWarner Losh /*
152*ca987d46SWarner Losh ** get the file's size and make sure it exists
153*ca987d46SWarner Losh */
154*ca987d46SWarner Losh result = stat( pFilename->text, &buf );
155*ca987d46SWarner Losh
156*ca987d46SWarner Losh if (result != 0)
157*ca987d46SWarner Losh {
158*ca987d46SWarner Losh vmTextOut(pVM, "Unable to stat file: ", 0);
159*ca987d46SWarner Losh vmTextOut(pVM, pFilename->text, 1);
160*ca987d46SWarner Losh vmThrow(pVM, VM_QUIT);
161*ca987d46SWarner Losh }
162*ca987d46SWarner Losh
163*ca987d46SWarner Losh fp = fopen(pFilename->text, "r");
164*ca987d46SWarner Losh if (!fp)
165*ca987d46SWarner Losh {
166*ca987d46SWarner Losh vmTextOut(pVM, "Unable to open file ", 0);
167*ca987d46SWarner Losh vmTextOut(pVM, pFilename->text, 1);
168*ca987d46SWarner Losh vmThrow(pVM, VM_QUIT);
169*ca987d46SWarner Losh }
170*ca987d46SWarner Losh
171*ca987d46SWarner Losh id = pVM->sourceID;
172*ca987d46SWarner Losh pVM->sourceID.p = (void *)fp;
173*ca987d46SWarner Losh
174*ca987d46SWarner Losh /* feed each line to ficlExec */
175*ca987d46SWarner Losh while (fgets(cp, nLINEBUF, fp))
176*ca987d46SWarner Losh {
177*ca987d46SWarner Losh int len = strlen(cp) - 1;
178*ca987d46SWarner Losh
179*ca987d46SWarner Losh nLine++;
180*ca987d46SWarner Losh if (len <= 0)
181*ca987d46SWarner Losh continue;
182*ca987d46SWarner Losh
183*ca987d46SWarner Losh result = ficlExecC(pVM, cp, len);
184*ca987d46SWarner Losh if (result != VM_QUIT && result != VM_USEREXIT && result != VM_OUTOFTEXT )
185*ca987d46SWarner Losh {
186*ca987d46SWarner Losh pVM->sourceID = id;
187*ca987d46SWarner Losh fclose(fp);
188*ca987d46SWarner Losh vmThrowErr(pVM, "Error loading file <%s> line %d", pFilename->text, nLine);
189*ca987d46SWarner Losh break;
190*ca987d46SWarner Losh }
191*ca987d46SWarner Losh }
192*ca987d46SWarner Losh /*
193*ca987d46SWarner Losh ** Pass an empty line with SOURCE-ID == -1 to flush
194*ca987d46SWarner Losh ** any pending REFILLs (as required by FILE wordset)
195*ca987d46SWarner Losh */
196*ca987d46SWarner Losh pVM->sourceID.i = -1;
197*ca987d46SWarner Losh ficlExec(pVM, "");
198*ca987d46SWarner Losh
199*ca987d46SWarner Losh pVM->sourceID = id;
200*ca987d46SWarner Losh fclose(fp);
201*ca987d46SWarner Losh
202*ca987d46SWarner Losh /* handle "bye" in loaded files. --lch */
203*ca987d46SWarner Losh if (result == VM_USEREXIT)
204*ca987d46SWarner Losh vmThrow(pVM, VM_USEREXIT);
205*ca987d46SWarner Losh return;
206*ca987d46SWarner Losh }
207*ca987d46SWarner Losh
208*ca987d46SWarner Losh /*
209*ca987d46SWarner Losh ** Dump a tab delimited file that summarizes the contents of the
210*ca987d46SWarner Losh ** dictionary hash table by hashcode...
211*ca987d46SWarner Losh */
spewHash(FICL_VM * pVM)212*ca987d46SWarner Losh static void spewHash(FICL_VM *pVM)
213*ca987d46SWarner Losh {
214*ca987d46SWarner Losh FICL_HASH *pHash = vmGetDict(pVM)->pForthWords;
215*ca987d46SWarner Losh FICL_WORD *pFW;
216*ca987d46SWarner Losh FILE *pOut;
217*ca987d46SWarner Losh unsigned i;
218*ca987d46SWarner Losh unsigned nHash = pHash->size;
219*ca987d46SWarner Losh
220*ca987d46SWarner Losh if (!vmGetWordToPad(pVM))
221*ca987d46SWarner Losh vmThrow(pVM, VM_OUTOFTEXT);
222*ca987d46SWarner Losh
223*ca987d46SWarner Losh pOut = fopen(pVM->pad, "w");
224*ca987d46SWarner Losh if (!pOut)
225*ca987d46SWarner Losh {
226*ca987d46SWarner Losh vmTextOut(pVM, "unable to open file", 1);
227*ca987d46SWarner Losh return;
228*ca987d46SWarner Losh }
229*ca987d46SWarner Losh
230*ca987d46SWarner Losh for (i=0; i < nHash; i++)
231*ca987d46SWarner Losh {
232*ca987d46SWarner Losh int n = 0;
233*ca987d46SWarner Losh
234*ca987d46SWarner Losh pFW = pHash->table[i];
235*ca987d46SWarner Losh while (pFW)
236*ca987d46SWarner Losh {
237*ca987d46SWarner Losh n++;
238*ca987d46SWarner Losh pFW = pFW->link;
239*ca987d46SWarner Losh }
240*ca987d46SWarner Losh
241*ca987d46SWarner Losh fprintf(pOut, "%d\t%d", i, n);
242*ca987d46SWarner Losh
243*ca987d46SWarner Losh pFW = pHash->table[i];
244*ca987d46SWarner Losh while (pFW)
245*ca987d46SWarner Losh {
246*ca987d46SWarner Losh fprintf(pOut, "\t%s", pFW->name);
247*ca987d46SWarner Losh pFW = pFW->link;
248*ca987d46SWarner Losh }
249*ca987d46SWarner Losh
250*ca987d46SWarner Losh fprintf(pOut, "\n");
251*ca987d46SWarner Losh }
252*ca987d46SWarner Losh
253*ca987d46SWarner Losh fclose(pOut);
254*ca987d46SWarner Losh return;
255*ca987d46SWarner Losh }
256*ca987d46SWarner Losh
ficlBreak(FICL_VM * pVM)257*ca987d46SWarner Losh static void ficlBreak(FICL_VM *pVM)
258*ca987d46SWarner Losh {
259*ca987d46SWarner Losh pVM->state = pVM->state;
260*ca987d46SWarner Losh return;
261*ca987d46SWarner Losh }
262*ca987d46SWarner Losh
ficlClock(FICL_VM * pVM)263*ca987d46SWarner Losh static void ficlClock(FICL_VM *pVM)
264*ca987d46SWarner Losh {
265*ca987d46SWarner Losh clock_t now = clock();
266*ca987d46SWarner Losh stackPushUNS(pVM->pStack, (FICL_UNS)now);
267*ca987d46SWarner Losh return;
268*ca987d46SWarner Losh }
269*ca987d46SWarner Losh
clocksPerSec(FICL_VM * pVM)270*ca987d46SWarner Losh static void clocksPerSec(FICL_VM *pVM)
271*ca987d46SWarner Losh {
272*ca987d46SWarner Losh stackPushUNS(pVM->pStack, CLOCKS_PER_SEC);
273*ca987d46SWarner Losh return;
274*ca987d46SWarner Losh }
275*ca987d46SWarner Losh
276*ca987d46SWarner Losh
execxt(FICL_VM * pVM)277*ca987d46SWarner Losh static void execxt(FICL_VM *pVM)
278*ca987d46SWarner Losh {
279*ca987d46SWarner Losh FICL_WORD *pFW;
280*ca987d46SWarner Losh #if FICL_ROBUST > 1
281*ca987d46SWarner Losh vmCheckStack(pVM, 1, 0);
282*ca987d46SWarner Losh #endif
283*ca987d46SWarner Losh
284*ca987d46SWarner Losh pFW = stackPopPtr(pVM->pStack);
285*ca987d46SWarner Losh ficlExecXT(pVM, pFW);
286*ca987d46SWarner Losh
287*ca987d46SWarner Losh return;
288*ca987d46SWarner Losh }
289*ca987d46SWarner Losh
290*ca987d46SWarner Losh
buildTestInterface(FICL_SYSTEM * pSys)291*ca987d46SWarner Losh void buildTestInterface(FICL_SYSTEM *pSys)
292*ca987d46SWarner Losh {
293*ca987d46SWarner Losh ficlBuild(pSys, "break", ficlBreak, FW_DEFAULT);
294*ca987d46SWarner Losh ficlBuild(pSys, "clock", ficlClock, FW_DEFAULT);
295*ca987d46SWarner Losh ficlBuild(pSys, "cd", ficlChDir, FW_DEFAULT);
296*ca987d46SWarner Losh ficlBuild(pSys, "execxt", execxt, FW_DEFAULT);
297*ca987d46SWarner Losh ficlBuild(pSys, "load", ficlLoad, FW_DEFAULT);
298*ca987d46SWarner Losh ficlBuild(pSys, "pwd", ficlGetCWD, FW_DEFAULT);
299*ca987d46SWarner Losh ficlBuild(pSys, "system", ficlSystem, FW_DEFAULT);
300*ca987d46SWarner Losh ficlBuild(pSys, "spewhash", spewHash, FW_DEFAULT);
301*ca987d46SWarner Losh ficlBuild(pSys, "clocks/sec",
302*ca987d46SWarner Losh clocksPerSec, FW_DEFAULT);
303*ca987d46SWarner Losh
304*ca987d46SWarner Losh return;
305*ca987d46SWarner Losh }
306*ca987d46SWarner Losh
307*ca987d46SWarner Losh
main(int argc,char ** argv)308*ca987d46SWarner Losh int main(int argc, char **argv)
309*ca987d46SWarner Losh {
310*ca987d46SWarner Losh char in[256];
311*ca987d46SWarner Losh FICL_VM *pVM;
312*ca987d46SWarner Losh FICL_SYSTEM *pSys;
313*ca987d46SWarner Losh
314*ca987d46SWarner Losh pSys = ficlInitSystem(10000);
315*ca987d46SWarner Losh buildTestInterface(pSys);
316*ca987d46SWarner Losh pVM = ficlNewVM(pSys);
317*ca987d46SWarner Losh
318*ca987d46SWarner Losh ficlEvaluate(pVM, ".ver .( " __DATE__ " ) cr quit");
319*ca987d46SWarner Losh
320*ca987d46SWarner Losh /*
321*ca987d46SWarner Losh ** load file from cmd line...
322*ca987d46SWarner Losh */
323*ca987d46SWarner Losh if (argc > 1)
324*ca987d46SWarner Losh {
325*ca987d46SWarner Losh sprintf(in, ".( loading %s ) cr load %s\n cr", argv[1], argv[1]);
326*ca987d46SWarner Losh ficlEvaluate(pVM, in);
327*ca987d46SWarner Losh }
328*ca987d46SWarner Losh
329*ca987d46SWarner Losh for (;;)
330*ca987d46SWarner Losh {
331*ca987d46SWarner Losh int ret;
332*ca987d46SWarner Losh if (fgets(in, sizeof(in) - 1, stdin) == NULL)
333*ca987d46SWarner Losh break;
334*ca987d46SWarner Losh ret = ficlExec(pVM, in);
335*ca987d46SWarner Losh if (ret == VM_USEREXIT)
336*ca987d46SWarner Losh {
337*ca987d46SWarner Losh ficlTermSystem(pSys);
338*ca987d46SWarner Losh break;
339*ca987d46SWarner Losh }
340*ca987d46SWarner Losh }
341*ca987d46SWarner Losh
342*ca987d46SWarner Losh return 0;
343*ca987d46SWarner Losh }
344*ca987d46SWarner Losh
345