summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog12
-rw-r--r--eval.c6
-rw-r--r--stream.c11
-rw-r--r--txr.133
4 files changed, 40 insertions, 22 deletions
diff --git a/ChangeLog b/ChangeLog
index abfb9ae8..92088732 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
2014-02-18 Kaz Kylheku <kaz@kylheku.com>
+ The mode argument in some stream-opening functions becomes optional.
+
+ * eval.c (eval_init): Change registration for open_file, open_tail
+ and open_command.
+
+ * stream.c (open_file, open_tail, open_command): mode_str argument
+ defaulted.
+
+ * txr.1: Updated.
+
+2014-02-18 Kaz Kylheku <kaz@kylheku.com>
+
* stream.c (open_tail): Fix 2013-12-02 regression:
seek_to_end_p argument being ignored, with the
behavior being no initial seek to the end.
diff --git a/eval.c b/eval.c
index 36ac3ea9..f23546f2 100644
--- a/eval.c
+++ b/eval.c
@@ -2952,9 +2952,9 @@ void eval_init(void)
reg_var(intern(lit("s-ixoth"), user_package), &s_ixoth);
reg_fun(intern(lit("open-directory"), user_package), func_n1(open_directory));
- reg_fun(intern(lit("open-file"), user_package), func_n2(open_file));
- reg_fun(intern(lit("open-tail"), user_package), func_n3(open_tail));
- reg_fun(intern(lit("open-command"), user_package), func_n2(open_command));
+ reg_fun(intern(lit("open-file"), user_package), func_n2o(open_file, 1));
+ reg_fun(intern(lit("open-tail"), user_package), func_n3o(open_tail, 1));
+ reg_fun(intern(lit("open-command"), user_package), func_n2o(open_command, 1));
reg_fun(intern(lit("open-pipe"), user_package), func_n2(open_command));
reg_fun(intern(lit("open-process"), user_package), func_n3o(open_process, 2));
reg_fun(intern(lit("remove-path"), user_package), func_n1(remove_path));
diff --git a/stream.c b/stream.c
index 5b1b966c..65fec6fd 100644
--- a/stream.c
+++ b/stream.c
@@ -2071,7 +2071,7 @@ val open_directory(val path)
val open_file(val path, val mode_str)
{
- FILE *f = w_fopen(c_str(path), c_str(mode_str));
+ FILE *f = w_fopen(c_str(path), c_str(default_arg(mode_str, lit("r"))));
if (!f)
uw_throwf(file_error_s, lit("error opening ~a: ~a/~s"),
@@ -2082,19 +2082,20 @@ val open_file(val path, val mode_str)
val open_tail(val path, val mode_str, val seek_end_p)
{
- FILE *f = w_fopen(c_str(path), c_str(mode_str));
+ val mode = default_arg(mode_str, lit("r"));
+ FILE *f = w_fopen(c_str(path), c_str(mode));
struct stdio_handle *h;
val stream;
unsigned long state = 0;
- if (f && seek_end_p)
+ if (f && default_bool_arg(seek_end_p))
if (fseek(f, 0, SEEK_END) < 0)
uw_throwf(file_error_s, lit("error seeking to end of ~a: ~a/~s"),
path, num(errno), string_utf8(strerror(errno)), nao);
stream = make_tail_stream(f, path);
h = (struct stdio_handle *) stream->co.handle;
- h->mode = mode_str;
+ h->mode = mode;
if (!f)
tail_strategy(stream, &state);
return stream;
@@ -2102,7 +2103,7 @@ val open_tail(val path, val mode_str, val seek_end_p)
val open_command(val path, val mode_str)
{
- FILE *f = w_popen(c_str(path), c_str(mode_str));
+ FILE *f = w_popen(c_str(path), c_str(default_arg(mode_str, lit("r"))));
if (!f)
uw_throwf(file_error_s, lit("error opening pipe ~a: ~a/~s"),
diff --git a/txr.1 b/txr.1
index 518fdc20..45ec1806 100644
--- a/txr.1
+++ b/txr.1
@@ -11598,13 +11598,14 @@ The . and .. entries in Unix filesystems are not skipped.
.TP
Syntax:
- (open-file <path> <mode-string>)
+ (open-file <path> [<mode-string>])
.TP
Description:
The open-file function creates a stream connected to the file
which is located at the given <path>, which is a string.
+
The <mode-string> argument is a string which uses the same
conventions as the mode argument of the C language fopen function.
The mode string determines whether the stream is an input stream
@@ -11612,12 +11613,14 @@ or output stream. Note that the "b" mode is not supported.
Whether a stream is text or binary depends on which operations
are invoked on it.
+If the <mode-string> argument is omitted, the mode "r" is used.
+
.SS Function open-tail
.TP
Syntax:
- (open-tail <path> <mode-string> <seek-to-end-p>)
+ (open-tail <path> [ [<mode-string>] <seek-to-end-p> ])
.TP
Description:
@@ -11625,19 +11628,18 @@ Description:
The open-tail function creates a tail stream connected to the file which is
located at the given <path>. The <mode-string> argument is a string which uses
the same conventions as the mode argument of the C language fopen function.
-The mode string determines whether the stream is an input stream
-or output stream. Note that the "b" mode is not supported.
-Whether a stream is text or binary depends on which operations
-are invoked on it.
+If it is missing, it defaults to "r". Note that the "b" mode is not supported.
+Whether a stream is text or binary depends on which operations are invoked on
+it.
The <seek-to-end-p> argument is a boolean which determines whether the initial
-read/write position is at the start of the file, or just past the end. This
-argument only makes a difference if the file exists at the time open-tail is
-called. If the file does not exist, and is later created, then the tail stream
-will follow that file from the beginning. In other words, <seek-to-end-p>
-controls whether the tail stream reads all the existing data in the file, if
-any, or whether it reads only newly added data from approximately the time the
-stream is created.
+read/write position is at the start of the file, or just past the end.
+It defaults to nil. This argument only makes a difference if the file exists
+at the time open-tail is called. If the file does not exist, and is later
+created, then the tail stream will follow that file from the beginning. In
+other words, <seek-to-end-p> controls whether the tail stream reads all the
+existing data in the file, if any, or whether it reads only newly added data
+from approximately the time the stream is created.
A tail stream has special semantics with regard to reading at the end
of file. A tail stream never reports an end-of-file condition; instead
@@ -11658,7 +11660,7 @@ changes the current working directory, and the path name is relative.
.TP
Syntax:
- (open-command <system-command> <mode-string>)
+ (open-command <system-command> [<mode-string>])
(open-process <command> <mode-string> [<argument-strings>])
.TP
@@ -11669,6 +11671,9 @@ with the TXR program. Both functions return a unidirectional stream for
communicating with these programs: either an output stream, or an input
stream, depending on the contents of <mode-string>.
+In open-command, the <mode-string> argument is optional, defaulting to
+the value "r" if it is missing.
+
The open-command function accepts, via the <system-command> string parameter, a
system command, which is in a system-dependent syntax. On a POSIX system, this
would be in the POSIX Shell Command Language.