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 /input.c | |
| 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]
Diffstat (limited to 'input.c')
| -rw-r--r-- | input.c | 46 |
1 files changed, 31 insertions, 15 deletions
@@ -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 |