Sat, 08 Mar 2003 15:55:01 +0100
trunk: changeset 50
Added CONFOPTS_NOT_SET and parse_config_tokz_skip_section.
0 | 1 | /* |
2 | * libtu/output.c | |
3 | * | |
36 | 4 | * Copyright (c) Tuomo Valkonen 1999-2002. |
0 | 5 | * See the included file LICENSE for details. |
6 | */ | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <errno.h> | |
11 | #include <strings.h> | |
12 | #include <string.h> | |
13 | ||
5 | 14 | #include <libtu/misc.h> |
15 | #include <libtu/output.h> | |
16 | #include <libtu/util.h> | |
0 | 17 | |
35 | 18 | #if !defined(HAS_SYSTEM_ASPRINTF) |
25 | 19 | #include "../snprintf_2.2/snprintf.h" |
20 | #endif | |
21 | ||
0 | 22 | |
23 | /* verbose | |
24 | */ | |
25 | ||
26 | static bool verbose_mode=FALSE; | |
27 | static int verbose_indent_lvl=0; | |
28 | static bool progname_enable=TRUE; | |
35 | 29 | static WarnHandler *current_warn_handler=NULL; |
0 | 30 | |
31 | #define INDENTATOR_LENGTH 4 | |
32 | ||
33 | static char indentator[]={' ', ' ', ' ', ' '}; | |
34 | ||
35 | 35 | static void do_dispatch_message(const char *message); |
36 | ||
0 | 37 | |
38 | void verbose(const char *p, ...) | |
39 | { | |
40 | va_list args; | |
41 | ||
42 | va_start(args, p); | |
43 | ||
44 | verbose_v(p, args); | |
45 | ||
46 | va_end(args); | |
47 | } | |
48 | ||
49 | ||
50 | void verbose_v(const char *p, va_list args) | |
51 | { | |
52 | int i; | |
53 | ||
54 | if(verbose_mode){ | |
55 | for(i=0; i<verbose_indent_lvl; i++) | |
56 | writef(stdout, indentator, INDENTATOR_LENGTH); | |
57 | ||
58 | vprintf(p, args); | |
59 | fflush(stdout); | |
60 | } | |
61 | } | |
62 | ||
63 | ||
64 | void verbose_enable(bool enable) | |
65 | { | |
66 | verbose_mode=enable; | |
67 | } | |
68 | ||
69 | ||
70 | int verbose_indent(int depth) | |
71 | { | |
72 | int old=verbose_indent_lvl; | |
73 | ||
74 | if(depth>=0) | |
75 | verbose_indent_lvl=depth; | |
76 | ||
77 | return old; | |
78 | } | |
79 | ||
80 | ||
81 | void warn_progname_enable(bool enable) | |
82 | { | |
83 | progname_enable=enable; | |
84 | } | |
85 | ||
86 | ||
87 | static void put_prog_name() | |
88 | { | |
89 | const char*progname; | |
90 | ||
91 | if(!progname_enable) | |
92 | return; | |
93 | ||
94 | progname=prog_execname(); | |
95 | ||
96 | if(progname==NULL) | |
97 | return; | |
98 | ||
99 | fprintf(stderr, "%s: ", (char*)progname); | |
100 | } | |
101 | ||
27 | 102 | /* warn |
103 | */ | |
0 | 104 | |
35 | 105 | |
106 | static void fallback_warn() | |
107 | { | |
108 | put_prog_name(); | |
109 | fprintf(stderr, "Oops. Error string compilation failed: %s", | |
110 | strerror(errno)); | |
111 | } | |
112 | ||
113 | ||
25 | 114 | #define CALL_V(NAME, ARGS) \ |
35 | 115 | do { va_list args; va_start(args, p); NAME ARGS; va_end(args); } while(0) |
27 | 116 | |
35 | 117 | #define DO_DISPATCH(NAME, ARGS) \ |
118 | do{ \ | |
119 | char *msg; \ | |
120 | if((msg=NAME ARGS)!=NULL){ \ | |
121 | do_dispatch_message(msg); \ | |
122 | free(msg);\ | |
123 | }else{ \ | |
124 | fallback_warn(); \ | |
125 | } \ | |
126 | }while(0) | |
25 | 127 | |
33 | 128 | |
31 | 129 | void libtu_asprintf(char **ret, const char *p, ...) |
130 | { | |
131 | CALL_V(vasprintf, (ret, p, args)); | |
132 | } | |
133 | ||
134 | ||
135 | void libtu_vasprintf(char **ret, const char *p, va_list args) | |
136 | { | |
137 | vasprintf(ret, p, args); | |
138 | } | |
139 | ||
33 | 140 | |
0 | 141 | void warn(const char *p, ...) |
142 | { | |
25 | 143 | CALL_V(warn_v, (p, args)); |
144 | } | |
145 | ||
146 | ||
147 | void warn_obj(const char *obj, const char *p, ...) | |
148 | { | |
149 | CALL_V(warn_obj_v, (obj, p, args)); | |
150 | } | |
151 | ||
152 | ||
153 | void warn_obj_line(const char *obj, int line, const char *p, ...) | |
154 | { | |
155 | CALL_V(warn_obj_line_v, (obj, line, p, args)); | |
156 | } | |
157 | ||
158 | ||
159 | void warn_obj_v(const char *obj, const char *p, va_list args) | |
160 | { | |
161 | warn_obj_line_v(obj, -1, p, args); | |
162 | } | |
163 | ||
164 | ||
0 | 165 | void warn_v(const char *p, va_list args) |
166 | { | |
35 | 167 | DO_DISPATCH(errmsg_v, (p, args)); |
0 | 168 | } |
169 | ||
170 | ||
171 | void warn_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
172 | { | |
35 | 173 | DO_DISPATCH(errmsg_obj_line_v, (obj, line, p, args)); |
0 | 174 | } |
175 | ||
176 | ||
27 | 177 | void warn_err() |
178 | { | |
35 | 179 | DO_DISPATCH(errmsg_err, ()); |
27 | 180 | } |
181 | ||
182 | ||
183 | void warn_err_obj(const char *obj) | |
184 | { | |
35 | 185 | DO_DISPATCH(errmsg_err_obj, (obj)); |
27 | 186 | } |
187 | ||
188 | void warn_err_obj_line(const char *obj, int line) | |
189 | { | |
35 | 190 | DO_DISPATCH(errmsg_err_obj_line, (obj, line)); |
27 | 191 | } |
192 | ||
193 | ||
194 | /* errmsg | |
195 | */ | |
196 | ||
197 | #define CALL_V_RET(NAME, ARGS) \ | |
35 | 198 | char *ret; va_list args; va_start(args, p); ret=NAME ARGS; \ |
199 | va_end(args); return ret; | |
27 | 200 | |
201 | ||
202 | char* errmsg(const char *p, ...) | |
203 | { | |
204 | CALL_V_RET(errmsg_v, (p, args)); | |
205 | } | |
206 | ||
207 | ||
208 | char *errmsg_obj(const char *obj, const char *p, ...) | |
209 | { | |
210 | CALL_V_RET(errmsg_obj_v, (obj, p, args)); | |
211 | } | |
212 | ||
213 | ||
214 | char *errmsg_obj_line(const char *obj, int line, const char *p, ...) | |
215 | { | |
216 | CALL_V_RET(errmsg_obj_line_v, (obj, line, p, args)); | |
217 | } | |
218 | ||
219 | ||
220 | char* errmsg_obj_v(const char *obj, const char *p, va_list args) | |
221 | { | |
222 | return errmsg_obj_line_v(obj, -1, p, args); | |
223 | } | |
224 | ||
225 | ||
226 | char *errmsg_v(const char *p, va_list args) | |
227 | { | |
228 | char *res; | |
229 | vasprintf(&res, p, args); | |
230 | return res; | |
231 | } | |
232 | ||
233 | ||
25 | 234 | char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args) |
235 | { | |
236 | char *res1=NULL, *res2, *res3; | |
35 | 237 | |
25 | 238 | if(obj!=NULL){ |
239 | if(line>0) | |
240 | asprintf(&res1, TR("%s:%d: "), obj, line); | |
241 | else | |
242 | asprintf(&res1, "%s: ", obj); | |
243 | }else{ | |
244 | if(line>0) | |
245 | asprintf(&res1, TR("%d: "), line); | |
246 | } | |
35 | 247 | vasprintf(&res2, p, args); |
25 | 248 | if(res1!=NULL){ |
249 | if(res2==NULL) | |
250 | return NULL; | |
251 | res3=scat(res1, res2); | |
252 | free(res1); | |
253 | free(res2); | |
254 | return res3; | |
255 | } | |
256 | return res2; | |
257 | } | |
258 | ||
259 | ||
260 | char *errmsg_err() | |
261 | { | |
262 | char *res; | |
263 | asprintf(&res, "%s\n", strerror(errno)); | |
264 | return res; | |
265 | } | |
266 | ||
267 | ||
268 | char *errmsg_err_obj(const char *obj) | |
269 | { | |
270 | char *res; | |
271 | if(obj!=NULL) | |
272 | asprintf(&res, "%s: %s\n", obj, strerror(errno)); | |
273 | else | |
274 | asprintf(&res, "%s\n", strerror(errno)); | |
275 | return res; | |
0 | 276 | } |
277 | ||
278 | ||
25 | 279 | char *errmsg_err_obj_line(const char *obj, int line) |
280 | { | |
281 | char *res; | |
282 | if(obj!=NULL){ | |
283 | if(line>0) | |
284 | asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno)); | |
285 | else | |
286 | asprintf(&res, "%s: %s\n", obj, strerror(errno)); | |
287 | }else{ | |
288 | if(line>0) | |
289 | asprintf(&res, TR("%d: %s\n"), line, strerror(errno)); | |
290 | else | |
291 | asprintf(&res, TR("%s\n"), strerror(errno)); | |
292 | } | |
293 | return res; | |
294 | } | |
31 | 295 | |
25 | 296 | |
0 | 297 | /* die |
298 | */ | |
299 | ||
35 | 300 | |
0 | 301 | void die(const char *p, ...) |
302 | { | |
35 | 303 | set_warn_handler(NULL); |
25 | 304 | CALL_V(die_v, (p, args)); |
0 | 305 | } |
306 | ||
307 | ||
308 | void die_v(const char *p, va_list args) | |
309 | { | |
35 | 310 | set_warn_handler(NULL); |
0 | 311 | warn_v(p, args); |
312 | exit(EXIT_FAILURE); | |
313 | } | |
314 | ||
315 | ||
316 | void die_obj(const char *obj, const char *p, ...) | |
317 | { | |
35 | 318 | set_warn_handler(NULL); |
25 | 319 | CALL_V(die_obj_v, (obj, p, args)); |
0 | 320 | } |
321 | ||
322 | ||
323 | void die_obj_line(const char *obj, int line, const char *p, ...) | |
324 | { | |
35 | 325 | set_warn_handler(NULL); |
25 | 326 | CALL_V(die_obj_line_v, (obj, line, p, args)); |
0 | 327 | } |
328 | ||
329 | ||
330 | void die_obj_v(const char *obj, const char *p, va_list args) | |
331 | { | |
35 | 332 | set_warn_handler(NULL); |
0 | 333 | warn_obj_v(obj, p, args); |
334 | exit(EXIT_FAILURE); | |
335 | } | |
336 | ||
337 | ||
338 | void die_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
339 | { | |
35 | 340 | set_warn_handler(NULL); |
0 | 341 | warn_obj_line_v(obj, line, p, args); |
342 | exit(EXIT_FAILURE); | |
343 | } | |
344 | ||
345 | ||
346 | void die_err() | |
347 | { | |
35 | 348 | set_warn_handler(NULL); |
0 | 349 | warn_err(); |
350 | exit(EXIT_FAILURE); | |
351 | } | |
352 | ||
353 | ||
354 | void die_err_obj(const char *obj) | |
355 | { | |
35 | 356 | set_warn_handler(NULL); |
0 | 357 | warn_err_obj(obj); |
358 | exit(EXIT_FAILURE); | |
359 | } | |
360 | ||
361 | ||
362 | void die_err_obj_line(const char *obj, int line) | |
363 | { | |
35 | 364 | set_warn_handler(NULL); |
0 | 365 | warn_err_obj_line(obj, line); |
366 | exit(EXIT_FAILURE); | |
367 | } | |
35 | 368 | |
369 | ||
370 | static void default_warn_handler(const char *message) | |
371 | { | |
372 | put_prog_name(); | |
373 | fprintf(stderr, "%s", message); | |
374 | putc('\n', stderr); | |
375 | } | |
376 | ||
377 | ||
378 | static void do_dispatch_message(const char *message) | |
379 | { | |
380 | if(current_warn_handler!=NULL) | |
381 | current_warn_handler(message); | |
382 | else | |
383 | default_warn_handler(message); | |
384 | } | |
385 | ||
386 | ||
387 | WarnHandler *set_warn_handler(WarnHandler *handler) | |
388 | { | |
389 | WarnHandler *old=current_warn_handler; | |
390 | current_warn_handler=handler; | |
391 | return old; | |
392 | } |