]> git.cameronkatri.com Git - mandoc.git/blob - mdocml.c
Segmentation into html and dummy parsers.
[mandoc.git] / mdocml.c
1 /* $Id: mdocml.c,v 1.6 2008/11/23 11:05:25 kristaps Exp $ */
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
33 #define BUFFER_IN_DEF BUFSIZ
34 #define BUFFER_OUT_DEF BUFSIZ
35
36 static void usage(void);
37 static int begin_io(const struct md_args *,
38 char *, char *);
39 static int leave_io(const struct md_buf *,
40 const struct md_buf *, int);
41 static int begin_bufs(const struct md_args *,
42 struct md_buf *, struct md_buf *);
43 static int leave_bufs(const struct md_buf *,
44 const struct md_buf *, int);
45
46 int
47 main(int argc, char *argv[])
48 {
49 int c;
50 char *out, *in;
51 struct md_args args;
52
53 extern char *optarg;
54 extern int optind;
55
56 out = in = NULL;
57
58 while (-1 != (c = getopt(argc, argv, "o:")))
59 switch (c) {
60 case ('o'):
61 out = optarg;
62 break;
63 default:
64 usage();
65 return(1);
66 }
67
68 argv += optind;
69 argc -= optind;
70
71 if (1 == argc)
72 in = *argv++;
73
74 args.type = MD_HTML4_STRICT;
75
76 return(begin_io(&args, out ? out : "-", in ? in : "-"));
77 }
78
79
80 static int
81 leave_io(const struct md_buf *out,
82 const struct md_buf *in, int c)
83 {
84 assert(out);
85 assert(in);
86
87 if (-1 != in->fd && -1 == close(in->fd)) {
88 assert(in->name);
89 warn("%s", in->name);
90 c = 1;
91 }
92 if (-1 != out->fd && STDOUT_FILENO != out->fd &&
93 -1 == close(out->fd)) {
94 assert(out->name);
95 warn("%s", out->name);
96 c = 1;
97 }
98
99 return(c);
100 }
101
102
103 static int
104 begin_io(const struct md_args *args, char *out, char *in)
105 {
106 struct md_buf fi;
107 struct md_buf fo;
108
109 #define FI_FL O_RDONLY
110 #define FO_FL O_WRONLY|O_CREAT|O_TRUNC
111
112 assert(args);
113 assert(out);
114 assert(in);
115
116 bzero(&fi, sizeof(struct md_buf));
117 bzero(&fo, sizeof(struct md_buf));
118
119 fi.fd = STDIN_FILENO;
120 fo.fd = STDOUT_FILENO;
121
122 fi.name = in;
123 fo.name = out;
124
125 if (0 != strncmp(fi.name, "-", 1))
126 if (-1 == (fi.fd = open(fi.name, FI_FL, 0))) {
127 warn("%s", fi.name);
128 return(leave_io(&fo, &fi, 1));
129 }
130
131 if (0 != strncmp(fo.name, "-", 1))
132 if (-1 == (fo.fd = open(fo.name, FO_FL, 0644))) {
133 warn("%s", fo.name);
134 return(leave_io(&fo, &fi, 1));
135 }
136
137 return(leave_io(&fo, &fi, begin_bufs(args, &fo, &fi)));
138 }
139
140
141 static int
142 leave_bufs(const struct md_buf *out,
143 const struct md_buf *in, int c)
144 {
145 assert(out);
146 assert(in);
147 if (out->buf)
148 free(out->buf);
149 if (in->buf)
150 free(in->buf);
151 return(c);
152 }
153
154
155 static int
156 begin_bufs(const struct md_args *args,
157 struct md_buf *out, struct md_buf *in)
158 {
159 struct stat stin, stout;
160 int c;
161
162 assert(args);
163 assert(in);
164 assert(out);
165
166 if (-1 == fstat(in->fd, &stin)) {
167 warn("%s", in->name);
168 return(1);
169 } else if (-1 == fstat(out->fd, &stout)) {
170 warn("%s", out->name);
171 return(1);
172 }
173
174 in->bufsz = MAX(stin.st_blksize, BUFFER_IN_DEF);
175 out->bufsz = MAX(stout.st_blksize, BUFFER_OUT_DEF);
176
177 if (NULL == (in->buf = malloc(in->bufsz))) {
178 warn("malloc");
179 return(leave_bufs(out, in, 1));
180 } else if (NULL == (out->buf = malloc(out->bufsz))) {
181 warn("malloc");
182 return(leave_bufs(out, in, 1));
183 }
184
185 c = md_run(args, out, in);
186 return(leave_bufs(out, in, -1 == c ? 1 : 0));
187 }
188
189
190 static void
191 usage(void)
192 {
193 extern char *__progname;
194
195 (void)printf("usage: %s [-o outfile] [infile]\n", __progname);
196 }