summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2013-12-02 07:41:37 -0800
committerKaz Kylheku <kaz@kylheku.com>2013-12-02 07:41:37 -0800
commit4c6209c7cceac38a0ce852d3d0990a6f098be87a (patch)
tree3623d0ea6607b6e383a440ef1ca180e106885cec
parentfb9aba2ed5f2cb69da4737f1f65949da81af6b94 (diff)
downloadtxr-4c6209c7cceac38a0ce852d3d0990a6f098be87a.tar.gz
txr-4c6209c7cceac38a0ce852d3d0990a6f098be87a.tar.bz2
txr-4c6209c7cceac38a0ce852d3d0990a6f098be87a.zip
* stream.c (tail_strategy): Execute the strategy code also
in the case that the stream's FILE * handle is null. This handles opening the file for the first time. (make_stdio_stream_common): Do not use the FILE * handle if it is null. (open_tail): Do not open the file immediately and error out. This is undesirable because log files might not exist at the time open_tail is called on them. Instead, produce a stream which contains a null file handle, and use tail_strategy to poll for the file to come into existence. * utf8.c (w_freopen): Just in case freopen doesn't like a null pointer for the existing stream, use fopen instead if that is the case.
-rw-r--r--ChangeLog15
-rw-r--r--stream.c24
-rw-r--r--utf8.c2
3 files changed, 24 insertions, 17 deletions
diff --git a/ChangeLog b/ChangeLog
index ca50a21c..ed2b531b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
2013-12-02 Kaz Kylheku <kaz@kylheku.com>
+ * stream.c (tail_strategy): Execute the strategy code also
+ in the case that the stream's FILE * handle is null. This handles
+ opening the file for the first time.
+ (make_stdio_stream_common): Do not use the FILE * handle if it is null.
+ (open_tail): Do not open the file immediately and error out.
+ This is undesirable because log files might not exist at the time
+ open_tail is called on them. Instead, produce a stream which contains
+ a null file handle, and use tail_strategy to poll for the file
+ to come into existence.
+
+ * utf8.c (w_freopen): Just in case freopen doesn't like a null
+ pointer for the existing stream, use fopen instead if that is the case.
+
+2013-12-02 Kaz Kylheku <kaz@kylheku.com>
+
* genvim.txr: Missing catch and finally keywords added.
* txr.vim: Regenerated.
diff --git a/stream.c b/stream.c
index 8bae32eb..b401c911 100644
--- a/stream.c
+++ b/stream.c
@@ -360,17 +360,17 @@ static void tail_calc(unsigned long *state, int *sec, int *mod)
static void tail_strategy(val stream, unsigned long *state)
{
+ struct stdio_handle *h = (struct stdio_handle *) stream->co.handle;
int sec, mod;
tail_calc(state, &sec, &mod);
sleep(sec);
- if (*state % mod == 0) {
- struct stdio_handle *h = (struct stdio_handle *) stream->co.handle;
- long save_pos, size;
+ if (*state % mod == 0 || h->f == 0) {
+ long save_pos = 0, size;
- if ((save_pos = ftell(h->f)) == -1)
+ if (h->f != 0 && (save_pos = ftell(h->f)) == -1)
return;
for (;;) {
@@ -925,7 +925,7 @@ static val make_stdio_stream_common(FILE *f, val descr, struct cobj_ops *ops)
utf8_decoder_init(&h->ud);
h->pid = 0;
#if HAVE_ISATTY
- h->is_real_time = (isatty(fileno(h->f)) == 1);
+ h->is_real_time = (h->f != 0 && isatty(fileno(h->f)) == 1);
#else
h->is_real_time = 0;
#endif
@@ -1798,22 +1798,14 @@ 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));
struct stdio_handle *h;
val stream;
+ unsigned long state = 0;
- if (!f)
- uw_throwf(file_error_s, lit("error opening ~a: ~a/~s"),
- path, num(errno), string_utf8(strerror(errno)), nao);
-
- if (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);
+ stream = make_tail_stream(0, path);
h = (struct stdio_handle *) stream->co.handle;
h->mode = mode_str;
+ tail_strategy(stream, &state);
return stream;
}
diff --git a/utf8.c b/utf8.c
index 99e21978..a65c864e 100644
--- a/utf8.c
+++ b/utf8.c
@@ -376,7 +376,7 @@ FILE *w_freopen(const wchar_t *wname, const wchar_t *wmode, FILE *fold)
{
char *name = utf8_dup_to(wname);
char *mode = utf8_dup_to(wmode);
- FILE *f = freopen(name, mode, fold);
+ FILE *f = fold ? freopen(name, mode, fold) : fopen(name, mode);
free(name);
free(mode);
return f;