Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: make error messages for option parsin...



details:   https://anonhg.NetBSD.org/src/rev/41bf675ff110
branches:  trunk
changeset: 990484:41bf675ff110
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Oct 28 21:32:48 2021 +0000

description:
indent: make error messages for option parsing more precise

diffstat:

 tests/usr.bin/indent/t_errors.sh |  30 ++++++++++++------------
 usr.bin/indent/args.c            |  47 +++++++++++++++++++++------------------
 usr.bin/indent/indent.c          |   6 ++--
 3 files changed, 43 insertions(+), 40 deletions(-)

diffs (230 lines):

diff -r 68afca4f7af7 -r 41bf675ff110 tests/usr.bin/indent/t_errors.sh
--- a/tests/usr.bin/indent/t_errors.sh  Thu Oct 28 21:02:04 2021 +0000
+++ b/tests/usr.bin/indent/t_errors.sh  Thu Oct 28 21:32:48 2021 +0000
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $NetBSD: t_errors.sh,v 1.6 2021/10/28 21:02:05 rillig Exp $
+# $NetBSD: t_errors.sh,v 1.7 2021/10/28 21:32:49 rillig Exp $
 #
 # Copyright (c) 2021 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -61,11 +61,11 @@
            -bacchus
 }
 
-atf_test_case 'option_int_missing_parameter'
-option_int_missing_parameter_body()
+atf_test_case 'option_int_missing_argument'
+option_int_missing_argument_body()
 {
        expect_error \
-           'indent: Command line: option "-ts" requires an integer parameter' \
+           'indent: Command line: argument "x" to option "-ts" must be an integer' \
            -tsx
 }
 
@@ -89,7 +89,7 @@
 option_tabsize_negative_body()
 {
        expect_error \
-           'indent: Command line: option "-ts" requires an integer parameter' \
+           'indent: Command line: argument "-1" to option "-ts" must be between 1 and 80' \
            -ts-1
 }
 
@@ -97,7 +97,7 @@
 option_tabsize_zero_body()
 {
        expect_error \
-           'indent: Command line: invalid argument "0" for option "-ts"' \
+           'indent: Command line: argument "0" to option "-ts" must be between 1 and 80' \
            -ts0
 }
 
@@ -106,7 +106,7 @@
 {
        # Integer overflow, on both ILP32 and LP64 platforms.
        expect_error \
-           'indent: Command line: invalid argument "81" for option "-ts"' \
+           'indent: Command line: argument "81" to option "-ts" must be between 1 and 80' \
            -ts81
 }
 
@@ -115,7 +115,7 @@
 {
        # Integer overflow, on both ILP32 and LP64 platforms.
        expect_error \
-           'indent: Command line: invalid argument "3000000000" for option "-ts"' \
+           'indent: Command line: argument "3000000000" to option "-ts" must be between 1 and 80' \
            -ts3000000000
 }
 
@@ -123,7 +123,7 @@
 option_indent_size_zero_body()
 {
        expect_error \
-           'indent: Command line: invalid argument "0" for option "-i"' \
+           'indent: Command line: argument "0" to option "-i" must be between 1 and 80' \
            -i0
 }
 
@@ -131,7 +131,7 @@
 option_int_trailing_garbage_body()
 {
        expect_error \
-           'indent: Command line: invalid argument "3garbage" for option "-i"' \
+           'indent: Command line: argument "3garbage" to option "-i" must be an integer' \
            -i3garbage
 }
 
@@ -163,15 +163,15 @@
 {
        # TODO: Write '-cli' instead of only 'cli'.
        expect_error \
-           'indent: Command line: ``cli'\'\'' requires a parameter' \
+           'indent: Command line: ``cli'\'\'' requires an argument' \
            -cli
 
        expect_error \
-           'indent: Command line: ``T'\'\'' requires a parameter' \
+           'indent: Command line: ``T'\'\'' requires an argument' \
            -T
 
        expect_error \
-           'indent: Command line: ``U'\'\'' requires a parameter' \
+           'indent: Command line: ``U'\'\'' requires an argument' \
            -U
 }
 
@@ -252,7 +252,7 @@
        echo '/* comment */' > arg1.c
 
        expect_error \
-           'indent: unknown parameter: arg3.c' \
+           'indent: too many arguments: arg3.c' \
            arg1.c arg2.c arg3.c arg4.c
 }
 
