diff options
author | Antti-Juhani Kaijanaho <ajk@debian.org> | 2008-07-05 13:37:37 +0300 |
---|---|---|
committer | Antti-Juhani Kaijanaho <ajk@debian.org> | 2008-07-05 15:51:48 +0300 |
commit | f35dc6d61bd6b5a4c661c0d31a43a46ad5f555a9 (patch) | |
tree | 21b9db60e3f2b6fdc68d970eca1385da51ecfe66 /lib | |
parent | 0314f5e77a0bc238795008f74f7b2e5fd2c72e06 (diff) | |
download | dctrl-tools-f35dc6d61bd6b5a4c661c0d31a43a46ad5f555a9.tar.gz |
msg.[ch] (and others): allow the message functions to handle format strings
There are some places in the code where it would simplify both the code
and the translations to have message strings be printf-formatted. This
change changes the message function interfaces so that they can take
a variable number of arguments. This required changes at all call sites.
At the same time, the key logic of the message functions is moved out of
the headers, leaving only the "shall we output this" test for inlining.
Signed-off-by: Antti-Juhani Kaijanaho <ajk@debian.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/misc.c | 6 | ||||
-rw-r--r-- | lib/msg.c | 16 | ||||
-rw-r--r-- | lib/msg.h | 41 | ||||
-rw-r--r-- | lib/paragraph.c | 16 | ||||
-rw-r--r-- | lib/predicate.c | 8 | ||||
-rw-r--r-- | lib/sorter.c | 6 |
6 files changed, 57 insertions, 36 deletions
@@ -1,5 +1,5 @@ /* dctrl-tools - Debian control file inspection tools - Copyright © 2004 Antti-Juhani Kaijanaho + Copyright © 2004, 2008 Antti-Juhani Kaijanaho This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -51,14 +51,14 @@ int to_stdout (const char * fname) f = fopen (fname, "r"); if (f == 0) { - message (L_FATAL, strerror (errno), COPYING); + message(L_FATAL, COPYING, "%s", strerror (errno)); return 0; } while ( ( c = getc (f)) != EOF) putc(c, t); if (ferror (f)) { - message (L_FATAL, strerror (errno), COPYING); + message(L_FATAL, COPYING, "%s", strerror(errno)); rv = 0; } @@ -1,5 +1,5 @@ /* grep-dctrl - grep Debian control files - Copyright © 1999, 2004 Antti-Juhani Kaijanaho + Copyright © 1999, 2004, 2008 Antti-Juhani Kaijanaho This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -89,3 +89,17 @@ str2loglevel (const char * s) return avec[i].integer; return -1; } + +void +msg_primitive(const char *fname, int line, const char *fmt, va_list ap) +{ + if (fname == 0) { + fprintf(stderr, "%s: ", get_progname()); + } else if (line > 0) { + fprintf(stderr, "%s: %s:%i: ", get_progname(), fname, line); + } else { + fprintf(stderr, "%s: %s: ", get_progname(), fname); + } + vfprintf(stderr, fmt, ap); + fputs("\n.", stderr); +} @@ -1,5 +1,5 @@ /* dctrl-tools - Debian control file inspection tools - Copyright © 1999 Antti-Juhani Kaijanaho + Copyright © 1999, 2008 Antti-Juhani Kaijanaho This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -69,30 +69,39 @@ void record_error(void) errors = 1; } +void +msg_primitive(const char *fname, int line, const char *fmt, va_list ap); + +static void +line_message(int severity, const char *fname, int line, const char *fmt, ...) + __attribute__((format(printf,4,5))); + inline static void -line_message (int severity, const char * s, const char * fname, int line) +line_message(int severity, const char *fname, int line, const char *fmt, ...) { if (do_msg(severity)) { - - if (fname == 0) { - fprintf (stderr, "%s: %s.\n", get_progname(), s); - } else { - if (line > 0) { - fprintf (stderr, "%s: %s:%i: %s.\n", - get_progname(), fname, line, s); - } else { - fprintf (stderr, "%s: %s: %s.\n", - get_progname(), fname, s); - } - } + va_list ap; + va_start(ap, fmt); + msg_primitive(fname, line, fmt, ap); + va_end(ap); if (severity >= L_IMPORTANT) record_error(); } } +static void +message (int severity, const char * fname, const char * s, ...) + __attribute__((format(printf,3,4))); + inline static void -message (int severity, const char * s, const char * fname) +message (int severity, const char * fname, const char * fmt, ...) { - line_message (severity, s, fname, 0); + if (do_msg(severity)) { + va_list ap; + va_start(ap, fmt); + msg_primitive(fname, 0, fmt, ap); + va_end(ap); + if (severity >= L_IMPORTANT) record_error(); + } } #ifndef MSG_C__ diff --git a/lib/paragraph.c b/lib/paragraph.c index 72d648a..f48fbcc 100644 --- a/lib/paragraph.c +++ b/lib/paragraph.c @@ -1,5 +1,5 @@ /* dctrl-tools - Debian control file inspection tools - Copyright © 2003, 2004 Antti-Juhani Kaijanaho + Copyright © 2003, 2004, 2008 Antti-Juhani Kaijanaho This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -118,16 +118,14 @@ FIELD_NAME: switch (c) { case '\n': case -1: if (pp->ignore_broken_paras) { - line_message(L_IMPORTANT, - _("warning: expected a colon"), - fp->fname, - c == '\n' ?line-1:line); + line_message(L_IMPORTANT, fp->fname, + c == '\n' ?line-1:line, + _("warning: expected a colon")); goto FAIL; } else { - line_message(L_FATAL, - _("expected a colon"), - fp->fname, - c == '\n' ?line-1:line); + line_message(L_FATAL, fp->fname, + c == '\n' ?line-1:line, + _("expected a colon")); fail(); } break; diff --git a/lib/predicate.c b/lib/predicate.c index dd5fbf2..2657457 100644 --- a/lib/predicate.c +++ b/lib/predicate.c @@ -1,5 +1,5 @@ /* dctrl-tools - Debian control file inspection tools - Copyright © 2003, 2004 Antti-Juhani Kaijanaho + Copyright © 2003, 2004, 2008 Antti-Juhani Kaijanaho This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -37,7 +37,7 @@ void addinsn(struct predicate * p, int insn) { if (insn == I_NOP) return; if (p->proglen >= MAX_OPS) { - message(L_FATAL, _("predicate is too complex"), 0); + message(L_FATAL, 0, _("predicate is too complex")); fail(); } p->program[p->proglen++] = insn; @@ -69,7 +69,7 @@ void predicate_finish_atom(struct predicate * p) char * s; s = get_regerror(rerr, &atom->regex); if (s == 0) fatal_enomem(0); - message(L_FATAL, s, 0); + message(L_FATAL, 0, "%s", s); free(s); fail(); } @@ -136,7 +136,7 @@ static bool verify_atom(struct atom * atom, para_t * para) assert(regex_errcode != 0 && regex_errcode != REG_NOMATCH); s = get_regerror (regex_errcode, &atom->regex); if (s == 0) { enomem (0); return false; } - message(L_IMPORTANT, s, 0); + message(L_IMPORTANT, 0, "%s", s); free(s); return false; } diff --git a/lib/sorter.c b/lib/sorter.c index 9f86f03..fad4fb6 100644 --- a/lib/sorter.c +++ b/lib/sorter.c @@ -1,5 +1,5 @@ /* dctrl-tools - Debian control file inspection tools - Copyright © 2004, 2005 Antti-Juhani Kaijanaho + Copyright © 2004, 2005, 2008 Antti-Juhani Kaijanaho This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -45,8 +45,8 @@ int para_compare(keys_t *keys, const para_t *a, const para_t *b) bool aok = parse_version(&ar, af, strlen(af)); bool bok = parse_version(&br, bf, strlen(bf)); if (!aok || !bok) { - message(L_IMPORTANT, - _("Parse error in field."), 0); + message(L_IMPORTANT, 0, + _("Parse error in field.")); free(af); free(bf); return aok ? (bok ? 0 : 1) : (bok ? -1 : 0); |