From 7bccff13730eb4997a570532c6e190e7bc7ea9f2 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 10 Dec 2013 08:19:11 -0800 Subject: * eval.c (eval_init): Register streamp, real_time_stream_p, stream_get_prop and stream_set_prop as intrinsics. * stream.c (stream_get_prop): New function. * stream.h (stream_get_prop): Declared. * syslog.c (syslog_get_prop): Bugfix: check for prio_k not real_time_k. * txr.1: Updated. --- ChangeLog | 13 +++++++++ eval.c | 5 +++- stream.c | 12 +++++++++ stream.h | 1 + syslog.c | 2 +- txr.1 | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 122 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0274738b..314e0570 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2013-12-10 Kaz Kylheku + + * eval.c (eval_init): Register streamp, real_time_stream_p, + stream_get_prop and stream_set_prop as intrinsics. + + * stream.c (stream_get_prop): New function. + + * stream.h (stream_get_prop): Declared. + + * syslog.c (syslog_get_prop): Bugfix: check for prio_k not real_time_k. + + * txr.1: Updated. + 2013-12-10 Kaz Kylheku syslog support; bitwise logior and logand functions become variadic. diff --git a/eval.c b/eval.c index 2184ffc3..6f99308a 100644 --- a/eval.c +++ b/eval.c @@ -2385,7 +2385,10 @@ void eval_init(void) reg_fun(intern(lit("flush-stream"), user_package), func_n1(flush_stream)); reg_fun(intern(lit("seek-stream"), user_package), func_n3(seek_stream)); reg_fun(intern(lit("stat"), user_package), func_n1(statf)); - + reg_fun(intern(lit("streamp"), user_package), func_n1(streamp)); + reg_fun(intern(lit("real-time-stream-p"), user_package), func_n1(real_time_stream_p)); + reg_fun(intern(lit("stream-set-prop"), user_package), func_n3(stream_set_prop)); + reg_fun(intern(lit("stream-get-prop"), user_package), func_n2(stream_get_prop)); reg_var(intern(lit("s-ifmt"), user_package), &s_ifmt); reg_var(intern(lit("s-iflnk"), user_package), &s_iflnk); reg_var(intern(lit("s-ifreg"), user_package), &s_ifreg); diff --git a/stream.c b/stream.c index 66dc5c30..6f60e7cf 100644 --- a/stream.c +++ b/stream.c @@ -1051,6 +1051,18 @@ val stream_set_prop(val stream, val ind, val prop) } } +val stream_get_prop(val stream, val ind) +{ + type_check (stream, COBJ); + type_assert (stream->co.cls == stream_s, (lit("~a is not a stream"), + stream, nao)); + + { + struct strm_ops *ops = (struct strm_ops *) stream->co.ops; + return ops->get_prop ? ops->get_prop(stream, ind) : nil; + } +} + val real_time_stream_p(val obj) { if (streamp(obj)) { diff --git a/stream.h b/stream.h index 4b4bd40e..342336d6 100644 --- a/stream.h +++ b/stream.h @@ -73,6 +73,7 @@ val make_dir_stream(DIR *); val streamp(val obj); val real_time_stream_p(val obj); val stream_set_prop(val stream, val ind, val prop); +val stream_get_prop(val stream, val ind); val close_stream(val stream, val throw_on_error); val get_line(val); val get_char(val); diff --git a/syslog.c b/syslog.c index 3ae0d137..0bcd1cd3 100644 --- a/syslog.c +++ b/syslog.c @@ -178,7 +178,7 @@ static val syslog_put_byte(val stream, int ch) static val syslog_get_prop(val stream, val ind) { - if (ind == real_time_k) { + if (ind == prio_k) { val cell = (val) stream->co.handle; return car(cell); } diff --git a/txr.1 b/txr.1 index 4e0799b4..20795c2a 100644 --- a/txr.1 +++ b/txr.1 @@ -9948,7 +9948,7 @@ The iffi function defaults to the identity function when is omitted or nil, and therefore is useful in situations when one value is to be replaced with another one when the condition is true, otherwise left alone. -.SH INPUT AND OUTPUT +.SH INPUT AND OUTPUT (STREAMS) TXR Lisp supports input and output streams of various kinds, with generic operations that work across the stream types. @@ -10151,6 +10151,50 @@ and calls to these functions: For pprint and tostringp, the equivalence is produced by using "~a" in format rather than "~s". +.SS Function streamp + +.TP +Syntax: + + (streamp ) + +.TP +Description: + +The streamp function returns t if is any type of stream. +Otherwise it returns nil. + +.SS Function real-time-stream-p + +.TP +Syntax: + + (real-time-stream-p ) + +.TP +Description: + +The real-time-streamp-p function returns t if is a stream marked as +"real-time". If is not a stream, or not a stream marked as "real-time", +then it returns nil. + +Note: the expression (real-time-stream-p S) is almost like +(stream-get-prop S :real-time), except the latter requires S to be a stream. + +Only certain kinds of streams accept the real-time attribute: file streams and +tail streams. This attribute controls the semantics of the application of +lazy-stream-cons to the stream. For a real-time stream, lazy-stream-cons +returns a stream with "naive" semantics which returns data as soon as it is +available, at the cost of generating spurious nil item when the stream +terminates. The application has to recognize and discard that nil item. +The ordinary lazy streams read ahead by one line and suppress this extra +item, so their representation is more accurate. + +Streams connected to TTY devices (devices which for which the isatty function +reports true) are marked as real-time. This is only supported on platforms that +have an isatty function. + + .SS Function make-string-input-stream .TP @@ -10389,6 +10433,38 @@ returns t if it is successful. On failure, it throws an exception of type stream-error. +.SS Functions stream-get-prop and stream-set-prop + +.TP +Syntax: + + (stream-get-prop ) + (stream-set-prop ) + +.TP +Description: + +These functions get and set properties on a stream. Only certain properties +are meaningful with certain kinds of streams, and the meaning depends on +the stream. If two or more stream types support a property of the same name, it +is expected that the property has the same or very similar meaning for both +streams to the maximum extent that similarity is possible possible. + +The stream-set-prop fucntion sets a property on a stream. The +argument is a symbol, usually a keyword symbol, denoting the property, +and is the property value. If the stream understands and accepts the +property, the function returns t. Otherwise it returns nil. + +The stream-get-prop function inquires about the value of a property on a +stream. If the stream understands the property, then it returns its current +value. If the stream does not understand a property, nil is returned, which is +also returned if the property exists, but its value happens to be nil. + +Properties are currently used for marking certain streams as "real-time" (see +the stream-real-time-p function above), and also for setting the priority at +which messages are reported to syslog by the *stdlog* stream (see *stdlog* +in the UNIX SYSLOG section). + .SH FILESYSTEM ACCESS .SS Function stat @@ -10981,13 +11057,26 @@ These are the integer priority codes specified in the syslog call. .SS The *stdlog* variable holds a special kind of stream: a syslog stream. Each newline-terminated line of text sent to this stream becomes a log -message. The stream internally maintains a priority value that is applied +message. + +The stream internally maintains a priority value that is applied when it generates messages. By default, this value is that of log-info. +The stream holds the priority as the value of the :prio property, +which may be changed with the stream-set-prop function. + +The latest priority value which has been configured on the stream is used +at the time the newline character is processed and the log message +is generated, not necessarily the value which was in effect at the time the +accumulation of a line began to take place. + Note that messages generated via the openlog function are not newline terminated; this is a convention of the syslog stream. The newline characters written to the syslog stream are only used to recognize the message framing, and are not sent to syslog. +Multiple lines can be written to the stream in a single operation, and +these result in multiple syslog mesages. + .SS The openlog function .TP -- cgit v1.2.3