summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2009-10-24 09:08:26 +0000
committerSimon Tatham <anakin@pobox.com>2009-10-24 09:08:26 +0000
commitccb035ab284dcce968d948a5f04eef96c9cc541d (patch)
treeda95a7e83c75ea5f9e28d5c2e66532ef9b4390f3
parent0a4281ca5b029a961fbef0856e27dee2a23c8cdb (diff)
downloadhalibut-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.c2
-rw-r--r--halibut.h1
-rw-r--r--input.c46
-rw-r--r--main.c7
4 files changed, 38 insertions, 18 deletions
diff --git a/error.c b/error.c
index 7897d7a..00d007d 100644
--- a/error.c
+++ b/error.c
@@ -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)
diff --git a/halibut.h b/halibut.h
index 4f433e1..11b8ac2 100644
--- a/halibut.h
+++ b/halibut.h
@@ -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;
diff --git a/input.c b/input.c
index f43604f..4978bb9 100644
--- a/input.c
+++ b/input.c
@@ -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
diff --git a/main.c b/main.c
index 60ad74f..dae6581 100644
--- a/main.c
+++ b/main.c
@@ -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;
}
}