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

Annotation of docbook2mdoc/main.c, Revision 1.1

1.1     ! schwarze    1: /* $Id$ */
        !             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) {
        !            83:                        if ((tree->flags & TREE_FAIL) == 0)
        !            84:                                rc = 0;
        !            85:                        ptree_print(tree);
        !            86:                        pnode_unlink(tree->root);
        !            87:                        tree->root = NULL;
        !            88:                }
        !            89:                parse_free(parser);
        !            90:        } else
        !            91:                perror(NULL);
        !            92:
        !            93:        if (fd != STDIN_FILENO)
        !            94:                close(fd);
        !            95:        return rc;
        !            96:
        !            97: usage:
        !            98:        fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname);
        !            99:        return 1;
        !           100: }

CVSweb