xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 23073)
12492Sdlw /*
2*23073Skre  * Copyright (c) 1980 Regents of the University of California.
3*23073Skre  * All rights reserved.  The Berkeley software License Agreement
4*23073Skre  * specifies the terms and conditions for redistribution.
52492Sdlw  *
6*23073Skre  *	@(#)err.c	5.1	06/07/85
7*23073Skre  */
8*23073Skre 
9*23073Skre /*
1020984Slibs  * fatal(): i/o error routine
1120984Slibs  * flush_(): flush file buffer
122492Sdlw  */
132492Sdlw 
142492Sdlw #include <sys/types.h>
152492Sdlw #include <sys/stat.h>
162492Sdlw #include <signal.h>
1720984Slibs #include "fio.h"
182492Sdlw 
192492Sdlw /*
202492Sdlw  * global definitions
212492Sdlw  */
222492Sdlw 
2319920Slibs unit units[MXUNIT];	/*unit table*/
242492Sdlw flag reading;		/*1 if reading,		0 if writing*/
252492Sdlw flag external;		/*1 if external io,	0 if internal */
262492Sdlw flag sequential;	/*1 if sequential io,	0 if direct*/
272492Sdlw flag formatted;		/*1 if formatted io,	0 if unformatted, -1 if list*/
282492Sdlw char *fmtbuf, *icptr, *icend, *fmtptr;
292492Sdlw int (*doed)(),(*doned)();
302492Sdlw int (*doend)(),(*donewrec)(),(*dorevert)(),(*dotab)();
312492Sdlw int (*lioproc)();
322492Sdlw int (*getn)(),(*putn)(),(*ungetn)();	/*for formatted io*/
332492Sdlw FILE *cf;		/*current file structure*/
342492Sdlw unit *curunit;		/*current unit structure*/
352492Sdlw int lunit;		/*current logical unit*/
362492Sdlw char *lfname;		/*current filename*/
372492Sdlw int recpos;		/*place in current record*/
382492Sdlw ftnint recnum;		/* current record number */
392492Sdlw int reclen;		/* current record length */
402492Sdlw int cursor,scale;
412492Sdlw int radix;
422492Sdlw ioflag signit,tab,cplus,cblank,elist,errflag,endflag,lquit,l_first;
432492Sdlw flag leof;
442492Sdlw int lcount,line_len;
4512021Sdlw struct ioiflg ioiflg_;	/* initialization flags */
462492Sdlw 
472492Sdlw /*error messages*/
482492Sdlw 
492492Sdlw extern char *sys_errlist[];
502492Sdlw extern int sys_nerr;
512492Sdlw 
522765Sdlw extern char *f_errlist[];
532765Sdlw extern int f_nerr;
542492Sdlw 
552492Sdlw 
562492Sdlw fatal(n,s) char *s;
572492Sdlw {
582492Sdlw 	ftnint lu;
592492Sdlw 
602492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
612492Sdlw 		flush_(&lu);
622492Sdlw 	if(n<0)
632492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
642492Sdlw 	else if(n>=0 && n<sys_nerr)
652492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,sys_errlist[n]);
662765Sdlw 	else if(n>=F_ER && n<F_MAXERR)
672492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
682492Sdlw 	else
692492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
702492Sdlw 	if(external)
712492Sdlw 	{
722492Sdlw 		if(!lfname) switch (lunit)
732492Sdlw 		{	case STDERR: lfname = "stderr";
742492Sdlw 					break;
752492Sdlw 			case STDIN:  lfname = "stdin";
762492Sdlw 					break;
772492Sdlw 			case STDOUT: lfname = "stdout";
782492Sdlw 					break;
792492Sdlw 			default:     lfname = "";
802492Sdlw 		}
812492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
822492Sdlw 	}
832492Sdlw 	if (elist)
8420185Slibs 	{	fprintf(stderr,"lately: %s %s %s %s I/O\n",
852492Sdlw 			reading?"reading":"writing",
862492Sdlw 			sequential?"sequential":"direct",
872492Sdlw 			formatted>0?"formatted":(formatted<0?"list":"unformatted"),
882492Sdlw 			external?"external":"internal");
892492Sdlw 		if (formatted)
902492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
912492Sdlw 			if (external)
922492Sdlw 			{	if(reading && curunit->useek)
932492Sdlw 					prnt_ext();  /* print external data */
942492Sdlw 			}
952492Sdlw 			else prnt_int();	/* print internal array */
962492Sdlw 		}
972492Sdlw 	}
9820185Slibs 	f77_abort(n);
992492Sdlw }
1002492Sdlw 
10120984Slibs LOCAL
1022492Sdlw prnt_ext()
1032492Sdlw {	int ch;
1042492Sdlw 	int i=1;
1052492Sdlw 	long loc;
1062492Sdlw 	fprintf (stderr, "part of last data: ");
1072492Sdlw 	loc = ftell(curunit->ufd);
1082492Sdlw 	if(loc)
1092492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1102492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1113665Sdlw 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
1122492Sdlw 	}
1132492Sdlw 	fputc('|',stderr);
1143665Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
1152492Sdlw 	fputc('\n',stderr);
1162492Sdlw }
1172492Sdlw 
11820984Slibs LOCAL
1192492Sdlw prnt_int()
1202492Sdlw {	char *ep;
1212492Sdlw 	fprintf (stderr,"part of last string: ");
1222492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1233665Sdlw 	while (ep<icptr) ffputc(*ep++,stderr);
1242492Sdlw 	fputc('|',stderr);
1253665Sdlw 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
1262492Sdlw 	fputc('\n',stderr);
1272492Sdlw }
1282492Sdlw 
12920984Slibs LOCAL
1302492Sdlw prnt_fmt(n) int n;
1312492Sdlw {	int i; char *ep;
13217980Slibs 	fprintf(stderr, "format: ");
1332590Sdlw 	if(n==F_ERFMT)
1342492Sdlw 	{	i = fmtptr - fmtbuf;
13517980Slibs 		ep = fmtptr - (i<25?i:25);
13617980Slibs 		if(ep != fmtbuf) fprintf(stderr, "... ");
1372492Sdlw 		i = i + 5;
1382492Sdlw 	}
1392492Sdlw 	else
1402492Sdlw 	{	ep = fmtbuf;
1412492Sdlw 		i = 25;
1422492Sdlw 		fmtptr = fmtbuf - 1;
1432492Sdlw 	}
1442492Sdlw 	while(i && *ep)
1453665Sdlw 	{	ffputc((*ep==GLITCH)?'"':*ep,stderr);
1462492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1472492Sdlw 		ep++; i--;
1482492Sdlw 	}
14917980Slibs 	if(*ep) fprintf(stderr, " ...");
1502492Sdlw 	fputc('\n',stderr);
1512492Sdlw }
1522492Sdlw 
15320984Slibs LOCAL
1543665Sdlw ffputc(c, f)
1553665Sdlw int	c;
1563665Sdlw FILE	*f;
1573665Sdlw {
1583665Sdlw 	c &= 0177;
1593665Sdlw 	if (c < ' ' || c == 0177)
1603665Sdlw 	{
1613665Sdlw 		fputc('^', f);
1623665Sdlw 		c ^= 0100;
1633665Sdlw 	}
1643665Sdlw 	fputc(c, f);
1653665Sdlw }
1663665Sdlw 
16720984Slibs ftnint
16820984Slibs flush_(u) ftnint *u;
16911919Sdlw {
17020984Slibs 	FILE *F;
17120984Slibs 
17220984Slibs 	if(not_legal(*u))
17320984Slibs 		return(F_ERUNIT);
17420984Slibs 	F = units[*u].ufd;
17520984Slibs 	if(F)
17620984Slibs 		return(fflush(F));
17720984Slibs 	else
17820984Slibs 		return(F_ERNOPEN);
1792492Sdlw }
180