Debian Codegraph @

prim/tc3/libsrc/error.c

raw
1 /*===========================================================================
2 Copyright (C) 1985-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
17 MA 02139, USA.
18
19 Corresponding concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
30 .TYPE Module
31 .IDENTIFICATION error.c
32 .AUTHOR Alan Richmond [ST-ECF]
33 .LANGUAGE C
34 .KEYWORDS Error handling.
35 .ENVIRONMENT
36 .COMMENTS Error Handler.
37 This module contains the error state: a flag which is TRUE
38 if an error has been reported, and an associated message.
39 In the ST implementation ( import_ST is defined),
40 the error state is passed down to the ST interface.
41 A future implementation may use a/the stack structure; the
42 present version is primarily designed for use with the
43 Dialogue Manager prompting facility, which reports the error
44 to the user. Currently no record of `underlying' errors is
45 required, but this may change.
46 .VERSION 1.0 13-Dec-1985: Creation
47 .VERSION 1.1 28-Apr-1986: check_range added.
48 .VERSION 2.0 23-May-1986: Removed ST staff (at lower level).
49 .VERSION 2.1 12-Jun-1987: Removed bug in eh_ed_str2
50 Added eh_loc_error.
51 .VERSION 2.2 25-Jun-1987: Made availabler with SW_Level
52 (0 = Minimal - 1 = Full)
53 .VERSION 2.3 23-Feb-1988: MIDAS compatibility
54 .VERSION 2.4 08-Dec-1988: Truncate text to ensure logged message.
55 Be sure eh_ed_str2 logs the mesage.
56 .VERSION 2.5 14-Jul-1989: Added eh_ed_as, to be sure that null pointers
57 don't result in a crash...
58
59 090831 last modif
60 -----------------------------*/
61
62 #include <midas_def.h>
63 #include <macrogen.h>
64 #include <error.h>
65 #include <string.h>
66
67 #define LINESIZE (sizeof(locbuf)-1)
68 /* Maximum size of a line */
69
70 extern int pm_tr2();
71
72
73 int eh_put2(), eh_ed_str2();
74
75
76 static char locbuf[81]="";
77 static char error_state = 0;
78 static char avoid[] = "";
79 static int keep_bytes = 0; /* To be sure to get complete message */
80 static int error_lev = _ERROR_; /* Error or Warning Level */
81
82 /*==========================================================================*/
83 static int edval (x, i)
84 /*+++
85 .PURPOSE Edit the long integer number
86 .RETURNS The new index in locbuf
87 .REMARKS
88 --------*/
89 long int x; /* IN: Number ot edit */
90 int i; /* IN: Index in locbuf */
91 {
92 register int k;
93 register long int j;
94 MID_STATIC char edbuf[12] = " ";
95
96 j = ABSOLUTE(x);
97
98 for (k = sizeof(edbuf); ;)
99 { edbuf[--k]= (j%10) + '0';
100 j /= 10;
101 if (j == 0) break;
102 }
103 if (x < 0) edbuf[--k] = '-';
104
105 i += oscopy(&locbuf[i], &edbuf[k], sizeof(edbuf)-k);
106 return(i);
107 }
108
109 /*=========================================================================*/
110 /* Public Routines */
111 /*=========================================================================*/
112 int eh_state ()
113 /*+++
114 .PURPOSE Test for error state, i.e. any error was reported, and not yet cleared
115 .RETURNS 0 if no error, 1 if error exists.
116 ---------*/
117 {
118 return(error_state);
119 }
120
121 /*=========================================================================*/
122 int eh_class (class)
123 /*+++
124 .PURPOSE Define error level for next message
125 .RETURNS error state
126 .REMARKS Class is _ERROR_ or _WARNING_
127 ---------*/
128 int class; /* _ERROR_ or _WARNING_ */
129 {
130 error_lev = class;
131
132 return(error_state);
133 }
134
135 /*=========================================================================*/
136 int eh_put (subr, message, class)
137 /*+++
138 .PURPOSE Store error message.
139 .RETURNS 0 (Not an error) / 1 (Is a error)
140 .REMARKS Class is _ERROR_ or _WARNING_
141 ---------*/
142 char *subr; /* IN: subroutine name (unused) */
143 char *message; /* IN: message text */
144 int class; /* _ERROR_ or _WARNING_ */
145 {
146 return(eh_put2(subr, message, class, strlen(message)));
147 }
148
149 /*=========================================================================*/
150 int eh_put1(message)
151 /*+++
152 .PURPOSE Store error message.
153 .RETURNS 0 (Not an error) / 1 (Is a error)
154 .REMARKS Class is _ERROR_
155 ---------*/
156 char *message; /* IN: message text */
157 {
158 return(eh_put2((char *)0, message, _ERROR_, strlen(message)));
159 }
160
161 /*=========================================================================*/
162 int eh_put2 (subr, message, class, lmsg)
163 /*+++
164 .PURPOSE Store error message.
165 .RETURNS 0 (Not an error) / 1 (Is a error)
166 .REMARKS Class is _ERROR_ or _WARNING_
167 ---------*/
168 char *subr; /* IN: subroutine name (unused) */
169 char *message; /* IN: message text */
170 int class; /* _ERROR_ or _WARNING_ */
171 int lmsg; /* IN: Length of message */
172 {
173 register char *p;
174 register int i;
175
176 if (!message) return(0); /* No message at all ... */
177
178 pm_tr2(class, message, lmsg);
179
180 if (message != locbuf)
181 { for (p = message, i = 0; (i < LINESIZE) && (*p); )
182 locbuf[i++] = *(p++);
183 locbuf[i] = EOS;
184 }
185
186 if(class == _ERROR_) error_state = 1;
187
188 error_lev = _ERROR_; /* Error or Warning Level */
189
190 return(error_state);
191 }
192
193 /*=========================================================================*/
194 char *eh_loc (level, subr, class)
195 /*+++
196 .PURPOSE Retrieve address of a stored error message
197 .RETURNS Address of message / NULL if no message
198 .REMARKS
199 ---------*/
200 int level; /* TBD */
201 char *subr; /* OUT: subroutine name (unused) */
202 int class; /* TBD */
203 {
204 return (error_state ? locbuf : NULL_PTR(char));
205 }
206
207 /*=========================================================================*/
208 int eh_clear (level)
209 /*+++
210 .PURPOSE Clear error state.
211 .RETURNS 0
212 .REMARKS
213 ---------*/
214 int level; /* TBD */
215 {
216 error_state = 0;
217 return(error_state);
218 }
219
220 /***************************************************************************
221 * eh_ed routines
222 ****************************************************************************/
223
224 static int eh_ed0(text)
225 /*+++
226 .PURPOSE Copies the text to internal buffer
227 .RETURNS Index of first useable character in internal buffer
228 .REMARKS Routine not traced. Leaves at least keep_bytes.
229 ---------*/
230 char *text; /* IN: text to copy */
231 {
232 register int i, l;
233 register char *p;
234
235 l = LINESIZE - keep_bytes;
236 for (i=0, p=text; (i < l) && (*p); )
237 locbuf[i++] = *(p++);
238 if ( (*p == '\0') && (*(p-1) != ' '))
239 locbuf[i++] = ' '; /* Add blank */
240
241 if ( (*p) && (i >= 4) ) /* Too long text... */
242 { p = &locbuf[i-4];
243 *(p++) = '.', *(p++) = '.', *(p++) = '.';
244 *(p++) = ' ';
245 }
246
247 return(i);
248 }
249
250 /*=========================================================================*/
251 int eh_ed_as(text, string)
252 /*+++
253 .PURPOSE Error: text followed by EOS string
254 .RETURNS 1
255 .REMARKS string can be a null pointer.
256 ---------*/
257 char *text; /* IN: Text to insert */
258 char *string; /* IN: String to insert, terminated with EOS */
259 {
260 char *p;
261
262 p = (string ? string : "<NuLL>");
263
264 return(eh_ed_str2(text, p, strlen(p)));
265 }
266
267 /*=========================================================================*/
268 int eh_ed_str2 (text, string, length)
269 /*+++
270 .PURPOSE Error: text followed by string
271 .RETURNS 1
272 .REMARKS Routine not traced
273 ---------*/
274 char *text; /* IN: Text to insert */
275 char *string; /* IN: String to insert */
276 int length; /* IN: length of string */
277 {
278 register int i, l;
279 register char *p;
280
281 keep_bytes = length;
282
283 i = eh_ed0(text);
284
285 for (p = string, l = length; (i < LINESIZE) && (--l >= 0) ; )
286 locbuf[i++] = *(p++);
287
288 locbuf[i] = EOS;
289
290 return(eh_put2(avoid, locbuf, error_lev, i));
291 }
292
293 /*=========================================================================*/
294 int eh_ed_i (text, value)
295 /*++++++++
296 .PURPOSE Store error message followed by a number.
297 .RETURNS 1
298 .REMARKS Routine not traced
299 ---------*/
300 char *text; /* IN: EOS-terminated Text to insert*/
301 int value; /* IN: value to edit */
302 {
303 register int i;
304 long int x;
305
306 keep_bytes = 11;
307 i = eh_ed0(text);
308
309 if (i < LINESIZE - 11)
310 { x = value;
311 i = edval(x, i);
312 }
313 locbuf[i] = EOS;
314
315 return(eh_put2(avoid, locbuf, error_lev, i));
316 }
317