2
** stub main for testing FICL under Win32
10
#include <sys/types.h>
19
** Ficl interface to _getcwd (Win32)
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 (Win32)
35
** Gets a newline (or NULL) delimited string from the input
36
** and feeds it to the Win32 chdir function...
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 the Win32 system function...
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;
109
vmGetString(pVM, pFilename, '\n');
111
if (pFilename->count <= 0)
113
vmTextOut(pVM, "Warning (load): nothing happened", 1);
118
** get the file's size and make sure it exists
120
result = stat( pFilename->text, &buf );
124
vmTextOut(pVM, "Unable to stat file: ", 0);
125
vmTextOut(pVM, pFilename->text, 1);
126
vmThrow(pVM, VM_QUIT);
129
fp = fopen(pFilename->text, "r");
132
vmTextOut(pVM, "Unable to open file ", 0);
133
vmTextOut(pVM, pFilename->text, 1);
134
vmThrow(pVM, VM_QUIT);
138
pVM->sourceID.p = (void *)fp;
140
/* feed each line to ficlExec */
141
while (fgets(cp, nLINEBUF, fp))
143
int len = strlen(cp) - 1;
152
result = ficlExec(pVM, cp);
153
if (result != VM_OUTOFTEXT)
157
vmThrowErr(pVM, "Error loading file <%s> line %d", pFilename->text, nLine);
162
** Pass an empty line with SOURCE-ID == -1 to flush
163
** any pending REFILLs (as required by FILE wordset)
165
pVM->sourceID.i = -1;
175
** Dump a tab delimited file that summarizes the contents of the
176
** dictionary hash table by hashcode...
178
static void spewHash(FICL_VM *pVM)
180
FICL_HASH *pHash = ficlGetDict()->pForthWords;
184
unsigned nHash = pHash->size;
186
if (!vmGetWordToPad(pVM))
187
vmThrow(pVM, VM_OUTOFTEXT);
189
pOut = fopen(pVM->pad, "w");
192
vmTextOut(pVM, "unable to open file", 1);
196
for (i=0; i < nHash; i++)
200
pFW = pHash->table[i];
207
fprintf(pOut, "%d\t%d", i, n);
209
pFW = pHash->table[i];
212
fprintf(pOut, "\t%s", pFW->name);
223
static void ficlBreak(FICL_VM *pVM)
225
pVM->state = pVM->state;
229
static void ficlClock(FICL_VM *pVM)
231
clock_t now = clock();
232
stackPushUNS(pVM->pStack, (UNS32)now);
236
static void clocksPerSec(FICL_VM *pVM)
238
stackPushUNS(pVM->pStack, CLOCKS_PER_SEC);
243
static void execxt(FICL_VM *pVM)
247
vmCheckStack(pVM, 1, 0);
250
pFW = stackPopPtr(pVM->pStack);
251
ficlExecXT(pVM, pFW);
257
static void buildTestInterface(void)
259
ficlBuild("break", ficlBreak, FW_DEFAULT);
260
ficlBuild("clock", ficlClock, FW_DEFAULT);
261
ficlBuild("cd", ficlChDir, FW_DEFAULT);
262
ficlBuild("execxt", execxt, FW_DEFAULT);
263
ficlBuild("load", ficlLoad, FW_DEFAULT);
264
ficlBuild("pwd", ficlGetCWD, FW_DEFAULT);
265
ficlBuild("system", ficlSystem, FW_DEFAULT);
266
ficlBuild("spewhash", spewHash, FW_DEFAULT);
267
ficlBuild("clocks/sec",
268
clocksPerSec, FW_DEFAULT);
274
static int quiet = 0;
276
int main(int argc, char **argv)
278
char in[BUFSIZ], * s;
280
extern char * optarg;
281
extern int optind, opterr, optopt;
286
while ((c = getopt(argc, argv, "q")) != EOF)
297
ficlInitSystem(10000);
298
buildTestInterface();
302
ficlExec(pVM, ".ver .( " __DATE__ " ) cr quit");
304
for ( ; optind < argc; optind++) {
305
sprintf(in, ".( loading %s ) cr load %s\n cr", argv[optind], argv[optind]);
316
ret = ficlExec(pVM, in);
317
} while (ret != VM_USEREXIT && (s = fgets(in, sizeof(in)-1, stdin)) != NULL);