-
Notifications
You must be signed in to change notification settings - Fork 23
/
common.c
413 lines (344 loc) · 10.3 KB
/
common.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Distributed under Mozilla Public Licence 2.0 */
/* https://www.mozilla.org/en-US/MPL/2.0/ */
/* 2016-08-16 (C) Jonas S Karlsson, [email protected] */
/* common code for all platforms */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#include <ctype.h>
#ifndef UNIX
#include "FreeRTOS.h"
#include "task.h"
#include "espressif/esp_common.h"
#include "espressif/sdk_private.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "ssid_config.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#endif
#ifdef UNIX
#include <sys/socket.h>
#endif
#include "compat.h"
char* readline_int(char* prompt, int maxlen, int (*myreadchar)(char*)) {
if (prompt) {
printf("%s", prompt);
fflush(stdout);
}
char buffer[maxlen+1];
int i = 0;
char ch;
while (myreadchar(&ch) == 1) {
if (ch == '\b' || ch == 0x7f) {
if (i > 0) {
i--;
printf("\b \b"); fflush(stdout);
}
continue;
}
//printf("[%d]", ch); fflush(stdout);
if (ch == 3) { // ctrl-c, restart
i = 0;
ch = 12; // force a redraw of prompt
}
if (ch == 12) { // ctrl-l, redraw
buffer[i] = 0;
printf("\n%s%s", prompt, &buffer[0]);
fflush(stdout);
continue;
}
if (ch == 4) { // ctrl-d eof only at beginning of line
if (i)
continue;
else
return NULL;
}
int eol = (ch == '\n' || ch == '\r');
if (!eol) {
putchar(ch); fflush(stdout);
buffer[i++] = ch;
}
if (i == maxlen) printf("\nWarning, result truncated at maxlen=%d!\n", maxlen);
if (i == maxlen || eol) {
buffer[i] = 0;
printf("\n");
return strdup(buffer);
}
}
return NULL;
}
// default blocking getchar
int readline_getchar(char * chp) {
return read(0, (void*) chp, 1); // 0 is stdin
}
char* readline(char* prompt, int maxlen) {
return readline_int(prompt, maxlen, &readline_getchar);
}
// simplistic XML parser:
// xml_out -> xml_char -> xml_tag/xml_tag_name/xml_attr_value
//
void f_xml_emit_text(void* userdata, char* path[], char c) {
//printf("%c", c);
}
void f_xml_emit_tag(void* userdata, char* path[], char* tag) {
printf("TAG=%s\n", tag);
}
void f_xml_emit_attr(void* userdata, char* path[], char* tag, char* attr, char* value) {
printf("TAG=%s ATTR=%s VALUE=%s\n", tag, attr, value);
}
int http_get(char* url, char* server) {
wget_data data;
memset(&data, 0, sizeof(data));
data.xml_emit_text = f_xml_emit_text;
data.xml_emit_tag = f_xml_emit_tag;
data.xml_emit_attr = f_xml_emit_attr;
wget(&data, url, server);
return 1;
}
void xml_tag(wget_data* data, char c) {
data->tag[data->tag_pos++] = c;
if (data->tag_pos > TAG_SIZE) data->tag_pos--; // overwrite
data->tag[data->tag_pos] = 0;
}
void xml_tag_name(wget_data* data, char c) {
// print path
if (0) {
int i = 0;
for(i = 0; i < data->path_pos; i++) printf("%s -> ", data->path[i]);
printf("\n");
}
if (c == '/' || data->tag[data->tag_pos-1] == '/') {
if (data->xml_emit_tag)
data->xml_emit_tag(data->userdata, data->path, data->tag);
data->tag[data->tag_pos--] = 0; // remove '/'
} else if (data->tag[0] == '/') { // </TAG>
if (data->xml_emit_tag)
data->xml_emit_tag(data->userdata, data->path, data->tag);
//printf("STRCMP(%s, %s)\n", xml_path[xml_pos-1], ¤t_tag[1]);
if (strcmp(data->path[data->path_pos - 1], &(data->tag[1])) == 0) {
data->path[data->path_pos] = NULL;
data->path_pos--;
} else {
// mismatch
// TODO:????
}
} else { // <TAG>
if (data->xml_emit_tag)
data->xml_emit_tag(data->userdata, data->path, data->tag);
data->path[data->path_pos++] = strdup(data->tag);
if (data->path_pos > TAG_SIZE-1) data->path_pos--;
}
data->tag[0] = 0;
data->tag_pos = 0;
}
void xml_attr(wget_data* data, char c) {
data->attr[data->attr_pos++] = c;
if (data->attr_pos > TAG_SIZE) data->attr_pos--;
data->attr[data->attr_pos] = 0;
}
void xml_attr_value(wget_data* data, char c) {
data->value[data->value_pos++] = c;
if (data->value_pos > VALUE_SIZE) data->value_pos--;
data->value[data->value_pos] = 0;
}
// states
#define TAG_NORMAL 0
#define TAG_START 1
#define TAG_ATTR 2
void xml_char(wget_data* data, int c) {
// reset at beginning and end
if (c < 0) {
data->state = TAG_NORMAL;
return;
}
switch (data->state) {
case TAG_NORMAL:
if (c == '<') data->state = TAG_START;
else if (data->xml_emit_text)
data->xml_emit_text(data->userdata, data->path, c);
break;
case TAG_START:
if (c == '>') data->state = TAG_NORMAL;
else if (c == ' ' || c == '\n' || c == '\r') data->state = TAG_ATTR;
else xml_tag(data, c);
if (data->state == TAG_NORMAL) xml_tag_name(data, c);
break;
case TAG_ATTR:
if (c == '>') data->state = TAG_NORMAL;
else if (c == ' ' || c == '\n' || c == '\r') data->state = TAG_ATTR;
else if (c == '/') { xml_tag(data, c); xml_tag_name(data, c); data->state = TAG_NORMAL; }
else if (c == '"' || c == '\'') data->state = c;
else if (c == '=') ; // ignore // TODO: fix
else xml_attr(data, c);
if (data->state == TAG_NORMAL) xml_tag_name(data, c);
break;
case '"': case '\'':
if (c == data->state) data->state = TAG_ATTR;
// TODO: \' \" ???
else xml_attr_value(data, c);
if (data->state == TAG_ATTR) {
if (data->xml_emit_attr)
data->xml_emit_attr(data->userdata, data->path, data->tag, data->attr, data->value);
data->attr_pos = 0;
data->attr[0] = 0;
data->value_pos = 0;
data->value[0] = 0;
}
break;
}
}
int xml_out(wget_data* data, char* buff, int bytes) {
int i = 0;
for (i = 0; i < bytes; i++) xml_char(data, buff[i]);
return 1; // continue
}
int wget(wget_data* data, char* url, char* server) {
int successes = 0, failures = 0;
//printf("HTTP get task starting...\r\n");
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
//printf("Running DNS lookup for %s...\r\n", url);
int err = getaddrinfo(server, "80", &hints, &res);
if (err != 0 || res == NULL) {
printf("DNS lookup failed err=%d res=%p server=%s\r\n", err, res, server);
if (res)
freeaddrinfo(res);
failures++;
return 1;
}
/* Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
//struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
//printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(*addr));
int s = socket(res->ai_family, res->ai_socktype, 0);
if (s < 0) {
printf("... Failed to allocate socket.\r\n");
freeaddrinfo(res);
failures++;
return 2;
}
if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
close(s);
freeaddrinfo(res);
printf("... socket connect failed.\r\n");
failures++;
return 3;
}
//printf("... connected\r\n");
freeaddrinfo(res);
// TODO: not efficient?
#define WRITE(msg) (write((s), (msg), strlen(msg)) < 0)
if (WRITE("GET ") ||
WRITE(url) ||
WRITE("\r\n") ||
WRITE("User-Agent: esp-open-rtos/0.1 esp8266\r\n\r\n"))
#undef WRITE
{
printf("... socket send failed\r\n");
close(s);
failures++;
return 4;
}
//printf("... socket send success\r\n");
char buff[MAX_BUFF] = {0};
int r = 0;
xml_char(data, -1);
do {
r = read(s, buff, sizeof(buff));
if (r > 0 && !xml_out(data, buff, r)) break;
} while (r > 0);
// mark end
xml_char(data, -2);
//printf("... done reading from socket. Last read return=%d errno=%d\r\n", r, errno);
if (r != 0)
failures++;
else
successes++;
close(s);
return 0;
}
int iscomment(char* s) {
while (s && *s && isspace(*s++));
return (s && *s == ';');
}
// line is increased in increments of this
#define MAXLINE 256
int process_file(void* envp, char* filename, process_input process, int verbosity) {
FILE *file = fopen(filename, "r");
if (!file) {
perror(filename);
return -1;
}
char* input = calloc(MAXLINE, 1);
int sz = 0;
int len = 0;
char line[MAXLINE] = {0};
int lineno = 0;
int startno = 0;
int end = 0;
int ret = 0;
do {
// printf("%s.%d: %s", filename, lineno, line);
// chunk finished (non-empty line with some input)
if (end || !isspace(line[0])) {
if (startno > 0)
ret = process(envp, input, filename, startno, lineno - 1, verbosity);
//printf("\n--------------RET=%d\n", ret);
if (ret == -1) break;
input[0] = 0;
len = 0;
startno = lineno;
}
if (end) break;
// append line
// TODO: move comment handling to reader!!!
if (line[0] && !iscomment(line)) {
if (sz < len + strlen(line) + 1) {
sz += MAXLINE;
input = realloc(input, sz);
}
strcat(input+len, line); // safe!
len += strlen(line);
}
end = !fgets(line, sizeof(line) - 1, file);
lineno++;
} while(1);
free(input);
fclose(file);
return ret;
}
#define IMACS
#undef CTRL // shadows something from ttyefault.h (included termios.h)
#define NO_MAIN
#ifndef UNIX
#define OTA
#endif
#define BUFF_SIZE 1024
#ifdef IMACS
#include "imacs/imacs.c"
#endif
char* editor(char* s, char* title) {
#ifdef IMACS
imacs_buffer b;
imacs_init(&b, s, 0);
b.filename = title ? title : "esp-lisp-data";
maineditor(&b);
clear();
s = strdup(b.buff);
// TODO: a imacs_clean()?
free(b.buff);
b.buff = NULL;
#endif
return s;
}