1 /* $Id: demandoc.c,v 1.15 2015/02/10 08:05:30 schwarze Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
33 static void pline(int, int *, int *, int);
34 static void pman(const struct man_node *, int *, int *, int);
35 static void pmandoc(struct mparse *, int, const char *, int);
36 static void pmdoc(const struct mdoc_node *, int *, int *, int);
37 static void pstring(const char *, int, int *, int);
38 static void usage(void);
40 static const char *progname;
43 main(int argc, char *argv[])
46 struct mchars *mchars;
51 progname = "demandoc";
52 else if ((progname = strrchr(argv[0], '/')) == NULL)
60 while (-1 != (ch = getopt(argc, argv, "ikm:pw")))
75 return((int)MANDOCLEVEL_BADARG);
81 mchars = mchars_alloc();
82 mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, mchars, NULL);
86 pmandoc(mp, STDIN_FILENO, "<stdin>", list);
88 for (i = 0; i < argc; i++) {
90 if (mparse_open(mp, &fd, argv[i]) != MANDOCLEVEL_OK) {
94 pmandoc(mp, fd, argv[i], list);
99 return((int)MANDOCLEVEL_OK);
106 fprintf(stderr, "usage: %s [-w] [files...]\n", progname);
110 pmandoc(struct mparse *mp, int fd, const char *fn, int list)
116 mparse_readfd(mp, fd, fn);
117 mparse_result(mp, &mdoc, &man, NULL);
122 pmdoc(mdoc_node(mdoc), &line, &col, list);
124 pman(man_node(man), &line, &col, list);
133 * Strip the escapes out of a string, emitting the results.
136 pstring(const char *p, int col, int *colp, int list)
139 const char *start, *end;
143 * Print as many column spaces til we achieve parity with the
148 if (list && '\0' != *p) {
149 while (isspace((unsigned char)*p))
152 while ('\'' == *p || '(' == *p || '"' == *p)
155 emit = isalpha((unsigned char)p[0]) &&
156 isalpha((unsigned char)p[1]);
158 for (start = p; '\0' != *p; p++)
161 esc = mandoc_escape(&p, NULL, NULL);
162 if (ESCAPE_ERROR == esc)
165 } else if (isspace((unsigned char)*p))
171 if ('.' == *end || ',' == *end ||
172 '\'' == *end || '"' == *end ||
173 ')' == *end || '!' == *end ||
174 '?' == *end || ':' == *end ||
180 if (emit && end - start >= 1) {
181 for ( ; start <= end; start++)
182 if (ASCII_HYPH == *start)
185 putchar((unsigned char)*start);
189 if (isspace((unsigned char)*p))
195 while (*colp < col) {
201 * Print the input word, skipping any special characters.
206 esc = mandoc_escape(&p, NULL, NULL);
207 if (ESCAPE_ERROR == esc)
210 putchar((unsigned char )*p++);
216 pline(int line, int *linep, int *col, int list)
223 * Print out as many lines as needed to reach parity with the
227 while (*linep < line) {
236 pmdoc(const struct mdoc_node *p, int *line, int *col, int list)
239 for ( ; p; p = p->next) {
240 if (MDOC_LINE & p->flags)
241 pline(p->line, line, col, list);
242 if (MDOC_TEXT == p->type)
243 pstring(p->string, p->pos, col, list);
245 pmdoc(p->child, line, col, list);
250 pman(const struct man_node *p, int *line, int *col, int list)
253 for ( ; p; p = p->next) {
254 if (MAN_LINE & p->flags)
255 pline(p->line, line, col, list);
256 if (MAN_TEXT == p->type)
257 pstring(p->string, p->pos, col, list);
259 pman(p->child, line, col, list);