Annotation of mandoc/mdocml.c, Revision 1.19
1.19 ! kristaps 1: /* $Id: mdocml.c,v 1.18 2008/12/09 00:27:17 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
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
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <sys/param.h>
20: #include <sys/stat.h>
21:
22: #include <assert.h>
23: #include <err.h>
24: #include <fcntl.h>
25: #include <getopt.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
31: #include "libmdocml.h"
32:
1.9 kristaps 33: #define BUFFER_IN_DEF BUFSIZ /* See begin_bufs. */
34: #define BUFFER_OUT_DEF BUFSIZ /* See begin_bufs. */
1.2 kristaps 35:
1.17 kristaps 36: #ifdef DEBUG
37: #define CSS "mdocml.css"
38: #else
39: #define CSS "/usr/local/share/mdocml/mdocml.css"
40: #endif
41:
1.9 kristaps 42: static void usage(void);
43:
44: static int begin_io(const struct md_args *,
1.5 kristaps 45: char *, char *);
1.9 kristaps 46: static int leave_io(const struct md_buf *,
1.5 kristaps 47: const struct md_buf *, int);
1.9 kristaps 48: static int begin_bufs(const struct md_args *,
1.5 kristaps 49: struct md_buf *, struct md_buf *);
50: static int leave_bufs(const struct md_buf *,
51: const struct md_buf *, int);
1.1 kristaps 52:
1.19 ! kristaps 53: #ifdef __linux__
! 54: extern int getsubopt(char **, char *const *, char **);
! 55: #endif
! 56:
1.1 kristaps 57: int
58: main(int argc, char *argv[])
59: {
60: int c;
1.18 kristaps 61: char *out, *in, *opts, *v;
1.5 kristaps 62: struct md_args args;
1.18 kristaps 63: #define ALL 0
64: #define ERROR 1
65: char *toks[] = { "all", "error", NULL };
1.1 kristaps 66:
67: extern char *optarg;
68: extern int optind;
69:
1.17 kristaps 70: out = in = NULL;
1.10 kristaps 71:
72: (void)memset(&args, 0, sizeof(struct md_args));
1.17 kristaps 73:
74: args.type = MD_XML;
75:
1.18 kristaps 76: while (-1 != (c = getopt(argc, argv, "c:ef:o:vW:")))
1.1 kristaps 77: switch (c) {
1.17 kristaps 78: case ('c'):
79: if (args.type != MD_HTML)
80: errx(1, "-c only valid for -fhtml");
81: args.params.html.css = optarg;
82: break;
83: case ('e'):
84: if (args.type != MD_HTML)
85: errx(1, "-e only valid for -fhtml");
86: args.params.html.flags |= HTML_CSS_EMBED;
87: break;
1.16 kristaps 88: case ('f'):
1.17 kristaps 89: if (0 == strcmp(optarg, "html"))
90: args.type = MD_HTML;
91: else if (0 == strcmp(optarg, "xml"))
92: args.type = MD_XML;
93: else
94: errx(1, "invalid filter type");
1.16 kristaps 95: break;
1.1 kristaps 96: case ('o'):
97: out = optarg;
98: break;
1.13 kristaps 99: case ('v'):
100: args.verbosity++;
101: break;
102: case ('W'):
1.18 kristaps 103: opts = optarg;
104: while (*opts)
105: switch (getsubopt(&opts, toks, &v)) {
106: case (ALL):
107: args.warnings |= MD_WARN_ALL;
108: break;
109: case (ERROR):
110: args.warnings |= MD_WARN_ERROR;
111: break;
112: default:
113: usage();
114: return(1);
115: }
1.13 kristaps 116: break;
1.1 kristaps 117: default:
118: usage();
119: return(1);
120: }
121:
1.17 kristaps 122: if (MD_HTML == args.type)
123: if (NULL == args.params.html.css)
124: args.params.html.css = CSS;
125:
1.1 kristaps 126: argv += optind;
1.4 kristaps 127: argc -= optind;
1.1 kristaps 128:
1.4 kristaps 129: if (1 == argc)
130: in = *argv++;
1.1 kristaps 131:
1.5 kristaps 132: return(begin_io(&args, out ? out : "-", in ? in : "-"));
1.1 kristaps 133: }
134:
135:
1.9 kristaps 136: /*
137: * Close out file descriptors opened in begin_io. If the descriptor
138: * refers to stdin/stdout, then do nothing.
139: */
1.1 kristaps 140: static int
1.5 kristaps 141: leave_io(const struct md_buf *out,
142: const struct md_buf *in, int c)
1.1 kristaps 143: {
1.4 kristaps 144: assert(out);
1.1 kristaps 145: assert(in);
146:
1.4 kristaps 147: if (-1 != in->fd && -1 == close(in->fd)) {
148: assert(in->name);
149: warn("%s", in->name);
150: c = 1;
151: }
152: if (-1 != out->fd && STDOUT_FILENO != out->fd &&
153: -1 == close(out->fd)) {
154: assert(out->name);
155: warn("%s", out->name);
156: c = 1;
157: }
1.15 kristaps 158: if (1 == c && STDOUT_FILENO != out->fd)
159: if (-1 == unlink(out->name))
160: warn("%s", out->name);
1.1 kristaps 161:
1.4 kristaps 162: return(c);
1.1 kristaps 163: }
164:
165:
1.9 kristaps 166: /*
167: * Open file descriptors or assign stdin/stdout, if dictated by the "-"
168: * token instead of a filename.
169: */
1.1 kristaps 170: static int
1.5 kristaps 171: begin_io(const struct md_args *args, char *out, char *in)
1.1 kristaps 172: {
1.5 kristaps 173: struct md_buf fi;
174: struct md_buf fo;
1.4 kristaps 175:
176: #define FI_FL O_RDONLY
177: #define FO_FL O_WRONLY|O_CREAT|O_TRUNC
1.1 kristaps 178:
1.5 kristaps 179: assert(args);
1.1 kristaps 180: assert(out);
181: assert(in);
182:
1.5 kristaps 183: bzero(&fi, sizeof(struct md_buf));
184: bzero(&fo, sizeof(struct md_buf));
1.1 kristaps 185:
1.4 kristaps 186: fi.fd = STDIN_FILENO;
187: fo.fd = STDOUT_FILENO;
1.1 kristaps 188:
1.4 kristaps 189: fi.name = in;
190: fo.name = out;
1.1 kristaps 191:
1.4 kristaps 192: if (0 != strncmp(fi.name, "-", 1))
193: if (-1 == (fi.fd = open(fi.name, FI_FL, 0))) {
194: warn("%s", fi.name);
195: return(leave_io(&fo, &fi, 1));
196: }
1.1 kristaps 197:
1.4 kristaps 198: if (0 != strncmp(fo.name, "-", 1))
199: if (-1 == (fo.fd = open(fo.name, FO_FL, 0644))) {
200: warn("%s", fo.name);
201: return(leave_io(&fo, &fi, 1));
202: }
1.1 kristaps 203:
1.5 kristaps 204: return(leave_io(&fo, &fi, begin_bufs(args, &fo, &fi)));
1.4 kristaps 205: }
1.1 kristaps 206:
207:
1.9 kristaps 208: /*
209: * Free buffers allocated in begin_bufs.
210: */
1.4 kristaps 211: static int
1.5 kristaps 212: leave_bufs(const struct md_buf *out,
213: const struct md_buf *in, int c)
1.4 kristaps 214: {
215: assert(out);
216: assert(in);
217: if (out->buf)
218: free(out->buf);
219: if (in->buf)
220: free(in->buf);
1.1 kristaps 221: return(c);
222: }
223:
224:
1.9 kristaps 225: /*
226: * Allocate buffers to the maximum of either the input file's blocksize
227: * or BUFFER_IN_DEF/BUFFER_OUT_DEF, which should be around BUFSIZE.
228: */
1.1 kristaps 229: static int
1.5 kristaps 230: begin_bufs(const struct md_args *args,
231: struct md_buf *out, struct md_buf *in)
1.1 kristaps 232: {
233: struct stat stin, stout;
1.5 kristaps 234: int c;
1.1 kristaps 235:
1.5 kristaps 236: assert(args);
1.1 kristaps 237: assert(in);
238: assert(out);
239:
240: if (-1 == fstat(in->fd, &stin)) {
1.3 kristaps 241: warn("%s", in->name);
1.1 kristaps 242: return(1);
1.12 kristaps 243: } else if (STDIN_FILENO != in->fd && 0 == stin.st_size) {
1.8 kristaps 244: warnx("%s: empty file", in->name);
245: return(1);
1.1 kristaps 246: } else if (-1 == fstat(out->fd, &stout)) {
1.3 kristaps 247: warn("%s", out->name);
1.1 kristaps 248: return(1);
249: }
250:
1.3 kristaps 251: in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF);
252: out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF);
1.1 kristaps 253:
1.3 kristaps 254: if (NULL == (in->buf = malloc(in->bufsz))) {
1.1 kristaps 255: warn("malloc");
1.4 kristaps 256: return(leave_bufs(out, in, 1));
1.3 kristaps 257: } else if (NULL == (out->buf = malloc(out->bufsz))) {
1.1 kristaps 258: warn("malloc");
1.4 kristaps 259: return(leave_bufs(out, in, 1));
1.1 kristaps 260: }
261:
1.5 kristaps 262: c = md_run(args, out, in);
263: return(leave_bufs(out, in, -1 == c ? 1 : 0));
1.1 kristaps 264: }
265:
266:
267: static void
268: usage(void)
269: {
270: extern char *__progname;
271:
1.19 ! kristaps 272: (void)fprintf(stderr, "usage: %s [-v] [-Wwarn...] "
! 273: "[-f filter] [-o outfile] [infile]\n",
! 274: __progname);
1.1 kristaps 275: }
1.18 kristaps 276:
CVSweb