1 | #include "f2c.h" |
---|
2 | #include "fio.h" |
---|
3 | #include "fmt.h" |
---|
4 | |
---|
5 | extern icilist *f__svic; |
---|
6 | extern char *f__icptr; |
---|
7 | |
---|
8 | static int |
---|
9 | mv_cur(Void) /* shouldn't use fseek because it insists on calling fflush */ |
---|
10 | /* instead we know too much about stdio */ |
---|
11 | { |
---|
12 | int cursor = f__cursor; |
---|
13 | f__cursor = 0; |
---|
14 | if(f__external == 0) { |
---|
15 | if(cursor < 0) { |
---|
16 | if(f__hiwater < f__recpos) |
---|
17 | f__hiwater = f__recpos; |
---|
18 | f__recpos += cursor; |
---|
19 | f__icptr += cursor; |
---|
20 | if(f__recpos < 0) |
---|
21 | err(f__elist->cierr, 110, "left off"); |
---|
22 | } |
---|
23 | else if(cursor > 0) { |
---|
24 | if(f__recpos + cursor >= f__svic->icirlen) |
---|
25 | err(f__elist->cierr, 110, "recend"); |
---|
26 | if(f__hiwater <= f__recpos) |
---|
27 | for(; cursor > 0; cursor--) |
---|
28 | (*f__putn)(' '); |
---|
29 | else if(f__hiwater <= f__recpos + cursor) { |
---|
30 | cursor -= f__hiwater - f__recpos; |
---|
31 | f__icptr += f__hiwater - f__recpos; |
---|
32 | f__recpos = f__hiwater; |
---|
33 | for(; cursor > 0; cursor--) |
---|
34 | (*f__putn)(' '); |
---|
35 | } |
---|
36 | else { |
---|
37 | f__icptr += cursor; |
---|
38 | f__recpos += cursor; |
---|
39 | } |
---|
40 | } |
---|
41 | return(0); |
---|
42 | } |
---|
43 | if (cursor > 0) { |
---|
44 | if(f__hiwater <= f__recpos) |
---|
45 | for(;cursor>0;cursor--) (*f__putn)(' '); |
---|
46 | else if(f__hiwater <= f__recpos + cursor) { |
---|
47 | cursor -= f__hiwater - f__recpos; |
---|
48 | f__recpos = f__hiwater; |
---|
49 | for(; cursor > 0; cursor--) |
---|
50 | (*f__putn)(' '); |
---|
51 | } |
---|
52 | else { |
---|
53 | f__recpos += cursor; |
---|
54 | } |
---|
55 | } |
---|
56 | else if (cursor < 0) |
---|
57 | { |
---|
58 | if(cursor + f__recpos < 0) |
---|
59 | err(f__elist->cierr,110,"left off"); |
---|
60 | if(f__hiwater < f__recpos) |
---|
61 | f__hiwater = f__recpos; |
---|
62 | f__recpos += cursor; |
---|
63 | } |
---|
64 | return(0); |
---|
65 | } |
---|
66 | |
---|
67 | static int |
---|
68 | #ifdef KR_headers |
---|
69 | wrt_Z(n,w,minlen,len) Uint *n; int w, minlen; ftnlen len; |
---|
70 | #else |
---|
71 | wrt_Z(Uint *n, int w, int minlen, ftnlen len) |
---|
72 | #endif |
---|
73 | { |
---|
74 | register char *s, *se; |
---|
75 | register int i, w1; |
---|
76 | static int one = 1; |
---|
77 | static char hex[] = "0123456789ABCDEF"; |
---|
78 | s = (char *)n; |
---|
79 | --len; |
---|
80 | if (*(char *)&one) { |
---|
81 | /* little endian */ |
---|
82 | se = s; |
---|
83 | s += len; |
---|
84 | i = -1; |
---|
85 | } |
---|
86 | else { |
---|
87 | se = s + len; |
---|
88 | i = 1; |
---|
89 | } |
---|
90 | for(;; s += i) |
---|
91 | if (s == se || *s) |
---|
92 | break; |
---|
93 | w1 = (i*(se-s) << 1) + 1; |
---|
94 | if (*s & 0xf0) |
---|
95 | w1++; |
---|
96 | if (w1 > w) |
---|
97 | for(i = 0; i < w; i++) |
---|
98 | (*f__putn)('*'); |
---|
99 | else { |
---|
100 | if ((minlen -= w1) > 0) |
---|
101 | w1 += minlen; |
---|
102 | while(--w >= w1) |
---|
103 | (*f__putn)(' '); |
---|
104 | while(--minlen >= 0) |
---|
105 | (*f__putn)('0'); |
---|
106 | if (!(*s & 0xf0)) { |
---|
107 | (*f__putn)(hex[*s & 0xf]); |
---|
108 | if (s == se) |
---|
109 | return 0; |
---|
110 | s += i; |
---|
111 | } |
---|
112 | for(;; s += i) { |
---|
113 | (*f__putn)(hex[*s >> 4 & 0xf]); |
---|
114 | (*f__putn)(hex[*s & 0xf]); |
---|
115 | if (s == se) |
---|
116 | break; |
---|
117 | } |
---|
118 | } |
---|
119 | return 0; |
---|
120 | } |
---|
121 | |
---|
122 | static int |
---|
123 | #ifdef KR_headers |
---|
124 | wrt_I(n,w,len, base) Uint *n; ftnlen len; register int base; |
---|
125 | #else |
---|
126 | wrt_I(Uint *n, int w, ftnlen len, register int base) |
---|
127 | #endif |
---|
128 | { int ndigit,sign,spare,i; |
---|
129 | longint x; |
---|
130 | char *ans; |
---|
131 | if(len==sizeof(integer)) x=n->il; |
---|
132 | else if(len == sizeof(char)) x = n->ic; |
---|
133 | #ifdef Allow_TYQUAD |
---|
134 | else if (len == sizeof(longint)) x = n->ili; |
---|
135 | #endif |
---|
136 | else x=n->is; |
---|
137 | ans=f__icvt(x,&ndigit,&sign, base); |
---|
138 | spare=w-ndigit; |
---|
139 | if(sign || f__cplus) spare--; |
---|
140 | if(spare<0) |
---|
141 | for(i=0;i<w;i++) (*f__putn)('*'); |
---|
142 | else |
---|
143 | { for(i=0;i<spare;i++) (*f__putn)(' '); |
---|
144 | if(sign) (*f__putn)('-'); |
---|
145 | else if(f__cplus) (*f__putn)('+'); |
---|
146 | for(i=0;i<ndigit;i++) (*f__putn)(*ans++); |
---|
147 | } |
---|
148 | return(0); |
---|
149 | } |
---|
150 | static int |
---|
151 | #ifdef KR_headers |
---|
152 | wrt_IM(n,w,m,len,base) Uint *n; ftnlen len; int base; |
---|
153 | #else |
---|
154 | wrt_IM(Uint *n, int w, int m, ftnlen len, int base) |
---|
155 | #endif |
---|
156 | { int ndigit,sign,spare,i,xsign; |
---|
157 | longint x; |
---|
158 | char *ans; |
---|
159 | if(sizeof(integer)==len) x=n->il; |
---|
160 | else if(len == sizeof(char)) x = n->ic; |
---|
161 | #ifdef Allow_TYQUAD |
---|
162 | else if (len == sizeof(longint)) x = n->ili; |
---|
163 | #endif |
---|
164 | else x=n->is; |
---|
165 | ans=f__icvt(x,&ndigit,&sign, base); |
---|
166 | if(sign || f__cplus) xsign=1; |
---|
167 | else xsign=0; |
---|
168 | if(ndigit+xsign>w || m+xsign>w) |
---|
169 | { for(i=0;i<w;i++) (*f__putn)('*'); |
---|
170 | return(0); |
---|
171 | } |
---|
172 | if(x==0 && m==0) |
---|
173 | { for(i=0;i<w;i++) (*f__putn)(' '); |
---|
174 | return(0); |
---|
175 | } |
---|
176 | if(ndigit>=m) |
---|
177 | spare=w-ndigit-xsign; |
---|
178 | else |
---|
179 | spare=w-m-xsign; |
---|
180 | for(i=0;i<spare;i++) (*f__putn)(' '); |
---|
181 | if(sign) (*f__putn)('-'); |
---|
182 | else if(f__cplus) (*f__putn)('+'); |
---|
183 | for(i=0;i<m-ndigit;i++) (*f__putn)('0'); |
---|
184 | for(i=0;i<ndigit;i++) (*f__putn)(*ans++); |
---|
185 | return(0); |
---|
186 | } |
---|
187 | static int |
---|
188 | #ifdef KR_headers |
---|
189 | wrt_AP(s) char *s; |
---|
190 | #else |
---|
191 | wrt_AP(char *s) |
---|
192 | #endif |
---|
193 | { char quote; |
---|
194 | int i; |
---|
195 | |
---|
196 | if(f__cursor && (i = mv_cur())) |
---|
197 | return i; |
---|
198 | quote = *s++; |
---|
199 | for(;*s;s++) |
---|
200 | { if(*s!=quote) (*f__putn)(*s); |
---|
201 | else if(*++s==quote) (*f__putn)(*s); |
---|
202 | else return(1); |
---|
203 | } |
---|
204 | return(1); |
---|
205 | } |
---|
206 | static int |
---|
207 | #ifdef KR_headers |
---|
208 | wrt_H(a,s) char *s; |
---|
209 | #else |
---|
210 | wrt_H(int a, char *s) |
---|
211 | #endif |
---|
212 | { |
---|
213 | int i; |
---|
214 | |
---|
215 | if(f__cursor && (i = mv_cur())) |
---|
216 | return i; |
---|
217 | while(a--) (*f__putn)(*s++); |
---|
218 | return(1); |
---|
219 | } |
---|
220 | #ifdef KR_headers |
---|
221 | wrt_L(n,len, sz) Uint *n; ftnlen sz; |
---|
222 | #else |
---|
223 | wrt_L(Uint *n, int len, ftnlen sz) |
---|
224 | #endif |
---|
225 | { int i; |
---|
226 | long x; |
---|
227 | if(sizeof(long)==sz) x=n->il; |
---|
228 | else if(sz == sizeof(char)) x = n->ic; |
---|
229 | else x=n->is; |
---|
230 | for(i=0;i<len-1;i++) |
---|
231 | (*f__putn)(' '); |
---|
232 | if(x) (*f__putn)('T'); |
---|
233 | else (*f__putn)('F'); |
---|
234 | return(0); |
---|
235 | } |
---|
236 | static int |
---|
237 | #ifdef KR_headers |
---|
238 | wrt_A(p,len) char *p; ftnlen len; |
---|
239 | #else |
---|
240 | wrt_A(char *p, ftnlen len) |
---|
241 | #endif |
---|
242 | { |
---|
243 | while(len-- > 0) (*f__putn)(*p++); |
---|
244 | return(0); |
---|
245 | } |
---|
246 | static int |
---|
247 | #ifdef KR_headers |
---|
248 | wrt_AW(p,w,len) char * p; ftnlen len; |
---|
249 | #else |
---|
250 | wrt_AW(char * p, int w, ftnlen len) |
---|
251 | #endif |
---|
252 | { |
---|
253 | while(w>len) |
---|
254 | { w--; |
---|
255 | (*f__putn)(' '); |
---|
256 | } |
---|
257 | while(w-- > 0) |
---|
258 | (*f__putn)(*p++); |
---|
259 | return(0); |
---|
260 | } |
---|
261 | |
---|
262 | static int |
---|
263 | #ifdef KR_headers |
---|
264 | wrt_G(p,w,d,e,len) ufloat *p; ftnlen len; |
---|
265 | #else |
---|
266 | wrt_G(ufloat *p, int w, int d, int e, ftnlen len) |
---|
267 | #endif |
---|
268 | { double up = 1,x; |
---|
269 | int i=0,oldscale,n,j; |
---|
270 | x = len==sizeof(real)?p->pf:p->pd; |
---|
271 | if(x < 0 ) x = -x; |
---|
272 | if(x<.1) { |
---|
273 | if (x != 0.) |
---|
274 | return(wrt_E(p,w,d,e,len)); |
---|
275 | i = 1; |
---|
276 | goto have_i; |
---|
277 | } |
---|
278 | for(;i<=d;i++,up*=10) |
---|
279 | { if(x>=up) continue; |
---|
280 | have_i: |
---|
281 | oldscale = f__scale; |
---|
282 | f__scale = 0; |
---|
283 | if(e==0) n=4; |
---|
284 | else n=e+2; |
---|
285 | i=wrt_F(p,w-n,d-i,len); |
---|
286 | for(j=0;j<n;j++) (*f__putn)(' '); |
---|
287 | f__scale=oldscale; |
---|
288 | return(i); |
---|
289 | } |
---|
290 | return(wrt_E(p,w,d,e,len)); |
---|
291 | } |
---|
292 | #ifdef KR_headers |
---|
293 | w_ed(p,ptr,len) struct syl *p; char *ptr; ftnlen len; |
---|
294 | #else |
---|
295 | w_ed(struct syl *p, char *ptr, ftnlen len) |
---|
296 | #endif |
---|
297 | { |
---|
298 | int i; |
---|
299 | |
---|
300 | if(f__cursor && (i = mv_cur())) |
---|
301 | return i; |
---|
302 | switch(p->op) |
---|
303 | { |
---|
304 | default: |
---|
305 | fprintf(stderr,"w_ed, unexpected code: %d\n", p->op); |
---|
306 | sig_die(f__fmtbuf, 1); |
---|
307 | case I: return(wrt_I((Uint *)ptr,p->p1,len, 10)); |
---|
308 | case IM: |
---|
309 | return(wrt_IM((Uint *)ptr,p->p1,p->p2.i[0],len,10)); |
---|
310 | |
---|
311 | /* O and OM don't work right for character, double, complex, */ |
---|
312 | /* or doublecomplex, and they differ from Fortran 90 in */ |
---|
313 | /* showing a minus sign for negative values. */ |
---|
314 | |
---|
315 | case O: return(wrt_I((Uint *)ptr, p->p1, len, 8)); |
---|
316 | case OM: |
---|
317 | return(wrt_IM((Uint *)ptr,p->p1,p->p2.i[0],len,8)); |
---|
318 | case L: return(wrt_L((Uint *)ptr,p->p1, len)); |
---|
319 | case A: return(wrt_A(ptr,len)); |
---|
320 | case AW: |
---|
321 | return(wrt_AW(ptr,p->p1,len)); |
---|
322 | case D: |
---|
323 | case E: |
---|
324 | case EE: |
---|
325 | return(wrt_E((ufloat *)ptr,p->p1,p->p2.i[0],p->p2.i[1],len)); |
---|
326 | case G: |
---|
327 | case GE: |
---|
328 | return(wrt_G((ufloat *)ptr,p->p1,p->p2.i[0],p->p2.i[1],len)); |
---|
329 | case F: return(wrt_F((ufloat *)ptr,p->p1,p->p2.i[0],len)); |
---|
330 | |
---|
331 | /* Z and ZM assume 8-bit bytes. */ |
---|
332 | |
---|
333 | case Z: return(wrt_Z((Uint *)ptr,p->p1,0,len)); |
---|
334 | case ZM: |
---|
335 | return(wrt_Z((Uint *)ptr,p->p1,p->p2.i[0],len)); |
---|
336 | } |
---|
337 | } |
---|
338 | #ifdef KR_headers |
---|
339 | w_ned(p) struct syl *p; |
---|
340 | #else |
---|
341 | w_ned(struct syl *p) |
---|
342 | #endif |
---|
343 | { |
---|
344 | switch(p->op) |
---|
345 | { |
---|
346 | default: fprintf(stderr,"w_ned, unexpected code: %d\n", p->op); |
---|
347 | sig_die(f__fmtbuf, 1); |
---|
348 | case SLASH: |
---|
349 | return((*f__donewrec)()); |
---|
350 | case T: f__cursor = p->p1-f__recpos - 1; |
---|
351 | return(1); |
---|
352 | case TL: f__cursor -= p->p1; |
---|
353 | if(f__cursor < -f__recpos) /* TL1000, 1X */ |
---|
354 | f__cursor = -f__recpos; |
---|
355 | return(1); |
---|
356 | case TR: |
---|
357 | case X: |
---|
358 | f__cursor += p->p1; |
---|
359 | return(1); |
---|
360 | case APOS: |
---|
361 | return(wrt_AP(p->p2.s)); |
---|
362 | case H: |
---|
363 | return(wrt_H(p->p1,p->p2.s)); |
---|
364 | } |
---|
365 | } |
---|