xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 20185)
12492Sdlw /*
2*20185Slibs char id_err[] = "@(#)err.c	1.15";
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 
1919920Slibs 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)
81*20185Slibs 	{	fprintf(stderr,"lately: %s %s %s %s I/O\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 	}
95*20185Slibs 	f77_abort(n);
962492Sdlw }
972492Sdlw 
982492Sdlw prnt_ext()
992492Sdlw {	int ch;
1002492Sdlw 	int i=1;
1012492Sdlw 	long loc;
1022492Sdlw 	fprintf (stderr, "part of last data: ");
1032492Sdlw 	loc = ftell(curunit->ufd);
1042492Sdlw 	if(loc)
1052492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1062492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1073665Sdlw 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
1082492Sdlw 	}
1092492Sdlw 	fputc('|',stderr);
1103665Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
1112492Sdlw 	fputc('\n',stderr);
1122492Sdlw }
1132492Sdlw 
1142492Sdlw prnt_int()
1152492Sdlw {	char *ep;
1162492Sdlw 	fprintf (stderr,"part of last string: ");
1172492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1183665Sdlw 	while (ep<icptr) ffputc(*ep++,stderr);
1192492Sdlw 	fputc('|',stderr);
1203665Sdlw 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
1212492Sdlw 	fputc('\n',stderr);
1222492Sdlw }
1232492Sdlw 
1242492Sdlw prnt_fmt(n) int n;
1252492Sdlw {	int i; char *ep;
12617980Slibs 	fprintf(stderr, "format: ");
1272590Sdlw 	if(n==F_ERFMT)
1282492Sdlw 	{	i = fmtptr - fmtbuf;
12917980Slibs 		ep = fmtptr - (i<25?i:25);
13017980Slibs 		if(ep != fmtbuf) fprintf(stderr, "... ");
1312492Sdlw 		i = i + 5;
1322492Sdlw 	}
1332492Sdlw 	else
1342492Sdlw 	{	ep = fmtbuf;
1352492Sdlw 		i = 25;
1362492Sdlw 		fmtptr = fmtbuf - 1;
1372492Sdlw 	}
1382492Sdlw 	while(i && *ep)
1393665Sdlw 	{	ffputc((*ep==GLITCH)?'"':*ep,stderr);
1402492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1412492Sdlw 		ep++; i--;
1422492Sdlw 	}
14317980Slibs 	if(*ep) fprintf(stderr, " ...");
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);
16618015Slibs 	setlinebuf(stderr);
1672492Sdlw }
1682492Sdlw 
169