-struct buf {
- char *p;
- size_t n, sz;
-};
-#define BUF_INIT { 0, 0, 0 }
-#define BUF_REWIND(b) do { (b)->n = 0; } while (0)
-#define BUF_FREE(b) do { \
- buf *_b = (b); \
- free(_b->p); _b->p = 0; _b->n = _b->sz = 0; \
-} while (0)
-#define BUF_PUTC(b, ch) do { \
- struct buf *_b = (b); \
- if (_b->n >= _b->sz) { \
- _b->sz = _b->sz ? 2*_b->sz : 32; \
- _b->p = realloc(_b->p, _b->sz); \
- if (!_b->p) bail("out of memory allocating %zu bytes", _b->sz); \
- } \
- _b->p[_b->n] = (ch); \
-} while (0)
-
-static int read_line(FILE *fp, struct buf *b)
-{
- int ch;
-
- ch = getc(fp);
- if (ch == EOF)
- return (-1);
- else if (ch != '\n') do {
- BUF_PUTC(b, ch); b->n++;
- ch = getc(fp);
- } while (ch != EOF && ch != '\n');
- BUF_PUTC(b, 0);
- return (0);
-}
-
-static double parse_float(const char **p_inout, double min, double max,
- const char *what)
-{
- const char *p;
- char *q;
- double x;
- int err;
-
- err = errno; errno = 0;
- p = *p_inout;
- x = strtod(p, &q);
- if (errno || x < min || x > max) bail("bad %s `%s'", what, p);
- *p_inout = q; errno = err;
- return (x);
-}
-
-static long parse_int(const char **p_inout, long min, long max,
- const char *what)
-{
- const char *p;
- char *q;
- long x;
- int err;
-
- err = errno; errno = 0;
- p = *p_inout;
- x = strtoul(p, &q, 0);
- if (errno || x < min || x > max) bail("bad %s `%s'", what, p);
- *p_inout = q; errno = err;
- return (x);
-}
-