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