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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
include <ctotok.h>
include <error.h>
include <ctype.h>
task sym = t_sym
define LOOKUP 1
define ENTER 2
define MARK 3
define FREE 4
define LISTALL 5
define SQUEEZE 6
define SAVE 7
define RESTORE 8
define INFO 9
define SCANFILE 10
define HELP 11
# SYM -- Test symbol entry and retrieval using the SYMTAB package.
procedure t_sym()
bool timeit
pointer stp, sym
long svtime[2]
char lbuf[SZ_LINE], key[SZ_FNAME]
int ip, lp, stmt, marker, fd, indexlen, stablen, sbuflen, junk
bool clgetb()
int ctowrd(), strlen(), getline(), strmatch()
int stpstr(), sthead(), stnext(), open(), clgeti()
pointer stopen(), stfind(), stenter(), strestore()
begin
indexlen = clgeti ("indexlen")
stablen = clgeti ("stablen")
sbuflen = clgeti ("sbuflen")
timeit = clgetb ("timeit")
stp = stopen ("symtab.zzdebug test symbol table",
indexlen, stablen, sbuflen)
repeat {
call printf ("* ")
call flush (STDOUT)
if (getline (STDIN, lbuf) == EOF) {
call printf ("\n")
break
} else if (strmatch (lbuf, "^bye") > 0)
break
for (ip=1; IS_WHITE(lbuf[ip]); ip=ip+1)
;
# Determine type of statement.
switch (lbuf[ip]) {
case '\n':
next
case '=':
ip = ip + 1
stmt = LOOKUP
default:
if (strmatch (lbuf[ip], "^.mark") > 0) {
stmt = MARK
} else if (strmatch (lbuf[ip], "^.free") > 0) {
stmt = FREE
} else if (strmatch (lbuf[ip], "^.list") > 0) {
stmt = LISTALL
} else if (strmatch (lbuf[ip], "^.squeeze") > 0) {
stmt = SQUEEZE
} else if (strmatch (lbuf[ip], "^.save") > 0) {
stmt = SAVE
ip = ip + 5
} else if (strmatch (lbuf[ip], "^.restore") > 0) {
stmt = RESTORE
ip = ip + 8
} else if (strmatch (lbuf[ip], "^.info") > 0) {
stmt = INFO
ip = ip + 5
} else if (strmatch (lbuf[ip], "^.scanfile") > 0) {
stmt = SCANFILE
ip = ip + 9
} else if (strmatch (lbuf[ip], "^.help") > 0) {
stmt = HELP
ip = ip + 5
} else
stmt = ENTER
}
# Extract key name (or filename).
junk = ctowrd (lbuf, ip, key, SZ_FNAME)
if (timeit)
call sys_mtime (svtime)
switch (stmt) {
case LOOKUP:
# Lookup symbol in table.
sym = stfind (stp, key)
if (sym == NULL) {
call eprintf ("`%s' not found\n")
call pargstr (key)
next
}
# Print keyword = value.
call psym (stp, sym)
case ENTER:
# Enter symbol in table.
sym = stenter (stp, key, 1)
# Get offset of value string.
ip = strmatch (lbuf, "=")
if (ip == 0)
ip = strlen(lbuf) + 1
else {
while (IS_WHITE (lbuf[ip]))
ip = ip + 1
}
# Step on the newline.
for (lp=ip; lbuf[lp] != EOS; lp=lp+1)
if (lbuf[lp] == '\n') {
lbuf[lp] = EOS
break
}
# Deposit value string in symbol table string buffer and save
# offset in symstruct.
Memi[sym] = -stpstr (stp, lbuf[ip], 0)
case MARK:
call stmark (stp, marker)
case FREE:
call stfree (stp, marker)
case LISTALL:
for (sym=sthead(stp); sym != NULL; sym=stnext(stp,sym))
call psym (stp, sym)
case SQUEEZE:
call stsqueeze (stp)
case SAVE:
# In this case 'key' contains the savefile filename.
iferr (call delete (key))
;
iferr (fd = open (key, NEW_FILE, BINARY_FILE)) {
call erract (EA_WARN)
next
}
call stsave (stp, fd)
call close (fd)
case RESTORE:
# In this case 'key' contains the savefile filename.
iferr (fd = open (key, READ_ONLY, BINARY_FILE)) {
call erract (EA_WARN)
next
}
call stclose (stp)
stp = strestore (fd)
call close (fd)
case INFO:
if (key[1] == 'v')
call stinfo (stp, STDOUT, YES)
else
call stinfo (stp, STDOUT, NO)
case SCANFILE:
call scanfile (key, stp)
case HELP:
call zz_help (STDOUT)
default:
call eprintf ("syntax error\n")
}
if (timeit)
call sys_ptime (STDOUT, key, svtime)
}
call stclose (stp)
end
# SCANFILE -- Scan a text file, breaking the input up into a series of tokens.
# Place each new integer token in the symbol table. If the token is already
# present in the table, increment its count field.
procedure scanfile (fname, stp)
char fname[ARB] # file to be scanned
pointer stp # symtab descriptor
char lbuf[SZ_LINE], tokbuf[SZ_FNAME]
int fd, ip, token
pointer sym
int open(), getline(), ctotok()
pointer stenter(), stfind()
errchk open, stenter
begin
fd = open (fname, READ_ONLY, TEXT_FILE)
while (getline (fd, lbuf) != EOF) {
ip = 1
repeat {
token = ctotok (lbuf, ip, tokbuf, SZ_FNAME)
if (token == TOK_IDENTIFIER) {
sym = stfind (stp, tokbuf)
if (sym == NULL) {
sym = stenter (stp, tokbuf, 1)
Memi[sym] = 1
} else
Memi[sym] = Memi[sym] + 1
}
} until (token == TOK_NEWLINE || token == TOK_EOS)
}
call close (fd)
end
# PSYM -- Print the name and value of a symbol in the form "key = value".
# There are two types of values, string and count. A string operand is
# flagged as negative.
procedure psym (stp, sym)
pointer stp # symtab descriptor
pointer sym # pointer to symbol
int val
pointer vp
pointer strefsbuf(), stname()
begin
val = Memi[sym]
if (val < 0) {
vp = strefsbuf (stp, -val)
call printf ("%s = %s\n")
call pargstr (Memc[stname(stp,sym)])
call pargstr (Memc[vp])
} else {
call printf ("%s = %d\n")
call pargstr (Memc[stname(stp,sym)])
call pargi (val)
}
end
# ZZ_HELP -- Print command dictionary for interpreter.
procedure zz_help (fd)
int fd
begin
call fprintf (fd, ".mark mark top of symbol table\n")
call fprintf (fd, ".free free back to last mark\n")
call fprintf (fd, ".list list all symbols in table\n")
call fprintf (fd, ".squeeze minimize storage\n")
call fprintf (fd, ".save <fname> save table in a file\n")
call fprintf (fd, ".restore <fname> restore table from a file\n")
call fprintf (fd, ".info print info on table\n")
call fprintf (fd, ".scanfile <fname> enter symbols from file\n")
call fprintf (fd, "keyword = value enter a symbol in table\n")
call fprintf (fd, "= keyword print value of named symbol\n")
call fprintf (fd, "bye exit\n")
call flush (fd)
end
|