1998-11-03 06:11:35 +00:00
|
|
|
/*
|
1998-11-04 03:42:16 +00:00
|
|
|
** stub main for testing FICL
|
1998-11-03 06:11:35 +00:00
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
1999-09-29 04:43:16 +00:00
|
|
|
/* $FreeBSD$ */
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1999-09-29 04:43:16 +00:00
|
|
|
#include <time.h>
|
1998-11-03 06:11:35 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "ficl.h"
|
|
|
|
|
|
|
|
/*
|
1998-11-04 03:42:16 +00:00
|
|
|
** Ficl interface to getcwd
|
1998-11-03 06:11:35 +00:00
|
|
|
** Prints the current working directory using the VM's
|
|
|
|
** textOut method...
|
|
|
|
*/
|
|
|
|
static void ficlGetCWD(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
cp = getcwd(NULL, 80);
|
|
|
|
vmTextOut(pVM, cp, 1);
|
|
|
|
free(cp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-04 03:42:16 +00:00
|
|
|
** Ficl interface to chdir
|
1998-11-03 06:11:35 +00:00
|
|
|
** Gets a newline (or NULL) delimited string from the input
|
1998-11-04 03:42:16 +00:00
|
|
|
** and feeds it to chdir()
|
1998-11-03 06:11:35 +00:00
|
|
|
** Example:
|
|
|
|
** cd c:\tmp
|
|
|
|
*/
|
|
|
|
static void ficlChDir(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
|
|
|
|
vmGetString(pVM, pFS, '\n');
|
|
|
|
if (pFS->count > 0)
|
|
|
|
{
|
|
|
|
int err = chdir(pFS->text);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
vmTextOut(pVM, "Error: path not found", 1);
|
|
|
|
vmThrow(pVM, VM_QUIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vmTextOut(pVM, "Warning (chdir): nothing happened", 1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Ficl interface to system (ANSI)
|
|
|
|
** Gets a newline (or NULL) delimited string from the input
|
1998-11-04 03:42:16 +00:00
|
|
|
** and feeds it to system()
|
1998-11-03 06:11:35 +00:00
|
|
|
** Example:
|
|
|
|
** system del *.*
|
|
|
|
** \ ouch!
|
|
|
|
*/
|
|
|
|
static void ficlSystem(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
FICL_STRING *pFS = (FICL_STRING *)pVM->pad;
|
|
|
|
|
|
|
|
vmGetString(pVM, pFS, '\n');
|
|
|
|
if (pFS->count > 0)
|
|
|
|
{
|
|
|
|
int err = system(pFS->text);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
sprintf(pVM->pad, "System call returned %d", err);
|
|
|
|
vmTextOut(pVM, pVM->pad, 1);
|
|
|
|
vmThrow(pVM, VM_QUIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vmTextOut(pVM, "Warning (system): nothing happened", 1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Ficl add-in to load a text file and execute it...
|
|
|
|
** Cheesy, but illustrative.
|
|
|
|
** Line oriented... filename is newline (or NULL) delimited.
|
|
|
|
** Example:
|
|
|
|
** load test.ficl
|
|
|
|
*/
|
|
|
|
#define nLINEBUF 256
|
|
|
|
static void ficlLoad(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
char cp[nLINEBUF];
|
|
|
|
char filename[nLINEBUF];
|
|
|
|
FICL_STRING *pFilename = (FICL_STRING *)filename;
|
|
|
|
int nLine = 0;
|
|
|
|
FILE *fp;
|
|
|
|
int result;
|
|
|
|
CELL id;
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
|
|
|
|
vmGetString(pVM, pFilename, '\n');
|
|
|
|
|
|
|
|
if (pFilename->count <= 0)
|
|
|
|
{
|
|
|
|
vmTextOut(pVM, "Warning (load): nothing happened", 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** get the file's size and make sure it exists
|
|
|
|
*/
|
|
|
|
result = stat( pFilename->text, &buf );
|
|
|
|
|
|
|
|
if (result != 0)
|
|
|
|
{
|
|
|
|
vmTextOut(pVM, "Unable to stat file: ", 0);
|
|
|
|
vmTextOut(pVM, pFilename->text, 1);
|
|
|
|
vmThrow(pVM, VM_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(pFilename->text, "r");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
vmTextOut(pVM, "Unable to open file ", 0);
|
|
|
|
vmTextOut(pVM, pFilename->text, 1);
|
|
|
|
vmThrow(pVM, VM_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
id = pVM->sourceID;
|
|
|
|
pVM->sourceID.p = (void *)fp;
|
|
|
|
|
|
|
|
/* feed each line to ficlExec */
|
|
|
|
while (fgets(cp, nLINEBUF, fp))
|
|
|
|
{
|
|
|
|
int len = strlen(cp) - 1;
|
|
|
|
|
|
|
|
nLine++;
|
|
|
|
if (len <= 0)
|
|
|
|
continue;
|
|
|
|
|
1999-09-29 04:43:16 +00:00
|
|
|
result = ficlExecC(pVM, cp, len);
|
1999-01-22 23:52:59 +00:00
|
|
|
if (result != VM_QUIT && result != VM_USEREXIT && result != VM_OUTOFTEXT )
|
1998-11-03 06:11:35 +00:00
|
|
|
{
|
|
|
|
pVM->sourceID = id;
|
|
|
|
fclose(fp);
|
|
|
|
vmThrowErr(pVM, "Error loading file <%s> line %d", pFilename->text, nLine);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
1999-09-29 04:43:16 +00:00
|
|
|
** Pass an empty line with SOURCE-ID == -1 to flush
|
1998-11-03 06:11:35 +00:00
|
|
|
** any pending REFILLs (as required by FILE wordset)
|
|
|
|
*/
|
|
|
|
pVM->sourceID.i = -1;
|
1999-11-23 11:17:37 +00:00
|
|
|
ficlExec(pVM, "");
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
pVM->sourceID = id;
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Dump a tab delimited file that summarizes the contents of the
|
|
|
|
** dictionary hash table by hashcode...
|
|
|
|
*/
|
|
|
|
static void spewHash(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
FICL_HASH *pHash = ficlGetDict()->pForthWords;
|
|
|
|
FICL_WORD *pFW;
|
|
|
|
FILE *pOut;
|
|
|
|
unsigned i;
|
|
|
|
unsigned nHash = pHash->size;
|
|
|
|
|
|
|
|
if (!vmGetWordToPad(pVM))
|
|
|
|
vmThrow(pVM, VM_OUTOFTEXT);
|
|
|
|
|
|
|
|
pOut = fopen(pVM->pad, "w");
|
|
|
|
if (!pOut)
|
|
|
|
{
|
|
|
|
vmTextOut(pVM, "unable to open file", 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i < nHash; i++)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
pFW = pHash->table[i];
|
|
|
|
while (pFW)
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
pFW = pFW->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(pOut, "%d\t%d", i, n);
|
|
|
|
|
|
|
|
pFW = pHash->table[i];
|
|
|
|
while (pFW)
|
|
|
|
{
|
|
|
|
fprintf(pOut, "\t%s", pFW->name);
|
|
|
|
pFW = pFW->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(pOut, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(pOut);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ficlBreak(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
pVM->state = pVM->state;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-09-29 04:43:16 +00:00
|
|
|
static void ficlClock(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
clock_t now = clock();
|
|
|
|
stackPushUNS(pVM->pStack, (UNS32)now);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clocksPerSec(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
stackPushUNS(pVM->pStack, CLOCKS_PER_SEC);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void execxt(FICL_VM *pVM)
|
|
|
|
{
|
|
|
|
FICL_WORD *pFW;
|
|
|
|
#if FICL_ROBUST > 1
|
|
|
|
vmCheckStack(pVM, 1, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pFW = stackPopPtr(pVM->pStack);
|
|
|
|
ficlExecXT(pVM, pFW);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
void buildTestInterface(void)
|
|
|
|
{
|
|
|
|
ficlBuild("break", ficlBreak, FW_DEFAULT);
|
1999-09-29 04:43:16 +00:00
|
|
|
ficlBuild("clock", ficlClock, FW_DEFAULT);
|
1998-11-03 06:11:35 +00:00
|
|
|
ficlBuild("cd", ficlChDir, FW_DEFAULT);
|
1999-09-29 04:43:16 +00:00
|
|
|
ficlBuild("execxt", execxt, FW_DEFAULT);
|
1998-11-03 06:11:35 +00:00
|
|
|
ficlBuild("load", ficlLoad, FW_DEFAULT);
|
|
|
|
ficlBuild("pwd", ficlGetCWD, FW_DEFAULT);
|
|
|
|
ficlBuild("system", ficlSystem, FW_DEFAULT);
|
|
|
|
ficlBuild("spewhash", spewHash, FW_DEFAULT);
|
1999-09-29 04:43:16 +00:00
|
|
|
ficlBuild("clocks/sec",
|
|
|
|
clocksPerSec, FW_DEFAULT);
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
1999-11-23 11:17:37 +00:00
|
|
|
char in[256];
|
1998-11-03 06:11:35 +00:00
|
|
|
FICL_VM *pVM;
|
|
|
|
|
|
|
|
ficlInitSystem(10000);
|
|
|
|
buildTestInterface();
|
|
|
|
pVM = ficlNewVM();
|
|
|
|
|
1999-11-23 11:17:37 +00:00
|
|
|
ficlExec(pVM, ".ver .( " __DATE__ " ) cr quit");
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** load file from cmd line...
|
|
|
|
*/
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
sprintf(in, ".( loading %s ) cr load %s\n cr", argv[1], argv[1]);
|
1999-11-23 11:17:37 +00:00
|
|
|
ficlExec(pVM, in);
|
1998-11-03 06:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int ret;
|
1998-11-04 03:42:16 +00:00
|
|
|
if (fgets(in, sizeof(in) - 1, stdin) == NULL)
|
|
|
|
break;
|
1999-11-23 11:17:37 +00:00
|
|
|
ret = ficlExec(pVM, in);
|
1998-11-03 06:11:35 +00:00
|
|
|
if (ret == VM_USEREXIT)
|
|
|
|
{
|
|
|
|
ficlTermSystem();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|