tclIO.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * tclIO.h --
  3. *
  4. * This file provides the generic portions (those that are the same on
  5. * all platforms and for all channel types) of Tcl's IO facilities.
  6. *
  7. * Copyright (c) 1998-2000 Ajuba Solutions
  8. * Copyright (c) 1995-1997 Sun Microsystems, Inc.
  9. *
  10. * See the file "license.terms" for information on usage and redistribution of
  11. * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. */
  13. /*
  14. * Make sure that both EAGAIN and EWOULDBLOCK are defined. This does not
  15. * compile on systems where neither is defined. We want both defined so that
  16. * we can test safely for both. In the code we still have to test for both
  17. * because there may be systems on which both are defined and have different
  18. * values.
  19. */
  20. #if ((!defined(EWOULDBLOCK)) && (defined(EAGAIN)))
  21. # define EWOULDBLOCK EAGAIN
  22. #endif
  23. #if ((!defined(EAGAIN)) && (defined(EWOULDBLOCK)))
  24. # define EAGAIN EWOULDBLOCK
  25. #endif
  26. #if ((!defined(EAGAIN)) && (!defined(EWOULDBLOCK)))
  27. #error one of EWOULDBLOCK or EAGAIN must be defined
  28. #endif
  29. /*
  30. * struct ChannelBuffer:
  31. *
  32. * Buffers data being sent to or from a channel.
  33. */
  34. typedef struct ChannelBuffer {
  35. int refCount; /* Current uses count */
  36. int nextAdded; /* The next position into which a character
  37. * will be put in the buffer. */
  38. int nextRemoved; /* Position of next byte to be removed from
  39. * the buffer. */
  40. int bufLength; /* How big is the buffer? */
  41. struct ChannelBuffer *nextPtr;
  42. /* Next buffer in chain. */
  43. char buf[TCLFLEXARRAY]; /* Placeholder for real buffer. The real
  44. * buffer occuppies this space + bufSize-1
  45. * bytes. This must be the last field in the
  46. * structure. */
  47. } ChannelBuffer;
  48. #define CHANNELBUFFER_HEADER_SIZE TclOffset(ChannelBuffer, buf)
  49. /*
  50. * How much extra space to allocate in buffer to hold bytes from previous
  51. * buffer (when converting to UTF-8) or to hold bytes that will go to next
  52. * buffer (when converting from UTF-8).
  53. */
  54. #define BUFFER_PADDING 16
  55. /*
  56. * The following defines the *default* buffer size for channels.
  57. */
  58. #define CHANNELBUFFER_DEFAULT_SIZE (1024 * 4)
  59. /*
  60. * The following structure describes the information saved from a call to
  61. * "fileevent". This is used later when the event being waited for to invoke
  62. * the saved script in the interpreter designed in this record.
  63. */
  64. typedef struct EventScriptRecord {
  65. struct Channel *chanPtr; /* The channel for which this script is
  66. * registered. This is used only when an error
  67. * occurs during evaluation of the script, to
  68. * delete the handler. */
  69. Tcl_Obj *scriptPtr; /* Script to invoke. */
  70. Tcl_Interp *interp; /* In what interpreter to invoke script? */
  71. int mask; /* Events must overlap current mask for the
  72. * stored script to be invoked. */
  73. struct EventScriptRecord *nextPtr;
  74. /* Next in chain of records. */
  75. } EventScriptRecord;
  76. /*
  77. * struct Channel:
  78. *
  79. * One of these structures is allocated for each open channel. It contains
  80. * data specific to the channel but which belongs to the generic part of the
  81. * Tcl channel mechanism, and it points at an instance specific (and type
  82. * specific) instance data, and at a channel type structure.
  83. */
  84. typedef struct Channel {
  85. struct ChannelState *state; /* Split out state information */
  86. ClientData instanceData; /* Instance-specific data provided by creator
  87. * of channel. */
  88. const Tcl_ChannelType *typePtr; /* Pointer to channel type structure. */
  89. struct Channel *downChanPtr;/* Refers to channel this one was stacked
  90. * upon. This reference is NULL for normal
  91. * channels. See Tcl_StackChannel. */
  92. struct Channel *upChanPtr; /* Refers to the channel above stacked this
  93. * one. NULL for the top most channel. */
  94. /*
  95. * Intermediate buffers to hold pre-read data for consumption by a newly
  96. * stacked transformation. See 'Tcl_StackChannel'.
  97. */
  98. ChannelBuffer *inQueueHead; /* Points at first buffer in input queue. */
  99. ChannelBuffer *inQueueTail; /* Points at last buffer in input queue. */
  100. int refCount;
  101. } Channel;
  102. /*
  103. * struct ChannelState:
  104. *
  105. * One of these structures is allocated for each open channel. It contains
  106. * data specific to the channel but which belongs to the generic part of the
  107. * Tcl channel mechanism, and it points at an instance specific (and type
  108. * specific) instance data, and at a channel type structure.
  109. */
  110. typedef struct ChannelState {
  111. char *channelName; /* The name of the channel instance in Tcl
  112. * commands. Storage is owned by the generic
  113. * IO code, is dynamically allocated. */
  114. int flags; /* ORed combination of the flags defined
  115. * below. */
  116. Tcl_Encoding encoding; /* Encoding to apply when reading or writing
  117. * data on this channel. NULL means no
  118. * encoding is applied to data. */
  119. Tcl_EncodingState inputEncodingState;
  120. /* Current encoding state, used when
  121. * converting input data bytes to UTF-8. */
  122. int inputEncodingFlags; /* Encoding flags to pass to conversion
  123. * routine when converting input data bytes to
  124. * UTF-8. May be TCL_ENCODING_START before
  125. * converting first byte and TCL_ENCODING_END
  126. * when EOF is seen. */
  127. Tcl_EncodingState outputEncodingState;
  128. /* Current encoding state, used when
  129. * converting UTF-8 to output data bytes. */
  130. int outputEncodingFlags; /* Encoding flags to pass to conversion
  131. * routine when converting UTF-8 to output
  132. * data bytes. May be TCL_ENCODING_START
  133. * before converting first byte and
  134. * TCL_ENCODING_END when EOF is seen. */
  135. TclEolTranslation inputTranslation;
  136. /* What translation to apply for end of line
  137. * sequences on input? */
  138. TclEolTranslation outputTranslation;
  139. /* What translation to use for generating end
  140. * of line sequences in output? */
  141. int inEofChar; /* If nonzero, use this as a signal of EOF on
  142. * input. */
  143. int outEofChar; /* If nonzero, append this to the channel when
  144. * it is closed if it is open for writing. */
  145. int unreportedError; /* Non-zero if an error report was deferred
  146. * because it happened in the background. The
  147. * value is the POSIX error code. */
  148. int refCount; /* How many interpreters hold references to
  149. * this IO channel? */
  150. struct CloseCallback *closeCbPtr;
  151. /* Callbacks registered to be called when the
  152. * channel is closed. */
  153. char *outputStage; /* Temporary staging buffer used when
  154. * translating EOL before converting from
  155. * UTF-8 to external form. */
  156. ChannelBuffer *curOutPtr; /* Current output buffer being filled. */
  157. ChannelBuffer *outQueueHead;/* Points at first buffer in output queue. */
  158. ChannelBuffer *outQueueTail;/* Points at last buffer in output queue. */
  159. ChannelBuffer *saveInBufPtr;/* Buffer saved for input queue - eliminates
  160. * need to allocate a new buffer for "gets"
  161. * that crosses buffer boundaries. */
  162. ChannelBuffer *inQueueHead; /* Points at first buffer in input queue. */
  163. ChannelBuffer *inQueueTail; /* Points at last buffer in input queue. */
  164. struct ChannelHandler *chPtr;/* List of channel handlers registered for
  165. * this channel. */
  166. int interestMask; /* Mask of all events this channel has
  167. * handlers for. */
  168. EventScriptRecord *scriptRecordPtr;
  169. /* Chain of all scripts registered for event
  170. * handlers ("fileevent") on this channel. */
  171. int bufSize; /* What size buffers to allocate? */
  172. Tcl_TimerToken timer; /* Handle to wakeup timer for this channel. */
  173. struct CopyState *csPtrR; /* State of background copy for which channel
  174. * is input, or NULL. */
  175. struct CopyState *csPtrW; /* State of background copy for which channel
  176. * is output, or NULL. */
  177. Channel *topChanPtr; /* Refers to topmost channel in a stack. Never
  178. * NULL. */
  179. Channel *bottomChanPtr; /* Refers to bottommost channel in a stack.
  180. * This channel can be relied on to live as
  181. * long as the channel state. Never NULL. */
  182. struct ChannelState *nextCSPtr;
  183. /* Next in list of channels currently open. */
  184. Tcl_ThreadId managingThread;/* TIP #10: Id of the thread managing this
  185. * stack of channels. */
  186. /*
  187. * TIP #219 ... Info for the I/O system ...
  188. * Error message set by channel drivers, for the propagation of arbitrary
  189. * Tcl errors. This information, if present (chanMsg not NULL), takes
  190. * precedence over a posix error code returned by a channel operation.
  191. */
  192. Tcl_Obj* chanMsg;
  193. Tcl_Obj* unreportedMsg; /* Non-NULL if an error report was deferred
  194. * because it happened in the background. The
  195. * value is the chanMg, if any. #219's
  196. * companion to 'unreportedError'. */
  197. int epoch; /* Used to test validity of stored channelname
  198. * lookup results. */
  199. } ChannelState;
  200. /*
  201. * Values for the flags field in Channel. Any ORed combination of the
  202. * following flags can be stored in the field. These flags record various
  203. * options and state bits about the channel. In addition to the flags below,
  204. * the channel can also have TCL_READABLE (1<<1) and TCL_WRITABLE (1<<2) set.
  205. */
  206. #define CHANNEL_NONBLOCKING (1<<3) /* Channel is currently in nonblocking
  207. * mode. */
  208. #define CHANNEL_LINEBUFFERED (1<<4) /* Output to the channel must be
  209. * flushed after every newline. */
  210. #define CHANNEL_UNBUFFERED (1<<5) /* Output to the channel must always
  211. * be flushed immediately. */
  212. #define BG_FLUSH_SCHEDULED (1<<7) /* A background flush of the queued
  213. * output buffers has been
  214. * scheduled. */
  215. #define CHANNEL_CLOSED (1<<8) /* Channel has been closed. No further
  216. * Tcl-level IO on the channel is
  217. * allowed. */
  218. #define CHANNEL_EOF (1<<9) /* EOF occurred on this channel. This
  219. * bit is cleared before every input
  220. * operation. */
  221. #define CHANNEL_STICKY_EOF (1<<10) /* EOF occurred on this channel
  222. * because we saw the input
  223. * eofChar. This bit prevents clearing
  224. * of the EOF bit before every input
  225. * operation. */
  226. #define CHANNEL_BLOCKED (1<<11) /* EWOULDBLOCK or EAGAIN occurred on
  227. * this channel. This bit is cleared
  228. * before every input or output
  229. * operation. */
  230. #define INPUT_SAW_CR (1<<12) /* Channel is in CRLF eol input
  231. * translation mode and the last byte
  232. * seen was a "\r". */
  233. #define CHANNEL_DEAD (1<<13) /* The channel has been closed by the
  234. * exit handler (on exit) but not
  235. * deallocated. When any IO operation
  236. * sees this flag on a channel, it
  237. * does not call driver level
  238. * functions to avoid referring to
  239. * deallocated data. */
  240. #define CHANNEL_NEED_MORE_DATA (1<<14) /* The last input operation failed
  241. * because there was not enough data
  242. * to complete the operation. This
  243. * flag is set when gets fails to get
  244. * a complete line or when read fails
  245. * to get a complete character. When
  246. * set, file events will not be
  247. * delivered for buffered data until
  248. * the state of the channel
  249. * changes. */
  250. #define CHANNEL_RAW_MODE (1<<16) /* When set, notes that the Raw API is
  251. * being used. */
  252. #define CHANNEL_INCLOSE (1<<19) /* Channel is currently being closed.
  253. * Its structures are still live and
  254. * usable, but it may not be closed
  255. * again from within the close
  256. * handler. */
  257. #define CHANNEL_CLOSEDWRITE (1<<21) /* Channel write side has been closed.
  258. * No further Tcl-level write IO on
  259. * the channel is allowed. */
  260. /*
  261. * The length of time to wait between synthetic timer events. Must be zero or
  262. * bad things tend to happen.
  263. */
  264. #define SYNTHETIC_EVENT_TIME 0
  265. /*
  266. * Local Variables:
  267. * mode: c
  268. * c-basic-offset: 4
  269. * fill-column: 78
  270. * End:
  271. */