]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* processlines.c */ |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | #include <string.h> | |
5 | #include "duktape.h" | |
6 | ||
7 | int main(int argc, const char *argv[]) { | |
8 | duk_context *ctx = NULL; | |
9 | char line[4096]; | |
10 | char idx; | |
11 | int ch; | |
12 | ||
13 | ctx = duk_create_heap_default(); | |
14 | if (!ctx) { | |
15 | printf("Failed to create a Duktape heap.\n"); | |
16 | exit(1); | |
17 | } | |
18 | ||
19 | if (duk_peval_file(ctx, "process.js") != 0) { | |
20 | printf("Error: %s\n", duk_safe_to_string(ctx, -1)); | |
21 | goto finished; | |
22 | } | |
23 | duk_pop(ctx); /* ignore result */ | |
24 | ||
25 | memset(line, 0, sizeof(line)); | |
26 | idx = 0; | |
27 | for (;;) { | |
28 | if (idx >= sizeof(line)) { | |
29 | printf("Line too long\n"); | |
30 | exit(1); | |
31 | } | |
32 | ||
33 | ch = fgetc(stdin); | |
34 | if (ch == 0x0a) { | |
35 | line[idx++] = '\0'; | |
36 | ||
37 | duk_push_global_object(ctx); | |
38 | duk_get_prop_string(ctx, -1 /*index*/, "processLine"); | |
39 | duk_push_string(ctx, line); | |
40 | if (duk_pcall(ctx, 1 /*nargs*/) != 0) { | |
41 | printf("Error: %s\n", duk_safe_to_string(ctx, -1)); | |
42 | } else { | |
43 | printf("%s\n", duk_safe_to_string(ctx, -1)); | |
44 | } | |
45 | duk_pop(ctx); /* pop result/error */ | |
46 | ||
47 | idx = 0; | |
48 | } else if (ch == EOF) { | |
49 | break; | |
50 | } else { | |
51 | line[idx++] = (char) ch; | |
52 | } | |
53 | } | |
54 | ||
55 | finished: | |
56 | duk_destroy_heap(ctx); | |
57 | ||
58 | exit(0); | |
59 | } |