1 /* Id: demandoc.c,v 1.7 2012/05/31 22:27:14 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.
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
[])
49 progname
= strrchr(argv
[0], '/');
58 while (-1 != (ch
= getopt(argc
, argv
, "ikm:pw")))
73 return((int)MANDOCLEVEL_BADARG
);
79 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
, NULL
);
83 pmandoc(mp
, STDIN_FILENO
, "<stdin>", list
);
85 for (i
= 0; i
< argc
; i
++) {
87 pmandoc(mp
, -1, argv
[i
], list
);
91 return((int)MANDOCLEVEL_OK
);
98 fprintf(stderr
, "usage: %s [-w] [files...]\n", progname
);
102 pmandoc(struct mparse
*mp
, int fd
, const char *fn
, int list
)
108 if (mparse_readfd(mp
, fd
, fn
) >= MANDOCLEVEL_FATAL
) {
109 fprintf(stderr
, "%s: Parse failure\n", fn
);
113 mparse_result(mp
, &mdoc
, &man
);
118 pmdoc(mdoc_node(mdoc
), &line
, &col
, list
);
120 pman(man_node(man
), &line
, &col
, list
);
129 * Strip the escapes out of a string, emitting the results.
132 pstring(const char *p
, int col
, int *colp
, int list
)
135 const char *start
, *end
;
139 * Print as many column spaces til we achieve parity with the
144 if (list
&& '\0' != *p
) {
145 while (isspace((unsigned char)*p
))
148 while ('\'' == *p
|| '(' == *p
|| '"' == *p
)
151 emit
= isalpha((unsigned char)p
[0]) &&
152 isalpha((unsigned char)p
[1]);
154 for (start
= p
; '\0' != *p
; p
++)
157 esc
= mandoc_escape(&p
, NULL
, NULL
);
158 if (ESCAPE_ERROR
== esc
)
161 } else if (isspace((unsigned char)*p
))
167 if ('.' == *end
|| ',' == *end
||
168 '\'' == *end
|| '"' == *end
||
169 ')' == *end
|| '!' == *end
||
170 '?' == *end
|| ':' == *end
||
176 if (emit
&& end
- start
>= 1) {
177 for ( ; start
<= end
; start
++)
178 if (ASCII_HYPH
== *start
)
181 putchar((unsigned char)*start
);
185 if (isspace((unsigned char)*p
))
191 while (*colp
< col
) {
197 * Print the input word, skipping any special characters.
202 esc
= mandoc_escape(&p
, NULL
, NULL
);
203 if (ESCAPE_ERROR
== esc
)
206 putchar((unsigned char )*p
++);
212 pline(int line
, int *linep
, int *col
, int list
)
219 * Print out as many lines as needed to reach parity with the
223 while (*linep
< line
) {
232 pmdoc(const struct mdoc_node
*p
, int *line
, int *col
, int list
)
235 for ( ; p
; p
= p
->next
) {
236 if (MDOC_LINE
& p
->flags
)
237 pline(p
->line
, line
, col
, list
);
238 if (MDOC_TEXT
== p
->type
)
239 pstring(p
->string
, p
->pos
, col
, list
);
241 pmdoc(p
->child
, line
, col
, list
);
246 pman(const struct man_node
*p
, int *line
, int *col
, int list
)
249 for ( ; p
; p
= p
->next
) {
250 if (MAN_LINE
& p
->flags
)
251 pline(p
->line
, line
, col
, list
);
252 if (MAN_TEXT
== p
->type
)
253 pstring(p
->string
, p
->pos
, col
, list
);
255 pman(p
->child
, line
, col
, list
);