@@ -352,7 +352,7 @@
 {
        atf_add_test_case 'option_unknown'
        atf_add_test_case 'option_bool_trailing_garbage'
-       atf_add_test_case 'option_int_missing_parameter'
+       atf_add_test_case 'option_int_missing_argument'
        atf_add_test_case 'option_profile_not_found'
        atf_add_test_case 'option_buffer_overflow'
        atf_add_test_case 'option_typedefs_not_found'
diff -r 68afca4f7af7 -r 41bf675ff110 usr.bin/indent/args.c
--- a/usr.bin/indent/args.c     Thu Oct 28 21:02:04 2021 +0000
+++ b/usr.bin/indent/args.c     Thu Oct 28 21:32:48 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.63 2021/10/28 21:02:04 rillig Exp $ */
+/*     $NetBSD: args.c,v 1.64 2021/10/28 21:32:48 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: args.c,v 1.63 2021/10/28 21:02:04 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.64 2021/10/28 21:32:48 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/args.c 336318 2018-07-15 21:04:21Z pstef $");
 #endif
@@ -200,7 +200,7 @@
     return false;
 
 need_param:
-    errx(1, "%s: ``%.*s'' requires a parameter",
+    errx(1, "%s: ``%.*s'' requires an argument",
        option_source, (int)(arg_end - arg), arg);
     /* NOTREACHED */
 }
@@ -221,37 +221,40 @@
 set_option(const char *arg, const char *option_source)
 {
     const struct pro *p;
-    const char *param_start;
+    const char *arg_arg;
 
     arg++;                     /* skip leading '-' */
     if (set_special_option(arg, option_source))
        return;
 
-    for (p = pro + array_length(pro); p-- != pro;) {
-       param_start = skip_over(arg, p->p_may_negate, p->p_name);
-       if (param_start != NULL)
+    for (p = pro + array_length(pro); p-- != pro;)
+       if ((arg_arg = skip_over(arg, p->p_may_negate, p->p_name)) != NULL)
            goto found;
-    }
     errx(1, "%s: unknown option \"-%s\"", option_source, arg);
+found:
 
-found:
     if (p->p_is_bool) {
-       if (param_start[0] != '\0')
+       if (arg_arg[0] != '\0')
            errx(1, "%s: unknown option \"-%s\"", option_source, arg);
+
        *(bool *)p->p_var = p->p_may_negate ? arg[0] != 'n' : p->p_bool_value;
-    } else {
-       if (!isdigit((unsigned char)*param_start))
-           errx(1, "%s: option \"-%s\" requires an integer parameter",
-               option_source, p->p_name);
-       errno = 0;
-       char *end;
-       long num = strtol(param_start, &end, 10);
-       if (!(errno == 0 && *end == '\0' &&
-               p->i_min <= num && num <= p->i_max))
-           errx(1, "%s: invalid argument \"%s\" for option \"-%s\"",
-               option_source, param_start, p->p_name);
-       *(int *)p->p_var = (int)num;
+       return;
     }
+
+    errno = 0;
+    char *end;
+    long num = strtol(arg_arg, &end, 10);
+    if (!(errno == 0 && *end == '\0'))
+       errx(1, "%s: argument \"%s\" to option \"-%s\" must be an integer",
+           option_source, arg_arg, p->p_name);
+
+    if (!(isdigit((unsigned char)*arg_arg) &&
+           p->i_min <= num && num <= p->i_max))
+       errx(1,
+           "%s: argument \"%s\" to option \"-%s\" must be between %d and %d",
+           option_source, arg_arg, p->p_name, p->i_min, p->i_max);
+
+    *(int *)p->p_var = (int)num;
 }
 
 static void
diff -r 68afca4f7af7 -r 41bf675ff110 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Thu Oct 28 21:02:04 2021 +0000
+++ b/usr.bin/indent/indent.c   Thu Oct 28 21:32:48 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.162 2021/10/26 21:37:27 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.163 2021/10/28 21:32:49 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.162 2021/10/26 21:37:27 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.163 2021/10/28 21:32:49 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -547,7 +547,7 @@
                err(1, "%s", out_name);
 
        } else
-           errx(1, "unknown parameter: %s", argv[i]);
+           errx(1, "too many arguments: %s", argv[i]);
     }
 
     if (input == NULL)



Home | Main Index | Thread Index | Old Index