diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-07-29 23:03:28 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-07-29 23:03:28 -0700 |
commit | e44c113ee17c7cf15e8b1891f4d51ec03b16bc24 (patch) | |
tree | af8691f7c51ee977a9939e1624375262f6e311c4 /syslog.c | |
parent | b6955812c33d432557b93fe4638b411ff944334b (diff) | |
download | txr-e44c113ee17c7cf15e8b1891f4d51ec03b16bc24.tar.gz txr-e44c113ee17c7cf15e8b1891f4d51ec03b16bc24.tar.bz2 txr-e44c113ee17c7cf15e8b1891f4d51ec03b16bc24.zip |
Deriving streams from the same base, so
we can give streams some common slots.
* stream.c (strm_base_init, strm_base_cleanup, strm_base_mark,
stream_destroy_op, stream_mark_op): New functions.
(null_ops): Switch to stream_destroy_op and stream_mark_op.
(make_null_stream): Associate a strm_base instance with the
cobj handle rather than a null pointer.
(struct stdio_handle): Inherit struct strm_base.
(stdio_stream_destroy): Clean up the strm_base part.
(stdio_stream_mark): Mark the strm_base part.
(make_stdio_stream_common): Initialize the strm_base part.
(struct dir_handle): Inherit struct strm_base.
(dir_destroy): Clean up the strm_base part.
(dir_mark): Mark the strm_base part.
(make_dir_stream): Initialize the strm_base part.
(struct string_in): New structure. Replaces ad-hoc cons
cell used for string input streams.
(string_in_stream_mark): Mark new structure.
(string_in_get_line, string_in_get_char, string_in_unget_char,
string_in_get_prop, string_in_get_error, make_string_input_stream):
Convert to new structure.
(string_in_ops): Switch to stream_destroy_op.
(struct byte_input): Inherit struct strm_base.
(byte_in_stream_destroy): No need to check handle for null.
No need to set handle to null after freeing: gc does it.
Clean up the strm_base part.
(byte_in_ops): Switch to stream_mark_op.
(make_string_byte_input_stream): Initialize the strm_base part.
(struct string_output): Inherit struct strm_base.
(string_out_stream_destroy): No need to check handle for null
since the logic elsewhere has changed.
Clean up the strm_base part.
No need to set handle to null.
(string_out_ops): Switch to stream_mark_op.
(make_string_output_stream): Initialize the strm_base part.
(get_string_from_stream): Don't free the handle.
Null out the buffer so->buf whose ownership passes to the string.
(struct strlist_out): New structure. Replaces ad-hoc cons cell
used for string list output stream.
(strlist_mark): Renamed to strlist_out_mark. Mark the strm_base
part.
(strlist_out_put_string, strlist_out_put_char,
make_strlist_output_stream, get_list_from_stream):
Convert to new structure.
(strlist_out_ops): Switch to stream_destroy_op.
Follow rename of strlist_mark.
(struct cat_strm): New structure, replacing ad-hoc list pointer
Diffstat (limited to 'syslog.c')
-rw-r--r-- | syslog.c | 53 |
1 files changed, 35 insertions, 18 deletions
@@ -43,6 +43,12 @@ #include "eval.h" #include "syslog.h" +struct syslog_strm { + struct strm_base a; + val prio; + val strstream; +}; + val prio_k; static struct strm_ops syslog_strm_ops; @@ -119,14 +125,18 @@ val closelog_wrap(void) static void syslog_mark(val stream) { + struct syslog_strm *s = coerce(struct syslog_strm *, stream->co.handle); + strm_base_mark(&s->a); + gc_mark(s->prio); + gc_mark(s->strstream); val stuff = coerce(val, stream->co.handle); gc_mark(stuff); } static val syslog_put_string(val stream, val str) { - val cell = coerce(val, stream->co.handle); - cons_bind (prio, strstream, cell); + struct syslog_strm *s = coerce(struct syslog_strm *, stream->co.handle); + val strstream = s->strstream; for (;;) { val length = length_str(str); @@ -141,51 +151,51 @@ static val syslog_put_string(val stream, val str) break; str = sub_str(str, plus(span_to_newline, num(1)), nil); - syslog_wrap(prio, lit("~a"), list(get_string_from_stream(strstream), nao)); + syslog_wrap(s->prio, lit("~a"), list(get_string_from_stream(strstream), nao)); strstream = make_string_output_stream(); } - set(cdr_l(cell), strstream); + set(mkloc(s->strstream, stream), strstream); return t; } static val syslog_put_char(val stream, val ch) { - val cell = coerce(val, stream->co.handle); - cons_bind (prio, strstream, cell); + struct syslog_strm *s = coerce(struct syslog_strm *, stream->co.handle); + val strstream = s->strstream; if (ch == chr('\n')) { - syslog_wrap(prio, lit("~a"), list(get_string_from_stream(strstream), nao)); + syslog_wrap(s->prio, lit("~a"), list(get_string_from_stream(strstream), nao)); strstream = make_string_output_stream(); } else { put_char(ch, strstream); } - set(cdr_l(cell), strstream); + set(mkloc(s->strstream, stream), strstream); return t; } static val syslog_put_byte(val stream, int ch) { - val cell = coerce(val, stream->co.handle); - cons_bind (prio, strstream, cell); + struct syslog_strm *s = coerce(struct syslog_strm *, stream->co.handle); + val strstream = s->strstream; if (ch == '\n') { - syslog_wrap(prio, lit("~a"), list(get_string_from_stream(strstream), nao)); + syslog_wrap(s->prio, lit("~a"), list(get_string_from_stream(strstream), nao)); strstream = make_string_output_stream(); } else { put_byte(num(ch), strstream); } - set(cdr_l(cell), strstream); + set(mkloc(s->strstream, stream), strstream); return t; } static val syslog_get_prop(val stream, val ind) { if (ind == prio_k) { - val cell = coerce(val, stream->co.handle); - return car(cell); + struct syslog_strm *s = coerce(struct syslog_strm *, stream->co.handle); + return s->prio; } else if (ind == name_k) { return lit("syslog"); } @@ -195,8 +205,8 @@ static val syslog_get_prop(val stream, val ind) static val syslog_set_prop(val stream, val ind, val prop) { if (ind == prio_k) { - val cell = coerce(val, stream->co.handle); - set(car_l(cell), prop); + struct syslog_strm *s = coerce(struct syslog_strm *, stream->co.handle); + set(mkloc(s->prio, stream), prop); return t; } return nil; @@ -219,6 +229,13 @@ static struct strm_ops syslog_strm_ops = val make_syslog_stream(val prio) { - return cobj(coerce(mem_t *, cons(prio, make_string_output_stream())), - stream_s, &syslog_strm_ops.cobj_ops); + struct syslog_strm *s = coerce(struct syslog_strm *, chk_malloc(sizeof *s)); + val stream; + val strstream = make_string_output_stream(); + strm_base_init(&s->a); + s->prio = prio; + s->strstream = nil; + stream = cobj(coerce(mem_t *, s), stream_s, &syslog_strm_ops.cobj_ops); + s->strstream = strstream; + return stream; } |