]>
git.cameronkatri.com Git - mandoc.git/blob - mdocml.c
1 /* $Id: mdocml.c,v 1.14 2008/11/28 11:21: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. */
36 static void usage(void);
38 static int begin_io(const struct md_args
*,
40 static int leave_io(const struct md_buf
*,
41 const struct md_buf
*, int);
42 static int begin_bufs(const struct md_args
*,
43 struct md_buf
*, struct md_buf
*);
44 static int leave_bufs(const struct md_buf
*,
45 const struct md_buf
*, int);
48 main(int argc
, char *argv
[])
59 (void)memset(&args
, 0, sizeof(struct md_args
));
61 while (-1 != (c
= getopt(argc
, argv
, "o:vW")))
70 args
.warnings
|= MD_WARN_ALL
;
83 return(begin_io(&args
, out
? out
: "-", in
? in
: "-"));
88 * Close out file descriptors opened in begin_io. If the descriptor
89 * refers to stdin/stdout, then do nothing.
92 leave_io(const struct md_buf
*out
,
93 const struct md_buf
*in
, int c
)
98 if (-1 != in
->fd
&& -1 == close(in
->fd
)) {
100 warn("%s", in
->name
);
103 if (-1 != out
->fd
&& STDOUT_FILENO
!= out
->fd
&&
104 -1 == close(out
->fd
)) {
106 warn("%s", out
->name
);
115 * Open file descriptors or assign stdin/stdout, if dictated by the "-"
116 * token instead of a filename.
119 begin_io(const struct md_args
*args
, char *out
, char *in
)
124 #define FI_FL O_RDONLY
125 #define FO_FL O_WRONLY|O_CREAT|O_TRUNC
131 bzero(&fi
, sizeof(struct md_buf
));
132 bzero(&fo
, sizeof(struct md_buf
));
134 fi
.fd
= STDIN_FILENO
;
135 fo
.fd
= STDOUT_FILENO
;
140 if (0 != strncmp(fi
.name
, "-", 1))
141 if (-1 == (fi
.fd
= open(fi
.name
, FI_FL
, 0))) {
143 return(leave_io(&fo
, &fi
, 1));
146 if (0 != strncmp(fo
.name
, "-", 1))
147 if (-1 == (fo
.fd
= open(fo
.name
, FO_FL
, 0644))) {
149 return(leave_io(&fo
, &fi
, 1));
152 return(leave_io(&fo
, &fi
, begin_bufs(args
, &fo
, &fi
)));
157 * Free buffers allocated in begin_bufs.
160 leave_bufs(const struct md_buf
*out
,
161 const struct md_buf
*in
, int c
)
174 * Allocate buffers to the maximum of either the input file's blocksize
175 * or BUFFER_IN_DEF/BUFFER_OUT_DEF, which should be around BUFSIZE.
178 begin_bufs(const struct md_args
*args
,
179 struct md_buf
*out
, struct md_buf
*in
)
181 struct stat stin
, stout
;
188 if (-1 == fstat(in
->fd
, &stin
)) {
189 warn("%s", in
->name
);
191 } else if (STDIN_FILENO
!= in
->fd
&& 0 == stin
.st_size
) {
192 warnx("%s: empty file", in
->name
);
194 } else if (-1 == fstat(out
->fd
, &stout
)) {
195 warn("%s", out
->name
);
199 in
->bufsz
= MAX(stin
.st_blksize
, BUFFER_IN_DEF
);
200 out
->bufsz
= MAX(stout
.st_blksize
, BUFFER_OUT_DEF
);
202 if (NULL
== (in
->buf
= malloc(in
->bufsz
))) {
204 return(leave_bufs(out
, in
, 1));
205 } else if (NULL
== (out
->buf
= malloc(out
->bufsz
))) {
207 return(leave_bufs(out
, in
, 1));
210 c
= md_run(args
, out
, in
);
211 return(leave_bufs(out
, in
, -1 == c
? 1 : 0));
218 extern char *__progname
;
220 (void)printf("usage: %s [-vW] [-o outfile] [infile]\n", __progname
);