=================================================================== RCS file: /cvs/docbook2mdoc/parse.c,v retrieving revision 1.23 retrieving revision 1.24 diff -u -p -r1.23 -r1.24 --- docbook2mdoc/parse.c 2019/04/08 14:37:31 1.23 +++ docbook2mdoc/parse.c 2019/04/08 22:47:34 1.24 @@ -1,4 +1,4 @@ -/* $Id: parse.c,v 1.23 2019/04/08 14:37:31 schwarze Exp $ */ +/* $Id: parse.c,v 1.24 2019/04/08 22:47:34 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons * Copyright (c) 2019 Ingo Schwarze @@ -17,6 +17,9 @@ */ #include #include +#include +#include +#include #include #include #include @@ -273,6 +276,7 @@ static const struct entity entities[] = { static size_t parse_string(struct parse *, char *, size_t, enum pstate *, int); +static void parse_fd(struct parse *, int); static void @@ -957,8 +961,18 @@ parse_string(struct parse *p, char *b, size_t rlen, return poff; } -struct ptree * -parse_file(struct parse *p, int fd, const char *fname) + +/* + * The read loop. + * If the previous token was incomplete and asked for more input, + * we have to enter the read loop once more even on EOF. + * Once rsz is 0, incomplete tokens will no longer ask for more input + * but instead use whatever there is, and then exit the read loop. + * The minus one on the size limit for read(2) is needed such that + * advance() can set b[rlen] to NUL when needed. + */ +static void +parse_fd(struct parse *p, int fd) { char b[4096]; ssize_t rsz; /* Return value from read(2). */ @@ -966,24 +980,8 @@ parse_file(struct parse *p, int fd, const char *fname) size_t poff; /* Parse offset in b[]. */ enum pstate pstate; - p->fname = fname; - p->nline = 1; - p->ncol = 1; - pstate = PARSE_ELEM; rlen = 0; - - /* - * Read loop. - * - * If the previous token was incomplete and asked for more - * input, we have to enter the read loop once more even on EOF. - * Once rsz is 0, incomplete tokens will no longer ask - * for more input but instead use whatever there is, - * and then exit the read loop. - * The minus one on the size limit for read(2) is needed - * such that advance() can set b[rlen] to NUL when needed. - */ - + pstate = PARSE_ELEM; while ((rsz = read(fd, b + rlen, sizeof(b) - rlen - 1)) >= 0 && (rlen += rsz) > 0) { poff = parse_string(p, b, rlen, &pstate, rsz > 0); @@ -992,13 +990,70 @@ parse_file(struct parse *p, int fd, const char *fname) rlen -= poff; memmove(b, b + poff, rlen); } - if (rsz < 0) { - perror(fname); - p->tree->flags |= TREE_FAIL; + if (rsz < 0) + error_msg(p, "read: %s", strerror(errno)); +} + +/* + * Open and parse a file. + */ +struct ptree * +parse_file(struct parse *p, int fd, const char *fname) +{ + const char *save_fname; + int save_line, save_col; + + /* Save and initialize reporting data. */ + + save_fname = p->fname; + save_line = p->nline; + save_col = p->ncol; + p->fname = fname; + p->line = 0; + p->col = 0; + + /* Open the file, unless it is already open. */ + + if (fd == -1 && (fd = open(fname, O_RDONLY, 0)) == -1) { + error_msg(p, "open: %s", strerror(errno)); + p->fname = save_fname; + return p->tree; } - pnode_closetext(p); - if ((p->tree->flags & TREE_CLOSED) == 0) - warn_msg(p, "document not closed"); - pnode_unlink(p->doctype); + + /* + * After opening the starting file, change to the directory it + * is located in, in case it wants to include any further files, + * which are typically given with relative paths in DocBook. + * Do this on a best-effort basis; don't complain about failure. + */ + + if (save_fname == NULL && (fname = dirname(fname)) != NULL && + strcmp(fname, ".") != 0) + (void)chdir(fname); + + /* Run the read loop. */ + + p->nline = 1; + p->ncol = 1; + parse_fd(p, fd); + + /* On the top level, finalize the parse tree. */ + + if (save_fname == NULL) { + pnode_closetext(p); + if (p->tree->root == NULL) + error_msg(p, "empty document"); + else if ((p->tree->flags & TREE_CLOSED) == 0) + warn_msg(p, "document not closed"); + pnode_unlink(p->doctype); + } + + /* Clean up. */ + + if (fd != STDIN_FILENO) + close(fd); + p->fname = save_fname; + p->nline = save_line; + p->ncol = save_col; return p->tree; }