Debian Codegraph @

debian/external_deps/fetk/punc/src/vf2c/lread.c

raw
1 #include "f2c.h"
2 #include "fio.h"
3
4 /* Compile with -DF8X_NML_ELIDE_QUOTES to permit eliding quotation */
5 /* marks in namelist input a la the Fortran 8X Draft published in */
6 /* the May 1989 issue of Fortran Forum. */
7
8
9 extern char *f__fmtbuf;
10
11 #ifdef Allow_TYQUAD
12 static longint f__llx;
13 #endif
14
15 #ifdef KR_headers
16 extern double atof();
17 extern char *malloc(), *realloc();
18 int (*f__lioproc)(), (*l_getc)(), (*l_ungetc)();
19 #else
20 #undef abs
21 #undef min
22 #undef max
23 #include "stdlib.h"
24 #endif
25
26 #include "fmt.h"
27 #include "lio.h"
28 #include "ctype.h"
29 #include "fp.h"
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #ifndef KR_headers
35 int (*f__lioproc)(ftnint*, char*, ftnlen, ftnint), (*l_getc)(void),
36 (*l_ungetc)(int,FILE*);
37 #endif
38
39 int l_eof;
40
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)
47 #define SX 1
48 #define B 2
49 #define AX 4
50 #define EX 8
51 #define SG 16
52 #define WH 32
53 char f__ltab[128+1] = { /* offset one for EOF */
54 0,
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
63 };
64
65 #ifdef ungetc
66 static int
67 #ifdef KR_headers
68 un_getc(x,f__cf) int x; FILE *f__cf;
69 #else
70 un_getc(int x, FILE *f__cf)
71 #endif
72 { return ungetc(x,f__cf); }
73 #else
74 #define un_getc ungetc
75 #ifdef KR_headers
76 extern int ungetc();
77 #else
78 extern int ungetc(int, FILE*); /* for systems with a buggy stdio.h */
79 #endif
80 #endif
81
82 int
83 t_getc(Void)
84 { int ch;
85 if(f__curunit->uend) return(EOF);
86 if((ch=getc(f__cf))!=EOF) return(ch);
87 if(feof(f__cf))
88 f__curunit->uend = l_eof = 1;
89 return(EOF);
90 }
91 integer e_rsle(Void)
92 {
93 int ch;
94 if(f__curunit->uend) return(0);
95 while((ch=t_getc())!='\n')
96 if (ch == EOF) {
97 if(feof(f__cf))
98 f__curunit->uend = l_eof = 1;
99 return EOF;
100 }
101 return(0);
102 }
103
104 flag f__lquit;
105 int f__lcount,f__ltype,nml_read;
106 char *f__lchar;
107 double f__lx,f__ly;
108 #define ERR(x) if(n=(x)) return(n)
109 #define GETC(x) (x=(*l_getc)())
110 #define Ungetc(x,y) (*l_ungetc)(x,y)
111
112 static int
113 #ifdef KR_headers
114 l_R(poststar, reqint) int poststar, reqint;
115 #else
116 l_R(int poststar, int reqint)
117 #endif
118 {
119 char s[FMAX+EXPMAXDIGS+4];
120 register int ch;
121 register char *sp, *spe, *sp1;
122 long e, exp;
123 int havenum, havestar, se;
124
125 if (!poststar) {
126 if (f__lcount > 0)
127 return(0);
128 f__lcount = 1;
129 }
130 #ifdef Allow_TYQUAD
131 f__llx = 0;
132 #endif
133 f__ltype = 0;
134 exp = 0;
135 havestar = 0;
136 retry:
137 sp1 = sp = s;
138 spe = sp + FMAX;
139 havenum = 0;
140
141 switch(GETC(ch)) {
142 case '-': *sp++ = ch; sp1++; spe++;
143 case '+':
144 GETC(ch);
145 }
146 while(ch == '0') {
147 ++havenum;
148 GETC(ch);
149 }
150 while(isdigit(ch)) {
151 if (sp < spe) *sp++ = ch;
152 else ++exp;
153 GETC(ch);
154 }
155 if (ch == '*' && !poststar) {
156 if (sp == sp1 || exp || *s == '-') {
157 errfl(f__elist->cierr,112,"bad repetition count");
158 }
159 poststar = havestar = 1;
160 *sp = 0;
161 f__lcount = atoi(s);
162 goto retry;
163 }
164 if (ch == '.') {
165 #ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
166 if (reqint)
167 errfl(f__elist->cierr,115,"invalid integer");
168 #endif
169 GETC(ch);
170 if (sp == sp1)
171 while(ch == '0') {
172 ++havenum;
173 --exp;
174 GETC(ch);
175 }
176 while(isdigit(ch)) {
177 if (sp < spe)
178 { *sp++ = ch; --exp; }
179 GETC(ch);
180 }
181 }
182 havenum += sp - sp1;
183 se = 0;
184 if (issign(ch))
185 goto signonly;
186 if (havenum && isexp(ch)) {
187 #ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
188 if (reqint)
189 errfl(f__elist->cierr,115,"invalid integer");
190 #endif
191 GETC(ch);
192 if (issign(ch)) {
193 signonly:
194 if (ch == '-') se = 1;
195 GETC(ch);
196 }
197 if (!isdigit(ch)) {
198 bad:
199 errfl(f__elist->cierr,112,"exponent field");
200 }
201
202 e = ch - '0';
203 while(isdigit(GETC(ch))) {
204 e = 10*e + ch - '0';
205 if (e > EXPMAX)
206 goto bad;
207 }
208 if (se)
209 exp -= e;
210 else
211 exp += e;
212 }
213 (void) Ungetc(ch, f__cf);
214 if (sp > sp1) {
215 ++havenum;
216 while(*--sp == '0')
217 ++exp;
218 if (exp)
219 sprintf(sp+1, "e%ld", exp);
220 else
221 sp[1] = 0;
222 f__lx = atof(s);
223 #ifdef Allow_TYQUAD
224 if (reqint&2 && (se = sp - sp1 + exp) > 14 && se < 20) {
225 /* Assuming 64-bit longint and 32-bit long. */
226 if (exp < 0)
227 sp += exp;
228 if (sp1 <= sp) {
229 f__llx = *sp1 - '0';
230 while(++sp1 <= sp)
231 f__llx = 10*f__llx + (*sp1 - '0');
232 }
233 while(--exp >= 0)
234 f__llx *= 10;
235 if (*s == '-')
236 f__llx = -f__llx;
237 }
238 #endif
239 }
240 else
241 f__lx = 0.;
242 if (havenum)
243 f__ltype = TYLONG;
244 else
245 switch(ch) {
246 case ',':
247 case '/':
248 break;
249 default:
250 if (havestar && ( ch == ' '
251 ||ch == '\t'
252 ||ch == '\n'))
253 break;
254 if (nml_read > 1) {
255 f__lquit = 2;
256 return 0;
257 }
258 errfl(f__elist->cierr,112,"invalid number");
259 }
260 return 0;
261 }
262
263 static int
264 #ifdef KR_headers
265 rd_count(ch) register int ch;
266 #else
267 rd_count(register int ch)
268 #endif
269 {
270 if (ch < '0' || ch > '9')
271 return 1;
272 f__lcount = ch - '0';
273 while(GETC(ch) >= '0' && ch <= '9')
274 f__lcount = 10*f__lcount + ch - '0';
275 Ungetc(ch,f__cf);
276 return f__lcount <= 0;
277 }
278
279 static int
280 l_C(Void)
281 { int ch, nml_save;
282 double lz;
283 if(f__lcount>0) return(0);
284 f__ltype=0;
285 GETC(ch);
286 if(ch!='(')
287 {
288 if (nml_read > 1 && (ch < '0' || ch > '9')) {
289 Ungetc(ch,f__cf);
290 f__lquit = 2;
291 return 0;
292 }
293 if (rd_count(ch))
294 if(!f__cf || !feof(f__cf))
295 errfl(f__elist->cierr,112,"complex format");
296 else
297 err(f__elist->cierr,(EOF),"lread");
298 if(GETC(ch)!='*')
299 {
300 if(!f__cf || !feof(f__cf))
301 errfl(f__elist->cierr,112,"no star");
302 else
303 err(f__elist->cierr,(EOF),"lread");
304 }
305 if(GETC(ch)!='(')
306 { Ungetc(ch,f__cf);
307 return(0);
308 }
309 }
310 else
311 f__lcount = 1;
312 while(iswhit(GETC(ch)));
313 Ungetc(ch,f__cf);
314 nml_save = nml_read;
315 nml_read = 0;
316 if (ch = l_R(1,0))
317 return ch;
318 if (!f__ltype)
319 errfl(f__elist->cierr,112,"no real part");
320 lz = f__lx;
321 while(iswhit(GETC(ch)));
322 if(ch!=',')
323 { (void) Ungetc(ch,f__cf);
324 errfl(f__elist->cierr,112,"no comma");
325 }
326 while(iswhit(GETC(ch)));
327 (void) Ungetc(ch,f__cf);
328 if (ch = l_R(1,0))
329 return ch;
330 if (!f__ltype)
331 errfl(f__elist->cierr,112,"no imaginary part");
332 while(iswhit(GETC(ch)));
333 if(ch!=')') errfl(f__elist->cierr,112,"no )");
334 f__ly = f__lx;
335 f__lx = lz;
336 #ifdef Allow_TYQUAD
337 f__llx = 0;
338 #endif
339 nml_read = nml_save;
340 return(0);
341 }
342
343 static char nmLbuf[256], *nmL_next;
344 static int (*nmL_getc_save)(Void);
345 #ifdef KR_headers
346 static int (*nmL_ungetc_save)(/* int, FILE* */);
347 #else
348 static int (*nmL_ungetc_save)(int, FILE*);
349 #endif
350
351 static int
352 nmL_getc(Void)
353 {
354 int rv;
355 if (rv = *nmL_next++)
356 return rv;
357 l_getc = nmL_getc_save;
358 l_ungetc = nmL_ungetc_save;
359 return (*l_getc)();
360 }
361
362 static int
363 #ifdef KR_headers
364 nmL_ungetc(x, f) int x; FILE *f;
365 #else
366 nmL_ungetc(int x, FILE *f)
367 #endif
368 {
369 f = f; /* banish non-use warning */
370 return *--nmL_next = x;
371 }
372
373 static int
374 #ifdef KR_headers
375 Lfinish(ch, dot, rvp) int ch, dot, *rvp;
376 #else
377 Lfinish(int ch, int dot, int *rvp)
378 #endif
379 {
380 char *s, *se;
381 static char what[] = "namelist input";
382
383 s = nmLbuf + 2;
384 se = nmLbuf + sizeof(nmLbuf) - 1;
385 *s++ = ch;
386 while(!issep(GETC(ch)) && ch!=EOF) {
387 if (s >= se) {
388 nmLbuf_ovfl:
389 return *rvp = err__fl(f__elist->cierr,131,what);
390 }
391 *s++ = ch;
392 if (ch != '=')
393 continue;
394 if (dot)
395 return *rvp = err__fl(f__elist->cierr,112,what);
396 got_eq:
397 *s = 0;
398 nmL_getc_save = l_getc;
399 l_getc = nmL_getc;
400 nmL_ungetc_save = l_ungetc;
401 l_ungetc = nmL_ungetc;
402 nmLbuf[1] = *(nmL_next = nmLbuf) = ',';
403 *rvp = f__lcount = 0;
404 return 1;
405 }
406 if (dot)
407 goto done;
408 for(;;) {
409 if (s >= se)
410 goto nmLbuf_ovfl;
411 *s++ = ch;
412 if (!isblnk(ch))
413 break;
414 if (GETC(ch) == EOF)
415 goto done;
416 }
417 if (ch == '=')
418 goto got_eq;
419 done:
420 Ungetc(ch, f__cf);
421 return 0;
422 }
423
424 static int
425 l_L(Void)
426 {
427 int ch, rv, sawdot;
428
429 if(f__lcount>0)
430 return(0);
431 f__lcount = 1;
432 f__ltype=0;
433 GETC(ch);
434 if(isdigit(ch))
435 {
436 rd_count(ch);
437 if(GETC(ch)!='*')
438 if(!f__cf || !feof(f__cf))
439 errfl(f__elist->cierr,112,"no star");
440 else
441 err(f__elist->cierr,(EOF),"lread");
442 GETC(ch);
443 }
444 sawdot = 0;
445 if(ch == '.') {
446 sawdot = 1;
447 GETC(ch);
448 }
449 switch(ch)
450 {
451 case 't':
452 case 'T':
453 if (nml_read && Lfinish(ch, sawdot, &rv))
454 return rv;
455 f__lx=1;
456 break;
457 case 'f':
458 case 'F':
459 if (nml_read && Lfinish(ch, sawdot, &rv))
460 return rv;
461 f__lx=0;
462 break;
463 default:
464 if(isblnk(ch) || issep(ch) || ch==EOF)
465 { (void) Ungetc(ch,f__cf);
466 return(0);
467 }
468 if (nml_read > 1) {
469 Ungetc(ch,f__cf);
470 f__lquit = 2;
471 return 0;
472 }
473 errfl(f__elist->cierr,112,"logical");
474 }
475 f__ltype=TYLONG;
476 while(!issep(GETC(ch)) && ch!=EOF);
477 Ungetc(ch, f__cf);
478 return(0);
479 }
480
481 #define BUFSIZE 128
482
483 static int
484 l_CHAR(Void)
485 { int ch,size,i;
486 static char rafail[] = "realloc failure";
487 char quote,*p;
488 if(f__lcount>0) return(0);
489 f__ltype=0;
490 if(f__lchar!=NULL) free(f__lchar);
491 size=BUFSIZE;
492 p=f__lchar = (char *)malloc((unsigned int)size);
493 if(f__lchar == NULL)
494 errfl(f__elist->cierr,113,"no space");
495
496 GETC(ch);
497 if(isdigit(ch)) {
498 /* allow Fortran 8x-style unquoted string... */
499 /* either find a repetition count or the string */
500 f__lcount = ch - '0';
501 *p++ = ch;
502 for(i = 1;;) {
503 switch(GETC(ch)) {
504 case '*':
505 if (f__lcount == 0) {
506 f__lcount = 1;
507 #ifndef F8X_NML_ELIDE_QUOTES
508 if (nml_read)
509 goto no_quote;
510 #endif
511 goto noquote;
512 }
513 p = f__lchar;
514 goto have_lcount;
515 case ',':
516 case ' ':
517 case '\t':
518 case '\n':
519 case '/':
520 Ungetc(ch,f__cf);
521 /* no break */
522 case EOF:
523 f__lcount = 1;
524 f__ltype = TYCHAR;
525 return *p = 0;
526 }
527 if (!isdigit(ch)) {
528 f__lcount = 1;
529 #ifndef F8X_NML_ELIDE_QUOTES
530 if (nml_read) {
531 no_quote:
532 errfl(f__elist->cierr,112,
533 "undelimited character string");
534 }
535 #endif
536 goto noquote;
537 }
538 *p++ = ch;
539 f__lcount = 10*f__lcount + ch - '0';
540 if (++i == size) {
541 f__lchar = (char *)realloc(f__lchar,
542 (unsigned int)(size += BUFSIZE));
543 if(f__lchar == NULL)
544 errfl(f__elist->cierr,113,rafail);
545 p = f__lchar + i;
546 }
547 }
548 }
549 else (void) Ungetc(ch,f__cf);
550 have_lcount:
551 if(GETC(ch)=='\'' || ch=='"') quote=ch;
552 else if(isblnk(ch) || (issep(ch) && ch != '\n') || ch==EOF) {
553 Ungetc(ch,f__cf);
554 return 0;
555 }
556 #ifndef F8X_NML_ELIDE_QUOTES
557 else if (nml_read > 1) {
558 Ungetc(ch,f__cf);
559 f__lquit = 2;
560 return 0;
561 }
562 #endif
563 else {
564 /* Fortran 8x-style unquoted string */
565 *p++ = ch;
566 for(i = 1;;) {
567 switch(GETC(ch)) {
568 case ',':
569 case ' ':
570 case '\t':
571 case '\n':
572 case '/':
573 Ungetc(ch,f__cf);
574 /* no break */
575 case EOF:
576 f__ltype = TYCHAR;
577 return *p = 0;
578 }
579 noquote:
580 *p++ = ch;
581 if (++i == size) {
582 f__lchar = (char *)realloc(f__lchar,
583 (unsigned int)(size += BUFSIZE));
584 if(f__lchar == NULL)
585 errfl(f__elist->cierr,113,rafail);
586 p = f__lchar + i;
587 }
588 }
589 }
590 f__ltype=TYCHAR;
591 for(i=0;;)
592 { while(GETC(ch)!=quote && ch!='\n'
593 && ch!=EOF && ++i<size) *p++ = ch;
594 if(i==size)
595 {
596 newone:
597 f__lchar= (char *)realloc(f__lchar,
598 (unsigned int)(size += BUFSIZE));
599 if(f__lchar == NULL)
600 errfl(f__elist->cierr,113,rafail);
601 p=f__lchar+i-1;
602 *p++ = ch;
603 }
604 else if(ch==EOF) return(EOF);
605 else if(ch=='\n')
606 { if(*(p-1) != '\\') continue;
607 i--;
608 p--;
609 if(++i<size) *p++ = ch;
610 else goto newone;
611 }
612 else if(GETC(ch)==quote)
613 { if(++i<size) *p++ = ch;
614 else goto newone;
615 }
616 else
617 { (void) Ungetc(ch,f__cf);
618 *p = 0;
619 return(0);
620 }
621 }
622 }
623
624 int
625 #ifdef KR_headers
626 c_le(a) cilist *a;
627 #else
628 c_le(cilist *a)
629 #endif
630 {
631 if(!f__init)
632 f_init();
633 f__fmtbuf="list io";
634 f__curunit = &f__units[a->ciunit];
635 if(a->ciunit>=MXUNIT || a->ciunit<0)
636 err(a->cierr,101,"stler");
637 f__scale=f__recpos=0;
638 f__elist=a;
639 if(f__curunit->ufd==NULL && fk_open(SEQ,FMT,a->ciunit))
640 err(a->cierr,102,"lio");
641 f__cf=f__curunit->ufd;
642 if(!f__curunit->ufmt) err(a->cierr,103,"lio")
643 return(0);
644 }
645
646 int
647 #ifdef KR_headers
648 l_read(number,ptr,len,type) ftnint *number,type; char *ptr; ftnlen len;
649 #else
650 l_read(ftnint *number, char *ptr, ftnlen len, ftnint type)
651 #endif
652 {
653 #define Ptr ((flex *)ptr)
654 int i,n,ch;
655 doublereal *yy;
656 real *xx;
657 for(i=0;i<*number;i++)
658 {
659 if(f__lquit) return(0);
660 if(l_eof)
661 err(f__elist->ciend, EOF, "list in")
662 if(f__lcount == 0) {
663 f__ltype = 0;
664 for(;;) {
665 GETC(ch);
666 switch(ch) {
667 case EOF:
668 err(f__elist->ciend,(EOF),"list in")
669 case ' ':
670 case '\t':
671 case '\n':
672 continue;
673 case '/':
674 f__lquit = 1;
675 goto loopend;
676 case ',':
677 f__lcount = 1;
678 goto loopend;
679 default:
680 (void) Ungetc(ch, f__cf);
681 goto rddata;
682 }
683 }
684 }
685 rddata:
686 switch((int)type)
687 {
688 case TYINT1:
689 case TYSHORT:
690 case TYLONG:
691 #ifndef ALLOW_FLOAT_IN_INTEGER_LIST_INPUT
692 ERR(l_R(0,1));
693 break;
694 #endif
695 case TYREAL:
696 case TYDREAL:
697 ERR(l_R(0,0));
698 break;
699 #ifdef TYQUAD
700 case TYQUAD:
701 n = l_R(0,2);
702 if (n)
703 return n;
704 break;
705 #endif
706 case TYCOMPLEX:
707 case TYDCOMPLEX:
708 ERR(l_C());
709 break;
710 case TYLOGICAL1:
711 case TYLOGICAL2:
712 case TYLOGICAL:
713 ERR(l_L());
714 break;
715 case TYCHAR:
716 ERR(l_CHAR());
717 break;
718 }
719 while (GETC(ch) == ' ' || ch == '\t');
720 if (ch != ',' || f__lcount > 1)
721 Ungetc(ch,f__cf);
722 loopend:
723 if(f__lquit) return(0);
724 if(f__cf && ferror(f__cf)) {
725 clearerr(f__cf);
726 errfl(f__elist->cierr,errno,"list in");
727 }
728 if(f__ltype==0) goto bump;
729 switch((int)type)
730 {
731 case TYINT1:
732 case TYLOGICAL1:
733 Ptr->flchar = (char)f__lx;
734 break;
735 case TYLOGICAL2:
736 case TYSHORT:
737 Ptr->flshort = (short)f__lx;
738 break;
739 case TYLOGICAL:
740 case TYLONG:
741 Ptr->flint = (ftnint)f__lx;
742 break;
743 #ifdef Allow_TYQUAD
744 case TYQUAD:
745 if (!(Ptr->fllongint = f__llx))
746 Ptr->fllongint = f__lx;
747 break;
748 #endif
749 case TYREAL:
750 Ptr->flreal=f__lx;
751 break;
752 case TYDREAL:
753 Ptr->fldouble=f__lx;
754 break;
755 case TYCOMPLEX:
756 xx=(real *)ptr;
757 *xx++ = f__lx;
758 *xx = f__ly;
759 break;
760 case TYDCOMPLEX:
761 yy=(doublereal *)ptr;
762 *yy++ = f__lx;
763 *yy = f__ly;
764 break;
765 case TYCHAR:
766 b_char(f__lchar,ptr,len);
767 break;
768 }
769 bump:
770 if(f__lcount>0) f__lcount--;
771 ptr += len;
772 if (nml_read)
773 nml_read++;
774 }
775 return(0);
776 #undef Ptr
777 }
778 #ifdef KR_headers
779 integer s_rsle(a) cilist *a;
780 #else
781 integer s_rsle(cilist *a)
782 #endif
783 {
784 int n;
785
786 f__reading=1;
787 f__external=1;
788 f__formatted=1;
789 if(n=c_le(a)) return(n);
790 f__lioproc = l_read;
791 f__lquit = 0;
792 f__lcount = 0;
793 l_eof = 0;
794 if(f__curunit->uwrt && f__nowreading(f__curunit))
795 err(a->cierr,errno,"read start");
796 if(f__curunit->uend)
797 err(f__elist->ciend,(EOF),"read start");
798 l_getc = t_getc;
799 l_ungetc = un_getc;
800 f__doend = xrd_SL;
801 return(0);
802 }
803 #ifdef __cplusplus
804 }
805 #endif