xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 12021)
12492Sdlw /*
2*12021Sdlw char id_err[] = "@(#)err.c	1.9";
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 
192492Sdlw unit units[MXUNIT] = 0;	/*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;
42*12021Sdlw 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();
972492Sdlw 	abort();
982492Sdlw }
992492Sdlw 
1002492Sdlw prnt_ext()
1012492Sdlw {	int ch;
1022492Sdlw 	int i=1;
1032492Sdlw 	long loc;
1042492Sdlw 	fprintf (stderr, "part of last data: ");
1052492Sdlw 	loc = ftell(curunit->ufd);
1062492Sdlw 	if(loc)
1072492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1082492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1093665Sdlw 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
1102492Sdlw 	}
1112492Sdlw 	fputc('|',stderr);
1123665Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
1132492Sdlw 	fputc('\n',stderr);
1142492Sdlw }
1152492Sdlw 
1162492Sdlw prnt_int()
1172492Sdlw {	char *ep;
1182492Sdlw 	fprintf (stderr,"part of last string: ");
1192492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1203665Sdlw 	while (ep<icptr) ffputc(*ep++,stderr);
1212492Sdlw 	fputc('|',stderr);
1223665Sdlw 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
1232492Sdlw 	fputc('\n',stderr);
1242492Sdlw }
1252492Sdlw 
1262492Sdlw prnt_fmt(n) int n;
1272492Sdlw {	int i; char *ep;
1282492Sdlw 	fprintf(stderr, "part of last format: ");
1292590Sdlw 	if(n==F_ERFMT)
1302492Sdlw 	{	i = fmtptr - fmtbuf;
1312492Sdlw 		ep = fmtptr - (i<20?i:20);
1322492Sdlw 		i = i + 5;
1332492Sdlw 	}
1342492Sdlw 	else
1352492Sdlw 	{	ep = fmtbuf;
1362492Sdlw 		i = 25;
1372492Sdlw 		fmtptr = fmtbuf - 1;
1382492Sdlw 	}
1392492Sdlw 	while(i && *ep)
1403665Sdlw 	{	ffputc((*ep==GLITCH)?'"':*ep,stderr);
1412492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1422492Sdlw 		ep++; i--;
1432492Sdlw 	}
1442492Sdlw 	fputc('\n',stderr);
1452492Sdlw }
1462492Sdlw 
1473665Sdlw ffputc(c, f)
1483665Sdlw int	c;
1493665Sdlw FILE	*f;
1503665Sdlw {
1513665Sdlw 	c &= 0177;
1523665Sdlw 	if (c < ' ' || c == 0177)
1533665Sdlw 	{
1543665Sdlw 		fputc('^', f);
1553665Sdlw 		c ^= 0100;
1563665Sdlw 	}
1573665Sdlw 	fputc(c, f);
1583665Sdlw }
1593665Sdlw 
1602492Sdlw /*initialization routine*/
1612492Sdlw f_init()
16211919Sdlw {
16312006Sdlw 	ini_std(STDERR, stderr, WRITE);
16412006Sdlw 	ini_std(STDIN, stdin, READ);
16512006Sdlw 	ini_std(STDOUT, stdout, WRITE);
1662492Sdlw }
1672492Sdlw 
168