xref: /csrg-svn/usr.bin/f77/libI77/dfe.c (revision 17967)
12486Sdlw /*
2*17967Slibs char id_dfe[] = "@(#)dfe.c	1.5";
32486Sdlw  *
42486Sdlw  * direct formatted external i/o
52486Sdlw  */
62486Sdlw 
72486Sdlw #include "fio.h"
82486Sdlw 
92486Sdlw extern int rd_ed(),rd_ned(),w_ed(),w_ned();
102486Sdlw int y_getc(),y_putc(),y_rnew(),y_wnew(),y_tab();
112486Sdlw 
124113Sdlw char rdfe[] = "read dfe";
134113Sdlw char wdfe[] = "write dfe";
142486Sdlw 
152486Sdlw s_rdfe(a) cilist *a;
162486Sdlw {
172486Sdlw 	int n;
182486Sdlw 	reading = YES;
192486Sdlw 	if(n=c_dfe(a,READ)) return(n);
204113Sdlw 	if(curunit->uwrt && ! nowreading(curunit)) err(errflag, errno, rdfe)
212486Sdlw 	getn = y_getc;
222486Sdlw 	doed = rd_ed;
232486Sdlw 	doned = rd_ned;
242486Sdlw 	dotab = y_tab;
252486Sdlw 	dorevert = doend = donewrec = y_rnew;
26*17967Slibs 	if(pars_f()) err(errflag,F_ERFMT,rdfe)
272486Sdlw 	fmt_bg();
282486Sdlw 	return(OK);
292486Sdlw }
302486Sdlw 
312486Sdlw s_wdfe(a) cilist *a;
322486Sdlw {
332486Sdlw 	int n;
342486Sdlw 	reading = NO;
352486Sdlw 	if(n=c_dfe(a,WRITE)) return(n);
362486Sdlw 	curunit->uend = NO;
374113Sdlw 	if(!curunit->uwrt && ! nowwriting(curunit)) err(errflag, errno, wdfe)
382486Sdlw 	putn = y_putc;
392486Sdlw 	doed = w_ed;
402486Sdlw 	doned = w_ned;
412486Sdlw 	dotab = y_tab;
422486Sdlw 	dorevert = doend = donewrec = y_wnew;
43*17967Slibs 	if(pars_f()) err(errflag,F_ERFMT,wdfe)
442486Sdlw 	fmt_bg();
452486Sdlw 	return(OK);
462486Sdlw }
472486Sdlw 
482486Sdlw e_rdfe()
492486Sdlw {
502486Sdlw 	en_fio();
512486Sdlw 	return(OK);
522486Sdlw }
532486Sdlw 
542486Sdlw e_wdfe()
552486Sdlw {
562486Sdlw 	en_fio();
572486Sdlw 	return(OK);
582486Sdlw }
592486Sdlw 
602486Sdlw c_dfe(a,flag) cilist *a;
612486Sdlw {	int n;
622486Sdlw 	sequential = NO;
632486Sdlw 	external = formatted = FORMATTED;
642486Sdlw 	lfname = NULL;
652486Sdlw 	elist = NO;
662486Sdlw 	cursor=scale=recpos=reclen=0;
672486Sdlw 	radix = 10;
682486Sdlw 	signit = YES;
692486Sdlw 	fmtbuf = a->cifmt;
702486Sdlw 	errflag = a->cierr;
712486Sdlw 	endflag = a->ciend;
722486Sdlw 	lunit = a->ciunit;
734113Sdlw 	if(not_legal(lunit)) err(errflag,F_ERUNIT,rdfe+5);
742486Sdlw 	curunit = &units[lunit];
752486Sdlw 	if(!curunit->ufd && (n=fk_open(flag,DIR,FMT,(ftnint)lunit)))
764113Sdlw 		err(errflag,n,rdfe+5)
772486Sdlw 	cf = curunit->ufd;
782486Sdlw 	elist = YES;
792486Sdlw 	lfname = curunit->ufnm;
804113Sdlw 	if(!curunit->ufmt) err(errflag,F_ERNOFIO,rdfe+5)
814113Sdlw 	if(!curunit->useek || !curunit->url) err(errflag,F_ERNODIO,rdfe+5)
822486Sdlw 	recnum = a->cirec - 1;
832486Sdlw 	fseek(cf, (long)curunit->url * recnum, 0);
842486Sdlw 	cblank = curunit->ublnk;
852486Sdlw 	cplus = NO;
862486Sdlw 	return(OK);
872486Sdlw }
882486Sdlw 
892486Sdlw y_getc()
902486Sdlw {
912486Sdlw 	int ch;
922486Sdlw 	if(curunit->uend) return(EOF);
932486Sdlw 	if(curunit->url==1 || recpos++ < curunit->url)
942486Sdlw 	{
952486Sdlw 		if((ch=getc(cf))!=EOF)
962486Sdlw 		{
972486Sdlw 				return(ch);
982486Sdlw 		}
992486Sdlw 		if(feof(cf))
1002486Sdlw 		{
1012486Sdlw 			curunit->uend = YES;
1022486Sdlw 			return(EOF);
1032486Sdlw 		}
1042486Sdlw 		err(errflag,errno,rdfe);
1052486Sdlw 	}
1062486Sdlw 	else return(' ');
1072486Sdlw }
1082486Sdlw 
1092486Sdlw y_putc(c)
1102486Sdlw {
1112584Sdlw 	if(curunit->url!=1 && recpos++ >= curunit->url) err(errflag,F_EREREC,wdfe)
1122486Sdlw 	putc(c,cf);
1132486Sdlw 	return(OK);
1142486Sdlw }
1152486Sdlw 
1162486Sdlw y_tab()
1172486Sdlw {	int n;
1182486Sdlw 	if(curunit->url==1)
1192486Sdlw 	{
12012046Sdlw 		if(cursor < 0 && -cursor > ftell(cf)) rewind(cf);
12112046Sdlw 		else	fseek(cf,(long)cursor,1);
12212046Sdlw 		return(cursor=0);
1232486Sdlw 	}
1242486Sdlw 	else
1252486Sdlw 	{	if(reclen < recpos) reclen = recpos;
12612046Sdlw 		if((recpos + cursor) < 0) cursor = -recpos;	/* BOR */
1272486Sdlw 		n = reclen - recpos;		/* n >= 0 */
1282486Sdlw 		if(!reading && (cursor-n) > 0)
1292486Sdlw 		{	recpos = reclen;
1302486Sdlw 			cursor -= n;
1312486Sdlw 			fseek(cf,(long)n,1);
1322486Sdlw 			while(cursor--) if(n=(*putn)(' ')) return(n);
1332486Sdlw 			return(cursor=0);
1342486Sdlw 		}
1352486Sdlw 		recpos += cursor;
1364113Sdlw 		if(recpos >= curunit->url) err(errflag,F_EREREC,rdfe+5)
1372486Sdlw 	}
1382486Sdlw 	fseek(cf,(long)cursor,1);
1392486Sdlw 	return(cursor=0);
1402486Sdlw }
1412486Sdlw 
1422486Sdlw /*
1432486Sdlw /*y_rev()
1442486Sdlw /*{	/*what about work done?*/
1452486Sdlw /*	if(curunit->url==1) return(0);
1462486Sdlw /*	while(recpos<curunit->url) (*putn)(' ');
1472486Sdlw /*	recpos=0;
1482486Sdlw /*	return(0);
1492486Sdlw /*}
1502486Sdlw /*
1512486Sdlw /*y_err()
1522486Sdlw /*{
1534113Sdlw /*	err(errflag, F_EREREC, rdfe+5);
1542486Sdlw /*}
1552486Sdlw */
1562486Sdlw 
1572486Sdlw y_rnew()
1582486Sdlw {	if(curunit->url != 1)
1592486Sdlw 	{	fseek(cf,(long)curunit->url*(++recnum),0);
1602486Sdlw 		recpos = reclen = cursor = 0;
1612486Sdlw 	}
1622486Sdlw 	return(OK);
1632486Sdlw }
1642486Sdlw 
1652486Sdlw y_wnew()
1662486Sdlw {	if(curunit->url != 1)
1672486Sdlw 	{	if(reclen > recpos)
1682486Sdlw 		{	fseek(cf,(long)(reclen-recpos),1);
1692486Sdlw 			recpos = reclen;
1702486Sdlw 		}
1712486Sdlw 		while(recpos < curunit->url) (*putn)(' ');
1722486Sdlw 		recnum++;
1732486Sdlw 		recpos = reclen = cursor = 0;
1742486Sdlw 	}
1752486Sdlw 	return(OK);
1762486Sdlw }
1772486Sdlw 
1782486Sdlw y_rend()
1792486Sdlw {
1802486Sdlw 	return(OK);
1812486Sdlw }
1822486Sdlw 
1832486Sdlw y_wend()
1842486Sdlw {
1852486Sdlw 	return(y_wnew());
1862486Sdlw }
187