* Copyright (c) 1983 The Regents of the University of California.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)keywords.c 5.4 (Berkeley) %G%";
* Keywords, variables, and aliases (oh my!).
private String reserved
[] ={
"alias", "and", "assign", "at", "call", "catch", "cont",
"debug", "delete", "div", "down", "dump", "edit", "file", "func",
"gripe", "help", "if", "ignore", "in",
"list", "mod", "next", "nexti", "nil", "not", "or",
"print", "psym", "quit", "rerun", "return", "run",
"set", "sh", "skip", "source", "status", "step", "stepi",
"stop", "stopi", "trace", "tracei", "unalias", "unset", "up", "use",
"whatis", "when", "where", "whereis", "which",
"INT", "CHAR", "REAL", "NAME", "STRING", "->"
* The keyword table is a traditional hash table with collisions
#define HASHTABLESIZE 1007
typedef enum { ISKEYWORD
, ISALIAS
, ISVAR
} KeywordType
;
typedef unsigned int Hashvalue
;
private Keyword hashtab
[HASHTABLESIZE
];
#define hash(n) ((((unsigned) n) >> 2) mod HASHTABLESIZE)
* Enter all the reserved words into the keyword table.
* If the vaddrs flag is set (through the -k command line option) then
* set the special "$mapaddrs" variable. This assumes that the
* command line arguments are scanned before this routine is called.
for (i
= ALIAS
; i
<= WHICH
; i
++) {
keyword(reserved
[ord(i
) - ord(ALIAS
)], i
);
defvar(identname("$mapaddrs", true), nil
);
* Deallocate the keyword table.
register Keyword k
, nextk
;
for (i
= 0; i
< HASHTABLESIZE
; i
++) {
* Insert a name into the keyword table and return the keyword for it.
private Keyword
keywords_insert (n
)
* Find the keyword associated with the given name.
private Keyword
keywords_lookup (n
)
while (k
!= nil
and k
->name
!= n
) {
* Delete the given keyword of the given class.
private boolean
keywords_delete (n
, class)
register Keyword k
, prevk
;
while (k
!= nil
and (k
->name
!= n
or k
->class != class)) {
* Enter a keyword into the table. It is assumed to not be there already.
* The string is assumed to be statically allocated.
* Define a builtin command name alias.
private defalias (s1
, s2
)
alias(identname(s1
, true), nil
, s2
);
* Look for a word of a particular class.
private Keyword
findword (n
, class)
while (k
!= nil
and (k
->name
!= n
or k
->class != class)) {
* Return the token associated with a given keyword string.
* If there is none, return the given default value.
public Token
findkeyword (n
, def
)
k
= findword(n
, ISKEYWORD
);
* Return the associated string if there is an alias with the given name.
public boolean
findalias (n
, pl
, str
)
k
= findword(n
, ISALIAS
);
*pl
= k
->value
.alias
.paramlist
;
*str
= k
->value
.alias
.expansion
;
* Return the string associated with a token corresponding to a keyword.
public String
keywdstring (t
)
return reserved
[ord(t
) - ord(ALIAS
)];
* Process an alias command, either entering a new alias or printing out
public alias (newcmd
, args
, str
)
k
= findword(newcmd
, ISALIAS
);
k
= keywords_insert(newcmd
);
k
->value
.alias
.paramlist
= args
;
k
->value
.alias
.expansion
= str
;
private print_alias (cmd
)
for (i
= 0; i
< HASHTABLESIZE
; i
++) {
for (k
= hashtab
[i
]; k
!= nil
; k
= k
->chain
) {
if (k
->class == ISALIAS
) {
printf("alias %s", ident(k
->name
));
printparams(k
->value
.alias
.paramlist
);
printf("\t\"%s\"\n", k
->value
.alias
.expansion
);
printf("%s", ident(k
->name
));
printparams(k
->value
.alias
.paramlist
);
printf("\t%s\n", k
->value
.alias
.expansion
);
k
= findword(cmd
, ISALIAS
);
printparams(k
->value
.alias
.paramlist
);
printf("%s\n", k
->value
.alias
.expansion
);
if (not keywords_delete(n
, ISALIAS
)) {
error("%s is not aliased", ident(n
));
error("\"%s\" is a program symbol -- use assign", ident(n
));
if (n
== identname("$mapaddrs", true)) {
* Return the value associated with a variable.
* Return whether or not a variable is set.
public boolean
varIsSet (s
)
return (boolean
) (findword(identname(s
, false), ISVAR
) != nil
);
if (not keywords_delete(n
, ISVAR
)) {
error("%s is not set", ident(n
));
if (n
== identname("$mapaddrs", true)) {
* Print out all the values of set variables.
for (i
= 0; i
< HASHTABLESIZE
; i
++) {
for (k
= hashtab
[i
]; k
!= nil
; k
= k
->chain
) {
printf("%s", ident(k
->name
));
if (k
->value
.var
!= nil
) {
prtree(stdout
, k
->value
.var
);