diff options
| author | Simon Tatham <anakin@pobox.com> | 2009-10-24 09:08:26 +0000 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 2009-10-24 09:08:26 +0000 |
| commit | ccb035ab284dcce968d948a5f04eef96c9cc541d (patch) | |
| tree | da95a7e83c75ea5f9e28d5c2e66532ef9b4390f3 | |
| parent | 0a4281ca5b029a961fbef0856e27dee2a23c8cdb (diff) | |
| download | halibut-ccb035ab284dcce968d948a5f04eef96c9cc541d.zip halibut-ccb035ab284dcce968d948a5f04eef96c9cc541d.tar.gz halibut-ccb035ab284dcce968d948a5f04eef96c9cc541d.tar.bz2 halibut-ccb035ab284dcce968d948a5f04eef96c9cc541d.tar.xz | |
Enable Halibut to read a .but file from standard input, by supplying
the special filename '-'.
[originally from svn r8728]
| -rw-r--r-- | error.c | 2 | ||||
| -rw-r--r-- | halibut.h | 1 | ||||
| -rw-r--r-- | input.c | 46 | ||||
| -rw-r--r-- | main.c | 7 |
4 files changed, 38 insertions, 18 deletions
@@ -393,7 +393,7 @@ static void do_error(int code, va_list ap) { if (flags & PREFIX) fputs("halibut: ", stderr); if (flags & FILEPOS) { - fprintf(stderr, "%s:", fpos.filename); + fprintf(stderr, "%s:", fpos.filename ? fpos.filename : "<standard input>"); if (fpos.line > 0) fprintf(stderr, "%d:", fpos.line); if (fpos.col > 0) @@ -62,6 +62,7 @@ struct input_Tag { int nfiles; /* how many in the list */ FILE *currfp; /* the currently open one */ int currindex; /* which one is that in the list */ + int wantclose; /* does the current file want closing */ pushback *pushback; /* pushed-back input characters */ int npushback, pushbacksize; filepos pos; @@ -119,7 +119,8 @@ static int get(input *in, filepos *pos, rdstringc *rsc) { int c = getc(in->currfp); if (c == EOF) { - fclose(in->currfp); + if (in->wantclose) + fclose(in->currfp); in->currfp = NULL; return EOF; } @@ -1601,23 +1602,38 @@ paragraph *read_input(input *in, indexdata *idx) { macros = newtree234(macrocmp); while (in->currindex < in->nfiles) { - in->currfp = fopen(in->filenames[in->currindex], "r"); - if (in->currfp) { - setpos(in, in->filenames[in->currindex]); - in->charset = in->defcharset; - in->csstate = charset_init_state; - in->wcpos = in->nwc = 0; - in->pushback_chars = NULL; + setpos(in, in->filenames[in->currindex]); + in->charset = in->defcharset; + in->csstate = charset_init_state; + in->wcpos = in->nwc = 0; + in->pushback_chars = NULL; + + if (!in->filenames[in->currindex]) { + in->currfp = stdin; + in->wantclose = FALSE; /* don't fclose stdin */ + /* + * When reading standard input, we always expect to see + * an actual Halibut file and not any of the unusual + * input types like fonts. + */ reader = NULL; - len = fread(mag, 1, sizeof(mag), in->currfp); - for (i = 0; i < lenof(magics); i++) { - if (len >= magics[i].nmagic && - memcmp(mag, magics[i].magic, magics[i].nmagic) == 0) { - reader = magics[i].reader; - break; + } else { + in->currfp = fopen(in->filenames[in->currindex], "r"); + if (in->currfp) { + in->wantclose = TRUE; + reader = NULL; + len = fread(mag, 1, sizeof(mag), in->currfp); + for (i = 0; i < lenof(magics); i++) { + if (len >= magics[i].nmagic && + memcmp(mag, magics[i].magic, magics[i].nmagic) == 0) { + reader = magics[i].reader; + break; + } } + rewind(in->currfp); } - rewind(in->currfp); + } + if (in->currfp) { if (reader == NULL) read_file(&hptr, in, idx, macros); else @@ -85,7 +85,7 @@ int main(int argc, char **argv) { */ while (--argc) { char *p = *++argv; - if (*p == '-') { + if (*p == '-' && p[1]) { /* * An option. */ @@ -264,7 +264,10 @@ int main(int argc, char **argv) { /* * A non-option argument. */ - infiles[nfiles++] = p; + if (!strcmp(p, "-")) + infiles[nfiles++] = NULL; /* special case: read stdin */ + else + infiles[nfiles++] = p; } } |