summaryrefslogtreecommitdiffstats
path: root/stream.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-10-20 05:21:40 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-10-20 05:21:40 -0700
commit86f3d2ba19925ccece5c2cecc46db57817ffa85b (patch)
treee3100caf6b5b4d92d0361e8b873508f0fa0d9a4c /stream.c
parenta22082e8325d6eb9911604ead620485184c1df6e (diff)
downloadtxr-86f3d2ba19925ccece5c2cecc46db57817ffa85b.tar.gz
txr-86f3d2ba19925ccece5c2cecc46db57817ffa85b.tar.bz2
txr-86f3d2ba19925ccece5c2cecc46db57817ffa85b.zip
Add stream printing context.
This is some infrastructure which will support *print-circle*. * lib.h (struct strm_ctx): Forward declared. (struct cobj_ops): Add context parameter to print function pointer. (cobj_print_op, obj_print_impl): Add context parameter to declarations. * hash.c (hash_print_op): Take context argument and pass it down in obj_print_impl calls. * lib.c (cobj_print_op, out_quasi_str): Likewise (obj_print_impl): Likewise, and also pass to COBJ print method. (obj_print, obj_pprint): Pass null pointer as context argument to obj_print_impl. * regex.c (regex_print): Take context parameter and ignore it. * socket.c (dgram_print): Likewise. * stream.h (struct strm_ctx): New struct type. (struct strm_base): New ctx member, pointer to struct strm_ctx. (stream_print_op): Add context parameter to declaration. (get_set_ctx, get_ctx): Declared. * stream.c (strm_base_init): Add null pointer to initializer. (strm_base_cleanup): Add assertion against context pointer being non-null: that indicates that some stream operation installed a context pointer and neglected to restore it to null before returning, which is bad because context will be stack allocated. (stream_print_op, stdio_stream_print, cat_stream_print): Take context parameter and ignore it. (get_set_ctx, get_ctx): New functions. * struct.c (struct_type_print): Take context parameter and ignore it. (struct_inst_print): Take context parameter and pass down to obj_print_impl.
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c31
1 files changed, 26 insertions, 5 deletions
diff --git a/stream.c b/stream.c
index 0c7fa12b..3282b7f6 100644
--- a/stream.c
+++ b/stream.c
@@ -90,13 +90,13 @@ val shell, shell_arg;
void strm_base_init(struct strm_base *s)
{
- static struct strm_base init = { indent_off, 60, 10, 0, 0 };
+ static struct strm_base init = { indent_off, 60, 10, 0, 0, 0 };
*s = init;
}
void strm_base_cleanup(struct strm_base *s)
{
- (void) s;
+ bug_unless (s->ctx == 0);
}
void strm_base_mark(struct strm_base *s)
@@ -104,10 +104,11 @@ void strm_base_mark(struct strm_base *s)
(void) s;
}
-void stream_print_op(val stream, val out, val pretty)
+void stream_print_op(val stream, val out, val pretty, struct strm_ctx *ctx)
{
val name = stream_get_prop(stream, name_k);
(void) pretty;
+ (void) ctx;
format(out, lit("#<~a ~p>"), name, stream, nao);
}
@@ -388,7 +389,8 @@ struct stdio_handle {
#endif
};
-static void stdio_stream_print(val stream, val out, val pretty)
+static void stdio_stream_print(val stream, val out, val pretty,
+ struct strm_ctx *ctx)
{
struct stdio_handle *h = coerce(struct stdio_handle *, stream->co.handle);
struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops);
@@ -396,6 +398,7 @@ static void stdio_stream_print(val stream, val out, val pretty)
val descr = ops->get_prop(stream, name_k);
(void) pretty;
+ (void) ctx;
if (h->pid)
format(out, lit("#<~a ~a ~a ~p>"), name, descr, num(h->pid), stream, nao);
@@ -2181,13 +2184,15 @@ struct cat_strm {
val streams;
};
-static void cat_stream_print(val stream, val out, val pretty)
+static void cat_stream_print(val stream, val out, val pretty,
+ struct strm_ctx *ctx)
{
struct cat_strm *s = coerce(struct cat_strm *, stream->co.handle);
struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops);
val name = static_str(ops->name);
(void) pretty;
+ (void) ctx;
format(out, lit("#<~a ~s>"), name, s->streams, nao);
}
@@ -3535,6 +3540,22 @@ val width_check(val stream, val alt)
return t;
}
+struct strm_ctx *get_set_ctx(val stream, struct strm_ctx *ctx)
+{
+ struct strm_base *s = coerce(struct strm_base *,
+ cobj_handle(stream, stream_s));
+ struct strm_ctx *ret = s->ctx;
+ s->ctx = ctx;
+ return ret;
+}
+
+struct strm_ctx *get_ctx(val stream)
+{
+ struct strm_base *s = coerce(struct strm_base *,
+ cobj_handle(stream, stream_s));
+ return s->ctx;
+}
+
val get_string(val stream_in, val nchars, val close_after_p)
{
val stream = default_arg(stream_in, std_input);