]>
git.cameronkatri.com Git - mandoc.git/blob - demandoc.c
1 /* $Id: demandoc.c,v 1.29 2017/06/24 14:38:32 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 roff_node
*, int *, int *, int);
35 static void pmandoc(struct mparse
*, int, const char *, int);
36 static void pmdoc(const struct roff_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
[])
50 progname
= "demandoc";
51 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
59 while (-1 != (ch
= getopt(argc
, argv
, "ikm:pw")))
74 return (int)MANDOCLEVEL_BADARG
;
81 mp
= mparse_alloc(MPARSE_SO
, MANDOCERR_MAX
, NULL
,
82 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_man
*man
;
115 mparse_readfd(mp
, fd
, fn
);
117 mparse_result(mp
, &man
, NULL
);
123 if (man
->macroset
== MACROSET_MDOC
) {
125 pmdoc(man
->first
->child
, &line
, &col
, list
);
128 pman(man
->first
->child
, &line
, &col
, list
);
136 * Strip the escapes out of a string, emitting the results.
139 pstring(const char *p
, int col
, int *colp
, int list
)
142 const char *start
, *end
;
146 * Print as many column spaces til we achieve parity with the
151 if (list
&& '\0' != *p
) {
152 while (isspace((unsigned char)*p
))
155 while ('\'' == *p
|| '(' == *p
|| '"' == *p
)
158 emit
= isalpha((unsigned char)p
[0]) &&
159 isalpha((unsigned char)p
[1]);
161 for (start
= p
; '\0' != *p
; p
++)
164 esc
= mandoc_escape(&p
, NULL
, NULL
);
165 if (ESCAPE_ERROR
== esc
)
168 } else if (isspace((unsigned char)*p
))
174 if ('.' == *end
|| ',' == *end
||
175 '\'' == *end
|| '"' == *end
||
176 ')' == *end
|| '!' == *end
||
177 '?' == *end
|| ':' == *end
||
183 if (emit
&& end
- start
>= 1) {
184 for ( ; start
<= end
; start
++)
185 if (ASCII_HYPH
== *start
)
188 putchar((unsigned char)*start
);
192 if (isspace((unsigned char)*p
))
198 while (*colp
< col
) {
204 * Print the input word, skipping any special characters.
209 esc
= mandoc_escape(&p
, NULL
, NULL
);
210 if (ESCAPE_ERROR
== esc
)
213 putchar((unsigned char )*p
++);
219 pline(int line
, int *linep
, int *col
, int list
)
226 * Print out as many lines as needed to reach parity with the
230 while (*linep
< line
) {
239 pmdoc(const struct roff_node
*p
, int *line
, int *col
, int list
)
242 for ( ; p
; p
= p
->next
) {
243 if (NODE_LINE
& p
->flags
)
244 pline(p
->line
, line
, col
, list
);
245 if (ROFFT_TEXT
== p
->type
)
246 pstring(p
->string
, p
->pos
, col
, list
);
248 pmdoc(p
->child
, line
, col
, list
);
253 pman(const struct roff_node
*p
, int *line
, int *col
, int list
)
256 for ( ; p
; p
= p
->next
) {
257 if (NODE_LINE
& p
->flags
)
258 pline(p
->line
, line
, col
, list
);
259 if (ROFFT_TEXT
== p
->type
)
260 pstring(p
->string
, p
->pos
, col
, list
);
262 pman(p
->child
, line
, col
, list
);