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