| 1 | /* Copyright (C) 1995,1996,1998,1999 Free Software Foundation, Inc. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; see the file COPYING. If not, write to |
| 15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 16 | * Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * As a special exception, the Free Software Foundation gives permission |
| 19 | * for additional uses of the text contained in its release of GUILE. |
| 20 | * |
| 21 | * The exception is that, if you link the GUILE library with other files |
| 22 | * to produce an executable, this does not by itself cause the |
| 23 | * resulting executable to be covered by the GNU General Public License. |
| 24 | * Your use of that executable is in no way restricted on account of |
| 25 | * linking the GUILE library code into it. |
| 26 | * |
| 27 | * This exception does not however invalidate any other reasons why |
| 28 | * the executable file might be covered by the GNU General Public License. |
| 29 | * |
| 30 | * This exception applies only to the code released by the |
| 31 | * Free Software Foundation under the name GUILE. If you copy |
| 32 | * code from other Free Software Foundation releases into a copy of |
| 33 | * GUILE, as the General Public License permits, the exception does |
| 34 | * not apply to the code that you add in this way. To avoid misleading |
| 35 | * anyone as to the status of such modified files, you must delete |
| 36 | * this exception notice from them. |
| 37 | * |
| 38 | * If you write modifications of your own for GUILE, it is your choice |
| 39 | * whether to permit this exception to apply to your modifications. |
| 40 | * If you do not wish that, delete this exception notice. */ |
| 41 | \f |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include "_scm.h" |
| 45 | #include "unif.h" |
| 46 | #include "eval.h" |
| 47 | #include "read.h" |
| 48 | |
| 49 | #include "strports.h" |
| 50 | |
| 51 | #ifdef HAVE_STRING_H |
| 52 | #include <string.h> |
| 53 | #endif |
| 54 | |
| 55 | \f |
| 56 | |
| 57 | /* {Ports - string ports} |
| 58 | * |
| 59 | */ |
| 60 | |
| 61 | /* NOTES: |
| 62 | write_buf/write_end point to the ends of the allocated string. |
| 63 | read_buf/read_end in principle point to the part of the string which |
| 64 | has been written to, but this is only updated after a flush. |
| 65 | read_pos and write_pos in principle should be equal, but this is only true |
| 66 | when rw_active is 0. |
| 67 | */ |
| 68 | |
| 69 | static int |
| 70 | stfill_buffer (SCM port) |
| 71 | { |
| 72 | scm_port *pt = SCM_PTAB_ENTRY (port); |
| 73 | |
| 74 | if (pt->read_pos >= pt->read_end) |
| 75 | return EOF; |
| 76 | else |
| 77 | return scm_return_first (*pt->read_pos, port); |
| 78 | } |
| 79 | |
| 80 | /* change the size of a port's string to new_size. this doesn't |
| 81 | change read_buf_size. */ |
| 82 | static void |
| 83 | st_resize_port (scm_port *pt, off_t new_size) |
| 84 | { |
| 85 | off_t index = pt->write_pos - pt->write_buf; |
| 86 | |
| 87 | pt->write_buf_size = new_size; |
| 88 | |
| 89 | scm_vector_set_length_x (pt->stream, SCM_MAKINUM (new_size)); |
| 90 | |
| 91 | /* reset buffer in case reallocation moved the string. */ |
| 92 | { |
| 93 | pt->read_buf = pt->write_buf = SCM_CHARS (pt->stream); |
| 94 | pt->read_pos = pt->write_pos = pt->write_buf + index; |
| 95 | pt->write_end = pt->write_buf + pt->write_buf_size; |
| 96 | pt->read_end = pt->read_buf + pt->read_buf_size; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* amount by which write_buf is expanded. */ |
| 101 | #define SCM_WRITE_BLOCK 80 |
| 102 | |
| 103 | /* ensure that write_pos < write_end by enlarging the buffer when |
| 104 | necessary. update read_buf to account for written chars. */ |
| 105 | static void |
| 106 | st_flush (SCM port) |
| 107 | { |
| 108 | scm_port *pt = SCM_PTAB_ENTRY (port); |
| 109 | |
| 110 | if (pt->write_pos == pt->write_end) |
| 111 | { |
| 112 | st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK); |
| 113 | } |
| 114 | pt->read_pos = pt->write_pos; |
| 115 | if (pt->read_pos > pt->read_end) |
| 116 | { |
| 117 | pt->read_end = (unsigned char *) pt->read_pos; |
| 118 | pt->read_buf_size = pt->read_end - pt->read_buf; |
| 119 | } |
| 120 | pt->rw_active = 0; |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | st_read_flush (SCM port, int offset) |
| 125 | { |
| 126 | scm_port *pt = SCM_PTAB_ENTRY (port); |
| 127 | |
| 128 | pt->write_pos = (unsigned char *) pt->read_pos; |
| 129 | pt->rw_active = 0; |
| 130 | } |
| 131 | |
| 132 | static off_t |
| 133 | st_seek (SCM port, off_t offset, int whence) |
| 134 | { |
| 135 | scm_port *pt = SCM_PTAB_ENTRY (port); |
| 136 | off_t target; |
| 137 | |
| 138 | /* we can assume at this point that pt->write_pos == pt->read_pos. */ |
| 139 | switch (whence) |
| 140 | { |
| 141 | case SEEK_CUR: |
| 142 | target = pt->read_pos - pt->read_buf + offset; |
| 143 | break; |
| 144 | case SEEK_END: |
| 145 | target = pt->read_end - pt->read_buf + offset; |
| 146 | break; |
| 147 | default: /* SEEK_SET */ |
| 148 | target = offset; |
| 149 | break; |
| 150 | } |
| 151 | if (target < 0) |
| 152 | scm_misc_error ("st_seek", "negative offset", SCM_EOL); |
| 153 | if (target >= pt->write_buf_size) |
| 154 | { |
| 155 | if (!(SCM_CAR (port) & SCM_WRTNG)) |
| 156 | { |
| 157 | if (target > pt->write_buf_size) |
| 158 | { |
| 159 | scm_misc_error ("st_seek", "seek past end of read-only strport", |
| 160 | SCM_EOL); |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | st_resize_port (pt, target + (target == pt->write_buf_size |
| 166 | ? SCM_WRITE_BLOCK |
| 167 | : 0)); |
| 168 | } |
| 169 | } |
| 170 | pt->read_pos = pt->write_pos = pt->read_buf + target; |
| 171 | if (pt->read_pos > pt->read_end) |
| 172 | { |
| 173 | pt->read_end = (unsigned char *) pt->read_pos; |
| 174 | pt->read_buf_size = pt->read_end - pt->read_buf; |
| 175 | } |
| 176 | return target; |
| 177 | } |
| 178 | |
| 179 | static void |
| 180 | st_ftruncate (SCM port, off_t length) |
| 181 | { |
| 182 | scm_port *pt = SCM_PTAB_ENTRY (port); |
| 183 | |
| 184 | if (length > pt->write_buf_size) |
| 185 | st_resize_port (pt, length); |
| 186 | |
| 187 | pt->read_buf_size = length; |
| 188 | pt->read_end = pt->read_buf + length; |
| 189 | if (pt->read_pos > pt->read_end) |
| 190 | pt->read_pos = pt->read_end; |
| 191 | |
| 192 | if (pt->write_pos > pt->read_end) |
| 193 | pt->write_pos = pt->read_end; |
| 194 | } |
| 195 | |
| 196 | SCM |
| 197 | scm_mkstrport (pos, str, modes, caller) |
| 198 | SCM pos; |
| 199 | SCM str; |
| 200 | long modes; |
| 201 | const char * caller; |
| 202 | { |
| 203 | SCM z; |
| 204 | scm_port *pt; |
| 205 | int str_len; |
| 206 | |
| 207 | SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller); |
| 208 | SCM_ASSERT (SCM_NIMP(str) && SCM_ROSTRINGP(str), str, SCM_ARG1, caller); |
| 209 | str_len = SCM_ROLENGTH (str); |
| 210 | if (SCM_INUM (pos) > str_len) |
| 211 | scm_out_of_range (caller, pos); |
| 212 | if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG))) |
| 213 | scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL); |
| 214 | SCM_NEWCELL (z); |
| 215 | SCM_DEFER_INTS; |
| 216 | pt = scm_add_to_port_table (z); |
| 217 | SCM_SETCAR (z, scm_tc16_strport | modes); |
| 218 | SCM_SETPTAB_ENTRY (z, pt); |
| 219 | SCM_SETSTREAM (z, str); |
| 220 | pt->write_buf = pt->read_buf = SCM_ROCHARS (str); |
| 221 | pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos); |
| 222 | pt->write_buf_size = pt->read_buf_size = str_len; |
| 223 | pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size; |
| 224 | |
| 225 | /* doesn't check (modes & SCM_RDNG), since the read_buf must be |
| 226 | maintained even for output-only ports. */ |
| 227 | pt->rw_random = modes & SCM_WRTNG; |
| 228 | |
| 229 | SCM_ALLOW_INTS; |
| 230 | |
| 231 | /* ensure write_pos is writable. */ |
| 232 | if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end) |
| 233 | st_flush (z); |
| 234 | return z; |
| 235 | } |
| 236 | |
| 237 | /* create a new string from a string port's buffer. */ |
| 238 | SCM scm_strport_to_string (SCM port) |
| 239 | { |
| 240 | scm_port *pt = SCM_PTAB_ENTRY (port); |
| 241 | |
| 242 | if (pt->rw_active == SCM_PORT_WRITE) |
| 243 | st_flush (port); |
| 244 | return scm_makfromstr (SCM_CHARS (SCM_STREAM (port)), |
| 245 | pt->read_buf_size, 0); |
| 246 | } |
| 247 | |
| 248 | SCM_PROC(s_call_with_output_string, "call-with-output-string", 1, 0, 0, scm_call_with_output_string); |
| 249 | |
| 250 | SCM |
| 251 | scm_call_with_output_string (proc) |
| 252 | SCM proc; |
| 253 | { |
| 254 | SCM p; |
| 255 | |
| 256 | p = scm_mkstrport (SCM_INUM0, |
| 257 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), |
| 258 | SCM_OPN | SCM_WRTNG, |
| 259 | s_call_with_output_string); |
| 260 | scm_apply (proc, p, scm_listofnull); |
| 261 | |
| 262 | return scm_strport_to_string (p); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | |
| 267 | /* Return a Scheme string obtained by printing a given object. |
| 268 | */ |
| 269 | |
| 270 | |
| 271 | SCM |
| 272 | scm_strprint_obj (obj) |
| 273 | SCM obj; |
| 274 | { |
| 275 | SCM str; |
| 276 | SCM port; |
| 277 | |
| 278 | str = scm_makstr (0, 0); |
| 279 | port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj"); |
| 280 | scm_prin1 (obj, port, 1); |
| 281 | { |
| 282 | return scm_strport_to_string (port); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | SCM_PROC(s_call_with_input_string, "call-with-input-string", 2, 0, 0, scm_call_with_input_string); |
| 290 | |
| 291 | SCM |
| 292 | scm_call_with_input_string (str, proc) |
| 293 | SCM str; |
| 294 | SCM proc; |
| 295 | { |
| 296 | SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, s_call_with_input_string); |
| 297 | return scm_apply (proc, p, scm_listofnull); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | |
| 302 | /* Given a null-terminated string EXPR containing a Scheme expression |
| 303 | read it, and return it as an SCM value. */ |
| 304 | SCM |
| 305 | scm_read_0str (expr) |
| 306 | char *expr; |
| 307 | { |
| 308 | SCM port = scm_mkstrport (SCM_INUM0, |
| 309 | scm_makfrom0str (expr), |
| 310 | SCM_OPN | SCM_RDNG, |
| 311 | "scm_eval_0str"); |
| 312 | SCM form; |
| 313 | |
| 314 | /* Read expressions from that port; ignore the values. */ |
| 315 | form = scm_read (port); |
| 316 | |
| 317 | scm_close_port (port); |
| 318 | return form; |
| 319 | } |
| 320 | |
| 321 | /* Given a null-terminated string EXPR containing Scheme program text, |
| 322 | evaluate it, and return the result of the last expression evaluated. */ |
| 323 | SCM |
| 324 | scm_eval_0str (expr) |
| 325 | char *expr; |
| 326 | { |
| 327 | return scm_eval_string (scm_makfrom0str (expr)); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | SCM_PROC (s_eval_string, "eval-string", 1, 0, 0, scm_eval_string); |
| 332 | |
| 333 | SCM |
| 334 | scm_eval_string (string) |
| 335 | SCM string; |
| 336 | { |
| 337 | SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG, |
| 338 | "scm_eval_0str"); |
| 339 | SCM form; |
| 340 | SCM ans = SCM_UNSPECIFIED; |
| 341 | |
| 342 | /* Read expressions from that port; ignore the values. */ |
| 343 | while (!SCM_EOF_OBJECT_P (form = scm_read (port))) |
| 344 | ans = scm_eval_x (form); |
| 345 | |
| 346 | /* Don't close the port here; if we re-enter this function via a |
| 347 | continuation, then the next time we enter it, we'll get an error. |
| 348 | It's a string port anyway, so there's no advantage to closing it |
| 349 | early. */ |
| 350 | |
| 351 | return ans; |
| 352 | } |
| 353 | |
| 354 | void scm_make_stptob (void); /* Called from ports.c */ |
| 355 | |
| 356 | void |
| 357 | scm_make_stptob () |
| 358 | { |
| 359 | long tc = scm_make_port_type ("string", stfill_buffer, st_flush); |
| 360 | scm_set_ptob_mark (tc, scm_markstream); |
| 361 | scm_set_ptob_flush_input (tc, st_read_flush); |
| 362 | scm_set_ptob_seek (tc, st_seek); |
| 363 | scm_set_ptob_truncate (tc, st_ftruncate); |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | scm_init_strports () |
| 368 | { |
| 369 | #include "strports.x" |
| 370 | } |
| 371 | |