=================================================================== RCS file: /cvs/texi2mdoc/util.c,v retrieving revision 1.11 retrieving revision 1.12 diff -u -p -r1.11 -r1.12 --- texi2mdoc/util.c 2015/02/23 17:24:51 1.11 +++ texi2mdoc/util.c 2015/02/23 22:50:11 1.12 @@ -1,4 +1,4 @@ -/* $Id: util.c,v 1.11 2015/02/23 17:24:51 kristaps Exp $ */ +/* $Id: util.c,v 1.12 2015/02/23 22:50:11 kristaps Exp $ */ /* * Copyright (c) 2015 Kristaps Dzonsons * @@ -43,7 +43,10 @@ texifilepop(struct texi *p) assert(p->filepos > 0); f = &p->files[--p->filepos]; - munmap(f->map, f->mapsz); + if (TEXISRC_FILE == f->type) + munmap(f->map, f->mapsz); + else + free(f->map); } static void @@ -955,6 +958,47 @@ parseto(struct texi *p, const char *buf, } /* + * Like parsefile() but used for reading from stdandard input. + * This can only be called for the first file! + */ +void +parsestdin(struct texi *p) +{ + struct texifile *f; + size_t off; + ssize_t ssz; + + assert(0 == p->filepos); + f = &p->files[p->filepos]; + memset(f, 0, sizeof(struct texifile)); + + f->type = TEXISRC_STDIN; + f->name = ""; + + for (off = 0; ; off += (size_t)ssz) { + if (off == f->mapsz) { + if (f->mapsz == (1U << 31)) + texierr(p, "stdin buffer too long"); + f->mapsz = f->mapsz > 65536 / 2 ? + 2 * f->mapsz : 65536; + f->map = realloc(f->map, f->mapsz); + if (NULL == f->map) + texiabort(p, NULL); + } + ssz = read(STDIN_FILENO, + f->map + (int)off, f->mapsz - off); + if (0 == ssz) + break; + else if (-1 == ssz) + texiabort(p, NULL); + } + + p->filepos++; + parseeof(p, f->map, off); + texifilepop(p); +} + +/* * Memory-map the file "fname" and begin parsing it unless "parse" is * zero, in which case we just dump the file to stdout (making sure it * doesn't trip up mdoc(7) along the way). @@ -973,6 +1017,7 @@ parsefile(struct texi *p, const char *fname, int parse f = &p->files[p->filepos]; memset(f, 0, sizeof(struct texifile)); + f->type = TEXISRC_FILE; f->name = fname; if (-1 == (fd = open(fname, O_RDONLY, 0))) { texiabort(p, fname);