]>
git.cameronkatri.com Git - mandoc.git/blob - mdocml.c
7f9142113ccd8afc7cbbfb683c5da0c0919ce2a7
1 /* $Id: mdocml.c,v 1.4 2008/11/22 18:34:06 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
34 #define BUFFER_OUT_DEF BUFSIZ
36 static void usage(void);
37 static int begin_io(const char *, const char *);
38 static int leave_io(const struct md_mbuf
*,
39 const struct md_rbuf
*, int);
40 static int begin_bufs(struct md_mbuf
*, struct md_rbuf
*);
41 static int leave_bufs(const struct md_mbuf
*,
42 const struct md_rbuf
*, int);
45 main(int argc
, char *argv
[])
55 while (-1 != (c
= getopt(argc
, argv
, "o:")))
71 return(begin_io(out
? out
: "-", in
? in
: "-"));
76 leave_io(const struct md_mbuf
*out
,
77 const struct md_rbuf
*in
, int c
)
82 if (-1 != in
->fd
&& -1 == close(in
->fd
)) {
87 if (-1 != out
->fd
&& STDOUT_FILENO
!= out
->fd
&&
88 -1 == close(out
->fd
)) {
90 warn("%s", out
->name
);
99 begin_io(const char *out
, const char *in
)
104 #define FI_FL O_RDONLY
105 #define FO_FL O_WRONLY|O_CREAT|O_TRUNC
110 bzero(&fi
, sizeof(struct md_rbuf
));
111 bzero(&fo
, sizeof(struct md_mbuf
));
113 fi
.fd
= STDIN_FILENO
;
114 fo
.fd
= STDOUT_FILENO
;
119 if (0 != strncmp(fi
.name
, "-", 1))
120 if (-1 == (fi
.fd
= open(fi
.name
, FI_FL
, 0))) {
122 return(leave_io(&fo
, &fi
, 1));
125 if (0 != strncmp(fo
.name
, "-", 1))
126 if (-1 == (fo
.fd
= open(fo
.name
, FO_FL
, 0644))) {
128 return(leave_io(&fo
, &fi
, 1));
131 return(leave_io(&fo
, &fi
, begin_bufs(&fo
, &fi
)));
136 leave_bufs(const struct md_mbuf
*out
,
137 const struct md_rbuf
*in
, int c
)
150 begin_bufs(struct md_mbuf
*out
, struct md_rbuf
*in
)
152 struct stat stin
, stout
;
157 if (-1 == fstat(in
->fd
, &stin
)) {
158 warn("%s", in
->name
);
160 } else if (-1 == fstat(out
->fd
, &stout
)) {
161 warn("%s", out
->name
);
165 in
->bufsz
= MAX(stin
.st_blksize
, BUFFER_IN_DEF
);
166 out
->bufsz
= MAX(stout
.st_blksize
, BUFFER_OUT_DEF
);
168 if (NULL
== (in
->buf
= malloc(in
->bufsz
))) {
170 return(leave_bufs(out
, in
, 1));
171 } else if (NULL
== (out
->buf
= malloc(out
->bufsz
))) {
173 return(leave_bufs(out
, in
, 1));
176 return(leave_bufs(out
, in
, md_run(MD_DUMMY
, out
, in
)));
183 extern char *__progname
;
185 (void)printf("usage: %s [-o outfile] [infile]\n", __progname
);