forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesslines.c
More file actions
59 lines (50 loc) · 1.36 KB
/
Copy pathprocesslines.c
File metadata and controls
59 lines (50 loc) · 1.36 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
/* processlines.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "duktape.h"
int main(int argc, const char *argv[]) {
duk_context *ctx = NULL;
char line[4096];
char idx;
int ch;
ctx = duk_create_heap_default();
if (!ctx) {
printf("Failed to create a Duktape heap.\n");
exit(1);
}
if (duk_peval_file(ctx, "process.js") != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
goto finished;
}
duk_pop(ctx); /* ignore result */
memset(line, 0, sizeof(line));
idx = 0;
for (;;) {
if (idx >= sizeof(line)) {
printf("Line too long\n");
exit(1);
}
ch = fgetc(stdin);
if (ch == 0x0a) {
line[idx++] = '\0';
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1 /*index*/, "processLine");
duk_push_string(ctx, line);
if (duk_pcall(ctx, 1 /*nargs*/) != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
} else {
printf("%s\n", duk_safe_to_string(ctx, -1));
}
duk_pop(ctx); /* pop result/error */
idx = 0;
} else if (ch == EOF) {
break;
} else {
line[idx++] = (char) ch;
}
}
finished:
duk_destroy_heap(ctx);
exit(0);
}