| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* print.d - print |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <ecl/ecl.h> |
| 17 |
#include <ecl/internal.h> |
| 18 |
|
| 19 |
cl_object |
| 20 |
_ecl_stream_or_default_output(cl_object stream) |
| 21 |
{ |
| 22 |
if (Null(stream)) |
| 23 |
return ecl_symbol_value(@'*standard-output*'); |
| 24 |
else if (stream == ECL_T) |
| 25 |
return ecl_symbol_value(@'*terminal-io*'); |
| 26 |
return stream; |
| 27 |
} |
| 28 |
|
| 29 |
int |
| 30 |
ecl_print_base(void) |
| 31 |
{ |
| 32 |
const cl_env_ptr the_env = ecl_process_env(); |
| 33 |
cl_object object = ecl_cmp_symbol_value(the_env, @'*print-base*'); |
| 34 |
cl_fixnum base; |
| 35 |
unlikely_if (!ECL_FIXNUMP(object) || (base = ecl_fixnum(object)) < 2 || base > 36) { |
| 36 |
ECL_SETQ(the_env, @'*print-base*', ecl_make_fixnum(10)); |
| 37 |
FEerror("The value of *PRINT-BASE*~% ~S~%" |
| 38 |
"is not of the expected type (INTEGER 2 36)", 1, object); |
| 39 |
} |
| 40 |
return base; |
| 41 |
} |
| 42 |
|
| 43 |
cl_fixnum |
| 44 |
ecl_print_level(void) |
| 45 |
{ |
| 46 |
const cl_env_ptr the_env = ecl_process_env(); |
| 47 |
cl_object object = ecl_cmp_symbol_value(the_env, @'*print-level*'); |
| 48 |
cl_fixnum level; |
| 49 |
if (object == ECL_NIL) { |
| 50 |
level = MOST_POSITIVE_FIXNUM; |
| 51 |
} else if (ECL_FIXNUMP(object)) { |
| 52 |
level = ecl_fixnum(object); |
| 53 |
if (level < 0) { |
| 54 |
ERROR: ECL_SETQ(the_env, @'*print-level*', ECL_NIL); |
| 55 |
FEerror("The value of *PRINT-LEVEL*~% ~S~%" |
| 56 |
"is not of the expected type (OR NULL (INTEGER 0 *))", |
| 57 |
1, object); |
| 58 |
} |
| 59 |
} else if (ecl_unlikely(!ECL_BIGNUMP(object))) { |
| 60 |
goto ERROR; |
| 61 |
} else { |
| 62 |
level = MOST_POSITIVE_FIXNUM; |
| 63 |
} |
| 64 |
return level; |
| 65 |
} |
| 66 |
|
| 67 |
cl_fixnum |
| 68 |
ecl_print_length(void) |
| 69 |
{ |
| 70 |
const cl_env_ptr the_env = ecl_process_env(); |
| 71 |
cl_object object = ecl_cmp_symbol_value(the_env, @'*print-length*'); |
| 72 |
cl_fixnum length; |
| 73 |
if (object == ECL_NIL) { |
| 74 |
length = MOST_POSITIVE_FIXNUM; |
| 75 |
} else if (ECL_FIXNUMP(object)) { |
| 76 |
length = ecl_fixnum(object); |
| 77 |
unlikely_if (length < 0) { |
| 78 |
ERROR: ECL_SETQ(the_env, @'*print-length*', ECL_NIL); |
| 79 |
FEerror("The value of *PRINT-LENGTH*~% ~S~%" |
| 80 |
"is not of the expected type (OR NULL (INTEGER 0 *))", |
| 81 |
1, object); |
| 82 |
} |
| 83 |
} else if (ecl_unlikely(!ECL_BIGNUMP(object))) { |
| 84 |
goto ERROR; |
| 85 |
} else { |
| 86 |
length = MOST_POSITIVE_FIXNUM; |
| 87 |
} |
| 88 |
return length; |
| 89 |
} |
| 90 |
|
| 91 |
bool |
| 92 |
ecl_print_radix(void) |
| 93 |
{ |
| 94 |
return ecl_symbol_value(@'*print-radix*') != ECL_NIL; |
| 95 |
} |
| 96 |
|
| 97 |
cl_object |
| 98 |
ecl_print_case(void) |
| 99 |
{ |
| 100 |
const cl_env_ptr the_env = ecl_process_env(); |
| 101 |
cl_object output = ecl_cmp_symbol_value(the_env, @'*print-case*'); |
| 102 |
unlikely_if (output != @':upcase' && |
| 103 |
output != @':downcase' && |
| 104 |
output != @':capitalize') |
| 105 |
{ |
| 106 |
ECL_SETQ(the_env, @'*print-case*', @':downcase'); |
| 107 |
FEerror("The value of *PRINT-CASE*~% ~S~%" |
| 108 |
"is not of the expected type " |
| 109 |
"(MEMBER :UPCASE :DOWNCASE :CAPITALIZE)", 1, output); |
| 110 |
} |
| 111 |
return output; |
| 112 |
} |
| 113 |
|
| 114 |
bool |
| 115 |
ecl_print_gensym(void) |
| 116 |
{ |
| 117 |
return ecl_symbol_value(@'*print-gensym*') != ECL_NIL; |
| 118 |
} |
| 119 |
|
| 120 |
bool |
| 121 |
ecl_print_array(void) |
| 122 |
{ |
| 123 |
return ecl_symbol_value(@'*print-array*') != ECL_NIL; |
| 124 |
} |
| 125 |
|
| 126 |
bool |
| 127 |
ecl_print_readably(void) |
| 128 |
{ |
| 129 |
return ecl_symbol_value(@'*print-readably*') != ECL_NIL; |
| 130 |
} |
| 131 |
|
| 132 |
bool |
| 133 |
ecl_print_escape(void) |
| 134 |
{ |
| 135 |
return ecl_symbol_value(@'*print-escape*') != ECL_NIL; |
| 136 |
} |
| 137 |
|
| 138 |
bool |
| 139 |
ecl_print_circle(void) |
| 140 |
{ |
| 141 |
return ecl_symbol_value(@'*print-circle*') != ECL_NIL; |
| 142 |
} |
| 143 |
|
| 144 |
@(defun write (x |
| 145 |
&key ((:stream strm) ECL_NIL) |
| 146 |
(array ecl_cmp_symbol_value(the_env, @'*print-array*')) |
| 147 |
(base ecl_cmp_symbol_value(the_env, @'*print-base*')) |
| 148 |
((:case cas) ecl_cmp_symbol_value(the_env, @'*print-case*')) |
| 149 |
(circle ecl_cmp_symbol_value(the_env, @'*print-circle*')) |
| 150 |
(escape ecl_cmp_symbol_value(the_env, @'*print-escape*')) |
| 151 |
(gensym ecl_cmp_symbol_value(the_env, @'*print-gensym*')) |
| 152 |
(length ecl_cmp_symbol_value(the_env, @'*print-length*')) |
| 153 |
(level ecl_cmp_symbol_value(the_env, @'*print-level*')) |
| 154 |
(lines ecl_cmp_symbol_value(the_env, @'*print-lines*')) |
| 155 |
(miser_width ecl_cmp_symbol_value(the_env, @'*print-miser-width*')) |
| 156 |
(pprint_dispatch ecl_cmp_symbol_value(the_env, @'*print-pprint-dispatch*')) |
| 157 |
(pretty ecl_cmp_symbol_value(the_env, @'*print-pretty*')) |
| 158 |
(radix ecl_cmp_symbol_value(the_env, @'*print-radix*')) |
| 159 |
(readably ecl_cmp_symbol_value(the_env, @'*print-readably*')) |
| 160 |
(right_margin ecl_cmp_symbol_value(the_env, @'*print-right-margin*'))) |
| 161 |
@ |
| 162 |
ecl_bds_bind(the_env, @'*print-array*', array); |
| 163 |
ecl_bds_bind(the_env, @'*print-base*', base); |
| 164 |
ecl_bds_bind(the_env, @'*print-case*', cas); |
| 165 |
ecl_bds_bind(the_env, @'*print-circle*', circle); |
| 166 |
ecl_bds_bind(the_env, @'*print-escape*', escape); |
| 167 |
ecl_bds_bind(the_env, @'*print-gensym*', gensym); |
| 168 |
ecl_bds_bind(the_env, @'*print-level*', level); |
| 169 |
ecl_bds_bind(the_env, @'*print-length*', length); |
| 170 |
ecl_bds_bind(the_env, @'*print-lines*', lines); |
| 171 |
ecl_bds_bind(the_env, @'*print-miser-width*', miser_width); |
| 172 |
ecl_bds_bind(the_env, @'*print-pprint-dispatch*', pprint_dispatch); |
| 173 |
ecl_bds_bind(the_env, @'*print-pretty*', pretty); |
| 174 |
ecl_bds_bind(the_env, @'*print-radix*', radix); |
| 175 |
ecl_bds_bind(the_env, @'*print-readably*', readably); |
| 176 |
ecl_bds_bind(the_env, @'*print-right-margin*', right_margin); |
| 177 |
|
| 178 |
strm = _ecl_stream_or_default_output(strm); |
| 179 |
si_write_object(x, strm); |
| 180 |
ecl_force_output(strm); |
| 181 |
|
| 182 |
ecl_bds_unwind_n(the_env, 15); |
| 183 |
@(return x); |
| 184 |
@) |
| 185 |
|
| 186 |
@(defun prin1 (obj &optional strm) |
| 187 |
@ |
| 188 |
ecl_prin1(obj, strm); |
| 189 |
@(return obj); |
| 190 |
@) |
| 191 |
|
| 192 |
@(defun print (obj &optional strm) |
| 193 |
@ |
| 194 |
ecl_print(obj, strm); |
| 195 |
@(return obj); |
| 196 |
@) |
| 197 |
|
| 198 |
@(defun pprint (obj &optional strm) |
| 199 |
@ |
| 200 |
strm = _ecl_stream_or_default_output(strm); |
| 201 |
ecl_bds_bind(the_env, @'*print-escape*', ECL_T); |
| 202 |
ecl_bds_bind(the_env, @'*print-pretty*', ECL_T); |
| 203 |
ecl_write_char('\n', strm); |
| 204 |
si_write_object(obj, strm); |
| 205 |
ecl_force_output(strm); |
| 206 |
ecl_bds_unwind_n(the_env, 2); |
| 207 |
@(return); |
| 208 |
@) |
| 209 |
|
| 210 |
@(defun princ (obj &optional strm) |
| 211 |
@ |
| 212 |
ecl_princ(obj, strm); |
| 213 |
@(return obj); |
| 214 |
@) |
| 215 |
|
| 216 |
@(defun write-char (c &optional strm) |
| 217 |
@ |
| 218 |
/* INV: ecl_char_code() checks the type of `c' */ |
| 219 |
strm = _ecl_stream_or_default_output(strm); |
| 220 |
c = ECL_CODE_CHAR(ecl_write_char(ecl_char_code(c), strm)); |
| 221 |
@(return c); |
| 222 |
@) |
| 223 |
|
| 224 |
@(defun write-string (strng &o strm &k (start ecl_make_fixnum(0)) end) |
| 225 |
@ |
| 226 |
unlikely_if (!ECL_STRINGP(strng)) |
| 227 |
FEwrong_type_nth_arg(@[write-string], 1, strng, @[string]); |
| 228 |
strm = _ecl_stream_or_default_output(strm); |
| 229 |
/* Optimization: directly call stream-write-string for CLOS streams */ |
| 230 |
#ifdef ECL_CLOS_STREAMS |
| 231 |
if (!ECL_ANSI_STREAM_P(strm)) |
| 232 |
_ecl_funcall5(@'gray::stream-write-string', strm, strng, start, end); |
| 233 |
else |
| 234 |
#endif |
| 235 |
si_do_write_sequence(strng, strm, start, end); |
| 236 |
@(return strng); |
| 237 |
@) |
| 238 |
|
| 239 |
@(defun write-line (strng &o strm &k (start ecl_make_fixnum(0)) end) |
| 240 |
@ |
| 241 |
unlikely_if (!ECL_STRINGP(strng)) |
| 242 |
FEwrong_type_nth_arg(@[write-line], 1, strng, @[string]); |
| 243 |
strm = _ecl_stream_or_default_output(strm); |
| 244 |
/* Optimization: directly call stream-write-string for CLOS streams */ |
| 245 |
#ifdef ECL_CLOS_STREAMS |
| 246 |
if (!ECL_ANSI_STREAM_P(strm)) |
| 247 |
_ecl_funcall5(@'gray::stream-write-string', strm, strng, |
| 248 |
start, end); |
| 249 |
else |
| 250 |
#endif |
| 251 |
si_do_write_sequence(strng, strm, start, end); |
| 252 |
ecl_terpri(strm); |
| 253 |
@(return strng); |
| 254 |
@) |
| 255 |
|
| 256 |
@(defun terpri (&optional strm) |
| 257 |
@ |
| 258 |
ecl_terpri(strm); |
| 259 |
@(return ECL_NIL); |
| 260 |
@) |
| 261 |
|
| 262 |
@(defun fresh-line (&optional strm) |
| 263 |
@ |
| 264 |
strm = _ecl_stream_or_default_output(strm); |
| 265 |
#ifdef ECL_CLOS_STREAMS |
| 266 |
if (!ECL_ANSI_STREAM_P(strm)) { |
| 267 |
return _ecl_funcall2(@'gray::stream-fresh-line', strm); |
| 268 |
} |
| 269 |
#endif |
| 270 |
if (ecl_file_column(strm) == 0) { |
| 271 |
@(return ECL_NIL); |
| 272 |
} |
| 273 |
ecl_write_char('\n', strm); |
| 274 |
ecl_force_output(strm); |
| 275 |
@(return ECL_T); |
| 276 |
@) |
| 277 |
|
| 278 |
@(defun finish-output (&o strm) |
| 279 |
@ |
| 280 |
strm = _ecl_stream_or_default_output(strm); |
| 281 |
#ifdef ECL_CLOS_STREAMS |
| 282 |
if (!ECL_ANSI_STREAM_P(strm)) { |
| 283 |
return _ecl_funcall2(@'gray::stream-finish-output', strm); |
| 284 |
} |
| 285 |
#endif |
| 286 |
ecl_finish_output(strm); |
| 287 |
@(return ECL_NIL); |
| 288 |
@) |
| 289 |
|
| 290 |
@(defun force-output (&o strm) |
| 291 |
@ |
| 292 |
strm = _ecl_stream_or_default_output(strm); |
| 293 |
ecl_force_output(strm); |
| 294 |
@(return ECL_NIL); |
| 295 |
@) |
| 296 |
|
| 297 |
@(defun clear-output (&o strm) |
| 298 |
@ |
| 299 |
strm = _ecl_stream_or_default_output(strm); |
| 300 |
ecl_clear_output(strm); |
| 301 |
@(return ECL_NIL); |
| 302 |
@) |
| 303 |
|
| 304 |
cl_object |
| 305 |
cl_write_byte(cl_object integer, cl_object binary_output_stream) |
| 306 |
{ |
| 307 |
ecl_write_byte(integer, binary_output_stream); |
| 308 |
@(return integer); |
| 309 |
} |
| 310 |
|
| 311 |
@(defun write-sequence (sequence stream &key (start ecl_make_fixnum(0)) end) |
| 312 |
@ |
| 313 |
#ifdef ECL_CLOS_STREAMS |
| 314 |
if (!ECL_ANSI_STREAM_P(stream)) { |
| 315 |
return _ecl_funcall5(@'gray::stream-write-sequence', |
| 316 |
stream, sequence, start, end); |
| 317 |
} else |
| 318 |
#endif |
| 319 |
return si_do_write_sequence(sequence, stream, start, end); |
| 320 |
@) |
| 321 |
|
| 322 |
cl_object |
| 323 |
ecl_princ(cl_object obj, cl_object strm) |
| 324 |
{ |
| 325 |
const cl_env_ptr the_env = ecl_process_env(); |
| 326 |
strm = _ecl_stream_or_default_output(strm); |
| 327 |
ecl_bds_bind(the_env, @'*print-escape*', ECL_NIL); |
| 328 |
ecl_bds_bind(the_env, @'*print-readably*', ECL_NIL); |
| 329 |
si_write_object(obj, strm); |
| 330 |
ecl_bds_unwind_n(the_env, 2); |
| 331 |
return obj; |
| 332 |
} |
| 333 |
|
| 334 |
cl_object |
| 335 |
ecl_prin1(cl_object obj, cl_object strm) |
| 336 |
{ |
| 337 |
const cl_env_ptr the_env = ecl_process_env(); |
| 338 |
strm = _ecl_stream_or_default_output(strm); |
| 339 |
ecl_bds_bind(the_env, @'*print-escape*', ECL_T); |
| 340 |
si_write_object(obj, strm); |
| 341 |
ecl_force_output(strm); |
| 342 |
ecl_bds_unwind1(the_env); |
| 343 |
return obj; |
| 344 |
} |
| 345 |
|
| 346 |
cl_object |
| 347 |
ecl_print(cl_object obj, cl_object strm) |
| 348 |
{ |
| 349 |
strm = _ecl_stream_or_default_output(strm); |
| 350 |
ecl_terpri(strm); |
| 351 |
ecl_prin1(obj, strm); |
| 352 |
ecl_princ_char(' ', strm); |
| 353 |
return obj; |
| 354 |
} |
| 355 |
|
| 356 |
cl_object |
| 357 |
ecl_terpri(cl_object strm) |
| 358 |
{ |
| 359 |
strm = _ecl_stream_or_default_output(strm); |
| 360 |
#ifdef ECL_CLOS_STREAMS |
| 361 |
if (!ECL_ANSI_STREAM_P(strm)) { |
| 362 |
return _ecl_funcall2(@'gray::stream-terpri', strm); |
| 363 |
} |
| 364 |
#endif |
| 365 |
ecl_write_char('\n', strm); |
| 366 |
ecl_force_output(strm); |
| 367 |
return(ECL_NIL); |
| 368 |
} |
| 369 |
|
| 370 |
void |
| 371 |
ecl_write_string(cl_object strng, cl_object strm) |
| 372 |
{ |
| 373 |
cl_write_string(2, strng, strm); |
| 374 |
ecl_force_output(strm); |
| 375 |
} |
| 376 |
|
| 377 |
/* |
| 378 |
THE ULTRA-SPECIAL-DINNER-SERVICE OPTIMIZATION |
| 379 |
*/ |
| 380 |
void |
| 381 |
ecl_princ_str(const char *s, cl_object strm) |
| 382 |
{ |
| 383 |
strm = _ecl_stream_or_default_output(strm); |
| 384 |
writestr_stream(s, strm); |
| 385 |
} |
| 386 |
|
| 387 |
int |
| 388 |
ecl_princ_char(int c, cl_object strm) |
| 389 |
{ |
| 390 |
strm = _ecl_stream_or_default_output(strm); |
| 391 |
ecl_write_char(c, strm); |
| 392 |
if (c == '\n') { |
| 393 |
ecl_force_output(strm); |
| 394 |
} |
| 395 |
return c; |
| 396 |
} |