Debian Codegraph @

utils/pmx/pmx-src/libf2c/rsne.c

raw
1 #include "f2c.h"
2 #include "fio.h"
3 #include "lio.h"
4
5 #define MAX_NL_CACHE 3 /* maximum number of namelist hash tables to cache */
6 #define MAXDIM 20 /* maximum number of subscripts */
7
8 struct dimen {
9 ftnlen extent;
10 ftnlen curval;
11 ftnlen delta;
12 ftnlen stride;
13 };
14 typedef struct dimen dimen;
15
16 struct hashentry {
17 struct hashentry *next;
18 char *name;
19 Vardesc *vd;
20 };
21 typedef struct hashentry hashentry;
22
23 struct hashtab {
24 struct hashtab *next;
25 Namelist *nl;
26 int htsize;
27 hashentry *tab[1];
28 };
29 typedef struct hashtab hashtab;
30
31 static hashtab *nl_cache;
32 static int n_nlcache;
33 static hashentry **zot;
34 static int colonseen;
35 extern ftnlen f__typesize[];
36
37 extern flag f__lquit;
38 extern int f__lcount, nml_read;
39 extern int t_getc(Void);
40
41 #ifdef KR_headers
42 extern char *malloc(), *memset();
43
44 #ifdef ungetc
45 static int
46 un_getc(x,f__cf) int x; FILE *f__cf;
47 { return ungetc(x,f__cf); }
48 #else
49 #define un_getc ungetc
50 extern int ungetc();
51 #endif
52
53 #else
54 #undef abs
55 #undef min
56 #undef max
57 #include "stdlib.h"
58 #include "string.h"
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62
63 #ifdef ungetc
64 static int
65 un_getc(int x, FILE *f__cf)
66 { return ungetc(x,f__cf); }
67 #else
68 #define un_getc ungetc
69 #endif
70 #endif
71
72 static Vardesc *
73 #ifdef KR_headers
74 hash(ht, s) hashtab *ht; register char *s;
75 #else
76 hash(hashtab *ht, register char *s)
77 #endif
78 {
79 register int c, x;
80 register hashentry *h;
81 char *s0 = s;
82
83 for(x = 0; c = *s++; x = x & 0x4000 ? ((x << 1) & 0x7fff) + 1 : x << 1)
84 x += c;
85 for(h = *(zot = ht->tab + x % ht->htsize); h; h = h->next)
86 if (!strcmp(s0, h->name))
87 return h->vd;
88 return 0;
89 }
90
91 hashtab *
92 #ifdef KR_headers
93 mk_hashtab(nl) Namelist *nl;
94 #else
95 mk_hashtab(Namelist *nl)
96 #endif
97 {
98 int nht, nv;
99 hashtab *ht;
100 Vardesc *v, **vd, **vde;
101 hashentry *he;
102
103 hashtab **x, **x0, *y;
104 for(x = &nl_cache; y = *x; x0 = x, x = &y->next)
105 if (nl == y->nl)
106 return y;
107 if (n_nlcache >= MAX_NL_CACHE) {
108 /* discard least recently used namelist hash table */
109 y = *x0;
110 free((char *)y->next);
111 y->next = 0;
112 }
113 else
114 n_nlcache++;
115 nv = nl->nvars;
116 if (nv >= 0x4000)
117 nht = 0x7fff;
118 else {
119 for(nht = 1; nht < nv; nht <<= 1);
120 nht += nht - 1;
121 }
122 ht = (hashtab *)malloc(sizeof(hashtab) + (nht-1)*sizeof(hashentry *)
123 + nv*sizeof(hashentry));
124 if (!ht)
125 return 0;
126 he = (hashentry *)&ht->tab[nht];
127 ht->nl = nl;
128 ht->htsize = nht;
129 ht->next = nl_cache;
130 nl_cache = ht;
131 memset((char *)ht->tab, 0, nht*sizeof(hashentry *));
132 vd = nl->vars;
133 vde = vd + nv;
134 while(vd < vde) {
135 v = *vd++;
136 if (!hash(ht, v->name)) {
137 he->next = *zot;
138 *zot = he;
139 he->name = v->name;
140 he->vd = v;
141 he++;
142 }
143 }
144 return ht;
145 }
146
147 static char Alpha[256], Alphanum[256];
148
149 static VOID
150 nl_init(Void) {
151 register char *s;
152 register int c;
153
154 if(!f__init)
155 f_init();
156 for(s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; c = *s++; )
157 Alpha[c]
158 = Alphanum[c]
159 = Alpha[c + 'a' - 'A']
160 = Alphanum[c + 'a' - 'A']
161 = c;
162 for(s = "0123456789_"; c = *s++; )
163 Alphanum[c] = c;
164 }
165
166 #define GETC(x) (x=(*l_getc)())
167 #define Ungetc(x,y) (*l_ungetc)(x,y)
168
169 static int
170 #ifdef KR_headers
171 getname(s, slen) register char *s; int slen;
172 #else
173 getname(register char *s, int slen)
174 #endif
175 {
176 register char *se = s + slen - 1;
177 register int ch;
178
179 GETC(ch);
180 if (!(*s++ = Alpha[ch & 0xff])) {
181 if (ch != EOF)
182 ch = 115;
183 errfl(f__elist->cierr, ch, "namelist read");
184 }
185 while(*s = Alphanum[GETC(ch) & 0xff])
186 if (s < se)
187 s++;
188 if (ch == EOF)
189 err(f__elist->cierr, EOF, "namelist read");
190 if (ch > ' ')
191 Ungetc(ch,f__cf);
192 return *s = 0;
193 }
194
195 static int
196 #ifdef KR_headers
197 getnum(chp, val) int *chp; ftnlen *val;
198 #else
199 getnum(int *chp, ftnlen *val)
200 #endif
201 {
202 register int ch, sign;
203 register ftnlen x;
204
205 while(GETC(ch) <= ' ' && ch >= 0);
206 if (ch == '-') {
207 sign = 1;
208 GETC(ch);
209 }
210 else {
211 sign = 0;
212 if (ch == '+')
213 GETC(ch);
214 }
215 x = ch - '0';
216 if (x < 0 || x > 9)
217 return 115;
218 while(GETC(ch) >= '0' && ch <= '9')
219 x = 10*x + ch - '0';
220 while(ch <= ' ' && ch >= 0)
221 GETC(ch);
222 if (ch == EOF)
223 return EOF;
224 *val = sign ? -x : x;
225 *chp = ch;
226 return 0;
227 }
228
229 static int
230 #ifdef KR_headers
231 getdimen(chp, d, delta, extent, x1)
232 int *chp; dimen *d; ftnlen delta, extent, *x1;
233 #else
234 getdimen(int *chp, dimen *d, ftnlen delta, ftnlen extent, ftnlen *x1)
235 #endif
236 {
237 register int k;
238 ftnlen x2, x3;
239
240 if (k = getnum(chp, x1))
241 return k;
242 x3 = 1;
243 if (*chp == ':') {
244 if (k = getnum(chp, &x2))
245 return k;
246 x2 -= *x1;
247 if (*chp == ':') {
248 if (k = getnum(chp, &x3))
249 return k;
250 if (!x3)
251 return 123;
252 x2 /= x3;
253 colonseen = 1;
254 }
255 if (x2 < 0 || x2 >= extent)
256 return 123;
257 d->extent = x2 + 1;
258 }
259 else
260 d->extent = 1;
261 d->curval = 0;
262 d->delta = delta;
263 d->stride = x3;
264 return 0;
265 }
266
267 #ifndef No_Namelist_Questions
268 static Void
269 #ifdef KR_headers
270 print_ne(a) cilist *a;
271 #else
272 print_ne(cilist *a)
273 #endif
274 {
275 flag intext = f__external;
276 int rpsave = f__recpos;
277 FILE *cfsave = f__cf;
278 unit *usave = f__curunit;
279 cilist t;
280 t = *a;
281 t.ciunit = 6;
282 s_wsne(&t);
283 fflush(f__cf);
284 f__external = intext;
285 f__reading = 1;
286 f__recpos = rpsave;
287 f__cf = cfsave;
288 f__curunit = usave;
289 f__elist = a;
290 }
291 #endif
292
293 static char where0[] = "namelist read start ";
294
295 int
296 #ifdef KR_headers
297 x_rsne(a) cilist *a;
298 #else
299 x_rsne(cilist *a)
300 #endif
301 {
302 int ch, got1, k, n, nd, quote, readall;
303 Namelist *nl;
304 static char where[] = "namelist read";
305 char buf[64];
306 hashtab *ht;
307 Vardesc *v;
308 dimen *dn, *dn0, *dn1;
309 ftnlen *dims, *dims1;
310 ftnlen b, b0, b1, ex, no, nomax, size, span;
311 ftnint no1, no2, type;
312 char *vaddr;
313 long iva, ivae;
314 dimen dimens[MAXDIM], substr;
315
316 if (!Alpha['a'])
317 nl_init();
318 f__reading=1;
319 f__formatted=1;
320 got1 = 0;
321 top:
322 for(;;) switch(GETC(ch)) {
323 case EOF:
324 eof:
325 err(a->ciend,(EOF),where0);
326 case '&':
327 case '$':
328 goto have_amp;
329 #ifndef No_Namelist_Questions
330 case '?':
331 print_ne(a);
332 continue;
333 #endif
334 default:
335 if (ch <= ' ' && ch >= 0)
336 continue;
337 #ifndef No_Namelist_Comments
338 while(GETC(ch) != '\n')
339 if (ch == EOF)
340 goto eof;
341 #else
342 errfl(a->cierr, 115, where0);
343 #endif
344 }
345 have_amp:
346 if (ch = getname(buf,sizeof(buf)))
347 return ch;
348 nl = (Namelist *)a->cifmt;
349 if (strcmp(buf, nl->name))
350 #ifdef No_Bad_Namelist_Skip
351 errfl(a->cierr, 118, where0);
352 #else
353 {
354 fprintf(stderr,
355 "Skipping namelist \"%s\": seeking namelist \"%s\".\n",
356 buf, nl->name);
357 fflush(stderr);
358 for(;;) switch(GETC(ch)) {
359 case EOF:
360 err(a->ciend, EOF, where0);
361 case '/':
362 case '&':
363 case '$':
364 if (f__external)
365 e_rsle();
366 else
367 z_rnew();
368 goto top;
369 case '"':
370 case '\'':
371 quote = ch;
372 more_quoted:
373 while(GETC(ch) != quote)
374 if (ch == EOF)
375 err(a->ciend, EOF, where0);
376 if (GETC(ch) == quote)
377 goto more_quoted;
378 Ungetc(ch,f__cf);
379 default:
380 continue;
381 }
382 }
383 #endif
384 ht = mk_hashtab(nl);
385 if (!ht)
386 errfl(f__elist->cierr, 113, where0);
387 for(;;) {
388 for(;;) switch(GETC(ch)) {
389 case EOF:
390 if (got1)
391 return 0;
392 err(a->ciend, EOF, where0);
393 case '/':
394 case '$':
395 case '&':
396 return 0;
397 default:
398 if (ch <= ' ' && ch >= 0 || ch == ',')
399 continue;
400 Ungetc(ch,f__cf);
401 if (ch = getname(buf,sizeof(buf)))
402 return ch;
403 goto havename;
404 }
405 havename:
406 v = hash(ht,buf);
407 if (!v)
408 errfl(a->cierr, 119, where);
409 while(GETC(ch) <= ' ' && ch >= 0);
410 vaddr = v->addr;
411 type = v->type;
412 if (type < 0) {
413 size = -type;
414 type = TYCHAR;
415 }
416 else
417 size = f__typesize[type];
418 ivae = size;
419 iva = readall = 0;
420 if (ch == '(' /*)*/ ) {
421 dn = dimens;
422 if (!(dims = v->dims)) {
423 if (type != TYCHAR)
424 errfl(a->cierr, 122, where);
425 if (k = getdimen(&ch, dn, (ftnlen)size,
426 (ftnlen)size, &b))
427 errfl(a->cierr, k, where);
428 if (ch != ')')
429 errfl(a->cierr, 115, where);
430 b1 = dn->extent;
431 if (--b < 0 || b + b1 > size)
432 return 124;
433 iva += b;
434 size = b1;
435 while(GETC(ch) <= ' ' && ch >= 0);
436 goto scalar;
437 }
438 nd = (int)dims[0];
439 nomax = span = dims[1];
440 ivae = iva + size*nomax;
441 colonseen = 0;
442 if (k = getdimen(&ch, dn, size, nomax, &b))
443 errfl(a->cierr, k, where);
444 no = dn->extent;
445 b0 = dims[2];
446 dims1 = dims += 3;
447 ex = 1;
448 for(n = 1; n++ < nd; dims++) {
449 if (ch != ',')
450 errfl(a->cierr, 115, where);
451 dn1 = dn + 1;
452 span /= *dims;
453 if (k = getdimen(&ch, dn1, dn->delta**dims,
454 span, &b1))
455 errfl(a->cierr, k, where);
456 ex *= *dims;
457 b += b1*ex;
458 no *= dn1->extent;
459 dn = dn1;
460 }
461 if (ch != ')')
462 errfl(a->cierr, 115, where);
463 readall = 1 - colonseen;
464 b -= b0;
465 if (b < 0 || b >= nomax)
466 errfl(a->cierr, 125, where);
467 iva += size * b;
468 dims = dims1;
469 while(GETC(ch) <= ' ' && ch >= 0);
470 no1 = 1;
471 dn0 = dimens;
472 if (type == TYCHAR && ch == '(' /*)*/) {
473 if (k = getdimen(&ch, &substr, size, size, &b))
474 errfl(a->cierr, k, where);
475 if (ch != ')')
476 errfl(a->cierr, 115, where);
477 b1 = substr.extent;
478 if (--b < 0 || b + b1 > size)
479 return 124;
480 iva += b;
481 b0 = size;
482 size = b1;
483 while(GETC(ch) <= ' ' && ch >= 0);
484 if (b1 < b0)
485 goto delta_adj;
486 }
487 if (readall)
488 goto delta_adj;
489 for(; dn0 < dn; dn0++) {
490 if (dn0->extent != *dims++ || dn0->stride != 1)
491 break;
492 no1 *= dn0->extent;
493 }
494 if (dn0 == dimens && dimens[0].stride == 1) {
495 no1 = dimens[0].extent;
496 dn0++;
497 }
498 delta_adj:
499 ex = 0;
500 for(dn1 = dn0; dn1 <= dn; dn1++)
501 ex += (dn1->extent-1)
502 * (dn1->delta *= dn1->stride);
503 for(dn1 = dn; dn1 > dn0; dn1--) {
504 ex -= (dn1->extent - 1) * dn1->delta;
505 dn1->delta -= ex;
506 }
507 }
508 else if (dims = v->dims) {
509 no = no1 = dims[1];
510 ivae = iva + no*size;
511 }
512 else
513 scalar:
514 no = no1 = 1;
515 if (ch != '=')
516 errfl(a->cierr, 115, where);
517 got1 = nml_read = 1;
518 f__lcount = 0;
519 readloop:
520 for(;;) {
521 if (iva >= ivae || iva < 0) {
522 f__lquit = 1;
523 goto mustend;
524 }
525 else if (iva + no1*size > ivae)
526 no1 = (ivae - iva)/size;
527 f__lquit = 0;
528 if (k = l_read(&no1, vaddr + iva, size, type))
529 return k;
530 if (f__lquit == 1)
531 return 0;
532 if (readall) {
533 iva += dn0->delta;
534 if (f__lcount > 0) {
535 no2 = (ivae - iva)/size;
536 if (no2 > f__lcount)
537 no2 = f__lcount;
538 if (k = l_read(&no2, vaddr + iva,
539 size, type))
540 return k;
541 iva += no2 * dn0->delta;
542 }
543 }
544 mustend:
545 GETC(ch);
546 if (readall)
547 if (iva >= ivae)
548 readall = 0;
549 else for(;;) {
550 switch(ch) {
551 case ' ':
552 case '\t':
553 case '\n':
554 GETC(ch);
555 continue;
556 }
557 break;
558 }
559 if (ch == '/' || ch == '$' || ch == '&') {
560 f__lquit = 1;
561 return 0;
562 }
563 else if (f__lquit) {
564 while(ch <= ' ' && ch >= 0)
565 GETC(ch);
566 Ungetc(ch,f__cf);
567 if (!Alpha[ch & 0xff] && ch >= 0)
568 errfl(a->cierr, 125, where);
569 break;
570 }
571 Ungetc(ch,f__cf);
572 if (readall && !Alpha[ch & 0xff])
573 goto readloop;
574 if ((no -= no1) <= 0)
575 break;
576 for(dn1 = dn0; dn1 <= dn; dn1++) {
577 if (++dn1->curval < dn1->extent) {
578 iva += dn1->delta;
579 goto readloop;
580 }
581 dn1->curval = 0;
582 }
583 break;
584 }
585 }
586 }
587
588 integer
589 #ifdef KR_headers
590 s_rsne(a) cilist *a;
591 #else
592 s_rsne(cilist *a)
593 #endif
594 {
595 extern int l_eof;
596 int n;
597
598 f__external=1;
599 l_eof = 0;
600 if(n = c_le(a))
601 return n;
602 if(f__curunit->uwrt && f__nowreading(f__curunit))
603 err(a->cierr,errno,where0);
604 l_getc = t_getc;
605 l_ungetc = un_getc;
606 f__doend = xrd_SL;
607 n = x_rsne(a);
608 nml_read = 0;
609 if (n)
610 return n;
611 return e_rsle();
612 }
613 #ifdef __cplusplus
614 }
615 #endif