2 ** stub main for testing FICL
6 /* $FreeBSD: src/sys/boot/ficl/testmain.c,v 1.5.2.1 2000/07/06 23:51:45 obrien Exp $ */
12 #include <sys/types.h>
19 ** Ficl interface to getcwd
20 ** Prints the current working directory using the VM's
23 static void ficlGetCWD(FICL_VM *pVM)
27 cp = getcwd(NULL, 80);
28 vmTextOut(pVM, cp, 1);
34 ** Ficl interface to chdir
35 ** Gets a newline (or NULL) delimited string from the input
36 ** and feeds it to chdir()
40 static void ficlChDir(FICL_VM *pVM)
42 FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
43 vmGetString(pVM, pFS, '\n');
46 int err = chdir(pFS->text);
49 vmTextOut(pVM, "Error: path not found", 1);
50 vmThrow(pVM, VM_QUIT);
55 vmTextOut(pVM, "Warning (chdir): nothing happened", 1);
61 ** Ficl interface to system (ANSI)
62 ** Gets a newline (or NULL) delimited string from the input
63 ** and feeds it to system()
68 static void ficlSystem(FICL_VM *pVM)
70 FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
72 vmGetString(pVM, pFS, '\n');
75 int err = system(pFS->text);
78 sprintf(pVM->pad, "System call returned %d", err);
79 vmTextOut(pVM, pVM->pad, 1);
80 vmThrow(pVM, VM_QUIT);
85 vmTextOut(pVM, "Warning (system): nothing happened", 1);
91 ** Ficl add-in to load a text file and execute it...
92 ** Cheesy, but illustrative.
93 ** Line oriented... filename is newline (or NULL) delimited.
98 static void ficlLoad(FICL_VM *pVM)
101 char filename[nLINEBUF];
102 FICL_STRING *pFilename = (FICL_STRING *)filename;
110 vmGetString(pVM, pFilename, '\n');
112 if (pFilename->count <= 0)
114 vmTextOut(pVM, "Warning (load): nothing happened", 1);
119 ** get the file's size and make sure it exists
121 result = stat( pFilename->text, &buf );
125 vmTextOut(pVM, "Unable to stat file: ", 0);
126 vmTextOut(pVM, pFilename->text, 1);
127 vmThrow(pVM, VM_QUIT);
130 fp = fopen(pFilename->text, "r");
133 vmTextOut(pVM, "Unable to open file ", 0);
134 vmTextOut(pVM, pFilename->text, 1);
135 vmThrow(pVM, VM_QUIT);
139 pVM->sourceID.p = (void *)fp;
141 /* feed each line to ficlExec */
142 while (fgets(cp, nLINEBUF, fp))
144 int len = strlen(cp) - 1;
150 result = ficlExecC(pVM, cp, len);
151 if (result != VM_QUIT && result != VM_USEREXIT && result != VM_OUTOFTEXT )
155 vmThrowErr(pVM, "Error loading file <%s> line %d", pFilename->text, nLine);
160 ** Pass an empty line with SOURCE-ID == -1 to flush
161 ** any pending REFILLs (as required by FILE wordset)
163 pVM->sourceID.i = -1;
173 ** Dump a tab delimited file that summarizes the contents of the
174 ** dictionary hash table by hashcode...
176 static void spewHash(FICL_VM *pVM)
178 FICL_HASH *pHash = ficlGetDict()->pForthWords;
182 unsigned nHash = pHash->size;
184 if (!vmGetWordToPad(pVM))
185 vmThrow(pVM, VM_OUTOFTEXT);
187 pOut = fopen(pVM->pad, "w");
190 vmTextOut(pVM, "unable to open file", 1);
194 for (i=0; i < nHash; i++)
198 pFW = pHash->table[i];
205 fprintf(pOut, "%d\t%d", i, n);
207 pFW = pHash->table[i];
210 fprintf(pOut, "\t%s", pFW->name);
221 static void ficlBreak(FICL_VM *pVM)
223 pVM->state = pVM->state;
227 static void ficlClock(FICL_VM *pVM)
229 clock_t now = clock();
230 stackPushUNS(pVM->pStack, (FICL_UNS)now);
234 static void clocksPerSec(FICL_VM *pVM)
236 stackPushUNS(pVM->pStack, CLOCKS_PER_SEC);
241 static void execxt(FICL_VM *pVM)
245 vmCheckStack(pVM, 1, 0);
248 pFW = stackPopPtr(pVM->pStack);
249 ficlExecXT(pVM, pFW);
255 void buildTestInterface(void)
257 ficlBuild("break", ficlBreak, FW_DEFAULT);
258 ficlBuild("clock", ficlClock, FW_DEFAULT);
259 ficlBuild("cd", ficlChDir, FW_DEFAULT);
260 ficlBuild("execxt", execxt, FW_DEFAULT);
261 ficlBuild("load", ficlLoad, FW_DEFAULT);
262 ficlBuild("pwd", ficlGetCWD, FW_DEFAULT);
263 ficlBuild("system", ficlSystem, FW_DEFAULT);
264 ficlBuild("spewhash", spewHash, FW_DEFAULT);
265 ficlBuild("clocks/sec",
266 clocksPerSec, FW_DEFAULT);
272 int main(int argc, char **argv)
277 ficlInitSystem(10000);
278 buildTestInterface();
281 ficlExec(pVM, ".ver .( " __DATE__ " ) cr quit");
284 ** load file from cmd line...
288 sprintf(in, ".( loading %s ) cr load %s\n cr", argv[1], argv[1]);
295 if (fgets(in, sizeof(in) - 1, stdin) == NULL)
297 ret = ficlExec(pVM, in);
298 if (ret == VM_USEREXIT)