1 |
– |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines for loading and checking variables from file. |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
|
#include <stdio.h> |
11 |
+ |
#include <stdlib.h> |
12 |
|
#include <ctype.h> |
13 |
|
#include "vars.h" |
14 |
|
|
15 |
|
#define NOCHAR 127 /* constant for character to delete */ |
16 |
|
|
17 |
< |
#ifndef malloc |
18 |
< |
extern char *malloc(), *realloc(); |
19 |
< |
#endif |
17 |
> |
extern char *fgetline(); |
18 |
|
|
19 |
|
|
20 |
+ |
void |
21 |
|
loadvars(rfname) /* load variables into vv from file */ |
22 |
|
char *rfname; |
23 |
|
{ |
29 |
|
fp = stdin; |
30 |
|
else if ((fp = fopen(rfname, "r")) == NULL) { |
31 |
|
perror(rfname); |
32 |
< |
exit(1); |
32 |
> |
quit(1); |
33 |
|
} |
34 |
|
while (fgetline(buf, sizeof(buf), fp) != NULL) { |
35 |
|
for (cp = buf; *cp; cp++) { |
45 |
|
} |
46 |
|
break; |
47 |
|
} |
48 |
< |
setvariable(buf); |
48 |
> |
if (setvariable(buf, matchvar) < 0) { |
49 |
> |
fprintf(stderr, "%s: unknown variable: %s\n", |
50 |
> |
rfname, buf); |
51 |
> |
quit(1); |
52 |
> |
} |
53 |
|
} |
54 |
|
fclose(fp); |
55 |
|
} |
56 |
|
|
57 |
|
|
58 |
< |
setvariable(ass) /* assign variable according to string */ |
58 |
> |
int |
59 |
> |
setvariable(ass, mv) /* assign variable according to string */ |
60 |
|
register char *ass; |
61 |
+ |
VARIABLE *(*mv)(); |
62 |
|
{ |
63 |
|
char varname[32]; |
64 |
|
int n; |
74 |
|
*cp++ = *ass++; |
75 |
|
*cp = '\0'; |
76 |
|
if (!varname[0]) |
77 |
< |
return; /* no variable name! */ |
77 |
> |
return(0); /* no variable name! */ |
78 |
|
/* trim value */ |
79 |
|
while (isspace(*ass) || *ass == '=') |
80 |
|
ass++; |
81 |
|
for (n = strlen(ass); n > 0; n--) |
82 |
|
if (!isspace(ass[n-1])) |
83 |
|
break; |
84 |
< |
if (!n && !nowarn) { |
85 |
< |
fprintf(stderr, "%s: warning - missing value for variable '%s'\n", |
81 |
< |
progname, varname); |
82 |
< |
return; |
83 |
< |
} |
84 |
> |
if (!n) |
85 |
> |
return(0); /* no assignment */ |
86 |
|
/* match variable from list */ |
87 |
< |
vp = matchvar(varname); |
88 |
< |
if (vp == NULL) { |
89 |
< |
fprintf(stderr, "%s: unknown variable '%s'\n", |
88 |
< |
progname, varname); |
89 |
< |
exit(1); |
90 |
< |
} |
87 |
> |
vp = (*mv)(varname); |
88 |
> |
if (vp == NULL) |
89 |
> |
return(-1); |
90 |
|
/* assign new value */ |
91 |
|
if (i = vp->nass) { |
92 |
|
cp = vp->value; |
94 |
|
while (*cp++) |
95 |
|
; |
96 |
|
i = cp - vp->value; |
97 |
< |
vp->value = realloc(vp->value, i+n+1); |
97 |
> |
vp->value = (char *)realloc((void *)vp->value, i+n+1); |
98 |
|
} else |
99 |
< |
vp->value = malloc(n+1); |
99 |
> |
vp->value = (char *)malloc(n+1); |
100 |
|
if (vp->value == NULL) { |
101 |
|
perror(progname); |
102 |
< |
exit(1); |
102 |
> |
quit(1); |
103 |
|
} |
104 |
|
cp = vp->value+i; /* copy value, squeezing spaces */ |
105 |
|
*cp = *ass; |
113 |
|
} |
114 |
|
if (isspace(*cp)) /* remove trailing space */ |
115 |
|
*cp = '\0'; |
116 |
< |
vp->nass++; |
116 |
> |
return(++vp->nass); |
117 |
|
} |
118 |
|
|
119 |
|
|
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
+ |
void |
152 |
|
checkvalues() /* check assignments */ |
153 |
|
{ |
154 |
|
register int i; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
+ |
void |
163 |
|
onevalue(vp) /* only one assignment for this variable */ |
164 |
|
register VARIABLE *vp; |
165 |
|
{ |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
+ |
void |
179 |
|
catvalues(vp) /* concatenate variable values */ |
180 |
|
register VARIABLE *vp; |
181 |
|
{ |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
+ |
void |
208 |
|
boolvalue(vp) /* check boolean for legal values */ |
209 |
|
register VARIABLE *vp; |
210 |
|
{ |
220 |
|
} |
221 |
|
fprintf(stderr, "%s: illegal value for boolean variable '%s'\n", |
222 |
|
progname, vp->name); |
223 |
< |
exit(1); |
223 |
> |
quit(1); |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
+ |
void |
228 |
|
qualvalue(vp) /* check qualitative var. for legal values */ |
229 |
|
register VARIABLE *vp; |
230 |
|
{ |
243 |
|
} |
244 |
|
fprintf(stderr, "%s: illegal value for qualitative variable '%s'\n", |
245 |
|
progname, vp->name); |
246 |
< |
exit(1); |
246 |
> |
quit(1); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
+ |
void |
251 |
|
intvalue(vp) /* check integer variable for legal values */ |
252 |
|
register VARIABLE *vp; |
253 |
|
{ |
256 |
|
if (isint(vp->value)) return; |
257 |
|
fprintf(stderr, "%s: illegal value for integer variable '%s'\n", |
258 |
|
progname, vp->name); |
259 |
< |
exit(1); |
259 |
> |
quit(1); |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
+ |
void |
264 |
|
fltvalue(vp) /* check float variable for legal values */ |
265 |
|
register VARIABLE *vp; |
266 |
|
{ |
269 |
|
if (isflt(vp->value)) return; |
270 |
|
fprintf(stderr, "%s: illegal value for real variable '%s'\n", |
271 |
|
progname, vp->name); |
272 |
< |
exit(1); |
272 |
> |
quit(1); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
+ |
void |
277 |
|
printvars(fp) /* print variable values */ |
278 |
|
register FILE *fp; |
279 |
|
{ |
284 |
|
for (j = 0; j < vdef(i); j++) { /* print each assignment */ |
285 |
|
fputs(vnam(i), fp); |
286 |
|
fputs("= ", fp); |
287 |
< |
k = clipline = ( vv[i].fixval == catvalues ? 64 : 320 ) |
287 |
> |
k = clipline = ( vv[i].fixval == catvalues ? 64 : 236 ) |
288 |
|
- strlen(vnam(i)) ; |
289 |
|
cp = nvalue(i, j); |
290 |
|
while (*cp) { |
291 |
|
putc(*cp++, fp); |
292 |
|
if (--k <= 0) { /* line too long */ |
293 |
|
while (*cp && !isspace(*cp)) |
294 |
< |
putc(*cp++, fp); /* finish this word */ |
294 |
> |
fputc(*cp++, fp); /* finish this word */ |
295 |
|
if (*cp) { /* start new line */ |
296 |
< |
putc('\n', fp); |
297 |
< |
fputs(vnam(i), fp); |
298 |
< |
putc('=', fp); |
296 |
> |
if (vv[i].fixval == catvalues) { |
297 |
> |
fputc('\n', fp); |
298 |
> |
fputs(vnam(i), fp); |
299 |
> |
fputc('=', fp); |
300 |
> |
} else |
301 |
> |
fputs(" \\\n", fp); |
302 |
|
k = clipline; |
303 |
|
} |
304 |
|
} |
305 |
|
} |
306 |
< |
putc('\n', fp); |
306 |
> |
fputc('\n', fp); |
307 |
|
} |
308 |
|
fflush(fp); |
309 |
|
} |