Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
processlines.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "duktape.h"

Go to the source code of this file.

Functions

int main (int argc, const char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
const char * argv[] )

Definition at line 7 of file duktape-1.5.2/examples/guide/processlines.c.

7 {
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
#define printf

References duk_create_heap_default, duk_destroy_heap(), duk_get_prop_string(), duk_pcall(), duk_peval_file, duk_pop(), duk_push_global_object(), duk_push_string(), duk_safe_to_string, NULL, and printf.