xref: /csrg-svn/usr.bin/f77/libI77/err.c (revision 2492)
1*2492Sdlw /*
2*2492Sdlw char id_err[] = "@(#)err.c	1.1";
3*2492Sdlw  *
4*2492Sdlw  * file i/o error and initialization routines
5*2492Sdlw  */
6*2492Sdlw 
7*2492Sdlw #include <sys/types.h>
8*2492Sdlw #include <sys/stat.h>
9*2492Sdlw #include <signal.h>
10*2492Sdlw #include "fiodefs.h"
11*2492Sdlw 
12*2492Sdlw /*
13*2492Sdlw  * global definitions
14*2492Sdlw  */
15*2492Sdlw 
16*2492Sdlw char *tmplate = "tmp.FXXXXXX";	/* scratch file template */
17*2492Sdlw char *fortfile = "fort.%d";	/* default file template */
18*2492Sdlw 
19*2492Sdlw unit units[MXUNIT] = 0;	/*unit table*/
20*2492Sdlw flag reading;		/*1 if reading,		0 if writing*/
21*2492Sdlw flag external;		/*1 if external io,	0 if internal */
22*2492Sdlw flag sequential;	/*1 if sequential io,	0 if direct*/
23*2492Sdlw flag formatted;		/*1 if formatted io,	0 if unformatted, -1 if list*/
24*2492Sdlw char *fmtbuf, *icptr, *icend, *fmtptr;
25*2492Sdlw int (*doed)(),(*doned)();
26*2492Sdlw int (*doend)(),(*donewrec)(),(*dorevert)(),(*dotab)();
27*2492Sdlw int (*lioproc)();
28*2492Sdlw int (*getn)(),(*putn)(),(*ungetn)();	/*for formatted io*/
29*2492Sdlw icilist *svic;		/* active internal io list */
30*2492Sdlw FILE *cf;		/*current file structure*/
31*2492Sdlw unit *curunit;		/*current unit structure*/
32*2492Sdlw int lunit;		/*current logical unit*/
33*2492Sdlw char *lfname;		/*current filename*/
34*2492Sdlw int recpos;		/*place in current record*/
35*2492Sdlw ftnint recnum;		/* current record number */
36*2492Sdlw int reclen;		/* current record length */
37*2492Sdlw int cursor,scale;
38*2492Sdlw int radix;
39*2492Sdlw ioflag signit,tab,cplus,cblank,elist,errflag,endflag,lquit,l_first;
40*2492Sdlw flag leof;
41*2492Sdlw int lcount,line_len;
42*2492Sdlw 
43*2492Sdlw /*error messages*/
44*2492Sdlw 
45*2492Sdlw extern char *sys_errlist[];
46*2492Sdlw extern int sys_nerr;
47*2492Sdlw 
48*2492Sdlw #include "f_errlist.h"
49*2492Sdlw 
50*2492Sdlw 
51*2492Sdlw fatal(n,s) char *s;
52*2492Sdlw {
53*2492Sdlw 	ftnint lu;
54*2492Sdlw 
55*2492Sdlw 	for (lu=1; lu < MXUNIT; lu++)
56*2492Sdlw 		flush_(&lu);
57*2492Sdlw 	if(n<0)
58*2492Sdlw 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
59*2492Sdlw 	else if(n>=0 && n<sys_nerr)
60*2492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,sys_errlist[n]);
61*2492Sdlw 	else if(n>=F_ER && n<MAXERR)
62*2492Sdlw 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
63*2492Sdlw 	else
64*2492Sdlw 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
65*2492Sdlw 	if(external)
66*2492Sdlw 	{
67*2492Sdlw 		if(!lfname) switch (lunit)
68*2492Sdlw 		{	case STDERR: lfname = "stderr";
69*2492Sdlw 					break;
70*2492Sdlw 			case STDIN:  lfname = "stdin";
71*2492Sdlw 					break;
72*2492Sdlw 			case STDOUT: lfname = "stdout";
73*2492Sdlw 					break;
74*2492Sdlw 			default:     lfname = "";
75*2492Sdlw 		}
76*2492Sdlw 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
77*2492Sdlw 	}
78*2492Sdlw 	if (elist)
79*2492Sdlw 	{	fprintf(stderr,"lately: %s %s %s %s IO\n",
80*2492Sdlw 			reading?"reading":"writing",
81*2492Sdlw 			sequential?"sequential":"direct",
82*2492Sdlw 			formatted>0?"formatted":(formatted<0?"list":"unformatted"),
83*2492Sdlw 			external?"external":"internal");
84*2492Sdlw 		if (formatted)
85*2492Sdlw 		{	if(fmtbuf) prnt_fmt(n);
86*2492Sdlw 			if (external)
87*2492Sdlw 			{	if(reading && curunit->useek)
88*2492Sdlw 					prnt_ext();  /* print external data */
89*2492Sdlw 			}
90*2492Sdlw 			else prnt_int();	/* print internal array */
91*2492Sdlw 		}
92*2492Sdlw 	}
93*2492Sdlw 	f_exit();
94*2492Sdlw 	_cleanup();
95*2492Sdlw 	abort();
96*2492Sdlw }
97*2492Sdlw 
98*2492Sdlw prnt_ext()
99*2492Sdlw {	int ch;
100*2492Sdlw 	int i=1;
101*2492Sdlw 	long loc;
102*2492Sdlw 	fprintf (stderr, "part of last data: ");
103*2492Sdlw 	loc = ftell(curunit->ufd);
104*2492Sdlw 	if(loc)
105*2492Sdlw 	{	if(loc==1L) rewind(curunit->ufd);
106*2492Sdlw 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
107*2492Sdlw 		while(i--) fputc(fgetc(curunit->ufd),stderr);
108*2492Sdlw 	}
109*2492Sdlw 	fputc('|',stderr);
110*2492Sdlw 	for(i=0;i<5 && (ch=fgetc(curunit->ufd)!=EOF);i++) fputc(ch,stderr);
111*2492Sdlw 	fputc('\n',stderr);
112*2492Sdlw }
113*2492Sdlw 
114*2492Sdlw prnt_int()
115*2492Sdlw {	char *ep;
116*2492Sdlw 	fprintf (stderr,"part of last string: ");
117*2492Sdlw 	ep = icptr - (recpos<12?recpos:12);
118*2492Sdlw 	while (ep<icptr) fputc(*ep++,stderr);
119*2492Sdlw 	fputc('|',stderr);
120*2492Sdlw 	while (ep<(icptr+5) && ep<icend) fputc(*ep++,stderr);
121*2492Sdlw 	fputc('\n',stderr);
122*2492Sdlw }
123*2492Sdlw 
124*2492Sdlw prnt_fmt(n) int n;
125*2492Sdlw {	int i; char *ep;
126*2492Sdlw 	fprintf(stderr, "part of last format: ");
127*2492Sdlw 	if(n==100)
128*2492Sdlw 	{	i = fmtptr - fmtbuf;
129*2492Sdlw 		ep = fmtptr - (i<20?i:20);
130*2492Sdlw 		i = i + 5;
131*2492Sdlw 	}
132*2492Sdlw 	else
133*2492Sdlw 	{	ep = fmtbuf;
134*2492Sdlw 		i = 25;
135*2492Sdlw 		fmtptr = fmtbuf - 1;
136*2492Sdlw 	}
137*2492Sdlw 	while(i && *ep)
138*2492Sdlw 	{	fputc((*ep==GLITCH)?'"':*ep,stderr);
139*2492Sdlw 		if(ep==fmtptr) fputc('|',stderr);
140*2492Sdlw 		ep++; i--;
141*2492Sdlw 	}
142*2492Sdlw 	fputc('\n',stderr);
143*2492Sdlw }
144*2492Sdlw 
145*2492Sdlw /*initialization routine*/
146*2492Sdlw f_init()
147*2492Sdlw {	ini_std(STDERR, stderr, WRITE);
148*2492Sdlw 	ini_std(STDIN, stdin, READ);
149*2492Sdlw 	ini_std(STDOUT, stdout, WRITE);
150*2492Sdlw }
151*2492Sdlw 
152