Annotation of docbook2mdoc/main.c, Revision 1.7
1.7 ! schwarze 1: /* $Id: main.c,v 1.6 2019/04/12 19:14:50 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 <getopt.h>
19: #include <stdio.h>
20: #include <string.h>
21: #include <unistd.h>
22:
23: #include "node.h"
24: #include "parse.h"
25: #include "format.h"
26:
27: /*
28: * The steering function of the docbook2mdoc(1) program.
29: */
30:
1.6 schwarze 31: enum outt {
32: OUTT_MDOC = 0,
33: OUTT_TREE,
34: OUTT_LINT
35: };
36:
1.1 schwarze 37: int
38: main(int argc, char *argv[])
39: {
40: struct parse *parser;
41: struct ptree *tree;
42: const char *progname;
43: const char *fname;
44: int ch, fd, rc, warn;
1.6 schwarze 45: enum outt outtype;
1.1 schwarze 46:
47: if ((progname = strrchr(argv[0], '/')) == NULL)
48: progname = argv[0];
49: else
50: progname++;
51:
52: warn = 0;
1.6 schwarze 53: outtype = OUTT_MDOC;
54: while ((ch = getopt(argc, argv, "T:W")) != -1) {
1.1 schwarze 55: switch (ch) {
1.6 schwarze 56: case 'T':
57: if (strcmp(optarg, "mdoc") == 0)
58: outtype = OUTT_MDOC;
59: else if (strcmp(optarg, "tree") == 0)
60: outtype = OUTT_TREE;
61: else if (strcmp(optarg, "lint") == 0)
62: outtype = OUTT_LINT;
63: else {
64: fprintf(stderr, "%s: Bad argument\n",
65: optarg);
66: goto usage;
67: }
68: break;
1.1 schwarze 69: case 'W':
70: warn = 1;
71: break;
72: default:
73: goto usage;
74: }
75: }
76: argc -= optind;
77: argv += optind;
78:
79: /*
80: * Argument processing:
81: * Open file or use standard input.
82: */
83:
84: if (argc > 1) {
85: fprintf(stderr, "%s: Too many arguments\n", argv[1]);
86: goto usage;
1.4 schwarze 87: } else if (argc == 1) {
88: fname = argv[0];
89: fd = -1;
90: } else {
91: fname = "<stdin>";
92: fd = STDIN_FILENO;
93: }
1.1 schwarze 94:
1.4 schwarze 95: /* Parse. */
1.1 schwarze 96:
1.7 ! schwarze 97: parser = parse_alloc(warn);
1.4 schwarze 98: tree = parse_file(parser, fd, fname);
1.5 schwarze 99: rc = tree->flags & TREE_ERROR ? 3 : tree->flags & TREE_WARN ? 2 : 0;
1.1 schwarze 100:
1.4 schwarze 101: /* Format. */
1.1 schwarze 102:
1.6 schwarze 103: if (outtype != OUTT_LINT && tree->root != NULL) {
1.5 schwarze 104: if (rc > 2)
1.4 schwarze 105: fputc('\n', stderr);
1.6 schwarze 106: if (outtype == OUTT_MDOC)
107: ptree_print_mdoc(tree);
108: else
109: ptree_print_tree(tree);
1.5 schwarze 110: if (rc > 2)
1.4 schwarze 111: fputs("\nThe output may be incomplete, see the "
112: "parse error reported above.\n\n", stderr);
113: }
114: parse_free(parser);
1.1 schwarze 115: return rc;
116:
117: usage:
1.6 schwarze 118: fprintf(stderr, "usage: %s [-W] [-T mdoc | tree | lint] "
119: "[input_filename]\n", progname);
1.5 schwarze 120: return 5;
1.1 schwarze 121: }
CVSweb