Fri, 27 Oct 2000 15:17:27 +0200
trunk: changeset 28
Added errmsg_* functions alike warn_* -- asprintf and vasprintf
required now.
Makefile | file | annotate | diff | comparison | revisions | |
README | file | annotate | diff | comparison | revisions | |
include/libtu/output.h | file | annotate | diff | comparison | revisions | |
output.c | file | annotate | diff | comparison | revisions |
--- a/Makefile Mon Oct 02 17:57:46 2000 +0200 +++ b/Makefile Fri Oct 27 15:17:27 2000 +0200 @@ -12,6 +12,12 @@ OBJS= misc.o output.o util.o optparser.o parser.o tokenizer.o +ifndef HAS_SYSTEM_ASPRINTF +OBJS += ../snprintf_2.2/snprintf.o +else +CFLAGS += -DHAS_SYSTEM_ASPRINTF +endif + LIBDIR=$(PREFIX)/lib INCDIR=$(PREFIX)/include/libtu
--- a/README Mon Oct 02 17:57:46 2000 +0200 +++ b/README Fri Oct 27 15:17:27 2000 +0200 @@ -20,3 +20,12 @@ If you want to use it in your programs, you may try to figure out how by reading the header files (include/*.h) and test programs (tester*.c). Or you could try urging me to write some sort of a manual. + +--- + +Libtu needs the functions asprintf and vasprintf. These do not +exist on most platforms. One implementation is available at +<http://www.ijs.si/software/snprintf/>, which is used by default +and is supposed to be found at ../snprintf_2.2. To use the +system's versions, if available, modify system.mk. +
--- a/include/libtu/output.h Mon Oct 02 17:57:46 2000 +0200 +++ b/include/libtu/output.h Fri Oct 27 15:17:27 2000 +0200 @@ -45,4 +45,17 @@ extern void warn_err_obj(const char *obj); extern void warn_err_obj_line(const char *obj, int line); + +extern char *errmsg(const char *p, ...); +extern char *errmsg_v(const char *p, va_list args); + +extern char *errmsg_obj(const char *obj, const char *p, ...); +extern char *errmsg_obj_v(const char *obj, const char *p, va_list args); +extern char *errmsg_obj_line(const char *obj, int line, const char *p, ...); +extern char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args); + +extern char *errmsg_err(); +extern char *errmsg_err_obj(const char *obj); +extern char *errmsg_err_obj_line(const char *obj, int line); + #endif /* LIBTU_OUTPUT_H */
--- a/output.c Mon Oct 02 17:57:46 2000 +0200 +++ b/output.c Fri Oct 27 15:17:27 2000 +0200 @@ -15,6 +15,12 @@ #include <libtu/output.h> #include <libtu/util.h> +#ifndef HAS_SYSTEM_ASPRINTF +#define NEED_ASPRINTF +#define NEED_VASPRINTF +#include "../snprintf_2.2/snprintf.h" +#endif + /* verbose */ @@ -96,15 +102,56 @@ } +#define CALL_V(NAME, ARGS) \ + va_list args; va_start(args, p); NAME ARGS; va_end(args); +#define CALL_V_RET(NAME, ARGS) \ + char *ret; va_list args; va_start(args, p); ret=NAME ARGS; va_end(args); return ret; + void warn(const char *p, ...) { - va_list args; - - va_start(args, p); - - warn_v(p, args); - - va_end(args); + CALL_V(warn_v, (p, args)); +} + + +char* errmsg(const char *p, ...) +{ + CALL_V_RET(errmsg_v, (p, args)); +} + + +void warn_obj(const char *obj, const char *p, ...) +{ + CALL_V(warn_obj_v, (obj, p, args)); +} + + +char *errmsg_obj(const char *obj, const char *p, ...) +{ + CALL_V_RET(errmsg_obj_v, (obj, p, args)); +} + + +void warn_obj_line(const char *obj, int line, const char *p, ...) +{ + CALL_V(warn_obj_line_v, (obj, line, p, args)); +} + + +char *errmsg_obj_line(const char *obj, int line, const char *p, ...) +{ + CALL_V_RET(errmsg_obj_line_v, (obj, line, p, args)); +} + + +void warn_obj_v(const char *obj, const char *p, va_list args) +{ + warn_obj_line_v(obj, -1, p, args); +} + + +char* errmsg_obj_v(const char *obj, const char *p, va_list args) +{ + return errmsg_obj_line_v(obj, -1, p, args); } @@ -116,37 +163,11 @@ } -void warn_obj(const char *obj, const char *p, ...) -{ - va_list args; - - va_start(args, p); - - warn_obj_v(obj, p, args); - - va_end(args); -} - - -void warn_obj_line(const char *obj, int line, const char *p, ...) +char *errmsg_v(const char *p, va_list args) { - va_list args; - - va_start(args, p); - - warn_obj_line_v(obj, line, p, args); - - va_end(args); -} - - -void warn_obj_v(const char *obj, const char *p, va_list args) -{ - put_prog_name(); - if(obj!=NULL) - fprintf(stderr,"%s: ", obj); - vfprintf(stderr, p, args); - putc('\n', stderr); + char *res; + vasprintf(&res, p, args); + return res; } @@ -167,6 +188,31 @@ } +char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args) +{ + char *res1=NULL, *res2, *res3; + if(obj!=NULL){ + if(line>0) + asprintf(&res1, TR("%s:%d: "), obj, line); + else + asprintf(&res1, "%s: ", obj); + }else{ + if(line>0) + asprintf(&res1, TR("%d: "), line); + } + asprintf(&res2, p, args); + if(res1!=NULL){ + if(res2==NULL) + return NULL; + res3=scat(res1, res2); + free(res1); + free(res2); + return res3; + } + return res2; +} + + void warn_err() { put_prog_name(); @@ -174,6 +220,14 @@ } +char *errmsg_err() +{ + char *res; + asprintf(&res, "%s\n", strerror(errno)); + return res; +} + + void warn_err_obj(const char *obj) { put_prog_name(); @@ -181,7 +235,17 @@ fprintf(stderr, "%s: %s\n", obj, strerror(errno)); else fprintf(stderr, "%s\n", strerror(errno)); +} + +char *errmsg_err_obj(const char *obj) +{ + char *res; + if(obj!=NULL) + asprintf(&res, "%s: %s\n", obj, strerror(errno)); + else + asprintf(&res, "%s\n", strerror(errno)); + return res; } @@ -203,18 +267,30 @@ } +char *errmsg_err_obj_line(const char *obj, int line) +{ + char *res; + if(obj!=NULL){ + if(line>0) + asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno)); + else + asprintf(&res, "%s: %s\n", obj, strerror(errno)); + }else{ + if(line>0) + asprintf(&res, TR("%d: %s\n"), line, strerror(errno)); + else + asprintf(&res, TR("%s\n"), strerror(errno)); + } + return res; +} + + /* die */ void die(const char *p, ...) { - va_list args; - - va_start(args, p); - - die_v(p, args); - - va_end(args); + CALL_V(die_v, (p, args)); } @@ -227,21 +303,13 @@ void die_obj(const char *obj, const char *p, ...) { - va_list args; - - va_start(args, p); - die_obj_v(obj, p, args); - va_end(args); + CALL_V(die_obj_v, (obj, p, args)); } void die_obj_line(const char *obj, int line, const char *p, ...) { - va_list args; - - va_start(args, p); - die_obj_line_v(obj, line, p, args); - va_end(args); + CALL_V(die_obj_line_v, (obj, line, p, args)); }