aboutsummaryrefslogtreecommitdiff
path: root/tests/module/test_stdin.c
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-08-29 18:17:40 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-08-29 18:17:40 -0700
commit77c4a7eb74cc51419331009ca83671395f263c6c (patch)
tree3afe00e915fb253e975cc359365d8340f0d350f8 /tests/module/test_stdin.c
parent8f998042dc594b59129a77556d1c98160220b585 (diff)
parent0d23384af497877ad156af27e6f4aa731acea461 (diff)
Merge pull request #1555 from inolen/tty_fixes
added stubs for tcgetattr and tcsetattr, minor node tty fixes
Diffstat (limited to 'tests/module/test_stdin.c')
-rw-r--r--tests/module/test_stdin.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/module/test_stdin.c b/tests/module/test_stdin.c
new file mode 100644
index 00000000..4838d466
--- /dev/null
+++ b/tests/module/test_stdin.c
@@ -0,0 +1,57 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#if EMSCRIPTEN
+#include <emscripten.h>
+#endif
+
+int line = 0;
+
+void main_loop(void *arg)
+{
+ char str[10] = {0};
+ int ret;
+
+ errno = 0;
+ while (errno != EAGAIN) {
+ if (line == 0) {
+ ret = fgetc(stdin);
+ if (ret != EOF) putc(ret, stdout);
+ if (ret == '\n') line++;
+ } else if (line > 0) {
+ ret = scanf("%10s", str);
+ if (ret > 0) puts(str);
+ }
+
+ if (ferror(stdin) && errno != EAGAIN) {
+ puts("error");
+ exit(EXIT_FAILURE);
+ }
+
+ if (feof(stdin)) {
+ puts("eof");
+ exit(EXIT_SUCCESS);
+ }
+
+ clearerr(stdin);
+ }
+}
+
+int main(int argc, char const *argv[])
+{
+ fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
+
+ // SM shell doesn't implement an event loop and therefor doesn't support
+ // emscripten_set_main_loop. However, its stdin reads are sync so it
+ // should exit out after calling main_loop once.
+ main_loop(NULL);
+
+#if EMSCRIPTEN
+ emscripten_set_main_loop(main_loop, 60, 0);
+#else
+ while (1) main_loop(NULL); sleep(1);
+#endif
+ return 0;
+} \ No newline at end of file