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 $ */
7 /* $DragonFly: src/sys/boot/ficl/testmain.c,v 1.2 2003/06/17 04:28:17 dillon Exp $ */
13 #include <sys/types.h>
20 ** Ficl interface to getcwd
21 ** Prints the current working directory using the VM's
24 static void ficlGetCWD(FICL_VM *pVM)
28 cp = getcwd(NULL, 80);
29 vmTextOut(pVM, cp, 1);
35 ** Ficl interface to chdir
36 ** Gets a newline (or NULL) delimited string from the input
37 ** and feeds it to chdir()
41 static void ficlChDir(FICL_VM *pVM)
43 FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
44 vmGetString(pVM, pFS, '\n');
47 int err = chdir(pFS->text);
50 vmTextOut(pVM, "Error: path not found", 1);
51 vmThrow(pVM, VM_QUIT);
56 vmTextOut(pVM, "Warning (chdir): nothing happened", 1);
62 ** Ficl interface to system (ANSI)
63 ** Gets a newline (or NULL) delimited string from the input
64 ** and feeds it to system()
69 static void ficlSystem(FICL_VM *pVM)
71 FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
73 vmGetString(pVM, pFS, '\n');
76 int err = system(pFS->text);
79 sprintf(pVM->pad, "System call returned %d", err);
80 vmTextOut(pVM, pVM->pad, 1);
81 vmThrow(pVM, VM_QUIT);
86 vmTextOut(pVM, "Warning (system): nothing happened", 1);
92 ** Ficl add-in to load a text file and execute it...
93 ** Cheesy, but illustrative.
94 ** Line oriented... filename is newline (or NULL) delimited.
99 static void ficlLoad(FICL_VM *pVM)
102 char filename[nLINEBUF];
103 FICL_STRING *pFilename = (FICL_STRING *)filename;
111 vmGetString(pVM, pFilename, '\n');
113 if (pFilename->count <= 0)
115 vmTextOut(pVM, "Warning (load): nothing happened", 1);
120 ** get the file's size and make sure it exists
122 result = stat( pFilename->text, &buf );
126 vmTextOut(pVM, "Unable to stat file: ", 0);
127 vmTextOut(pVM, pFilename->text, 1);
128 vmThrow(pVM, VM_QUIT);
131 fp = fopen(pFilename->text, "r");
134 vmTextOut(pVM, "Unable to open file ", 0);
135 vmTextOut(pVM, pFilename->text, 1);
136 vmThrow(pVM, VM_QUIT);
140 pVM->sourceID.p = (void *)fp;
142 /* feed each line to ficlExec */
143 while (fgets(cp, nLINEBUF, fp))
145 int len = strlen(cp) - 1;
151 result = ficlExecC(pVM, cp, len);
152 if (result != VM_QUIT && result != VM_USEREXIT && result != VM_OUTOFTEXT )
156 vmThrowErr(pVM, "Error loading file <%s> line %d", pFilename->text, nLine);
161 ** Pass an empty line with SOURCE-ID == -1 to flush
162 ** any pending REFILLs (as required by FILE wordset)
164 pVM->sourceID.i = -1;
174 ** Dump a tab delimited file that summarizes the contents of the
175 ** dictionary hash table by hashcode...
177 static void spewHash(FICL_VM *pVM)
179 FICL_HASH *pHash = ficlGetDict()->pForthWords;
183 unsigned nHash = pHash->size;
185 if (!vmGetWordToPad(pVM))
186 vmThrow(pVM, VM_OUTOFTEXT);
188 pOut = fopen(pVM->pad, "w");
191 vmTextOut(pVM, "unable to open file", 1);
195 for (i=0; i < nHash; i++)
199 pFW = pHash->table[i];
206 fprintf(pOut, "%d\t%d", i, n);
208 pFW = pHash->table[i];
211 fprintf(pOut, "\t%s", pFW->name);
222 static void ficlBreak(FICL_VM *pVM)
224 pVM->state = pVM->state;
228 static void ficlClock(FICL_VM *pVM)
230 clock_t now = clock();
231 stackPushUNS(pVM->pStack, (FICL_UNS)now);
235 static void clocksPerSec(FICL_VM *pVM)
237 stackPushUNS(pVM->pStack, CLOCKS_PER_SEC);
242 static void execxt(FICL_VM *pVM)
246 vmCheckStack(pVM, 1, 0);
249 pFW = stackPopPtr(pVM->pStack);
250 ficlExecXT(pVM, pFW);
256 void buildTestInterface(void)
258 ficlBuild("break", ficlBreak, FW_DEFAULT);
259 ficlBuild("clock", ficlClock, FW_DEFAULT);
260 ficlBuild("cd", ficlChDir, FW_DEFAULT);
261 ficlBuild("execxt", execxt, FW_DEFAULT);
262 ficlBuild("load", ficlLoad, FW_DEFAULT);
263 ficlBuild("pwd", ficlGetCWD, FW_DEFAULT);
264 ficlBuild("system", ficlSystem, FW_DEFAULT);
265 ficlBuild("spewhash", spewHash, FW_DEFAULT);
266 ficlBuild("clocks/sec",
267 clocksPerSec, FW_DEFAULT);
273 int main(int argc, char **argv)
278 ficlInitSystem(10000);
279 buildTestInterface();
282 ficlExec(pVM, ".ver .( " __DATE__ " ) cr quit");
285 ** load file from cmd line...
289 sprintf(in, ".( loading %s ) cr load %s\n cr", argv[1], argv[1]);
296 if (fgets(in, sizeof(in) - 1, stdin) == NULL)
298 ret = ficlExec(pVM, in);
299 if (ret == VM_USEREXIT)