From 35684c08a8429cf1c1148c74d2df8cdf4c31bbac Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 13 Mar 2021 18:53:31 -0500 Subject: [PATCH] Improve cdecl99 error output. Ensure that printed error messages are appropriately prefixed, and mark some missed strings as translateable. --- src/cdecl99.c | 29 ++++++++++++++++++++++++----- src/cdecl99.h | 2 ++ src/commands.c | 4 ++-- src/execute.gperf | 3 +-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/cdecl99.c b/src/cdecl99.c index 2a522d4..27ce048 100644 --- a/src/cdecl99.c +++ b/src/cdecl99.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -37,6 +38,7 @@ #include "cdecl.h" static const char *progname = "cdecl99"; +static bool interactive = true; #include "options.h" static const char sopts[] = SOPT_STRING; @@ -45,6 +47,21 @@ static const struct option lopts[] = { {0} }; +void print_error(const char *fmt, ...) +{ + va_list(ap); + + if (!interactive) + fprintf(stderr, "%s: ", progname); + fprintf(stderr, "%s", _("error: ")); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fprintf(stderr, "\n"); +} + static void print_version(void) { const char *copysign = "(C)"; @@ -236,8 +253,7 @@ static int repl_noninteractive(void) free(line); if (ferror(stdin)) { - errno = saved_errno; - perror("read error"); + print_error("%s", strerror(saved_errno)); return -1; } @@ -257,7 +273,7 @@ static void init_i18n(void) int main(int argc, char **argv) { - bool show_intro = true, interactive = true, execute = false; + bool show_intro = true, execute = false; const char *filename = NULL; int i, opt, rc; @@ -296,7 +312,9 @@ int main(int argc, char **argv) } if (optind < argc) { - fprintf(stderr, _("%s: excess command-line arguments:"), + fprintf(stderr, "%s: ", progname); + + fprintf(stderr, _("excess command-line arguments:"), progname); for (i = optind; i < argc; i++) { fprintf(stderr, " %s", argv[i]); @@ -317,7 +335,8 @@ int main(int argc, char **argv) /* --execute supersedes --filename */ if (filename && !execute) { if (!freopen(filename, "r", stdin)) { - perror(filename); + print_error("failed to open %s: %s\n", filename, + strerror(errno)); return EXIT_FAILURE; } } diff --git a/src/cdecl99.h b/src/cdecl99.h index e1030d4..254b29f 100644 --- a/src/cdecl99.h +++ b/src/cdecl99.h @@ -28,6 +28,8 @@ int run_command_simplify(const char *arg); int run_command_explain(const char *arg); int run_command_declare(const char *cmdarg); +void print_error(const char *fmt, ...); + #if HAVE_RL_ADD_HISTORY && HAVE_READLINE_HISTORY_H # include #else diff --git a/src/commands.c b/src/commands.c index d435f50..dea4219 100644 --- a/src/commands.c +++ b/src/commands.c @@ -45,7 +45,7 @@ retry: tmp = realloc(buf, rc + 1); if (!tmp) { - fprintf(stderr, "%s\n", _("failed to allocate memory")); + print_error("%s", _("failed to allocate memory)")); return NULL; } @@ -67,7 +67,7 @@ int run_command_explain(const char *arg) decl = cdecl_parse_decl(arg); if (!decl) { err = cdecl_get_error(); - fprintf(stderr, "%s\n", err->str); + print_error("%s", err->str); goto out; } diff --git a/src/execute.gperf b/src/execute.gperf index 5f6740e..b1e7d59 100644 --- a/src/execute.gperf +++ b/src/execute.gperf @@ -94,8 +94,7 @@ int run_command(const char *line, int interactive) c = in_word_set(cmd, arg-cmd); if (!c) { - fprintf(stderr, _("unknown command %.*s\n"), - (int)(arg-cmd), cmd); + print_error(_("unknown command %.*s"), (int)(arg-cmd), cmd); if (interactive) { fprintf(stderr, "%s\n", _("Try \"help\" for a list of possible commands.")); -- 2.43.0