2017-08-23 10:57:30 +08:00
|
|
|
// Implementation of the math builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2021-11-10 09:07:15 +08:00
|
|
|
#include "math.h"
|
2019-10-14 06:50:48 +08:00
|
|
|
|
2017-08-23 10:57:30 +08:00
|
|
|
#include <algorithm>
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cerrno>
|
2018-03-08 01:01:12 +08:00
|
|
|
#include <cmath>
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cstddef>
|
2019-10-14 06:50:48 +08:00
|
|
|
#include <cstring>
|
2018-07-24 15:00:06 +08:00
|
|
|
#include <limits>
|
2017-08-23 10:57:30 +08:00
|
|
|
#include <string>
|
|
|
|
|
2021-11-10 09:07:15 +08:00
|
|
|
#include "../builtin.h"
|
|
|
|
#include "../common.h"
|
|
|
|
#include "../fallback.h" // IWYU pragma: keep
|
|
|
|
#include "../io.h"
|
|
|
|
#include "../tinyexpr.h"
|
|
|
|
#include "../wgetopt.h"
|
|
|
|
#include "../wutil.h" // IWYU pragma: keep
|
2017-08-23 10:57:30 +08:00
|
|
|
|
2018-07-24 15:00:06 +08:00
|
|
|
// The maximum number of points after the decimal that we'll print.
|
|
|
|
static constexpr int kDefaultScale = 6;
|
|
|
|
|
|
|
|
// The end of the range such that every integer is representable as a double.
|
|
|
|
// i.e. this is the first value such that x + 1 == x (or == x + 2, depending on rounding mode).
|
|
|
|
static constexpr double kMaximumContiguousInteger =
|
|
|
|
double(1LLU << std::numeric_limits<double>::digits);
|
|
|
|
|
2017-08-23 10:57:30 +08:00
|
|
|
struct math_cmd_opts_t {
|
|
|
|
bool print_help = false;
|
2020-10-08 01:03:19 +08:00
|
|
|
bool have_scale = false;
|
2018-07-24 15:00:06 +08:00
|
|
|
int scale = kDefaultScale;
|
2020-10-08 01:03:19 +08:00
|
|
|
int base = 10;
|
2017-08-23 10:57:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// This command is atypical in using the "+" (REQUIRE_ORDER) option for flag parsing.
|
|
|
|
// This is needed because of the minus, `-`, operator in math expressions.
|
2020-10-08 01:03:19 +08:00
|
|
|
static const wchar_t *const short_options = L"+:hs:b:";
|
2019-11-19 10:34:50 +08:00
|
|
|
static const struct woption long_options[] = {{L"scale", required_argument, nullptr, 's'},
|
2020-10-08 01:03:19 +08:00
|
|
|
{L"base", required_argument, nullptr, 'b'},
|
2019-11-19 10:34:50 +08:00
|
|
|
{L"help", no_argument, nullptr, 'h'},
|
2022-04-02 05:25:02 +08:00
|
|
|
{}};
|
2017-08-23 10:57:30 +08:00
|
|
|
|
|
|
|
static int parse_cmd_opts(math_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
2021-02-14 10:41:09 +08:00
|
|
|
int argc, const wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
2017-08-23 10:57:30 +08:00
|
|
|
const wchar_t *cmd = L"math";
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
2019-11-19 10:34:50 +08:00
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
|
2017-08-23 10:57:30 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 's': {
|
2020-10-08 01:03:19 +08:00
|
|
|
opts.have_scale = true;
|
2019-02-13 19:54:19 +08:00
|
|
|
// "max" is the special value that tells us to pick the maximum scale.
|
2019-03-13 05:06:01 +08:00
|
|
|
if (std::wcscmp(w.woptarg, L"max") == 0) {
|
2019-02-13 19:54:19 +08:00
|
|
|
opts.scale = 15;
|
|
|
|
} else {
|
|
|
|
opts.scale = fish_wcstoi(w.woptarg);
|
|
|
|
if (errno || opts.scale < 0 || opts.scale > 15) {
|
2021-11-08 08:41:47 +08:00
|
|
|
streams.err.append_format(_(L"%ls: %ls: invalid scale value\n"), cmd,
|
|
|
|
w.woptarg);
|
2019-02-13 19:54:19 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-08-23 10:57:30 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-10-08 01:03:19 +08:00
|
|
|
case 'b': {
|
|
|
|
if (std::wcscmp(w.woptarg, L"hex") == 0) {
|
|
|
|
opts.base = 16;
|
|
|
|
} else if (std::wcscmp(w.woptarg, L"octal") == 0) {
|
|
|
|
opts.base = 8;
|
|
|
|
} else {
|
|
|
|
opts.base = fish_wcstoi(w.woptarg);
|
|
|
|
if (errno || (opts.base != 8 && opts.base != 16)) {
|
2021-11-02 18:09:38 +08:00
|
|
|
streams.err.append_format(_(L"%ls: %ls: invalid base value\n"), cmd,
|
2020-12-06 22:33:04 +08:00
|
|
|
w.woptarg);
|
2020-10-08 01:03:19 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-08-23 10:57:30 +08:00
|
|
|
case 'h': {
|
|
|
|
opts.print_help = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ':': {
|
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
// For most commands this is an error. We ignore it because a math expression
|
|
|
|
// can begin with a minus sign.
|
|
|
|
*optind = w.woptind - 1;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 01:03:19 +08:00
|
|
|
if (opts.have_scale && opts.scale != 0 && opts.base != 10) {
|
2021-11-08 08:41:47 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO2, cmd,
|
|
|
|
L"non-zero scale value only valid for base 10");
|
2020-10-08 01:03:19 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-08-23 10:57:30 +08:00
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We read from stdin if we are the second or later process in a pipeline.
|
|
|
|
static bool math_args_from_stdin(const io_streams_t &streams) {
|
|
|
|
return streams.stdin_is_directly_redirected;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the arguments from stdin.
|
|
|
|
static const wchar_t *math_get_arg_stdin(wcstring *storage, const io_streams_t &streams) {
|
|
|
|
std::string arg;
|
|
|
|
for (;;) {
|
|
|
|
char ch = '\0';
|
|
|
|
long rc = read_blocked(streams.stdin_fd, &ch, 1);
|
|
|
|
|
2021-02-11 09:19:08 +08:00
|
|
|
if (rc < 0) { // error
|
|
|
|
wperror(L"read");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-23 10:57:30 +08:00
|
|
|
|
|
|
|
if (rc == 0) { // EOF
|
2019-11-19 10:34:50 +08:00
|
|
|
if (arg.empty()) return nullptr;
|
2017-08-23 10:57:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '\n') break; // we're done
|
|
|
|
|
|
|
|
arg += ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
*storage = str2wcstring(arg);
|
|
|
|
return storage->c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the next argument from argv.
|
2021-02-14 10:41:09 +08:00
|
|
|
static const wchar_t *math_get_arg_argv(int *argidx, const wchar_t **argv) {
|
2019-11-19 10:34:50 +08:00
|
|
|
return argv && argv[*argidx] ? argv[(*argidx)++] : nullptr;
|
2017-08-23 10:57:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the arguments from argv or stdin based on the execution context. This mimics how builtin
|
|
|
|
/// `string` does it.
|
2021-02-14 10:41:09 +08:00
|
|
|
static const wchar_t *math_get_arg(int *argidx, const wchar_t **argv, wcstring *storage,
|
2017-08-24 11:38:40 +08:00
|
|
|
const io_streams_t &streams) {
|
2017-08-23 10:57:30 +08:00
|
|
|
if (math_args_from_stdin(streams)) {
|
2021-02-11 09:19:08 +08:00
|
|
|
assert(streams.stdin_fd >= 0 &&
|
|
|
|
"stdin should not be closed since it is directly redirected");
|
2017-08-23 10:57:30 +08:00
|
|
|
return math_get_arg_stdin(storage, streams);
|
|
|
|
}
|
|
|
|
return math_get_arg_argv(argidx, argv);
|
|
|
|
}
|
|
|
|
|
2020-03-14 04:59:10 +08:00
|
|
|
static const wchar_t *math_describe_error(const te_error_t &error) {
|
2021-11-02 18:09:38 +08:00
|
|
|
if (error.position == 0) return L"NO ERROR";
|
2018-02-12 07:02:47 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
switch (error.type) {
|
|
|
|
case TE_ERROR_NONE:
|
|
|
|
DIE("Error has no position");
|
2020-01-31 04:46:22 +08:00
|
|
|
case TE_ERROR_UNKNOWN_FUNCTION:
|
|
|
|
return _(L"Unknown function");
|
2019-05-05 18:09:25 +08:00
|
|
|
case TE_ERROR_MISSING_CLOSING_PAREN:
|
|
|
|
return _(L"Missing closing parenthesis");
|
|
|
|
case TE_ERROR_MISSING_OPENING_PAREN:
|
|
|
|
return _(L"Missing opening parenthesis");
|
|
|
|
case TE_ERROR_TOO_FEW_ARGS:
|
|
|
|
return _(L"Too few arguments");
|
|
|
|
case TE_ERROR_TOO_MANY_ARGS:
|
|
|
|
return _(L"Too many arguments");
|
|
|
|
case TE_ERROR_MISSING_OPERATOR:
|
|
|
|
return _(L"Missing operator");
|
2019-10-30 19:49:13 +08:00
|
|
|
case TE_ERROR_UNEXPECTED_TOKEN:
|
|
|
|
return _(L"Unexpected token");
|
2019-11-01 15:40:56 +08:00
|
|
|
case TE_ERROR_LOGICAL_OPERATOR:
|
|
|
|
return _(L"Logical operations are not supported, use `test` instead");
|
2019-05-05 18:09:25 +08:00
|
|
|
case TE_ERROR_UNKNOWN:
|
|
|
|
return _(L"Expression is bogus");
|
|
|
|
default:
|
|
|
|
return L"Unknown error";
|
2018-02-12 07:02:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:00:06 +08:00
|
|
|
/// Return a formatted version of the value \p v respecting the given \p opts.
|
|
|
|
static wcstring format_double(double v, const math_cmd_opts_t &opts) {
|
2020-10-08 01:03:19 +08:00
|
|
|
if (opts.base == 16) {
|
|
|
|
v = trunc(v);
|
2021-11-08 08:41:47 +08:00
|
|
|
const char *mneg = (v < 0.0 ? "-" : "");
|
|
|
|
return format_string(L"%s0x%llx", mneg, (long long)std::fabs(v));
|
2020-10-08 01:03:19 +08:00
|
|
|
} else if (opts.base == 8) {
|
|
|
|
v = trunc(v);
|
2021-11-08 08:41:47 +08:00
|
|
|
if (v == 0.0) return L"0"; // not 00
|
|
|
|
const char *mneg = (v < 0.0 ? "-" : "");
|
|
|
|
return format_string(L"%s0%llo", mneg, (long long)std::fabs(v));
|
2020-10-08 01:03:19 +08:00
|
|
|
}
|
|
|
|
|
2018-10-21 18:20:18 +08:00
|
|
|
// As a special-case, a scale of 0 means to truncate to an integer
|
|
|
|
// instead of rounding.
|
|
|
|
if (opts.scale == 0) {
|
2019-11-25 04:40:03 +08:00
|
|
|
v = trunc(v);
|
2018-10-21 18:20:18 +08:00
|
|
|
return format_string(L"%.*f", opts.scale, v);
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:00:06 +08:00
|
|
|
wcstring ret = format_string(L"%.*f", opts.scale, v);
|
|
|
|
// If we contain a decimal separator, trim trailing zeros after it, and then the separator
|
|
|
|
// itself if there's nothing after it. Detect a decimal separator as a non-digit.
|
|
|
|
const wchar_t *const digits = L"0123456789";
|
|
|
|
if (ret.find_first_not_of(digits) != wcstring::npos) {
|
|
|
|
while (ret.back() == L'0') {
|
|
|
|
ret.pop_back();
|
|
|
|
}
|
2019-03-13 05:06:01 +08:00
|
|
|
if (!std::wcschr(digits, ret.back())) {
|
2018-07-24 15:00:06 +08:00
|
|
|
ret.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we trimmed everything it must have just been zero.
|
|
|
|
if (ret.empty()) {
|
|
|
|
ret.push_back(L'0');
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-24 11:38:40 +08:00
|
|
|
/// Evaluate math expressions.
|
2020-03-14 04:59:10 +08:00
|
|
|
static int evaluate_expression(const wchar_t *cmd, const parser_t &parser, io_streams_t &streams,
|
|
|
|
const math_cmd_opts_t &opts, wcstring &expression) {
|
2017-08-23 10:57:30 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
|
2018-02-26 18:25:07 +08:00
|
|
|
int retval = STATUS_CMD_OK;
|
2018-02-12 07:02:47 +08:00
|
|
|
te_error_t error;
|
2020-12-15 05:54:53 +08:00
|
|
|
double v = te_interp(expression.c_str(), &error);
|
2018-02-11 22:44:35 +08:00
|
|
|
|
2018-02-12 07:02:47 +08:00
|
|
|
if (error.position == 0) {
|
2018-03-08 01:01:12 +08:00
|
|
|
// Check some runtime errors after the fact.
|
|
|
|
// TODO: Really, this should be done in tinyexpr
|
|
|
|
// (e.g. infinite is the result of "x / 0"),
|
|
|
|
// but that's much more work.
|
2020-12-15 06:02:54 +08:00
|
|
|
const wchar_t *error_message = nullptr;
|
2018-03-08 01:13:26 +08:00
|
|
|
if (std::isinf(v)) {
|
2020-12-15 06:02:54 +08:00
|
|
|
error_message = L"Result is infinite";
|
2018-03-08 01:01:12 +08:00
|
|
|
} else if (std::isnan(v)) {
|
2020-12-15 06:02:54 +08:00
|
|
|
error_message = L"Result is not a number";
|
2021-11-08 08:41:47 +08:00
|
|
|
} else if (std::fabs(v) >= kMaximumContiguousInteger) {
|
2020-12-15 06:02:54 +08:00
|
|
|
error_message = L"Result magnitude is too large";
|
2018-07-24 15:00:06 +08:00
|
|
|
}
|
|
|
|
if (error_message) {
|
2020-12-15 06:02:54 +08:00
|
|
|
streams.err.append_format(L"%ls: Error: %ls\n", cmd, error_message);
|
2018-03-08 01:01:12 +08:00
|
|
|
streams.err.append_format(L"'%ls'\n", expression.c_str());
|
|
|
|
retval = STATUS_CMD_ERROR;
|
2017-12-18 06:41:55 +08:00
|
|
|
} else {
|
2018-07-24 15:00:06 +08:00
|
|
|
streams.out.append(format_double(v, opts));
|
|
|
|
streams.out.push_back(L'\n');
|
2017-12-18 06:41:55 +08:00
|
|
|
}
|
2018-02-11 02:46:34 +08:00
|
|
|
} else {
|
2019-03-15 06:12:14 +08:00
|
|
|
streams.err.append_format(L"%ls: Error: %ls\n", cmd, math_describe_error(error));
|
2018-02-12 07:02:47 +08:00
|
|
|
streams.err.append_format(L"'%ls'\n", expression.c_str());
|
2019-05-05 18:09:25 +08:00
|
|
|
streams.err.append_format(L"%*ls%ls\n", error.position - 1, L" ", L"^");
|
2018-02-26 18:25:07 +08:00
|
|
|
retval = STATUS_CMD_ERROR;
|
2017-12-18 06:41:55 +08:00
|
|
|
}
|
2018-02-26 18:25:07 +08:00
|
|
|
return retval;
|
2017-08-23 10:57:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The math builtin evaluates math expressions.
|
2021-02-14 10:41:09 +08:00
|
|
|
maybe_t<int> builtin_math(parser_t &parser, io_streams_t &streams, const wchar_t **argv) {
|
|
|
|
const wchar_t *cmd = argv[0];
|
2017-08-23 10:57:30 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
math_cmd_opts_t opts;
|
|
|
|
int optind;
|
|
|
|
|
|
|
|
// Is this really the right way to handle no expression present?
|
|
|
|
// if (argc == 0) return STATUS_CMD_OK;
|
|
|
|
|
|
|
|
int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
2019-10-20 17:38:17 +08:00
|
|
|
builtin_print_help(parser, streams, cmd);
|
2017-08-23 10:57:30 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstring expression;
|
|
|
|
wcstring storage;
|
|
|
|
while (const wchar_t *arg = math_get_arg(&optind, argv, &storage, streams)) {
|
|
|
|
if (!expression.empty()) expression.push_back(L' ');
|
|
|
|
expression.append(arg);
|
|
|
|
}
|
|
|
|
|
2018-03-02 05:27:24 +08:00
|
|
|
if (expression.empty()) {
|
2021-11-02 18:09:38 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_MIN_ARG_COUNT1, cmd, 1, 0);
|
2018-03-02 05:27:24 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
2017-08-23 10:57:30 +08:00
|
|
|
return evaluate_expression(cmd, parser, streams, opts, expression);
|
|
|
|
}
|