Consolidated 'stdio' into one file.

parent b0ad5378
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "duktape/duktape.h"
#include "bindings_stdio.h"
void push_bindings_stdio(duk_context *ctx) {
duk_push_c_function(ctx, c_print, DUK_VARARGS);
duk_put_prop_string(ctx, -2, "print");
duk_push_c_function(ctx, c_getchar, 0);
duk_put_prop_string(ctx, -2, "getchar");
duk_push_c_function(ctx, c_putchar, 1);
duk_put_prop_string(ctx, -2, "putchar");
duk_push_c_function(ctx, c_getbuffer, 1);
duk_put_prop_string(ctx, -2, "getbuffer");
duk_push_c_function(ctx, c_putbuffer, 1);
duk_put_prop_string(ctx, -2, "putbuffer");
}
duk_ret_t c_print(duk_context *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_safe_to_string(ctx, -1));
return 0;
}
duk_ret_t c_getchar (duk_context *ctx) {
int c;
c = fgetc(stdin);
if (c != EOF) {
duk_push_int(ctx, c);
return 1;
}
if (feof(stdin)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdin)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdin);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_getchar.");
}
duk_ret_t c_putchar (duk_context *ctx) {
int c = duk_require_int(ctx, 0);
if (fputc(c, stdout) != EOF) {
duk_push_int(ctx, c);
return 1;
}
if (feof(stdout)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdout)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdout);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_putchar.");
}
duk_ret_t c_getbuffer (duk_context *ctx) {
char * buffer;
duk_size_t length;
buffer = duk_require_buffer_data(ctx, 0, &length);
size_t bytes_read = fread(buffer, sizeof(char), length, stdin);
if (bytes_read == 0) {
if (feof(stdin)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdin)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdin);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_getbuffer.");
}
duk_push_int(ctx, bytes_read);
return 1;
}
duk_ret_t c_putbuffer (duk_context *ctx) {
char * buffer;
duk_size_t length;
buffer = duk_require_buffer_data(ctx, 0, &length);
size_t written = fwrite(buffer, sizeof(char), length, stdout);
if (written != length) {
if (feof(stdout)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdout)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdout);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_putbuffer.");
}
duk_push_int(ctx, written);
return 1;
}
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "duktape/duktape.h"
#include "c_getbuffer.h"
int c_getbuffer (duk_context *ctx) {
char * buffer;
duk_size_t length;
buffer = duk_require_buffer_data(ctx, 0, &length);
size_t bytes_read = fread(buffer, sizeof(char), length, stdin);
if (bytes_read == 0) {
if (feof(stdin)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdin)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdin);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_getbuffer.");
}
duk_push_int(ctx, bytes_read);
return 1;
}
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "duktape/duktape.h"
#include "c_getchar.h"
int c_getchar (duk_context *ctx) {
int c;
c = fgetc(stdin);
if (c != EOF) {
duk_push_int(ctx, c);
return 1;
}
if (feof(stdin)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdin)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdin);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_getchar.");
}
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "duktape/duktape.h"
#include "c_putbuffer.h"
int c_putbuffer (duk_context *ctx) {
char * buffer;
duk_size_t length;
buffer = duk_require_buffer_data(ctx, 0, &length);
size_t written = fwrite(buffer, sizeof(char), length, stdout);
if (written != length) {
if (feof(stdout)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdout)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdout);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_putbuffer.");
}
duk_push_int(ctx, written);
return 1;
}
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "duktape/duktape.h"
#include "c_putchar.h"
int c_putchar (duk_context *ctx) {
int c = duk_require_int(ctx, 0);
if (fputc(c, stdout) != EOF) {
duk_push_int(ctx, c);
return 1;
}
if (feof(stdout)) {
duk_push_error_object(ctx, errno, "EOF");
return 1;
}
if (ferror(stdout)) {
if (errno == EWOULDBLOCK) {
duk_push_undefined(ctx);
return 1;
}
duk_push_error_object(ctx, errno, strerror(errno));
clearerr(stdout);
return duk_throw(ctx);
}
return duk_error(ctx, 999, "Unhandled situation in c_putchar.");
}
......@@ -87,16 +87,7 @@ int main(int argc, char *argv[] /* char *environ[] */) {
duk_put_prop_string(ctx, -2, "main");
// Provide access to stdin and stdout
duk_push_object(ctx);
duk_push_c_function(ctx, native_print, DUK_VARARGS);
duk_put_prop_string(ctx, -2, "print");
duk_push_c_function(ctx, c_getchar, 0);
duk_put_prop_string(ctx, -2, "getchar");
duk_push_c_function(ctx, c_putchar, 1);
duk_put_prop_string(ctx, -2, "putchar");
duk_push_c_function(ctx, c_getbuffer, 1);
duk_put_prop_string(ctx, -2, "getbuffer");
duk_push_c_function(ctx, c_putbuffer, 1);
duk_put_prop_string(ctx, -2, "putbuffer");
push_bindings_stdio(ctx);
duk_put_prop_string(ctx, -2, "stdio");
// Provide access to native functions
duk_push_object(ctx);
......
#include "duktape/duktape.h"
#include "native_print.h"
duk_ret_t native_print(duk_context *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_safe_to_string(ctx, -1));
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment