xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 20984)
12492Sdlw /*
2*20984Slibs char id_err[] = "@(#)err.c	1.16";
32492Sdlw  *
4*20984Slibs  * fatal(): i/o error routine
5*20984Slibs  * flush_(): flush file buffer
62492Sdlw  */
72492Sdlw 
82492Sdlw #include <sys/types.h>
92492Sdlw #include <sys/stat.h>
102492Sdlw #include <signal.h>
11*20984Slibs #include "fio.h"
122492Sdlw 
132492Sdlw /*
142492Sdlw  * global definitions
152492Sdlw  */
162492Sdlw 
1719920Slibs unit units[MXUNIT];	/*unit table*/
182492Sdlw flag reading;		/*1 if reading,		0 if writing*/
192492Sdlw flag external;		/*1 if external io,	0 if internal */
202492Sdlw flag sequential;	/*1 if sequential io,	0 if direct*/
212492Sdlw flag formatted;		/*1 if formatted io,	0 if unformatted, -1 if list*/
222492Sdlw char *fmtbuf, *icptr, *icend, *fmtptr;
232492Sdlw int (*doed)(),(*doned)();
242492Sdlw int (*doend)(),(*donewrec)(),(*dorevert)(),(*dotab)();
252492Sdlw int (*lioproc)();
262492Sdlw int (*getn)(),(*putn)(),(*ungetn)();	/*for formatted io*/
272492Sdlw FILE *cf;		/*current file structure*/
282492Sdlw unit *curunit;		/*current unit structure*/
292492Sdlw int lunit;		/*current logical unit*/
302492Sdlw char *lfname;		/*current filename*/
312492Sdlw int recpos;		/*place in current record*/
322492Sdlw ftnint recnum;		/* current record number */
332492Sdlw int reclen;		/* current record length */
342492Sdlw int cursor,scale;
352492Sdlw int radix;
362492Sdlw ioflag signit,tab,cplus,cblank,elist,errflag,endflag,lquit,l_first;
372492Sdlw flag leof;
382492Sdlw int lcount,line_len;
3912021Sdlw struct ioiflg ioiflg_;	/* initialization flags */
402492Sdlw 
412492Sdlw /*error messages*/
422492Sdlw 
432492Sdlw extern char *sys_errlist[];
442492Sdlw extern int sys_nerr;
452492Sdlw 
462765Sdlw extern char *f_errlist[];
472765Sdlw extern int f_nerr;
482492Sdlw 
492492Sdlw 
502492Sdlw fatal(n,s) char *s;
512492Sdlw {
522492Sdlw 	ftnint lu;
532492Sdlw 
542492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
552492Sdlw 		flush_(&lu);
562492Sdlw 	if(n<0)
572492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
582492Sdlw 	else if(n>=0 && n<sys_nerr)
592492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,sys_errlist[n]);
602765Sdlw 	else if(n>=F_ER && n<F_MAXERR)
612492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
622492Sdlw 	else
632492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
642492Sdlw 	if(external)
652492Sdlw 	{
662492Sdlw 		if(!lfname) switch (lunit)
672492Sdlw 		{	case STDERR: lfname = "stderr";
682492Sdlw 					break;
692492Sdlw 			case STDIN:  lfname = "stdin";
702492Sdlw 					break;
712492Sdlw 			case STDOUT: lfname = "stdout";
722492Sdlw 					break;
732492Sdlw 			default:     lfname = "";
742492Sdlw 		}
752492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
762492Sdlw 	}
772492Sdlw 	if (elist)
7820185Slibs 	{	fprintf(stderr,"lately: %s %s %s %s I/O\n",
792492Sdlw 			reading?"reading":"writing",
802492Sdlw 			sequential?"sequential":"direct",
812492Sdlw 			formatted>0?"formatted":(formatted<0?"list":"unformatted"),
822492Sdlw 			external?"external":"internal");
832492Sdlw 		if (formatted)
842492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
852492Sdlw 			if (external)
862492Sdlw 			{	if(reading && curunit->useek)
872492Sdlw 					prnt_ext();  /* print external data */
882492Sdlw 			}
892492Sdlw 			else prnt_int();	/* print internal array */
902492Sdlw 		}
912492Sdlw 	}
9220185Slibs 	f77_abort(n);
932492Sdlw }
942492Sdlw 
95*20984Slibs LOCAL
962492Sdlw prnt_ext()
972492Sdlw {	int ch;
982492Sdlw 	int i=1;
992492Sdlw 	long loc;
1002492Sdlw 	fprintf (stderr, "part of last data: ");
1012492Sdlw 	loc = ftell(curunit->ufd);
1022492Sdlw 	if(loc)
1032492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1042492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1053665Sdlw 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
1062492Sdlw 	}
1072492Sdlw 	fputc('|',stderr);
1083665Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
1092492Sdlw 	fputc('\n',stderr);
1102492Sdlw }
1112492Sdlw 
112*20984Slibs LOCAL
1132492Sdlw prnt_int()
1142492Sdlw {	char *ep;
1152492Sdlw 	fprintf (stderr,"part of last string: ");
1162492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1173665Sdlw 	while (ep<icptr) ffputc(*ep++,stderr);
1182492Sdlw 	fputc('|',stderr);
1193665Sdlw 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
1202492Sdlw 	fputc('\n',stderr);
1212492Sdlw }
1222492Sdlw 
123*20984Slibs LOCAL
1242492Sdlw prnt_fmt(n) int n;
1252492Sdlw {	int i; char *ep;
12617980Slibs 	fprintf(stderr, "format: ");
1272590Sdlw 	if(n==F_ERFMT)
1282492Sdlw 	{	i = fmtptr - fmtbuf;
12917980Slibs 		ep = fmtptr - (i<25?i:25);
13017980Slibs 		if(ep != fmtbuf) fprintf(stderr, "... ");
1312492Sdlw 		i = i + 5;
1322492Sdlw 	}
1332492Sdlw 	else
1342492Sdlw 	{	ep = fmtbuf;
1352492Sdlw 		i = 25;
1362492Sdlw 		fmtptr = fmtbuf - 1;
1372492Sdlw 	}
1382492Sdlw 	while(i && *ep)
1393665Sdlw 	{	ffputc((*ep==GLITCH)?'"':*ep,stderr);
1402492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1412492Sdlw 		ep++; i--;
1422492Sdlw 	}
14317980Slibs 	if(*ep) fprintf(stderr, " ...");
1442492Sdlw 	fputc('\n',stderr);
1452492Sdlw }
1462492Sdlw 
147*20984Slibs LOCAL
1483665Sdlw ffputc(c, f)
1493665Sdlw int	c;
1503665Sdlw FILE	*f;
1513665Sdlw {
1523665Sdlw 	c &= 0177;
1533665Sdlw 	if (c < ' ' || c == 0177)
1543665Sdlw 	{
1553665Sdlw 		fputc('^', f);
1563665Sdlw 		c ^= 0100;
1573665Sdlw 	}
1583665Sdlw 	fputc(c, f);
1593665Sdlw }
1603665Sdlw 
161*20984Slibs ftnint
162*20984Slibs flush_(u) ftnint *u;
16311919Sdlw {
164*20984Slibs 	FILE *F;
165*20984Slibs 
166*20984Slibs 	if(not_legal(*u))
167*20984Slibs 		return(F_ERUNIT);
168*20984Slibs 	F = units[*u].ufd;
169*20984Slibs 	if(F)
170*20984Slibs 		return(fflush(F));
171*20984Slibs 	else
172*20984Slibs 		return(F_ERNOPEN);
1732492Sdlw }
174