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