Annotation of mandoc/mdocml.c, Revision 1.18
1.18 ! kristaps 1: /* $Id: mdocml.c,v 1.17 2008/12/04 11:25:29 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:
53: int
54: main(int argc, char *argv[])
55: {
56: int c;
1.18 ! kristaps 57: char *out, *in, *opts, *v;
1.5 kristaps 58: struct md_args args;
1.18 ! kristaps 59: #define ALL 0
! 60: #define ERROR 1
! 61: char *toks[] = { "all", "error", NULL };
1.1 kristaps 62:
63: extern char *optarg;
64: extern int optind;
65:
1.17 kristaps 66: out = in = NULL;
1.10 kristaps 67:
68: (void)memset(&args, 0, sizeof(struct md_args));
1.17 kristaps 69:
70: args.type = MD_XML;
71:
1.18 ! kristaps 72: while (-1 != (c = getopt(argc, argv, "c:ef:o:vW:")))
1.1 kristaps 73: switch (c) {
1.17 kristaps 74: case ('c'):
75: if (args.type != MD_HTML)
76: errx(1, "-c only valid for -fhtml");
77: args.params.html.css = optarg;
78: break;
79: case ('e'):
80: if (args.type != MD_HTML)
81: errx(1, "-e only valid for -fhtml");
82: args.params.html.flags |= HTML_CSS_EMBED;
83: break;
1.16 kristaps 84: case ('f'):
1.17 kristaps 85: if (0 == strcmp(optarg, "html"))
86: args.type = MD_HTML;
87: else if (0 == strcmp(optarg, "xml"))
88: args.type = MD_XML;
89: else
90: errx(1, "invalid filter type");
1.16 kristaps 91: break;
1.1 kristaps 92: case ('o'):
93: out = optarg;
94: break;
1.13 kristaps 95: case ('v'):
96: args.verbosity++;
97: break;
98: case ('W'):
1.18 ! kristaps 99: opts = optarg;
! 100: while (*opts)
! 101: switch (getsubopt(&opts, toks, &v)) {
! 102: case (ALL):
! 103: args.warnings |= MD_WARN_ALL;
! 104: break;
! 105: case (ERROR):
! 106: args.warnings |= MD_WARN_ERROR;
! 107: break;
! 108: default:
! 109: usage();
! 110: return(1);
! 111: }
1.13 kristaps 112: break;
1.1 kristaps 113: default:
114: usage();
115: return(1);
116: }
117:
1.17 kristaps 118: if (MD_HTML == args.type)
119: if (NULL == args.params.html.css)
120: args.params.html.css = CSS;
121:
1.1 kristaps 122: argv += optind;
1.4 kristaps 123: argc -= optind;
1.1 kristaps 124:
1.4 kristaps 125: if (1 == argc)
126: in = *argv++;
1.1 kristaps 127:
1.5 kristaps 128: return(begin_io(&args, out ? out : "-", in ? in : "-"));
1.1 kristaps 129: }
130:
131:
1.9 kristaps 132: /*
133: * Close out file descriptors opened in begin_io. If the descriptor
134: * refers to stdin/stdout, then do nothing.
135: */
1.1 kristaps 136: static int
1.5 kristaps 137: leave_io(const struct md_buf *out,
138: const struct md_buf *in, int c)
1.1 kristaps 139: {
1.4 kristaps 140: assert(out);
1.1 kristaps 141: assert(in);
142:
1.4 kristaps 143: if (-1 != in->fd && -1 == close(in->fd)) {
144: assert(in->name);
145: warn("%s", in->name);
146: c = 1;
147: }
148: if (-1 != out->fd && STDOUT_FILENO != out->fd &&
149: -1 == close(out->fd)) {
150: assert(out->name);
151: warn("%s", out->name);
152: c = 1;
153: }
1.15 kristaps 154: if (1 == c && STDOUT_FILENO != out->fd)
155: if (-1 == unlink(out->name))
156: warn("%s", out->name);
1.1 kristaps 157:
1.4 kristaps 158: return(c);
1.1 kristaps 159: }
160:
161:
1.9 kristaps 162: /*
163: * Open file descriptors or assign stdin/stdout, if dictated by the "-"
164: * token instead of a filename.
165: */
1.1 kristaps 166: static int
1.5 kristaps 167: begin_io(const struct md_args *args, char *out, char *in)
1.1 kristaps 168: {
1.5 kristaps 169: struct md_buf fi;
170: struct md_buf fo;
1.4 kristaps 171:
172: #define FI_FL O_RDONLY
173: #define FO_FL O_WRONLY|O_CREAT|O_TRUNC
1.1 kristaps 174:
1.5 kristaps 175: assert(args);
1.1 kristaps 176: assert(out);
177: assert(in);
178:
1.5 kristaps 179: bzero(&fi, sizeof(struct md_buf));
180: bzero(&fo, sizeof(struct md_buf));
1.1 kristaps 181:
1.4 kristaps 182: fi.fd = STDIN_FILENO;
183: fo.fd = STDOUT_FILENO;
1.1 kristaps 184:
1.4 kristaps 185: fi.name = in;
186: fo.name = out;
1.1 kristaps 187:
1.4 kristaps 188: if (0 != strncmp(fi.name, "-", 1))
189: if (-1 == (fi.fd = open(fi.name, FI_FL, 0))) {
190: warn("%s", fi.name);
191: return(leave_io(&fo, &fi, 1));
192: }
1.1 kristaps 193:
1.4 kristaps 194: if (0 != strncmp(fo.name, "-", 1))
195: if (-1 == (fo.fd = open(fo.name, FO_FL, 0644))) {
196: warn("%s", fo.name);
197: return(leave_io(&fo, &fi, 1));
198: }
1.1 kristaps 199:
1.5 kristaps 200: return(leave_io(&fo, &fi, begin_bufs(args, &fo, &fi)));
1.4 kristaps 201: }
1.1 kristaps 202:
203:
1.9 kristaps 204: /*
205: * Free buffers allocated in begin_bufs.
206: */
1.4 kristaps 207: static int
1.5 kristaps 208: leave_bufs(const struct md_buf *out,
209: const struct md_buf *in, int c)
1.4 kristaps 210: {
211: assert(out);
212: assert(in);
213: if (out->buf)
214: free(out->buf);
215: if (in->buf)
216: free(in->buf);
1.1 kristaps 217: return(c);
218: }
219:
220:
1.9 kristaps 221: /*
222: * Allocate buffers to the maximum of either the input file's blocksize
223: * or BUFFER_IN_DEF/BUFFER_OUT_DEF, which should be around BUFSIZE.
224: */
1.1 kristaps 225: static int
1.5 kristaps 226: begin_bufs(const struct md_args *args,
227: struct md_buf *out, struct md_buf *in)
1.1 kristaps 228: {
229: struct stat stin, stout;
1.5 kristaps 230: int c;
1.1 kristaps 231:
1.5 kristaps 232: assert(args);
1.1 kristaps 233: assert(in);
234: assert(out);
235:
236: if (-1 == fstat(in->fd, &stin)) {
1.3 kristaps 237: warn("%s", in->name);
1.1 kristaps 238: return(1);
1.12 kristaps 239: } else if (STDIN_FILENO != in->fd && 0 == stin.st_size) {
1.8 kristaps 240: warnx("%s: empty file", in->name);
241: return(1);
1.1 kristaps 242: } else if (-1 == fstat(out->fd, &stout)) {
1.3 kristaps 243: warn("%s", out->name);
1.1 kristaps 244: return(1);
245: }
246:
1.3 kristaps 247: in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF);
248: out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF);
1.1 kristaps 249:
1.3 kristaps 250: if (NULL == (in->buf = malloc(in->bufsz))) {
1.1 kristaps 251: warn("malloc");
1.4 kristaps 252: return(leave_bufs(out, in, 1));
1.3 kristaps 253: } else if (NULL == (out->buf = malloc(out->bufsz))) {
1.1 kristaps 254: warn("malloc");
1.4 kristaps 255: return(leave_bufs(out, in, 1));
1.1 kristaps 256: }
257:
1.5 kristaps 258: c = md_run(args, out, in);
259: return(leave_bufs(out, in, -1 == c ? 1 : 0));
1.1 kristaps 260: }
261:
262:
263: static void
264: usage(void)
265: {
266: extern char *__progname;
267:
1.18 ! kristaps 268: (void)printf("usage: %s [-v] [-Wwarn...] [-f filter] "
! 269: "[-o outfile] [infile]\n", __progname);
1.1 kristaps 270: }
1.18 ! kristaps 271:
CVSweb