]>
git.cameronkatri.com Git - mandoc.git/blob - demandoc.c
1 /* $Id: demandoc.c,v 1.32 2018/12/30 00:49:54 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>
32 #include "mandoc_parse.h"
34 static void pline(int, int *, int *, int);
35 static void pman(const struct roff_node
*, int *, int *, int);
36 static void pmandoc(struct mparse
*, int, const char *, int);
37 static void pmdoc(const struct roff_node
*, int *, int *, int);
38 static void pstring(const char *, int, int *, int);
39 static void usage(void);
41 static const char *progname
;
44 main(int argc
, char *argv
[])
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
;
82 mp
= mparse_alloc(MPARSE_SO
| MPARSE_VALIDATE
, MANDOC_OS_OTHER
, NULL
);
86 pmandoc(mp
, STDIN_FILENO
, "<stdin>", list
);
88 for (i
= 0; i
< argc
; i
++) {
90 if ((fd
= mparse_open(mp
, argv
[i
])) == -1) {
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
)
112 struct roff_meta
*meta
;
115 mparse_readfd(mp
, fd
, fn
);
117 meta
= mparse_result(mp
);
121 if (meta
->macroset
== MACROSET_MDOC
)
122 pmdoc(meta
->first
->child
, &line
, &col
, list
);
124 pman(meta
->first
->child
, &line
, &col
, list
);
131 * Strip the escapes out of a string, emitting the results.
134 pstring(const char *p
, int col
, int *colp
, int list
)
137 const char *start
, *end
;
141 * Print as many column spaces til we achieve parity with the
146 if (list
&& '\0' != *p
) {
147 while (isspace((unsigned char)*p
))
150 while ('\'' == *p
|| '(' == *p
|| '"' == *p
)
153 emit
= isalpha((unsigned char)p
[0]) &&
154 isalpha((unsigned char)p
[1]);
156 for (start
= p
; '\0' != *p
; p
++)
159 esc
= mandoc_escape(&p
, NULL
, NULL
);
160 if (ESCAPE_ERROR
== esc
)
163 } else if (isspace((unsigned char)*p
))
169 if ('.' == *end
|| ',' == *end
||
170 '\'' == *end
|| '"' == *end
||
171 ')' == *end
|| '!' == *end
||
172 '?' == *end
|| ':' == *end
||
178 if (emit
&& end
- start
>= 1) {
179 for ( ; start
<= end
; start
++)
180 if (ASCII_HYPH
== *start
)
183 putchar((unsigned char)*start
);
187 if (isspace((unsigned char)*p
))
193 while (*colp
< col
) {
199 * Print the input word, skipping any special characters.
204 esc
= mandoc_escape(&p
, NULL
, NULL
);
205 if (ESCAPE_ERROR
== esc
)
208 putchar((unsigned char )*p
++);
214 pline(int line
, int *linep
, int *col
, int list
)
221 * Print out as many lines as needed to reach parity with the
225 while (*linep
< line
) {
234 pmdoc(const struct roff_node
*p
, int *line
, int *col
, int list
)
237 for ( ; p
; p
= p
->next
) {
238 if (NODE_LINE
& p
->flags
)
239 pline(p
->line
, line
, col
, list
);
240 if (ROFFT_TEXT
== p
->type
)
241 pstring(p
->string
, p
->pos
, col
, list
);
243 pmdoc(p
->child
, line
, col
, list
);
248 pman(const struct roff_node
*p
, int *line
, int *col
, int list
)
251 for ( ; p
; p
= p
->next
) {
252 if (NODE_LINE
& p
->flags
)
253 pline(p
->line
, line
, col
, list
);
254 if (ROFFT_TEXT
== p
->type
)
255 pstring(p
->string
, p
->pos
, col
, list
);
257 pman(p
->child
, line
, col
, list
);