xref: /original-bsd/old/dbx/keywords.c (revision 3708840b)
1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)keywords.c 1.3 05/18/83";
4 
5 /*
6  * Keyword management.
7  */
8 
9 #include "defs.h"
10 #include "keywords.h"
11 #include "scanner.h"
12 #include "names.h"
13 #include "symbols.h"
14 #include "tree.h"
15 #include "y.tab.h"
16 
17 #ifndef public
18 #include "scanner.h"
19 #endif
20 
21 private String reserved[] ={
22     "alias", "and", "assign", "at", "call", "catch", "cont",
23     "debug", "delete", "div", "dump", "edit", "file", "func",
24     "gripe", "help", "if", "ignore", "in",
25     "list", "mod", "next", "nexti", "nil", "not", "or",
26     "print", "psym", "quit", "run",
27     "sh", "skip", "source", "status", "step", "stepi",
28     "stop", "stopi", "trace", "tracei",
29     "use", "whatis", "when", "where", "whereis", "which",
30     "INT", "REAL", "NAME", "STRING",
31     "LFORMER", "RFORMER", "#^", "->"
32 };
33 
34 /*
35  * The keyword table is a traditional hash table with collisions
36  * resolved by chaining.
37  */
38 
39 #define HASHTABLESIZE 503
40 
41 typedef struct Keyword {
42     Name name;
43     Token toknum : 16;
44     Boolean isalias : 16;
45     struct Keyword *chain;
46 } *Keyword;
47 
48 typedef unsigned int Hashvalue;
49 
50 private Keyword hashtab[HASHTABLESIZE];
51 
52 #define hash(n) ((((unsigned) n) >> 2) mod HASHTABLESIZE)
53 
54 /*
55  * Enter all the reserved words into the keyword table.
56  */
57 
58 public enterkeywords()
59 {
60     register Integer i;
61 
62     for (i = ALIAS; i <= WHICH; i++) {
63 	keyword(reserved[ord(i) - ord(ALIAS)], i, false);
64     }
65     keyword("set", ASSIGN, false);
66 }
67 
68 /*
69  * Deallocate the keyword table.
70  */
71 
72 public keywords_free()
73 {
74     register Integer i;
75     register Keyword k, nextk;
76 
77     for (i = 0; i < HASHTABLESIZE; i++) {
78 	k = hashtab[i];
79 	while (k != nil) {
80 	    nextk = k->chain;
81 	    dispose(k);
82 	    k = nextk;
83 	}
84 	hashtab[i] = nil;
85     }
86 }
87 
88 /*
89  * Enter a keyword into the name table.  It is assumed to not be there already.
90  * The string is assumed to be statically allocated.
91  */
92 
93 private keyword(s, t, isalias)
94 String s;
95 Token t;
96 Boolean isalias;
97 {
98     register Hashvalue h;
99     register Keyword k;
100     Name n;
101 
102     n = identname(s, true);
103     h = hash(n);
104     k = new(Keyword);
105     k->name = n;
106     k->toknum = t;
107     k->isalias = isalias;
108     k->chain = hashtab[h];
109     hashtab[h] = k;
110 }
111 
112 /*
113  * Return the string associated with a token corresponding to a keyword.
114  */
115 
116 public String keywdstring(t)
117 Token t;
118 {
119     return reserved[ord(t) - ord(ALIAS)];
120 }
121 
122 /*
123  * Find a keyword in the keyword table.
124  * We assume that tokens cannot legitimately be nil (0).
125  */
126 
127 public Token findkeyword(n)
128 Name n;
129 {
130     register Hashvalue h;
131     register Keyword k;
132     Token t;
133 
134     h = hash(n);
135     k = hashtab[h];
136     while (k != nil and k->name != n) {
137 	k = k->chain;
138     }
139     if (k == nil) {
140 	t = nil;
141     } else {
142 	t = k->toknum;
143     }
144     return t;
145 }
146 
147 /*
148  * Create an alias.
149  */
150 
151 public enter_alias(newcmd, oldcmd)
152 Name newcmd;
153 Name oldcmd;
154 {
155     Token t;
156 
157     t = findkeyword(oldcmd);
158     if (t == nil) {
159 	error("\"%s\" is not a command", ident(oldcmd));
160     } else {
161 	keyword(ident(newcmd), t, true);
162     }
163 }
164 
165 /*
166  * Print out an alias.
167  */
168 
169 public print_alias(cmd)
170 Name cmd;
171 {
172     register Keyword k;
173     register Integer i;
174     Token t;
175 
176     if (cmd == nil) {
177 	for (i = 0; i < HASHTABLESIZE; i++) {
178 	    for (k = hashtab[i]; k != nil; k = k->chain) {
179 		if (k->isalias) {
180 		    if (isredirected()) {
181 			printf("alias ");
182 		    }
183 		    printf("%s\t%s\n", ident(k->name), keywdstring(k->toknum));
184 		}
185 	    }
186 	}
187     } else {
188 	t = findkeyword(cmd);
189 	if (t == nil) {
190 	    printf("\n");
191 	} else {
192 	    printf("%s\n", keywdstring(t));
193 	}
194     }
195 }
196