Skip to content

Commit cc7d550

Browse files
committed
Really finish renaming to getenv
1 parent 1f504e5 commit cc7d550

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

py/circuitpy_defns.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,6 @@ SRC_SHARED_MODULE_ALL = \
586586
displayio/TileGrid.c \
587587
displayio/area.c \
588588
displayio/__init__.c \
589-
_environ/__init__.c \
590589
floppyio/__init__.c \
591590
fontio/BuiltinFont.c \
592591
fontio/__init__.c \

shared-module/os/__init__.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@
2828

2929
typedef enum {
3030
GETENV_OK = 0,
31-
ENVIRON_ERR_OPEN,
32-
ENVIRON_ERR_UNICODE,
33-
ENVIRON_ERR_LENGTH,
34-
ENVIRON_ERR_NOT_FOUND,
35-
ENVIRON_ERR_UNEXPECTED = 0xff00, // logical or'd with the byte value
31+
GETENV_ERR_OPEN,
32+
GETENV_ERR_UNICODE,
33+
GETENV_ERR_LENGTH,
34+
GETENV_ERR_NOT_FOUND,
35+
GETENV_ERR_UNEXPECTED = 0xff00, // logical or'd with the byte value
3636
} os_getenv_err_t;
3737

3838
// Allocation free version that returns the full length of the value.
39-
// If it fits, the return value is 0-terminated. If the value doesn't fit,
40-
// *value_len may be an over-estimate but never an under-estimate.
39+
// If it fits, the return value is 0-terminated. The passed in buffer
40+
// may be modified even if an error is returned. Allocation free.
4141
os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len);
4242

43-
// Returns ENVIRON_ERR_OK and sets value to the read value. Returns
44-
// ENVIRON_ERR_... if the value was not numeric. allocation-free.
45-
os_getenv_err_t common_hal_os_environ_get_key_int(const char *key, mp_int_t *value);
43+
// Returns GETENV_OK and sets value to the read value. Returns
44+
// GETENV_ERR_... if the value was not numeric. allocation-free.
45+
// If any error code is returned, value is guaranteed not modified
46+
os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value);

shared-module/os/getenv.c

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ STATIC os_getenv_err_t read_unicode_escape(file_arg *active_file, int sz, vstr_t
187187
char *end;
188188
unsigned long c = strtoul(hex_buf, &end, 16);
189189
if (end != &hex_buf[sz]) {
190-
return ENVIRON_ERR_UNEXPECTED | *end;
190+
return GETENV_ERR_UNEXPECTED | *end;
191191
}
192192
if (c >= 0x110000) {
193-
return ENVIRON_ERR_UNICODE;
193+
return GETENV_ERR_UNICODE;
194194
}
195195
vstr_add_char_nonstd(buf, c);
196-
return ENVIRON_OK;
196+
return GETENV_OK;
197197
}
198198

199199
// Read a quoted string
@@ -203,7 +203,7 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
203203
switch (character) {
204204
case 0:
205205
case '\n':
206-
return ENVIRON_ERR_UNEXPECTED | character;
206+
return GETENV_ERR_UNEXPECTED | character;
207207

208208
case '"':
209209
character = consume_whitespace(active_file);
@@ -212,17 +212,17 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
212212
next_line(active_file);
213213
MP_FALLTHROUGH;
214214
case '\n':
215-
return ENVIRON_OK;
215+
return GETENV_OK;
216216
default:
217-
return ENVIRON_ERR_UNEXPECTED | character;
217+
return GETENV_ERR_UNEXPECTED | character;
218218
}
219219

