| 1 |
/* |
| 2 |
* gauche/port.h - Port API |
| 3 |
* |
| 4 |
* Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 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 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#ifndef GAUCHE_PORT_H |
| 35 |
#define GAUCHE_PORT_H |
| 36 |
|
| 37 |
/* Port is the Scheme way of I/O abstraction. R5RS's definition of |
| 38 |
* of the port is very simple and straightforward. Practical |
| 39 |
* applications, however, require far more detailed control over |
| 40 |
* the I/O channel, as well as the reasonable performance. |
| 41 |
* |
| 42 |
* Current implementation is a bit messy, trying to achieve both |
| 43 |
* performance and feature requirements. In the core API level, |
| 44 |
* ports are categorized in one of three types: file ports, string |
| 45 |
* ports and procedural ports. A port may be an input port or |
| 46 |
* an output port. A port may handle byte (binary) streams, |
| 47 |
* as well as character streams. Some port may |
| 48 |
* interchange byte (binary) I/O versus character I/O, while some |
| 49 |
* may signal an error if you mix those operations. |
| 50 |
* |
| 51 |
* You shouldn't rely on the underlying port implementation, for |
| 52 |
* it is likely to be changed in future. There are enough macros |
| 53 |
* and API functions provided to use and extend the port mechanism. |
| 54 |
* See also ext/vport for the way to extend the port from Scheme. |
| 55 |
* |
| 56 |
* Most public port APIs locks the given port to ensure it won't |
| 57 |
* interfere with other threads. Some basic APIs have corresponding |
| 58 |
* "Unsafe" version (e.g. Scm_Putc() vs Scm_PutcUnsafe()), which |
| 59 |
* assumes the caller already has the lock; but the base version |
| 60 |
* won't do extra locking when the calling thread alreay hold the lock, |
| 61 |
* so there's no reason for general code to call unsafe verson directly. |
| 62 |
*/ |
| 63 |
|
| 64 |
/*================================================================ |
| 65 |
* Port structures & flags |
| 66 |
*/ |
| 67 |
|
| 68 |
/* Substructures */ |
| 69 |
|
| 70 |
/* The alternative of FILE* structure, used by buffered (file) port. |
| 71 |
The members are owned by the port, and client shouldn't change the |
| 72 |
elements. You can create your own custom buffered port by using |
| 73 |
Scm_MakeBufferedPort() --- with it, you pass ScmPortBuffer with |
| 74 |
the function pointers filled in, which is copied to the port's |
| 75 |
internal ScmPortBuffer structure. |
| 76 |
See port.c for the details of function pointers. */ |
| 77 |
|
| 78 |
typedef struct ScmPortBufferRec { |
| 79 |
char *buffer; /* ptr to the buffer area */ |
| 80 |
char *current; /* current buffer position */ |
| 81 |
char *end; /* the end of the current valid data */ |
| 82 |
ScmSize size; /* buffer size */ |
| 83 |
int mode; /* buffering mode (ScmPortBufferMode) & SIGPIPE flag */ |
| 84 |
ScmSize (*filler)(ScmPort *p, ScmSize min); |
| 85 |
ScmSize (*flusher)(ScmPort *p, ScmSize cnt, int forcep); |
| 86 |
void (*closer)(ScmPort *p); |
| 87 |
int (*ready)(ScmPort *p); |
| 88 |
int (*filenum)(ScmPort *p); |
| 89 |
off_t (*seeker)(ScmPort *p, off_t offset, int whence); |
| 90 |
void *data; /* aux data to be used in handlers (heap allocated) */ |
| 91 |
long idata; /* aux data to be used in handlers (single word) */ |
| 92 |
|
| 93 |
/* The following fields are added in 0.9.10. When you pass ScmPortBuffer |
| 94 |
to the constructor, those fields are looked at only when you specify |
| 95 |
SCM_PORT_WITH_POSITION. They are needed to fully support srfi-192-style |
| 96 |
port positioning. See the Port Positioning Interface below. */ |
| 97 |
ScmObj (*getpos)(ScmPort *p); |
| 98 |
void (*setpos)(ScmPort *p, ScmObj pos); |
| 99 |
u_long flags; /* reserved */ |
| 100 |
} ScmPortBuffer; |
| 101 |
|
| 102 |
/* The function table of procedural port. */ |
| 103 |
typedef struct ScmPortVTableRec { |
| 104 |
int (*Getb)(ScmPort *p); |
| 105 |
int (*Getc)(ScmPort *p); |
| 106 |
ScmSize (*Getz)(char *buf, ScmSize buflen, ScmPort *p); |
| 107 |
int (*Ready)(ScmPort *p, int charp); |
| 108 |
void (*Putb)(ScmByte b, ScmPort *p); |
| 109 |
void (*Putc)(ScmChar c, ScmPort *p); |
| 110 |
void (*Putz)(const char *buf, ScmSize size, ScmPort *p); |
| 111 |
void (*Puts)(ScmString *s, ScmPort *p); |
| 112 |
void (*Flush)(ScmPort *p); |
| 113 |
void (*Close)(ScmPort *p); |
| 114 |
off_t (*Seek)(ScmPort *p, off_t off, int whence); |
| 115 |
void *data; /* aux data to be used in handlers (heap allocated) */ |
| 116 |
long idata; /* aux data to be used in handlers (single word) */ |
| 117 |
|
| 118 |
/* The following fields are added in 0.9.10. When you pass ScmPortBuffer |
| 119 |
to the constructor, those fields are looked at only when you specify |
| 120 |
SCM_PORT_WITH_POSITION. They are needed to fully support srfi-192-style |
| 121 |
port positioning. See the Port Positioning Interface below. */ |
| 122 |
ScmObj (*GetPos)(ScmPort *p); |
| 123 |
void (*SetPos)(ScmPort *p, ScmObj pos); |
| 124 |
u_long flags; /* reserved */ |
| 125 |
} ScmPortVTable; |
| 126 |
|
| 127 |
/* Input string port */ |
| 128 |
typedef struct ScmPortInputStringRec { |
| 129 |
const char *start; |
| 130 |
const char *current; |
| 131 |
const char *end; |
| 132 |
} ScmPortInputString; |
| 133 |
|
| 134 |
/* Note on Port Positioning Interface |
| 135 |
|
| 136 |
Gauche historically supported lseek(2) style interface, which can handle |
| 137 |
both getting and setting the position. R6RS introduced |
| 138 |
port-position and set-port-position! interface, which then adopted as |
| 139 |
SRFI-192. It requires different strategy than lseek interface. |
| 140 |
|
| 141 |
1) SRFI-192 allows port position can be gotten but not be set, and that |
| 142 |
needs to be queried by port-has-set-port-position!?. With lseek API, |
| 143 |
port position is either totally unavaiable or can be gotten/set. |
| 144 |
(seek callback may raise an error when setting is not supported, but |
| 145 |
it can't be queried.) |
| 146 |
|
| 147 |
2) SRFI-192 allows arbitrary Scheme object as the position, while lseek |
| 148 |
API assumes integer byte offset. Indeed, for the textual port, meaning |
| 149 |
of byte offset is vague. |
| 150 |
|
| 151 |
3) Lseek allows position to be set relative to the current position or |
| 152 |
to the end of the file. SRFI-192 set-port-position! is always the |
| 153 |
absolute position. |
| 154 |
|
| 155 |
To support both interface, we have these hooks in SCM_PORT_FILE and |
| 156 |
SCM_PORT_PROC type of ports (the field names differ for the historical |
| 157 |
reasons): |
| 158 |
|
| 159 |
off_t SEEK(ScmPort*, off_t, int) ;; seeker, Seek |
| 160 |
ScmObj GETPOS(ScmPort*) ;; getpos, GetPos |
| 161 |
void SETPOS(ScmPort*, ScmObj) ;; setpos, SetPos |
| 162 |
|
| 163 |
GETPOS/SETPOS takes precedence to SEEK. |
| 164 |
|
| 165 |
Port is port-has-port-position? if it has either GETPOS or SEEK handler, |
| 166 |
and port-has-set-port-position!? if it has either SETPOS or SEEK handler. |
| 167 |
|
| 168 |
For SCM_PORT_BUF, the position returned by GETPOS and that SETPOS takes |
| 169 |
must be an exact Scheme integer, representing the integer byte offset. |
| 170 |
Since the input is buffered, we need to calculate the actual position |
| 171 |
in the buffer, from the source position returned by GETPOS. |
| 172 |
|
| 173 |
For SCM_PORT_PROC, we treat the position as an opaque object. Note that |
| 174 |
input port has a small buffer for peeked byte/character, that makes |
| 175 |
things complicated. Since we can't adjust the opaque position, we instead |
| 176 |
call GETPOS before peek and remember the position. |
| 177 |
*/ |
| 178 |
|
| 179 |
/* |
| 180 |
* We don't expose much about ScmPort struct to the user. A bunch of |
| 181 |
* flags are public, but most of the stuff are hidden. See priv/portP.h |
| 182 |
* for the 'real' definition. |
| 183 |
* |
| 184 |
* ScmPort should never be allocated directly, or statically. Port |
| 185 |
* constructors properly initializes hidden fields. |
| 186 |
*/ |
| 187 |
|
| 188 |
#define SCM_PORT_HEADER \ |
| 189 |
SCM_INSTANCE_HEADER; \ |
| 190 |
u_int direction : 2; /* SCM_PORT_INPUT or SCM_PORT_OUTPUT. \ |
| 191 |
There may be I/O port in future. */ \ |
| 192 |
u_int type : 2; /* SCM_PORT_{FILE|ISTR|OSTR|PROC} */ \ |
| 193 |
u_int scrcnt : 3; /* # of bytes in the scratch buffer */ \ |
| 194 |
\ |
| 195 |
u_int ownerp : 1; /* TRUE if this port owns underlying \ |
| 196 |
file pointer */ \ |
| 197 |
u_int closed : 1; /* TRUE if this port is closed */ \ |
| 198 |
u_int error : 1; /* Error has been occurred */ \ |
| 199 |
\ |
| 200 |
u_int flags : 5 /* see ScmPortFlags below */ |
| 201 |
|
| 202 |
struct ScmPortRec { |
| 203 |
SCM_PORT_HEADER; |
| 204 |
/* See portP.h for the real definition. The magic number is chosen |
| 205 |
so that ScmPort has enough size to contain the real struct. |
| 206 |
|
| 207 |
We tested to use a pointer to the real struct, but I/O intensive |
| 208 |
benchmark showed 2-3% reduction of speed. So we avoid indirection. |
| 209 |
*/ |
| 210 |
char opaque[30*sizeof(ScmWord) + 2*sizeof(ScmInternalFastlock)]; |
| 211 |
}; |
| 212 |
|
| 213 |
/* Port direction. |
| 214 |
Bidirectional port have both SCM_PORT_INPUT and SCM_PORT_OUTPUT bits set. |
| 215 |
SCM_PORT_OUTPUT_TRANSIENT is only used for constructor to indicate |
| 216 |
that the output buffer doesn't need to be flushed when the process |
| 217 |
exits. It is same as SCM_PORT_OUTPUT for non-buffered ports. |
| 218 |
Only the lower two bits are stored in port->direction. |
| 219 |
*/ |
| 220 |
enum ScmPortDirection { |
| 221 |
SCM_PORT_INPUT = 1, |
| 222 |
SCM_PORT_OUTPUT = 2, |
| 223 |
SCM_PORT_IOMASK = 3, |
| 224 |
SCM_PORT_OUTPUT_TRANSIENT = 4+SCM_PORT_OUTPUT |
| 225 |
}; |
| 226 |
|
| 227 |
/* |
| 228 |
* The 'mode' flags |
| 229 |
* |
| 230 |
* Bit 0, 1, 2 - ScmPortBufferingMode |
| 231 |
* Bit 3-7 - Reserved |
| 232 |
* Bit 8 - SCM_PORT_BUFFER_SIGPIPE_SENSITIVE |
| 233 |
*/ |
| 234 |
|
| 235 |
/* Port buffering mode */ |
| 236 |
enum ScmPortBufferMode { |
| 237 |
SCM_PORT_BUFFER_FULL = 0, /* full buffering */ |
| 238 |
SCM_PORT_BUFFER_LINE = 1, /* flush the buffer for each line */ |
| 239 |
SCM_PORT_BUFFER_NONE = 2, /* flush the buffer for every output */ |
| 240 |
|
| 241 |
SCM_PORT_BUFFER_MODE_MASK = 0x07 /* for future extension */ |
| 242 |
}; |
| 243 |
|
| 244 |
/* If this flag is set in `mode' member of ScmPortBuffer, SIGPIPE causes |
| 245 |
the process to terminate. By default this flag is off except stdin and |
| 246 |
stdout ports; it is to emulate default Unix behavior. On Windows platform |
| 247 |
this flag is ignored, for we don't have SIGPIPE. */ |
| 248 |
#define SCM_PORT_BUFFER_SIGPIPE_SENSITIVE (1L<<8) |
| 249 |
|
| 250 |
/* Port types. The type is also represented by a port's class, but |
| 251 |
C routine can dispatch quicker using these flags. User code |
| 252 |
doesn't need to care about these. */ |
| 253 |
enum ScmPortType { |
| 254 |
SCM_PORT_FILE, /* file (buffered) port */ |
| 255 |
SCM_PORT_ISTR, /* input string port */ |
| 256 |
SCM_PORT_OSTR, /* output string port */ |
| 257 |
SCM_PORT_PROC /* virtual port */ |
| 258 |
}; |
| 259 |
|
| 260 |
/* Return value from Scm_FdReady */ |
| 261 |
enum ScmFdReadyResult { |
| 262 |
SCM_FD_WOULDBLOCK, |
| 263 |
SCM_FD_READY, |
| 264 |
SCM_FD_UNKNOWN |
| 265 |
}; |
| 266 |
|
| 267 |
/* The flag bits passed to 'flags' argument of various port constructors */ |
| 268 |
enum ScmPortConstructorFlag { |
| 269 |
SCM_PORT_OWNER = (1L<<0), /* Set the ownerp flag of the port */ |
| 270 |
SCM_PORT_WITH_POSITION = (1L<<1) /* The additional getpos/setpos fields |
| 271 |
are looked at. */ |
| 272 |
}; |
| 273 |
|
| 274 |
/* Other flags used internally */ |
| 275 |
/* NB: The first two flags only matter when port->recursiveContext is set, |
| 276 |
and they're transient by nature. See write.c for the details. */ |
| 277 |
enum ScmPortFlags { |
| 278 |
SCM_PORT_WRITESS = (1L<<0), /* we're write/ss mode. */ |
| 279 |
SCM_PORT_WALKING = (1L<<1), /* indicates we're currently in 'walk' pass |
| 280 |
of two-pass writing. */ |
| 281 |
SCM_PORT_PRIVATE = (1L<<2), /* this port is for 'private' use within |
| 282 |
a thread, so never need to be locked. */ |
| 283 |
SCM_PORT_CASE_FOLD = (1L<<3),/* read from or write to this port should |
| 284 |
be case folding. */ |
| 285 |
SCM_PORT_TRANSIENT = (1L<<4) /* a buffered output port that's used |
| 286 |
transiently and doesn't need to be |
| 287 |
registered for flushing. */ |
| 288 |
}; |
| 289 |
|
| 290 |
/*================================================================ |
| 291 |
* Generic operations |
| 292 |
*/ |
| 293 |
|
| 294 |
/* Predicates & accessors */ |
| 295 |
#define SCM_PORTP(obj) (SCM_ISA(obj, SCM_CLASS_PORT)) |
| 296 |
|
| 297 |
#define SCM_PORT(obj) ((ScmPort *)(obj)) |
| 298 |
#define SCM_PORT_TYPE(obj) (SCM_PORT(obj)->type) |
| 299 |
#define SCM_PORT_DIR(obj) (SCM_PORT(obj)->direction) |
| 300 |
#define SCM_PORT_FLAGS(obj) (SCM_PORT(obj)->flags) |
| 301 |
|
| 302 |
#define SCM_PORT_CASE_FOLDING(obj) (SCM_PORT_FLAGS(obj)&SCM_PORT_CASE_FOLD) |
| 303 |
|
| 304 |
#define SCM_PORT_CLOSED_P(obj) (SCM_PORT(obj)->closed) |
| 305 |
#define SCM_PORT_OWNER_P(obj) (SCM_PORT(obj)->ownerp) |
| 306 |
#define SCM_PORT_ERROR_OCCURRED_P(obj) (SCM_PORT(obj)->error) |
| 307 |
|
| 308 |
#define SCM_IPORTP(obj) (SCM_PORTP(obj)&&(SCM_PORT_DIR(obj)&SCM_PORT_INPUT)) |
| 309 |
#define SCM_OPORTP(obj) (SCM_PORTP(obj)&&(SCM_PORT_DIR(obj)&SCM_PORT_OUTPUT)) |
| 310 |
|
| 311 |
/* The following macros are for the backward compatibility; their use is |
| 312 |
deprecated. (We used to have these macros directly access ScmPort |
| 313 |
structure to eliminate function call overhead. It turned out |
| 314 |
it matters little. Use function APIs.) */ |
| 315 |
#define SCM_PUTB(b, p) Scm_Putb(b, SCM_PORT(p)) |
| 316 |
#define SCM_PUTC(c, p) Scm_Putc(c, SCM_PORT(p)) |
| 317 |
#define SCM_PUTZ(s, l, p) Scm_Putz(s, l, SCM_PORT(p)) |
| 318 |
#define SCM_PUTS(s, p) Scm_Puts(SCM_STRING(s), SCM_PORT(p)) |
| 319 |
#define SCM_FLUSH(p) Scm_Flush(SCM_PORT(p)) |
| 320 |
#define SCM_PUTNL(p) SCM_PUTC('\n', p) |
| 321 |
#define SCM_UNGETC(c, port) Scm_Ungetc(c, SCM_PORT(port)) |
| 322 |
#define SCM_GETB(b, p) (b = Scm_Getb(SCM_PORT(p))) |
| 323 |
#define SCM_GETC(c, p) (c = Scm_Getc(SCM_PORT(p))) |
| 324 |
#define SCM_PORT_BUFFER_ROOM(p) Scm_PortBufferRoom(p) |
| 325 |
#define SCM_PORT_BUFFER_AVAIL(p) Scm_PortBufferAvail(p) |
| 326 |
|
| 327 |
|
| 328 |
SCM_CLASS_DECL(Scm_PortClass); |
| 329 |
#define SCM_CLASS_PORT (&Scm_PortClass) |
| 330 |
|
| 331 |
SCM_CLASS_DECL(Scm_CodingAwarePortClass); |
| 332 |
#define SCM_CLASS_CODING_AWARE_PORT (&Scm_CodingAwarePortClass) |
| 333 |
|
| 334 |
/* Conversion between Scheme keyword and ScmPortBufferMode enums */ |
| 335 |
SCM_EXTERN ScmObj Scm_GetPortBufferingModeAsKeyword(ScmPort *port); |
| 336 |
SCM_EXTERN int Scm_BufferingModeAsKeyword(ScmObj flag, |
| 337 |
int direction, |
| 338 |
int fallback); |
| 339 |
|
| 340 |
SCM_EXTERN ScmObj Scm_GetBufferingMode(ScmPort *port); /* obsoleted */ |
| 341 |
SCM_EXTERN int Scm_BufferingMode(ScmObj flag, /* obsoleted */ |
| 342 |
int direction, |
| 343 |
int fallback); |
| 344 |
|
| 345 |
SCM_EXTERN int Scm_GetPortBufferingMode(ScmPort *port); |
| 346 |
SCM_EXTERN void Scm_SetPortBufferingMode(ScmPort *port, int mode); |
| 347 |
SCM_EXTERN int Scm_GetPortBufferSigpipeSensitive(ScmPort *port); |
| 348 |
SCM_EXTERN void Scm_SetPortBufferSigpipeSensitive(ScmPort *port, int sensitive); |
| 349 |
SCM_EXTERN int Scm_GetPortCaseFolding(ScmPort *port); |
| 350 |
SCM_EXTERN void Scm_SetPortCaseFolding(ScmPort *port, int flag); |
| 351 |
SCM_EXTERN ScmObj Scm_GetPortReaderLexicalMode(ScmPort *port); |
| 352 |
SCM_EXTERN void Scm_SetPortReaderLexicalMode(ScmPort *port, ScmObj obj); |
| 353 |
|
| 354 |
SCM_EXTERN void Scm_SetPortErrorOccurred(ScmPort *port, int val); |
| 355 |
|
| 356 |
SCM_EXTERN int Scm_PortPositionable(ScmPort *port, int setp); |
| 357 |
|
| 358 |
SCM_EXTERN void Scm_FlushAllPorts(int exitting); |
| 359 |
|
| 360 |
SCM_EXTERN void Scm_LinkPorts(ScmPort *iport, ScmPort *oport); |
| 361 |
SCM_EXTERN void Scm_UnlinkPorts(ScmPort *port); |
| 362 |
|
| 363 |
SCM_EXTERN ScmObj Scm_PortName(ScmPort *port); |
| 364 |
SCM_EXTERN ScmSize Scm_PortLine(ScmPort *port); |
| 365 |
SCM_EXTERN ScmSize Scm_PortBytes(ScmPort *port); |
| 366 |
SCM_EXTERN ScmSize Scm_PortColumn(ScmPort *port); |
| 367 |
SCM_EXTERN ScmObj Scm_GetPortPosition(ScmPort *port); |
| 368 |
SCM_EXTERN ScmObj Scm_GetPortPositionUnsafe(ScmPort *port); |
| 369 |
SCM_EXTERN ScmObj Scm_SetPortPosition(ScmPort *port, ScmObj pos); |
| 370 |
SCM_EXTERN ScmObj Scm_PortSeek(ScmPort *port, ScmObj off, int whence); |
| 371 |
SCM_EXTERN ScmObj Scm_PortSeekUnsafe(ScmPort *port, ScmObj off, int whence); |
| 372 |
SCM_EXTERN int Scm_PortFileNo(ScmPort *port); |
| 373 |
SCM_EXTERN void Scm_PortFdDup(ScmPort *dst, ScmPort *src); |
| 374 |
SCM_EXTERN int Scm_FdReady(int fd, int dir); |
| 375 |
SCM_EXTERN int Scm_ByteReady(ScmPort *port); |
| 376 |
SCM_EXTERN int Scm_ByteReadyUnsafe(ScmPort *port); |
| 377 |
SCM_EXTERN int Scm_CharReady(ScmPort *port); |
| 378 |
SCM_EXTERN int Scm_CharReadyUnsafe(ScmPort *port); |
| 379 |
|
| 380 |
SCM_EXTERN ScmPortBuffer *Scm_PortBufferStruct(ScmPort *port); |
| 381 |
SCM_EXTERN ScmPortInputString *Scm_PortInputStringStruct(ScmPort *port); |
| 382 |
SCM_EXTERN ScmDString *Scm_PortOutputDString(ScmPort *port); |
| 383 |
SCM_EXTERN ScmPortVTable *Scm_PortVTableStruct(ScmPort *port); |
| 384 |
|
| 385 |
SCM_EXTERN ScmSize Scm_PortBufferRoom(ScmPort *port); |
| 386 |
SCM_EXTERN ScmSize Scm_PortBufferAvail(ScmPort *port); |
| 387 |
|
| 388 |
SCM_EXTERN ScmWriteState *Scm_PortWriteState(ScmPort *); |
| 389 |
SCM_EXTERN void Scm_PortWriteStateSet(ScmPort *, ScmWriteState*); |
| 390 |
|
| 391 |
SCM_EXTERN void Scm_ClosePort(ScmPort *port); |
| 392 |
|
| 393 |
SCM_EXTERN ScmObj Scm_VMWithPortLocking(ScmPort *port, |
| 394 |
ScmObj closure); |
| 395 |
|
| 396 |
SCM_EXTERN ScmObj Scm_PortAttrGet(ScmPort *port, ScmObj key, |
| 397 |
ScmObj fallback); |
| 398 |
SCM_EXTERN ScmObj Scm_PortAttrSet(ScmPort *port, ScmObj key, ScmObj val); |
| 399 |
SCM_EXTERN ScmObj Scm_PortAttrDelete(ScmPort *port, ScmObj key); |
| 400 |
SCM_EXTERN ScmObj Scm_PortAttrs(ScmPort *port); |
| 401 |
|
| 402 |
SCM_EXTERN void Scm_Putb(ScmByte b, ScmPort *port); |
| 403 |
SCM_EXTERN void Scm_Putc(ScmChar c, ScmPort *port); |
| 404 |
SCM_EXTERN void Scm_Puts(ScmString *s, ScmPort *port); |
| 405 |
SCM_EXTERN void Scm_Putz(const char *s, ScmSize len, ScmPort *port); |
| 406 |
SCM_EXTERN void Scm_Flush(ScmPort *port); |
| 407 |
|
| 408 |
SCM_EXTERN void Scm_PutbUnsafe(ScmByte b, ScmPort *port); |
| 409 |
SCM_EXTERN void Scm_PutcUnsafe(ScmChar c, ScmPort *port); |
| 410 |
SCM_EXTERN void Scm_PutsUnsafe(ScmString *s, ScmPort *port); |
| 411 |
SCM_EXTERN void Scm_PutzUnsafe(const char *s, ScmSize len, ScmPort *port); |
| 412 |
SCM_EXTERN void Scm_FlushUnsafe(ScmPort *port); |
| 413 |
|
| 414 |
SCM_EXTERN void Scm_Ungetc(ScmChar ch, ScmPort *port); |
| 415 |
SCM_EXTERN void Scm_Ungetb(int b, ScmPort *port); |
| 416 |
SCM_EXTERN int Scm_Getb(ScmPort *port); |
| 417 |
SCM_EXTERN int Scm_Getc(ScmPort *port); |
| 418 |
SCM_EXTERN ScmSize Scm_Getz(char *buf, ScmSize buflen, ScmPort *port); |
| 419 |
SCM_EXTERN ScmChar Scm_Peekc(ScmPort *port); |
| 420 |
SCM_EXTERN int Scm_Peekb(ScmPort *port); |
| 421 |
SCM_EXTERN ScmObj Scm_UngottenChars(ScmPort *port); |
| 422 |
SCM_EXTERN ScmObj Scm_UngottenBytes(ScmPort *port); |
| 423 |
|
| 424 |
SCM_EXTERN void Scm_UngetcUnsafe(ScmChar ch, ScmPort *port); |
| 425 |
SCM_EXTERN void Scm_UngetbUnsafe(int b, ScmPort *port); |
| 426 |
SCM_EXTERN int Scm_GetbUnsafe(ScmPort *port); |
| 427 |
SCM_EXTERN int Scm_GetcUnsafe(ScmPort *port); |
| 428 |
SCM_EXTERN ScmSize Scm_GetzUnsafe(char *buf, ScmSize buflen, ScmPort *port); |
| 429 |
SCM_EXTERN ScmChar Scm_PeekcUnsafe(ScmPort *port); |
| 430 |
SCM_EXTERN int Scm_PeekbUnsafe(ScmPort *port); |
| 431 |
SCM_EXTERN ScmObj Scm_UngottenCharsUnsafe(ScmPort *port); |
| 432 |
SCM_EXTERN ScmObj Scm_UngottenBytesUnsafe(ScmPort *port); |
| 433 |
|
| 434 |
SCM_EXTERN ScmObj Scm_ReadLine(ScmPort *port); |
| 435 |
SCM_EXTERN ScmObj Scm_ReadLineUnsafe(ScmPort *port); |
| 436 |
|
| 437 |
/*================================================================ |
| 438 |
* File ports |
| 439 |
*/ |
| 440 |
|
| 441 |
SCM_EXTERN ScmObj Scm_OpenFilePort(const char *path, int flags, |
| 442 |
int buffering, int perm); |
| 443 |
|
| 444 |
SCM_EXTERN ScmObj Scm_Stdin(void); |
| 445 |
SCM_EXTERN ScmObj Scm_Stdout(void); |
| 446 |
SCM_EXTERN ScmObj Scm_Stderr(void); |
| 447 |
|
| 448 |
SCM_EXTERN ScmObj Scm_SetStdin(ScmPort *port); |
| 449 |
SCM_EXTERN ScmObj Scm_SetStdout(ScmPort *port); |
| 450 |
SCM_EXTERN ScmObj Scm_SetStderr(ScmPort *port); |
| 451 |
|
| 452 |
SCM_EXTERN ScmPort *Scm_CurrentInputPort(void); |
| 453 |
SCM_EXTERN ScmPort *Scm_CurrentOutputPort(void); |
| 454 |
SCM_EXTERN ScmPort *Scm_CurrentErrorPort(void); |
| 455 |
|
| 456 |
#define SCM_CURIN Scm_CurrentInputPort() |
| 457 |
#define SCM_CUROUT Scm_CurrentOutputPort() |
| 458 |
#define SCM_CURERR Scm_CurrentErrorPort() |
| 459 |
|
| 460 |
SCM_EXTERN ScmObj Scm_SetCurrentInputPort(ScmPort *port); |
| 461 |
SCM_EXTERN ScmObj Scm_SetCurrentOutputPort(ScmPort *port); |
| 462 |
SCM_EXTERN ScmObj Scm_SetCurrentErrorPort(ScmPort *port); |
| 463 |
|
| 464 |
/*================================================================ |
| 465 |
* String ports |
| 466 |
*/ |
| 467 |
|
| 468 |
enum { |
| 469 |
SCM_PORT_STRING_PRIVATE = 1 /* for flags arg */ |
| 470 |
}; |
| 471 |
|
| 472 |
SCM_EXTERN ScmObj Scm_MakeInputStringPortFull(ScmString *str, |
| 473 |
ScmObj name, |
| 474 |
u_long flags); |
| 475 |
SCM_EXTERN ScmObj Scm_MakeOutputStringPortFull(ScmObj name, |
| 476 |
u_long flags); |
| 477 |
|
| 478 |
/* these two are deprecated */ |
| 479 |
SCM_EXTERN ScmObj Scm_MakeInputStringPort(ScmString *str, int privatep); |
| 480 |
SCM_EXTERN ScmObj Scm_MakeOutputStringPort(int privatep); |
| 481 |
|
| 482 |
SCM_EXTERN ScmObj Scm_GetOutputString(ScmPort *port, int flags); |
| 483 |
SCM_EXTERN ScmObj Scm_GetOutputStringUnsafe(ScmPort *port, int flags); |
| 484 |
SCM_EXTERN ScmObj Scm_GetRemainingInputString(ScmPort *port, int flags); |
| 485 |
|
| 486 |
/*================================================================ |
| 487 |
* Other type of ports |
| 488 |
*/ |
| 489 |
|
| 490 |
SCM_EXTERN ScmObj Scm_MakeVirtualPortFull(ScmClass *klass, |
| 491 |
ScmObj name, int direction, |
| 492 |
const ScmPortVTable *vtable, |
| 493 |
u_long flags); |
| 494 |
|
| 495 |
/* deprecated */ |
| 496 |
SCM_EXTERN ScmObj Scm_MakeVirtualPort(ScmClass *klass, |
| 497 |
int direction, |
| 498 |
const ScmPortVTable *vtable); |
| 499 |
|
| 500 |
|
| 501 |
SCM_EXTERN ScmObj Scm_MakeBufferedPortFull(ScmClass *klass, |
| 502 |
ScmObj name, int direction, |
| 503 |
ScmPortBuffer *bufrec, |
| 504 |
u_long flags); |
| 505 |
|
| 506 |
/* deprecated */ |
| 507 |
SCM_EXTERN ScmObj Scm_MakeBufferedPort(ScmClass *klass, |
| 508 |
ScmObj name, int direction, |
| 509 |
int ownerp, |
| 510 |
ScmPortBuffer *bufrec); |
| 511 |
|
| 512 |
SCM_EXTERN ScmObj Scm_MakePortWithFd(ScmObj name, |
| 513 |
int direction, |
| 514 |
int fd, |
| 515 |
int bufmode, |
| 516 |
int ownerp); |
| 517 |
SCM_EXTERN ScmObj Scm_MakeCodingAwarePort(ScmPort *iport); |
| 518 |
|
| 519 |
#endif /*GAUCHE_PORT_H*/ |