6 /* Compile with -DF8X_NML_ELIDE_QUOTES to permit eliding quotation */
7 /* marks in namelist input a la the Fortran 8X Draft published in */
8 /* the May 1989 issue of Fortran Forum. */
11 extern char *f__fmtbuf;
15 static longint f__llx;
21 extern char *malloc(), *realloc();
22 int (*f__lioproc)(), (*l_getc)(), (*l_ungetc)();
35 int (*f__lioproc)(ftnint*, char*, ftnlen, ftnint), (*l_getc)(void),
36 (*l_ungetc)(int,FILE*);
41 #define isblnk(x) (f__ltab[x+1]&B)
42 #define issep(x) (f__ltab[x+1]&SX)
43 #define isapos(x) (f__ltab[x+1]&AX)
44 #define isexp(x) (f__ltab[x+1]&EX)
45 #define issign(x) (f__ltab[x+1]&SG)
46 #define iswhit(x) (f__ltab[x+1]&WH)
53 char f__ltab[128+1] = { /* offset one for EOF */
55 0,0,AX,0,0,0,0,0,0,WH|B,SX|WH,0,0,0,0,0,
56 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57 SX|B|WH,0,AX,0,0,0,0,AX,0,0,0,SG,SX,SG,0,SX,
58 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
59 0,0,0,0,EX,EX,0,0,0,0,0,0,0,0,0,0,
60 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
61 AX,0,0,0,EX,EX,0,0,0,0,0,0,0,0,0,0,
62 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
68 un_getc(x,f__cf) int x; FILE *f__cf;
70 un_getc(int x, FILE *f__cf)
72 { return ungetc(x,f__cf); }
74 #define un_getc ungetc
78 extern int ungetc(int, FILE*); /* for systems with a buggy stdio.h */
84 if(f__curunit->uend) return(EOF);
85 if((ch=getc(f__cf))!=EOF) return(ch);
87 f__curunit->uend = l_eof = 1;
94 if(f__curunit->uend) return(0);
95 while((ch=t_getc())!='\n')
98 f__curunit->uend = l_eof = 1;
105 int f__lcount,f__ltype,nml_read;
108 #define ERR(x) if(n=(x)) {f__init &= ~2; return(n);}
109 #define GETC(x) (x=(*l_getc)())
110 #define Ungetc(x,y) (*l_ungetc)(x,y)
114 l_R(poststar, reqint) int poststar, reqint;
116 l_R(int poststar, int reqint)
119 char s[FMAX+EXPMAXDIGS+4];
121 register char *sp, *spe, *sp1;
123 int havenum, havestar, se;
142 case '-': *sp++ = ch; sp1++; spe++;
151 if (sp < spe) *sp++ = ch;
155 if (ch == '*' && !poststar) {
156 if (sp == sp1 || exp || *s == '-') {
157 errfl(f__elist->cierr,112,"bad repetition count");
159 poststar = havestar = 1;
165 #ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
167 errfl(f__elist->cierr,115,"invalid integer");
178 { *sp++ = ch; --exp; }
186 if (havenum && isexp(ch)) {
187 #ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
189 errfl(f__elist->cierr,115,"invalid integer");
194 if (ch == '-') se = 1;
199 errfl(f__elist->cierr,112,"exponent field");
203 while(isdigit(GETC(ch))) {
213 (void) Ungetc(ch, f__cf);
219 sprintf(sp+1, "e%ld", exp);
224 if (reqint&2 && (se = sp - sp1 + exp) > 14 && se < 20) {
225 /* Assuming 64-bit longint and 32-bit long. */
231 f__llx = 10*f__llx + (*sp1 - '0');
250 if (havestar && ( ch == ' '
258 errfl(f__elist->cierr,112,"invalid number");
265 rd_count(ch) register int ch;
267 rd_count(register int ch)
270 if (ch < '0' || ch > '9')
272 f__lcount = ch - '0';
273 while(GETC(ch) >= '0' && ch <= '9')
274 f__lcount = 10*f__lcount + ch - '0';
276 return f__lcount <= 0;
283 if(f__lcount>0) return(0);
288 if (nml_read > 1 && (ch < '0' || ch > '9')) {
294 if(!f__cf || !feof(f__cf))
295 errfl(f__elist->cierr,112,"complex format");
297 err(f__elist->cierr,(EOF),"lread");
300 if(!f__cf || !feof(f__cf))
301 errfl(f__elist->cierr,112,"no star");
303 err(f__elist->cierr,(EOF),"lread");
312 while(iswhit(GETC(ch)));
319 errfl(f__elist->cierr,112,"no real part");
321 while(iswhit(GETC(ch)));
323 { (void) Ungetc(ch,f__cf);
324 errfl(f__elist->cierr,112,"no comma");
326 while(iswhit(GETC(ch)));
327 (void) Ungetc(ch,f__cf);
331 errfl(f__elist->cierr,112,"no imaginary part");
332 while(iswhit(GETC(ch)));
333 if(ch!=')') errfl(f__elist->cierr,112,"no )");
343 static char nmLbuf[256], *nmL_next;
344 static int (*nmL_getc_save)(Void);
346 static int (*nmL_ungetc_save)(/* int, FILE* */);
348 static int (*nmL_ungetc_save)(int, FILE*);
355 if (rv = *nmL_next++)
357 l_getc = nmL_getc_save;
358 l_ungetc = nmL_ungetc_save;
364 nmL_ungetc(x, f) int x; FILE *f;
366 nmL_ungetc(int x, FILE *f)
369 f = f; /* banish non-use warning */
370 return *--nmL_next = x;
375 Lfinish(ch, dot, rvp) int ch, dot, *rvp;
377 Lfinish(int ch, int dot, int *rvp)
381 static char what[] = "namelist input";
384 se = nmLbuf + sizeof(nmLbuf) - 1;
386 while(!issep(GETC(ch)) && ch!=EOF) {
389 return *rvp = err__fl(f__elist->cierr,131,what);
395 return *rvp = err__fl(f__elist->cierr,112,what);
398 nmL_getc_save = l_getc;
400 nmL_ungetc_save = l_ungetc;
401 l_ungetc = nmL_ungetc;
402 nmLbuf[1] = *(nmL_next = nmLbuf) = ',';
403 *rvp = f__lcount = 0;
437 if(!f__cf || !feof(f__cf))
438 errfl(f__elist->cierr,112,"no star");
440 err(f__elist->cierr,(EOF),"lread");
452 if (nml_read && Lfinish(ch, sawdot, &rv))
458 if (nml_read && Lfinish(ch, sawdot, &rv))
463 if(isblnk(ch) || issep(ch) || ch==EOF)
464 { (void) Ungetc(ch,f__cf);
472 errfl(f__elist->cierr,112,"logical");
475 while(!issep(GETC(ch)) && ch!=EOF);
476 (void) Ungetc(ch, f__cf);
485 static char rafail[] = "realloc failure";
487 if(f__lcount>0) return(0);
489 if(f__lchar!=NULL) free(f__lchar);
491 p=f__lchar = (char *)malloc((unsigned int)size);
493 errfl(f__elist->cierr,113,"no space");
497 /* allow Fortran 8x-style unquoted string... */
498 /* either find a repetition count or the string */
499 f__lcount = ch - '0';
504 if (f__lcount == 0) {
506 #ifndef F8X_NML_ELIDE_QUOTES
528 #ifndef F8X_NML_ELIDE_QUOTES
531 errfl(f__elist->cierr,112,
532 "undelimited character string");
538 f__lcount = 10*f__lcount + ch - '0';
540 f__lchar = (char *)realloc(f__lchar,
541 (unsigned int)(size += BUFSIZE));
543 errfl(f__elist->cierr,113,rafail);
548 else (void) Ungetc(ch,f__cf);
550 if(GETC(ch)=='\'' || ch=='"') quote=ch;
551 else if(isblnk(ch) || (issep(ch) && ch != '\n') || ch==EOF) {
555 #ifndef F8X_NML_ELIDE_QUOTES
556 else if (nml_read > 1) {
563 /* Fortran 8x-style unquoted string */
581 f__lchar = (char *)realloc(f__lchar,
582 (unsigned int)(size += BUFSIZE));
584 errfl(f__elist->cierr,113,rafail);
591 { while(GETC(ch)!=quote && ch!='\n'
592 && ch!=EOF && ++i<size) *p++ = ch;
596 f__lchar= (char *)realloc(f__lchar,
597 (unsigned int)(size += BUFSIZE));
599 errfl(f__elist->cierr,113,rafail);
603 else if(ch==EOF) return(EOF);
605 { if(*(p-1) != '\\') continue;
608 if(++i<size) *p++ = ch;
611 else if(GETC(ch)==quote)
612 { if(++i<size) *p++ = ch;
616 { (void) Ungetc(ch,f__cf);
628 if(f__init != 1) f_init();
631 f__curunit = &f__units[a->ciunit];
633 if(a->ciunit>=MXUNIT || a->ciunit<0)
634 err(a->cierr,101,"stler");
635 f__scale=f__recpos=0;
637 if(f__curunit->ufd==NULL && fk_open(SEQ,FMT,a->ciunit))
638 err(a->cierr,102,"lio");
639 f__cf=f__curunit->ufd;
640 if(!f__curunit->ufmt) err(a->cierr,103,"lio");
644 l_read(number,ptr,len,type) ftnint *number,type; char *ptr; ftnlen len;
646 l_read(ftnint *number, char *ptr, ftnlen len, ftnint type)
649 #define Ptr ((flex *)ptr)
653 for(i=0;i<*number;i++)
655 if(f__lquit) return(0);
657 err(f__elist->ciend, EOF, "list in");
664 err(f__elist->ciend,(EOF),"list in");
676 (void) Ungetc(ch, f__cf);
687 #ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
715 while (GETC(ch) == ' ' || ch == '\t');
716 if (ch != ',' || f__lcount > 1)
719 if(f__lquit) return(0);
720 if(f__cf && ferror(f__cf)) {
722 errfl(f__elist->cierr,errno,"list in");
724 if(f__ltype==0) goto bump;
729 Ptr->flchar = (char)f__lx;
733 Ptr->flshort = (short)f__lx;
737 Ptr->flint = (ftnint)f__lx;
741 if (!(Ptr->fllongint = f__llx))
742 Ptr->fllongint = f__lx;
757 yy=(doublereal *)ptr;
762 b_char(f__lchar,ptr,len);
766 if(f__lcount>0) f__lcount--;
775 integer s_rsle(a) cilist *a;
777 integer s_rsle(cilist *a)
785 if(n=c_le(a)) return(n);
790 if(f__curunit->uwrt && f__nowreading(f__curunit))
791 err(a->cierr,errno,"read start");
793 err(f__elist->ciend,(EOF),"read start");