xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 2765)
12492Sdlw /*
2*2765Sdlw char id_err[] = "@(#)err.c	1.4";
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;
422492Sdlw 
432492Sdlw /*error messages*/
442492Sdlw 
452492Sdlw extern char *sys_errlist[];
462492Sdlw extern int sys_nerr;
472492Sdlw 
48*2765Sdlw extern char *f_errlist[];
49*2765Sdlw extern int f_nerr;
502492Sdlw 
512492Sdlw 
522492Sdlw fatal(n,s) char *s;
532492Sdlw {
542492Sdlw 	ftnint lu;
552492Sdlw 
562492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
572492Sdlw 		flush_(&lu);
582492Sdlw 	if(n<0)
592492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
602492Sdlw 	else if(n>=0 && n<sys_nerr)
612492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,sys_errlist[n]);
62*2765Sdlw 	else if(n>=F_ER && n<F_MAXERR)
632492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
642492Sdlw 	else
652492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
662492Sdlw 	if(external)
672492Sdlw 	{
682492Sdlw 		if(!lfname) switch (lunit)
692492Sdlw 		{	case STDERR: lfname = "stderr";
702492Sdlw 					break;
712492Sdlw 			case STDIN:  lfname = "stdin";
722492Sdlw 					break;
732492Sdlw 			case STDOUT: lfname = "stdout";
742492Sdlw 					break;
752492Sdlw 			default:     lfname = "";
762492Sdlw 		}
772492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
782492Sdlw 	}
792492Sdlw 	if (elist)
802492Sdlw 	{	fprintf(stderr,"lately: %s %s %s %s IO\n",
812492Sdlw 			reading?"reading":"writing",
822492Sdlw 			sequential?"sequential":"direct",
832492Sdlw 			formatted>0?"formatted":(formatted<0?"list":"unformatted"),
842492Sdlw 			external?"external":"internal");
852492Sdlw 		if (formatted)
862492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
872492Sdlw 			if (external)
882492Sdlw 			{	if(reading && curunit->useek)
892492Sdlw 					prnt_ext();  /* print external data */
902492Sdlw 			}
912492Sdlw 			else prnt_int();	/* print internal array */
922492Sdlw 		}
932492Sdlw 	}
942492Sdlw 	f_exit();
952492Sdlw 	_cleanup();
962492Sdlw 	abort();
972492Sdlw }
982492Sdlw 
992492Sdlw prnt_ext()
1002492Sdlw {	int ch;
1012492Sdlw 	int i=1;
1022492Sdlw 	long loc;
1032492Sdlw 	fprintf (stderr, "part of last data: ");
1042492Sdlw 	loc = ftell(curunit->ufd);
1052492Sdlw 	if(loc)
1062492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
1072492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
1082492Sdlw 		while(i--) fputc(fgetc(curunit->ufd),stderr);
1092492Sdlw 	}
1102492Sdlw 	fputc('|',stderr);
1112492Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd)!=EOF);i++) fputc(ch,stderr);
1122492Sdlw 	fputc('\n',stderr);
1132492Sdlw }
1142492Sdlw 
1152492Sdlw prnt_int()
1162492Sdlw {	char *ep;
1172492Sdlw 	fprintf (stderr,"part of last string: ");
1182492Sdlw 	ep = icptr - (recpos<12?recpos:12);
1192492Sdlw 	while (ep<icptr) fputc(*ep++,stderr);
1202492Sdlw 	fputc('|',stderr);
1212492Sdlw 	while (ep<(icptr+5) && ep<icend) fputc(*ep++,stderr);
1222492Sdlw 	fputc('\n',stderr);
1232492Sdlw }
1242492Sdlw 
1252492Sdlw prnt_fmt(n) int n;
1262492Sdlw {	int i; char *ep;
1272492Sdlw 	fprintf(stderr, "part of last format: ");
1282590Sdlw 	if(n==F_ERFMT)
1292492Sdlw 	{	i = fmtptr - fmtbuf;
1302492Sdlw 		ep = fmtptr - (i<20?i:20);
1312492Sdlw 		i = i + 5;
1322492Sdlw 	}
1332492Sdlw 	else
1342492Sdlw 	{	ep = fmtbuf;
1352492Sdlw 		i = 25;
1362492Sdlw 		fmtptr = fmtbuf - 1;
1372492Sdlw 	}
1382492Sdlw 	while(i && *ep)
1392492Sdlw 	{	fputc((*ep==GLITCH)?'"':*ep,stderr);
1402492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1412492Sdlw 		ep++; i--;
1422492Sdlw 	}
1432492Sdlw 	fputc('\n',stderr);
1442492Sdlw }
1452492Sdlw 
1462492Sdlw /*initialization routine*/
1472492Sdlw f_init()
1482492Sdlw {	ini_std(STDERR, stderr, WRITE);
1492492Sdlw 	ini_std(STDIN, stdin, READ);
1502492Sdlw 	ini_std(STDOUT, stdout, WRITE);
1512492Sdlw }
1522492Sdlw 
153