xref: /original-bsd/usr.bin/f77/libI77/dfe.c (revision 56abee86)
1 /*
2 char id_dfe[] = "@(#)dfe.c	1.2";
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 char *dfe = "dfe";
13 char *rdfe = "read dfe";
14 char *wdfe = "write dfe";
15 
16 s_rdfe(a) cilist *a;
17 {
18 	int n;
19 	reading = YES;
20 	if(n=c_dfe(a,READ)) return(n);
21 	if(curunit->uwrt) nowreading(curunit);
22 	getn = y_getc;
23 	doed = rd_ed;
24 	doned = rd_ned;
25 	dotab = y_tab;
26 	dorevert = doend = donewrec = y_rnew;
27 	if(pars_f(fmtbuf)) err(errflag,F_ERFMT,rdfe)
28 	fmt_bg();
29 	return(OK);
30 }
31 
32 s_wdfe(a) cilist *a;
33 {
34 	int n;
35 	reading = NO;
36 	if(n=c_dfe(a,WRITE)) return(n);
37 	curunit->uend = NO;
38 	if(!curunit->uwrt) nowwriting(curunit);
39 	putn = y_putc;
40 	doed = w_ed;
41 	doned = w_ned;
42 	dotab = y_tab;
43 	dorevert = doend = donewrec = y_wnew;
44 	if(pars_f(fmtbuf)) err(errflag,F_ERFMT,wdfe)
45 	fmt_bg();
46 	return(OK);
47 }
48 
49 e_rdfe()
50 {
51 	en_fio();
52 	return(OK);
53 }
54 
55 e_wdfe()
56 {
57 	en_fio();
58 	return(OK);
59 }
60 
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,dfe);
75 	curunit = &units[lunit];
76 	if(!curunit->ufd && (n=fk_open(flag,DIR,FMT,(ftnint)lunit)))
77 		err(errflag,n,dfe)
78 	cf = curunit->ufd;
79 	elist = YES;
80 	lfname = curunit->ufnm;
81 	if(!curunit->ufmt) err(errflag,F_ERNOFIO,dfe)
82 	if(!curunit->useek || !curunit->url) err(errflag,F_ERNODIO,dfe)
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 y_getc()
91 {
92 	int ch;
93 	if(curunit->uend) return(EOF);
94 	if(curunit->url==1 || recpos++ < curunit->url)
95 	{
96 		if((ch=getc(cf))!=EOF)
97 		{
98 				return(ch);
99 		}
100 		if(feof(cf))
101 		{
102 			curunit->uend = YES;
103 			return(EOF);
104 		}
105 		err(errflag,errno,rdfe);
106 	}
107 	else return(' ');
108 }
109 
110 y_putc(c)
111 {
112 	if(curunit->url!=1 && recpos++ >= curunit->url) err(errflag,F_EREREC,wdfe)
113 	putc(c,cf);
114 	return(OK);
115 }
116 
117 y_tab()
118 {	int n;
119 	if(curunit->url==1)
120 	{
121 		if(cursor < 0 && -cursor > ftell(cf)) return(F_ERBREC);
122 	}
123 	else
124 	{	if(reclen < recpos) reclen = recpos;
125 		if((recpos + cursor) < 0) return(F_ERBREC);
126 		n = reclen - recpos;		/* n >= 0 */
127 		if(!reading && (cursor-n) > 0)
128 		{	recpos = reclen;
129 			cursor -= n;
130 			fseek(cf,(long)n,1);
131 			while(cursor--) if(n=(*putn)(' ')) return(n);
132 			return(cursor=0);
133 		}
134 		recpos += cursor;
135 		if(recpos >= curunit->url) err(errflag,F_EREREC,dfe)
136 	}
137 	fseek(cf,(long)cursor,1);
138 	return(cursor=0);
139 }
140 
141 /*
142 /*y_rev()
143 /*{	/*what about work done?*/
144 /*	if(curunit->url==1) return(0);
145 /*	while(recpos<curunit->url) (*putn)(' ');
146 /*	recpos=0;
147 /*	return(0);
148 /*}
149 /*
150 /*y_err()
151 /*{
152 /*	err(errflag, F_EREREC, dfe);
153 /*}
154 */
155 
156 y_rnew()
157 {	if(curunit->url != 1)
158 	{	fseek(cf,(long)curunit->url*(++recnum),0);
159 		recpos = reclen = cursor = 0;
160 	}
161 	return(OK);
162 }
163 
164 y_wnew()
165 {	if(curunit->url != 1)
166 	{	if(reclen > recpos)
167 		{	fseek(cf,(long)(reclen-recpos),1);
168 			recpos = reclen;
169 		}
170 		while(recpos < curunit->url) (*putn)(' ');
171 		recnum++;
172 		recpos = reclen = cursor = 0;
173 	}
174 	return(OK);
175 }
176 
177 y_rend()
178 {
179 	return(OK);
180 }
181 
182 y_wend()
183 {
184 	return(y_wnew());
185 }
186