summaryrefslogtreecommitdiff
path: root/usr/src/common/ficl/extras.c
blob: 2174f7a027a32addbc82ecf9474f5446166e4b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "ficl.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * Ficl interface to system (ANSI)
 * Gets a newline (or NULL) delimited string from the input
 * and feeds it to the ANSI system function...
 * Example:
 *    system del *.*
 *    \ ouch!
 */
static void
ficlPrimitiveSystem(ficlVm *vm)
{
	ficlCountedString *counted = (ficlCountedString *)vm->pad;

	(void) ficlVmGetString(vm, counted, '\n');
	if (FICL_COUNTED_STRING_GET_LENGTH(*counted) > 0) {
		int returnValue = \
		    system(FICL_COUNTED_STRING_GET_POINTER(*counted));
		if (returnValue) {
			(void) sprintf(vm->pad, "System call returned %d\n",
			    returnValue);
			ficlVmTextOut(vm, vm->pad);
			ficlVmThrow(vm, FICL_VM_STATUS_QUIT);
		}
	} else {
		ficlVmTextOut(vm, "Warning (system): nothing happened\n");
	}
}

/*
 * 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.f
 */
#define	BUFFER_SIZE	256
static void
ficlPrimitiveLoad(ficlVm *vm)
{
	char buffer[BUFFER_SIZE];
	char filename[BUFFER_SIZE];
	ficlCountedString *counted = (ficlCountedString *)filename;
	int line = 0;
	FILE *f;
	int result = 0;
	ficlCell oldSourceId;
	ficlString s;

	(void) ficlVmGetString(vm, counted, '\n');

	if (FICL_COUNTED_STRING_GET_LENGTH(*counted) <= 0) {
		ficlVmTextOut(vm, "Warning (load): nothing happened\n");
		return;
	}

	/*
	 * get the file's size and make sure it exists
	 */

	f = fopen(FICL_COUNTED_STRING_GET_POINTER(*counted), "r");
	if (!f) {
		ficlVmTextOut(vm, "Unable to open file ");
		ficlVmTextOut(vm, FICL_COUNTED_STRING_GET_POINTER(*counted));
		ficlVmTextOut(vm, "\n");
		ficlVmThrow(vm, FICL_VM_STATUS_QUIT);
	}

	oldSourceId = vm->sourceId;
	vm->sourceId.p = (void *)f;

	/* feed each line to ficlExec */
	while (fgets(buffer, BUFFER_SIZE, f)) {
		int length = strlen(buffer) - 1;

		line++;
		if (length <= 0)
			continue;

		if (buffer[length] == '\n')
			buffer[length--] = '\0';

		FICL_STRING_SET_POINTER(s, buffer);
		FICL_STRING_SET_LENGTH(s, length + 1);
		result = ficlVmExecuteString(vm, s);
		/* handle "bye" in loaded files. --lch */
		switch (result) {
		case FICL_VM_STATUS_OUT_OF_TEXT:
		case FICL_VM_STATUS_USER_EXIT:
		break;

		default:
			vm->sourceId = oldSourceId;
			(void) fclose(f);
			ficlVmThrowError(vm, "Error loading file <%s> line %d",
			    FICL_COUNTED_STRING_GET_POINTER(*counted), line);
		break;
		}
	}
	/*
	 * Pass an empty line with SOURCE-ID == -1 to flush
	 * any pending REFILLs (as required by FILE wordset)
	 */
	vm->sourceId.i = -1;
	FICL_STRING_SET_FROM_CSTRING(s, "");
	(void) ficlVmExecuteString(vm, s);

	vm->sourceId = oldSourceId;
	(void) fclose(f);

	/* handle "bye" in loaded files. --lch */
	if (result == FICL_VM_STATUS_USER_EXIT)
		ficlVmThrow(vm, FICL_VM_STATUS_USER_EXIT);
}

/*
 * Dump a tab delimited file that summarizes the contents of the
 * dictionary hash table by hashcode...
 */
static void
ficlPrimitiveSpewHash(ficlVm *vm)
{
	ficlHash *hash = ficlVmGetDictionary(vm)->forthWordlist;
	ficlWord *word;
	FILE *f;
	unsigned i;
	unsigned hashSize = hash->size;

	if (!ficlVmGetWordToPad(vm))
		ficlVmThrow(vm, FICL_VM_STATUS_OUT_OF_TEXT);

	f = fopen(vm->pad, "w");
	if (!f) {
		ficlVmTextOut(vm, "unable to open file\n");
		return;
	}

	for (i = 0; i < hashSize; i++) {
		int n = 0;

		word = hash->table[i];
		while (word) {
			n++;
			word = word->link;
		}

		(void) fprintf(f, "%d\t%d", i, n);

		word = hash->table[i];
		while (word) {
			(void) fprintf(f, "\t%s", word->name);
			word = word->link;
		}

		(void) fprintf(f, "\n");
	}

	(void) fclose(f);
}

static void
ficlPrimitiveBreak(ficlVm *vm)
{
	vm->state = vm->state;
}

void
ficlSystemCompileExtras(ficlSystem *system)
{
	ficlDictionary *dictionary = ficlSystemGetDictionary(system);

	(void) ficlDictionarySetPrimitive(dictionary, "break",
	    ficlPrimitiveBreak, FICL_WORD_DEFAULT);
	(void) ficlDictionarySetPrimitive(dictionary, "load",
	    ficlPrimitiveLoad, FICL_WORD_DEFAULT);
	(void) ficlDictionarySetPrimitive(dictionary, "spewhash",
	    ficlPrimitiveSpewHash, FICL_WORD_DEFAULT);
	(void) ficlDictionarySetPrimitive(dictionary, "system",
	    ficlPrimitiveSystem, FICL_WORD_DEFAULT);
}