~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/module/test_stdin.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <errno.h>
 
2
#include <fcntl.h>
 
3
#include <stdlib.h>
 
4
#include <stdio.h>
 
5
#include <unistd.h>
 
6
#if EMSCRIPTEN
 
7
#include <emscripten.h>
 
8
#endif
 
9
 
 
10
int line = 0;
 
11
 
 
12
void main_loop(void *arg)
 
13
{
 
14
  char str[10] = {0};
 
15
  int ret;
 
16
 
 
17
  errno = 0;
 
18
  while (errno != EAGAIN) {
 
19
    if (line == 0) {
 
20
      ret = fgetc(stdin);
 
21
      if (ret != EOF) putc(ret, stdout);
 
22
      if (ret == '\n') line++;
 
23
    } else if (line > 0) {
 
24
      ret = scanf("%10s", str);
 
25
      if (ret > 0) puts(str);
 
26
    }
 
27
 
 
28
    if (ferror(stdin) && errno != EAGAIN) {
 
29
      puts("error");
 
30
      exit(EXIT_FAILURE);
 
31
    }
 
32
 
 
33
    if (feof(stdin)) {
 
34
      puts("eof");
 
35
      exit(EXIT_SUCCESS);
 
36
    }
 
37
 
 
38
    clearerr(stdin);
 
39
  }
 
40
}
 
41
 
 
42
int main(int argc, char const *argv[])
 
43
{
 
44
  fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
 
45
 
 
46
  // SM shell doesn't implement an event loop and therefor doesn't support
 
47
  // emscripten_set_main_loop. However, its stdin reads are sync so it
 
48
  // should exit out after calling main_loop once.
 
49
  main_loop(NULL);
 
50
 
 
51
#if EMSCRIPTEN
 
52
  emscripten_set_main_loop(main_loop, 60, 0);
 
53
#else
 
54
  while (1) main_loop(NULL); sleep(1);
 
55
#endif
 
56
  return 0;
 
57
}
 
 
b'\\ No newline at end of file'