]> git.draconx.ca Git - cdecl99.git/commitdiff
Improve cdecl99 error output.
authorNick Bowler <nbowler@draconx.ca>
Sat, 13 Mar 2021 23:53:31 +0000 (18:53 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sun, 14 Mar 2021 03:17:56 +0000 (22:17 -0500)
Ensure that printed error messages are appropriately prefixed,
and mark some missed strings as translateable.

src/cdecl99.c
src/cdecl99.h
src/commands.c
src/execute.gperf

index 2a522d4c268fa32201c9bc05e30c9b37bad5d1d4..27ce048c73c1053e2fd1a074f332b17a92211490 100644 (file)
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <locale.h>
 #include <assert.h>
+#include <stdarg.h>
 
 #include <getopt.h>
 #include <gettext.h>
@@ -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;
                }
        }
index e1030d44994df369cab205ca93b8271703f464d5..254b29fd4e41c8c21428d0a5876c9fc6e3a11791 100644 (file)
@@ -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 <readline/history.h>
 #else
index d435f50ae8e34a8ba598c0fe0b5b4c303dd0792c..dea4219837e624c5f83237ec46efc6fb41d1c061 100644 (file)
@@ -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;
        }
 
index 5f6740e40c7d2ba9bd231f5389f618163592d7b9..b1e7d5931ee2e4746c3c04bf80050cb1ab6583c6 100644 (file)
@@ -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."));