Remove building with NOCRYPTO option
[minix.git] / external / bsd / mdocml / dist / demandoc.c
blob6ed5a3f44b46d2d356bcc6bb02224057c27a26ed
1 /* Id: demandoc.c,v 1.7 2012/05/31 22:27:14 schwarze Exp */
2 /*
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.
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
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>
29 #include "man.h"
30 #include "mdoc.h"
31 #include "mandoc.h"
33 static void pline(int, int *, int *, int);
34 static void pman(const struct man_node *, int *, int *, int);
35 static void pmandoc(struct mparse *, int, const char *, int);
36 static void pmdoc(const struct mdoc_node *, int *, int *, int);
37 static void pstring(const char *, int, int *, int);
38 static void usage(void);
40 static const char *progname;
42 int
43 main(int argc, char *argv[])
45 struct mparse *mp;
46 int ch, i, list;
47 extern int optind;
49 progname = strrchr(argv[0], '/');
50 if (progname == NULL)
51 progname = argv[0];
52 else
53 ++progname;
55 mp = NULL;
56 list = 0;
58 while (-1 != (ch = getopt(argc, argv, "ikm:pw")))
59 switch (ch) {
60 case ('i'):
61 /* FALLTHROUGH */
62 case ('k'):
63 /* FALLTHROUGH */
64 case ('m'):
65 /* FALLTHROUGH */
66 case ('p'):
67 break;
68 case ('w'):
69 list = 1;
70 break;
71 default:
72 usage();
73 return((int)MANDOCLEVEL_BADARG);
76 argc -= optind;
77 argv += optind;
79 mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL, NULL);
80 assert(mp);
82 if (0 == argc)
83 pmandoc(mp, STDIN_FILENO, "<stdin>", list);
85 for (i = 0; i < argc; i++) {
86 mparse_reset(mp);
87 pmandoc(mp, -1, argv[i], list);
90 mparse_free(mp);
91 return((int)MANDOCLEVEL_OK);
94 static void
95 usage(void)
98 fprintf(stderr, "usage: %s [-w] [files...]\n", progname);
101 static void
102 pmandoc(struct mparse *mp, int fd, const char *fn, int list)
104 struct mdoc *mdoc;
105 struct man *man;
106 int line, col;
108 if (mparse_readfd(mp, fd, fn) >= MANDOCLEVEL_FATAL) {
109 fprintf(stderr, "%s: Parse failure\n", fn);
110 return;
113 mparse_result(mp, &mdoc, &man);
114 line = 1;
115 col = 0;
117 if (mdoc)
118 pmdoc(mdoc_node(mdoc), &line, &col, list);
119 else if (man)
120 pman(man_node(man), &line, &col, list);
121 else
122 return;
124 if ( ! list)
125 putchar('\n');
129 * Strip the escapes out of a string, emitting the results.
131 static void
132 pstring(const char *p, int col, int *colp, int list)
134 enum mandoc_esc esc;
135 const char *start, *end;
136 int emit;
139 * Print as many column spaces til we achieve parity with the
140 * input document.
143 again:
144 if (list && '\0' != *p) {
145 while (isspace((unsigned char)*p))
146 p++;
148 while ('\'' == *p || '(' == *p || '"' == *p)
149 p++;
151 emit = isalpha((unsigned char)p[0]) &&
152 isalpha((unsigned char)p[1]);
154 for (start = p; '\0' != *p; p++)
155 if ('\\' == *p) {
156 p++;
157 esc = mandoc_escape(&p, NULL, NULL);
158 if (ESCAPE_ERROR == esc)
159 return;
160 emit = 0;
161 } else if (isspace((unsigned char)*p))
162 break;
164 end = p - 1;
166 while (end > start)
167 if ('.' == *end || ',' == *end ||
168 '\'' == *end || '"' == *end ||
169 ')' == *end || '!' == *end ||
170 '?' == *end || ':' == *end ||
171 ';' == *end)
172 end--;
173 else
174 break;
176 if (emit && end - start >= 1) {
177 for ( ; start <= end; start++)
178 if (ASCII_HYPH == *start)
179 putchar('-');
180 else
181 putchar((unsigned char)*start);
182 putchar('\n');
185 if (isspace((unsigned char)*p))
186 goto again;
188 return;
191 while (*colp < col) {
192 putchar(' ');
193 (*colp)++;
197 * Print the input word, skipping any special characters.
199 while ('\0' != *p)
200 if ('\\' == *p) {
201 p++;
202 esc = mandoc_escape(&p, NULL, NULL);
203 if (ESCAPE_ERROR == esc)
204 break;
205 } else {
206 putchar((unsigned char )*p++);
207 (*colp)++;
211 static void
212 pline(int line, int *linep, int *col, int list)
215 if (list)
216 return;
219 * Print out as many lines as needed to reach parity with the
220 * original input.
223 while (*linep < line) {
224 putchar('\n');
225 (*linep)++;
228 *col = 0;
231 static void
232 pmdoc(const struct mdoc_node *p, int *line, int *col, int list)
235 for ( ; p; p = p->next) {
236 if (MDOC_LINE & p->flags)
237 pline(p->line, line, col, list);
238 if (MDOC_TEXT == p->type)
239 pstring(p->string, p->pos, col, list);
240 if (p->child)
241 pmdoc(p->child, line, col, list);
245 static void
246 pman(const struct man_node *p, int *line, int *col, int list)
249 for ( ; p; p = p->next) {
250 if (MAN_LINE & p->flags)
251 pline(p->line, line, col, list);
252 if (MAN_TEXT == p->type)
253 pstring(p->string, p->pos, col, list);
254 if (p->child)
255 pman(p->child, line, col, list);