]>
git.cameronkatri.com Git - mandoc.git/blob - mdocml.c
1 /* $Id: mdocml.c,v 1.6 2008/11/23 11:05:25 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 struct md_args
*,
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);
47 main(int argc
, char *argv
[])
58 while (-1 != (c
= getopt(argc
, argv
, "o:")))
74 args
.type
= MD_HTML4_STRICT
;
76 return(begin_io(&args
, out
? out
: "-", in
? in
: "-"));
81 leave_io(const struct md_buf
*out
,
82 const struct md_buf
*in
, int c
)
87 if (-1 != in
->fd
&& -1 == close(in
->fd
)) {
92 if (-1 != out
->fd
&& STDOUT_FILENO
!= out
->fd
&&
93 -1 == close(out
->fd
)) {
95 warn("%s", out
->name
);
104 begin_io(const struct md_args
*args
, char *out
, char *in
)
109 #define FI_FL O_RDONLY
110 #define FO_FL O_WRONLY|O_CREAT|O_TRUNC
116 bzero(&fi
, sizeof(struct md_buf
));
117 bzero(&fo
, sizeof(struct md_buf
));
119 fi
.fd
= STDIN_FILENO
;
120 fo
.fd
= STDOUT_FILENO
;
125 if (0 != strncmp(fi
.name
, "-", 1))
126 if (-1 == (fi
.fd
= open(fi
.name
, FI_FL
, 0))) {
128 return(leave_io(&fo
, &fi
, 1));
131 if (0 != strncmp(fo
.name
, "-", 1))
132 if (-1 == (fo
.fd
= open(fo
.name
, FO_FL
, 0644))) {
134 return(leave_io(&fo
, &fi
, 1));
137 return(leave_io(&fo
, &fi
, begin_bufs(args
, &fo
, &fi
)));
142 leave_bufs(const struct md_buf
*out
,
143 const struct md_buf
*in
, int c
)
156 begin_bufs(const struct md_args
*args
,
157 struct md_buf
*out
, struct md_buf
*in
)
159 struct stat stin
, stout
;
166 if (-1 == fstat(in
->fd
, &stin
)) {
167 warn("%s", in
->name
);
169 } else if (-1 == fstat(out
->fd
, &stout
)) {
170 warn("%s", out
->name
);
174 in
->bufsz
= MAX(stin
.st_blksize
, BUFFER_IN_DEF
);
175 out
->bufsz
= MAX(stout
.st_blksize
, BUFFER_OUT_DEF
);
177 if (NULL
== (in
->buf
= malloc(in
->bufsz
))) {
179 return(leave_bufs(out
, in
, 1));
180 } else if (NULL
== (out
->buf
= malloc(out
->bufsz
))) {
182 return(leave_bufs(out
, in
, 1));
185 c
= md_run(args
, out
, in
);
186 return(leave_bufs(out
, in
, -1 == c
? 1 : 0));
193 extern char *__progname
;
195 (void)printf("usage: %s [-o outfile] [infile]\n", __progname
);