Module Name: src Committed By: rillig Date: Thu Oct 28 21:32:49 UTC 2021
Modified Files: src/tests/usr.bin/indent: t_errors.sh src/usr.bin/indent: args.c indent.c Log Message: indent: make error messages for option parsing more precise To generate a diff of this commit: cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/indent/t_errors.sh cvs rdiff -u -r1.63 -r1.64 src/usr.bin/indent/args.c cvs rdiff -u -r1.162 -r1.163 src/usr.bin/indent/indent.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/indent/t_errors.sh diff -u src/tests/usr.bin/indent/t_errors.sh:1.6 src/tests/usr.bin/indent/t_errors.sh:1.7 --- src/tests/usr.bin/indent/t_errors.sh:1.6 Thu Oct 28 21:02:05 2021 +++ src/tests/usr.bin/indent/t_errors.sh Thu Oct 28 21:32:49 2021 @@ -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 @@ option_bool_trailing_garbage_body() -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 @@ atf_test_case 'option_tabsize_negative' 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 @@ atf_test_case 'option_tabsize_zero' 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 @@ option_tabsize_large_body() { # 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 @@ option_tabsize_very_large_body() { # 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 @@ atf_test_case 'option_indent_size_zero' 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 @@ atf_test_case 'option_int_trailing_garba 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 @@ option_special_missing_param_body() { # 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 @@ argument_too_many_body() 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_init_test_cases() { 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' Index: src/usr.bin/indent/args.c diff -u src/usr.bin/indent/args.c:1.63 src/usr.bin/indent/args.c:1.64 --- src/usr.bin/indent/args.c:1.63 Thu Oct 28 21:02:04 2021 +++ src/usr.bin/indent/args.c Thu Oct 28 21:32:48 2021 @@ -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 @@ static char sccsid[] = "@(#)args.c 8.1 ( #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 @@ set_special_option(const char *arg, cons 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 @@ void 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: + 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 Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.162 src/usr.bin/indent/indent.c:1.163 --- src/usr.bin/indent/indent.c:1.162 Tue Oct 26 21:37:27 2021 +++ src/usr.bin/indent/indent.c Thu Oct 28 21:32:49 2021 @@ -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 @@ static char sccsid[] = "@(#)indent.c 5.1 #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 @@ main_parse_command_line(int argc, char * err(1, "%s", out_name); } else - errx(1, "unknown parameter: %s", argv[i]); + errx(1, "too many arguments: %s", argv[i]); } if (input == NULL)