xref: /original-bsd/usr.bin/f77/libI77/dfe.c (revision fbed46ce)
1 /*
2 char id_dfe[] = "@(#)dfe.c	1.3";
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 rdfe[] = "read dfe";
13 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(fmtbuf)) 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(fmtbuf)) 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 c_dfe(a,flag) cilist *a;
61 {	int n;
62 	sequential = NO;
63 	external = formatted = FORMATTED;
64 	lfname = NULL;
65 	elist = NO;
66 	cursor=scale=recpos=reclen=0;
67 	radix = 10;
68 	signit = YES;
69 	fmtbuf = a->cifmt;
70 	errflag = a->cierr;
71 	endflag = a->ciend;
72 	lunit = a->ciunit;
73 	if(not_legal(lunit)) err(errflag,F_ERUNIT,rdfe+5);
74 	curunit = &units[lunit];
75 	if(!curunit->ufd && (n=fk_open(flag,DIR,FMT,(ftnint)lunit)))
76 		err(errflag,n,rdfe+5)
77 	cf = curunit->ufd;
78 	elist = YES;
79 	lfname = curunit->ufnm;
80 	if(!curunit->ufmt) err(errflag,F_ERNOFIO,rdfe+5)
81 	if(!curunit->useek || !curunit->url) err(errflag,F_ERNODIO,rdfe+5)
82 	recnum = a->cirec - 1;
83 	fseek(cf, (long)curunit->url * recnum, 0);
84 	cblank = curunit->ublnk;
85 	cplus = NO;
86 	return(OK);
87 }
88 
89 y_getc()
90 {
91 	int ch;
92 	if(curunit->uend) return(EOF);
93 	if(curunit->url==1 || recpos++ < curunit->url)
94 	{
95 		if((ch=getc(cf))!=EOF)
96 		{
97 				return(ch);
98 		}
99 		if(feof(cf))
100 		{
101 			curunit->uend = YES;
102 			return(EOF);
103 		}
104 		err(errflag,errno,rdfe);
105 	}
106 	else return(' ');
107 }
108 
109 y_putc(c)
110 {
111 	if(curunit->url!=1 && recpos++ >= curunit->url) err(errflag,F_EREREC,wdfe)
112 	putc(c,cf);
113 	return(OK);
114 }
115 
116 y_tab()
117 {	int n;
118 	if(curunit->url==1)
119 	{
120 		if(cursor < 0 && -cursor > ftell(cf)) return(F_ERBREC);
121 	}
122 	else
123 	{	if(reclen < recpos) reclen = recpos;
124 		if((recpos + cursor) < 0) return(F_ERBREC);
125 		n = reclen - recpos;		/* n >= 0 */
126 		if(!reading && (cursor-n) > 0)
127 		{	recpos = reclen;
128 			cursor -= n;
129 			fseek(cf,(long)n,1);
130 			while(cursor--) if(n=(*putn)(' ')) return(n);
131 			return(cursor=0);
132 		}
133 		recpos += cursor;
134 		if(recpos >= curunit->url) err(errflag,F_EREREC,rdfe+5)
135 	}
136 	fseek(cf,(long)cursor,1);
137 	return(cursor=0);
138 }
139 
140 /*
141 /*y_rev()
142 /*{	/*what about work done?*/
143 /*	if(curunit->url==1) return(0);
144 /*	while(recpos<curunit->url) (*putn)(' ');
145 /*	recpos=0;
146 /*	return(0);
147 /*}
148 /*
149 /*y_err()
150 /*{
151 /*	err(errflag, F_EREREC, rdfe+5);
152 /*}
153 */
154 
155 y_rnew()
156 {	if(curunit->url != 1)
157 	{	fseek(cf,(long)curunit->url*(++recnum),0);
158 		recpos = reclen = cursor = 0;
159 	}
160 	return(OK);
161 }
162 
163 y_wnew()
164 {	if(curunit->url != 1)
165 	{	if(reclen > recpos)
166 		{	fseek(cf,(long)(reclen-recpos),1);
167 			recpos = reclen;
168 		}
169 		while(recpos < curunit->url) (*putn)(' ');
170 		recnum++;
171 		recpos = reclen = cursor = 0;
172 	}
173 	return(OK);
174 }
175 
176 y_rend()
177 {
178 	return(OK);
179 }
180 
181 y_wend()
182 {
183 	return(y_wnew());
184 }
185