Subversion Repositories HelenOS-historic

Rev

Rev 1366 | Rev 1452 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <io/io.h>
  2. #include <io/stream.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <libc.h>
  6. #include <ipc/ipc.h>
  7. #include <ipc/ns.h>
  8. #include <ipc/fb.h>
  9. #include <ipc/services.h>
  10. #include <console.h>
  11.  
  12. #define FDS 32
  13.  
  14. typedef struct stream_t {
  15.     pwritefn_t w;
  16.     preadfn_t r;
  17.     void * param;
  18. } stream_t;
  19.  
  20. int console_phone = -1;
  21.  
  22. stream_t streams[FDS] = {{0, 0, 0}};
  23.  
  24. /*
  25. ssize_t write_stdout(void *param, const void * buf, size_t count);
  26. ssize_t write_stdout(void *param, const void * buf, size_t count)
  27. {
  28.     return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
  29. }*/
  30.  
  31. static ssize_t write_stderr(void *param, const void *buf, size_t count)
  32. {
  33.     return count;
  34.     //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
  35. }
  36.  
  37. static char read_stdin(void)
  38. {
  39.     ipcarg_t r0,r1;
  40.     ipc_call_sync_2(console_phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1);
  41.    
  42.     return r0;
  43.     //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
  44. }
  45. static ssize_t write_stdout(void *param, const void *buf, size_t count)
  46. {
  47.     int i;
  48.     ipcarg_t r0,r1;
  49.  
  50.     for (i = 0; i < count; i++)
  51.         ipc_call_sync_2(console_phone, CONSOLE_PUTCHAR, 0, ((const char *)buf)[i], &r0, &r1);
  52.    
  53.     return count;
  54.     //return (ssize_t) __SYSCALL3(SYS_IO, 1, (sysarg_t) buf, (sysarg_t) count);
  55. }
  56.  
  57.  
  58.  
  59. static stream_t open_stdin(void)
  60. {
  61.     stream_t stream;
  62.     int phoneid;
  63.     int res;
  64.    
  65.     if (console_phone < 0) {
  66.         while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
  67.             volatile int a;
  68.             for (a = 0; a < 1048576; a++);
  69.         }
  70.     }
  71.    
  72.     stream.r = read_stdin;
  73.     stream.param = 0;
  74.     return stream;
  75. }
  76.  
  77. static stream_t open_stdout(void)
  78. {
  79.     stream_t stream;
  80.     int res;
  81.    
  82.     if (console_phone < 0) {
  83.         while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
  84.             volatile int a;
  85.             for (a = 0; a < 1048576; a++);
  86.         }
  87.     }
  88.    
  89.     stream.w = write_stdout;
  90.     stream.param = 0;
  91.     return stream;
  92. }
  93.  
  94. fd_t open(const char *fname, int flags)
  95. {
  96.     int c = 0;
  97.    
  98.     while (((streams[c].w) || (streams[c].r)) && (c < FDS))
  99.         c++;
  100.     if (c == FDS)
  101.         return EMFILE;
  102.    
  103.     if (!strcmp(fname, "stdin")) {
  104.         streams[c] = open_stdin();
  105.         return c;
  106.     }
  107.    
  108.     if (!strcmp(fname, "stdout")) {
  109.         //streams[c].w = write_stdout;
  110.         //return c;
  111.         streams[c] = open_stdout();
  112.         return c;
  113.     }
  114.    
  115.     if (!strcmp(fname, "stderr")) {
  116.         streams[c].w = write_stderr;
  117.         return c;
  118.     }
  119. }
  120.  
  121.  
  122. ssize_t write(int fd, const void *buf, size_t count)
  123. {
  124.     if (fd < FDS)
  125.         return streams[fd].w(streams[fd].param, buf, count);
  126.    
  127.     return 0;
  128. }
  129.