]>
git.cameronkatri.com Git - mandoc.git/blob - demandoc.c
1 /* $Id: demandoc.c,v 1.34 2022/04/14 16:43:43 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>
31 #include "mandoc_dbg.h"
36 #include "mandoc_parse.h"
38 static void pline(int, int *, int *, int);
39 static void pman(const struct roff_node
*, int *, int *, int);
40 static void pmandoc(struct mparse
*, int, const char *, int);
41 static void pmdoc(const struct roff_node
*, int *, int *, int);
42 static void pstring(const char *, int, int *, int);
43 static void usage(void);
45 static const char *progname
;
48 main(int argc
, char *argv
[])
55 mandoc_dbg_init(argc
, argv
);
59 progname
= "demandoc";
60 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
68 while (-1 != (ch
= getopt(argc
, argv
, "ikm:pw")))
83 return (int)MANDOCLEVEL_BADARG
;
90 mp
= mparse_alloc(MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
|
91 MPARSE_VALIDATE
, MANDOC_OS_OTHER
, NULL
);
95 pmandoc(mp
, STDIN_FILENO
, "<stdin>", list
);
97 for (i
= 0; i
< argc
; i
++) {
99 if ((fd
= mparse_open(mp
, argv
[i
])) == -1) {
103 pmandoc(mp
, fd
, argv
[i
], list
);
111 return (int)MANDOCLEVEL_OK
;
118 fprintf(stderr
, "usage: %s [-w] [files...]\n", progname
);
122 pmandoc(struct mparse
*mp
, int fd
, const char *fn
, int list
)
124 struct roff_meta
*meta
;
127 mparse_readfd(mp
, fd
, fn
);
129 meta
= mparse_result(mp
);
133 if (meta
->macroset
== MACROSET_MDOC
)
134 pmdoc(meta
->first
->child
, &line
, &col
, list
);
136 pman(meta
->first
->child
, &line
, &col
, list
);
143 * Strip the escapes out of a string, emitting the results.
146 pstring(const char *p
, int col
, int *colp
, int list
)
149 const char *start
, *end
;
153 * Print as many column spaces til we achieve parity with the
158 if (list
&& '\0' != *p
) {
159 while (isspace((unsigned char)*p
))
162 while ('\'' == *p
|| '(' == *p
|| '"' == *p
)
165 emit
= isalpha((unsigned char)p
[0]) &&
166 isalpha((unsigned char)p
[1]);
168 for (start
= p
; '\0' != *p
; p
++)
171 esc
= mandoc_escape(&p
, NULL
, NULL
);
172 if (ESCAPE_ERROR
== esc
)
175 } else if (isspace((unsigned char)*p
))
181 if ('.' == *end
|| ',' == *end
||
182 '\'' == *end
|| '"' == *end
||
183 ')' == *end
|| '!' == *end
||
184 '?' == *end
|| ':' == *end
||
190 if (emit
&& end
- start
>= 1) {
191 for ( ; start
<= end
; start
++)
192 if (ASCII_HYPH
== *start
)
195 putchar((unsigned char)*start
);
199 if (isspace((unsigned char)*p
))
205 while (*colp
< col
) {
211 * Print the input word, skipping any special characters.
216 esc
= mandoc_escape(&p
, NULL
, NULL
);
217 if (ESCAPE_ERROR
== esc
)
220 putchar((unsigned char )*p
++);
226 pline(int line
, int *linep
, int *col
, int list
)
233 * Print out as many lines as needed to reach parity with the
237 while (*linep
< line
) {
246 pmdoc(const struct roff_node
*p
, int *line
, int *col
, int list
)
249 for ( ; p
; p
= p
->next
) {
250 if (NODE_LINE
& p
->flags
)
251 pline(p
->line
, line
, col
, list
);
252 if (ROFFT_TEXT
== p
->type
)
253 pstring(p
->string
, p
->pos
, col
, list
);
255 pmdoc(p
->child
, line
, col
, list
);
260 pman(const struct roff_node
*p
, int *line
, int *col
, int list
)
263 for ( ; p
; p
= p
->next
) {
264 if (NODE_LINE
& p
->flags
)
265 pline(p
->line
, line
, col
, list
);
266 if (ROFFT_TEXT
== p
->type
)
267 pstring(p
->string
, p
->pos
, col
, list
);
269 pman(p
->child
, line
, col
, list
);