Tue, 29 Aug 2000 00:03:00 +0200
trunk: changeset 26
Once again, 'make install' portability fixes
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 | |
18 | ||
19 | /* verbose | |
20 | */ | |
21 | ||
22 | static bool verbose_mode=FALSE; | |
23 | static int verbose_indent_lvl=0; | |
24 | static bool progname_enable=TRUE; | |
25 | ||
26 | #define INDENTATOR_LENGTH 4 | |
27 | ||
28 | static char indentator[]={' ', ' ', ' ', ' '}; | |
29 | ||
30 | ||
31 | void verbose(const char *p, ...) | |
32 | { | |
33 | va_list args; | |
34 | ||
35 | va_start(args, p); | |
36 | ||
37 | verbose_v(p, args); | |
38 | ||
39 | va_end(args); | |
40 | } | |
41 | ||
42 | ||
43 | void verbose_v(const char *p, va_list args) | |
44 | { | |
45 | int i; | |
46 | ||
47 | if(verbose_mode){ | |
48 | for(i=0; i<verbose_indent_lvl; i++) | |
49 | writef(stdout, indentator, INDENTATOR_LENGTH); | |
50 | ||
51 | vprintf(p, args); | |
52 | fflush(stdout); | |
53 | } | |
54 | } | |
55 | ||
56 | ||
57 | void verbose_enable(bool enable) | |
58 | { | |
59 | verbose_mode=enable; | |
60 | } | |
61 | ||
62 | ||
63 | int verbose_indent(int depth) | |
64 | { | |
65 | int old=verbose_indent_lvl; | |
66 | ||
67 | if(depth>=0) | |
68 | verbose_indent_lvl=depth; | |
69 | ||
70 | return old; | |
71 | } | |
72 | ||
73 | ||
74 | /* warn | |
75 | */ | |
76 | ||
77 | void warn_progname_enable(bool enable) | |
78 | { | |
79 | progname_enable=enable; | |
80 | } | |
81 | ||
82 | ||
83 | static void put_prog_name() | |
84 | { | |
85 | const char*progname; | |
86 | ||
87 | if(!progname_enable) | |
88 | return; | |
89 | ||
90 | progname=prog_execname(); | |
91 | ||
92 | if(progname==NULL) | |
93 | return; | |
94 | ||
95 | fprintf(stderr, "%s: ", (char*)progname); | |
96 | } | |
97 | ||
98 | ||
99 | void warn(const char *p, ...) | |
100 | { | |
101 | va_list args; | |
102 | ||
103 | va_start(args, p); | |
104 | ||
105 | warn_v(p, args); | |
106 | ||
107 | va_end(args); | |
108 | } | |
109 | ||
110 | ||
111 | void warn_v(const char *p, va_list args) | |
112 | { | |
113 | put_prog_name(); | |
114 | vfprintf(stderr, p, args); | |
115 | putc('\n', stderr); | |
116 | } | |
117 | ||
118 | ||
119 | void warn_obj(const char *obj, const char *p, ...) | |
120 | { | |
121 | va_list args; | |
122 | ||
123 | va_start(args, p); | |
124 | ||
125 | warn_obj_v(obj, p, args); | |
126 | ||
127 | va_end(args); | |
128 | } | |
129 | ||
130 | ||
131 | void warn_obj_line(const char *obj, int line, const char *p, ...) | |
132 | { | |
133 | va_list args; | |
134 | ||
135 | va_start(args, p); | |
136 | ||
137 | warn_obj_line_v(obj, line, p, args); | |
138 | ||
139 | va_end(args); | |
140 | } | |
141 | ||
142 | ||
143 | void warn_obj_v(const char *obj, const char *p, va_list args) | |
144 | { | |
145 | put_prog_name(); | |
146 | if(obj!=NULL) | |
147 | fprintf(stderr,"%s: ", obj); | |
148 | vfprintf(stderr, p, args); | |
149 | putc('\n', stderr); | |
150 | } | |
151 | ||
152 | ||
153 | void warn_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
154 | { | |
155 | put_prog_name(); | |
156 | if(obj!=NULL){ | |
157 | if(line>0) | |
158 | fprintf(stderr, TR("%s:%d: "), obj, line); | |
159 | else | |
160 | fprintf(stderr, "%s: ", obj); | |
161 | }else{ | |
162 | if(line>0) | |
163 | fprintf(stderr, TR("%d: "), line); | |
164 | } | |
165 | vfprintf(stderr, p, args); | |
166 | putc('\n', stderr); | |
167 | } | |
168 | ||
169 | ||
170 | void warn_err() | |
171 | { | |
172 | put_prog_name(); | |
173 | fprintf(stderr, "%s\n", strerror(errno)); | |
174 | } | |
175 | ||
176 | ||
177 | void warn_err_obj(const char *obj) | |
178 | { | |
179 | put_prog_name(); | |
180 | if(obj!=NULL) | |
181 | fprintf(stderr, "%s: %s\n", obj, strerror(errno)); | |
182 | else | |
183 | fprintf(stderr, "%s\n", strerror(errno)); | |
184 | ||
185 | } | |
186 | ||
187 | ||
188 | void warn_err_obj_line(const char *obj, int line) | |
189 | { | |
190 | put_prog_name(); | |
191 | if(obj!=NULL){ | |
192 | if(line>0) | |
193 | fprintf(stderr, TR("%s:%d: %s\n"), obj, line, strerror(errno)); | |
194 | else | |
195 | fprintf(stderr, "%s: %s\n", obj, strerror(errno)); | |
196 | }else{ | |
197 | if(line>0) | |
198 | fprintf(stderr, TR("%d: %s\n"), line, strerror(errno)); | |
199 | else | |
200 | fprintf(stderr, TR("%s\n"), strerror(errno)); | |
201 | } | |
202 | ||
203 | } | |
204 | ||
205 | ||
206 | /* die | |
207 | */ | |
208 | ||
209 | void die(const char *p, ...) | |
210 | { | |
211 | va_list args; | |
212 | ||
213 | va_start(args, p); | |
214 | ||
215 | die_v(p, args); | |
216 | ||
217 | va_end(args); | |
218 | } | |
219 | ||
220 | ||
221 | void die_v(const char *p, va_list args) | |
222 | { | |
223 | warn_v(p, args); | |
224 | exit(EXIT_FAILURE); | |
225 | } | |
226 | ||
227 | ||
228 | void die_obj(const char *obj, const char *p, ...) | |
229 | { | |
230 | va_list args; | |
231 | ||
232 | va_start(args, p); | |
233 | die_obj_v(obj, p, args); | |
234 | va_end(args); | |
235 | } | |
236 | ||
237 | ||
238 | void die_obj_line(const char *obj, int line, const char *p, ...) | |
239 | { | |
240 | va_list args; | |
241 | ||
242 | va_start(args, p); | |
243 | die_obj_line_v(obj, line, p, args); | |
244 | va_end(args); | |
245 | } | |
246 | ||
247 | ||
248 | void die_obj_v(const char *obj, const char *p, va_list args) | |
249 | { | |
250 | warn_obj_v(obj, p, args); | |
251 | exit(EXIT_FAILURE); | |
252 | } | |
253 | ||
254 | ||
255 | void die_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
256 | { | |
257 | warn_obj_line_v(obj, line, p, args); | |
258 | exit(EXIT_FAILURE); | |
259 | } | |
260 | ||
261 | ||
262 | void die_err() | |
263 | { | |
264 | warn_err(); | |
265 | exit(EXIT_FAILURE); | |
266 | } | |
267 | ||
268 | ||
269 | void die_err_obj(const char *obj) | |
270 | { | |
271 | warn_err_obj(obj); | |
272 | exit(EXIT_FAILURE); | |
273 | } | |
274 | ||
275 | ||
276 | void die_err_obj_line(const char *obj, int line) | |
277 | { | |
278 | warn_err_obj_line(obj, line); | |
279 | exit(EXIT_FAILURE); | |
280 | } |