Annotation of mandoc/demandoc.c, Revision 1.4
1.4 ! kristaps 1: /* $Id: demandoc.c,v 1.3 2011/09/01 10:49:13 kristaps 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: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
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:
29: #include "man.h"
30: #include "mdoc.h"
31: #include "mandoc.h"
32:
1.4 ! kristaps 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);
1.1 kristaps 38: static void usage(void);
39:
40: static const char *progname;
41:
42: int
43: main(int argc, char *argv[])
44: {
45: struct mparse *mp;
1.4 ! kristaps 46: int ch, i, list;
1.1 kristaps 47: extern int optind;
48:
49: progname = strrchr(argv[0], '/');
50: if (progname == NULL)
51: progname = argv[0];
52: else
53: ++progname;
54:
55: mp = NULL;
1.4 ! kristaps 56: list = 0;
1.1 kristaps 57:
1.4 ! kristaps 58: while (-1 != (ch = getopt(argc, argv, "ikm:pw")))
1.1 kristaps 59: switch (ch) {
1.4 ! kristaps 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;
1.1 kristaps 71: default:
72: usage();
73: return((int)MANDOCLEVEL_BADARG);
74: }
75:
76: argc -= optind;
77: argv += optind;
78:
79: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
80: assert(mp);
81:
82: if (0 == argc)
1.4 ! kristaps 83: pmandoc(mp, STDIN_FILENO, "<stdin>", list);
1.1 kristaps 84:
85: for (i = 0; i < argc; i++) {
86: mparse_reset(mp);
1.4 ! kristaps 87: pmandoc(mp, -1, argv[i], list);
1.1 kristaps 88: }
89:
90: mparse_free(mp);
1.3 kristaps 91: return((int)MANDOCLEVEL_OK);
1.1 kristaps 92: }
93:
94: static void
95: usage(void)
96: {
97:
1.4 ! kristaps 98: fprintf(stderr, "usage: %s [-w] [files...]\n", progname);
1.1 kristaps 99: }
100:
101: static void
1.4 ! kristaps 102: pmandoc(struct mparse *mp, int fd, const char *fn, int list)
1.1 kristaps 103: {
104: struct mdoc *mdoc;
105: struct man *man;
106: int line, col;
107:
108: if (mparse_readfd(mp, fd, fn) >= MANDOCLEVEL_FATAL) {
109: fprintf(stderr, "%s: Parse failure\n", fn);
110: return;
111: }
112:
113: mparse_result(mp, &mdoc, &man);
114: line = 1;
115: col = 0;
116:
117: if (mdoc)
1.4 ! kristaps 118: pmdoc(mdoc_node(mdoc), &line, &col, list);
1.1 kristaps 119: else if (man)
1.4 ! kristaps 120: pman(man_node(man), &line, &col, list);
1.1 kristaps 121: else
122: return;
123:
124: putchar('\n');
125: }
126:
127: /*
128: * Strip the escapes out of a string, emitting the results.
129: */
130: static void
1.4 ! kristaps 131: pstring(const char *p, int col, int *colp, int list)
1.1 kristaps 132: {
133: enum mandoc_esc esc;
134:
135: while (*colp < col) {
136: putchar(' ');
137: (*colp)++;
138: }
139:
1.4 ! kristaps 140: while ('\0' != *p)
1.1 kristaps 141: if ('\\' == *p) {
142: p++;
143: esc = mandoc_escape(&p, NULL, NULL);
144: if (ESCAPE_ERROR == esc)
1.4 ! kristaps 145: break;
1.1 kristaps 146: } else {
1.3 kristaps 147: putchar((unsigned char )*p++);
1.1 kristaps 148: (*colp)++;
149: }
150: }
151:
152: static void
1.4 ! kristaps 153: pline(int line, int *linep, int *col, int list)
1.1 kristaps 154: {
155:
156: while (*linep < line) {
157: putchar('\n');
158: (*linep)++;
159: }
1.4 ! kristaps 160:
1.1 kristaps 161: *col = 0;
162: }
163:
164: static void
1.4 ! kristaps 165: pmdoc(const struct mdoc_node *p, int *line, int *col, int list)
1.1 kristaps 166: {
167:
168: for ( ; p; p = p->next) {
169: if (MDOC_LINE & p->flags)
1.4 ! kristaps 170: pline(p->line, line, col, list);
1.1 kristaps 171: if (MDOC_TEXT == p->type)
1.4 ! kristaps 172: pstring(p->string, p->pos, col, list);
1.1 kristaps 173: if (p->child)
1.4 ! kristaps 174: pmdoc(p->child, line, col, list);
1.1 kristaps 175: }
176: }
177:
178: static void
1.4 ! kristaps 179: pman(const struct man_node *p, int *line, int *col, int list)
1.1 kristaps 180: {
181:
182: for ( ; p; p = p->next) {
183: if (MAN_LINE & p->flags)
1.4 ! kristaps 184: pline(p->line, line, col, list);
1.1 kristaps 185: if (MAN_TEXT == p->type)
1.4 ! kristaps 186: pstring(p->string, p->pos, col, list);
1.1 kristaps 187: if (p->child)
1.4 ! kristaps 188: pman(p->child, line, col, list);
1.1 kristaps 189: }
190: }
CVSweb