emit error message when test is given invalid int

This augments the previous change for issue #3346 by adding an error
message when an invalid integer is seen. This change is likely to be
controversial so I'm not going to squash it into the previous change.
This commit is contained in:
Kurtis Rader 2016-11-27 19:05:37 -08:00
parent 2f33c24a07
commit 54a76bb9e5
3 changed files with 162 additions and 157 deletions

View file

@ -1,6 +1,6 @@
function __fish_print_help --description "Print help message for the specified fish function or builtin" --argument item function __fish_print_help --description "Print help message for the specified fish function or builtin" --argument item
# special support for builtin_help_get() # special support for builtin_help_get()
set -l tty_width set -l tty_width 0
if test "$item" = "--tty-width" if test "$item" = "--tty-width"
set tty_width $argv[2] set tty_width $argv[2]
set item $argv[3] set item $argv[3]

View file

@ -4,6 +4,7 @@
#include "config.h" // IWYU pragma: keep #include "config.h" // IWYU pragma: keep
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -637,9 +638,13 @@ bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
// IEEE 1003.1 says nothing about what it means for two strings to be "algebraically equal". For // IEEE 1003.1 says nothing about what it means for two strings to be "algebraically equal". For
// example, should we interpret 0x10 as 0, 10, or 16? Here we use only base 10 and use wcstoll, // example, should we interpret 0x10 as 0, 10, or 16? Here we use only base 10 and use wcstoll,
// which allows for leading + and -, and leading whitespace. This matches bash. // which allows for leading + and -, and whitespace. This is consistent, albeit a bit more lenient
static bool parse_number(const wcstring &arg, long long *out) { // since we allow trailing whitespace, with other implementations such as bash.
static bool parse_number(const wcstring &arg, long long *out, wcstring_list_t &errors) {
*out = fish_wcstoll(arg.c_str()); *out = fish_wcstoll(arg.c_str());
if (errno) {
errors.push_back(format_string(_(L"invalid integer '%ls'"), arg.c_str()));
}
return !errno; return !errno;
} }
@ -655,28 +660,28 @@ static bool binary_primary_evaluate(test_expressions::token_t token, const wcstr
return left != right; return left != right;
} }
case test_number_equal: { case test_number_equal: {
return parse_number(left, &left_num) && parse_number(right, &right_num) && return parse_number(left, &left_num, errors) &&
left_num == right_num; parse_number(right, &right_num, errors) && left_num == right_num;
} }
case test_number_not_equal: { case test_number_not_equal: {
return parse_number(left, &left_num) && parse_number(right, &right_num) && return parse_number(left, &left_num, errors) &&
left_num != right_num; parse_number(right, &right_num, errors) && left_num != right_num;
} }
case test_number_greater: { case test_number_greater: {
return parse_number(left, &left_num) && parse_number(right, &right_num) && return parse_number(left, &left_num, errors) &&
left_num > right_num; parse_number(right, &right_num, errors) && left_num > right_num;
} }
case test_number_greater_equal: { case test_number_greater_equal: {
return parse_number(left, &left_num) && parse_number(right, &right_num) && return parse_number(left, &left_num, errors) &&
left_num >= right_num; parse_number(right, &right_num, errors) && left_num >= right_num;
} }
case test_number_lesser: { case test_number_lesser: {
return parse_number(left, &left_num) && parse_number(right, &right_num) && return parse_number(left, &left_num, errors) &&
left_num < right_num; parse_number(right, &right_num, errors) && left_num < right_num;
} }
case test_number_lesser_equal: { case test_number_lesser_equal: {
return parse_number(left, &left_num) && parse_number(right, &right_num) && return parse_number(left, &left_num, errors) &&
left_num <= right_num; parse_number(right, &right_num, errors) && left_num <= right_num;
} }
default: { default: {
errors.push_back(format_string(L"Unknown token type in %s", __func__)); errors.push_back(format_string(L"Unknown token type in %s", __func__));
@ -729,7 +734,7 @@ static bool unary_primary_evaluate(test_expressions::token_t token, const wcstri
return !wstat(arg, &buf) && buf.st_size > 0; return !wstat(arg, &buf) && buf.st_size > 0;
} }
case test_filedesc_t: { // "-t", whether the fd is associated with a terminal case test_filedesc_t: { // "-t", whether the fd is associated with a terminal
return parse_number(arg, &num) && num == (int)num && isatty((int)num); return parse_number(arg, &num, errors) && num == (int)num && isatty((int)num);
} }
case test_fileperm_r: { // "-r", read permission case test_fileperm_r: { // "-r", read permission
return !waccess(arg, R_OK); return !waccess(arg, R_OK);