File: builtin-command.c

package info (click to toggle)
nickle 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,612 kB
  • ctags: 3,746
  • sloc: ansic: 26,986; yacc: 1,873; sh: 954; lex: 884; makefile: 225
file content (258 lines) | stat: -rw-r--r-- 6,385 bytes parent folder | download | duplicates (6)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* $Header$ */

/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

/*
 *	command.c
 *
 *	provide builtin functions for the Command namespace
 */

#include	<ctype.h>
#include	<strings.h>
#include	<time.h>
#include	"builtin.h"

NamespacePtr CommandNamespace;

void
import_Command_namespace()
{
    ENTER ();
    static const struct fbuiltin_1 funcs_1[] = {
        { do_Command_delete, "delete", "b", "s", "\n"
	    " bool delete(string command)\n"
	    "\n"
	    " Remove 'command' from the set of valid commands\n" },
        { do_Command_edit, "edit", "v", "A*s", "\n"
	    " void edit(string[*] function)\n"
	    "\n"
	    " Invoke $EDITOR on 'function', parse the file when done\n" },
	{ do_Command_display, "display", "v", "p", "\n"
	    " void display (poly value)\n"
	    "\n"
	    " Built-in display primitive for read/eval/print loop" },
	{ do_Command_valid_name, "valid_name", "b", "A*s", "\n"
	    " bool valid_name (string[*] name)\n"
	    "\n"
	    " Check for a symbol table entry\n" },
        { 0 }
    };

    static const struct fbuiltin_2 funcs_2[] = {
        { do_Command_new, "new", "v", "sp", "\n"
	    " void new (string name, poly f)\n"
	    "\n"
	    " Create a new command which calls 'f'.\n"
	    " 'f' will be invoked with an array of values\n" },
        { do_Command_new_names, "new_names", "v", "sp", "\n"
	    " void new_names (string name, poly f)\n"
	    "\n"
	    " Create a new command which calls 'f' with literal arguments.\n"
	    " 'f' will be invoked with an array of strings\n" },
        { 0 }
    };

    static const struct fbuiltin_4 funcs_4[] = {
	{ do_Command_lex_input, "lex_input", "b", "fsbb", "\n"
	    " bool lex_input (file f, string name, bool after, bool interactive)"
	    "\n"
	    " Add 'f' to the list of files to be read by the lexer.\n"
	    " 'name' will be used when reporting parse errors.\n"
	    " If 'after', the specified file will be read when all other\n"
	    " input sources are exhausted.\n"
	    " If 'interactive', the lexer will display prompts as if\n"
	    " input was coming from a terminal.\n" },
        { 0 }
    };

    static const struct fbuiltin_v funcs_v[] = {
        { do_Command_undefine, "undefine", "v", ".A*s", "\n"
	    " void undefine (string[*] ... name)\n"
	    "\n"
	    " removes 'name' from its namespace\n" },
        { do_Command_pretty_print, "pretty_print", "v", "f.A*s", "\n"
	    " void pretty_print (file f, string[*] ... name)\n"
	    "\n"
	    " Prints the variable and value in a format capable of\n"
	    " reproducing the value if fed back to the parser\n" },
	
        { 0 }
    };

    CommandNamespace = BuiltinNamespace (/*parent*/ 0, "Command")->namespace.namespace;

    BuiltinFuncs1 (&CommandNamespace, funcs_1);
    BuiltinFuncs2 (&CommandNamespace, funcs_2);
    BuiltinFuncs4 (&CommandNamespace, funcs_4);
    BuiltinFuncsV (&CommandNamespace, funcs_v);
    EXIT ();
}

static char *
command_name (Value name)
{
    char    *cmd_base = StrzPart (name, "argument must be valid name");
    char    *cmd = cmd_base;
    int	    c;
    
    if (!cmd_base)
	return 0;
    while ((c = *cmd++))
    {
	if (isupper (c) || islower (c))
	    continue;
	if (cmd != cmd_base + 1)
	{
	    if (isdigit ((int)c) || c == '_')
	     continue;
	}
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("argument must be valid name"),
				NewInt (0), name);
	return 0;
    }
    return cmd_base;
}

static Value
do_Command_new_common (Value name, Value func, Bool names)
{
    ENTER();
    char    *cmd = command_name (name);
    
    if (!cmd)
	RETURN (Void);
    if (!ValueIsFunc(func))
    {
	RaiseStandardException (exception_invalid_argument, 3,
				NewStrString ("argument must be func"),
				NewInt (1), func);
	RETURN (Void);
    }
    CurrentCommands = NewCommand (CurrentCommands, AtomId (cmd),
				  func, names);
    RETURN (Void);
}

Value
do_Command_new (Value name, Value func)
{
    return do_Command_new_common (name, func, False);
}

Value
do_Command_new_names (Value name, Value func)
{
    return do_Command_new_common (name, func, True);
}

Value
do_Command_delete (Value name)
{
    ENTER();
    Atom    id;
    char    *cmd = command_name (name);

    if (!cmd)
	RETURN (Void);
    id = AtomId (cmd);
    if (!CommandFind (CurrentCommands, id))
	RETURN (FalseVal);
    CurrentCommands = CommandRemove (CurrentCommands, id);
    RETURN (TrueVal);
}

Value
do_Command_pretty_print (int argc, Value *args)
{
    ENTER();
    Value	    f;
    Value	    names;
    NamespacePtr    namespace;
    SymbolPtr	    symbol;
    Publish	    publish;
    int		    i;

    f = args[0];
    if (argc == 1) {
	PrettyPrint (f, publish_public, 0);
	RETURN (Void);
    }
    for (i = 1; i < argc; i++)
    {
	names = args[i];
	if (NamespaceLocate (names, &namespace, &symbol, &publish, False))
	    PrettyPrint (f, publish, symbol);
	else
	    RaiseStandardException (exception_invalid_argument, 3,
				    NewStrString ("name not found"),
				    NewInt (i), names);
    }
    RETURN (Void);
}

Value
do_Command_undefine (int argc, Value *args)
{
    ENTER ();
    NamespacePtr    namespace;
    SymbolPtr	    symbol;
    Publish	    publish;
    int		    i;
    
    for (i = 0; i < argc; i++)
	if (NamespaceLocate (args[i], &namespace, &symbol, &publish, True))
	    NamespaceRemoveName (namespace, symbol->symbol.name);
    RETURN (Void);
}

Value
do_Command_valid_name (Value names)
{
    ENTER ();
    NamespacePtr    namespace;
    SymbolPtr	    symbol;
    Publish	    publish;
    Value	    ret;
    
    if (NamespaceLocate (names, &namespace, &symbol, &publish, False))
	ret = TrueVal;
    else
	ret = FalseVal;
    RETURN (ret);
}

Value
do_Command_edit (Value names)
{
    ENTER();
    NamespacePtr    namespace;
    SymbolPtr	    symbol;
    Publish	    publish;

    if (NamespaceLocate (names, &namespace, &symbol, &publish, True))
	EditFunction (symbol, publish); 
    RETURN (Void);
}

Value
do_Command_display (Value v)
{
    ENTER ();
    FilePrintf (FileStdout, "%v\n", v);
    RETURN (Void);
}

Value
do_Command_lex_input (Value file, Value name, Value after, Value interactive)
{
    ENTER ();
    NewLexInput (file, AtomId (StringChars (&name->string)), 
		 after == TrueVal, interactive == TrueVal);
    RETURN (TrueVal);
}