Wed, 20 Dec 2000 20:34:02 +0100
trunk: changeset 32
Use 'ranlib' instead of 'ar s'.
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 | |
0 | 106 | void warn(const char *p, ...) |
107 | { | |
25 | 108 | CALL_V(warn_v, (p, args)); |
109 | } | |
110 | ||
111 | ||
112 | void warn_obj(const char *obj, const char *p, ...) | |
113 | { | |
114 | CALL_V(warn_obj_v, (obj, p, args)); | |
115 | } | |
116 | ||
117 | ||
118 | void warn_obj_line(const char *obj, int line, const char *p, ...) | |
119 | { | |
120 | CALL_V(warn_obj_line_v, (obj, line, p, args)); | |
121 | } | |
122 | ||
123 | ||
124 | void warn_obj_v(const char *obj, const char *p, va_list args) | |
125 | { | |
126 | warn_obj_line_v(obj, -1, p, args); | |
127 | } | |
128 | ||
129 | ||
0 | 130 | void warn_v(const char *p, va_list args) |
131 | { | |
132 | put_prog_name(); | |
133 | vfprintf(stderr, p, args); | |
134 | putc('\n', stderr); | |
135 | } | |
136 | ||
137 | ||
138 | void warn_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
139 | { | |
140 | put_prog_name(); | |
141 | if(obj!=NULL){ | |
142 | if(line>0) | |
143 | fprintf(stderr, TR("%s:%d: "), obj, line); | |
144 | else | |
145 | fprintf(stderr, "%s: ", obj); | |
146 | }else{ | |
147 | if(line>0) | |
148 | fprintf(stderr, TR("%d: "), line); | |
149 | } | |
150 | vfprintf(stderr, p, args); | |
151 | putc('\n', stderr); | |
152 | } | |
153 | ||
154 | ||
27 | 155 | void warn_err() |
156 | { | |
157 | put_prog_name(); | |
158 | fprintf(stderr, "%s\n", strerror(errno)); | |
159 | } | |
160 | ||
161 | ||
162 | void warn_err_obj(const char *obj) | |
163 | { | |
164 | put_prog_name(); | |
165 | if(obj!=NULL) | |
166 | fprintf(stderr, "%s: %s\n", obj, strerror(errno)); | |
167 | else | |
168 | fprintf(stderr, "%s\n", strerror(errno)); | |
169 | } | |
170 | ||
171 | void warn_err_obj_line(const char *obj, int line) | |
172 | { | |
173 | put_prog_name(); | |
174 | if(obj!=NULL){ | |
175 | if(line>0) | |
176 | fprintf(stderr, TR("%s:%d: %s\n"), obj, line, strerror(errno)); | |
177 | else | |
178 | fprintf(stderr, "%s: %s\n", obj, strerror(errno)); | |
179 | }else{ | |
180 | if(line>0) | |
181 | fprintf(stderr, TR("%d: %s\n"), line, strerror(errno)); | |
182 | else | |
183 | fprintf(stderr, TR("%s\n"), strerror(errno)); | |
184 | } | |
185 | ||
186 | } | |
187 | ||
188 | ||
189 | /* errmsg | |
190 | */ | |
191 | #ifndef LIBTU_NO_ERRMSG | |
192 | ||
193 | #define CALL_V_RET(NAME, ARGS) \ | |
194 | char *ret; va_list args; va_start(args, p); ret=NAME ARGS; va_end(args); return ret; | |
195 | ||
196 | ||
197 | char* errmsg(const char *p, ...) | |
198 | { | |
199 | CALL_V_RET(errmsg_v, (p, args)); | |
200 | } | |
201 | ||
202 | ||
203 | char *errmsg_obj(const char *obj, const char *p, ...) | |
204 | { | |
205 | CALL_V_RET(errmsg_obj_v, (obj, p, args)); | |
206 | } | |
207 | ||
208 | ||
209 | char *errmsg_obj_line(const char *obj, int line, const char *p, ...) | |
210 | { | |
211 | CALL_V_RET(errmsg_obj_line_v, (obj, line, p, args)); | |
212 | } | |
213 | ||
214 | ||
215 | char* errmsg_obj_v(const char *obj, const char *p, va_list args) | |
216 | { | |
217 | return errmsg_obj_line_v(obj, -1, p, args); | |
218 | } | |
219 | ||
220 | ||
221 | char *errmsg_v(const char *p, va_list args) | |
222 | { | |
223 | char *res; | |
224 | vasprintf(&res, p, args); | |
225 | return res; | |
226 | } | |
227 | ||
228 | ||
25 | 229 | char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args) |
230 | { | |
231 | char *res1=NULL, *res2, *res3; | |
232 | if(obj!=NULL){ | |
233 | if(line>0) | |
234 | asprintf(&res1, TR("%s:%d: "), obj, line); | |
235 | else | |
236 | asprintf(&res1, "%s: ", obj); | |
237 | }else{ | |
238 | if(line>0) | |
239 | asprintf(&res1, TR("%d: "), line); | |
240 | } | |
241 | asprintf(&res2, p, args); | |
242 | if(res1!=NULL){ | |
243 | if(res2==NULL) | |
244 | return NULL; | |
245 | res3=scat(res1, res2); | |
246 | free(res1); | |
247 | free(res2); | |
248 | return res3; | |
249 | } | |
250 | return res2; | |
251 | } | |
252 | ||
253 | ||
254 | char *errmsg_err() | |
255 | { | |
256 | char *res; | |
257 | asprintf(&res, "%s\n", strerror(errno)); | |
258 | return res; | |
259 | } | |
260 | ||
261 | ||
262 | char *errmsg_err_obj(const char *obj) | |
263 | { | |
264 | char *res; | |
265 | if(obj!=NULL) | |
266 | asprintf(&res, "%s: %s\n", obj, strerror(errno)); | |
267 | else | |
268 | asprintf(&res, "%s\n", strerror(errno)); | |
269 | return res; | |
0 | 270 | } |
271 | ||
272 | ||
25 | 273 | char *errmsg_err_obj_line(const char *obj, int line) |
274 | { | |
275 | char *res; | |
276 | if(obj!=NULL){ | |
277 | if(line>0) | |
278 | asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno)); | |
279 | else | |
280 | asprintf(&res, "%s: %s\n", obj, strerror(errno)); | |
281 | }else{ | |
282 | if(line>0) | |
283 | asprintf(&res, TR("%d: %s\n"), line, strerror(errno)); | |
284 | else | |
285 | asprintf(&res, TR("%s\n"), strerror(errno)); | |
286 | } | |
287 | return res; | |
288 | } | |
27 | 289 | #endif /* LIBTU_NO_ERRMSG */ |
25 | 290 | |
291 | ||
0 | 292 | /* die |
293 | */ | |
294 | ||
295 | void die(const char *p, ...) | |
296 | { | |
25 | 297 | CALL_V(die_v, (p, args)); |
0 | 298 | } |
299 | ||
300 | ||
301 | void die_v(const char *p, va_list args) | |
302 | { | |
303 | warn_v(p, args); | |
304 | exit(EXIT_FAILURE); | |
305 | } | |
306 | ||
307 | ||
308 | void die_obj(const char *obj, const char *p, ...) | |
309 | { | |
25 | 310 | CALL_V(die_obj_v, (obj, p, args)); |
0 | 311 | } |
312 | ||
313 | ||
314 | void die_obj_line(const char *obj, int line, const char *p, ...) | |
315 | { | |
25 | 316 | CALL_V(die_obj_line_v, (obj, line, p, args)); |
0 | 317 | } |
318 | ||
319 | ||
320 | void die_obj_v(const char *obj, const char *p, va_list args) | |
321 | { | |
322 | warn_obj_v(obj, p, args); | |
323 | exit(EXIT_FAILURE); | |
324 | } | |
325 | ||
326 | ||
327 | void die_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
328 | { | |
329 | warn_obj_line_v(obj, line, p, args); | |
330 | exit(EXIT_FAILURE); | |
331 | } | |
332 | ||
333 | ||
334 | void die_err() | |
335 | { | |
336 | warn_err(); | |
337 | exit(EXIT_FAILURE); | |
338 | } | |
339 | ||
340 | ||
341 | void die_err_obj(const char *obj) | |
342 | { | |
343 | warn_err_obj(obj); | |
344 | exit(EXIT_FAILURE); | |
345 | } | |
346 | ||
347 | ||
348 | void die_err_obj_line(const char *obj, int line) | |
349 | { | |
350 | warn_err_obj_line(obj, line); | |
351 | exit(EXIT_FAILURE); | |
352 | } |