Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (C) 2006 Josef Cejka
  3.  * Copyright (C) 2006 Jakub Vana
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30.  /** @addtogroup libc
  31.  * @{
  32.  */
  33. /** @file
  34.  */
  35.  
  36.  
  37. #include <io/io.h>
  38. #include <io/stream.h>
  39. #include <string.h>
  40. #include <malloc.h>
  41. #include <libc.h>
  42. #include <ipc/ipc.h>
  43. #include <ipc/ns.h>
  44. #include <ipc/fb.h>
  45. #include <ipc/services.h>
  46. #include <console.h>
  47. #include <unistd.h>
  48. #include <async.h>
  49.  
  50. #define FDS 32
  51.  
  52. typedef struct stream_t {
  53.     pwritefn_t w;
  54.     preadfn_t r;
  55.     void * param;
  56.     int phone;
  57. } stream_t;
  58.  
  59. static int console_phone = -1;
  60. static stream_t streams[FDS];
  61.  
  62. static ssize_t write_stderr(void *param, const void *buf, size_t count)
  63. {
  64.     return count;
  65. }
  66.  
  67. static ssize_t read_stdin(void *param, void *buf, size_t count)
  68. {
  69.     ipcarg_t r0,r1;
  70.     size_t i = 0;
  71.  
  72.     while (i < count) {
  73.         if (async_req_2(streams[0].phone, CONSOLE_GETCHAR, 0, 0, &r0, &r1) < 0) {
  74.             return -1;
  75.         }
  76.         ((char *)buf)[i++] = r0;
  77.     }
  78.     return i;
  79. }
  80.  
  81. static ssize_t write_stdout(void *param, const void *buf, size_t count)
  82. {
  83.     int i;
  84.     ipcarg_t r0,r1;
  85.  
  86.     for (i = 0; i < count; i++)
  87.         async_msg(streams[1].phone, CONSOLE_PUTCHAR, ((const char *)buf)[i]);
  88.    
  89.     return count;
  90. }
  91.  
  92.  
  93.  
  94. static stream_t open_stdin(void)
  95. {
  96.     stream_t stream;
  97.     int phoneid;
  98.     int res;
  99.    
  100.     if (console_phone < 0) {
  101.         while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
  102.             usleep(10000);
  103.         }
  104.     }
  105.    
  106.     stream.r = read_stdin;
  107.     stream.param = 0;
  108.     stream.phone = console_phone;
  109.    
  110.     return stream;
  111. }
  112.  
  113. static stream_t open_stdout(void)
  114. {
  115.     stream_t stream;
  116.     int res;
  117.  
  118.     if (console_phone < 0) {
  119.         while ((console_phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0)) < 0) {
  120.             usleep(10000);
  121.         }
  122.     }
  123.    
  124.     stream.w = write_stdout;
  125.     stream.phone = console_phone;
  126.     stream.param = 0;
  127.     return stream;
  128. }
  129.  
  130. static ssize_t write_null(void *param, const void *buf, size_t count)
  131. {
  132.     return count;
  133. }
  134.  
  135.  
  136. fd_t open(const char *fname, int flags)
  137. {
  138.     int c = 0;
  139.  
  140.     while (((streams[c].w) || (streams[c].r)) && (c < FDS))
  141.         c++;
  142.     if (c == FDS)
  143.         return EMFILE;
  144.    
  145.     if (!strcmp(fname, "stdin")) {
  146.         streams[c] = open_stdin();
  147.         return c;
  148.     }
  149.    
  150.     if (!strcmp(fname, "stdout")) {
  151.         streams[c] = open_stdout();
  152.         return c;
  153.     }
  154.    
  155.     if (!strcmp(fname, "stderr")) {
  156.         streams[c].w = write_stderr;
  157.         return c;
  158.     }
  159.     if (!strcmp(fname, "null")) {
  160.         streams[c].w = write_null;
  161.         return c;
  162.     }
  163. }
  164.  
  165.  
  166. ssize_t write(int fd, const void *buf, size_t count)
  167. {
  168. //  __SYSCALL3(SYS_IO, 1, (sysarg_t)buf, (sysarg_t) count);
  169. //  return count;
  170.     if (fd < FDS)
  171.         return streams[fd].w(streams[fd].param, buf, count);
  172.    
  173.     return 0;
  174. }
  175.  
  176. ssize_t read(int fd, void *buf, size_t count)
  177. {
  178.     if (fd < FDS)
  179.         return streams[fd].r(streams[fd].param, buf, count);
  180.    
  181.     return 0;
  182. }
  183.  
  184. int get_fd_phone(int fd)
  185. {
  186.     if (fd >= FDS || fd < 0)
  187.         return -1;
  188.     return streams[fd].phone;
  189. }
  190.  
  191.  
  192.  /** @}
  193.  */
  194.  
  195.  
  196.