Merge tag 'upstream/0.2.1'
[debian/yforth] / ycore.c
1 /* yForth? - A Forth interpreter written in ANSI C
2  * Copyright (C) 2012 Luca Padovani
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ------------------------------------------------------------------------
17  * Module name:     ycore.c
18  * Abstract:        Words defined for this particular implementation of
19                     forth. Do not expect to find these words in other
20                     implementations.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "ver.h"
27 #include "yforth.h"
28 #include "core.h"
29 #include "file.h"
30 #include "search.h"
31
32 /**************************************************************************/
33 /* WORDS DEFINITION *******************************************************/
34 /**************************************************************************/
35
36 /* ( --- ) print current version of yForth? */
37 void _yforth_version() {
38         print_version();
39 }
40
41 /* ( c-addr u --- ) save a snapshot of the current dictionary and vocabulary
42  * search order
43  */
44 void _save_image() {
45         FILE *f = fopen(get_file_name(), "wb");
46         struct image_header hd;
47         struct voc_marker vm;
48         if (f) {
49                 memset(&hd, 0, sizeof(struct image_header));
50                 strcpy(hd.header, "yForth? Image File\n");
51                 hd.ver_hi = VER_HI;
52                 hd.ver_lo = VER_LO;
53                 hd.base = dp0;
54                 hd.dspace_size = dspace_size;
55                 hd.pattern = VERSION_PATTERN;
56                 if (fwrite(&hd, sizeof(struct image_header), 1, f) < 1) _error = E_NOFILE;
57                 else {
58                         save_vocabulary(&vm);
59                         if (fwrite(&vm, sizeof(struct voc_marker), 1, f) < 1) _error = E_NOFILE;
60                         else {
61                                 if (fwrite(dp0, sizeof(Cell), dspace_size, f) < dspace_size)
62                                         _error = E_NOFILE;
63                         }
64                 }
65                 fclose(f);
66         } else _error = E_NOFILE;
67 }
68
69 /* ( c-addr u --- n ) execute command pointeb by c-addr via "system", n is 
70  * the result of operation as described in the C library manual 
71  */
72 void _system() {
73         register UCell len = *sp++;
74         register Char *name = (Char *) *sp; 
75         extern Char s_tmp_buffer[];
76         memcpy(s_tmp_buffer, name, len);
77         s_tmp_buffer[len] = '\0';
78         *sp = system(s_tmp_buffer);
79 }
80