main.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <stdio.h>
  2. #include <uv.h>
  3. uv_loop_t *loop;
  4. uv_fs_t stdin_watcher;
  5. uv_idle_t idler;
  6. char buffer[1024];
  7. void crunch_away(uv_idle_t* handle) {
  8. // Compute extra-terrestrial life
  9. // fold proteins
  10. // computer another digit of PI
  11. // or similar
  12. fprintf(stderr, "Computing PI...\n");
  13. // just to avoid overwhelming your terminal emulator
  14. uv_idle_stop(handle);
  15. }
  16. void on_type(uv_fs_t *req) {
  17. if (stdin_watcher.result > 0) {
  18. buffer[stdin_watcher.result] = '\0';
  19. printf("Typed %s\n", buffer);
  20. uv_buf_t buf = uv_buf_init(buffer, 1024);
  21. uv_fs_read(loop, &stdin_watcher, 0, &buf, 1, -1, on_type);
  22. uv_idle_start(&idler, crunch_away);
  23. }
  24. else if (stdin_watcher.result < 0) {
  25. fprintf(stderr, "error opening file: %s\n", uv_strerror(req->result));
  26. }
  27. }
  28. int main() {
  29. loop = uv_default_loop();
  30. uv_idle_init(loop, &idler);
  31. uv_buf_t buf = uv_buf_init(buffer, 1024);
  32. uv_fs_read(loop, &stdin_watcher, 0, &buf, 1, -1, on_type);
  33. uv_idle_start(&idler, crunch_away);
  34. return uv_run(loop, UV_RUN_DEFAULT);
  35. }