5
#define MAX_NL_CACHE 3 /* maximum number of namelist hash tables to cache */
6
#define MAXDIM 20 /* maximum number of subscripts */
14
typedef struct dimen dimen;
17
struct hashentry *next;
21
typedef struct hashentry hashentry;
29
typedef struct hashtab hashtab;
31
static hashtab *nl_cache;
33
static hashentry **zot;
35
extern ftnlen f__typesize[];
38
extern int f__lcount, nml_read;
39
extern int t_getc(Void);
42
extern char *malloc(), *memset();
46
un_getc(x,f__cf) int x; FILE *f__cf;
47
{ return ungetc(x,f__cf); }
49
#define un_getc ungetc
65
un_getc(int x, FILE *f__cf)
66
{ return ungetc(x,f__cf); }
68
#define un_getc ungetc
69
extern int ungetc(int, FILE*); /* for systems with a buggy stdio.h */
75
hash(ht, s) hashtab *ht; register char *s;
77
hash(hashtab *ht, register char *s)
81
register hashentry *h;
84
for(x = 0; c = *s++; x = x & 0x4000 ? ((x << 1) & 0x7fff) + 1 : x << 1)
86
for(h = *(zot = ht->tab + x % ht->htsize); h; h = h->next)
87
if (!strcmp(s0, h->name))
94
mk_hashtab(nl) Namelist *nl;
96
mk_hashtab(Namelist *nl)
101
Vardesc *v, **vd, **vde;
104
hashtab **x, **x0, *y;
105
for(x = &nl_cache; y = *x; x0 = x, x = &y->next)
108
if (n_nlcache >= MAX_NL_CACHE) {
109
/* discard least recently used namelist hash table */
111
free((char *)y->next);
120
for(nht = 1; nht < nv; nht <<= 1);
123
ht = (hashtab *)malloc(sizeof(hashtab) + (nht-1)*sizeof(hashentry *)
124
+ nv*sizeof(hashentry));
127
he = (hashentry *)&ht->tab[nht];
132
memset((char *)ht->tab, 0, nht*sizeof(hashentry *));
137
if (!hash(ht, v->name)) {
148
static char Alpha[256], Alphanum[256];
157
for(s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; c = *s++; )
160
= Alpha[c + 'a' - 'A']
161
= Alphanum[c + 'a' - 'A']
163
for(s = "0123456789_"; c = *s++; )
167
#define GETC(x) (x=(*l_getc)())
168
#define Ungetc(x,y) (*l_ungetc)(x,y)
172
getname(s, slen) register char *s; int slen;
174
getname(register char *s, int slen)
177
register char *se = s + slen - 1;
181
if (!(*s++ = Alpha[ch & 0xff])) {
184
errfl(f__elist->cierr, ch, "namelist read");
186
while(*s = Alphanum[GETC(ch) & 0xff])
190
err(f__elist->cierr, EOF, "namelist read");
198
getnum(chp, val) int *chp; ftnlen *val;
200
getnum(int *chp, ftnlen *val)
203
register int ch, sign;
206
while(GETC(ch) <= ' ' && ch >= 0);
219
while(GETC(ch) >= '0' && ch <= '9')
221
while(ch <= ' ' && ch >= 0)
225
*val = sign ? -x : x;
232
getdimen(chp, d, delta, extent, x1)
233
int *chp; dimen *d; ftnlen delta, extent, *x1;
235
getdimen(int *chp, dimen *d, ftnlen delta, ftnlen extent, ftnlen *x1)
241
if (k = getnum(chp, x1))
245
if (k = getnum(chp, &x2))
249
if (k = getnum(chp, &x3))
256
if (x2 < 0 || x2 >= extent)
268
#ifndef No_Namelist_Questions
271
print_ne(a) cilist *a;
276
flag intext = f__external;
277
int rpsave = f__recpos;
278
FILE *cfsave = f__cf;
279
unit *usave = f__curunit;
285
f__external = intext;
294
static char where0[] = "namelist read start ";
303
int ch, got1, k, n, nd, quote, readall;
305
static char where[] = "namelist read";
309
dimen *dn, *dn0, *dn1;
310
ftnlen *dims, *dims1;
311
ftnlen b, b0, b1, ex, no, nomax, size, span;
312
ftnint no1, no2, type;
315
dimen dimens[MAXDIM], substr;
323
for(;;) switch(GETC(ch)) {
326
err(a->ciend,(EOF),where0);
330
#ifndef No_Namelist_Questions
336
if (ch <= ' ' && ch >= 0)
338
#ifndef No_Namelist_Comments
339
while(GETC(ch) != '\n')
343
errfl(a->cierr, 115, where0);
347
if (ch = getname(buf,sizeof(buf)))
349
nl = (Namelist *)a->cifmt;
350
if (strcmp(buf, nl->name))
351
#ifdef No_Bad_Namelist_Skip
352
errfl(a->cierr, 118, where0);
356
"Skipping namelist \"%s\": seeking namelist \"%s\".\n",
359
for(;;) switch(GETC(ch)) {
361
err(a->ciend, EOF, where0);
374
while(GETC(ch) != quote)
376
err(a->ciend, EOF, where0);
377
if (GETC(ch) == quote)
387
errfl(f__elist->cierr, 113, where0);
389
for(;;) switch(GETC(ch)) {
393
err(a->ciend, EOF, where0);
399
if (ch <= ' ' && ch >= 0 || ch == ',')
402
if (ch = getname(buf,sizeof(buf)))
409
errfl(a->cierr, 119, where);
410
while(GETC(ch) <= ' ' && ch >= 0);
418
size = f__typesize[type];
421
if (ch == '(' /*)*/ ) {
423
if (!(dims = v->dims)) {
425
errfl(a->cierr, 122, where);
426
if (k = getdimen(&ch, dn, (ftnlen)size,
428
errfl(a->cierr, k, where);
430
errfl(a->cierr, 115, where);
432
if (--b < 0 || b + b1 > size)
436
while(GETC(ch) <= ' ' && ch >= 0);
440
nomax = span = dims[1];
441
ivae = iva + size*nomax;
443
if (k = getdimen(&ch, dn, size, nomax, &b))
444
errfl(a->cierr, k, where);
449
for(n = 1; n++ < nd; dims++) {
451
errfl(a->cierr, 115, where);
454
if (k = getdimen(&ch, dn1, dn->delta**dims,
456
errfl(a->cierr, k, where);
463
errfl(a->cierr, 115, where);
464
readall = 1 - colonseen;
466
if (b < 0 || b >= nomax)
467
errfl(a->cierr, 125, where);
470
while(GETC(ch) <= ' ' && ch >= 0);
473
if (type == TYCHAR && ch == '(' /*)*/) {
474
if (k = getdimen(&ch, &substr, size, size, &b))
475
errfl(a->cierr, k, where);
477
errfl(a->cierr, 115, where);
479
if (--b < 0 || b + b1 > size)
484
while(GETC(ch) <= ' ' && ch >= 0);
490
for(; dn0 < dn; dn0++) {
491
if (dn0->extent != *dims++ || dn0->stride != 1)
495
if (dn0 == dimens && dimens[0].stride == 1) {
496
no1 = dimens[0].extent;
501
for(dn1 = dn0; dn1 <= dn; dn1++)
502
ex += (dn1->extent-1)
503
* (dn1->delta *= dn1->stride);
504
for(dn1 = dn; dn1 > dn0; dn1--) {
505
ex -= (dn1->extent - 1) * dn1->delta;
509
else if (dims = v->dims) {
511
ivae = iva + no*size;
517
errfl(a->cierr, 115, where);
522
if (iva >= ivae || iva < 0) {
526
else if (iva + no1*size > ivae)
527
no1 = (ivae - iva)/size;
529
if (k = l_read(&no1, vaddr + iva, size, type))
536
no2 = (ivae - iva)/size;
539
if (k = l_read(&no2, vaddr + iva,
542
iva += no2 * dn0->delta;
560
if (ch == '/' || ch == '$' || ch == '&') {
565
while(ch <= ' ' && ch >= 0)
568
if (!Alpha[ch & 0xff] && ch >= 0)
569
errfl(a->cierr, 125, where);
573
if (readall && !Alpha[ch & 0xff])
575
if ((no -= no1) <= 0)
577
for(dn1 = dn0; dn1 <= dn; dn1++) {
578
if (++dn1->curval < dn1->extent) {
603
if(f__curunit->uwrt && f__nowreading(f__curunit))
604
err(a->cierr,errno,where0);