]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.8.0/examples/eventloop/main.c
2 * Main for evloop command line tool.
4 * Runs a given script from file or stdin inside an eventloop. The
5 * script can then access setTimeout() etc.
17 extern void poll_register(duk_context
*ctx
);
18 extern void ncurses_register(duk_context
*ctx
);
19 extern void socket_register(duk_context
*ctx
);
20 extern void fileio_register(duk_context
*ctx
);
21 extern void eventloop_register(duk_context
*ctx
);
22 extern int eventloop_run(duk_context
*ctx
); /* Duktape/C function, safe called */
24 static int c_evloop
= 0;
27 static void my_sighandler(int x
) {
28 fprintf(stderr
, "Got signal %d\n", x
);
31 static void set_sigint_handler(void) {
32 (void) signal(SIGINT
, my_sighandler
);
34 #endif /* NO_SIGNAL */
36 /* Print error to stderr and pop error. */
37 static void print_error(duk_context
*ctx
, FILE *f
) {
38 if (duk_is_object(ctx
, -1) && duk_has_prop_string(ctx
, -1, "stack")) {
39 /* XXX: print error objects specially */
40 /* XXX: pcall the string coercion */
41 duk_get_prop_string(ctx
, -1, "stack");
42 if (duk_is_string(ctx
, -1)) {
43 fprintf(f
, "%s\n", duk_get_string(ctx
, -1));
51 duk_to_string(ctx
, -1);
52 fprintf(f
, "%s\n", duk_get_string(ctx
, -1));
57 int wrapped_compile_execute(duk_context
*ctx
) {
61 /* Compile input and place it into global _USERCODE */
62 duk_compile(ctx
, comp_flags
);
63 duk_push_global_object(ctx
);
64 duk_insert(ctx
, -2); /* [ ... global func ] */
65 duk_put_prop_string(ctx
, -2, "_USERCODE");
68 printf("compiled usercode\n");
71 /* Start a zero timer which will call _USERCODE from within
74 fprintf(stderr
, "set _USERCODE timer\n");
76 duk_eval_string(ctx
, "setTimeout(function() { _USERCODE(); }, 0);");
79 /* Finally, launch eventloop. This call only returns after the
80 * eventloop terminates.
83 fprintf(stderr
, "calling eventloop_run()\n");
85 rc
= duk_safe_call(ctx
, eventloop_run
, 0 /*nargs*/, 1 /*nrets*/);
87 fprintf(stderr
, "eventloop_run() failed: %s\n", duk_to_string(ctx
, -1));
92 fprintf(stderr
, "calling EventLoop.run()\n");
94 duk_eval_string(ctx
, "EventLoop.run();");
101 int handle_fh(duk_context
*ctx
, FILE *f
, const char *filename
) {
108 if (fseek(f
, 0, SEEK_END
) < 0) {
111 len
= (int) ftell(f
);
112 if (fseek(f
, 0, SEEK_SET
) < 0) {
115 buf
= (char *) malloc(len
);
120 got
= fread((void *) buf
, (size_t) 1, (size_t) len
, f
);
122 duk_push_lstring(ctx
, buf
, got
);
123 duk_push_string(ctx
, filename
);
128 rc
= duk_safe_call(ctx
, wrapped_compile_execute
, 2 /*nargs*/, 1 /*nret*/);
129 if (rc
!= DUK_EXEC_SUCCESS
) {
130 print_error(ctx
, stderr
);
145 int handle_file(duk_context
*ctx
, const char *filename
) {
149 f
= fopen(filename
, "rb");
151 fprintf(stderr
, "failed to open source file: %s\n", filename
);
156 retval
= handle_fh(ctx
, f
, filename
);
165 int handle_stdin(duk_context
*ctx
) {
168 retval
= handle_fh(ctx
, stdin
, "stdin");
173 int main(int argc
, char *argv
[]) {
174 duk_context
*ctx
= NULL
;
176 const char *filename
= NULL
;
180 set_sigint_handler();
182 /* This is useful at the global level; libraries should avoid SIGPIPE though */
183 /*signal(SIGPIPE, SIG_IGN);*/
186 for (i
= 1; i
< argc
; i
++) {
191 if (strcmp(arg
, "-c") == 0) {
193 } else if (strlen(arg
) > 1 && arg
[0] == '-') {
206 ctx
= duk_create_heap_default();
209 ncurses_register(ctx
);
210 socket_register(ctx
);
211 fileio_register(ctx
);
214 fprintf(stderr
, "Using C based eventloop (omit -c to use Ecmascript based eventloop)\n");
217 eventloop_register(ctx
);
218 duk_eval_file(ctx
, "c_eventloop.js");
220 fprintf(stderr
, "Using Ecmascript based eventloop (give -c to use C based eventloop)\n");
223 duk_eval_file(ctx
, "ecma_eventloop.js");
226 fprintf(stderr
, "Executing code from: '%s'\n", filename
);
229 if (strcmp(filename
, "-") == 0) {
230 if (handle_stdin(ctx
) != 0) {
235 if (handle_file(ctx
, filename
) != 0) {
243 duk_destroy_heap(ctx
);
249 fprintf(stderr
, "Usage: evloop [-c] <filename>\n");
250 fprintf(stderr
, "\n");
251 fprintf(stderr
, "Uses an Ecmascript based eventloop (ecma_eventloop.js) by default.\n");
252 fprintf(stderr
, "If -c option given, uses a C based eventloop (c_eventloop.{c,js}).\n");
253 fprintf(stderr
, "If <filename> is '-', the entire STDIN executed.\n");