| 1 |
/* |
| 2 |
* portapi.c - port common 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 |
/* This file is included _twice_ by port.c to define safe- and unsafe- |
| 35 |
* variant of port common APIs. It is to minimize the overhead of |
| 36 |
* locking operations. |
| 37 |
* |
| 38 |
* The macro SHORTCUT allows 'safe' version to bypass lock/unlock |
| 39 |
* stuff by calling 'unsafe' version when the port is already locked by |
| 40 |
* the calling thread. |
| 41 |
*/ |
| 42 |
|
| 43 |
/* [scratch and ungottern buffer] |
| 44 |
* It is always possible to mix binary and character i/o for Gauche's |
| 45 |
* ports. To support peek operations in Scheme and 'unget' operations |
| 46 |
* in C, we need to buffer at most one character or its equivalent |
| 47 |
* byte sequence. The 'ungotten' and 'scratch' fields are used for |
| 48 |
* character and binary buffering, respectively. |
| 49 |
* (This level of buffering is common to all input port types, and |
| 50 |
* distinct from the buffering of 'buffered' (file) port type.) |
| 51 |
* |
| 52 |
* 'Ungotten' field keeps SCM__CHAR_INVALID if there's no buffered |
| 53 |
* character. Otherwise, its value is the buffered character. |
| 54 |
* The number of bytes in the 'scratch' array is kept in 'scrcnt' |
| 55 |
* field. If 'scrcnt' field is not zero, there's data in the |
| 56 |
* 'scratch' array. |
| 57 |
* |
| 58 |
* In no cases there should be data in both ungotten and scratch |
| 59 |
* field. The consistency is taken care of the routines defined here; |
| 60 |
* no other routine should touch these buffering field. |
| 61 |
*/ |
| 62 |
|
| 63 |
#ifdef SAFE_PORT_OP |
| 64 |
#define VMDECL ScmVM *vm = Scm_VM() |
| 65 |
#define LOCK(p) PORT_LOCK(p, vm) |
| 66 |
#define UNLOCK(p) PORT_UNLOCK(p) |
| 67 |
#define SAFE_CALL(p, exp) PORT_SAFE_CALL(p, exp, /*no cleanup*/) |
| 68 |
#define SHORTCUT(p, unsafe) \ |
| 69 |
do { if (PORT_LOCKED(p, vm)) { unsafe; }} while (0) |
| 70 |
#else |
| 71 |
#define VMDECL /*none*/ |
| 72 |
#define LOCK(p) /*none*/ |
| 73 |
#define UNLOCK(p) /*none*/ |
| 74 |
#define SAFE_CALL(p, exp) (exp) |
| 75 |
#define SHORTCUT(p, unsafe) /* none */ |
| 76 |
#endif |
| 77 |
|
| 78 |
/* Convenience macro */ |
| 79 |
#ifndef CLOSE_CHECK |
| 80 |
#define CLOSE_CHECK(port) \ |
| 81 |
do { \ |
| 82 |
if (SCM_PORT_CLOSED_P(port)) { \ |
| 83 |
UNLOCK(p); \ |
| 84 |
Scm_PortError((port), SCM_PORT_ERROR_CLOSED, \ |
| 85 |
"I/O attempted on closed port: %S", (port)); \ |
| 86 |
} \ |
| 87 |
} while (0) |
| 88 |
#endif /* CLOSE_CHECK */ |
| 89 |
|
| 90 |
#ifndef UNSAVE_POS |
| 91 |
#define UNSAVE_POS(port) PORT_SAVED_POS(port) = SCM_UNBOUND |
| 92 |
#endif /* UNSAVE_POS */ |
| 93 |
|
| 94 |
|
| 95 |
/* In the walk pass of multi-pass writing (see write.c), we set |
| 96 |
SCM_PORT_WALKING flag of the port. Usually Scm_Write family recognizes |
| 97 |
the flag and suppress output. However, in case if low-level port API |
| 98 |
is directly called during the walk pass, we just check the flag again. |
| 99 |
*/ |
| 100 |
#ifndef WALKER_CHECK |
| 101 |
#define WALKER_CHECK(port) \ |
| 102 |
do { \ |
| 103 |
if (PORT_WALKER_P(port)) return; \ |
| 104 |
} while (0) |
| 105 |
#endif /* WALKER_CHECK */ |
| 106 |
|
| 107 |
/* A utility to compute the current position for Puts and Putz. |
| 108 |
We only need to count from the last newline. If the string |
| 109 |
doesn't contain a newline, returns -1. |
| 110 |
TODO: Should we consider character width? */ |
| 111 |
#ifndef COLUMN_COUNT |
| 112 |
#define COLUMN_COUNT |
| 113 |
static int length_count(const u_char *start, const u_char *end) |
| 114 |
{ |
| 115 |
int col = 0; |
| 116 |
while (start < end) { |
| 117 |
u_char b = *start; |
| 118 |
int i = SCM_CHAR_NFOLLOWS(b); |
| 119 |
if (i < 0) break; |
| 120 |
col++; |
| 121 |
start += i+1; |
| 122 |
} |
| 123 |
return col; |
| 124 |
} |
| 125 |
|
| 126 |
static int column_count(const u_char *start, const u_char *end) |
| 127 |
{ |
| 128 |
const u_char *p = end-1; |
| 129 |
for (; p >= start; p--) { |
| 130 |
if (*p == '\n') return length_count(p+1, end); |
| 131 |
} |
| 132 |
return -1; |
| 133 |
} |
| 134 |
#endif /*COLUMN_COUNT*/ |
| 135 |
|
| 136 |
/*================================================================= |
| 137 |
* Putb |
| 138 |
*/ |
| 139 |
|
| 140 |
#ifdef SAFE_PORT_OP |
| 141 |
void Scm_Putb(ScmByte b, ScmPort *p) |
| 142 |
#else |
| 143 |
void Scm_PutbUnsafe(ScmByte b, ScmPort *p) |
| 144 |
#endif |
| 145 |
{ |
| 146 |
VMDECL; |
| 147 |
SHORTCUT(p, Scm_PutbUnsafe(b, p); return); |
| 148 |
WALKER_CHECK(p); |
| 149 |
LOCK(p); |
| 150 |
CLOSE_CHECK(p); |
| 151 |
PORT_FLUSHED_CLEAR(p); |
| 152 |
|
| 153 |
switch (SCM_PORT_TYPE(p)) { |
| 154 |
case SCM_PORT_FILE: |
| 155 |
if (PORT_BUF(p)->current >= PORT_BUF(p)->end) { |
| 156 |
SAFE_CALL(p, bufport_flush(p, PORT_BUF(p)->current - PORT_BUF(p)->buffer, FALSE)); |
| 157 |
} |
| 158 |
SCM_ASSERT(PORT_BUF(p)->current < PORT_BUF(p)->end); |
| 159 |
*PORT_BUF(p)->current++ = b; |
| 160 |
if (PORT_BUFFER_MODE(p) == SCM_PORT_BUFFER_NONE) { |
| 161 |
SAFE_CALL(p, bufport_flush(p, 1, FALSE)); |
| 162 |
} |
| 163 |
UNLOCK(p); |
| 164 |
break; |
| 165 |
case SCM_PORT_OSTR: |
| 166 |
SCM_DSTRING_PUTB(PORT_OSTR(p), b); |
| 167 |
UNLOCK(p); |
| 168 |
break; |
| 169 |
case SCM_PORT_PROC: |
| 170 |
SAFE_CALL(p, PORT_VT(p)->Putb(b, p)); |
| 171 |
UNLOCK(p); |
| 172 |
break; |
| 173 |
default: |
| 174 |
UNLOCK(p); |
| 175 |
Scm_PortError(p, SCM_PORT_ERROR_OUTPUT, |
| 176 |
"bad port type for output: %S", p); |
| 177 |
} |
| 178 |
|
| 179 |
/* Binary output doesn't increment column count, but we can reset |
| 180 |
it when newline is put. */ |
| 181 |
if (b == '\n') PORT_COLUMN(p) = 0; |
| 182 |
} |
| 183 |
|
| 184 |
/*================================================================= |
| 185 |
* Putc |
| 186 |
*/ |
| 187 |
|
| 188 |
#ifdef SAFE_PORT_OP |
| 189 |
void Scm_Putc(ScmChar c, ScmPort *p) |
| 190 |
#else |
| 191 |
void Scm_PutcUnsafe(ScmChar c, ScmPort *p) |
| 192 |
#endif |
| 193 |
{ |
| 194 |
VMDECL; |
| 195 |
SHORTCUT(p, Scm_PutcUnsafe(c, p); return); |
| 196 |
WALKER_CHECK(p); |
| 197 |
LOCK(p); |
| 198 |
CLOSE_CHECK(p); |
| 199 |
PORT_FLUSHED_CLEAR(p); |
| 200 |
|
| 201 |
switch (SCM_PORT_TYPE(p)) { |
| 202 |
case SCM_PORT_FILE: { |
| 203 |
volatile int nb = SCM_CHAR_NBYTES(c); |
| 204 |
if (PORT_BUF(p)->current+nb > PORT_BUF(p)->end) { |
| 205 |
SAFE_CALL(p, bufport_flush(p, PORT_BUF(p)->current - PORT_BUF(p)->buffer, FALSE)); |
| 206 |
} |
| 207 |
SCM_ASSERT(PORT_BUF(p)->current+nb <= PORT_BUF(p)->end); |
| 208 |
SCM_CHAR_PUT(PORT_BUF(p)->current, c); |
| 209 |
PORT_BUF(p)->current += nb; |
| 210 |
if (PORT_BUFFER_MODE(p) == SCM_PORT_BUFFER_LINE) { |
| 211 |
if (c == '\n') { |
| 212 |
SAFE_CALL(p, bufport_flush(p, nb, FALSE)); |
| 213 |
} |
| 214 |
} else if (PORT_BUFFER_MODE(p) == SCM_PORT_BUFFER_NONE) { |
| 215 |
SAFE_CALL(p, bufport_flush(p, nb, FALSE)); |
| 216 |
} |
| 217 |
UNLOCK(p); |
| 218 |
break; |
| 219 |
} |
| 220 |
case SCM_PORT_OSTR: |
| 221 |
SCM_DSTRING_PUTC(PORT_OSTR(p), c); |
| 222 |
UNLOCK(p); |
| 223 |
break; |
| 224 |
case SCM_PORT_PROC: |
| 225 |
SAFE_CALL(p, PORT_VT(p)->Putc(c, p)); |
| 226 |
UNSAVE_POS(p); |
| 227 |
UNLOCK(p); |
| 228 |
break; |
| 229 |
default: |
| 230 |
UNLOCK(p); |
| 231 |
Scm_PortError(p, SCM_PORT_ERROR_OUTPUT, |
| 232 |
"bad port type for output: %S", p); |
| 233 |
} |
| 234 |
|
| 235 |
/* TODO: Should we consider character width? */ |
| 236 |
if (c == '\n') PORT_COLUMN(p) = 0; |
| 237 |
else PORT_COLUMN(p)++; |
| 238 |
} |
| 239 |
|
| 240 |
/*================================================================= |
| 241 |
* Puts |
| 242 |
*/ |
| 243 |
|
| 244 |
#ifdef SAFE_PORT_OP |
| 245 |
void Scm_Puts(ScmString *s, ScmPort *p) |
| 246 |
#else |
| 247 |
void Scm_PutsUnsafe(ScmString *s, ScmPort *p) |
| 248 |
#endif |
| 249 |
{ |
| 250 |
VMDECL; |
| 251 |
SHORTCUT(p, Scm_PutsUnsafe(s, p); return); |
| 252 |
WALKER_CHECK(p); |
| 253 |
LOCK(p); |
| 254 |
CLOSE_CHECK(p); |
| 255 |
PORT_FLUSHED_CLEAR(p); |
| 256 |
|
| 257 |
switch (SCM_PORT_TYPE(p)) { |
| 258 |
case SCM_PORT_FILE: { |
| 259 |
ScmSmallInt size; |
| 260 |
const char *ss = Scm_GetStringContent(s, &size, NULL, NULL); |
| 261 |
SAFE_CALL(p, bufport_write(p, ss, size)); |
| 262 |
if (PORT_BUFFER_MODE(p) == SCM_PORT_BUFFER_LINE) { |
| 263 |
const char *cp = PORT_BUF(p)->current; |
| 264 |
while (cp-- > PORT_BUF(p)->buffer) { |
| 265 |
if (*cp == '\n') { |
| 266 |
SAFE_CALL(p, bufport_flush(p, cp - PORT_BUF(p)->current, FALSE)); |
| 267 |
break; |
| 268 |
} |
| 269 |
} |
| 270 |
} else if (PORT_BUFFER_MODE(p) == SCM_PORT_BUFFER_NONE) { |
| 271 |
SAFE_CALL(p, bufport_flush(p, 0, TRUE)); |
| 272 |
} |
| 273 |
UNLOCK(p); |
| 274 |
break; |
| 275 |
} |
| 276 |
case SCM_PORT_OSTR: |
| 277 |
Scm_DStringAdd(PORT_OSTR(p), s); |
| 278 |
UNLOCK(p); |
| 279 |
break; |
| 280 |
case SCM_PORT_PROC: |
| 281 |
SAFE_CALL(p, PORT_VT(p)->Puts(s, p)); |
| 282 |
UNSAVE_POS(p); |
| 283 |
UNLOCK(p); |
| 284 |
break; |
| 285 |
default: |
| 286 |
UNLOCK(p); |
| 287 |
Scm_PortError(p, SCM_PORT_ERROR_OUTPUT, |
| 288 |
"bad port type for output: %S", p); |
| 289 |
} |
| 290 |
|
| 291 |
const ScmStringBody *sb = SCM_STRING_BODY(s); |
| 292 |
int col = column_count((const u_char*)SCM_STRING_BODY_START(sb), |
| 293 |
(const u_char*)SCM_STRING_BODY_END(sb)); |
| 294 |
if (col < 0) PORT_COLUMN(p) += SCM_STRING_BODY_LENGTH(sb); |
| 295 |
else PORT_COLUMN(p) = col; |
| 296 |
} |
| 297 |
|
| 298 |
/*================================================================= |
| 299 |
* Putz |
| 300 |
*/ |
| 301 |
|
| 302 |
#ifdef SAFE_PORT_OP |
| 303 |
void Scm_Putz(const char * volatile s, volatile ScmSize siz, ScmPort *p) |
| 304 |
#else |
| 305 |
void Scm_PutzUnsafe(const char *s, volatile ScmSize siz, ScmPort *p) |
| 306 |
#endif |
| 307 |
{ |
| 308 |
VMDECL; |
| 309 |
SHORTCUT(p, Scm_PutzUnsafe(s, siz, p); return); |
| 310 |
WALKER_CHECK(p); |
| 311 |
LOCK(p); |
| 312 |
CLOSE_CHECK(p); |
| 313 |
PORT_FLUSHED_CLEAR(p); |
| 314 |
if (siz < 0) siz = (ScmSize)strlen(s); |
| 315 |
switch (SCM_PORT_TYPE(p)) { |
| 316 |
case SCM_PORT_FILE: |
| 317 |
SAFE_CALL(p, bufport_write(p, s, siz)); |
| 318 |
if (PORT_BUFFER_MODE(p) == SCM_PORT_BUFFER_LINE) { |
| 319 |
const char *cp = PORT_BUF(p)->current; |
| 320 |
while (cp-- > PORT_BUF(p)->buffer) { |
| 321 |
if (*cp == '\n') { |
| 322 |
SAFE_CALL(p, bufport_flush(p, (cp - PORT_BUF(p)->current), FALSE)); |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
} else if (PORT_BUFFER_MODE(p) == SCM_PORT_BUFFER_NONE) { |
| 327 |
SAFE_CALL(p, bufport_flush(p, 0, TRUE)); |
| 328 |
} |
| 329 |
UNLOCK(p); |
| 330 |
break; |
| 331 |
case SCM_PORT_OSTR: |
| 332 |
Scm_DStringPutz(PORT_OSTR(p), s, siz); |
| 333 |
UNLOCK(p); |
| 334 |
break; |
| 335 |
case SCM_PORT_PROC: |
| 336 |
SAFE_CALL(p, PORT_VT(p)->Putz(s, siz, p)); |
| 337 |
UNSAVE_POS(p); |
| 338 |
UNLOCK(p); |
| 339 |
break; |
| 340 |
default: |
| 341 |
UNLOCK(p); |
| 342 |
Scm_PortError(p, SCM_PORT_ERROR_OUTPUT, |
| 343 |
"bad port type for output: %S", p); |
| 344 |
} |
| 345 |
|
| 346 |
{ |
| 347 |
const u_char *start = (const u_char*)s; |
| 348 |
const u_char *end = (const u_char*)(s + siz); |
| 349 |
int col = column_count(start, end); |
| 350 |
if (col < 0) PORT_COLUMN(p) += length_count(start, end); |
| 351 |
else PORT_COLUMN(p) = col; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/*================================================================= |
| 356 |
* Flush |
| 357 |
*/ |
| 358 |
|
| 359 |
#ifdef SAFE_PORT_OP |
| 360 |
void Scm_Flush(ScmPort *p) |
| 361 |
#else |
| 362 |
void Scm_FlushUnsafe(ScmPort *p) |
| 363 |
#endif |
| 364 |
{ |
| 365 |
VMDECL; |
| 366 |
SHORTCUT(p, Scm_FlushUnsafe(p); return); |
| 367 |
WALKER_CHECK(p); |
| 368 |
LOCK(p); |
| 369 |
if (PORT_FLUSHED_P(p)) { |
| 370 |
UNLOCK(p); |
| 371 |
return; |
| 372 |
} |
| 373 |
CLOSE_CHECK(p); |
| 374 |
switch (SCM_PORT_TYPE(p)) { |
| 375 |
case SCM_PORT_FILE: |
| 376 |
SAFE_CALL(p, bufport_flush(p, 0, TRUE)); |
| 377 |
UNLOCK(p); |
| 378 |
break; |
| 379 |
case SCM_PORT_OSTR: |
| 380 |
UNLOCK(p); |
| 381 |
break; |
| 382 |
case SCM_PORT_PROC: |
| 383 |
SAFE_CALL(p, PORT_VT(p)->Flush(p)); |
| 384 |
UNLOCK(p); |
| 385 |
break; |
| 386 |
default: |
| 387 |
UNLOCK(p); |
| 388 |
Scm_PortError(p, SCM_PORT_ERROR_OUTPUT, |
| 389 |
"bad port type for output: %S", p); |
| 390 |
} |
| 391 |
PORT_FLUSHED_SET(p); |
| 392 |
} |
| 393 |
|
| 394 |
/*================================================================= |
| 395 |
* Ungetc & PeekChar |
| 396 |
*/ |
| 397 |
|
| 398 |
#ifdef SAFE_PORT_OP |
| 399 |
void Scm_Ungetc(ScmChar c, ScmPort *p) |
| 400 |
#else |
| 401 |
void Scm_UngetcUnsafe(ScmChar c, ScmPort *p) |
| 402 |
#endif |
| 403 |
{ |
| 404 |
VMDECL; |
| 405 |
SHORTCUT(p, Scm_UngetcUnsafe(c, p); return); |
| 406 |
LOCK(p); |
| 407 |
if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID |
| 408 |
|| p->scrcnt != 0) { |
| 409 |
UNLOCK(p); |
| 410 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, |
| 411 |
"pushback buffer overflow on port %S", p); |
| 412 |
} |
| 413 |
UNSAVE_POS(p); |
| 414 |
PORT_UNGOTTEN(p) = c; |
| 415 |
UNLOCK(p); |
| 416 |
} |
| 417 |
|
| 418 |
#ifdef SAFE_PORT_OP |
| 419 |
ScmChar Scm_Peekc(ScmPort *p) |
| 420 |
#else |
| 421 |
ScmChar Scm_PeekcUnsafe(ScmPort *p) |
| 422 |
#endif |
| 423 |
{ |
| 424 |
VMDECL; |
| 425 |
SHORTCUT(p, return Scm_PeekcUnsafe(p)); |
| 426 |
LOCK(p); |
| 427 |
ScmChar ch = PORT_UNGOTTEN(p); |
| 428 |
if (ch == SCM_CHAR_INVALID) { |
| 429 |
volatile ScmObj saved_pos = SCM_UNBOUND; |
| 430 |
UNSAVE_POS(p); |
| 431 |
if (SCM_PORT_TYPE(p) == SCM_PORT_PROC |
| 432 |
&& Scm_PortPositionable(p, FALSE)) { |
| 433 |
SAFE_CALL(p, saved_pos = Scm_PortSeekUnsafe(p, SCM_MAKE_INT(0), SEEK_CUR)); |
| 434 |
} |
| 435 |
SAFE_CALL(p, ch = Scm_GetcUnsafe(p)); |
| 436 |
PORT_UNGOTTEN(p) = ch; |
| 437 |
if (!SCM_UNBOUNDP(saved_pos)) { |
| 438 |
PORT_SAVED_POS(p) = saved_pos; |
| 439 |
} |
| 440 |
} |
| 441 |
UNLOCK(p); |
| 442 |
return ch; |
| 443 |
} |
| 444 |
|
| 445 |
/* At this moment we only allow one character to be 'ungotten', |
| 446 |
but we might change it in future, so this one returns a list. */ |
| 447 |
#ifdef SAFE_PORT_OP |
| 448 |
ScmObj Scm_UngottenChars(ScmPort *p) |
| 449 |
#else |
| 450 |
ScmObj Scm_UngottenCharsUnsafe(ScmPort *p) |
| 451 |
#endif |
| 452 |
{ |
| 453 |
VMDECL; |
| 454 |
SHORTCUT(p, return Scm_UngottenCharsUnsafe(p)); |
| 455 |
LOCK(p); |
| 456 |
ScmChar ch = PORT_UNGOTTEN(p); |
| 457 |
UNLOCK(p); |
| 458 |
if (ch == SCM_CHAR_INVALID) { |
| 459 |
return SCM_NIL; |
| 460 |
} else { |
| 461 |
return SCM_LIST1(SCM_MAKE_CHAR(ch)); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
/*================================================================= |
| 466 |
* Ungetb & PeekByte |
| 467 |
*/ |
| 468 |
|
| 469 |
#ifdef SAFE_PORT_OP |
| 470 |
void Scm_Ungetb(int b, ScmPort *p) |
| 471 |
#else |
| 472 |
void Scm_UngetbUnsafe(int b, ScmPort *p) |
| 473 |
#endif |
| 474 |
{ |
| 475 |
VMDECL; |
| 476 |
SHORTCUT(p, Scm_UngetbUnsafe(b, p); return); |
| 477 |
LOCK(p); |
| 478 |
if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID |
| 479 |
|| p->scrcnt >= SCM_CHAR_MAX_BYTES) { |
| 480 |
UNLOCK(p); |
| 481 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, |
| 482 |
"pushback buffer overflow on port %S", p); |
| 483 |
} |
| 484 |
PORT_SCRATCH(p)[p->scrcnt++] = b; |
| 485 |
UNLOCK(p); |
| 486 |
} |
| 487 |
|
| 488 |
#ifdef SAFE_PORT_OP |
| 489 |
int Scm_Peekb(ScmPort *p) |
| 490 |
#else |
| 491 |
int Scm_PeekbUnsafe(ScmPort *p) |
| 492 |
#endif |
| 493 |
{ |
| 494 |
int b; |
| 495 |
VMDECL; |
| 496 |
SHORTCUT(p, return Scm_PeekbUnsafe(p)); |
| 497 |
LOCK(p); |
| 498 |
flush_linked_port(p); |
| 499 |
if (p->scrcnt > 0) { |
| 500 |
b = (unsigned char)PORT_SCRATCH(p)[0]; |
| 501 |
} else { |
| 502 |
SCM_GETB(b, p); |
| 503 |
if (b == EOF) { |
| 504 |
PORT_UNGOTTEN(p) = EOF; |
| 505 |
} else if (b >= 0) { |
| 506 |
if (p->scrcnt > 0) { |
| 507 |
/* unshift scratch buffer */ |
| 508 |
SCM_ASSERT(p->scrcnt < SCM_CHAR_MAX_BYTES); |
| 509 |
for (int i=p->scrcnt; i>0; i--) { |
| 510 |
PORT_SCRATCH(p)[i] = PORT_SCRATCH(p)[i-1]; |
| 511 |
} |
| 512 |
PORT_SCRATCH(p)[0] = b; |
| 513 |
p->scrcnt++; |
| 514 |
} else { |
| 515 |
PORT_SCRATCH(p)[0] = b; |
| 516 |
p->scrcnt = 1; |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
UNLOCK(p); |
| 521 |
return b; |
| 522 |
} |
| 523 |
|
| 524 |
#ifdef SAFE_PORT_OP |
| 525 |
ScmObj Scm_UngottenBytes(ScmPort *p) |
| 526 |
#else |
| 527 |
ScmObj Scm_UngottenBytesUnsafe(ScmPort *p) |
| 528 |
#endif |
| 529 |
{ |
| 530 |
VMDECL; |
| 531 |
SHORTCUT(p, return Scm_UngottenBytesUnsafe(p)); |
| 532 |
char buf[SCM_CHAR_MAX_BYTES]; |
| 533 |
LOCK(p); |
| 534 |
for (int i=0; i<p->scrcnt; i++) buf[i] = PORT_SCRATCH(p)[i]; |
| 535 |
int n = p->scrcnt; |
| 536 |
UNLOCK(p); |
| 537 |
ScmObj h = SCM_NIL, t = SCM_NIL; |
| 538 |
for (int i=0; i<n; i++) { |
| 539 |
SCM_APPEND1(h, t, SCM_MAKE_INT((unsigned char)buf[i])); |
| 540 |
} |
| 541 |
return h; |
| 542 |
} |
| 543 |
|
| 544 |
/*================================================================= |
| 545 |
* Getb |
| 546 |
*/ |
| 547 |
|
| 548 |
#ifndef SHIFT_SCRATCH /* we need to define this only once */ |
| 549 |
#define SHIFT_SCRATCH |
| 550 |
|
| 551 |
/* shift scratch buffer content */ |
| 552 |
static inline void shift_scratch(ScmPort *p, int off) |
| 553 |
{ |
| 554 |
for (u_int i=0; i<p->scrcnt; i++) { |
| 555 |
PORT_SCRATCH(p)[i] = PORT_SCRATCH(p)[i+off]; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
/* handle the case that there's remaining data in the scratch buffer */ |
| 560 |
static int getb_scratch(ScmPort *p) |
| 561 |
{ |
| 562 |
int b = (unsigned char)PORT_SCRATCH(p)[0]; |
| 563 |
p->scrcnt--; |
| 564 |
shift_scratch(p, 1); |
| 565 |
return b; |
| 566 |
} |
| 567 |
|
| 568 |
/* handle the case that there's an ungotten char */ |
| 569 |
static int getb_ungotten(ScmPort *p) |
| 570 |
{ |
| 571 |
SCM_CHAR_PUT(PORT_SCRATCH(p), PORT_UNGOTTEN(p)); |
| 572 |
p->scrcnt = SCM_CHAR_NBYTES(PORT_UNGOTTEN(p)); |
| 573 |
PORT_UNGOTTEN(p) = SCM_CHAR_INVALID; |
| 574 |
return getb_scratch(p); |
| 575 |
} |
| 576 |
#endif /*SHIFT_SCRATCH*/ |
| 577 |
|
| 578 |
/* Getb body */ |
| 579 |
#ifdef SAFE_PORT_OP |
| 580 |
int Scm_Getb(ScmPort *p) |
| 581 |
#else |
| 582 |
int Scm_GetbUnsafe(ScmPort *p) |
| 583 |
#endif |
| 584 |
{ |
| 585 |
int b = 0; |
| 586 |
VMDECL; |
| 587 |
SHORTCUT(p, return Scm_GetbUnsafe(p)); |
| 588 |
LOCK(p); |
| 589 |
CLOSE_CHECK(p); |
| 590 |
flush_linked_port(p); |
| 591 |
|
| 592 |
/* check if there's "pushed back" stuff */ |
| 593 |
if (p->scrcnt) { |
| 594 |
b = getb_scratch(p); |
| 595 |
} else if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID) { |
| 596 |
if (PORT_UNGOTTEN(p) == EOF) { |
| 597 |
PORT_UNGOTTEN(p) = SCM_CHAR_INVALID; |
| 598 |
UNLOCK(p); |
| 599 |
return EOF; |
| 600 |
} |
| 601 |
b = getb_ungotten(p); |
| 602 |
} else { |
| 603 |
switch (SCM_PORT_TYPE(p)) { |
| 604 |
case SCM_PORT_FILE: |
| 605 |
if (PORT_BUF(p)->current >= PORT_BUF(p)->end) { |
| 606 |
ScmSize r = 0; |
| 607 |
SAFE_CALL(p, r = bufport_fill(p, 1, FALSE)); |
| 608 |
if (r == 0) { |
| 609 |
UNLOCK(p); |
| 610 |
return EOF; |
| 611 |
} |
| 612 |
} |
| 613 |
b = (unsigned char)*PORT_BUF(p)->current++; |
| 614 |
break; |
| 615 |
case SCM_PORT_ISTR: |
| 616 |
if (PORT_ISTR(p)->current >= PORT_ISTR(p)->end) b = EOF; |
| 617 |
else b = (unsigned char)*PORT_ISTR(p)->current++; |
| 618 |
break; |
| 619 |
case SCM_PORT_PROC: |
| 620 |
SAFE_CALL(p, b = PORT_VT(p)->Getb(p)); |
| 621 |
break; |
| 622 |
default: |
| 623 |
UNLOCK(p); |
| 624 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, |
| 625 |
"bad port type for input: %S", p); |
| 626 |
} |
| 627 |
PORT_BYTES(p)++; |
| 628 |
/* we may mix binary/textual input, so we keep lines updated too. */ |
| 629 |
if (b == '\n') { |
| 630 |
PORT_LINE(p)++; |
| 631 |
reset_linked_column(p); |
| 632 |
} |
| 633 |
} |
| 634 |
UNLOCK(p); |
| 635 |
return b; |
| 636 |
} |
| 637 |
|
| 638 |
/*================================================================= |
| 639 |
* Getc |
| 640 |
*/ |
| 641 |
|
| 642 |
/* handle the case that there's data in scratch area */ |
| 643 |
#ifdef SAFE_PORT_OP |
| 644 |
#define GETC_SCRATCH getc_scratch |
| 645 |
static int getc_scratch(ScmPort *p) |
| 646 |
#else |
| 647 |
#define GETC_SCRATCH getc_scratch_unsafe |
| 648 |
static int getc_scratch_unsafe(ScmPort *p) |
| 649 |
#endif |
| 650 |
{ |
| 651 |
char tbuf[SCM_CHAR_MAX_BYTES]; |
| 652 |
int nb = SCM_CHAR_NFOLLOWS(PORT_SCRATCH(p)[0]); |
| 653 |
int curr = p->scrcnt; |
| 654 |
|
| 655 |
memcpy(tbuf, PORT_SCRATCH(p), curr); |
| 656 |
p->scrcnt = 0; |
| 657 |
for (volatile int i=curr; i<=nb; i++) { |
| 658 |
int r = EOF; |
| 659 |
SAFE_CALL(p, r = Scm_Getb(p)); |
| 660 |
if (r == EOF) { |
| 661 |
UNLOCK(p); |
| 662 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, |
| 663 |
"encountered EOF in middle of a multibyte character from port %S", p); |
| 664 |
} |
| 665 |
tbuf[i] = (char)r; |
| 666 |
} |
| 667 |
int ch; |
| 668 |
SCM_CHAR_GET(tbuf, ch); |
| 669 |
if (ch == SCM_CHAR_INVALID) { |
| 670 |
/* This can happen if the input contains invalid byte sequence. |
| 671 |
We return the stray byte (which would eventually result |
| 672 |
an incomplete string when accumulated), while keeping the |
| 673 |
remaining bytes in the scrach buffer. */ |
| 674 |
ch = (ScmChar)(tbuf[0] & 0xff); |
| 675 |
memcpy(PORT_SCRATCH(p), tbuf+1, nb); |
| 676 |
p->scrcnt = nb; |
| 677 |
} |
| 678 |
return ch; |
| 679 |
} |
| 680 |
|
| 681 |
/* Getc body */ |
| 682 |
#ifdef SAFE_PORT_OP |
| 683 |
int Scm_Getc(ScmPort *p) |
| 684 |
#else |
| 685 |
int Scm_GetcUnsafe(ScmPort *p) |
| 686 |
#endif |
| 687 |
{ |
| 688 |
VMDECL; |
| 689 |
SHORTCUT(p, return Scm_GetcUnsafe(p)); |
| 690 |
LOCK(p); |
| 691 |
CLOSE_CHECK(p); |
| 692 |
flush_linked_port(p); |
| 693 |
if (p->scrcnt > 0) { |
| 694 |
int r = GETC_SCRATCH(p); |
| 695 |
UNLOCK(p); |
| 696 |
return r; |
| 697 |
} |
| 698 |
if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID) { |
| 699 |
int c = PORT_UNGOTTEN(p); |
| 700 |
PORT_UNGOTTEN(p) = SCM_CHAR_INVALID; |
| 701 |
UNLOCK(p); |
| 702 |
return c; |
| 703 |
} |
| 704 |
|
| 705 |
switch (SCM_PORT_TYPE(p)) { |
| 706 |
case SCM_PORT_FILE: { |
| 707 |
int c = 0; |
| 708 |
if (PORT_BUF(p)->current >= PORT_BUF(p)->end) { |
| 709 |
ScmSize r = 0; |
| 710 |
SAFE_CALL(p, r = bufport_fill(p, 1, FALSE)); |
| 711 |
if (r == 0) { |
| 712 |
UNLOCK(p); |
| 713 |
return EOF; |
| 714 |
} |
| 715 |
} |
| 716 |
int first = (unsigned char)*PORT_BUF(p)->current++; |
| 717 |
int nb = SCM_CHAR_NFOLLOWS(first); |
| 718 |
PORT_BYTES(p)++; |
| 719 |
if (nb > 0) { |
| 720 |
if (PORT_BUF(p)->current + nb > PORT_BUF(p)->end) { |
| 721 |
/* The buffer doesn't have enough bytes to consist a char. |
| 722 |
move the incomplete char to the scratch buffer and try |
| 723 |
to fetch the rest of the char. */ |
| 724 |
volatile int rest; |
| 725 |
volatile ScmSize filled = 0; |
| 726 |
p->scrcnt = (unsigned char)(PORT_BUF(p)->end - PORT_BUF(p)->current + 1); |
| 727 |
memcpy(PORT_SCRATCH(p), PORT_BUF(p)->current-1, p->scrcnt); |
| 728 |
PORT_BUF(p)->current = PORT_BUF(p)->end; |
| 729 |
rest = nb + 1 - p->scrcnt; |
| 730 |
for (;;) { |
| 731 |
SAFE_CALL(p, filled = bufport_fill(p, rest, FALSE)); |
| 732 |
if (filled <= 0) { |
| 733 |
/* TODO: make this behavior customizable */ |
| 734 |
UNLOCK(p); |
| 735 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, |
| 736 |
"encountered EOF in middle of a multibyte character from port %S", p); |
| 737 |
} |
| 738 |
if (filled >= rest) { |
| 739 |
memcpy(PORT_SCRATCH(p)+p->scrcnt, PORT_BUF(p)->current, rest); |
| 740 |
p->scrcnt += rest; |
| 741 |
PORT_BUF(p)->current += rest; |
| 742 |
break; |
| 743 |
} else { |
| 744 |
memcpy(PORT_SCRATCH(p)+p->scrcnt, PORT_BUF(p)->current, filled); |
| 745 |
p->scrcnt += filled; |
| 746 |
PORT_BUF(p)->current = PORT_BUF(p)->end; |
| 747 |
rest -= filled; |
| 748 |
} |
| 749 |
} |
| 750 |
SCM_CHAR_GET(PORT_SCRATCH(p), c); |
| 751 |
p->scrcnt = 0; |
| 752 |
} else { |
| 753 |
SCM_CHAR_GET(PORT_BUF(p)->current-1, c); |
| 754 |
PORT_BUF(p)->current += nb; |
| 755 |
} |
| 756 |
PORT_BYTES(p) += nb; |
| 757 |
} else { |
| 758 |
c = first; |
| 759 |
if (c == '\n') { |
| 760 |
PORT_LINE(p)++; |
| 761 |
reset_linked_column(p); |
| 762 |
} |
| 763 |
} |
| 764 |
UNLOCK(p); |
| 765 |
return c; |
| 766 |
} |
| 767 |
case SCM_PORT_ISTR: { |
| 768 |
if (PORT_ISTR(p)->current >= PORT_ISTR(p)->end) { |
| 769 |
UNLOCK(p); |
| 770 |
return EOF; |
| 771 |
} |
| 772 |
int c = 0; |
| 773 |
int first = (unsigned char)*PORT_ISTR(p)->current++; |
| 774 |
int nb = SCM_CHAR_NFOLLOWS(first); |
| 775 |
PORT_BYTES(p)++; |
| 776 |
if (nb > 0) { |
| 777 |
if (PORT_ISTR(p)->current + nb > PORT_ISTR(p)->end) { |
| 778 |
/* TODO: make this behavior customizable */ |
| 779 |
UNLOCK(p); |
| 780 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, |
| 781 |
"encountered EOF in middle of a multibyte character from port %S", p); |
| 782 |
} |
| 783 |
SCM_CHAR_GET(PORT_ISTR(p)->current-1, c); |
| 784 |
PORT_ISTR(p)->current += nb; |
| 785 |
PORT_BYTES(p) += nb; |
| 786 |
} else { |
| 787 |
c = first; |
| 788 |
if (c == '\n') { |
| 789 |
PORT_LINE(p)++; |
| 790 |
reset_linked_column(p); |
| 791 |
} |
| 792 |
} |
| 793 |
UNLOCK(p); |
| 794 |
return c; |
| 795 |
} |
| 796 |
case SCM_PORT_PROC: { |
| 797 |
int c = 0; |
| 798 |
UNSAVE_POS(p); |
| 799 |
SAFE_CALL(p, c = PORT_VT(p)->Getc(p)); |
| 800 |
if (c == '\n') { |
| 801 |
PORT_LINE(p)++; |
| 802 |
reset_linked_column(p); |
| 803 |
} |
| 804 |
UNLOCK(p); |
| 805 |
return c; |
| 806 |
} |
| 807 |
default: |
| 808 |
UNLOCK(p); |
| 809 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, "bad port type for input: %S", p); |
| 810 |
} |
| 811 |
return 0;/*dummy*/ |
| 812 |
} |
| 813 |
|
| 814 |
#undef GETC_SCRATCH |
| 815 |
|
| 816 |
/*================================================================= |
| 817 |
* Getz - block read. |
| 818 |
* If the buffering mode is BUFFER_FULL, this reads BUFLEN bytes |
| 819 |
* unless it reaches EOF. Otherwise, this reads less than BUFLEN |
| 820 |
* if the data is not immediately available. |
| 821 |
*/ |
| 822 |
|
| 823 |
#ifdef SAFE_PORT_OP |
| 824 |
#define GETZ_SCRATCH getz_scratch |
| 825 |
static ScmSize getz_scratch(char *buf, ScmSize buflen, ScmPort *p) |
| 826 |
#else |
| 827 |
#define GETZ_SCRATCH getz_scratch_unsafe |
| 828 |
static ScmSize getz_scratch_unsafe(char *buf, ScmSize buflen, ScmPort *p) |
| 829 |
#endif |
| 830 |
{ |
| 831 |
if (p->scrcnt >= (size_t)buflen) { |
| 832 |
memcpy(buf, PORT_SCRATCH(p), buflen); |
| 833 |
p->scrcnt -= buflen; |
| 834 |
shift_scratch(p, buflen); |
| 835 |
return buflen; |
| 836 |
} else { |
| 837 |
memcpy(buf, PORT_SCRATCH(p), p->scrcnt); |
| 838 |
ScmSize i = p->scrcnt; |
| 839 |
p->scrcnt = 0; |
| 840 |
ScmSize n = 0; |
| 841 |
SAFE_CALL(p, n = Scm_Getz(buf+i, buflen-i, p)); |
| 842 |
return i + n; |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
#ifndef GETZ_ISTR /* common part */ |
| 847 |
#define GETZ_ISTR getz_istr |
| 848 |
static ScmSize getz_istr(ScmPort *p, char *buf, ScmSize buflen) |
| 849 |
{ |
| 850 |
if (PORT_ISTR(p)->current + buflen >= PORT_ISTR(p)->end) { |
| 851 |
if (PORT_ISTR(p)->current >= PORT_ISTR(p)->end) return EOF; |
| 852 |
ScmSize siz = PORT_ISTR(p)->end - PORT_ISTR(p)->current; |
| 853 |
memcpy(buf, PORT_ISTR(p)->current, siz); |
| 854 |
PORT_ISTR(p)->current = PORT_ISTR(p)->end; |
| 855 |
return siz; |
| 856 |
} else { |
| 857 |
memcpy(buf, PORT_ISTR(p)->current, buflen); |
| 858 |
PORT_ISTR(p)->current += buflen; |
| 859 |
return buflen; |
| 860 |
} |
| 861 |
} |
| 862 |
#endif /*!GETZ_ISTR*/ |
| 863 |
|
| 864 |
#ifdef SAFE_PORT_OP |
| 865 |
ScmSize Scm_Getz(char *buf, ScmSize buflen, ScmPort *p) |
| 866 |
#else |
| 867 |
ScmSize Scm_GetzUnsafe(char *buf, ScmSize buflen, ScmPort *p) |
| 868 |
#endif |
| 869 |
{ |
| 870 |
VMDECL; |
| 871 |
SHORTCUT(p, return Scm_GetzUnsafe(buf, buflen, p)); |
| 872 |
LOCK(p); |
| 873 |
CLOSE_CHECK(p); |
| 874 |
flush_linked_port(p); |
| 875 |
|
| 876 |
if (p->scrcnt) { |
| 877 |
ScmSize r = GETZ_SCRATCH(buf, buflen, p); |
| 878 |
UNLOCK(p); |
| 879 |
return r; |
| 880 |
} |
| 881 |
if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID) { |
| 882 |
p->scrcnt = SCM_CHAR_NBYTES(PORT_UNGOTTEN(p)); |
| 883 |
SCM_CHAR_PUT(PORT_SCRATCH(p), PORT_UNGOTTEN(p)); |
| 884 |
PORT_UNGOTTEN(p) = SCM_CHAR_INVALID; |
| 885 |
ScmSize r = GETZ_SCRATCH(buf, buflen, p); |
| 886 |
UNLOCK(p); |
| 887 |
return r; |
| 888 |
} |
| 889 |
|
| 890 |
switch (SCM_PORT_TYPE(p)) { |
| 891 |
case SCM_PORT_FILE: { |
| 892 |
ScmSize siz = 0; |
| 893 |
SAFE_CALL(p, siz = bufport_read(p, buf, buflen)); |
| 894 |
PORT_BYTES(p) += siz; |
| 895 |
UNLOCK(p); |
| 896 |
if (siz == 0 && buflen != 0) return EOF; |
| 897 |
else return siz; |
| 898 |
} |
| 899 |
case SCM_PORT_ISTR: { |
| 900 |
ScmSize r = GETZ_ISTR(p, buf, buflen); |
| 901 |
PORT_BYTES(p) += r; |
| 902 |
UNLOCK(p); |
| 903 |
return r; |
| 904 |
} |
| 905 |
case SCM_PORT_PROC: { |
| 906 |
ScmSize r = 0; |
| 907 |
UNSAVE_POS(p); |
| 908 |
SAFE_CALL(p, r = PORT_VT(p)->Getz(buf, buflen, p)); |
| 909 |
PORT_BYTES(p) += r; |
| 910 |
UNLOCK(p); |
| 911 |
return r; |
| 912 |
} |
| 913 |
default: |
| 914 |
UNLOCK(p); |
| 915 |
Scm_PortError(p, SCM_PORT_ERROR_INPUT, "bad port type for input: %S", p); |
| 916 |
} |
| 917 |
return -1; /* dummy */ |
| 918 |
} |
| 919 |
|
| 920 |
#undef GETZ_SCRATCH |
| 921 |
|
| 922 |
/*================================================================= |
| 923 |
* ReadLine |
| 924 |
* Reads up to EOL or EOF. |
| 925 |
*/ |
| 926 |
|
| 927 |
/* Auxiliary procedures */ |
| 928 |
|
| 929 |
/* NB: it may be further optimized by scanning the contents of buffer |
| 930 |
when the port is a buffered port or an input string, which allows |
| 931 |
us to avoid mb->wc->mb conversion. See port.c, v 1.69 for some |
| 932 |
attempt to do so. The problem there is that if I have to take |
| 933 |
into account the cases of the ungotten char and the scratch buffer, |
| 934 |
code becomes ugly. There might be some better approach. */ |
| 935 |
|
| 936 |
#ifndef READLINE_AUX |
| 937 |
#define READLINE_AUX |
| 938 |
/* Assumes the port is locked, and the caller takes care of unlocking |
| 939 |
even if an error is signalled within this body */ |
| 940 |
/* NB: this routine reads bytes, not chars. It allows to readline |
| 941 |
from a port in unknown character encoding (e.g. reading the first |
| 942 |
line of xml doc to find out charset parameter). */ |
| 943 |
ScmObj readline_body(ScmPort *p) |
| 944 |
{ |
| 945 |
ScmDString ds; |
| 946 |
|
| 947 |
Scm_DStringInit(&ds); |
| 948 |
int b1 = Scm_GetbUnsafe(p); |
| 949 |
if (b1 == EOF) return SCM_EOF; |
| 950 |
for (;;) { |
| 951 |
if (b1 == EOF) return Scm_DStringGet(&ds, 0); |
| 952 |
if (b1 == '\n') break; |
| 953 |
if (b1 == '\r') { |
| 954 |
int b2 = Scm_GetbUnsafe(p); |
| 955 |
if (b2 == EOF || b2 == '\n') break; |
| 956 |
Scm_UngetbUnsafe(b2, p); |
| 957 |
break; |
| 958 |
} |
| 959 |
SCM_DSTRING_PUTB(&ds, b1); |
| 960 |
b1 = Scm_GetbUnsafe(p); |
| 961 |
} |
| 962 |
PORT_LINE(p)++; |
| 963 |
reset_linked_column(p); |
| 964 |
return Scm_DStringGet(&ds, 0); |
| 965 |
} |
| 966 |
#endif /* READLINE_AUX */ |
| 967 |
|
| 968 |
#ifdef SAFE_PORT_OP |
| 969 |
ScmObj Scm_ReadLine(ScmPort *p) |
| 970 |
#else |
| 971 |
ScmObj Scm_ReadLineUnsafe(ScmPort *p) |
| 972 |
#endif |
| 973 |
{ |
| 974 |
ScmObj r = SCM_UNDEFINED; |
| 975 |
VMDECL; |
| 976 |
SHORTCUT(p, return Scm_ReadLineUnsafe(p)); |
| 977 |
|
| 978 |
LOCK(p); |
| 979 |
flush_linked_port(p); |
| 980 |
SAFE_CALL(p, r = readline_body(p)); |
| 981 |
UNLOCK(p); |
| 982 |
return r; |
| 983 |
} |
| 984 |
|
| 985 |
/*================================================================= |
| 986 |
* ByteReady |
| 987 |
*/ |
| 988 |
|
| 989 |
#ifdef SAFE_PORT_OP |
| 990 |
int Scm_ByteReady(ScmPort *p) |
| 991 |
#else |
| 992 |
int Scm_ByteReadyUnsafe(ScmPort *p) |
| 993 |
#endif |
| 994 |
{ |
| 995 |
int r = 0; |
| 996 |
VMDECL; |
| 997 |
SHORTCUT(p, return Scm_ByteReadyUnsafe(p)); |
| 998 |
if (!SCM_IPORTP(p)) Scm_Error("input port required, but got %S", p); |
| 999 |
LOCK(p); |
| 1000 |
if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID |
| 1001 |
|| p->scrcnt > 0) { |
| 1002 |
r = TRUE; |
| 1003 |
} else { |
| 1004 |
switch (SCM_PORT_TYPE(p)) { |
| 1005 |
case SCM_PORT_FILE: |
| 1006 |
if (PORT_BUF(p)->current < PORT_BUF(p)->end) r = TRUE; |
| 1007 |
else if (PORT_BUF(p)->ready == NULL) r = TRUE; |
| 1008 |
else { |
| 1009 |
SAFE_CALL(p, r = (PORT_BUF(p)->ready(p) != SCM_FD_WOULDBLOCK)); |
| 1010 |
} |
| 1011 |
break; |
| 1012 |
case SCM_PORT_PROC: |
| 1013 |
SAFE_CALL(p, r = PORT_VT(p)->Ready(p, FALSE)); |
| 1014 |
break; |
| 1015 |
default: |
| 1016 |
r = TRUE; |
| 1017 |
} |
| 1018 |
} |
| 1019 |
UNLOCK(p); |
| 1020 |
return r; |
| 1021 |
} |
| 1022 |
|
| 1023 |
/*================================================================= |
| 1024 |
* CharReady |
| 1025 |
*/ |
| 1026 |
|
| 1027 |
#ifdef SAFE_PORT_OP |
| 1028 |
int Scm_CharReady(ScmPort *p) |
| 1029 |
#else |
| 1030 |
int Scm_CharReadyUnsafe(ScmPort *p) |
| 1031 |
#endif |
| 1032 |
{ |
| 1033 |
int r = 0; |
| 1034 |
VMDECL; |
| 1035 |
SHORTCUT(p, return Scm_CharReadyUnsafe(p)); |
| 1036 |
if (!SCM_IPORTP(p)) Scm_Error("input port required, but got %S", p); |
| 1037 |
LOCK(p); |
| 1038 |
if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID) r = TRUE; |
| 1039 |
else { |
| 1040 |
switch (SCM_PORT_TYPE(p)) { |
| 1041 |
case SCM_PORT_FILE: |
| 1042 |
if (PORT_BUF(p)->current < PORT_BUF(p)->end) r = TRUE; |
| 1043 |
else if (PORT_BUF(p)->ready == NULL) r = TRUE; |
| 1044 |
else { |
| 1045 |
SAFE_CALL(p, r = (PORT_BUF(p)->ready(p) != SCM_FD_WOULDBLOCK)); |
| 1046 |
} |
| 1047 |
break; |
| 1048 |
case SCM_PORT_PROC: |
| 1049 |
SAFE_CALL(p, r = PORT_VT(p)->Ready(p, TRUE)); |
| 1050 |
break; |
| 1051 |
default: |
| 1052 |
r = TRUE; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
UNLOCK(p); |
| 1056 |
return r; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/*================================================================= |
| 1060 |
* Port Positioning |
| 1061 |
*/ |
| 1062 |
|
| 1063 |
/* See the comment in port.h (Port Positioning Interface) */ |
| 1064 |
|
| 1065 |
/* For the sake of seek/tell, we treat scratch buffer and ungotten char |
| 1066 |
(collectively we call them pending bytes here) as if it's a cache--- |
| 1067 |
that is, we assume their content always mirrors the real content of |
| 1068 |
the underlying data. |
| 1069 |
|
| 1070 |
If port_pending_bytes() > 0, the "external" current position visible |
| 1071 |
from outside, and the "internal" current position that points into |
| 1072 |
the underlying data, differ. We discard the pending bytes and adjust |
| 1073 |
the target offset, if the target offset is relative to the current |
| 1074 |
position (SEEK_CUR). |
| 1075 |
|
| 1076 |
One optimization, though; if whence == SEEK_CUR and offset = 0, we don't |
| 1077 |
need to move anything. It's just to get the current offset. For this |
| 1078 |
case, instead of adjusting offset, we adjust the result. |
| 1079 |
*/ |
| 1080 |
|
| 1081 |
#ifndef PORT_PENDING_BYTES /* common part */ |
| 1082 |
#define PORT_PENDING_BYTES port_pending_bytes |
| 1083 |
static off_t port_pending_bytes(ScmPort *p) |
| 1084 |
{ |
| 1085 |
off_t unread_bytes = p->scrcnt; |
| 1086 |
if (PORT_UNGOTTEN(p) != SCM_CHAR_INVALID) { |
| 1087 |
unread_bytes += SCM_CHAR_NBYTES(PORT_UNGOTTEN(p)); |
| 1088 |
} |
| 1089 |
return unread_bytes; |
| 1090 |
} |
| 1091 |
#endif /*PORT_PENDING_BYTES*/ |
| 1092 |
|
| 1093 |
#ifndef SEEK_ISTR /* common part */ |
| 1094 |
#define SEEK_ISTR seek_istr |
| 1095 |
static ScmObj seek_istr(ScmPort *p, ScmObj off, int whence) |
| 1096 |
{ |
| 1097 |
/* If the port is istr, offset must always be an integer. */ |
| 1098 |
off_t o = Scm_IntegerToOffset(off); |
| 1099 |
off_t rr; |
| 1100 |
if (whence == SEEK_CUR) { |
| 1101 |
o += (off_t)(PORT_ISTR(p)->current - PORT_ISTR(p)->start); |
| 1102 |
} else if (whence == SEEK_END) { |
| 1103 |
o += (off_t)(PORT_ISTR(p)->end - PORT_ISTR(p)->start); |
| 1104 |
} |
| 1105 |
if (o < 0 || o > (off_t)(PORT_ISTR(p)->end - PORT_ISTR(p)->start)) { |
| 1106 |
rr = (off_t)-1; |
| 1107 |
} else { |
| 1108 |
PORT_ISTR(p)->current = PORT_ISTR(p)->start + o; |
| 1109 |
rr = (off_t)(PORT_ISTR(p)->current - PORT_ISTR(p)->start); |
| 1110 |
} |
| 1111 |
PORT_UNGOTTEN(p) = SCM_CHAR_INVALID; |
| 1112 |
p->scrcnt = 0; |
| 1113 |
if (rr == (off_t)-1) return SCM_FALSE; |
| 1114 |
return Scm_OffsetToInteger(rr); |
| 1115 |
} |
| 1116 |
#endif /*SEEK_ISTR*/ |
| 1117 |
|
| 1118 |
#ifdef SAFE_PORT_OP |
| 1119 |
ScmObj Scm_GetPortPosition(ScmPort *p) |
| 1120 |
#else |
| 1121 |
ScmObj Scm_GetPortPositionUnsafe(ScmPort *p) |
| 1122 |
#endif |
| 1123 |
{ |
| 1124 |
VMDECL; |
| 1125 |
SHORTCUT(p, return Scm_GetPortPositionUnsafe(p)); |
| 1126 |
if (SCM_PORT_CLOSED_P(p)) { |
| 1127 |
Scm_PortError(p, SCM_PORT_ERROR_CLOSED, |
| 1128 |
"attempt to take a position of a closed port: %S", p); |
| 1129 |
} |
| 1130 |
|
| 1131 |
ScmObj r = SCM_MAKE_INT(0); |
| 1132 |
volatile off_t pending_bytes = port_pending_bytes(p); |
| 1133 |
int err_disabled = FALSE; |
| 1134 |
int err_badpos = FALSE; |
| 1135 |
|
| 1136 |
LOCK(p); |
| 1137 |
switch (SCM_PORT_TYPE(p)) { |
| 1138 |
case SCM_PORT_FILE: |
| 1139 |
{ |
| 1140 |
off_t rr; |
| 1141 |
if (PORT_BUF(p)->getpos) { |
| 1142 |
SAFE_CALL(p, r = PORT_BUF(p)->getpos(p)); |
| 1143 |
if (!SCM_INTEGERP(r)) { |
| 1144 |
err_badpos = TRUE; |
| 1145 |
break; |
| 1146 |
} |
| 1147 |
rr = Scm_IntegerToOffset(r); |
| 1148 |
} else if (PORT_BUF(p)->seeker) { |
| 1149 |
SAFE_CALL(p, rr = PORT_BUF(p)->seeker(p, 0, SEEK_CUR)); |
| 1150 |
} else { |
| 1151 |
err_disabled = TRUE; |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
|
| 1155 |
if (SCM_PORT_DIR(p)&SCM_PORT_INPUT) { |
| 1156 |
rr -= (off_t)(PORT_BUF(p)->end - PORT_BUF(p)->current); |
| 1157 |
} else { |
| 1158 |
rr += (off_t)(PORT_BUF(p)->current - PORT_BUF(p)->buffer); |
| 1159 |
} |
| 1160 |
r = Scm_OffsetToInteger(rr); |
| 1161 |
} |
| 1162 |
break; |
| 1163 |
case SCM_PORT_ISTR: |
| 1164 |
r = Scm_OffsetToInteger(PORT_ISTR(p)->current - PORT_ISTR(p)->start); |
| 1165 |
break; |
| 1166 |
case SCM_PORT_OSTR: |
| 1167 |
r = Scm_MakeInteger(Scm_DStringSize(PORT_OSTR(p))); |
| 1168 |
break; |
| 1169 |
case SCM_PORT_PROC: |
| 1170 |
/* For procedural bytes, the positioning of pending bytes is |
| 1171 |
taken care of by PORT_SAVED_POS, so we set pending_bytes to 0.*/ |
| 1172 |
pending_bytes = 0; |
| 1173 |
if (PORT_VT(p)->GetPos) { |
| 1174 |
r = PORT_SAVED_POS(p); |
| 1175 |
if (SCM_UNBOUNDP(r)) { |
| 1176 |
SAFE_CALL(p, r = PORT_VT(p)->GetPos(p)); |
| 1177 |
} |
| 1178 |
} else if (PORT_VT(p)->Seek) { |
| 1179 |
off_t rr; |
| 1180 |
UNSAVE_POS(p); |
| 1181 |
SAFE_CALL(p, rr = PORT_VT(p)->Seek(p, 0, SEEK_CUR)); |
| 1182 |
r = Scm_OffsetToInteger(rr); |
| 1183 |
} else { |
| 1184 |
err_disabled = TRUE; |
| 1185 |
} |
| 1186 |
break; |
| 1187 |
} |
| 1188 |
UNLOCK(p); |
| 1189 |
|
| 1190 |
if (err_disabled) { |
| 1191 |
Scm_PortError(p, SCM_PORT_ERROR_SEEK, |
| 1192 |
"getting port position is disabled"); |
| 1193 |
} |
| 1194 |
if (err_badpos) { |
| 1195 |
Scm_PortError(p, SCM_PORT_ERROR_SEEK, |
| 1196 |
"getpos method returned invalid position: %S", r); |
| 1197 |
} |
| 1198 |
|
| 1199 |
if (pending_bytes > 0) { |
| 1200 |
if (r == SCM_MAKE_INT(-1)) return SCM_FALSE; |
| 1201 |
if (!SCM_INTEGERP(r)) return FALSE; |
| 1202 |
r = Scm_Sub(r, Scm_OffsetToInteger(pending_bytes)); |
| 1203 |
} |
| 1204 |
return r; |
| 1205 |
} |
| 1206 |
|
| 1207 |
/* Common routine for Scm_SetPortPosition and Scm_PortSeek. */ |
| 1208 |
#ifndef SET_PORT_POSITION |
| 1209 |
#define SET_PORT_POSITION |
| 1210 |
static ScmObj set_port_position(ScmPort *p, ScmObj pos, int whence) |
| 1211 |
{ |
| 1212 |
VMDECL; |
| 1213 |
|
| 1214 |
LOCK(p); |
| 1215 |
|
| 1216 |
volatile ScmObj off = pos; |
| 1217 |
ScmObj r = SCM_UNDEFINED; |
| 1218 |
int err_disabled = FALSE; |
| 1219 |
off_t pending = port_pending_bytes(p); |
| 1220 |
/* We discard pending bytes. */ |
| 1221 |
p->scrcnt = 0; |
| 1222 |
PORT_UNGOTTEN(p) = SCM_CHAR_INVALID; |
| 1223 |
/* ... and adjust offset, when it's relative. |
| 1224 |
NB: This only matters if the port has seeker protocol, instead |
| 1225 |
of getpos/setpos protocol. */ |
| 1226 |
if (SCM_PORT_TYPE(p) != SCM_PORT_PROC |
| 1227 |
&& whence == SEEK_CUR |
| 1228 |
&& SCM_INTEGERP(off)) { |
| 1229 |
off = Scm_Sub(off, SCM_MAKE_INT(pending)); |
| 1230 |
} |
| 1231 |
|
| 1232 |
switch (SCM_PORT_TYPE(p)) { |
| 1233 |
case SCM_PORT_FILE: |
| 1234 |
if (PORT_BUF(p)->setpos && whence == SEEK_SET) { |
| 1235 |
SAFE_CALL(p, PORT_BUF(p)->setpos(p, off)); |
| 1236 |
r = off; |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
|
| 1240 |
/* NB: we might be able to skip calling seeker if we keep the |
| 1241 |
# of bytes read or write so far, but such count may be off |
| 1242 |
when the port has been experienced an error condition. */ |
| 1243 |
/* NB: the following doesn't work if we have bidirectional port. |
| 1244 |
In such case we need to keep whether the last call of buffer |
| 1245 |
handling routine was input or output. */ |
| 1246 |
if (PORT_BUF(p)->seeker) { |
| 1247 |
/* NB: possible optimization: the specified position is within |
| 1248 |
the current buffer, we can avoid calling seeker. */ |
| 1249 |
off_t rr; |
| 1250 |
if (SCM_PORT_DIR(p)&SCM_PORT_INPUT) { |
| 1251 |
char *c = PORT_BUF(p)->current; /* save current ptr */ |
| 1252 |
if (whence == SEEK_CUR) { |
| 1253 |
off = Scm_Sub(off, Scm_MakeIntegerU(PORT_BUF(p)->end - c)); |
| 1254 |
} |
| 1255 |
PORT_BUF(p)->current = PORT_BUF(p)->end; /* invalidate buffer */ |
| 1256 |
SAFE_CALL(p, rr = PORT_BUF(p)->seeker(p, |
| 1257 |
Scm_IntegerToOffset(off), |
| 1258 |
whence)); |
| 1259 |
if (rr == (off_t)-1) { |
| 1260 |
/* This may happened if seeker somehow gave up */ |
| 1261 |
PORT_BUF(p)->current = c; |
| 1262 |
} |
| 1263 |
} else { |
| 1264 |
SAFE_CALL(p, bufport_flush(p, 0, TRUE)); |
| 1265 |
SAFE_CALL(p, rr = PORT_BUF(p)->seeker(p, |
| 1266 |
Scm_IntegerToOffset(off), |
| 1267 |
whence)); |
| 1268 |
} |
| 1269 |
if (rr == (off_t)-1) { |
| 1270 |
r = SCM_FALSE; |
| 1271 |
} else { |
| 1272 |
r = Scm_OffsetToInteger(rr); |
| 1273 |
} |
| 1274 |
} else { |
| 1275 |
err_disabled = TRUE; |
| 1276 |
} |
| 1277 |
break; |
| 1278 |
case SCM_PORT_ISTR: |
| 1279 |
r = SEEK_ISTR(p, off, whence); |
| 1280 |
break; |
| 1281 |
case SCM_PORT_OSTR: |
| 1282 |
/* Not supported yet */ |
| 1283 |
r = SCM_FALSE; |
| 1284 |
break; |
| 1285 |
case SCM_PORT_PROC: |
| 1286 |
if (PORT_VT(p)->SetPos && whence == SEEK_SET) { |
| 1287 |
UNSAVE_POS(p); |
| 1288 |
SAFE_CALL(p, PORT_VT(p)->SetPos(p, off)); |
| 1289 |
r = off; |
| 1290 |
} else if (PORT_VT(p)->Seek) { |
| 1291 |
UNSAVE_POS(p); |
| 1292 |
off_t rr; |
| 1293 |
SAFE_CALL(p, rr = PORT_VT(p)->Seek(p, |
| 1294 |
Scm_IntegerToOffset(off), |
| 1295 |
whence)); |
| 1296 |
r = Scm_OffsetToInteger(rr); |
| 1297 |
} else { |
| 1298 |
err_disabled = TRUE; |
| 1299 |
} |
| 1300 |
break; |
| 1301 |
} |
| 1302 |
UNLOCK(p); |
| 1303 |
|
| 1304 |
if (err_disabled) { |
| 1305 |
Scm_PortError(p, SCM_PORT_ERROR_SEEK, |
| 1306 |
"setting port position is disabled"); |
| 1307 |
} |
| 1308 |
return r; |
| 1309 |
} |
| 1310 |
|
| 1311 |
ScmObj Scm_SetPortPosition(ScmPort *p, ScmObj pos) |
| 1312 |
{ |
| 1313 |
return set_port_position(p, pos, SEEK_SET); |
| 1314 |
} |
| 1315 |
#endif /* SET_PORT_POSITION */ |
| 1316 |
|
| 1317 |
|
| 1318 |
/* srfi-181 allows port positions to be any Scheme object, so |
| 1319 |
off argument and return value can be any Scheme object---if it's |
| 1320 |
not an exact integer, it should be an object returned by getpos |
| 1321 |
callback. |
| 1322 |
If the port only has seeker callback, it must be an exact integer. |
| 1323 |
If the port implements setpos callback, whence must be SEEK_SET |
| 1324 |
for setting position. |
| 1325 |
*/ |
| 1326 |
#ifdef SAFE_PORT_OP |
| 1327 |
ScmObj Scm_PortSeek(ScmPort *p, volatile ScmObj off, int whence) |
| 1328 |
#else |
| 1329 |
ScmObj Scm_PortSeekUnsafe(ScmPort *p, ScmObj off, int whence) |
| 1330 |
#endif |
| 1331 |
{ |
| 1332 |
if (whence == SEEK_CUR && off == SCM_MAKE_INT(0)) { |
| 1333 |
return Scm_GetPortPosition(p); |
| 1334 |
} else { |
| 1335 |
return set_port_position(p, off, whence); |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
#undef VMDECL |
| 1340 |
#undef LOCK |
| 1341 |
#undef UNLOCK |
| 1342 |
#undef SAFE_CALL |
| 1343 |
#undef SHORTCUT |
| 1344 |
#undef CLOSE_CHECK |