forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.c
More file actions
250 lines (235 loc) · 8.61 KB
/
__init__.c
File metadata and controls
250 lines (235 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include "shared-bindings/dotenv/__init__.h"
#include "extmod/vfs.h"
#include "extmod/vfs_fat.h"
#include "py/mpstate.h"
#include "py/objstr.h"
#include "supervisor/filesystem.h"
STATIC uint8_t consume_spaces(FIL *active_file) {
uint8_t character = ' ';
UINT quantity_read = 1;
while (unichar_isspace(character) && quantity_read > 0) {
f_read(active_file, &character, 1, &quantity_read);
}
return character;
}
// Starting at the start of a new line, determines if the key matches the given
// key. File pointer is left after the = after the key.
STATIC bool key_matches(FIL *active_file, const char *key) {
uint8_t character = ' ';
UINT quantity_read = 1;
character = consume_spaces(active_file);
bool quoted = false;
if (character == '\'') {
quoted = true;
f_read(active_file, &character, 1, &quantity_read);
}
size_t key_pos = 0;
bool escaped = false;
bool matches = true;
size_t key_len = strlen(key);
while (quantity_read > 0) {
if (character == '\\' && !escaped && quoted) {
escaped = true;
} else if (!escaped && quoted && character == '\'') {
quoted = false;
// Move past the quoted before breaking so we can check the validity of data past it.
f_read(active_file, &character, 1, &quantity_read);
break;
} else if (!quoted && (unichar_isspace(character) || character == '=' || character == '\n' || character == '#')) {
break;
} else {
matches = matches && key[key_pos] == character;
escaped = false;
key_pos++;
}
f_read(active_file, &character, 1, &quantity_read);
}
if (unichar_isspace(character)) {
character = consume_spaces(active_file);
}
if (character == '=' || character == '\n' || character == '#') {
// Rewind one so the value can find it.
f_lseek(active_file, f_tell(active_file) - 1);
} else {
// We're followed by something else that is invalid syntax.
matches = false;
}
return matches && key_pos == key_len;
}
STATIC bool next_line(FIL *active_file) {
uint8_t character = ' ';
UINT quantity_read = 1;
bool quoted = false;
bool escaped = false;
// Track comments because they last until the end of the line.
bool comment = false;
FRESULT result = FR_OK;
// Consume all characters while quoted or others up to \n.
while (result == FR_OK && quantity_read > 0 && (quoted || character != '\n')) {
if (character == '#' || comment) {
// Comments consume any escaping.
comment = true;
} else if (!escaped) {
if (character == '\'') {
quoted = !quoted;
} else if (character == '\\') {
escaped = true;
}
} else {
escaped = false;
}
result = f_read(active_file, &character, 1, &quantity_read);
}
return result == FR_OK && quantity_read > 0;
}
STATIC mp_int_t read_value(FIL *active_file, char *value, size_t value_len) {
uint8_t character = ' ';
UINT quantity_read = 1;
// Consume spaces before =
character = consume_spaces(active_file);
if (character != '=') {
if (character == '#' || character == '\n') {
// Keys without an = after them are valid with the value None.
return 0;
}
// All other characters are invalid.
return -1;
}
character = ' ';
// Consume space after =
while (unichar_isspace(character) && quantity_read > 0) {
f_read(active_file, &character, 1, &quantity_read);
}
bool quoted = false;
if (character == '\'') {
quoted = true;
f_read(active_file, &character, 1, &quantity_read);
}
if (character == '"') {
// We don't support double quoted values.
return -1;
}
// Copy the value over.
size_t value_pos = 0;
bool escaped = false;
// Count trailing spaces so we can ignore them at the end of unquoted
// values.
size_t trailing_spaces = 0;
while (quantity_read > 0) {
// Consume the first \ if the value is quoted.
if (quoted && character == '\\' && !escaped) {
escaped = true;
// Drop this slash by short circuiting the rest of the loop.
f_read(active_file, &character, 1, &quantity_read);
continue;
}
if (quoted && !escaped && character == '\'') {
// trailing ' means the value is done.
break;
}
// Unquoted values are ended by a newline or comment.
if (!quoted && (character == '\n' || character == '#')) {
if (character == '\n') {
// Rewind one so the next_line can find the \n.
f_lseek(active_file, f_tell(active_file) - 1);
}
break;
}
if (!quoted && unichar_isspace(character)) {
trailing_spaces += 1;
} else {
trailing_spaces = 0;
}
escaped = false;
// Only copy the value over if we have space. Otherwise, we'll just
// count the overall length.
if (value_pos < value_len) {
value[value_pos] = character;
}
value_pos++;
f_read(active_file, &character, 1, &quantity_read);
}
return value_pos - trailing_spaces;
}
mp_int_t dotenv_get_key(const char *path, const char *key, char *value, mp_int_t value_len) {
FIL active_file;
FATFS *fs = filesystem_circuitpy();
FRESULT result = f_open(fs, &active_file, path, FA_READ);
if (result != FR_OK) {
return -1;
}
mp_int_t actual_value_len = -1;
bool read_ok = true;
while (read_ok) {
if (key_matches(&active_file, key)) {
actual_value_len = read_value(&active_file, value, value_len);
}
read_ok = next_line(&active_file);
}
f_close(&active_file);
return actual_value_len;
}
mp_obj_t common_hal_dotenv_get_key(const char *path, const char *key) {
// Use the stack for short values. Longer values will require a heap allocation after we know
// the length.
char value[64];
mp_int_t actual_len = dotenv_get_key(path, key, value, sizeof(value));
if (actual_len <= 0) {
return mp_const_none;
}
if ((size_t)actual_len >= sizeof(value)) {
mp_obj_str_t *str = MP_OBJ_TO_PTR(mp_obj_new_str_copy(&mp_type_str, NULL, actual_len + 1));
dotenv_get_key(path, key, (char *)str->data, actual_len + 1);
str->hash = qstr_compute_hash(str->data, actual_len);
return MP_OBJ_FROM_PTR(str);
}
return mp_obj_new_str(value, actual_len);
}
bool dotenv_get_key_terminated(const char *path, const char *key, char *value, mp_int_t value_len) {
mp_int_t actual_len = dotenv_get_key(path, key, value, value_len - 1);
if (actual_len >= value_len) {
return false;
}
value[actual_len] = '\0'; // terminate string
return true;
}
bool dotenv_get_key_int(const char *path, const char *key, mp_int_t *value) {
char buf[16];
if (!dotenv_get_key_terminated(path, key, buf, (mp_int_t)sizeof(buf))) {
return false;
}
char *end;
long result = strtol(buf, &end, 0);
if (end == buf || *end) { // If the whole buffer was not consumed it's an error
return false;
}
*value = (mp_int_t)result;
return true;
}