xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 42410)
12492Sdlw /*
223073Skre  * Copyright (c) 1980 Regents of the University of California.
323073Skre  * All rights reserved.  The Berkeley software License Agreement
423073Skre  * specifies the terms and conditions for redistribution.
52492Sdlw  *
6*42410Sbostic  *	@(#)err.c	5.3	05/28/90
723073Skre  */
823073Skre 
923073Skre /*
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*/
2724101Sjerry flag formatted;		/*1 if formatted io,	0 if unformatted,
2824101Sjerry 				-1 if list directed, -2 if namelist */
292492Sdlw char *fmtbuf, *icptr, *icend, *fmtptr;
302492Sdlw int (*doed)(),(*doned)();
312492Sdlw int (*doend)(),(*donewrec)(),(*dorevert)(),(*dotab)();
322492Sdlw int (*lioproc)();
332492Sdlw int (*getn)(),(*putn)(),(*ungetn)();	/*for formatted io*/
342492Sdlw FILE *cf;		/*current file structure*/
352492Sdlw unit *curunit;		/*current unit structure*/
362492Sdlw int lunit;		/*current logical unit*/
372492Sdlw char *lfname;		/*current filename*/
382492Sdlw int recpos;		/*place in current record*/
392492Sdlw ftnint recnum;		/* current record number */
402492Sdlw int reclen;		/* current record length */
412492Sdlw int cursor,scale;
422492Sdlw int radix;
432492Sdlw ioflag signit,tab,cplus,cblank,elist,errflag,endflag,lquit,l_first;
442492Sdlw flag leof;
452492Sdlw int lcount,line_len;
4612021Sdlw struct ioiflg ioiflg_;	/* initialization flags */
472492Sdlw 
482492Sdlw /*error messages*/
492492Sdlw 
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;
59*42410Sbostic 	char *strerror();
602492Sdlw 
612492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
622492Sdlw 		flush_(&lu);
632492Sdlw 	if(n<0)
642492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
652492Sdlw 	else if(n>=0 && n<sys_nerr)
66*42410Sbostic 		fprintf(stderr,"%s: [%d] %s\n",s,n, strerror(n));
672765Sdlw 	else if(n>=F_ER && n<F_MAXERR)
682492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
692492Sdlw 	else
702492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
712492Sdlw 	if(external)
722492Sdlw 	{
732492Sdlw 		if(!lfname) switch (lunit)
742492Sdlw 		{	case STDERR: lfname = "stderr";
752492Sdlw 					break;
762492Sdlw 			case STDIN:  lfname = "stdin";
772492Sdlw 					break;
782492Sdlw 			case STDOUT: lfname = "stdout";
792492Sdlw 					break;
802492Sdlw 			default:     lfname = "";
812492Sdlw 		}
822492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
832492Sdlw 	}
842492Sdlw 	if (elist)
8520185Slibs 	{	fprintf(stderr,"lately: %s %s %s %s I/O\n",
862492Sdlw 			reading?"reading":"writing",
872492Sdlw 			sequential?"sequential":"direct",
8824101Sjerry 			formatted>0?"formatted":(formatted==0?"unformatted":
8924101Sjerry 				(formatted==LISTDIRECTED?"list":"namelist")),
902492Sdlw 			external?"external":"internal");
912492Sdlw 		if (formatted)
922492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
932492Sdlw 			if (external)
942492Sdlw 			{	if(reading && curunit->useek)
952492Sdlw 					prnt_ext();  /* print external data */
962492Sdlw 			}
972492Sdlw 			else prnt_int();	/* print internal array */
982492Sdlw 		}
992492Sdlw 	}
10020185Slibs 	f77_abort(n);
1012492Sdlw }
1022492Sdlw 
10320984Slibs LOCAL
1042492Sdlw prnt_ext()
1052492Sdlw {	int ch;
1062492Sdlw 	int i=1;
1072492Sdlw 	long loc;
1082492Sdlw 	fprintf (stderr, "part of last data: ");
1092492Sdlw 	loc = ftell(curunit->ufd);
1102492Sdlw 	if(loc)
1112492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1122492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1133665Sdlw 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
1142492Sdlw 	}
1152492Sdlw 	fputc('|',stderr);
1163665Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
1172492Sdlw 	fputc('\n',stderr);
1182492Sdlw }
1192492Sdlw 
12020984Slibs LOCAL
1212492Sdlw prnt_int()
1222492Sdlw {	char *ep;
1232492Sdlw 	fprintf (stderr,"part of last string: ");
1242492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1253665Sdlw 	while (ep<icptr) ffputc(*ep++,stderr);
1262492Sdlw 	fputc('|',stderr);
1273665Sdlw 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
1282492Sdlw 	fputc('\n',stderr);
1292492Sdlw }
1302492Sdlw 
13120984Slibs LOCAL
1322492Sdlw prnt_fmt(n) int n;
1332492Sdlw {	int i; char *ep;
13417980Slibs 	fprintf(stderr, "format: ");
1352590Sdlw 	if(n==F_ERFMT)
1362492Sdlw 	{	i = fmtptr - fmtbuf;
13717980Slibs 		ep = fmtptr - (i<25?i:25);
13817980Slibs 		if(ep != fmtbuf) fprintf(stderr, "... ");
1392492Sdlw 		i = i + 5;
1402492Sdlw 	}
1412492Sdlw 	else
1422492Sdlw 	{	ep = fmtbuf;
1432492Sdlw 		i = 25;
1442492Sdlw 		fmtptr = fmtbuf - 1;
1452492Sdlw 	}
1462492Sdlw 	while(i && *ep)
1473665Sdlw 	{	ffputc((*ep==GLITCH)?'"':*ep,stderr);
1482492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1492492Sdlw 		ep++; i--;
1502492Sdlw 	}
15117980Slibs 	if(*ep) fprintf(stderr, " ...");
1522492Sdlw 	fputc('\n',stderr);
1532492Sdlw }
1542492Sdlw 
15520984Slibs LOCAL
1563665Sdlw ffputc(c, f)
1573665Sdlw int	c;
1583665Sdlw FILE	*f;
1593665Sdlw {
1603665Sdlw 	c &= 0177;
1613665Sdlw 	if (c < ' ' || c == 0177)
1623665Sdlw 	{
1633665Sdlw 		fputc('^', f);
1643665Sdlw 		c ^= 0100;
1653665Sdlw 	}
1663665Sdlw 	fputc(c, f);
1673665Sdlw }
1683665Sdlw 
16920984Slibs ftnint
17020984Slibs flush_(u) ftnint *u;
17111919Sdlw {
17220984Slibs 	FILE *F;
17320984Slibs 
17420984Slibs 	if(not_legal(*u))
17520984Slibs 		return(F_ERUNIT);
17620984Slibs 	F = units[*u].ufd;
17720984Slibs 	if(F)
17820984Slibs 		return(fflush(F));
17920984Slibs 	else
18020984Slibs 		return(F_ERNOPEN);
1812492Sdlw }
182