Use -static when testing --gc-sections on native targets
[official-gcc.git] / libf2c / libI77 / rdfmt.c
blob08ff0e827b4e4cbaa3e4fed48b69750053d02b8f
1 #include <ctype.h>
2 #include "f2c.h"
3 #include "fio.h"
5 extern int f__cursor;
6 #ifdef KR_headers
7 extern double atof();
8 #else
9 #undef abs
10 #undef min
11 #undef max
12 #include <stdlib.h>
13 #endif
15 #include "fmt.h"
16 #include "fp.h"
18 static int
19 #ifdef KR_headers
20 rd_Z(n,w,len) Uint *n; ftnlen len;
21 #else
22 rd_Z(Uint *n, int w, ftnlen len)
23 #endif
25 long x[9];
26 char *s, *s0, *s1, *se, *t;
27 int ch, i, w1, w2;
28 static char hex[256];
29 static int one = 1;
30 int bad = 0;
32 if (!hex['0']) {
33 s = "0123456789";
34 while(ch = *s++)
35 hex[ch] = ch - '0' + 1;
36 s = "ABCDEF";
37 while(ch = *s++)
38 hex[ch] = hex[ch + 'a' - 'A'] = ch - 'A' + 11;
40 s = s0 = (char *)x;
41 s1 = (char *)&x[4];
42 se = (char *)&x[8];
43 if (len > 4*sizeof(long))
44 return errno = 117;
45 while (w) {
46 GET(ch);
47 if (ch==',' || ch=='\n')
48 break;
49 w--;
50 if (ch > ' ') {
51 if (!hex[ch & 0xff])
52 bad++;
53 *s++ = ch;
54 if (s == se) {
55 /* discard excess characters */
56 for(t = s0, s = s1; t < s1;)
57 *t++ = *s++;
58 s = s1;
62 if (bad)
63 return errno = 115;
64 w = (int)len;
65 w1 = s - s0;
66 w2 = w1+1 >> 1;
67 t = (char *)n;
68 if (*(char *)&one) {
69 /* little endian */
70 t += w - 1;
71 i = -1;
73 else
74 i = 1;
75 for(; w > w2; t += i, --w)
76 *t = 0;
77 if (!w)
78 return 0;
79 if (w < w2)
80 s0 = s - (w << 1);
81 else if (w1 & 1) {
82 *t = hex[*s0++ & 0xff] - 1;
83 if (!--w)
84 return 0;
85 t += i;
87 do {
88 *t = hex[*s0 & 0xff]-1 << 4 | hex[s0[1] & 0xff]-1;
89 t += i;
90 s0 += 2;
92 while(--w);
93 return 0;
96 static int
97 #ifdef KR_headers
98 rd_I(n,w,len, base) Uint *n; int w; ftnlen len; register int base;
99 #else
100 rd_I(Uint *n, int w, ftnlen len, register int base)
101 #endif
103 int bad, ch, sign;
104 longint x = 0;
106 if (w <= 0)
107 goto have_x;
108 for(;;) {
109 GET(ch);
110 if (ch != ' ')
111 break;
112 if (!--w)
113 goto have_x;
115 sign = 0;
116 switch(ch) {
117 case ',':
118 case '\n':
119 w = 0;
120 goto have_x;
121 case '-':
122 sign = 1;
123 case '+':
124 break;
125 default:
126 if (ch >= '0' && ch <= '9') {
127 x = ch - '0';
128 break;
130 goto have_x;
132 while(--w) {
133 GET(ch);
134 if (ch >= '0' && ch <= '9') {
135 x = x*base + ch - '0';
136 continue;
138 if (ch != ' ') {
139 if (ch == '\n' || ch == ',')
140 w = 0;
141 break;
143 if (f__cblank)
144 x *= base;
146 if (sign)
147 x = -x;
148 have_x:
149 if(len == sizeof(integer))
150 n->il=x;
151 else if(len == sizeof(char))
152 n->ic = (char)x;
153 #ifdef Allow_TYQUAD
154 else if (len == sizeof(longint))
155 n->ili = x;
156 #endif
157 else
158 n->is = (short)x;
159 if (w) {
160 while(--w)
161 GET(ch);
162 return errno = 115;
164 return 0;
167 static int
168 #ifdef KR_headers
169 rd_L(n,w,len) ftnint *n; ftnlen len;
170 #else
171 rd_L(ftnint *n, int w, ftnlen len)
172 #endif
173 { int ch, dot, lv;
175 if (w <= 0)
176 goto bad;
177 for(;;) {
178 GET(ch);
179 --w;
180 if (ch != ' ')
181 break;
182 if (!w)
183 goto bad;
185 dot = 0;
186 retry:
187 switch(ch) {
188 case '.':
189 if (dot++ || !w)
190 goto bad;
191 GET(ch);
192 --w;
193 goto retry;
194 case 't':
195 case 'T':
196 lv = 1;
197 break;
198 case 'f':
199 case 'F':
200 lv = 0;
201 break;
202 default:
203 bad:
204 for(; w > 0; --w)
205 GET(ch);
206 /* no break */
207 case ',':
208 case '\n':
209 return errno = 116;
211 switch(len) {
212 case sizeof(char): *(char *)n = (char)lv; break;
213 case sizeof(short): *(short *)n = (short)lv; break;
214 default: *n = lv;
216 while(w-- > 0) {
217 GET(ch);
218 if (ch == ',' || ch == '\n')
219 break;
221 return 0;
224 static int
225 #ifdef KR_headers
226 rd_F(p, w, d, len) ufloat *p; ftnlen len;
227 #else
228 rd_F(ufloat *p, int w, int d, ftnlen len)
229 #endif
231 char s[FMAX+EXPMAXDIGS+4];
232 register int ch;
233 register char *sp, *spe, *sp1;
234 double x;
235 int scale1, se;
236 long e, exp;
238 sp1 = sp = s;
239 spe = sp + FMAX;
240 exp = -d;
241 x = 0.;
243 do {
244 GET(ch);
245 w--;
246 } while (ch == ' ' && w);
247 switch(ch) {
248 case '-': *sp++ = ch; sp1++; spe++;
249 case '+':
250 if (!w) goto zero;
251 --w;
252 GET(ch);
254 while(ch == ' ') {
255 blankdrop:
256 if (!w--) goto zero; GET(ch); }
257 while(ch == '0')
258 { if (!w--) goto zero; GET(ch); }
259 if (ch == ' ' && f__cblank)
260 goto blankdrop;
261 scale1 = f__scale;
262 while(isdigit(ch)) {
263 digloop1:
264 if (sp < spe) *sp++ = ch;
265 else ++exp;
266 digloop1e:
267 if (!w--) goto done;
268 GET(ch);
270 if (ch == ' ') {
271 if (f__cblank)
272 { ch = '0'; goto digloop1; }
273 goto digloop1e;
275 if (ch == '.') {
276 exp += d;
277 if (!w--) goto done;
278 GET(ch);
279 if (sp == sp1) { /* no digits yet */
280 while(ch == '0') {
281 skip01:
282 --exp;
283 skip0:
284 if (!w--) goto done;
285 GET(ch);
287 if (ch == ' ') {
288 if (f__cblank) goto skip01;
289 goto skip0;
292 while(isdigit(ch)) {
293 digloop2:
294 if (sp < spe)
295 { *sp++ = ch; --exp; }
296 digloop2e:
297 if (!w--) goto done;
298 GET(ch);
300 if (ch == ' ') {
301 if (f__cblank)
302 { ch = '0'; goto digloop2; }
303 goto digloop2e;
306 switch(ch) {
307 default:
308 break;
309 case '-': se = 1; goto signonly;
310 case '+': se = 0; goto signonly;
311 case 'e':
312 case 'E':
313 case 'd':
314 case 'D':
315 if (!w--)
316 goto bad;
317 GET(ch);
318 while(ch == ' ') {
319 if (!w--)
320 goto bad;
321 GET(ch);
323 se = 0;
324 switch(ch) {
325 case '-': se = 1;
326 case '+':
327 signonly:
328 if (!w--)
329 goto bad;
330 GET(ch);
332 while(ch == ' ') {
333 if (!w--)
334 goto bad;
335 GET(ch);
337 if (!isdigit(ch))
338 goto bad;
340 e = ch - '0';
341 for(;;) {
342 if (!w--)
343 { ch = '\n'; break; }
344 GET(ch);
345 if (!isdigit(ch)) {
346 if (ch == ' ') {
347 if (f__cblank)
348 ch = '0';
349 else continue;
351 else
352 break;
354 e = 10*e + ch - '0';
355 if (e > EXPMAX && sp > sp1)
356 goto bad;
358 if (se)
359 exp -= e;
360 else
361 exp += e;
362 scale1 = 0;
364 switch(ch) {
365 case '\n':
366 case ',':
367 break;
368 default:
369 bad:
370 return (errno = 115);
372 done:
373 if (sp > sp1) {
374 while(*--sp == '0')
375 ++exp;
376 if (exp -= scale1)
377 sprintf(sp+1, "e%ld", exp);
378 else
379 sp[1] = 0;
380 x = atof(s);
382 zero:
383 if (len == sizeof(real))
384 p->pf = x;
385 else
386 p->pd = x;
387 return(0);
391 static int
392 #ifdef KR_headers
393 rd_A(p,len) char *p; ftnlen len;
394 #else
395 rd_A(char *p, ftnlen len)
396 #endif
397 { int i,ch;
398 for(i=0;i<len;i++)
399 { GET(ch);
400 *p++=VAL(ch);
402 return(0);
404 static int
405 #ifdef KR_headers
406 rd_AW(p,w,len) char *p; ftnlen len;
407 #else
408 rd_AW(char *p, int w, ftnlen len)
409 #endif
410 { int i,ch;
411 if(w>=len)
412 { for(i=0;i<w-len;i++)
413 GET(ch);
414 for(i=0;i<len;i++)
415 { GET(ch);
416 *p++=VAL(ch);
418 return(0);
420 for(i=0;i<w;i++)
421 { GET(ch);
422 *p++=VAL(ch);
424 for(i=0;i<len-w;i++) *p++=' ';
425 return(0);
427 static int
428 #ifdef KR_headers
429 rd_H(n,s) char *s;
430 #else
431 rd_H(int n, char *s)
432 #endif
433 { int i,ch;
434 for(i=0;i<n;i++)
435 if((ch=(*f__getn)())<0) return(ch);
436 else *s++ = ch=='\n'?' ':ch;
437 return(1);
439 static int
440 #ifdef KR_headers
441 rd_POS(s) char *s;
442 #else
443 rd_POS(char *s)
444 #endif
445 { char quote;
446 int ch;
447 quote= *s++;
448 for(;*s;s++)
449 if(*s==quote && *(s+1)!=quote) break;
450 else if((ch=(*f__getn)())<0) return(ch);
451 else *s = ch=='\n'?' ':ch;
452 return(1);
454 #ifdef KR_headers
455 rd_ed(p,ptr,len) struct syl *p; char *ptr; ftnlen len;
456 #else
457 rd_ed(struct syl *p, char *ptr, ftnlen len)
458 #endif
459 { int ch;
460 for(;f__cursor>0;f__cursor--) if((ch=(*f__getn)())<0) return(ch);
461 if(f__cursor<0)
462 { if(f__recpos+f__cursor < 0) /*err(elist->cierr,110,"fmt")*/
463 f__cursor = -f__recpos; /* is this in the standard? */
464 if(f__external == 0) {
465 extern char *f__icptr;
466 f__icptr += f__cursor;
468 else if(f__curunit && f__curunit->useek)
469 (void) fseek(f__cf,(long) f__cursor,SEEK_CUR);
470 else
471 err(f__elist->cierr,106,"fmt");
472 f__recpos += f__cursor;
473 f__cursor=0;
475 switch(p->op)
477 default: fprintf(stderr,"rd_ed, unexpected code: %d\n", p->op);
478 sig_die(f__fmtbuf, 1);
479 case IM:
480 case I: ch = rd_I((Uint *)ptr,p->p1,len, 10);
481 break;
483 /* O and OM don't work right for character, double, complex, */
484 /* or doublecomplex, and they differ from Fortran 90 in */
485 /* showing a minus sign for negative values. */
487 case OM:
488 case O: ch = rd_I((Uint *)ptr, p->p1, len, 8);
489 break;
490 case L: ch = rd_L((ftnint *)ptr,p->p1,len);
491 break;
492 case A: ch = rd_A(ptr,len);
493 break;
494 case AW:
495 ch = rd_AW(ptr,p->p1,len);
496 break;
497 case E: case EE:
498 case D:
499 case G:
500 case GE:
501 case F: ch = rd_F((ufloat *)ptr,p->p1,p->p2.i[0],len);
502 break;
504 /* Z and ZM assume 8-bit bytes. */
506 case ZM:
507 case Z:
508 ch = rd_Z((Uint *)ptr, p->p1, len);
509 break;
511 if(ch == 0) return(ch);
512 else if(ch == EOF) return(EOF);
513 if (f__cf)
514 clearerr(f__cf);
515 return(errno);
517 #ifdef KR_headers
518 rd_ned(p) struct syl *p;
519 #else
520 rd_ned(struct syl *p)
521 #endif
523 switch(p->op)
525 default: fprintf(stderr,"rd_ned, unexpected code: %d\n", p->op);
526 sig_die(f__fmtbuf, 1);
527 case APOS:
528 return(rd_POS(p->p2.s));
529 case H: return(rd_H(p->p1,p->p2.s));
530 case SLASH: return((*f__donewrec)());
531 case TR:
532 case X: f__cursor += p->p1;
533 return(1);
534 case T: f__cursor=p->p1-f__recpos - 1;
535 return(1);
536 case TL: f__cursor -= p->p1;
537 if(f__cursor < -f__recpos) /* TL1000, 1X */
538 f__cursor = -f__recpos;
539 return(1);