1 /* $Id: demandoc.c,v 1.12 2014/10/28 17:36:19 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;
50 progname = strrchr(argv[0], '/');
59 while (-1 != (ch = getopt(argc, argv, "ikm:pw")))
74 return((int)MANDOCLEVEL_BADARG);
80 mchars = mchars_alloc();
81 mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL, mchars, NULL);
85 pmandoc(mp, STDIN_FILENO, "<stdin>", list);
87 for (i = 0; i < argc; i++) {
89 pmandoc(mp, -1, argv[i], list);
94 return((int)MANDOCLEVEL_OK);
101 fprintf(stderr, "usage: %s [-w] [files...]\n", progname);
105 pmandoc(struct mparse *mp, int fd, const char *fn, int list)
111 if (mparse_readfd(mp, fd, fn) >= MANDOCLEVEL_FATAL) {
112 fprintf(stderr, "%s: Parse failure\n", fn);
116 mparse_result(mp, &mdoc, &man, NULL);
121 pmdoc(mdoc_node(mdoc), &line, &col, list);
123 pman(man_node(man), &line, &col, list);
132 * Strip the escapes out of a string, emitting the results.
135 pstring(const char *p, int col, int *colp, int list)
138 const char *start, *end;
142 * Print as many column spaces til we achieve parity with the
147 if (list && '\0' != *p) {
148 while (isspace((unsigned char)*p))
151 while ('\'' == *p || '(' == *p || '"' == *p)
154 emit = isalpha((unsigned char)p[0]) &&
155 isalpha((unsigned char)p[1]);
157 for (start = p; '\0' != *p; p++)
160 esc = mandoc_escape(&p, NULL, NULL);
161 if (ESCAPE_ERROR == esc)
164 } else if (isspace((unsigned char)*p))
170 if ('.' == *end || ',' == *end ||
171 '\'' == *end || '"' == *end ||
172 ')' == *end || '!' == *end ||
173 '?' == *end || ':' == *end ||
179 if (emit && end - start >= 1) {
180 for ( ; start <= end; start++)
181 if (ASCII_HYPH == *start)
184 putchar((unsigned char)*start);
188 if (isspace((unsigned char)*p))
194 while (*colp < col) {
200 * Print the input word, skipping any special characters.
205 esc = mandoc_escape(&p, NULL, NULL);
206 if (ESCAPE_ERROR == esc)
209 putchar((unsigned char )*p++);
215 pline(int line, int *linep, int *col, int list)
222 * Print out as many lines as needed to reach parity with the
226 while (*linep < line) {
235 pmdoc(const struct mdoc_node *p, int *line, int *col, int list)
238 for ( ; p; p = p->next) {
239 if (MDOC_LINE & p->flags)
240 pline(p->line, line, col, list);
241 if (MDOC_TEXT == p->type)
242 pstring(p->string, p->pos, col, list);
244 pmdoc(p->child, line, col, list);
249 pman(const struct man_node *p, int *line, int *col, int list)
252 for ( ; p; p = p->next) {
253 if (MAN_LINE & p->flags)
254 pline(p->line, line, col, list);
255 if (MAN_TEXT == p->type)
256 pstring(p->string, p->pos, col, list);
258 pman(p->child, line, col, list);