output.c

changeset 25
9d41e3611050
parent 9
55e7f2ff6021
child 26
ebc92fce5a3c
equal deleted inserted replaced
24:c3fa6c2785dd 25:9d41e3611050
13 13
14 #include <libtu/misc.h> 14 #include <libtu/misc.h>
15 #include <libtu/output.h> 15 #include <libtu/output.h>
16 #include <libtu/util.h> 16 #include <libtu/util.h>
17 17
18 #ifndef HAS_SYSTEM_ASPRINTF
19 #define NEED_ASPRINTF
20 #define NEED_VASPRINTF
21 #include "../snprintf_2.2/snprintf.h"
22 #endif
23
18 24
19 /* verbose 25 /* verbose
20 */ 26 */
21 27
22 static bool verbose_mode=FALSE; 28 static bool verbose_mode=FALSE;
94 100
95 fprintf(stderr, "%s: ", (char*)progname); 101 fprintf(stderr, "%s: ", (char*)progname);
96 } 102 }
97 103
98 104
105 #define CALL_V(NAME, ARGS) \
106 va_list args; va_start(args, p); NAME ARGS; va_end(args);
107 #define CALL_V_RET(NAME, ARGS) \
108 char *ret; va_list args; va_start(args, p); ret=NAME ARGS; va_end(args); return ret;
109
99 void warn(const char *p, ...) 110 void warn(const char *p, ...)
100 { 111 {
101 va_list args; 112 CALL_V(warn_v, (p, args));
102 113 }
103 va_start(args, p); 114
104 115
105 warn_v(p, args); 116 char* errmsg(const char *p, ...)
106 117 {
107 va_end(args); 118 CALL_V_RET(errmsg_v, (p, args));
119 }
120
121
122 void warn_obj(const char *obj, const char *p, ...)
123 {
124 CALL_V(warn_obj_v, (obj, p, args));
125 }
126
127
128 char *errmsg_obj(const char *obj, const char *p, ...)
129 {
130 CALL_V_RET(errmsg_obj_v, (obj, p, args));
131 }
132
133
134 void warn_obj_line(const char *obj, int line, const char *p, ...)
135 {
136 CALL_V(warn_obj_line_v, (obj, line, p, args));
137 }
138
139
140 char *errmsg_obj_line(const char *obj, int line, const char *p, ...)
141 {
142 CALL_V_RET(errmsg_obj_line_v, (obj, line, p, args));
143 }
144
145
146 void warn_obj_v(const char *obj, const char *p, va_list args)
147 {
148 warn_obj_line_v(obj, -1, p, args);
149 }
150
151
152 char* errmsg_obj_v(const char *obj, const char *p, va_list args)
153 {
154 return errmsg_obj_line_v(obj, -1, p, args);
108 } 155 }
109 156
110 157
111 void warn_v(const char *p, va_list args) 158 void warn_v(const char *p, va_list args)
112 { 159 {
114 vfprintf(stderr, p, args); 161 vfprintf(stderr, p, args);
115 putc('\n', stderr); 162 putc('\n', stderr);
116 } 163 }
117 164
118 165
119 void warn_obj(const char *obj, const char *p, ...) 166 char *errmsg_v(const char *p, va_list args)
120 { 167 {
121 va_list args; 168 char *res;
122 169 vasprintf(&res, p, args);
123 va_start(args, p); 170 return res;
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 } 171 }
151 172
152 173
153 void warn_obj_line_v(const char *obj, int line, const char *p, va_list args) 174 void warn_obj_line_v(const char *obj, int line, const char *p, va_list args)
154 { 175 {
165 vfprintf(stderr, p, args); 186 vfprintf(stderr, p, args);
166 putc('\n', stderr); 187 putc('\n', stderr);
167 } 188 }
168 189
169 190
191 char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args)
192 {
193 char *res1=NULL, *res2, *res3;
194 if(obj!=NULL){
195 if(line>0)
196 asprintf(&res1, TR("%s:%d: "), obj, line);
197 else
198 asprintf(&res1, "%s: ", obj);
199 }else{
200 if(line>0)
201 asprintf(&res1, TR("%d: "), line);
202 }
203 asprintf(&res2, p, args);
204 if(res1!=NULL){
205 if(res2==NULL)
206 return NULL;
207 res3=scat(res1, res2);
208 free(res1);
209 free(res2);
210 return res3;
211 }
212 return res2;
213 }
214
215
170 void warn_err() 216 void warn_err()
171 { 217 {
172 put_prog_name(); 218 put_prog_name();
173 fprintf(stderr, "%s\n", strerror(errno)); 219 fprintf(stderr, "%s\n", strerror(errno));
220 }
221
222
223 char *errmsg_err()
224 {
225 char *res;
226 asprintf(&res, "%s\n", strerror(errno));
227 return res;
174 } 228 }
175 229
176 230
177 void warn_err_obj(const char *obj) 231 void warn_err_obj(const char *obj)
178 { 232 {
179 put_prog_name(); 233 put_prog_name();
180 if(obj!=NULL) 234 if(obj!=NULL)
181 fprintf(stderr, "%s: %s\n", obj, strerror(errno)); 235 fprintf(stderr, "%s: %s\n", obj, strerror(errno));
182 else 236 else
183 fprintf(stderr, "%s\n", strerror(errno)); 237 fprintf(stderr, "%s\n", strerror(errno));
184 238 }
239
240
241 char *errmsg_err_obj(const char *obj)
242 {
243 char *res;
244 if(obj!=NULL)
245 asprintf(&res, "%s: %s\n", obj, strerror(errno));
246 else
247 asprintf(&res, "%s\n", strerror(errno));
248 return res;
185 } 249 }
186 250
187 251
188 void warn_err_obj_line(const char *obj, int line) 252 void warn_err_obj_line(const char *obj, int line)
189 { 253 {
201 } 265 }
202 266
203 } 267 }
204 268
205 269
270 char *errmsg_err_obj_line(const char *obj, int line)
271 {
272 char *res;
273 if(obj!=NULL){
274 if(line>0)
275 asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno));
276 else
277 asprintf(&res, "%s: %s\n", obj, strerror(errno));
278 }else{
279 if(line>0)
280 asprintf(&res, TR("%d: %s\n"), line, strerror(errno));
281 else
282 asprintf(&res, TR("%s\n"), strerror(errno));
283 }
284 return res;
285 }
286
287
206 /* die 288 /* die
207 */ 289 */
208 290
209 void die(const char *p, ...) 291 void die(const char *p, ...)
210 { 292 {
211 va_list args; 293 CALL_V(die_v, (p, args));
212
213 va_start(args, p);
214
215 die_v(p, args);
216
217 va_end(args);
218 } 294 }
219 295
220 296
221 void die_v(const char *p, va_list args) 297 void die_v(const char *p, va_list args)
222 { 298 {
225 } 301 }
226 302
227 303
228 void die_obj(const char *obj, const char *p, ...) 304 void die_obj(const char *obj, const char *p, ...)
229 { 305 {
230 va_list args; 306 CALL_V(die_obj_v, (obj, p, args));
231
232 va_start(args, p);
233 die_obj_v(obj, p, args);
234 va_end(args);
235 } 307 }
236 308
237 309
238 void die_obj_line(const char *obj, int line, const char *p, ...) 310 void die_obj_line(const char *obj, int line, const char *p, ...)
239 { 311 {
240 va_list args; 312 CALL_V(die_obj_line_v, (obj, line, p, args));
241
242 va_start(args, p);
243 die_obj_line_v(obj, line, p, args);
244 va_end(args);
245 } 313 }
246 314
247 315
248 void die_obj_v(const char *obj, const char *p, va_list args) 316 void die_obj_v(const char *obj, const char *p, va_list args)
249 { 317 {

mercurial