7 * Ficl interface to system (ANSI)
8 * Gets a newline (or NULL) delimited string from the input
9 * and feeds it to the ANSI system function...
15 ficlPrimitiveSystem(ficlVm
*vm
)
17 ficlCountedString
*counted
= (ficlCountedString
*)vm
->pad
;
19 ficlVmGetString(vm
, counted
, '\n');
20 if (FICL_COUNTED_STRING_GET_LENGTH(*counted
) > 0) {
22 system(FICL_COUNTED_STRING_GET_POINTER(*counted
));
24 sprintf(vm
->pad
, "System call returned %d\n",
26 ficlVmTextOut(vm
, vm
->pad
);
27 ficlVmThrow(vm
, FICL_VM_STATUS_QUIT
);
30 ficlVmTextOut(vm
, "Warning (system): nothing happened\n");
35 * Ficl add-in to load a text file and execute it...
36 * Cheesy, but illustrative.
37 * Line oriented... filename is newline (or NULL) delimited.
41 #define BUFFER_SIZE 256
43 ficlPrimitiveLoad(ficlVm
*vm
)
45 char buffer
[BUFFER_SIZE
];
46 char filename
[BUFFER_SIZE
];
47 ficlCountedString
*counted
= (ficlCountedString
*)filename
;
54 ficlVmGetString(vm
, counted
, '\n');
56 if (FICL_COUNTED_STRING_GET_LENGTH(*counted
) <= 0) {
57 ficlVmTextOut(vm
, "Warning (load): nothing happened\n");
62 * get the file's size and make sure it exists
65 f
= fopen(FICL_COUNTED_STRING_GET_POINTER(*counted
), "r");
67 ficlVmTextOut(vm
, "Unable to open file ");
68 ficlVmTextOut(vm
, FICL_COUNTED_STRING_GET_POINTER(*counted
));
69 ficlVmTextOut(vm
, "\n");
70 ficlVmThrow(vm
, FICL_VM_STATUS_QUIT
);
73 oldSourceId
= vm
->sourceId
;
74 vm
->sourceId
.p
= (void *)f
;
76 /* feed each line to ficlExec */
77 while (fgets(buffer
, BUFFER_SIZE
, f
)) {
78 int length
= strlen(buffer
) - 1;
84 if (buffer
[length
] == '\n')
85 buffer
[length
--] = '\0';
87 FICL_STRING_SET_POINTER(s
, buffer
);
88 FICL_STRING_SET_LENGTH(s
, length
+ 1);
89 result
= ficlVmExecuteString(vm
, s
);
90 /* handle "bye" in loaded files. --lch */
92 case FICL_VM_STATUS_OUT_OF_TEXT
:
93 case FICL_VM_STATUS_USER_EXIT
:
97 vm
->sourceId
= oldSourceId
;
99 ficlVmThrowError(vm
, "Error loading file <%s> line %d",
100 FICL_COUNTED_STRING_GET_POINTER(*counted
), line
);
105 * Pass an empty line with SOURCE-ID == -1 to flush
106 * any pending REFILLs (as required by FILE wordset)
109 FICL_STRING_SET_FROM_CSTRING(s
, "");
110 ficlVmExecuteString(vm
, s
);
112 vm
->sourceId
= oldSourceId
;
115 /* handle "bye" in loaded files. --lch */
116 if (result
== FICL_VM_STATUS_USER_EXIT
)
117 ficlVmThrow(vm
, FICL_VM_STATUS_USER_EXIT
);
121 * Dump a tab delimited file that summarizes the contents of the
122 * dictionary hash table by hashcode...
125 ficlPrimitiveSpewHash(ficlVm
*vm
)
127 ficlHash
*hash
= ficlVmGetDictionary(vm
)->forthWordlist
;
131 unsigned hashSize
= hash
->size
;
133 if (!ficlVmGetWordToPad(vm
))
134 ficlVmThrow(vm
, FICL_VM_STATUS_OUT_OF_TEXT
);
136 f
= fopen(vm
->pad
, "w");
138 ficlVmTextOut(vm
, "unable to open file\n");
142 for (i
= 0; i
< hashSize
; i
++) {
145 word
= hash
->table
[i
];
151 fprintf(f
, "%d\t%d", i
, n
);
153 word
= hash
->table
[i
];
155 fprintf(f
, "\t%s", word
->name
);
166 ficlPrimitiveBreak(ficlVm
*vm
)
168 vm
->state
= vm
->state
;
172 ficlSystemCompileExtras(ficlSystem
*system
)
174 ficlDictionary
*dictionary
= ficlSystemGetDictionary(system
);
176 ficlDictionarySetPrimitive(dictionary
, "break", ficlPrimitiveBreak
,
178 ficlDictionarySetPrimitive(dictionary
, "load", ficlPrimitiveLoad
,
180 ficlDictionarySetPrimitive(dictionary
, "spewhash",
181 ficlPrimitiveSpewHash
, FICL_WORD_DEFAULT
);
182 ficlDictionarySetPrimitive(dictionary
, "system", ficlPrimitiveSystem
,