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