Really finish renaming to getenv

This commit is contained in:
Jeff Epler 2022-12-09 14:28:46 -06:00
parent 1f504e5c0f
commit cc7d550407
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
3 changed files with 48 additions and 48 deletions

View File

@ -586,7 +586,6 @@ SRC_SHARED_MODULE_ALL = \
displayio/TileGrid.c \ displayio/TileGrid.c \
displayio/area.c \ displayio/area.c \
displayio/__init__.c \ displayio/__init__.c \
_environ/__init__.c \
floppyio/__init__.c \ floppyio/__init__.c \
fontio/BuiltinFont.c \ fontio/BuiltinFont.c \
fontio/__init__.c \ fontio/__init__.c \

View File

@ -28,18 +28,19 @@
typedef enum { typedef enum {
GETENV_OK = 0, GETENV_OK = 0,
ENVIRON_ERR_OPEN, GETENV_ERR_OPEN,
ENVIRON_ERR_UNICODE, GETENV_ERR_UNICODE,
ENVIRON_ERR_LENGTH, GETENV_ERR_LENGTH,
ENVIRON_ERR_NOT_FOUND, GETENV_ERR_NOT_FOUND,
ENVIRON_ERR_UNEXPECTED = 0xff00, // logical or'd with the byte value GETENV_ERR_UNEXPECTED = 0xff00, // logical or'd with the byte value
} os_getenv_err_t; } os_getenv_err_t;
// Allocation free version that returns the full length of the value. // Allocation free version that returns the full length of the value.
// If it fits, the return value is 0-terminated. If the value doesn't fit, // If it fits, the return value is 0-terminated. The passed in buffer
// *value_len may be an over-estimate but never an under-estimate. // may be modified even if an error is returned. Allocation free.
os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len); os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len);
// Returns ENVIRON_ERR_OK and sets value to the read value. Returns // Returns GETENV_OK and sets value to the read value. Returns
// ENVIRON_ERR_... if the value was not numeric. allocation-free. // GETENV_ERR_... if the value was not numeric. allocation-free.
os_getenv_err_t common_hal_os_environ_get_key_int(const char *key, mp_int_t *value); // If any error code is returned, value is guaranteed not modified
os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value);

View File

