Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duktape-1.5.2/examples/guide/processlines.c
Go to the documentation of this file.
1/* processlines.c */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include "duktape.h"
6
7int main(int argc, const char *argv[]) {
8 duk_context *ctx = NULL;
9 char line[4096];
10 char idx;
11 int ch;
12
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
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:
57
58 exit(0);
59}
DUK_EXTERNAL const char * duk_push_string(duk_context *ctx, const char *str)
DUK_EXTERNAL duk_bool_t duk_get_prop_string(duk_context *ctx, duk_idx_t obj_index, const char *key)
DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx)
DUK_EXTERNAL void duk_push_global_object(duk_context *ctx)
DUK_EXTERNAL void duk_pop(duk_context *ctx)
DUK_EXTERNAL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs)
#define duk_safe_to_string(ctx, index)
#define duk_create_heap_default()
#define duk_peval_file(ctx, path)
#define NULL
Definition gmacros.h:924
int main(void)
Definition sanitycheckc.c:1
#define printf