home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / fsext / fsext.txh < prev    next >
Encoding:
Text File  |  1996-09-19  |  6.5 KB  |  239 lines

  1. @node File System Extensions, file system
  2.  
  3. @subheading Description
  4.  
  5. The File System Extensions are a part of the lowest level of I/O
  6. operations in the C runtime library of DJGPP.  These extensions are
  7. provided to allow for cases where Unix uses a file descriptor to
  8. access such items as serial ports, memory, and the network, but DOS
  9. does not.  It allows a set of functions (called an @i{extension}) to
  10. gain control when one of these low-level functions is called on a file
  11. descriptor set up by the extension.
  12.  
  13. Each extension must provide one or two handler functions.  All handler
  14. functions take the same arguments:
  15.  
  16. @example
  17.   int function(__FSEXT_Fnumber func_number, int *rv, va_list args);
  18. @end example
  19.  
  20. The @var{func_number} identifies which function is to be emulated.
  21. The file @code{<sys/fsext.h>} defines the function numbers as follows:
  22.  
  23. @table @code
  24. @item __FSEXT_nop
  25.  
  26. A no-op.  This is currently unused by the library functions.
  27.  
  28. @item __FSEXT_open
  29.  
  30. An open handler.  This is called just before the library is about to
  31. issue the DOS OpenFile call on behalf of your program.
  32.  
  33. @item __FSEXT_creat
  34.  
  35. A create handler.  Called when a file needs to be created.  Note that
  36. the handler should both create the ``file'' and open it.
  37.  
  38. @item __FSEXT_read
  39.  
  40. A read handler.  Called when data should be read from a ``file''.
  41.  
  42. @item __FSEXT_write
  43.  
  44. A write handler.  Called to write data to a ``file''.
  45.  
  46. @item __FSEXT_read
  47.  
  48. A ready handler.  It is called by @code{select} library function
  49. (@pxref{select}) when it needs to know whether a handle used to
  50. reference the ``file'' is ready for reading or writing, or has an error
  51. condition set.  The handler should return an OR'ed bit mask of the
  52. following bits (defined on @code{<sys/fsext.h>}):
  53.  
  54. @table @code
  55.  
  56. @item __FSEXT_ready_read
  57.  
  58. The ``file'' is ready for reading.
  59.  
  60. @item __FSEXT_ready_write
  61.  
  62. The ``file'' is ready for writing.
  63.  
  64. @item __FSEXT_ready_error
  65.  
  66. The ``file'' has an error condition set.
  67.  
  68. @end table
  69.  
  70. @item __FSEXT_close
  71.  
  72. A close handler.  Called when the ``file'' should be closed.
  73.  
  74. @end table
  75.  
  76.  
  77. @var{rv} points to a temporary return value pointer.  If the function is
  78. emulated by the handler, the return value should be stored here, and the
  79. handler should return a nonzero value.  If the handler returns zero, it is
  80. assumed to have not emulated the call, and the regular DOS I/O function
  81. will happen.  The @var{args} represent the arguments passed to the
  82. original function; these point to the actual arguments on the stack, so
  83. the emulation may choose to modify them and return zero to the regular
  84. function, which will then act on the modified arguments.
  85.  
  86. A normal extension would provide these parts:
  87.  
  88. @itemize @bullet
  89.  
  90. @item
  91.  
  92. Some function to create a connection to the extension.  This may be a
  93. custom function (such as @code{socket} for networking) or an extension
  94. to open (such as for @code{/dev/null} emulation).
  95.  
  96. @item
  97.  
  98. Initialization code that adds the open handler, if any.
  99.  
  100. @item
  101.  
  102. Overrides for the basic I/O functions, such as @code{read} and
  103. @code{write}.  This is a single function in the extension that uses
  104. the function number parameter to select an extension function.
  105.  
  106. @item
  107.  
  108. The core functionality of the extension, if any.
  109.  
  110. @end itemize
  111.  
  112. @c ----------------------------------------------------------------------
  113. @node __FSEXT_alloc_fd, file system
  114. @subheading Syntax
  115.  
  116. @example
  117. #include <sys/fsext.h>
  118.  
  119. int __FSEXT_alloc_fd(__FSEXT_Function *_function);
  120. @end example
  121.  
  122. @subheading Description
  123.  
  124. This function is part of the @ref{File System Extensions}.  It is used
  125. by extensions that fully emulate the I/O functions, and thus don't
  126. have a corresponding DOS file handle.  This function opens DOS's
  127. @samp{NUL} device, so as to allocate a handle that DOS won't then reuse.
  128. It also assigns the handler function for that descriptor.
  129.  
  130. The module is responsible for calling @code{_close} on the descriptor
  131. after setting the handler function to zero in the extended close
  132. handler.
  133.  
  134. @subheading Example
  135.  
  136. @example
  137.  
  138. int socket()
  139. @{
  140.   int fd = __FSEXT_alloc_fd(socket_handler);
  141.   init_socket(fd);
  142.   return fd;
  143. @}
  144. @end example
  145.  
  146.  
  147. @c ----------------------------------------------------------------------
  148. @node __FSEXT_set_function, file system
  149. @subheading Syntax
  150.  
  151. @example
  152. #include <sys/fsext.h>
  153.  
  154. int __FSEXT_set_function(int _fd, __FSEXT_Function *_function);
  155. @end example
  156.  
  157. @subheading Description
  158.  
  159. This function is part of the @ref{File System Extensions}.  It is used
  160. to set the handler function for those extensions that use DOS files
  161. for I/O.  One situation where you might need this is when you must catch
  162. output to the terminal and play some tricks with it, like colorize it or
  163. redirect it to another device.
  164.  
  165. @subheading Return Value
  166.  
  167. Zero in case of success, non-zero in case of failure (like if @var{_fd}
  168. is negative).
  169.  
  170. @subheading Example
  171.  
  172. @example
  173.  
  174. #include <sys/fsext.h>
  175. #include <conio.h>
  176.  
  177. /* A simple example of a write handler which converts DOS I/O to the
  178.    screen into direct writes to video RAM.  */
  179. static int
  180. my_screen_write (__FSEXT_Fnumber func, int *retval, va_list rest_args)
  181. @{
  182.   char *buf, *mybuf;
  183.   size_t buflen;
  184.   int fd = va_arg (rest_args, int);
  185.  
  186.   if (func != __FSEXT_write || !isatty (fd))
  187.     return 0;  /* and the usual DOS call will be issued */
  188.  
  189.   buf = va_arg (rest_args, char *);
  190.   buflen = va_arg (rest_args, size_t);
  191.   mybuf = alloca (buflen + 1);
  192.   memcpy (mybuf, buf, buflen);
  193.   mybuf[buflen] = '\0';
  194.   cputs (mybuf);
  195.   *retval = buflen;
  196.   return 1;  /* meaning that we handled the call */
  197. @}
  198.  
  199. /* Install our handler.  The `attribute constructor' causes this
  200.    function to be called by the startup code.  */
  201. static void __attribute__((constructor))
  202. install_screen_write_handler (void)
  203. @{
  204.   __FSEXT_set_function (fileno (stdout), my_screen_write);
  205. @}
  206.  
  207. @end example
  208.  
  209.  
  210. @c ----------------------------------------------------------------------
  211. @node __FSEXT_get_function, file system
  212. @subheading Syntax
  213.  
  214. @example
  215. #include <sys/fsext.h>
  216.  
  217. __FSEXT_Function *__FSEXT_get_function(int _fd);
  218. @end example
  219.  
  220. This function is part of the @ref{File System Extensions}.  It is used
  221. internal to libc.a to redirect I/O requests to the appropriate
  222. extensions.
  223.  
  224. @subheading Example
  225.  
  226. @example
  227. _read(int fd, void *buf, int len)
  228. @{
  229.   __FSEXT_Function *func = __FSEXT_get_function(fd);
  230.   if (func)
  231.   @{
  232.     int rv;
  233.     if (func(__FSEXT_read, &rv, &fd))
  234.       return rv;
  235.   @}
  236.   /* rest of read() */
  237. @}
  238. @end example
  239.