]> git.draconx.ca Git - cdecl99.git/commitdiff
cdecl99: Fix some improper error message formatting.
authorNick Bowler <nbowler@draconx.ca>
Thu, 25 May 2023 23:38:59 +0000 (19:38 -0400)
committerNick Bowler <nbowler@draconx.ca>
Thu, 25 May 2023 23:38:59 +0000 (19:38 -0400)
Errors opening the file specified by a --file option are printed with
two newlines, and errors generated by actual commands are not properly
prefixed with the program name.

Add test cases to catch these specific problems and fix them.

Makefile.am
src/cdecl99.c
src/commands.c
tests/general.at

index 258d84a1cbfff6be01ac58aefbe8c33ccc46b665..3dd5de8c912d5283cc82ba661bf01b38d63008ef 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright © 2011-2013, 2019-2022 Nick Bowler
+# Copyright © 2011-2013, 2019-2023 Nick Bowler
 #
 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
 # This is free software: you are free to do what the fuck you want to.
@@ -302,6 +302,7 @@ EXTRA_DIST += $(GPERFFILES)
 atlocal: config.status
        $(AM_V_GEN) :; { \
          printf ': "$${%s=%s}"\n' \
+           AWK '$(AWK)' \
            EXEEXT '$(EXEEXT)' \
            check_PROGRAMS '$(check_PROGRAMS)' \
            ; } >$@.tmp
index 485fe86df5d0528892441fa54003c93e1dad8118..015cf7a967bf3c39ba276bb2ba3b6a661af9a5d7 100644 (file)
@@ -262,7 +262,7 @@ int main(int argc, char **argv)
        /* --execute supersedes --filename */
        if (filename && !execute) {
                if (!freopen(filename, "r", stdin)) {
-                       print_error("failed to open %s: %s\n", filename,
+                       print_error("failed to open %s: %s", filename,
                                                        strerror(errno));
                        return EXIT_FAILURE;
                }
index 29184d5ac4641b37119d64e851bdfca39c36f842..fd2a5a38349b8ec6c3cb423cd5fef4820697b42c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Main command implementation routines for cdecl99.
- * Copyright © 2011-2012, 2020-2021 Nick Bowler
+ * Copyright © 2011-2012, 2020-2021, 2023 Nick Bowler
  *
  * 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
@@ -66,8 +66,7 @@ int run_command_explain(const char *arg)
 
        decl = cdecl_parse_decl(arg);
        if (!decl) {
-               err = cdecl_get_error();
-               print_error("%s", err->str);
+               print_error("%s", cdecl_get_error()->str);
                goto out;
        }
 
@@ -94,8 +93,7 @@ int run_command_simplify(const char *arg)
 
        decl = cdecl_parse_decl(arg);
        if (!decl) {
-               err = cdecl_get_error();
-               fprintf(stderr, "%s\n", err->str);
+               print_error("%s", cdecl_get_error()->str);
                goto out;
        }
 
@@ -126,7 +124,6 @@ out:
 
 int run_command_declare(const char *cmd)
 {
-       const struct cdecl_error *err;
        struct cdecl *decl;
        const char *str;
        int ret = -1;
@@ -134,8 +131,7 @@ int run_command_declare(const char *cmd)
        /* The name of the command is significant here. */
        decl = cdecl_parse_english(cmd);
        if (!decl) {
-               err = cdecl_get_error();
-               fprintf(stderr, "%s\n", err->str);
+               print_error("%s", cdecl_get_error()->str);
                goto out;
        }
 
index b5a623dabf6783008a1db823156af6c2366e9851..899c7e423734ab2a8e25c70fe901c02b3eece6b8 100644 (file)
@@ -83,3 +83,37 @@ int x()
 ]])
 
 AT_CLEANUP
+
+AT_SETUP([cdecl99 --file error message])
+
+AT_CHECK([cdecl99 --file nonexistent || exit 42], [42], [], [stderr])
+AT_CHECK([grep -v nonexistent stderr || true])
+
+AT_CLEANUP
+
+AT_SETUP([cdecl99 command error messages])
+
+# Extract progname from --help usage message
+# This will only get the start of progname if it includes spaces;
+# so we won't worry too hard about the exact format later.
+AT_CHECK([LC_ALL=C cdecl99 --help], [0], [stdout])
+progname=`$AWK 'NR == 1 { print $2; }' stdout`
+
+# every line is erroneous
+AT_DATA([input],
+[[explain int a b c
+simplify int a b c
+declare a as b c
+]])
+
+AT_DATA([check.awk],
+[[BEGIN { status=0; }
+$1 == progname || $1 == (progname ":") { next; }
+{ status=1; print "unprefixed message on line", NR ":", $0; }
+END { exit(status); }
+]])
+
+AT_CHECK([LC_ALL=C cdecl99 --file=input || exit 42], [42], [], [stderr])
+AT_CHECK([$AWK -v progname="$progname" -f check.awk stderr])
+
+AT_CLEANUP