[BACK]Return to main.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / docbook2mdoc

Annotation of docbook2mdoc/main.c, Revision 1.3

1.3     ! schwarze    1: /* $Id: main.c,v 1.2 2019/03/26 20:06:16 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #include <fcntl.h>
                     19: #include <getopt.h>
                     20: #include <stdio.h>
                     21: #include <string.h>
                     22: #include <unistd.h>
                     23:
                     24: #include "node.h"
                     25: #include "parse.h"
                     26: #include "format.h"
                     27:
                     28: /*
                     29:  * The steering function of the docbook2mdoc(1) program.
                     30:  */
                     31:
                     32: int
                     33: main(int argc, char *argv[])
                     34: {
                     35:        struct parse    *parser;
                     36:        struct ptree    *tree;
                     37:        const char      *progname;
                     38:        const char      *fname;
                     39:        int              ch, fd, rc, warn;
                     40:
                     41:        if ((progname = strrchr(argv[0], '/')) == NULL)
                     42:                progname = argv[0];
                     43:        else
                     44:                progname++;
                     45:
                     46:        warn = 0;
                     47:        while ((ch = getopt(argc, argv, "W")) != -1) {
                     48:                switch (ch) {
                     49:                case 'W':
                     50:                        warn = 1;
                     51:                        break;
                     52:                default:
                     53:                        goto usage;
                     54:                }
                     55:        }
                     56:        argc -= optind;
                     57:        argv += optind;
                     58:
                     59:        /*
                     60:         * Argument processing:
                     61:         * Open file or use standard input.
                     62:         */
                     63:
                     64:        if (argc > 1) {
                     65:                fprintf(stderr, "%s: Too many arguments\n", argv[1]);
                     66:                goto usage;
                     67:        } else
                     68:                fname = argc > 0 ? argv[0] : "-";
                     69:
                     70:        fd = strcmp(fname, "-") == 0 ?
                     71:                STDIN_FILENO : open(fname, O_RDONLY, 0);
                     72:
                     73:        if (fd == -1) {
                     74:                perror(fname);
                     75:                return 1;
                     76:        }
                     77:
                     78:        /* Parse and format. */
                     79:
                     80:        rc = 1;
                     81:        if ((parser = parse_alloc(warn)) != NULL) {
                     82:                if ((tree = parse_file(parser, fd, fname)) != NULL) {
1.2       schwarze   83:                        if (tree->flags & TREE_FAIL)
                     84:                                fputc('\n', stderr);
                     85:                        else
1.1       schwarze   86:                                rc = 0;
                     87:                        ptree_print(tree);
1.2       schwarze   88:                        if (tree->flags & TREE_FAIL)
1.3     ! schwarze   89:                                fputs("\nThe output may be incomplete, see"
        !            90:                                    " the parse error reported above.\n\n",
1.2       schwarze   91:                                    stderr);
1.1       schwarze   92:                        pnode_unlink(tree->root);
                     93:                        tree->root = NULL;
                     94:                }
                     95:                parse_free(parser);
                     96:        } else
                     97:                perror(NULL);
                     98:
                     99:        if (fd != STDIN_FILENO)
                    100:                close(fd);
                    101:        return rc;
                    102:
                    103: usage:
                    104:        fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname);
                    105:        return 1;
                    106: }

CVSweb