xref: /original-bsd/usr.bin/f77/libI77/dfe.c (revision 1b4ef7de)
1 /*
2 char id_dfe[] = "@(#)dfe.c	1.6";
3  *
4  * direct formatted external i/o
5  */
6 
7 #include "fio.h"
8 
9 extern int rd_ed(),rd_ned(),w_ed(),w_ned();
10 int y_getc(),y_putc(),y_rnew(),y_wnew(),y_tab();
11 
12 LOCAL char rdfe[] = "read dfe";
13 LOCAL char wdfe[] = "write dfe";
14 
15 s_rdfe(a) cilist *a;
16 {
17 	int n;
18 	reading = YES;
19 	if(n=c_dfe(a,READ)) return(n);
20 	if(curunit->uwrt && ! nowreading(curunit)) err(errflag, errno, rdfe)
21 	getn = y_getc;
22 	doed = rd_ed;
23 	doned = rd_ned;
24 	dotab = y_tab;
25 	dorevert = doend = donewrec = y_rnew;
26 	if(pars_f()) err(errflag,F_ERFMT,rdfe)
27 	fmt_bg();
28 	return(OK);
29 }
30 
31 s_wdfe(a) cilist *a;
32 {
33 	int n;
34 	reading = NO;
35 	if(n=c_dfe(a,WRITE)) return(n);
36 	curunit->uend = NO;
37 	if(!curunit->uwrt && ! nowwriting(curunit)) err(errflag, errno, wdfe)
38 	putn = y_putc;
39 	doed = w_ed;
40 	doned = w_ned;
41 	dotab = y_tab;
42 	dorevert = doend = donewrec = y_wnew;
43 	if(pars_f()) err(errflag,F_ERFMT,wdfe)
44 	fmt_bg();
45 	return(OK);
46 }
47 
48 e_rdfe()
49 {
50 	en_fio();
51 	return(OK);
52 }
53 
54 e_wdfe()
55 {
56 	en_fio();
57 	return(OK);
58 }
59 
60 LOCAL
61 c_dfe(a,flag) cilist *a;
62 {	int n;
63 	sequential = NO;
64 	external = formatted = FORMATTED;
65 	lfname = NULL;
66 	elist = NO;
67 	cursor=scale=recpos=reclen=0;
68 	radix = 10;
69 	signit = YES;
70 	fmtbuf = a->cifmt;
71 	errflag = a->cierr;
72 	endflag = a->ciend;
73 	lunit = a->ciunit;
74 	if(not_legal(lunit)) err(errflag,F_ERUNIT,rdfe+5);
75 	curunit = &units[lunit];
76 	if(!curunit->ufd && (n=fk_open(flag,DIR,FMT,(ftnint)lunit)))
77 		err(errflag,n,rdfe+5)
78 	cf = curunit->ufd;
79 	elist = YES;
80 	lfname = curunit->ufnm;
81 	if(!curunit->ufmt) err(errflag,F_ERNOFIO,rdfe+5)
82 	if(!curunit->useek || !curunit->url) err(errflag,F_ERNODIO,rdfe+5)
83 	recnum = a->cirec - 1;
84 	fseek(cf, (long)curunit->url * recnum, 0);
85 	cblank = curunit->ublnk;
86 	cplus = NO;
87 	return(OK);
88 }
89 
90 LOCAL
91 y_getc()
92 {
93 	int ch;
94 	if(curunit->uend) return(EOF);
95 	if(curunit->url==1 || recpos++ < curunit->url)
96 	{
97 		if((ch=getc(cf))!=EOF)
98 		{
99 				return(ch);
100 		}
101 		if(feof(cf))
102 		{
103 			curunit->uend = YES;
104 			return(EOF);
105 		}
106 		err(errflag,errno,rdfe);
107 	}
108 	else return(' ');
109 }
110 
111 LOCAL
112 y_putc(c)
113 {
114 	if(curunit->url!=1 && recpos++ >= curunit->url) err(errflag,F_EREREC,wdfe)
115 	putc(c,cf);
116 	return(OK);
117 }
118 
119 LOCAL
120 y_tab()
121 {	int n;
122 	if(curunit->url==1)
123 	{
124 		if(cursor < 0 && -cursor > ftell(cf)) rewind(cf);
125 		else	fseek(cf,(long)cursor,1);
126 		return(cursor=0);
127 	}
128 	else
129 	{	if(reclen < recpos) reclen = recpos;
130 		if((recpos + cursor) < 0) cursor = -recpos;	/* BOR */
131 		n = reclen - recpos;		/* n >= 0 */
132 		if(!reading && (cursor-n) > 0)
133 		{	recpos = reclen;
134 			cursor -= n;
135 			fseek(cf,(long)n,1);
136 			while(cursor--) if(n=(*putn)(' ')) return(n);
137 			return(cursor=0);
138 		}
139 		recpos += cursor;
140 		if(recpos >= curunit->url) err(errflag,F_EREREC,rdfe+5)
141 	}
142 	fseek(cf,(long)cursor,1);
143 	return(cursor=0);
144 }
145 
146 /*
147 /*y_rev()
148 /*{	/*what about work done?*/
149 /*	if(curunit->url==1) return(0);
150 /*	while(recpos<curunit->url) (*putn)(' ');
151 /*	recpos=0;
152 /*	return(0);
153 /*}
154 /*
155 /*y_err()
156 /*{
157 /*	err(errflag, F_EREREC, rdfe+5);
158 /*}
159 */
160 
161 LOCAL
162 y_rnew()
163 {	if(curunit->url != 1)
164 	{	fseek(cf,(long)curunit->url*(++recnum),0);
165 		recpos = reclen = cursor = 0;
166 	}
167 	return(OK);
168 }
169 
170 LOCAL
171 y_wnew()
172 {	if(curunit->url != 1)
173 	{	if(reclen > recpos)
174 		{	fseek(cf,(long)(reclen-recpos),1);
175 			recpos = reclen;
176 		}
177 		while(recpos < curunit->url) (*putn)(' ');
178 		recnum++;
179 		recpos = reclen = cursor = 0;
180 	}
181 	return(OK);
182 }
183 
184 LOCAL
185 y_rend()
186 {
187 	return(OK);
188 }
189 
190 LOCAL
191 y_wend()
192 {
193 	return(y_wnew());
194 }
195