220220
case '\\':
221221
character = get_next_byte(active_file);
222222
switch (character) {
223223
case 0:
224224
case '\n':
225-
return ENVIRON_ERR_UNEXPECTED | character;
225+
return GETENV_ERR_UNEXPECTED | character;
226226
case 'b':
227227
character = '\b';
228228
break;
@@ -246,7 +246,7 @@ STATIC os_getenv_err_t read_string_value(file_arg *active_file, vstr_t *buf) {
246246
int sz = (character == 'u') ? 4 : 8;
247247
os_getenv_err_t res;
248248
res = read_unicode_escape(active_file, sz, buf);
249-
if (res != ENVIRON_OK) {
249+
if (res != GETENV_OK) {
250250
return res;
251251
}
252252
continue;
@@ -267,12 +267,12 @@ STATIC os_getenv_err_t read_bare_value(file_arg *active_file, vstr_t *buf, int f
267267
while (true) {
268268
switch (character) {
269269
case 0:
270-
return ENVIRON_ERR_UNEXPECTED | character;
270+
return GETENV_ERR_UNEXPECTED | character;
271271
case '\n':
272-
return ENVIRON_OK;
272+
return GETENV_OK;
273273
case '#':
274274
next_line(active_file);
275-
return ENVIRON_OK;
275+
return GETENV_OK;
276276
default:
277277
vstr_add_byte_nonstd(buf, character);
278278
}
@@ -292,13 +292,13 @@ STATIC mp_int_t read_value(file_arg *active_file, vstr_t *buf, bool *quoted) {
292292
}
293293
}
294294

295-
STATIC os_getenv_err_t os_environ_get_key_vstr(const char *path, const char *key, vstr_t *buf, bool *quoted) {
295+
STATIC os_getenv_err_t os_getenv_vstr(const char *path, const char *key, vstr_t *buf, bool *quoted) {
296296
file_arg active_file;
297297
if (!open_file(path, &active_file)) {
298-
return ENVIRON_ERR_OPEN;
298+
return GETENV_ERR_OPEN;
299299
}
300300

301-
os_getenv_err_t result = ENVIRON_ERR_NOT_FOUND;
301+
os_getenv_err_t result = GETENV_ERR_NOT_FOUND;
302302
while (!is_eof(&active_file)) {
303303
if (key_matches(&active_file, key)) {
304304
result = read_value(&active_file, buf, quoted);
@@ -308,35 +308,35 @@ STATIC os_getenv_err_t os_environ_get_key_vstr(const char *path, const char *key
308308
return result;
309309
}
310310

311-
STATIC os_getenv_err_t os_environ_get_key_buf_terminated(const char *key, char *value, size_t value_len, bool *quoted) {
311+
STATIC os_getenv_err_t os_getenv_buf_terminated(const char *key, char *value, size_t value_len, bool *quoted) {
312312
vstr_t buf;
313313
vstr_init_fixed_buf(&buf, value_len, value);
314-
os_getenv_err_t result = os_environ_get_key_vstr(ENVIRON_PATH, key, &buf, quoted);
314+
os_getenv_err_t result = os_getenv_vstr(GETENV_PATH, key, &buf, quoted);
315315

316-
if (result == ENVIRON_OK) {
316+
if (result == GETENV_OK) {
317317
vstr_add_byte_nonstd(&buf, 0);
318318
memcpy(value, buf.buf, MIN(buf.len, value_len));
319319
if (buf.len > value_len) {
320-
result = ENVIRON_ERR_LENGTH;
320+
result = GETENV_ERR_LENGTH;
321321
}
322322
}
323323
return result;
324324
}
325325

326326
os_getenv_err_t common_hal_os_getenv_str(const char *key, char *value, size_t value_len) {
327327
bool quoted;
328-
os_getenv_err_t result = os_environ_get_key_buf_terminated(key, value, value_len, &quoted);
329-
if (result == ENVIRON_OK && !quoted) {
330-
result = ENVIRON_ERR_UNEXPECTED | value[0];
328+
os_getenv_err_t result = os_getenv_buf_terminated(key, value, value_len, &quoted);
329+
if (result == GETENV_OK && !quoted) {
330+
result = GETENV_ERR_UNEXPECTED | value[0];
331331
}
332332
return result;
333333
}
334334

335-
STATIC void throw__environ_error(os_getenv_err_t error) {
336-
if (error == ENVIRON_OK) {
335+
STATIC void throw_getenv_error(os_getenv_err_t error) {
336+
if (error == GETENV_OK) {
337337
return;
338338
}
339-
if (error & ENVIRON_ERR_UNEXPECTED) {
339+
if (error & GETENV_ERR_UNEXPECTED) {
340340
byte character = (error & 0xff);
341341
mp_print_t print;
342342
vstr_t vstr;
@@ -350,11 +350,11 @@ STATIC void throw__environ_error(os_getenv_err_t error) {
350350
vstr.len, vstr.buf);
351351
}
352352
switch (error) {
353-
case ENVIRON_ERR_OPEN:
353+
case GETENV_ERR_OPEN:
354354
mp_raise_ValueError(translate("File not found"));
355-
case ENVIRON_ERR_UNICODE:
355+
case GETENV_ERR_UNICODE:
356356
mp_raise_ValueError(translate("Invalid unicode escape"));
357-
case ENVIRON_ERR_NOT_FOUND:
357+
case GETENV_ERR_NOT_FOUND:
358358
mp_raise_ValueError(translate("Key not found"));
359359
default:
360360
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
366366
bool quoted;
367367

368368
vstr_init(&buf, 64);
369-
os_getenv_err_t result = os_environ_get_key_vstr(path, key, &buf, &quoted);
370-
if (result == ENVIRON_ERR_NOT_FOUND) {
369+
os_getenv_err_t result = os_getenv_vstr(path, key, &buf, &quoted);
370+
if (result == GETENV_ERR_NOT_FOUND) {
371371
return default_;
372372
}
373-
throw__environ_error(result);
373+
throw_getenv_error(result);
374374

375375
if (quoted) {
376376
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
380380
}
381381

382382
mp_obj_t common_hal_os_getenv(const char *key, mp_obj_t default_) {
383-
return common_hal_os_getenv_path(ENVIRON_PATH, key, default_);
383+
return common_hal_os_getenv_path(GETENV_PATH, key, default_);
384384
}
385385

386-
os_getenv_err_t common_hal_os_environ_get_key_int(const char *key, mp_int_t *value) {
386+
os_getenv_err_t common_hal_os_getenv_int(const char *key, mp_int_t *value) {
387387
char buf[16];
388388
bool quoted;
389-
os_getenv_err_t result = os_environ_get_key_buf_terminated(key, buf, sizeof(buf), &quoted);
390-
if (result != ENVIRON_OK) {
389+
os_getenv_err_t result = os_getenv_buf_terminated(key, buf, sizeof(buf), &quoted);
390+
if (result != GETENV_OK) {
391391
return result;
392392
}
393393
if (quoted) {
394-
return ENVIRON_ERR_UNEXPECTED | '"';
394+
return GETENV_ERR_UNEXPECTED | '"';
395395
}
396396
char *end;
397397
long num = strtol(buf, &end, 0);
398398
if (end == buf || *end) { // If the whole buffer was not consumed it's an error
399-
return ENVIRON_ERR_UNEXPECTED | *end;
399+
return GETENV_ERR_UNEXPECTED | *end;
400400
}
401401
*value = (mp_int_t)num;
402-
return ENVIRON_OK;
402+
return GETENV_OK;
403403
}

0 commit comments

Comments
 (0)