xref: /original-bsd/usr.bin/f77/libI77/err.c (revision db9e1aef)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)err.c	5.3	05/28/90
7  */
8 
9 /*
10  * fatal(): i/o error routine
11  * flush_(): flush file buffer
12  */
13 
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <signal.h>
17 #include "fio.h"
18 
19 /*
20  * global definitions
21  */
22 
23 unit units[MXUNIT];	/*unit table*/
24 flag reading;		/*1 if reading,		0 if writing*/
25 flag external;		/*1 if external io,	0 if internal */
26 flag sequential;	/*1 if sequential io,	0 if direct*/
27 flag formatted;		/*1 if formatted io,	0 if unformatted,
28 				-1 if list directed, -2 if namelist */
29 char *fmtbuf, *icptr, *icend, *fmtptr;
30 int (*doed)(),(*doned)();
31 int (*doend)(),(*donewrec)(),(*dorevert)(),(*dotab)();
32 int (*lioproc)();
33 int (*getn)(),(*putn)(),(*ungetn)();	/*for formatted io*/
34 FILE *cf;		/*current file structure*/
35 unit *curunit;		/*current unit structure*/
36 int lunit;		/*current logical unit*/
37 char *lfname;		/*current filename*/
38 int recpos;		/*place in current record*/
39 ftnint recnum;		/* current record number */
40 int reclen;		/* current record length */
41 int cursor,scale;
42 int radix;
43 ioflag signit,tab,cplus,cblank,elist,errflag,endflag,lquit,l_first;
44 flag leof;
45 int lcount,line_len;
46 struct ioiflg ioiflg_;	/* initialization flags */
47 
48 /*error messages*/
49 
50 extern int sys_nerr;
51 
52 extern char *f_errlist[];
53 extern int f_nerr;
54 
55 
56 fatal(n,s) char *s;
57 {
58 	ftnint lu;
59 	char *strerror();
60 
61 	for (lu=1; lu < MXUNIT; lu++)
62 		flush_(&lu);
63 	if(n<0)
64 		fprintf(stderr,"%s: [%d] end of file\n",s,n);
65 	else if(n>=0 && n<sys_nerr)
66 		fprintf(stderr,"%s: [%d] %s\n",s,n, strerror(n));
67 	else if(n>=F_ER && n<F_MAXERR)
68 		fprintf(stderr,"%s: [%d] %s\n",s,n,f_errlist[n-F_ER]);
69 	else
70 		fprintf(stderr,"%s: [%d] unknown error number\n",s,n);
71 	if(external)
72 	{
73 		if(!lfname) switch (lunit)
74 		{	case STDERR: lfname = "stderr";
75 					break;
76 			case STDIN:  lfname = "stdin";
77 					break;
78 			case STDOUT: lfname = "stdout";
79 					break;
80 			default:     lfname = "";
81 		}
82 		fprintf(stderr,"logical unit %d, named '%s'\n",lunit,lfname);
83 	}
84 	if (elist)
85 	{	fprintf(stderr,"lately: %s %s %s %s I/O\n",
86 			reading?"reading":"writing",
87 			sequential?"sequential":"direct",
88 			formatted>0?"formatted":(formatted==0?"unformatted":
89 				(formatted==LISTDIRECTED?"list":"namelist")),
90 			external?"external":"internal");
91 		if (formatted)
92 		{	if(fmtbuf) prnt_fmt(n);
93 			if (external)
94 			{	if(reading && curunit->useek)
95 					prnt_ext();  /* print external data */
96 			}
97 			else prnt_int();	/* print internal array */
98 		}
99 	}
100 	f77_abort(n);
101 }
102 
103 LOCAL
104 prnt_ext()
105 {	int ch;
106 	int i=1;
107 	long loc;
108 	fprintf (stderr, "part of last data: ");
109 	loc = ftell(curunit->ufd);
110 	if(loc)
111 	{	if(loc==1L) rewind(curunit->ufd);
112 		else for(;i<12 && last_char(curunit->ufd)!='\n';i++);
113 		while(i--) ffputc(fgetc(curunit->ufd),stderr);
114 	}
115 	fputc('|',stderr);
116 	for(i=0;i<5 && (ch=fgetc(curunit->ufd))!=EOF;i++) ffputc(ch,stderr);
117 	fputc('\n',stderr);
118 }
119 
120 LOCAL
121 prnt_int()
122 {	char *ep;
123 	fprintf (stderr,"part of last string: ");
124 	ep = icptr - (recpos<12?recpos:12);
125 	while (ep<icptr) ffputc(*ep++,stderr);
126 	fputc('|',stderr);
127 	while (ep<(icptr+5) && ep<icend) ffputc(*ep++,stderr);
128 	fputc('\n',stderr);
129 }
130 
131 LOCAL
132 prnt_fmt(n) int n;
133 {	int i; char *ep;
134 	fprintf(stderr, "format: ");
135 	if(n==F_ERFMT)
136 	{	i = fmtptr - fmtbuf;
137 		ep = fmtptr - (i<25?i:25);
138 		if(ep != fmtbuf) fprintf(stderr, "... ");
139 		i = i + 5;
140 	}
141 	else
142 	{	ep = fmtbuf;
143 		i = 25;
144 		fmtptr = fmtbuf - 1;
145 	}
146 	while(i && *ep)
147 	{	ffputc((*ep==GLITCH)?'"':*ep,stderr);
148 		if(ep==fmtptr) fputc('|',stderr);
149 		ep++; i--;
150 	}
151 	if(*ep) fprintf(stderr, " ...");
152 	fputc('\n',stderr);
153 }
154 
155 LOCAL
156 ffputc(c, f)
157 int	c;
158 FILE	*f;
159 {
160 	c &= 0177;
161 	if (c < ' ' || c == 0177)
162 	{
163 		fputc('^', f);
164 		c ^= 0100;
165 	}
166 	fputc(c, f);
167 }
168 
169 ftnint
170 flush_(u) ftnint *u;
171 {
172 	FILE *F;
173 
174 	if(not_legal(*u))
175 		return(F_ERUNIT);
176 	F = units[*u].ufd;
177 	if(F)
178 		return(fflush(F));
179 	else
180 		return(F_ERNOPEN);
181 }
182