]> git.cameronkatri.com Git - mandoc.git/blob - demandoc.c
In HTML output, man(7) .RS blocks get formatted as <div class="Bd-indent">,
[mandoc.git] / demandoc.c
1 /* $Id: demandoc.c,v 1.32 2018/12/30 00:49:54 schwarze Exp $ */
2 /*
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
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.
8 *
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.
16 */
17 #include "config.h"
18
19 #include <sys/types.h>
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "mandoc.h"
29 #include "roff.h"
30 #include "man.h"
31 #include "mdoc.h"
32 #include "mandoc_parse.h"
33
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);
40
41 static const char *progname;
42
43 int
44 main(int argc, char *argv[])
45 {
46 struct mparse *mp;
47 int ch, fd, i, list;
48 extern int optind;
49
50 if (argc < 1)
51 progname = "demandoc";
52 else if ((progname = strrchr(argv[0], '/')) == NULL)
53 progname = argv[0];
54 else
55 ++progname;
56
57 mp = NULL;
58 list = 0;
59
60 while (-1 != (ch = getopt(argc, argv, "ikm:pw")))
61 switch (ch) {
62 case ('i'):
63 /* FALLTHROUGH */
64 case ('k'):
65 /* FALLTHROUGH */
66 case ('m'):
67 /* FALLTHROUGH */
68 case ('p'):
69 break;
70 case ('w'):
71 list = 1;
72 break;
73 default:
74 usage();
75 return (int)MANDOCLEVEL_BADARG;
76 }
77
78 argc -= optind;
79 argv += optind;
80
81 mchars_alloc();
82 mp = mparse_alloc(MPARSE_SO | MPARSE_VALIDATE, MANDOC_OS_OTHER, NULL);
83 assert(mp);
84
85 if (argc < 1)
86 pmandoc(mp, STDIN_FILENO, "<stdin>", list);
87
88 for (i = 0; i < argc; i++) {
89 mparse_reset(mp);
90 if ((fd = mparse_open(mp, argv[i])) == -1) {
91 perror(argv[i]);
92 continue;
93 }
94 pmandoc(mp, fd, argv[i], list);
95 }
96
97 mparse_free(mp);
98 mchars_free();
99 return (int)MANDOCLEVEL_OK;
100 }
101
102 static void
103 usage(void)
104 {
105
106 fprintf(stderr, "usage: %s [-w] [files...]\n", progname);
107 }
108
109 static void
110 pmandoc(struct mparse *mp, int fd, const char *fn, int list)
111 {
112 struct roff_meta *meta;
113 int line, col;
114
115 mparse_readfd(mp, fd, fn);
116 close(fd);
117 meta = mparse_result(mp);
118 line = 1;
119 col = 0;
120
121 if (meta->macroset == MACROSET_MDOC)
122 pmdoc(meta->first->child, &line, &col, list);
123 else
124 pman(meta->first->child, &line, &col, list);
125
126 if ( ! list)
127 putchar('\n');
128 }
129
130 /*
131 * Strip the escapes out of a string, emitting the results.
132 */
133 static void
134 pstring(const char *p, int col, int *colp, int list)
135 {
136 enum mandoc_esc esc;
137 const char *start, *end;
138 int emit;
139
140 /*
141 * Print as many column spaces til we achieve parity with the
142 * input document.
143 */
144
145 again:
146 if (list && '\0' != *p) {
147 while (isspace((unsigned char)*p))
148 p++;
149
150 while ('\'' == *p || '(' == *p || '"' == *p)
151 p++;
152
153 emit = isalpha((unsigned char)p[0]) &&
154 isalpha((unsigned char)p[1]);
155
156 for (start = p; '\0' != *p; p++)
157 if ('\\' == *p) {
158 p++;
159 esc = mandoc_escape(&p, NULL, NULL);
160 if (ESCAPE_ERROR == esc)
161 return;
162 emit = 0;
163 } else if (isspace((unsigned char)*p))
164 break;
165
166 end = p - 1;
167
168 while (end > start)
169 if ('.' == *end || ',' == *end ||
170 '\'' == *end || '"' == *end ||
171 ')' == *end || '!' == *end ||
172 '?' == *end || ':' == *end ||
173 ';' == *end)
174 end--;
175 else
176 break;
177
178 if (emit && end - start >= 1) {
179 for ( ; start <= end; start++)
180 if (ASCII_HYPH == *start)
181 putchar('-');
182 else
183 putchar((unsigned char)*start);
184 putchar('\n');
185 }
186
187 if (isspace((unsigned char)*p))
188 goto again;
189
190 return;
191 }
192
193 while (*colp < col) {
194 putchar(' ');
195 (*colp)++;
196 }
197
198 /*
199 * Print the input word, skipping any special characters.
200 */
201 while ('\0' != *p)
202 if ('\\' == *p) {
203 p++;
204 esc = mandoc_escape(&p, NULL, NULL);
205 if (ESCAPE_ERROR == esc)
206 break;
207 } else {
208 putchar((unsigned char )*p++);
209 (*colp)++;
210 }
211 }
212
213 static void
214 pline(int line, int *linep, int *col, int list)
215 {
216
217 if (list)
218 return;
219
220 /*
221 * Print out as many lines as needed to reach parity with the
222 * original input.
223 */
224
225 while (*linep < line) {
226 putchar('\n');
227 (*linep)++;
228 }
229
230 *col = 0;
231 }
232
233 static void
234 pmdoc(const struct roff_node *p, int *line, int *col, int list)
235 {
236
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);
242 if (p->child)
243 pmdoc(p->child, line, col, list);
244 }
245 }
246
247 static void
248 pman(const struct roff_node *p, int *line, int *col, int list)
249 {
250
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);
256 if (p->child)
257 pman(p->child, line, col, list);
258 }
259 }