| 1 |
/* |
| 2 |
* write.c - writer |
| 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 |
#define LIBGAUCHE_BODY |
| 35 |
#include "gauche.h" |
| 36 |
#include "gauche/priv/configP.h" |
| 37 |
#include "gauche/writer.h" |
| 38 |
#include "gauche/priv/builtin-syms.h" |
| 39 |
#include "gauche/priv/macroP.h" /* PVREF stuff. Will go in future */ |
| 40 |
#include "gauche/priv/writerP.h" |
| 41 |
#include "gauche/priv/portP.h" |
| 42 |
#include "gauche/priv/stringP.h" |
| 43 |
#include "gauche/char_attr.h" |
| 44 |
|
| 45 |
#include <ctype.h> |
| 46 |
|
| 47 |
static void write_walk(ScmObj obj, ScmPort *port); |
| 48 |
static void write_ss(ScmObj obj, ScmPort *port, ScmWriteContext *ctx); |
| 49 |
static void write_rec(ScmObj obj, ScmPort *port, ScmWriteContext *ctx); |
| 50 |
static void write_object(ScmObj obj, ScmPort *out, ScmWriteContext *ctx); |
| 51 |
static ScmObj write_object_fallback(ScmObj *args, int nargs, ScmGeneric *gf); |
| 52 |
static int double_quote_closed_p(ScmString *s); |
| 53 |
SCM_DEFINE_GENERIC(Scm_GenericWriteObject, write_object_fallback, NULL); |
| 54 |
|
| 55 |
static void call_pprint(ScmObj obj, ScmPort *out); |
| 56 |
|
| 57 |
static const ScmWriteControls *defaultWriteControls; |
| 58 |
|
| 59 |
/*============================================================ |
| 60 |
* Writers |
| 61 |
*/ |
| 62 |
|
| 63 |
/* Note: all internal routine (static functions) assumes the output |
| 64 |
port is properly locked. */ |
| 65 |
|
| 66 |
/* Note: the current internal structure is in the transient state. |
| 67 |
handling of writer mode and context should be much better. |
| 68 |
Do not count on these APIs! */ |
| 69 |
|
| 70 |
/* Note: in order to support write/ss, we need to pass down the context |
| 71 |
along the call tree. |
| 72 |
For the time being, we attach transient extra info to the port during |
| 73 |
the call. It's a bit ugly, though, to rely on such mutation. In |
| 74 |
future we might introduce a 'wrapper' port, which operates just like |
| 75 |
the inner (wrapped) port but can carry extra info. We can do it with |
| 76 |
virtual port now, but it takes some overhead; we want it to be much |
| 77 |
lighter. |
| 78 |
*/ |
| 79 |
|
| 80 |
#define SPBUFSIZ 50 |
| 81 |
|
| 82 |
/* Two bitmask used internally to indicate extra write mode */ |
| 83 |
#define WRITE_LIMITED 0x10 /* we're limiting the length of output. */ |
| 84 |
|
| 85 |
/* VM-default case mode */ |
| 86 |
#define DEFAULT_CASE \ |
| 87 |
(SCM_VM_RUNTIME_FLAG_IS_SET(Scm_VM(), SCM_CASE_FOLD)? \ |
| 88 |
SCM_WRITE_CASE_FOLD:SCM_WRITE_CASE_NOFOLD) |
| 89 |
|
| 90 |
/* Whether we need 'walk' pass to find out shared and/or circular |
| 91 |
structure. Now we use two-pass writing by default, and use one-pass |
| 92 |
writing only when requested specifically. */ |
| 93 |
#define WRITER_NEED_2PASS(ctx) (SCM_WRITE_MODE(ctx) != SCM_WRITE_SIMPLE) |
| 94 |
|
| 95 |
/* |
| 96 |
* WriteContext public API |
| 97 |
*/ |
| 98 |
int Scm_WriteContextMode(const ScmWriteContext *ctx) |
| 99 |
{ |
| 100 |
return SCM_WRITE_MODE(ctx); |
| 101 |
} |
| 102 |
|
| 103 |
int Scm_WriteContextCase(const ScmWriteContext *ctx) |
| 104 |
{ |
| 105 |
return SCM_WRITE_CASE(ctx); |
| 106 |
} |
| 107 |
|
| 108 |
static void write_context_init(ScmWriteContext *ctx, int mode, int flags, int limit) |
| 109 |
{ |
| 110 |
ctx->mode = mode; |
| 111 |
/* if case mode is not specified, use default taken from VM default */ |
| 112 |
if (SCM_WRITE_CASE(ctx) == 0) ctx->mode |= DEFAULT_CASE; |
| 113 |
ctx->flags = flags; |
| 114 |
ctx->limit = limit; |
| 115 |
ctx->controls = NULL; |
| 116 |
if (limit > 0) ctx->flags |= WRITE_LIMITED; |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
* WriteControls |
| 121 |
*/ |
| 122 |
ScmWriteControls *Scm_MakeWriteControls(const ScmWriteControls *proto) |
| 123 |
{ |
| 124 |
ScmWriteControls *p = SCM_NEW(ScmWriteControls); |
| 125 |
SCM_SET_CLASS(p, SCM_CLASS_WRITE_CONTROLS); |
| 126 |
if (proto) { |
| 127 |
*p = *proto; |
| 128 |
} else { |
| 129 |
p->printLength = -1; |
| 130 |
p->printLevel = -1; |
| 131 |
p->printWidth = -1; |
| 132 |
p->printBase = 10; |
| 133 |
p->printRadix = FALSE; |
| 134 |
p->printPretty = FALSE; |
| 135 |
p->printIndent = 0; |
| 136 |
p->stringLength = -1; |
| 137 |
p->exactDecimal = FALSE; |
| 138 |
} |
| 139 |
return p; |
| 140 |
} |
| 141 |
|
| 142 |
const ScmWriteControls *Scm_DefaultWriteControls(void) |
| 143 |
{ |
| 144 |
return defaultWriteControls; |
| 145 |
} |
| 146 |
|
| 147 |
const ScmWriteControls *Scm_GetWriteControls(ScmWriteContext *ctx, |
| 148 |
ScmWriteState *st) |
| 149 |
{ |
| 150 |
if (ctx && ctx->controls) return ctx->controls; |
| 151 |
if (st && st->controls) return st->controls; |
| 152 |
return defaultWriteControls; |
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
* WriteState |
| 157 |
*/ |
| 158 |
/* The class definition is in libio.scm */ |
| 159 |
|
| 160 |
/* NB: For the time being, proto argument is ignored. */ |
| 161 |
ScmWriteState *Scm_MakeWriteState(ScmWriteState *proto SCM_UNUSED) |
| 162 |
{ |
| 163 |
ScmWriteState *z = SCM_NEW(ScmWriteState); |
| 164 |
SCM_SET_CLASS(z, SCM_CLASS_WRITE_STATE); |
| 165 |
z->sharedTable = NULL; |
| 166 |
z->sharedCounter = 0; |
| 167 |
z->currentLevel = 0; |
| 168 |
z->controls = NULL; |
| 169 |
return z; |
| 170 |
} |
| 171 |
|
| 172 |
/* Cleanup transient data attached to the port. */ |
| 173 |
static void cleanup_port_write_state(ScmPort *port) |
| 174 |
{ |
| 175 |
port->flags &= ~(SCM_PORT_WALKING|SCM_PORT_WRITESS); |
| 176 |
ScmWriteState *s = Scm_PortWriteState(port); |
| 177 |
if (s != NULL) { |
| 178 |
Scm_PortWriteStateSet(port, NULL); |
| 179 |
/* The table for recursive/shared detection should be GC'ed after |
| 180 |
we drop the reference to it. However, the table can be quite big |
| 181 |
after doing write-shared on a large graph, and our implementation of |
| 182 |
big hashtables isn't particularly friendly to GC---it is prone |
| 183 |
to be a victim of false pointers, especially on 32bit architecture. |
| 184 |
It becomes an issue if the app repeatedly use write-shared on |
| 185 |
large graph, for an incorrectly retained hashtable may have false |
| 186 |
pointers to other incorrectly retained hashtable, making the amount |
| 187 |
of retained garbage unbounded. So, we take extra step to clear |
| 188 |
the table to avoid the risk. In vast majority of the case, the |
| 189 |
table is used only for circle detection, in which case the table |
| 190 |
is small and it won't add much overhead. |
| 191 |
*/ |
| 192 |
if (s && s->sharedTable) { |
| 193 |
Scm_HashCoreClear(SCM_HASH_TABLE_CORE(s->sharedTable)); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/* |
| 199 |
* Entry points |
| 200 |
* |
| 201 |
* For shared/circular structure detection, we have to distinguish |
| 202 |
* the "toplevel" call to write and the recursive calls. The catch |
| 203 |
* is that Scm_Write etc. can be called recursively, via write-object |
| 204 |
* method, and we can't rely on its arguments to determine which is |
| 205 |
* the case. So we see the port to find out if we're in the recursive |
| 206 |
* mode (see the above discussion about the context.) |
| 207 |
*/ |
| 208 |
|
| 209 |
/* |
| 210 |
* Scm_Write - Standard Write, convenience version. Using default controls. |
| 211 |
*/ |
| 212 |
void Scm_Write(ScmObj obj, ScmObj p, int mode) |
| 213 |
{ |
| 214 |
Scm_WriteWithControls(obj, p, mode, NULL); |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* Scm_WriteWithControls - the general entry |
| 219 |
*/ |
| 220 |
void Scm_WriteWithControls(ScmObj obj, ScmObj p, int mode, |
| 221 |
const ScmWriteControls *ctrl) |
| 222 |
{ |
| 223 |
if (!SCM_OPORTP(p)) Scm_Error("output port required, but got %S", p); |
| 224 |
|
| 225 |
ScmPort *port = SCM_PORT(p); |
| 226 |
ScmVM *vm = Scm_VM(); |
| 227 |
if (ctrl == NULL) ctrl = Scm_DefaultWriteControls(); |
| 228 |
|
| 229 |
if (PORT_LOCK_OWNER_P(port, vm) && PORT_RECURSIVE_P(port)) { |
| 230 |
/* We're in the recursive call, so we just recurse into write_walk |
| 231 |
or write_rec, according to the phase. NB: The controls passed |
| 232 |
into the argument CTRL is ignored; the "root" control, passed |
| 233 |
to the toplevel write API, will be used. */ |
| 234 |
if (PORT_WALKER_P(port)) { |
| 235 |
/* Special treatment - if we're "display"-ing a string, we'll |
| 236 |
bypass walk path even if we're in the middle of write/ss. |
| 237 |
Using srfi-38 notation to show displayed strings doesn't |
| 238 |
make sense at all. */ |
| 239 |
if (!((mode == SCM_WRITE_DISPLAY) && SCM_STRINGP(obj))) { |
| 240 |
write_walk(obj, port); |
| 241 |
} |
| 242 |
} else { |
| 243 |
ScmWriteContext ctx; |
| 244 |
write_context_init(&ctx, mode, 0, 0); |
| 245 |
write_rec(obj, port, &ctx); |
| 246 |
} |
| 247 |
|
| 248 |
} else { |
| 249 |
/* We're in the toplevel call.*/ |
| 250 |
ScmWriteContext ctx; |
| 251 |
write_context_init(&ctx, mode, 0, 0); |
| 252 |
PORT_LOCK(port, vm); |
| 253 |
if (WRITER_NEED_2PASS(&ctx)) { |
| 254 |
ctx.controls = ctrl; |
| 255 |
PORT_SAFE_CALL(port, write_ss(obj, port, &ctx), |
| 256 |
cleanup_port_write_state(port)); |
| 257 |
} else { |
| 258 |
/* write-simple case. CTRL is ignored. */ |
| 259 |
PORT_SAFE_CALL(port, write_rec(obj, port, &ctx), /*no cleanup*/); |
| 260 |
} |
| 261 |
PORT_UNLOCK(port); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* Scm_WriteLimited - Write to limited length. |
| 267 |
* |
| 268 |
* Characters exceeding WIDTH are truncated. |
| 269 |
* If the output fits within WIDTH, # of characters actually written |
| 270 |
* is returned. Otherwise, -1 is returned. |
| 271 |
* |
| 272 |
* Currently this API is only used from Scm_Printf, for 'format' has been |
| 273 |
* moved to libfmt.scm. I don't like the way this is implemented and would |
| 274 |
* like to share this with libfmt.scm eventually. |
| 275 |
*/ |
| 276 |
int Scm_WriteLimited(ScmObj obj, ScmObj p, int mode, int width) |
| 277 |
{ |
| 278 |
if (!SCM_OPORTP(p)) { |
| 279 |
Scm_Error("output port required, but got %S", p); |
| 280 |
} |
| 281 |
|
| 282 |
ScmPort *port = SCM_PORT(p); |
| 283 |
|
| 284 |
/* The walk pass does not produce any output, so we don't bother to |
| 285 |
create an intermediate string port. */ |
| 286 |
if (PORT_LOCK_OWNER_P(port, Scm_VM()) && PORT_WALKER_P(port)) { |
| 287 |
SCM_ASSERT(PORT_RECURSIVE_P(port)); |
| 288 |
write_walk(obj, port); |
| 289 |
return 0; /* doesn't really matter */ |
| 290 |
} |
| 291 |
|
| 292 |
ScmObj out = Scm_MakeOutputStringPort(TRUE); |
| 293 |
Scm_PortWriteStateSet(SCM_PORT(out), Scm_PortWriteState(port)); |
| 294 |
ScmWriteContext ctx; |
| 295 |
write_context_init(&ctx, mode, 0, width); |
| 296 |
|
| 297 |
/* We don't need to lock 'out', nor clean it up, for it is private. */ |
| 298 |
/* This part is a bit confusing - we only need to call write_ss |
| 299 |
if we're at the toplevel call. */ |
| 300 |
if (PORT_RECURSIVE_P(SCM_PORT(port))) { |
| 301 |
write_rec(obj, SCM_PORT(out), &ctx); |
| 302 |
} else if (WRITER_NEED_2PASS(&ctx)) { |
| 303 |
write_ss(obj, SCM_PORT(out), &ctx); |
| 304 |
} else { |
| 305 |
write_rec(obj, SCM_PORT(out), &ctx); |
| 306 |
} |
| 307 |
|
| 308 |
ScmString *str = SCM_STRING(Scm_GetOutputString(SCM_PORT(out), 0)); |
| 309 |
int nc = SCM_STRING_BODY_LENGTH(SCM_STRING_BODY(str)); |
| 310 |
if (nc > width) { |
| 311 |
ScmObj sub = Scm_Substring(str, 0, width, FALSE); |
| 312 |
SCM_PUTS(sub, port); /* this locks port */ |
| 313 |
return -1; |
| 314 |
} else { |
| 315 |
SCM_PUTS(str, port); /* this locks port */ |
| 316 |
return nc; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/* OBSOLETED: This is redundant. Will be gone in 1.0 release. */ |
| 321 |
int Scm_WriteCircular(ScmObj obj, ScmObj port, int mode, int width) |
| 322 |
{ |
| 323 |
if (width <= 0) { |
| 324 |
Scm_Write(obj, port, mode); |
| 325 |
return 0; |
| 326 |
} else { |
| 327 |
return Scm_WriteLimited(obj, port, mode, width); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
/*=================================================================== |
| 332 |
* Internal writer |
| 333 |
*/ |
| 334 |
|
| 335 |
/* Obj is PTR, except pair and vector */ |
| 336 |
static void write_general(ScmObj obj, ScmPort *out, ScmWriteContext *ctx) |
| 337 |
{ |
| 338 |
ScmClass *c = Scm_ClassOf(obj); |
| 339 |
if (c->print) c->print(obj, out, ctx); |
| 340 |
else write_object(obj, out, ctx); |
| 341 |
} |
| 342 |
|
| 343 |
/* Default object printer delegates print action to generic function |
| 344 |
write-object. We can't use VMApply here since this function can be |
| 345 |
called deep in the recursive stack of Scm_Write, so the control |
| 346 |
may not return to VM immediately. */ |
| 347 |
static void write_object(ScmObj obj, ScmPort *port, |
| 348 |
ScmWriteContext *ctx SCM_UNUSED) |
| 349 |
{ |
| 350 |
Scm_ApplyRec(SCM_OBJ(&Scm_GenericWriteObject), |
| 351 |
SCM_LIST2(obj, SCM_OBJ(port))); |
| 352 |
} |
| 353 |
|
| 354 |
/* Default method for write-object */ |
| 355 |
static ScmObj write_object_fallback(ScmObj *args, int nargs, |
| 356 |
ScmGeneric *gf SCM_UNUSED) |
| 357 |
{ |
| 358 |
if (nargs != 2 || (nargs == 2 && !SCM_OPORTP(args[1]))) { |
| 359 |
Scm_Error("No applicable method for write-object with %S", |
| 360 |
Scm_ArrayToList(args, nargs)); |
| 361 |
} |
| 362 |
ScmClass *klass = Scm_ClassOf(args[0]); |
| 363 |
/* If any base class has print() pointer, we use it. */ |
| 364 |
for (ScmClass **cpa = klass->cpa; *cpa; cpa++) { |
| 365 |
if ((*cpa)->print) { |
| 366 |
/* NB: For historical reasons, the context passed to |
| 367 |
write_object isn't passed down to the generic |
| 368 |
function. We'll address it later, but for now |
| 369 |
we use the default context. */ |
| 370 |
ScmWriteContext ctx; |
| 371 |
write_context_init(&ctx, SCM_WRITE_WRITE, 0, 0); |
| 372 |
(*cpa)->print(args[0], SCM_PORT(args[1]), &ctx); |
| 373 |
return SCM_TRUE; |
| 374 |
} |
| 375 |
} |
| 376 |
/* Fallback printer */ |
| 377 |
Scm_Printf(SCM_PORT(args[1]), "#<%A%s%p>", |
| 378 |
klass->name, |
| 379 |
(SCM_FALSEP(klass->redefined)? " " : ":redefined "), |
| 380 |
args[0]); |
| 381 |
return SCM_TRUE; |
| 382 |
} |
| 383 |
|
| 384 |
/* character name table (first 33 chars of ASCII)*/ |
| 385 |
static const char *char_names[] = { |
| 386 |
"null", "x01", "x02", "x03", "x04", "x05", "x06", "alarm", |
| 387 |
"backspace","tab", "newline","x0b", "x0c", "return","x0e", "x0f", |
| 388 |
"x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", |
| 389 |
"x18", "x19", "x1a", "escape","x1c", "x1d", "x1e", "x1f", |
| 390 |
"space" |
| 391 |
}; |
| 392 |
|
| 393 |
/* Returns # of chars written. |
| 394 |
This can be better in char.c, but to do so, we'd better to clean up |
| 395 |
public interface for ScmWriteContext. |
| 396 |
TODO: It would be nice to have a mode to print character in unicode |
| 397 |
character name. |
| 398 |
*/ |
| 399 |
static size_t write_char(ScmChar ch, ScmPort *port, ScmWriteContext *ctx) |
| 400 |
{ |
| 401 |
if (SCM_WRITE_MODE(ctx) == SCM_WRITE_DISPLAY) { |
| 402 |
Scm_PutcUnsafe(ch, port); |
| 403 |
return 1; |
| 404 |
} else { |
| 405 |
const char *cname = NULL; |
| 406 |
char buf[SPBUFSIZ]; |
| 407 |
|
| 408 |
Scm_PutzUnsafe("#\\", -1, port); |
| 409 |
if (ch <= 0x20) cname = char_names[ch]; |
| 410 |
else if (ch == 0x7f) cname = "delete"; |
| 411 |
else { |
| 412 |
switch (Scm_CharGeneralCategory(ch)) { |
| 413 |
case SCM_CHAR_CATEGORY_Mn: |
| 414 |
case SCM_CHAR_CATEGORY_Mc: |
| 415 |
case SCM_CHAR_CATEGORY_Me: |
| 416 |
case SCM_CHAR_CATEGORY_Zs: |
| 417 |
case SCM_CHAR_CATEGORY_Zl: |
| 418 |
case SCM_CHAR_CATEGORY_Zp: |
| 419 |
case SCM_CHAR_CATEGORY_Cc: |
| 420 |
case SCM_CHAR_CATEGORY_Cf: |
| 421 |
case SCM_CHAR_CATEGORY_Cs: |
| 422 |
case SCM_CHAR_CATEGORY_Co: |
| 423 |
case SCM_CHAR_CATEGORY_Cn: |
| 424 |
/* NB: Legacy Gauche uses native character code for #\xNNNN |
| 425 |
notation, while R7RS uses Unicode codepoint. We eventually |
| 426 |
need a write mode (legacy or r7rs) and switch the output |
| 427 |
accordingly---the safe bet is to use #\uNNNN for legacy |
| 428 |
mode and #\xNNNN for R7RS mode. */ |
| 429 |
snprintf(buf, SPBUFSIZ, "x%04x", (unsigned int)ch); |
| 430 |
cname = buf; |
| 431 |
break; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
if (cname) { |
| 436 |
Scm_PutzUnsafe(cname, -1, port); |
| 437 |
return strlen(cname)+2; /* +2 for '#\' */ |
| 438 |
} else { |
| 439 |
Scm_PutcUnsafe(ch, port); |
| 440 |
return 3; /* +2 for '#\' */ |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
/* If OBJ is a primitive object (roughly, immediate or number), write it to |
| 446 |
PORT. Assumes the caller locks the PORT. |
| 447 |
Returns the # of characters written, or #f if OBJ is not a primitive object. |
| 448 |
*/ |
| 449 |
ScmObj Scm__WritePrimitive(ScmObj obj, ScmPort *port, ScmWriteContext *ctx) |
| 450 |
{ |
| 451 |
const ScmWriteControls *wp = |
| 452 |
Scm_GetWriteControls(ctx, Scm_PortWriteState(port)); |
| 453 |
|
| 454 |
#define CASE_ITAG_RET(obj, str) \ |
| 455 |
case SCM_ITAG(obj): \ |
| 456 |
Scm_PutzUnsafe(str, -1, port); \ |
| 457 |
return SCM_MAKE_INT(sizeof(str)-1); |
| 458 |
|
| 459 |
if (SCM_IMMEDIATEP(obj)) { |
| 460 |
switch (SCM_ITAG(obj)) { |
| 461 |
CASE_ITAG_RET(SCM_FALSE, "#f"); |
| 462 |
CASE_ITAG_RET(SCM_TRUE, "#t"); |
| 463 |
CASE_ITAG_RET(SCM_NIL, "()"); |
| 464 |
CASE_ITAG_RET(SCM_EOF, "#<eof>"); |
| 465 |
CASE_ITAG_RET(SCM_UNDEFINED, "#<undef>"); |
| 466 |
/* The following two values should never be seen by the user; but |
| 467 |
internal bugs may reveal them. */ |
| 468 |
CASE_ITAG_RET(SCM_UNBOUND, "#<unbound>"); |
| 469 |
CASE_ITAG_RET(SCM_UNINITIALIZED, "#<uninitialized>"); |
| 470 |
default: |
| 471 |
Scm_Panic("write: unknown itag object: %08x", SCM_WORD(obj)); |
| 472 |
} |
| 473 |
} |
| 474 |
else if (SCM_INTP(obj) && wp->printBase == 10 && !wp->printRadix) { |
| 475 |
/* Shortcut to avoid allocation */ |
| 476 |
char buf[SPBUFSIZ]; |
| 477 |
int k = snprintf(buf, SPBUFSIZ, "%ld", SCM_INT_VALUE(obj)); |
| 478 |
Scm_PutzUnsafe(buf, -1, port); |
| 479 |
return SCM_MAKE_INT(k); |
| 480 |
} |
| 481 |
else if (SCM_CHARP(obj)) { |
| 482 |
size_t k = write_char(SCM_CHAR_VALUE(obj), port, ctx); |
| 483 |
return SCM_MAKE_INT(k); |
| 484 |
} |
| 485 |
else if (SCM_NUMBERP(obj)) { |
| 486 |
ScmNumberFormat fmt; |
| 487 |
Scm_NumberFormatInit(&fmt); |
| 488 |
fmt.radix = wp->printBase; |
| 489 |
if (wp->printRadix) fmt.flags |= SCM_NUMBER_FORMAT_ALT_RADIX; |
| 490 |
if (wp->exactDecimal) fmt.flags |= SCM_NUMBER_FORMAT_EXACT_DECIMAL_POINT; |
| 491 |
return SCM_MAKE_INT(Scm_PrintNumber(port, obj, &fmt)); |
| 492 |
} |
| 493 |
/* PVREF only appears in pattern temlate in the current macro expander. |
| 494 |
It will be go away once we rewrite the expander. */ |
| 495 |
else if (SCM_PVREF_P(obj)) { |
| 496 |
char buf[SPBUFSIZ]; |
| 497 |
int k = snprintf(buf, SPBUFSIZ, "#<pvar %" PRIdPTR ".%" PRIdPTR ">", |
| 498 |
SCM_PVREF_LEVEL(obj), SCM_PVREF_COUNT(obj)); |
| 499 |
Scm_PutzUnsafe(buf, -1, port); |
| 500 |
return SCM_MAKE_INT(k); |
| 501 |
} |
| 502 |
else if (SCM_STRING_CURSOR_SMALL_P(obj)) { |
| 503 |
char buf[SPBUFSIZ]; |
| 504 |
int k = snprintf(buf, SPBUFSIZ, "#<string-cursor %ld>", |
| 505 |
SCM_STRING_CURSOR_SMALL_OFFSET(obj)); |
| 506 |
Scm_PutzUnsafe(buf, -1, port); |
| 507 |
return SCM_MAKE_INT(k); |
| 508 |
} |
| 509 |
return SCM_FALSE; |
| 510 |
} |
| 511 |
|
| 512 |
/* We need two passes to realize write/ss. |
| 513 |
|
| 514 |
The first pass ("walk" pass) traverses the data and finds out |
| 515 |
all shared substructures and/or cyclic references. It builds a |
| 516 |
hash table of objects that need special treatment. |
| 517 |
|
| 518 |
The second pass ("emit" pass) writes out the data. |
| 519 |
|
| 520 |
For the walk pass, we can't use generic traversal algorithm |
| 521 |
if the data contains user-defined structures. In which case, |
| 522 |
we delegate the walk task to the user-defined print routine. |
| 523 |
In the walk pass, we set SCM_PORT_WALKING flag of the port. |
| 524 |
Port API recognizes this flag and just ignore any output to |
| 525 |
this port. Writers recognize this flag and works as the |
| 526 |
walk pass. |
| 527 |
|
| 528 |
The walk pass sets up a hashtable that records how many times |
| 529 |
each aggregate datum has been seen. If it's >1, emit pass |
| 530 |
uses #n# and #n= notation. |
| 531 |
|
| 532 |
NB: R7RS write-shared doesn't require datum labels on strings, |
| 533 |
but srfi-38 does. We follow srfi-38. |
| 534 |
|
| 535 |
NB: The walk pass is now written in Scheme (libio.scm: write-walk), |
| 536 |
but the emit pass is in C (write_rec). Using naive recursion in write_rec |
| 537 |
can bust the C stack when deep structure is passed, even if it is |
| 538 |
not circular. |
| 539 |
Thus we avoided recursion by managing traversal stack by our own |
| 540 |
('stack' local variable). It made the code ugly. Oh well. |
| 541 |
|
| 542 |
We can't avoid recusion via class printer. It would need a major |
| 543 |
overhaul to fix that. However, just preventing blowup by lists |
| 544 |
and vectors is still useful. |
| 545 |
|
| 546 |
The stack is a list, whose element can be (#t count . list) or |
| 547 |
(index . vector). In the first case, the count part keeps |
| 548 |
track of how many siblings we already printed, and the list part is the |
| 549 |
rest of the list we should process after the current item is |
| 550 |
written out. In the second case, we're processing the vector, |
| 551 |
and the next item we should process is pointed by index. |
| 552 |
|
| 553 |
We also set a limit of stack depth in write_rec; in case |
| 554 |
car-circular list (e.g. #0=(#0#) ) is given to write-simple. |
| 555 |
It used to SEGV by busting C stack. With the change of making it |
| 556 |
non-recursive, it would hog all the heap before crash, which is |
| 557 |
rather unpleasant, so we make it bail out before that. |
| 558 |
*/ |
| 559 |
|
| 560 |
/* pass 1 */ |
| 561 |
/* Implemented in Scheme */ |
| 562 |
static void write_walk(ScmObj obj, ScmPort *port) |
| 563 |
{ |
| 564 |
static ScmObj proc = SCM_UNDEFINED; |
| 565 |
ScmWriteState *s = Scm_PortWriteState(port); |
| 566 |
SCM_ASSERT(s); |
| 567 |
ScmHashTable *ht = s->sharedTable; |
| 568 |
SCM_ASSERT(ht != NULL); |
| 569 |
SCM_BIND_PROC(proc, "%write-walk-rec", Scm_GaucheInternalModule()); |
| 570 |
Scm_ApplyRec3(proc, obj, SCM_OBJ(port), SCM_OBJ(ht)); |
| 571 |
} |
| 572 |
|
| 573 |
/* pass 2 */ |
| 574 |
|
| 575 |
/* A limit of stack depth to detect (potential) car-circular structure |
| 576 |
when we're writing out without shared structure notation. This is |
| 577 |
an arbitrary limit, but we used to SEGVed in such case, so it's better |
| 578 |
than that. */ |
| 579 |
#define STACK_LIMIT 0x1000000 |
| 580 |
|
| 581 |
/* Trick: The hashtable contains positive integer after the walk pass. |
| 582 |
If we emit a reference tag N, we replace the entry's value to -N, |
| 583 |
so that we can distinguish whether we've already emitted the object |
| 584 |
or not. */ |
| 585 |
static void write_rec(ScmObj obj, ScmPort *port, ScmWriteContext *ctx) |
| 586 |
{ |
| 587 |
char numbuf[50]; /* enough to contain long number */ |
| 588 |
ScmObj stack = SCM_NIL; |
| 589 |
ScmWriteState *st = Scm_PortWriteState(port); |
| 590 |
ScmHashTable *ht = (st? st->sharedTable : NULL); |
| 591 |
const ScmWriteControls *wp = Scm_GetWriteControls(ctx, st); |
| 592 |
int stack_depth = 0; /* only used when !ht */ |
| 593 |
|
| 594 |
#define PUSH(elt) \ |
| 595 |
do { \ |
| 596 |
stack = Scm_Cons(elt, stack); \ |
| 597 |
if (!ht && ++stack_depth > STACK_LIMIT) { \ |
| 598 |
Scm_Error("write recursed too deeply; " \ |
| 599 |
"maybe a circular structure?"); \ |
| 600 |
} \ |
| 601 |
} while (0) |
| 602 |
#define POP() \ |
| 603 |
do { \ |
| 604 |
stack = SCM_CDR(stack); \ |
| 605 |
if (!ht) stack_depth--; \ |
| 606 |
} while (0) |
| 607 |
#define CHECK_LEVEL() \ |
| 608 |
do { \ |
| 609 |
if (st) { \ |
| 610 |
if (wp->printLevel >= 0 && st->currentLevel >= wp->printLevel) { \ |
| 611 |
Scm_PutcUnsafe('#', port); \ |
| 612 |
goto next; \ |
| 613 |
} else { \ |
| 614 |
if (st) st->currentLevel++; \ |
| 615 |
} \ |
| 616 |
} \ |
| 617 |
} while (0) |
| 618 |
|
| 619 |
|
| 620 |
for (;;) { |
| 621 |
write1: |
| 622 |
if (ctx->flags & WRITE_LIMITED) { |
| 623 |
if (PORT_OSTR(port)->length >= ctx->limit) return; |
| 624 |
} |
| 625 |
|
| 626 |
/* number may be heap allocated, but we don't use srfi-38 notation. */ |
| 627 |
if (!SCM_PTRP(obj) || SCM_NUMBERP(obj)) { |
| 628 |
if (SCM_FALSEP(Scm__WritePrimitive(obj, port, ctx))) { |
| 629 |
Scm_Panic("write: got a bogus object: %08x", SCM_WORD(obj)); |
| 630 |
} |
| 631 |
goto next; |
| 632 |
} |
| 633 |
if ((SCM_STRINGP(obj) && SCM_STRING_SIZE(obj) == 0) |
| 634 |
|| (SCM_VECTORP(obj) && SCM_VECTOR_SIZE(obj) == 0)) { |
| 635 |
/* we don't put a reference tag for these */ |
| 636 |
write_general(obj, port, ctx); |
| 637 |
goto next; |
| 638 |
} |
| 639 |
|
| 640 |
/* obj is heap allocated and we may use label notation. */ |
| 641 |
if (ht) { |
| 642 |
ScmObj e = Scm_HashTableRef(ht, obj, SCM_MAKE_INT(1)); |
| 643 |
long k = SCM_INT_VALUE(e); |
| 644 |
if (k <= 0) { |
| 645 |
/* This object is already printed. */ |
| 646 |
snprintf(numbuf, 50, "#%ld#", -k); |
| 647 |
Scm_PutzUnsafe(numbuf, -1, port); |
| 648 |
goto next; |
| 649 |
} else if (k > 1) { |
| 650 |
/* This object will be seen again. Put a reference tag. */ |
| 651 |
ScmWriteState *s = Scm_PortWriteState(port); |
| 652 |
snprintf(numbuf, 50, "#%d=", s->sharedCounter); |
| 653 |
Scm_HashTableSet(ht, obj, SCM_MAKE_INT(-s->sharedCounter), 0); |
| 654 |
s->sharedCounter++; |
| 655 |
Scm_PutzUnsafe(numbuf, -1, port); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
/* Writes aggregates */ |
| 660 |
if (SCM_PAIRP(obj)) { |
| 661 |
|
| 662 |
CHECK_LEVEL(); |
| 663 |
|
| 664 |
/* special case for quote etc. |
| 665 |
NB: we need to check if we've seen SCM_CDR(obj), otherwise we'll |
| 666 |
get infinite recursion for the case like (cdr '#1='#1#). */ |
| 667 |
if (SCM_PAIRP(SCM_CDR(obj)) && SCM_NULLP(SCM_CDDR(obj)) |
| 668 |
&& (!ht |
| 669 |
|| SCM_FALSEP(Scm_HashTableRef(ht, SCM_CDR(obj), SCM_FALSE)))){ |
| 670 |
const char *prefix = NULL; |
| 671 |
if (SCM_CAR(obj) == SCM_SYM_QUOTE) { |
| 672 |
prefix = "'"; |
| 673 |
} else if (SCM_CAR(obj) == SCM_SYM_QUASIQUOTE) { |
| 674 |
prefix = "`"; |
| 675 |
} else if (SCM_CAR(obj) == SCM_SYM_UNQUOTE) { |
| 676 |
prefix = ","; |
| 677 |
} else if (SCM_CAR(obj) == SCM_SYM_UNQUOTE_SPLICING) { |
| 678 |
prefix = ",@"; |
| 679 |
} |
| 680 |
if (prefix) { |
| 681 |
Scm_PutzUnsafe(prefix, -1, port); |
| 682 |
obj = SCM_CADR(obj); |
| 683 |
goto write1; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
if (wp->printLength == 0) { |
| 688 |
/* in this case we don't print the elements at all, so we need |
| 689 |
to treat this specially. */ |
| 690 |
Scm_PutzUnsafe("(" SCM_WRITTEN_ELLIPSIS ")", -1, port); |
| 691 |
if (st) st->currentLevel--; |
| 692 |
goto next; |
| 693 |
} |
| 694 |
|
| 695 |
/* normal case */ |
| 696 |
Scm_PutcUnsafe('(', port); |
| 697 |
PUSH(Scm_Cons(SCM_TRUE, Scm_Cons(SCM_MAKE_INT(1), SCM_CDR(obj)))); |
| 698 |
obj = SCM_CAR(obj); |
| 699 |
goto write1; |
| 700 |
} else if (SCM_VECTORP(obj)) { |
| 701 |
|
| 702 |
CHECK_LEVEL(); |
| 703 |
|
| 704 |
if (wp->printLength == 0) { |
| 705 |
/* in this case we don't print the elements at all, so we need |
| 706 |
to treat this specially. */ |
| 707 |
Scm_PutzUnsafe("#(" SCM_WRITTEN_ELLIPSIS ")", -1, port); |
| 708 |
if (st) st->currentLevel--; |
| 709 |
goto next; |
| 710 |
} |
| 711 |
Scm_PutzUnsafe("#(", -1, port); |
| 712 |
PUSH(Scm_Cons(SCM_MAKE_INT(1), obj)); |
| 713 |
obj = SCM_VECTOR_ELEMENT(obj, 0); |
| 714 |
goto write1; |
| 715 |
} else if (Scm_ClassOf(obj)->flags & SCM_CLASS_AGGREGATE) { |
| 716 |
CHECK_LEVEL(); |
| 717 |
write_general(obj, port, ctx); |
| 718 |
if (st) st->currentLevel--; |
| 719 |
goto next; |
| 720 |
} else { |
| 721 |
write_general(obj, port, ctx); |
| 722 |
goto next; |
| 723 |
} |
| 724 |
|
| 725 |
next: |
| 726 |
while (SCM_PAIRP(stack)) { |
| 727 |
ScmObj top = SCM_CAR(stack); |
| 728 |
SCM_ASSERT(SCM_PAIRP(top)); |
| 729 |
if (SCM_INTP(SCM_CAR(top))) { |
| 730 |
/* we're processing a vector */ |
| 731 |
ScmObj v = SCM_CDR(top); |
| 732 |
int i = SCM_INT_VALUE(SCM_CAR(top)); |
| 733 |
int len = SCM_VECTOR_SIZE(v); |
| 734 |
|
| 735 |
if (i == len) { /* we've done this vector */ |
| 736 |
Scm_PutcUnsafe(')', port); |
| 737 |
POP(); |
| 738 |
} else if (wp->printLength >= 0 && wp->printLength <= i) { |
| 739 |
Scm_PutzUnsafe(" " SCM_WRITTEN_ELLIPSIS ")", -1, port); |
| 740 |
POP(); |
| 741 |
} else { |
| 742 |
Scm_PutcUnsafe(' ', port); |
| 743 |
obj = SCM_VECTOR_ELEMENT(v, i); |
| 744 |
SCM_SET_CAR_UNCHECKED(top, SCM_MAKE_INT(i+1)); |
| 745 |
goto write1; |
| 746 |
} |
| 747 |
} else { |
| 748 |
/* we're processing a list */ |
| 749 |
SCM_ASSERT(SCM_PAIRP(SCM_CDR(top))); |
| 750 |
long count = SCM_INT_VALUE(SCM_CADR(top)); |
| 751 |
ScmObj v = SCM_CDDR(top); |
| 752 |
if (SCM_NULLP(v)) { /* we've done with this list */ |
| 753 |
Scm_PutcUnsafe(')', port); |
| 754 |
POP(); |
| 755 |
} else if (!SCM_PAIRP(v)) { |
| 756 |
/* Improper list. We treat aggregate types specially, |
| 757 |
since such object at this position shouldn't increment |
| 758 |
"level" - its content is regarded as the same level of |
| 759 |
the current list. |
| 760 |
*/ |
| 761 |
Scm_PutzUnsafe(" . ", -1, port); |
| 762 |
if (Scm_ClassOf(v)->flags & SCM_CLASS_AGGREGATE) { |
| 763 |
if (st) st->currentLevel--; |
| 764 |
write_rec(v, port, ctx); |
| 765 |
if (st) st->currentLevel++; |
| 766 |
Scm_PutcUnsafe(')', port); |
| 767 |
POP(); |
| 768 |
} else { |
| 769 |
obj = v; |
| 770 |
SCM_SET_CAR_UNCHECKED(SCM_CDR(top), |
| 771 |
SCM_MAKE_INT(count+1)); |
| 772 |
SCM_SET_CDR_UNCHECKED(SCM_CDR(top), SCM_NIL); |
| 773 |
goto write1; |
| 774 |
} |
| 775 |
} else if (wp->printLength >= 0 && wp->printLength <= count) { |
| 776 |
/* print-length limit reached */ |
| 777 |
Scm_PutzUnsafe(" " SCM_WRITTEN_ELLIPSIS ")", -1, port); |
| 778 |
POP(); |
| 779 |
} else if (ht && !SCM_EQ(Scm_HashTableRef(ht, v, SCM_MAKE_INT(1)), SCM_MAKE_INT(1))) { |
| 780 |
/* cdr part is shared */ |
| 781 |
Scm_PutzUnsafe(" . ", -1, port); |
| 782 |
obj = v; |
| 783 |
SCM_SET_CAR_UNCHECKED(SCM_CDR(top), SCM_MAKE_INT(count+1)); |
| 784 |
SCM_SET_CDR_UNCHECKED(SCM_CDR(top), SCM_NIL); |
| 785 |
goto write1; |
| 786 |
} else { |
| 787 |
Scm_PutcUnsafe(' ', port); |
| 788 |
obj = SCM_CAR(v); |
| 789 |
SCM_SET_CAR_UNCHECKED(SCM_CDR(top), SCM_MAKE_INT(count+1)); |
| 790 |
SCM_SET_CDR_UNCHECKED(SCM_CDR(top), SCM_CDR(v)); |
| 791 |
goto write1; |
| 792 |
} |
| 793 |
} |
| 794 |
if (st) st->currentLevel--; |
| 795 |
} |
| 796 |
break; |
| 797 |
} |
| 798 |
|
| 799 |
#undef PUSH |
| 800 |
#undef POP |
| 801 |
#undef CHECK_DEPTH |
| 802 |
} |
| 803 |
|
| 804 |
/* Returns TRUE if OBJ is 'pretty-printable', that is, pass2 need to be |
| 805 |
handled with %pretty-print. */ |
| 806 |
static int pprintable_p(ScmObj obj) |
| 807 |
{ |
| 808 |
if (!SCM_PTRP(obj)) return FALSE; |
| 809 |
if (SCM_PAIRP(obj)) return TRUE; |
| 810 |
if (SCM_VECTORP(obj) && SCM_VECTOR_SIZE(obj) > 0) return TRUE; |
| 811 |
if (SCM_UVECTORP(obj) && SCM_UVECTOR_SIZE(obj) > 0) return TRUE; |
| 812 |
if (SCM_ISA(obj, SCM_CLASS_DICTIONARY)) return TRUE; |
| 813 |
return FALSE; |
| 814 |
} |
| 815 |
|
| 816 |
/* Write/ss main driver |
| 817 |
This should never be called recursively. |
| 818 |
We modify port->flags and port->writeState; they are cleaned up |
| 819 |
by the caller even if we throw an error during write. */ |
| 820 |
static void write_ss(ScmObj obj, ScmPort *port, ScmWriteContext *ctx) |
| 821 |
{ |
| 822 |
SCM_ASSERT(Scm_PortWriteState(port) == NULL); |
| 823 |
|
| 824 |
/* pass 1 */ |
| 825 |
port->flags |= SCM_PORT_WALKING; |
| 826 |
if (SCM_WRITE_MODE(ctx)==SCM_WRITE_SHARED) port->flags |= SCM_PORT_WRITESS; |
| 827 |
ScmWriteState *s = Scm_MakeWriteState(NULL); |
| 828 |
s->sharedTable = SCM_HASH_TABLE(Scm_MakeHashTableSimple(SCM_HASH_EQ, 0)); |
| 829 |
s->controls = ctx->controls; |
| 830 |
Scm_PortWriteStateSet(port, s); |
| 831 |
|
| 832 |
write_walk(obj, port); |
| 833 |
port->flags &= ~(SCM_PORT_WALKING|SCM_PORT_WRITESS); |
| 834 |
|
| 835 |
/* pass 2 */ |
| 836 |
if (ctx->controls && ctx->controls->printPretty && pprintable_p(obj)) { |
| 837 |
static ScmObj proc = SCM_UNDEFINED; |
| 838 |
SCM_BIND_PROC(proc, "%pretty-print", Scm_GaucheInternalModule()); |
| 839 |
Scm_ApplyRec4(proc, obj, SCM_OBJ(port), |
| 840 |
SCM_OBJ(s->sharedTable), SCM_OBJ(ctx->controls)); |
| 841 |
} else { |
| 842 |
write_rec(obj, port, ctx); |
| 843 |
} |
| 844 |
cleanup_port_write_state(port); |
| 845 |
} |
| 846 |
|
| 847 |
/*OBSOLETED*/ |
| 848 |
/*format is now in Scheme (libfmt.scm).*/ |
| 849 |
void Scm_Format(ScmPort *out SCM_UNUSED, |
| 850 |
ScmString *fmt SCM_UNUSED, |
| 851 |
ScmObj args SCM_UNUSED, |
| 852 |
int sharedp SCM_UNUSED) |
| 853 |
{ |
| 854 |
Scm_Error("Scm_Format is obsoleted"); |
| 855 |
} |
| 856 |
|
| 857 |
/* |
| 858 |
* Printf()-like formatters |
| 859 |
* |
| 860 |
* These functions are familiar to C-programmers. The differences |
| 861 |
* from C's printf() family are: |
| 862 |
* |
| 863 |
* - The first argument must be Scheme output port. |
| 864 |
* - In the format string, the following conversion directives can |
| 865 |
* be used, as well as the standard printf() directives: |
| 866 |
* |
| 867 |
* %[width][.prec]S - The corresponding argument must be |
| 868 |
* ScmObj, which is written out by WRITE |
| 869 |
* mode. If width is specified and no |
| 870 |
* prec is given, the output is padded |
| 871 |
* if it is shorter than width. If both |
| 872 |
* width and prec are given, the output |
| 873 |
* is truncated if it is wider than width. |
| 874 |
* |
| 875 |
* %[width][.prec]A - Same as %S, but use DISPLAY mode. |
| 876 |
* |
| 877 |
* %W - Call pprint. |
| 878 |
* |
| 879 |
* %C - Take ScmChar argument and outputs it. |
| 880 |
* |
| 881 |
* A pound flag '#' for the S directive causes circular-safe output. |
| 882 |
* |
| 883 |
* NB: %A is taken by C99 for hexadecimal output of double numbers. |
| 884 |
* We'll introduce a flag for S directive to use DISPLAY mode, and will |
| 885 |
* move away from %A in future. |
| 886 |
*/ |
| 887 |
|
| 888 |
/* NB: Scm_Vprintf scans format string twice. In the first pass, arguments |
| 889 |
* are retrieved from va_list variable and pushed to a list. In the second |
| 890 |
* pass, they are printed according to the format string. |
| 891 |
* It is necessary because we need to do the printing part within a closure |
| 892 |
* called by Scm_WithPortLocking. On some architecture, we can't pass |
| 893 |
* va_list type of argument in a closure packet easily. |
| 894 |
*/ |
| 895 |
/* NB: It is tempting to write the logic in Scheme, but we keep the main |
| 896 |
* feature in C so that Scm_Printf is available to debug Scheme code. |
| 897 |
* The exception is %W which need to call pprint. |
| 898 |
*/ |
| 899 |
|
| 900 |
/* Pass 1. Pop vararg and make a list of arguments. |
| 901 |
* NB: If we're "walking" pass, and the argument is a Lisp object, |
| 902 |
* we recurse to it in this pass. |
| 903 |
* NB: Without NOINLINE, gcc inlines this in Scm_Vprintf which causes warning |
| 904 |
* of register variable clobbering by longjmp. |
| 905 |
*/ |
| 906 |
static ScmObj SCM_NOINLINE vprintf_pass1(ScmPort *out, |
| 907 |
const char *fmt, |
| 908 |
va_list ap) |
| 909 |
{ |
| 910 |
ScmObj h = SCM_NIL, t = SCM_NIL; |
| 911 |
const char *fmtp = fmt; |
| 912 |
int c, longp; |
| 913 |
|
| 914 |
while ((c = *fmtp++) != 0) { |
| 915 |
if (c != '%') continue; |
| 916 |
longp = FALSE; |
| 917 |
while ((c = *fmtp++) != 0) { |
| 918 |
switch (c) { |
| 919 |
case 'd': case 'i': case 'c': |
| 920 |
if (longp) { |
| 921 |
signed long val = va_arg(ap, signed long); |
| 922 |
SCM_APPEND1(h, t, Scm_MakeInteger(val)); |
| 923 |
} else { |
| 924 |
signed int val = va_arg(ap, signed int); |
| 925 |
SCM_APPEND1(h, t, Scm_MakeInteger(val)); |
| 926 |
} |
| 927 |
break; |
| 928 |
case 'o': case 'u': case 'x': case 'X': |
| 929 |
if (longp) { |
| 930 |
unsigned long val = va_arg(ap, unsigned long); |
| 931 |
SCM_APPEND1(h, t, Scm_MakeIntegerU(val)); |
| 932 |
} else { |
| 933 |
unsigned int val = va_arg(ap, unsigned int); |
| 934 |
SCM_APPEND1(h, t, Scm_MakeIntegerU(val)); |
| 935 |
} |
| 936 |
break; |
| 937 |
case 'e': case 'E': case 'f': case 'g': case 'G': |
| 938 |
{ |
| 939 |
double val = va_arg(ap, double); |
| 940 |
SCM_APPEND1(h, t, Scm_MakeFlonum(val)); |
| 941 |
break; |
| 942 |
} |
| 943 |
case 's': |
| 944 |
{ |
| 945 |
char *val = va_arg(ap, char *); |
| 946 |
/* for safety */ |
| 947 |
if (val != NULL) SCM_APPEND1(h, t, SCM_MAKE_STR(val)); |
| 948 |
else SCM_APPEND1(h, t, SCM_MAKE_STR("(null)")); |
| 949 |
break; |
| 950 |
} |
| 951 |
case '%': break; |
| 952 |
case 'p': |
| 953 |
{ |
| 954 |
void *val = va_arg(ap, void *); |
| 955 |
SCM_APPEND1(h, t, Scm_MakeIntegerU((u_long)(intptr_t)val)); |
| 956 |
break; |
| 957 |
} |
| 958 |
case 'S': case 'A': case 'W': |
| 959 |
{ |
| 960 |
ScmObj o = va_arg(ap, ScmObj); |
| 961 |
SCM_APPEND1(h, t, o); |
| 962 |
if (PORT_WALKER_P(out)) write_walk(o, out); |
| 963 |
break; |
| 964 |
} |
| 965 |
case 'C': |
| 966 |
{ |
| 967 |
ScmChar c = va_arg(ap, ScmChar); |
| 968 |
SCM_APPEND1(h, t, Scm_MakeInteger(c)); |
| 969 |
break; |
| 970 |
} |
| 971 |
case '*': |
| 972 |
{ |
| 973 |
int c = va_arg(ap, int); |
| 974 |
SCM_APPEND1(h, t, Scm_MakeInteger(c)); |
| 975 |
continue; |
| 976 |
} |
| 977 |
case 'l': |
| 978 |
longp = TRUE; |
| 979 |
continue; |
| 980 |
default: |
| 981 |
continue; |
| 982 |
} |
| 983 |
break; |
| 984 |
} |
| 985 |
if (c == 0) { |
| 986 |
Scm_Error("incomplete %%-directive in format string: %s", fmt); |
| 987 |
} |
| 988 |
} |
| 989 |
return h; |
| 990 |
} |
| 991 |
|
| 992 |
/* Pass 2. */ |
| 993 |
static void vprintf_pass2(ScmPort *out, const char *fmt, ScmObj args) |
| 994 |
{ |
| 995 |
const char *fmtp = fmt; |
| 996 |
ScmDString argbuf; |
| 997 |
char buf[SPBUFSIZ]; |
| 998 |
int c; |
| 999 |
|
| 1000 |
while ((c = *fmtp++) != 0) { |
| 1001 |
int width, prec, dot_appeared, pound_appeared; |
| 1002 |
|
| 1003 |
if (c != '%') { |
| 1004 |
Scm_PutcUnsafe(c, out); |
| 1005 |
continue; |
| 1006 |
} |
| 1007 |
|
| 1008 |
Scm_DStringInit(&argbuf); |
| 1009 |
SCM_DSTRING_PUTB(&argbuf, c); |
| 1010 |
width = 0, prec = 0, dot_appeared = 0, pound_appeared = 0; |
| 1011 |
while ((c = *fmtp++) != 0) { |
| 1012 |
switch (c) { |
| 1013 |
case 'l': |
| 1014 |
SCM_DSTRING_PUTB(&argbuf, c); |
| 1015 |
continue; |
| 1016 |
case 'd': case 'i': case 'c': |
| 1017 |
{ |
| 1018 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1019 |
ScmObj val = SCM_CAR(args); |
| 1020 |
args = SCM_CDR(args); |
| 1021 |
SCM_ASSERT(SCM_EXACTP(val)); |
| 1022 |
SCM_DSTRING_PUTB(&argbuf, c); |
| 1023 |
SCM_DSTRING_PUTB(&argbuf, 0); |
| 1024 |
snprintf(buf, SPBUFSIZ, Scm_DStringGetz(&argbuf), |
| 1025 |
Scm_GetInteger(val)); |
| 1026 |
Scm_PutzUnsafe(buf, -1, out); |
| 1027 |
break; |
| 1028 |
} |
| 1029 |
case 'o': case 'u': case 'x': case 'X': |
| 1030 |
{ |
| 1031 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1032 |
ScmObj val = SCM_CAR(args); |
| 1033 |
args = SCM_CDR(args); |
| 1034 |
SCM_ASSERT(SCM_EXACTP(val)); |
| 1035 |
SCM_DSTRING_PUTB(&argbuf, c); |
| 1036 |
SCM_DSTRING_PUTB(&argbuf, 0); |
| 1037 |
snprintf(buf, SPBUFSIZ, Scm_DStringGetz(&argbuf), |
| 1038 |
Scm_GetUInteger(val)); |
| 1039 |
Scm_PutzUnsafe(buf, -1, out); |
| 1040 |
break; |
| 1041 |
} |
| 1042 |
case 'e': case 'E': case 'f': case 'g': case 'G': |
| 1043 |
{ |
| 1044 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1045 |
ScmObj val = SCM_CAR(args); |
| 1046 |
args = SCM_CDR(args); |
| 1047 |
SCM_ASSERT(SCM_FLONUMP(val)); |
| 1048 |
SCM_DSTRING_PUTB(&argbuf, c); |
| 1049 |
SCM_DSTRING_PUTB(&argbuf, 0); |
| 1050 |
snprintf(buf, SPBUFSIZ, Scm_DStringGetz(&argbuf), |
| 1051 |
Scm_GetDouble(val)); |
| 1052 |
Scm_PutzUnsafe(buf, -1, out); |
| 1053 |
break; |
| 1054 |
} |
| 1055 |
case 's': |
| 1056 |
{ |
| 1057 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1058 |
ScmObj val = SCM_CAR(args); |
| 1059 |
args = SCM_CDR(args); |
| 1060 |
SCM_ASSERT(SCM_STRINGP(val)); |
| 1061 |
Scm_PutsUnsafe(SCM_STRING(val), out); |
| 1062 |
|
| 1063 |
/* TODO: support right adjustment such as %-10s. |
| 1064 |
Currently we ignore minus sign and pad chars |
| 1065 |
on the right. */ |
| 1066 |
for (int len = SCM_STRING_BODY_LENGTH(SCM_STRING_BODY(val)); |
| 1067 |
len < width; |
| 1068 |
len++) { |
| 1069 |
Scm_PutcUnsafe(' ', out); |
| 1070 |
} |
| 1071 |
break; |
| 1072 |
} |
| 1073 |
case '%': |
| 1074 |
{ |
| 1075 |
Scm_PutcUnsafe('%', out); |
| 1076 |
break; |
| 1077 |
} |
| 1078 |
case 'p': |
| 1079 |
{ |
| 1080 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1081 |
ScmObj val = SCM_CAR(args); |
| 1082 |
args = SCM_CDR(args); |
| 1083 |
SCM_ASSERT(SCM_EXACTP(val)); |
| 1084 |
SCM_DSTRING_PUTB(&argbuf, c); |
| 1085 |
SCM_DSTRING_PUTB(&argbuf, 0); |
| 1086 |
snprintf(buf, SPBUFSIZ, Scm_DStringGetz(&argbuf), |
| 1087 |
(void*)(intptr_t)Scm_GetUInteger(val)); |
| 1088 |
Scm_PutzUnsafe(buf, -1, out); |
| 1089 |
break; |
| 1090 |
} |
| 1091 |
case 'S': case 'A': |
| 1092 |
{ |
| 1093 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1094 |
ScmObj val = SCM_CAR(args); |
| 1095 |
args = SCM_CDR(args); |
| 1096 |
|
| 1097 |
int mode = (pound_appeared |
| 1098 |
? SCM_WRITE_SHARED |
| 1099 |
: ((c == 'A') |
| 1100 |
? SCM_WRITE_DISPLAY |
| 1101 |
: SCM_WRITE_WRITE)); |
| 1102 |
if (width <= 0) { |
| 1103 |
Scm_Write(val, SCM_OBJ(out), mode); |
| 1104 |
} if (prec <= 0) { |
| 1105 |
Scm_WriteLimited(val, SCM_OBJ(out), mode, width); |
| 1106 |
} else { |
| 1107 |
ScmObj buf = Scm_MakeOutputStringPort(TRUE); |
| 1108 |
Scm_WriteLimited(val, buf, mode, width); |
| 1109 |
ScmObj s = Scm_GetOutputString(SCM_PORT(buf), 0); |
| 1110 |
Scm_PutsUnsafe(SCM_STRING(s), out); |
| 1111 |
ScmSmallInt n = SCM_STRING_LENGTH(s); |
| 1112 |
if (n > width) { |
| 1113 |
if (double_quote_closed_p(SCM_STRING(s))) |
| 1114 |
Scm_PutzUnsafe(" " SCM_WRITTEN_ELLIPSIS, |
| 1115 |
-1, out); |
| 1116 |
else |
| 1117 |
Scm_PutzUnsafe("\"" SCM_WRITTEN_ELLIPSIS, |
| 1118 |
-1, out); |
| 1119 |
} else { |
| 1120 |
for (; n < prec; n++) Scm_PutcUnsafe(' ', out); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
break; |
| 1124 |
} |
| 1125 |
case 'W': |
| 1126 |
{ |
| 1127 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1128 |
ScmObj val = SCM_CAR(args); |
| 1129 |
args = SCM_CDR(args); |
| 1130 |
call_pprint(val, out); |
| 1131 |
break; |
| 1132 |
} |
| 1133 |
case 'C': |
| 1134 |
{ |
| 1135 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1136 |
ScmObj val = SCM_CAR(args); |
| 1137 |
args = SCM_CDR(args); |
| 1138 |
SCM_ASSERT(SCM_EXACTP(val)); |
| 1139 |
Scm_PutcUnsafe(Scm_GetInteger(val), out); |
| 1140 |
break; |
| 1141 |
} |
| 1142 |
case '0': case '1': case '2': case '3': case '4': |
| 1143 |
case '5': case '6': case '7': case '8': case '9': |
| 1144 |
if (dot_appeared) { |
| 1145 |
prec = prec*10 + (c - '0'); |
| 1146 |
} else { |
| 1147 |
width = width*10 + (c - '0'); |
| 1148 |
} |
| 1149 |
goto fallback; |
| 1150 |
case '.': |
| 1151 |
dot_appeared++; |
| 1152 |
goto fallback; |
| 1153 |
case '#': |
| 1154 |
pound_appeared++; |
| 1155 |
goto fallback; |
| 1156 |
case '*': |
| 1157 |
SCM_ASSERT(SCM_PAIRP(args)); |
| 1158 |
if (dot_appeared) { |
| 1159 |
prec = Scm_GetInteger(SCM_CAR(args)); |
| 1160 |
} else { |
| 1161 |
width = Scm_GetInteger(SCM_CAR(args)); |
| 1162 |
} |
| 1163 |
args = SCM_CDR(args); |
| 1164 |
goto fallback; |
| 1165 |
fallback: |
| 1166 |
default: |
| 1167 |
SCM_DSTRING_PUTB(&argbuf, c); |
| 1168 |
continue; |
| 1169 |
} |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
if (c == 0) { |
| 1173 |
Scm_Error("incomplete %%-directive in format string: %s", fmt); |
| 1174 |
} |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* NB: libfmt has similar routine (quote-terminated?) in 'chop-and-out'. |
| 1179 |
Since Scm_Printf can be called during initialiation before Scheme runtime |
| 1180 |
is fully up, we need this implemented in C. */ |
| 1181 |
static int double_quote_closed_p(ScmString *s) |
| 1182 |
{ |
| 1183 |
ScmSmallInt siz; |
| 1184 |
const char *p = Scm_GetStringContent(s, &siz, NULL, NULL); |
| 1185 |
int inside = FALSE; |
| 1186 |
int escaped = FALSE; |
| 1187 |
for (int i = 0; i < siz; i++, p++) { |
| 1188 |
if (!escaped) { |
| 1189 |
if (*p == '"') { |
| 1190 |
inside = !inside; |
| 1191 |
} |
| 1192 |
if (*p == '\\') { |
| 1193 |
escaped = TRUE; |
| 1194 |
} else { |
| 1195 |
escaped = FALSE; |
| 1196 |
} |
| 1197 |
} else { |
| 1198 |
escaped = FALSE; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
return !inside; |
| 1202 |
} |
| 1203 |
|
| 1204 |
static void call_pprint(ScmObj obj, ScmPort *out) |
| 1205 |
{ |
| 1206 |
ScmWriteControls *wc = Scm_MakeWriteControls(NULL); |
| 1207 |
wc->printPretty = TRUE; |
| 1208 |
wc->printWidth = 79; |
| 1209 |
wc->printIndent = Scm_PortColumn(out); |
| 1210 |
Scm_WriteWithControls(obj, SCM_OBJ(out), SCM_WRITE_WRITE, wc); |
| 1211 |
} |
| 1212 |
|
| 1213 |
/* Public APIs */ |
| 1214 |
|
| 1215 |
void Scm_Vprintf(ScmPort *out, const char *fmt, va_list ap, |
| 1216 |
int sharedp SCM_UNUSED) |
| 1217 |
{ |
| 1218 |
if (!SCM_OPORTP(out)) Scm_Error("output port required, but got %S", out); |
| 1219 |
|
| 1220 |
/* TODO: handle sharedp */ |
| 1221 |
ScmObj args = vprintf_pass1(out, fmt, ap); |
| 1222 |
|
| 1223 |
ScmVM *vm = Scm_VM(); |
| 1224 |
PORT_LOCK(out, vm); |
| 1225 |
PORT_SAFE_CALL(out, vprintf_pass2(out, fmt, args), /*no cleanup*/); |
| 1226 |
PORT_UNLOCK(out); |
| 1227 |
} |
| 1228 |
|
| 1229 |
void Scm_Printf(ScmPort *out, const char *fmt, ...) |
| 1230 |
{ |
| 1231 |
va_list ap; |
| 1232 |
va_start(ap, fmt); |
| 1233 |
Scm_Vprintf(out, fmt, ap, FALSE); |
| 1234 |
va_end(ap); |
| 1235 |
} |
| 1236 |
|
| 1237 |
void Scm_PrintfShared(ScmPort *out, const char *fmt, ...) |
| 1238 |
{ |
| 1239 |
va_list ap; |
| 1240 |
va_start(ap, fmt); |
| 1241 |
Scm_Vprintf(out, fmt, ap, TRUE); |
| 1242 |
va_end(ap); |
| 1243 |
} |
| 1244 |
|
| 1245 |
ScmObj Scm_Sprintf(const char *fmt, ...) |
| 1246 |
{ |
| 1247 |
ScmObj r; |
| 1248 |
va_list args; |
| 1249 |
va_start(args, fmt); |
| 1250 |
r = Scm_Vsprintf(fmt, args, FALSE); |
| 1251 |
va_end(args); |
| 1252 |
return r; |
| 1253 |
} |
| 1254 |
|
| 1255 |
ScmObj Scm_SprintfShared(const char *fmt, ...) |
| 1256 |
{ |
| 1257 |
va_list args; |
| 1258 |
va_start(args, fmt); |
| 1259 |
ScmObj r = Scm_Vsprintf(fmt, args, TRUE); |
| 1260 |
va_end(args); |
| 1261 |
return r; |
| 1262 |
} |
| 1263 |
|
| 1264 |
ScmObj Scm_Vsprintf(const char *fmt, va_list ap, int sharedp) |
| 1265 |
{ |
| 1266 |
ScmObj ostr = Scm_MakeOutputStringPort(TRUE); |
| 1267 |
Scm_Vprintf(SCM_PORT(ostr), fmt, ap, sharedp); |
| 1268 |
return Scm_GetOutputString(SCM_PORT(ostr), 0); |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* |
| 1272 |
* Initialization |
| 1273 |
*/ |
| 1274 |
void Scm__InitWrite(void) |
| 1275 |
{ |
| 1276 |
Scm_InitBuiltinGeneric(&Scm_GenericWriteObject, "write-object", |
| 1277 |
Scm_GaucheModule()); |
| 1278 |
defaultWriteControls = Scm_MakeWriteControls(NULL); |
| 1279 |
} |