xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 19920)
12492Sdlw /*
2*19920Slibs char id_err[] = "@(#)err.c	1.14";
32492Sdlw  *
42492Sdlw  * file i/o error and initialization routines
52492Sdlw  */
62492Sdlw 
72492Sdlw #include <sys/types.h>
82492Sdlw #include <sys/stat.h>
92492Sdlw #include <signal.h>
102492Sdlw #include "fiodefs.h"
112492Sdlw 
122492Sdlw /*
132492Sdlw  * global definitions
142492Sdlw  */
152492Sdlw 
162492Sdlw char *tmplate = "tmp.FXXXXXX";	/* scratch file template */
172492Sdlw char *fortfile = "fort.%d";	/* default file template */
182492Sdlw 
19*19920Slibs unit units[MXUNIT];	/*unit table*/
202492Sdlw flag reading;		/*1 if reading,		0 if writing*/
212492Sdlw flag external;		/*1 if external io,	0 if internal */
222492Sdlw flag sequential;	/*1 if sequential io,	0 if direct*/
232492Sdlw flag formatted;		/*1 if formatted io,	0 if unformatted, -1 if list*/
242492Sdlw char *fmtbuf, *icptr, *icend, *fmtptr;
252492Sdlw int (*doed)(),(*doned)();
262492Sdlw int (*doend)(),(*donewrec)(),(*dorevert)(),(*dotab)();
272492Sdlw int (*lioproc)();
282492Sdlw int (*getn)(),(*putn)(),(*ungetn)();	/*for formatted io*/
292492Sdlw icilist *svic;		/* active internal io list */
302492Sdlw FILE *cf;		/*current file structure*/
312492Sdlw unit *curunit;		/*current unit structure*/
322492Sdlw int lunit;		/*current logical unit*/
332492Sdlw char *lfname;		/*current filename*/
342492Sdlw int recpos;		/*place in current record*/
352492Sdlw ftnint recnum;		/* current record number */
362492Sdlw int reclen;		/* current record length */
372492Sdlw int cursor,scale;
382492Sdlw int radix;
392492Sdlw ioflag signit,tab,cplus,cblank,elist,errflag,endflag,lquit,l_first;
402492Sdlw flag leof;
412492Sdlw int lcount,line_len;
4212021Sdlw struct ioiflg ioiflg_;	/* initialization flags */
432492Sdlw 
442492Sdlw /*error messages*/
452492Sdlw 
462492Sdlw extern char *sys_errlist[];
472492Sdlw extern int sys_nerr;
482492Sdlw 
492765Sdlw extern char *f_errlist[];
502765Sdlw extern int f_nerr;
512492Sdlw 
522492Sdlw 
532492Sdlw fatal(n,s) char *s;
542492Sdlw {
552492Sdlw 	ftnint lu;
562492Sdlw 
572492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
582492Sdlw 		flush_(&lu);
592492Sdlw 	if(n<0)
602492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
612492Sdlw 	else if(n>=0 && n<sys_nerr)
622492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,sys_errlist[n]);
632765Sdlw 	else if(n>=F_ER && n<F_MAXERR)
642492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
652492Sdlw 	else
662492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
672492Sdlw 	if(external)
682492Sdlw 	{
692492Sdlw 		if(!lfname) switch (lunit)
702492Sdlw 		{	case STDERR: lfname = "stderr";
712492Sdlw 					break;
722492Sdlw 			case STDIN:  lfname = "stdin";
732492Sdlw 					break;
742492Sdlw 			case STDOUT: lfname = "stdout";
752492Sdlw 					break;
762492Sdlw 			default:     lfname = "";
772492Sdlw 		}
782492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
792492Sdlw 	}
802492Sdlw 	if (elist)
812492Sdlw 	{	fprintf(stderr,"lately: %s %s %s %s IO\n",
822492Sdlw 			reading?"reading":"writing",
832492Sdlw 			sequential?"sequential":"direct",
842492Sdlw 			formatted>0?"formatted":(formatted<0?"list":"unformatted"),
852492Sdlw 			external?"external":"internal");
862492Sdlw 		if (formatted)
872492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
882492Sdlw 			if (external)
892492Sdlw 			{	if(reading && curunit->useek)
902492Sdlw 					prnt_ext();  /* print external data */
912492Sdlw 			}
922492Sdlw 			else prnt_int();	/* print internal array */
932492Sdlw 		}
942492Sdlw 	}
952492Sdlw 	f_exit();
962492Sdlw 	_cleanup();
9714844Sdlw #if	vax
9814844Sdlw 	signal(SIGILL, SIG_DFL);
9915300Sdlw 	sigsetmask(0);
10014844Sdlw #else	pdp11
10114844Sdlw 	signal(SIGIOT, SIG_DFL);
10214844Sdlw #endif
1032492Sdlw 	abort();
1042492Sdlw }
1052492Sdlw 
1062492Sdlw prnt_ext()
1072492Sdlw {	int ch;
1082492Sdlw 	int i=1;
1092492Sdlw 	long loc;
1102492Sdlw 	fprintf (stderr, "part of last data: ");
1112492Sdlw 	loc = ftell(curunit->ufd);
1122492Sdlw 	if(loc)
1132492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1142492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1153665Sdlw 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
1162492Sdlw 	}
1172492Sdlw 	fputc('|',stderr);
1183665Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
1192492Sdlw 	fputc('\n',stderr);
1202492Sdlw }
1212492Sdlw 
1222492Sdlw prnt_int()
1232492Sdlw {	char *ep;
1242492Sdlw 	fprintf (stderr,"part of last string: ");
1252492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1263665Sdlw 	while (ep<icptr) ffputc(*ep++,stderr);
1272492Sdlw 	fputc('|',stderr);
1283665Sdlw 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
1292492Sdlw 	fputc('\n',stderr);
1302492Sdlw }
1312492Sdlw 
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 
1553665Sdlw ffputc(c, f)
1563665Sdlw int	c;
1573665Sdlw FILE	*f;
1583665Sdlw {
1593665Sdlw 	c &= 0177;
1603665Sdlw 	if (c < ' ' || c == 0177)
1613665Sdlw 	{
1623665Sdlw 		fputc('^', f);
1633665Sdlw 		c ^= 0100;
1643665Sdlw 	}
1653665Sdlw 	fputc(c, f);
1663665Sdlw }
1673665Sdlw 
1682492Sdlw /*initialization routine*/
1692492Sdlw f_init()
17011919Sdlw {
17112006Sdlw 	ini_std(STDERR, stderr, WRITE);
17212006Sdlw 	ini_std(STDIN, stdin, READ);
17312006Sdlw 	ini_std(STDOUT, stdout, WRITE);
17418015Slibs 	setlinebuf(stderr);
1752492Sdlw }
1762492Sdlw 
177