xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 2590)
12492Sdlw /*
2*2590Sdlw char id_err[] = "@(#)err.c	1.2";
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 
482492Sdlw #include "f_errlist.h"
492492Sdlw 
502492Sdlw 
512492Sdlw fatal(n,s) char *s;
522492Sdlw {
532492Sdlw 	ftnint lu;
542492Sdlw 
552492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
562492Sdlw 		flush_(&lu);
572492Sdlw 	if(n<0)
582492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
592492Sdlw 	else if(n>=0 && n<sys_nerr)
602492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,sys_errlist[n]);
612492Sdlw 	else if(n>=F_ER && n<MAXERR)
622492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
632492Sdlw 	else
642492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
652492Sdlw 	if(external)
662492Sdlw 	{
672492Sdlw 		if(!lfname) switch (lunit)
682492Sdlw 		{	case STDERR: lfname = "stderr";
692492Sdlw 					break;
702492Sdlw 			case STDIN:  lfname = "stdin";
712492Sdlw 					break;
722492Sdlw 			case STDOUT: lfname = "stdout";
732492Sdlw 					break;
742492Sdlw 			default:     lfname = "";
752492Sdlw 		}
762492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
772492Sdlw 	}
782492Sdlw 	if (elist)
792492Sdlw 	{	fprintf(stderr,"lately: %s %s %s %s IO\n",
802492Sdlw 			reading?"reading":"writing",
812492Sdlw 			sequential?"sequential":"direct",
822492Sdlw 			formatted>0?"formatted":(formatted<0?"list":"unformatted"),
832492Sdlw 			external?"external":"internal");
842492Sdlw 		if (formatted)
852492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
862492Sdlw 			if (external)
872492Sdlw 			{	if(reading && curunit->useek)
882492Sdlw 					prnt_ext();  /* print external data */
892492Sdlw 			}
902492Sdlw 			else prnt_int();	/* print internal array */
912492Sdlw 		}
922492Sdlw 	}
932492Sdlw 	f_exit();
942492Sdlw 	_cleanup();
952492Sdlw 	abort();
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++);
1072492Sdlw 		while(i--) fputc(fgetc(curunit->ufd),stderr);
1082492Sdlw 	}
1092492Sdlw 	fputc('|',stderr);
1102492Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd)!=EOF);i++) fputc(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);
1182492Sdlw 	while (ep<icptr) fputc(*ep++,stderr);
1192492Sdlw 	fputc('|',stderr);
1202492Sdlw 	while (ep<(icptr+5) && ep<icend) fputc(*ep++,stderr);
1212492Sdlw 	fputc('\n',stderr);
1222492Sdlw }
1232492Sdlw 
1242492Sdlw prnt_fmt(n) int n;
1252492Sdlw {	int i; char *ep;
1262492Sdlw 	fprintf(stderr, "part of last format: ");
127*2590Sdlw 	if(n==F_ERFMT)
1282492Sdlw 	{	i = fmtptr - fmtbuf;
1292492Sdlw 		ep = fmtptr - (i<20?i:20);
1302492Sdlw 		i = i + 5;
1312492Sdlw 	}
1322492Sdlw 	else
1332492Sdlw 	{	ep = fmtbuf;
1342492Sdlw 		i = 25;
1352492Sdlw 		fmtptr = fmtbuf - 1;
1362492Sdlw 	}
1372492Sdlw 	while(i && *ep)
1382492Sdlw 	{	fputc((*ep==GLITCH)?'"':*ep,stderr);
1392492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
1402492Sdlw 		ep++; i--;
1412492Sdlw 	}
1422492Sdlw 	fputc('\n',stderr);
1432492Sdlw }
1442492Sdlw 
1452492Sdlw /*initialization routine*/
1462492Sdlw f_init()
1472492Sdlw {	ini_std(STDERR, stderr, WRITE);
1482492Sdlw 	ini_std(STDIN, stdin, READ);
1492492Sdlw 	ini_std(STDOUT, stdout, WRITE);
1502492Sdlw }
1512492Sdlw 
152