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