From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from localhost (fantadrom.bsd.lv [local]) by fantadrom.bsd.lv (OpenSMTPD) with ESMTPA id ecfedf00 for ; Mon, 8 Apr 2019 17:48:04 -0500 (EST) Date: Mon, 8 Apr 2019 17:48:04 -0500 (EST) X-Mailinglist: mandoc-source Reply-To: source@mandoc.bsd.lv MIME-Version: 1.0 From: schwarze@mandoc.bsd.lv To: source@mandoc.bsd.lv Subject: docbook2mdoc: Make the function parse_file() fit for recursion: * Save X-Mailer: activitymail 1.26, http://search.cpan.org/dist/activitymail/ Content-Type: text/plain; charset=utf-8 Message-ID: Log Message: ----------- Make the function parse_file() fit for recursion: * Save and restore reporting data around the read loop. * Open the file inside the function, not before calling it. * On the top level, change directory on a best-effort basis. * Finalize the parse tree only on the top level. No new functionality yet. Modified Files: -------------- docbook2mdoc: main.c parse.c Revision Data ------------- Index: parse.c =================================================================== RCS file: /home/cvs/mdocml/docbook2mdoc/parse.c,v retrieving revision 1.23 retrieving revision 1.24 diff -Lparse.c -Lparse.c -u -p -r1.23 -r1.24 --- parse.c +++ parse.c @@ -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, s 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, cons 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, cons 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; } Index: main.c =================================================================== RCS file: /home/cvs/mdocml/docbook2mdoc/main.c,v retrieving revision 1.3 retrieving revision 1.4 diff -Lmain.c -Lmain.c -u -p -r1.3 -r1.4 --- main.c +++ main.c @@ -15,7 +15,6 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include #include #include #include @@ -64,40 +63,34 @@ main(int argc, char *argv[]) if (argc > 1) { fprintf(stderr, "%s: Too many arguments\n", argv[1]); goto usage; - } else - fname = argc > 0 ? argv[0] : "-"; + } else if (argc == 1) { + fname = argv[0]; + fd = -1; + } else { + fname = ""; + fd = STDIN_FILENO; + } - fd = strcmp(fname, "-") == 0 ? - STDIN_FILENO : open(fname, O_RDONLY, 0); + /* Parse. */ - if (fd == -1) { - perror(fname); + if ((parser = parse_alloc(warn)) == NULL) { + perror(NULL); return 1; } + tree = parse_file(parser, fd, fname); + rc = tree->flags & TREE_FAIL ? 1 : 0; - /* Parse and format. */ - - rc = 1; - if ((parser = parse_alloc(warn)) != NULL) { - if ((tree = parse_file(parser, fd, fname)) != NULL) { - if (tree->flags & TREE_FAIL) - fputc('\n', stderr); - else - rc = 0; - ptree_print(tree); - if (tree->flags & TREE_FAIL) - fputs("\nThe output may be incomplete, see" - " the parse error reported above.\n\n", - stderr); - pnode_unlink(tree->root); - tree->root = NULL; - } - parse_free(parser); - } else - perror(NULL); + /* Format. */ - if (fd != STDIN_FILENO) - close(fd); + if (tree->root != NULL) { + if (rc) + fputc('\n', stderr); + ptree_print(tree); + if (rc) + fputs("\nThe output may be incomplete, see the " + "parse error reported above.\n\n", stderr); + } + parse_free(parser); return rc; usage: -- To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv