xref: /csrg-svn/usr.bin/f77/libI77/sfe.c (revision 2501)
1*2501Sdlw /*
2*2501Sdlw char id_sfe[] = "@(#)sfe.c	1.1";
3*2501Sdlw  *
4*2501Sdlw  * sequential formatted external routines
5*2501Sdlw  */
6*2501Sdlw 
7*2501Sdlw #include "fio.h"
8*2501Sdlw 
9*2501Sdlw /*
10*2501Sdlw  * read sequential formatted external
11*2501Sdlw  */
12*2501Sdlw 
13*2501Sdlw extern int rd_ed(),rd_ned();
14*2501Sdlw int x_rnew(),x_getc(),x_tab();
15*2501Sdlw 
16*2501Sdlw s_rsfe(a) cilist *a; /* start */
17*2501Sdlw {	int n;
18*2501Sdlw 	reading = YES;
19*2501Sdlw 	if(n=c_sfe(a,READ)) return(n);
20*2501Sdlw 	if(curunit->uwrt) nowreading(curunit);
21*2501Sdlw 	getn= x_getc;
22*2501Sdlw 	doed= rd_ed;
23*2501Sdlw 	doned= rd_ned;
24*2501Sdlw 	donewrec = dorevert = doend = x_rnew;
25*2501Sdlw 	dotab = x_tab;
26*2501Sdlw 	if(pars_f(fmtbuf)) err(errflag,100,"read sfe")
27*2501Sdlw 	fmt_bg();
28*2501Sdlw 	return(OK);
29*2501Sdlw }
30*2501Sdlw 
31*2501Sdlw x_rnew()			/* find next record */
32*2501Sdlw {	int ch;
33*2501Sdlw 	if(!curunit->uend)
34*2501Sdlw 		while((ch=getc(cf))!='\n' && ch!=EOF);
35*2501Sdlw 	cursor=recpos=reclen=0;
36*2501Sdlw 	return(OK);
37*2501Sdlw }
38*2501Sdlw 
39*2501Sdlw x_getc()
40*2501Sdlw {	int ch;
41*2501Sdlw 	if(curunit->uend) return(EOF);
42*2501Sdlw 	if((ch=getc(cf))!=EOF && ch!='\n')
43*2501Sdlw 	{	recpos++;
44*2501Sdlw 		return(ch);
45*2501Sdlw 	}
46*2501Sdlw 	if(ch=='\n')
47*2501Sdlw 	{	ungetc(ch,cf);
48*2501Sdlw 		return(ch);
49*2501Sdlw 	}
50*2501Sdlw 	if(feof(cf)) curunit->uend = YES;
51*2501Sdlw 	return(EOF);
52*2501Sdlw }
53*2501Sdlw 
54*2501Sdlw e_rsfe()
55*2501Sdlw {	int n;
56*2501Sdlw 	n=en_fio();
57*2501Sdlw 	fmtbuf=NULL;
58*2501Sdlw 	return(n);
59*2501Sdlw }
60*2501Sdlw 
61*2501Sdlw c_sfe(a,flag) cilist *a; /* check */
62*2501Sdlw {	unit *p;
63*2501Sdlw 	int n;
64*2501Sdlw 	external=sequential=formatted=FORMATTED;
65*2501Sdlw 	fmtbuf=a->cifmt;
66*2501Sdlw 	lfname = NULL;
67*2501Sdlw 	elist = NO;
68*2501Sdlw 	errflag = a->cierr;
69*2501Sdlw 	endflag = a->ciend;
70*2501Sdlw 	lunit = a->ciunit;
71*2501Sdlw 	if(not_legal(lunit)) err(errflag,101,"sfe");
72*2501Sdlw 	curunit = p = &units[lunit];
73*2501Sdlw 	if(!p->ufd && (n=fk_open(flag,SEQ,FMT,(ftnint)lunit)) )
74*2501Sdlw 		err(errflag,n,"sfe")
75*2501Sdlw 	cf = curunit->ufd;
76*2501Sdlw 	elist = YES;
77*2501Sdlw 	lfname = curunit->ufnm;
78*2501Sdlw 	if(!p->ufmt) err(errflag,102,"sfe")
79*2501Sdlw 	if(p->url) err(errflag,105,"sfe")
80*2501Sdlw 	cursor=recpos=scale=reclen=0;
81*2501Sdlw 	radix = 10;
82*2501Sdlw 	signit = YES;
83*2501Sdlw 	cblank = curunit->ublnk;
84*2501Sdlw 	cplus = NO;
85*2501Sdlw 	return(OK);
86*2501Sdlw }
87*2501Sdlw 
88*2501Sdlw /*
89*2501Sdlw  * write sequential formatted external
90*2501Sdlw  */
91*2501Sdlw 
92*2501Sdlw extern int w_ed(),w_ned();
93*2501Sdlw int x_putc(),pr_put(),x_wend(),x_wnew();
94*2501Sdlw ioflag new;
95*2501Sdlw 
96*2501Sdlw s_wsfe(a) cilist *a;	/*start*/
97*2501Sdlw {	int n;
98*2501Sdlw 	reading = NO;
99*2501Sdlw 	if(n=c_sfe(a,WRITE)) return(n);
100*2501Sdlw 	if(!curunit->uwrt) nowwriting(curunit);
101*2501Sdlw 	curunit->uend = NO;
102*2501Sdlw 	if (curunit->uprnt) putn = pr_put;
103*2501Sdlw 	else putn = x_putc;
104*2501Sdlw 	new = YES;
105*2501Sdlw 	doed= w_ed;
106*2501Sdlw 	doned= w_ned;
107*2501Sdlw 	doend = x_wend;
108*2501Sdlw 	dorevert = donewrec = x_wnew;
109*2501Sdlw 	dotab = x_tab;
110*2501Sdlw 	if(pars_f(fmtbuf)) err(errflag,100,"write sfe")
111*2501Sdlw 	fmt_bg();
112*2501Sdlw 	return(OK);
113*2501Sdlw }
114*2501Sdlw 
115*2501Sdlw x_putc(c)
116*2501Sdlw {
117*2501Sdlw 	if(c=='\n') recpos = reclen = cursor = 0;
118*2501Sdlw 	else recpos++;
119*2501Sdlw 	if (c) putc(c,cf);
120*2501Sdlw 	return(OK);
121*2501Sdlw }
122*2501Sdlw 
123*2501Sdlw pr_put(c)
124*2501Sdlw {
125*2501Sdlw 	if(c=='\n')
126*2501Sdlw 	{	new = YES;
127*2501Sdlw 		recpos = reclen = cursor = 0;
128*2501Sdlw 	}
129*2501Sdlw 	else if(new)
130*2501Sdlw 	{	new = NO;
131*2501Sdlw 		if(c=='0') c = '\n';
132*2501Sdlw 		else if(c=='1') c = '\f';
133*2501Sdlw 		else return(OK);
134*2501Sdlw 	}
135*2501Sdlw 	else recpos++;
136*2501Sdlw 	if (c) putc(c,cf);
137*2501Sdlw 	return(OK);
138*2501Sdlw }
139*2501Sdlw 
140*2501Sdlw x_tab()
141*2501Sdlw {	int n;
142*2501Sdlw 	if(reclen < recpos) reclen = recpos;
143*2501Sdlw 	if(curunit->useek)
144*2501Sdlw 	{	if((recpos+cursor) < 0) return(107);
145*2501Sdlw 		n = reclen - recpos;	/* distance to eor, n>=0 */
146*2501Sdlw 		if((cursor-n) > 0)
147*2501Sdlw 		{	fseek(cf,(long)n,1);  /* find current eor */
148*2501Sdlw 			recpos = reclen;
149*2501Sdlw 			cursor -= n;
150*2501Sdlw 		}
151*2501Sdlw 		else
152*2501Sdlw 		{	fseek(cf,(long)cursor,1);  /* do not pass go */
153*2501Sdlw 			recpos += cursor;
154*2501Sdlw 			return(cursor=0);
155*2501Sdlw 		}
156*2501Sdlw 	}
157*2501Sdlw 	else
158*2501Sdlw 		if(cursor < 0) return(120);	/* cant go back */
159*2501Sdlw 	while(cursor--)
160*2501Sdlw 	{	if(reading)
161*2501Sdlw 		{	n = (*getn)();
162*2501Sdlw 			if(n=='\n')
163*2501Sdlw 			{	(*ungetn)(n,cf);
164*2501Sdlw 				return(110);
165*2501Sdlw 			}
166*2501Sdlw 			if(n==EOF) return(EOF);
167*2501Sdlw 		}
168*2501Sdlw 		else	(*putn)(' ');	/* fill in the empty record */
169*2501Sdlw 	}
170*2501Sdlw 	return(cursor=0);
171*2501Sdlw }
172*2501Sdlw 
173*2501Sdlw x_wnew()
174*2501Sdlw {
175*2501Sdlw 	if(reclen>recpos) fseek(cf,(long)(reclen-recpos),1);
176*2501Sdlw 	return((*putn)('\n'));
177*2501Sdlw }
178*2501Sdlw 
179*2501Sdlw x_wend(last) char last;
180*2501Sdlw {
181*2501Sdlw 	if(reclen>recpos) fseek(cf,(long)(reclen-recpos),1);
182*2501Sdlw 	return((*putn)(last));
183*2501Sdlw }
184*2501Sdlw 
185*2501Sdlw /*
186*2501Sdlw /*xw_rev()
187*2501Sdlw /*{
188*2501Sdlw /*	if(workdone) x_wSL();
189*2501Sdlw /*	return(workdone=0);
190*2501Sdlw /*}
191*2501Sdlw /*
192*2501Sdlw */
193*2501Sdlw e_wsfe()
194*2501Sdlw {	return(e_rsfe()); }
195