]>
git.cameronkatri.com Git - mandoc.git/blob - mdocml.c
1 /* $Id: mdocml.c,v 1.19 2008/12/09 17:09:12 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
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
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.
19 #include <sys/param.h>
31 #include "libmdocml.h"
33 #define BUFFER_IN_DEF BUFSIZ /* See begin_bufs. */
34 #define BUFFER_OUT_DEF BUFSIZ /* See begin_bufs. */
37 #define CSS "mdocml.css"
39 #define CSS "/usr/local/share/mdocml/mdocml.css"
42 static void usage(void);
44 static int begin_io(const struct md_args
*,
46 static int leave_io(const struct md_buf
*,
47 const struct md_buf
*, int);
48 static int begin_bufs(const struct md_args
*,
49 struct md_buf
*, struct md_buf
*);
50 static int leave_bufs(const struct md_buf
*,
51 const struct md_buf
*, int);
54 extern int getsubopt(char **, char *const *, char **);
58 main(int argc
, char *argv
[])
61 char *out
, *in
, *opts
, *v
;
65 char *toks
[] = { "all", "error", NULL
};
72 (void)memset(&args
, 0, sizeof(struct md_args
));
76 while (-1 != (c
= getopt(argc
, argv
, "c:ef:o:vW:")))
79 if (args
.type
!= MD_HTML
)
80 errx(1, "-c only valid for -fhtml");
81 args
.params
.html
.css
= optarg
;
84 if (args
.type
!= MD_HTML
)
85 errx(1, "-e only valid for -fhtml");
86 args
.params
.html
.flags
|= HTML_CSS_EMBED
;
89 if (0 == strcmp(optarg
, "html"))
91 else if (0 == strcmp(optarg
, "xml"))
94 errx(1, "invalid filter type");
105 switch (getsubopt(&opts
, toks
, &v
)) {
107 args
.warnings
|= MD_WARN_ALL
;
110 args
.warnings
|= MD_WARN_ERROR
;
122 if (MD_HTML
== args
.type
)
123 if (NULL
== args
.params
.html
.css
)
124 args
.params
.html
.css
= CSS
;
132 return(begin_io(&args
, out
? out
: "-", in
? in
: "-"));
137 * Close out file descriptors opened in begin_io. If the descriptor
138 * refers to stdin/stdout, then do nothing.
141 leave_io(const struct md_buf
*out
,
142 const struct md_buf
*in
, int c
)
147 if (-1 != in
->fd
&& -1 == close(in
->fd
)) {
149 warn("%s", in
->name
);
152 if (-1 != out
->fd
&& STDOUT_FILENO
!= out
->fd
&&
153 -1 == close(out
->fd
)) {
155 warn("%s", out
->name
);
158 if (1 == c
&& STDOUT_FILENO
!= out
->fd
)
159 if (-1 == unlink(out
->name
))
160 warn("%s", out
->name
);
167 * Open file descriptors or assign stdin/stdout, if dictated by the "-"
168 * token instead of a filename.
171 begin_io(const struct md_args
*args
, char *out
, char *in
)
176 #define FI_FL O_RDONLY
177 #define FO_FL O_WRONLY|O_CREAT|O_TRUNC
183 bzero(&fi
, sizeof(struct md_buf
));
184 bzero(&fo
, sizeof(struct md_buf
));
186 fi
.fd
= STDIN_FILENO
;
187 fo
.fd
= STDOUT_FILENO
;
192 if (0 != strncmp(fi
.name
, "-", 1))
193 if (-1 == (fi
.fd
= open(fi
.name
, FI_FL
, 0))) {
195 return(leave_io(&fo
, &fi
, 1));
198 if (0 != strncmp(fo
.name
, "-", 1))
199 if (-1 == (fo
.fd
= open(fo
.name
, FO_FL
, 0644))) {
201 return(leave_io(&fo
, &fi
, 1));
204 return(leave_io(&fo
, &fi
, begin_bufs(args
, &fo
, &fi
)));
209 * Free buffers allocated in begin_bufs.
212 leave_bufs(const struct md_buf
*out
,
213 const struct md_buf
*in
, int c
)
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.
230 begin_bufs(const struct md_args
*args
,
231 struct md_buf
*out
, struct md_buf
*in
)
233 struct stat stin
, stout
;
240 if (-1 == fstat(in
->fd
, &stin
)) {
241 warn("%s", in
->name
);
243 } else if (STDIN_FILENO
!= in
->fd
&& 0 == stin
.st_size
) {
244 warnx("%s: empty file", in
->name
);
246 } else if (-1 == fstat(out
->fd
, &stout
)) {
247 warn("%s", out
->name
);
251 in
->bufsz
= MAX(stin
.st_blksize
, BUFFER_IN_DEF
);
252 out
->bufsz
= MAX(stout
.st_blksize
, BUFFER_OUT_DEF
);
254 if (NULL
== (in
->buf
= malloc(in
->bufsz
))) {
256 return(leave_bufs(out
, in
, 1));
257 } else if (NULL
== (out
->buf
= malloc(out
->bufsz
))) {
259 return(leave_bufs(out
, in
, 1));
262 c
= md_run(args
, out
, in
);
263 return(leave_bufs(out
, in
, -1 == c
? 1 : 0));
270 extern char *__progname
;
272 (void)fprintf(stderr
, "usage: %s [-v] [-Wwarn...] "
273 "[-f filter] [-o outfile] [infile]\n",