improve converting strings to ints/longs

The existing code is inconsistent, and in a couple of cases wrong, about
dealing with strings that are not valid ints. For example, there are
locations that call wcstol() and check errno without first setting errno
to zero. Normalize the code to a consistent pattern.  This is mostly to
deal with inconsistencies between BSD, GNU, and other UNIXes.

This does make some syntax more liberal. For example `echo $PATH[1 .. 3]`
is now valid due to uniformly allowing leading and trailing whitespace
around numbers. Whereas prior to this change you would get a "Invalid
index value" error. Contrast this with `echo $PATH[ 1.. 3 ]` which was
valid and still is.
This commit is contained in:
Kurtis Rader
2016-11-22 20:24:03 -08:00
parent 5ec9fcd8d4
commit a928517e95
18 changed files with 266 additions and 215 deletions

View File

@@ -14,7 +14,6 @@
#include <time.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include <algorithm>
#include <map>
#include <set>
@@ -412,10 +411,10 @@ void env_init(const struct config_paths_t *paths /* or NULL */) {
const env_var_t shlvl_str = env_get_string(L"SHLVL");
wcstring nshlvl_str = L"1";
if (!shlvl_str.missing()) {
wchar_t *end;
long shlvl_i = wcstol(shlvl_str.c_str(), &end, 10);
while (iswspace(*end)) ++end; // skip trailing whitespace
if (shlvl_i >= 0 && *end == '\0') {
const wchar_t *end;
// TODO: Figure out how to handle invalid numbers better. Shouldn't we issue a diagnostic?
long shlvl_i = fish_wcstol(shlvl_str.c_str(), &end);
if (!errno && shlvl_i >= 0) {
nshlvl_str = to_string<long>(shlvl_i + 1);
}
}
@@ -514,14 +513,10 @@ int env_set(const wcstring &key, const wchar_t *val, env_mode_flags_t var_mode)
}
if (key == L"umask") {
wchar_t *end;
// Set the new umask.
if (val && wcslen(val)) {
errno = 0;
long mask = wcstol(val, &end, 8);
if (!errno && (!*end) && (mask <= 0777) && (mask >= 0)) {
long mask = fish_wcstol(val, NULL, 8);
if (!errno && mask <= 0777 && mask >= 0) {
umask(mask);
// Do not actually create a umask variable, on env_get, it will be calculated
// dynamically.