version 1.1, 2019/03/26 18:32:07 |
version 1.10, 2019/05/01 09:02:25 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
#include <fcntl.h> |
|
#include <getopt.h> |
#include <getopt.h> |
|
#include <libgen.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
#include "node.h" |
#include "node.h" |
#include "parse.h" |
#include "parse.h" |
|
#include "reorg.h" |
#include "format.h" |
#include "format.h" |
|
|
/* |
/* |
* The steering function of the docbook2mdoc(1) program. |
* The steering function of the docbook2mdoc(1) program. |
*/ |
*/ |
|
|
|
enum outt { |
|
OUTT_MDOC = 0, |
|
OUTT_TREE, |
|
OUTT_LINT |
|
}; |
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
struct parse *parser; |
struct parse *parser; |
struct ptree *tree; |
struct ptree *tree; |
const char *progname; |
const char *progname; |
const char *fname; |
const char *bname, *fname, *sec; |
int ch, fd, rc, warn; |
int ch, fd, rc, warn; |
|
enum outt outtype; |
|
|
if ((progname = strrchr(argv[0], '/')) == NULL) |
if ((progname = strrchr(argv[0], '/')) == NULL) |
progname = argv[0]; |
progname = argv[0]; |
else |
else |
progname++; |
progname++; |
|
|
|
sec = NULL; |
warn = 0; |
warn = 0; |
while ((ch = getopt(argc, argv, "W")) != -1) { |
outtype = OUTT_MDOC; |
|
while ((ch = getopt(argc, argv, "s:T:W")) != -1) { |
switch (ch) { |
switch (ch) { |
|
case 's': |
|
sec = optarg; |
|
break; |
|
case 'T': |
|
if (strcmp(optarg, "mdoc") == 0) |
|
outtype = OUTT_MDOC; |
|
else if (strcmp(optarg, "tree") == 0) |
|
outtype = OUTT_TREE; |
|
else if (strcmp(optarg, "lint") == 0) |
|
outtype = OUTT_LINT; |
|
else { |
|
fprintf(stderr, "%s: Bad argument\n", |
|
optarg); |
|
goto usage; |
|
} |
|
break; |
case 'W': |
case 'W': |
warn = 1; |
warn = 1; |
break; |
break; |
Line 64 main(int argc, char *argv[]) |
|
Line 90 main(int argc, char *argv[]) |
|
if (argc > 1) { |
if (argc > 1) { |
fprintf(stderr, "%s: Too many arguments\n", argv[1]); |
fprintf(stderr, "%s: Too many arguments\n", argv[1]); |
goto usage; |
goto usage; |
} else |
} else if (argc == 1) { |
fname = argc > 0 ? argv[0] : "-"; |
fname = argv[0]; |
|
fd = -1; |
fd = strcmp(fname, "-") == 0 ? |
} else { |
STDIN_FILENO : open(fname, O_RDONLY, 0); |
fname = "<stdin>"; |
|
fd = STDIN_FILENO; |
if (fd == -1) { |
|
perror(fname); |
|
return 1; |
|
} |
} |
|
|
/* Parse and format. */ |
/* Parse. */ |
|
|
rc = 1; |
parser = parse_alloc(warn); |
if ((parser = parse_alloc(warn)) != NULL) { |
tree = parse_file(parser, fd, fname); |
if ((tree = parse_file(parser, fd, fname)) != NULL) { |
ptree_reorg(tree, sec); |
if ((tree->flags & TREE_FAIL) == 0) |
rc = tree->flags & TREE_ERROR ? 3 : tree->flags & TREE_WARN ? 2 : 0; |
rc = 0; |
|
ptree_print(tree); |
|
pnode_unlink(tree->root); |
|
tree->root = NULL; |
|
} |
|
parse_free(parser); |
|
} else |
|
perror(NULL); |
|
|
|
if (fd != STDIN_FILENO) |
/* Format. */ |
close(fd); |
|
|
if (outtype != OUTT_LINT && tree->root != NULL) { |
|
if (rc > 2) |
|
fputc('\n', stderr); |
|
if (outtype == OUTT_MDOC) { |
|
if (fd == -1 && (bname = basename(fname)) != NULL) |
|
printf(".\\\" automatically generated " |
|
"with %s %s\n", progname, bname); |
|
ptree_print_mdoc(tree); |
|
} else |
|
ptree_print_tree(tree); |
|
if (rc > 2) |
|
fputs("\nThe output may be incomplete, see the " |
|
"parse error reported above.\n\n", stderr); |
|
} |
|
parse_free(parser); |
return rc; |
return rc; |
|
|
usage: |
usage: |
fprintf(stderr, "usage: %s [-W] [input_filename]\n", progname); |
fprintf(stderr, "usage: %s [-W] [-s section] " |
return 1; |
"[-T mdoc | tree | lint] [input_filename]\n", progname); |
|
return 5; |
} |
} |