xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 47943)
1*47943Sbostic /*-
2*47943Sbostic  * Copyright (c) 1980 The Regents of the University of California.
3*47943Sbostic  * All rights reserved.
42492Sdlw  *
5*47943Sbostic  * %sccs.include.proprietary.c%
623073Skre  */
723073Skre 
8*47943Sbostic #ifndef lint
9*47943Sbostic static char sccsid[] = "@(#)err.c	5.4 (Berkeley) 04/12/91";
10*47943Sbostic #endif /* not lint */
11*47943Sbostic 
1223073Skre /*
1320984Slibs  * fatal(): i/o error routine
1420984Slibs  * flush_(): flush file buffer
152492Sdlw  */
162492Sdlw 
172492Sdlw #include <sys/types.h>
182492Sdlw #include <sys/stat.h>
192492Sdlw #include <signal.h>
2020984Slibs #include "fio.h"
212492Sdlw 
222492Sdlw /*
232492Sdlw  * global definitions
242492Sdlw  */
252492Sdlw 
2619920Slibs unit units[MXUNIT];	/*unit table*/
272492Sdlw flag reading;		/*1 if reading,		0 if writing*/
282492Sdlw flag external;		/*1 if external io,	0 if internal */
292492Sdlw flag sequential;	/*1 if sequential io,	0 if direct*/
3024101Sjerry flag formatted;		/*1 if formatted io,	0 if unformatted,
3124101Sjerry 				-1 if list directed, -2 if namelist */
322492Sdlw char *fmtbuf, *icptr, *icend, *fmtptr;
332492Sdlw int (*doed)(),(*doned)();
342492Sdlw int (*doend)(),(*donewrec)(),(*dorevert)(),(*dotab)();
352492Sdlw int (*lioproc)();
362492Sdlw int (*getn)(),(*putn)(),(*ungetn)();	/*for formatted io*/
372492Sdlw FILE *cf;		/*current file structure*/
382492Sdlw unit *curunit;		/*current unit structure*/
392492Sdlw int lunit;		/*current logical unit*/
402492Sdlw char *lfname;		/*current filename*/
412492Sdlw int recpos;		/*place in current record*/
422492Sdlw ftnint recnum;		/* current record number */
432492Sdlw int reclen;		/* current record length */
442492Sdlw int cursor,scale;
452492Sdlw int radix;
462492Sdlw ioflag signit,tab,cplus,cblank,elist,errflag,endflag,lquit,l_first;
472492Sdlw flag leof;
482492Sdlw int lcount,line_len;
4912021Sdlw struct ioiflg ioiflg_;	/* initialization flags */
502492Sdlw 
512492Sdlw /*error messages*/
522492Sdlw 
532492Sdlw extern int sys_nerr;
542492Sdlw 
552765Sdlw extern char *f_errlist[];
562765Sdlw extern int f_nerr;
572492Sdlw 
582492Sdlw 
fatal(n,s)592492Sdlw fatal(n,s) char *s;
602492Sdlw {
612492Sdlw 	ftnint lu;
6242410Sbostic 	char *strerror();
632492Sdlw 
642492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
652492Sdlw 		flush_(&lu);
662492Sdlw 	if(n<0)
672492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
682492Sdlw 	else if(n>=0 && n<sys_nerr)
6942410Sbostic 		fprintf(stderr,"%s: [%d] %s\n",s,n, strerror(n));
702765Sdlw 	else if(n>=F_ER && n<F_MAXERR)
712492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
722492Sdlw 	else
732492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
742492Sdlw 	if(external)
752492Sdlw 	{
762492Sdlw 		if(!lfname) switch (lunit)
772492Sdlw 		{	case STDERR: lfname = "stderr";
782492Sdlw 					break;
792492Sdlw 			case STDIN:  lfname = "stdin";
802492Sdlw 					break;
812492Sdlw 			case STDOUT: lfname = "stdout";
822492Sdlw 					break;
832492Sdlw 			default:     lfname = "";
842492Sdlw 		}
852492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
862492Sdlw 	}
872492Sdlw 	if (elist)
8820185Slibs 	{	fprintf(stderr,"lately: %s %s %s %s I/O\n",
892492Sdlw 			reading?"reading":"writing",
902492Sdlw 			sequential?"sequential":"direct",
9124101Sjerry 			formatted>0?"formatted":(formatted==0?"unformatted":
9224101Sjerry 				(formatted==LISTDIRECTED?"list":"namelist")),
932492Sdlw 			external?"external":"internal");
942492Sdlw 		if (formatted)
952492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
962492Sdlw 			if (external)
972492Sdlw 			{	if(reading && curunit->useek)
982492Sdlw 					prnt_ext();  /* print external data */
992492Sdlw 			}
1002492Sdlw 			else prnt_int();	/* print internal array */
1012492Sdlw 		}
1022492Sdlw 	}
10320185Slibs 	f77_abort(n);
1042492Sdlw }
1052492Sdlw 
10620984Slibs LOCAL
prnt_ext()1072492Sdlw prnt_ext()
1082492Sdlw {	int ch;
1092492Sdlw 	int i=1;
1102492Sdlw 	long loc;
1112492Sdlw 	fprintf (stderr, "part of last data: ");
1122492Sdlw 	loc = ftell(curunit->ufd);
1132492Sdlw 	if(loc)
1142492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1152492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1163665Sdlw 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
1172492Sdlw 	}
1182492Sdlw 	fputc('|',stderr);
1193665Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
1202492Sdlw 	fputc('\n',stderr);
1212492Sdlw }
1222492Sdlw 
12320984Slibs LOCAL
prnt_int()1242492Sdlw prnt_int()
1252492Sdlw {	char *ep;
1262492Sdlw 	fprintf (stderr,"part of last string: ");
1272492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1283665Sdlw 	while (ep<icptr) ffputc(*ep++,stderr);
1292492Sdlw 	fputc('|',stderr);
1303665Sdlw 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
1312492Sdlw 	fputc('\n',stderr);
1322492Sdlw }
1332492Sdlw 
13420984Slibs LOCAL
prnt_fmt(n)1352492Sdlw prnt_fmt(n) int n;
1362492Sdlw {	int i; char *ep;
13717980Slibs 	fprintf(stderr, "format: ");
1382590Sdlw 	if(n==F_ERFMT)
1392492Sdlw 	{	i = fmtptr - fmtbuf;
14017980Slibs 		ep = fmtptr - (i<25?i:25);
14117980Slibs 		if(ep != fmtbuf) fprintf(stderr, "... ");
1422492Sdlw 		i = i + 5;
1432492Sdlw 	}
1442492Sdlw 	else
1452492Sdlw 	{	ep = fmtbuf;
1462492Sdlw 		i = 25;
1472492Sdlw 		fmtptr = fmtbuf - 1;
1482492Sdlw 	}
1492492Sdlw 	while(i && *ep)
1503665Sdlw 	{	ffputc((*ep==GLITCH)?'"':*ep,stderr);
1512492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1522492Sdlw 		ep++; i--;
1532492Sdlw 	}
15417980Slibs 	if(*ep) fprintf(stderr, " ...");
1552492Sdlw 	fputc('\n',stderr);
1562492Sdlw }
1572492Sdlw 
15820984Slibs LOCAL
ffputc(c,f)1593665Sdlw ffputc(c, f)
1603665Sdlw int	c;
1613665Sdlw FILE	*f;
1623665Sdlw {
1633665Sdlw 	c &= 0177;
1643665Sdlw 	if (c < ' ' || c == 0177)
1653665Sdlw 	{
1663665Sdlw 		fputc('^', f);
1673665Sdlw 		c ^= 0100;
1683665Sdlw 	}
1693665Sdlw 	fputc(c, f);
1703665Sdlw }
1713665Sdlw 
17220984Slibs ftnint
flush_(u)17320984Slibs flush_(u) ftnint *u;
17411919Sdlw {
17520984Slibs 	FILE *F;
17620984Slibs 
17720984Slibs 	if(not_legal(*u))
17820984Slibs 		return(F_ERUNIT);
17920984Slibs 	F = units[*u].ufd;
18020984Slibs 	if(F)
18120984Slibs 		return(fflush(F));
18220984Slibs 	else
18320984Slibs 		return(F_ERNOPEN);
1842492Sdlw }
185