@ -187,13 +187,13 @@ STATIC os_getenv_err_t read_unicode_escape(file_arg *active_file, int sz, vstr_t
char *end; char *end;
unsigned long c = strtoul(hex_buf, &end, 16); unsigned long c = strtoul(hex_buf, &end, 16);
if (end != &hex_buf[sz]) { if (end != &hex_buf[sz]) {
return ENVIRON_ERR_UNEXPECTED | *end; return GETENV_ERR_UNEXPECTED | *end;
} }
if (c >= 0x110000) { if (c >= 0x110000) {
return ENVIRON_ERR_UNICODE; return GETENV_ERR_UNICODE;
} }
vstr_add_char_nonstd(buf, c); vstr_add_char_nonstd(buf, c);
return ENVIRON_OK; return GETENV_OK;
} }
// Read a quoted string // Read a quoted string
@ -203,7 +203,7 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
switch (character) { switch (character) {
case 0: case 0:
case '\n': case '\n':
return ENVIRON_ERR_UNEXPECTED | character; return GETENV_ERR_UNEXPECTED | character;
case '"': case '"':
character = consume_whitespace(active_file); character = consume_whitespace(active_file);
@ -212,9 +212,9 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
next_line(active_file); next_line(active_file);
MP_FALLTHROUGH; MP_FALLTHROUGH;
case '\n': case '\n':
return ENVIRON_OK; return GETENV_OK;
default: default:
return ENVIRON_ERR_UNEXPECTED | character; return GETENV_ERR_UNEXPECTED | character;
} }
case '\\': case '\\':
@ -222,7 +222,7 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
switch (character) { switch (character) {
case 0: case 0:
case '\n': case '\n':
return ENVIRON_ERR_UNEXPECTED | character; return GETENV_ERR_UNEXPECTED | character;
case 'b': case 'b':
character = '\b'; character = '\b';
break; break;
@ -246,7 +246,7 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
int sz = (character == 'u') ? 4 : 8; int sz = (character == 'u') ? 4 : 8;
os_getenv_err_t res; os_getenv_err_t res;
res = read_unicode_escape(active_file, sz, buf); res = read_unicode_escape(active_file, sz, buf);
if (res != ENVIRON_OK) { if (res != GETENV_OK) {
return res; return res;
} }
continue; continue;
@ -267,12 +267,12 @@ STATIC os_getenv_err_t read_bare_value(file_arg *active_file, vstr_t *buf, int f
while (true) { while (true) {
switch (character) { switch (character) {
case 0: case 0:
return ENVIRON_ERR_UNEXPECTED | character; return GETENV_ERR_UNEXPECTED | character;
case '\n': case '\n':
return ENVIRON_OK; return GETENV_OK;
case '#': case '#':
next_line(active_file); next_line(active_file);
return ENVIRON_OK; return GETENV_OK;
default: default:
vstr_add_byte_nonstd(buf, character); vstr_add_byte_nonstd(buf, character);
} }
@ -292,13 +292,13 @@ STATIC mp_int_t read_value(file_arg *active_file, vstr_t *buf, bool *quoted) {
} }
} }
STATIC os_getenv_err_t os_environ_get_key_vstr(const char *path, const char *key, vstr_t *buf, bool *quoted) { STATIC os_getenv_err_t os_getenv_vstr(const char *path, const char *key, vstr_t *buf, bool *quoted) {
file_arg active_file; file_arg active_file;
if (!open_file(path, &active_file)) { if (!open_file(path, &active_file)) {
return ENVIRON_ERR_OPEN; return GETENV_ERR_OPEN;
} }
os_getenv_err_t result = ENVIRON_ERR_NOT_FOUND; os_getenv_err_t result = GETENV_ERR_NOT_FOUND;
while (!is_eof(&active_file)) { while (!is_eof(&active_file)) {
if (key_matches(&active_file, key)) { if (key_matches(&active_file, key)) {
result = read_value(&active_file, buf, quoted); result = read_value(&active_file, buf, quoted);
@ -308,16 +308,16 @@ STATIC os_getenv_err_t os_environ_get_key_vstr(const char *path, const char *key
return result; return result;
} }
STATIC os_getenv_err_t os_environ_get_key_buf_terminated(const char *key, char *value, size_t value_len, bool *quoted) { STATIC os_getenv_err_t os_getenv_buf_terminated(const char *key, char *value, size_t value_len, bool *quoted) {
vstr_t buf; vstr_t buf;
vstr_init_fixed_buf(&buf, value_len, value); vstr_init_fixed_buf(&buf, value_len, value);
os_getenv_err_t result = os_environ_get_key_vstr(ENVIRON_PATH, key, &buf, quoted); os_getenv_err_t result = os_getenv_vstr(GETENV_PATH, key, &buf, quoted);
if (result == ENVIRON_OK) { if (result == GETENV_OK) {
vstr_add_byte_nonstd(&buf, 0); vstr_add_byte_nonstd(&buf, 0);
memcpy(value, buf.buf, MIN(buf.len, value_len)); memcpy(value, buf.buf, MIN(buf.len, value_len));
if (buf.len > value_len) { if (buf.len > value_len) {
result = ENVIRON_ERR_LENGTH; result = GETENV_ERR_LENGTH;
} }
} }
return result; return result;
@ -325,18 +325,18 @@ STATIC os_getenv_err_t os_environ_get_key_buf_terminated(const char *key, char *
os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len) { os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len) {
bool quoted; bool quoted;
os_getenv_err_t result = os_environ_get_key_buf_terminated(key, value, value_len, &quoted); os_getenv_err_t result = os_getenv_buf_terminated(key, value, value_len, &quoted);
if (result == ENVIRON_OK && !quoted) { if (result == GETENV_OK && !quoted) {
result = ENVIRON_ERR_UNEXPECTED | value[0]; result = GETENV_ERR_UNEXPECTED | value[0];
} }
return result; return result;
} }
STATIC void throw__environ_error(os_getenv_err_t error) { STATIC void throw_getenv_error(os_getenv_err_t error) {
if (error == ENVIRON_OK) { if (error == GETENV_OK) {
return; return;
} }
if (error & ENVIRON_ERR_UNEXPECTED) { if (error & GETENV_ERR_UNEXPECTED) {
byte character = (error & 0xff); byte character = (error & 0xff);
mp_print_t print; mp_print_t print;
vstr_t vstr; vstr_t vstr;
@ -350,11 +350,11 @@ STATIC void throw__environ_error(os_getenv_err_t error) {
vstr.len, vstr.buf); vstr.len, vstr.buf);
} }
switch (error) { switch (error) {
case ENVIRON_ERR_OPEN: case GETENV_ERR_OPEN:
mp_raise_ValueError(translate("File not found")); mp_raise_ValueError(translate("File not found"));
case ENVIRON_ERR_UNICODE: case GETENV_ERR_UNICODE:
mp_raise_ValueError(translate("Invalid unicode escape")); mp_raise_ValueError(translate("Invalid unicode escape"));
case ENVIRON_ERR_NOT_FOUND: case GETENV_ERR_NOT_FOUND:
mp_raise_ValueError(translate("Key not found")); mp_raise_ValueError(translate("Key not found"));
default: default:
mp_raise_RuntimeError(translate("Internal error")); mp_raise_RuntimeError(translate("Internal error"));
@ -366,11 +366,11 @@ mp_obj_t common_hal_os_getenv_path(const char *path, const char *key, mp_obj_t d
bool quoted; bool quoted;
vstr_init(&buf, 64); vstr_init(&buf, 64);
os_getenv_err_t result = os_environ_get_key_vstr(path, key, &buf, &quoted); os_getenv_err_t result = os_getenv_vstr(path, key, &buf, &quoted);
if (result == ENVIRON_ERR_NOT_FOUND) { if (result == GETENV_ERR_NOT_FOUND) {
return default_; return default_;
} }
throw__environ_error(result); throw_getenv_error(result);
if (quoted) { if (quoted) {
return mp_obj_new_str_from_vstr(&mp_type_str, &buf); return mp_obj_new_str_from_vstr(&mp_type_str, &buf);
@ -380,24 +380,24 @@ mp_obj_t common_hal_os_getenv_path(const char *path, const char *key, mp_obj_t d
} }
mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_) { mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_) {
return common_hal_os_getenv_path(ENVIRON_PATH, key, default_); return common_hal_os_getenv_path(GETENV_PATH, key, default_);
} }
os_getenv_err_t common_hal_os_environ_get_key_int(const char *key, mp_int_t *value) { os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value) {
char buf[16]; char buf[16];
bool quoted; bool quoted;
os_getenv_err_t result = os_environ_get_key_buf_terminated(key, buf, sizeof(buf), &quoted); os_getenv_err_t result = os_getenv_buf_terminated(key, buf, sizeof(buf), &quoted);
if (result != ENVIRON_OK) { if (result != GETENV_OK) {
return result; return result;
} }
if (quoted) { if (quoted) {
return ENVIRON_ERR_UNEXPECTED | '"'; return GETENV_ERR_UNEXPECTED | '"';
} }
char *end; char *end;
long num = strtol(buf, &end, 0); long num = strtol(buf, &end, 0);
if (end == buf || *end) { // If the whole buffer was not consumed it's an error if (end == buf || *end) { // If the whole buffer was not consumed it's an error
return ENVIRON_ERR_UNEXPECTED | *end; return GETENV_ERR_UNEXPECTED | *end;
} }
*value = (mp_int_t)num; *value = (mp_int_t)num;
return ENVIRON_OK; return GETENV_OK;
} }