]>
git.cameronkatri.com Git - mandoc.git/blob - preconv.c
ce091ec88afc126ed13cff45786f392b361f5c24
1 /* $Id: preconv.c,v 1.1 2011/05/26 00:30:11 kristaps Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 * The read_whole_file() and resize_buf() functions are copied from
33 * read.c, including all dependency code (MAP_FILE, etc.).
41 ENC_UTF_8
, /* UTF-8 */
42 ENC_US_ASCII
, /* US-ASCII */
43 ENC_LATIN_1
, /* Latin-1 */
48 char *buf
; /* binary input buffer */
49 size_t sz
; /* size of binary buffer */
50 size_t offs
; /* starting buffer offset */
55 int (*conv
)(const struct buf
*);
58 static int conv_latin_1(const struct buf
*);
59 static int conv_us_ascii(const struct buf
*);
60 static int conv_utf_8(const struct buf
*);
61 static int read_whole_file(const char *, int,
63 static void resize_buf(struct buf
*, size_t);
64 static void usage(void);
66 static const struct encode encs
[ENC__MAX
] = {
67 { "utf-8", conv_utf_8
}, /* ENC_UTF_8 */
68 { "us-ascii", conv_us_ascii
}, /* ENC_US_ASCII */
69 { "latin-1", conv_latin_1
}, /* ENC_LATIN_1 */
72 static const char *progname
;
78 fprintf(stderr
, "usage: %s "
81 "[file]\n", progname
);
85 conv_latin_1(const struct buf
*b
)
91 cp
= b
->buf
+ (int)b
->offs
;
94 * Latin-1 falls into the first 256 code-points of Unicode, so
95 * there's no need for any sort of translation. Just make the
96 * 8-bit characters use the Unicode escape.
99 for (i
= b
->offs
; i
< b
->sz
; i
++) {
100 c
= (unsigned char)*cp
++;
101 c
< 128 ? putchar(c
) : printf("\\[u%.4X]", c
);
108 conv_us_ascii(const struct buf
*b
)
112 * US-ASCII has no conversion since it falls into the first 128
116 fwrite(b
->buf
, 1, b
->sz
, stdout
);
121 conv_utf_8(const struct buf
*b
)
128 resize_buf(struct buf
*buf
, size_t initial
)
131 buf
->sz
= buf
->sz
> initial
/ 2 ?
132 2 * buf
->sz
: initial
;
134 buf
->buf
= realloc(buf
->buf
, buf
->sz
);
135 if (NULL
== buf
->buf
) {
142 read_whole_file(const char *f
, int fd
,
143 struct buf
*fb
, int *with_mmap
)
149 if (-1 == fstat(fd
, &st
)) {
155 * If we're a regular file, try just reading in the whole entry
156 * via mmap(). This is faster than reading it into blocks, and
157 * since each file is only a few bytes to begin with, I'm not
158 * concerned that this is going to tank any machines.
161 if (S_ISREG(st
.st_mode
) && st
.st_size
>= (1U << 31)) {
162 fprintf(stderr
, "%s: input too large\n", f
);
166 if (S_ISREG(st
.st_mode
)) {
168 fb
->sz
= (size_t)st
.st_size
;
169 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
,
170 MAP_FILE
|MAP_SHARED
, fd
, 0);
171 if (fb
->buf
!= MAP_FAILED
)
176 * If this isn't a regular file (like, say, stdin), then we must
177 * go the old way and just read things in bit by bit.
185 if (off
== fb
->sz
&& fb
->sz
== (1U << 31)) {
186 fprintf(stderr
, "%s: input too large\n", f
);
191 resize_buf(fb
, 65536);
193 ssz
= read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
211 main(int argc
, char *argv
[])
213 int i
, ch
, map
, fd
, rc
;
220 progname
= strrchr(argv
[0], '/');
221 if (progname
== NULL
)
229 enc
= def
= ENC__MAX
;
232 memset(&buf
, 0, sizeof(struct buf
));
234 while (-1 != (ch
= getopt(argc
, argv
, "D:e:rdvh")))
239 for (i
= 0; i
< ENC__MAX
; i
++) {
240 if (strcasecmp(optarg
, encs
[i
].name
))
252 fprintf(stderr
, "%s: Bad encoding\n", optarg
);
253 return(EXIT_FAILURE
);
259 /* Compatibility with GNU preconv. */
262 /* Compatibility with GNU preconv. */
266 return(EXIT_FAILURE
);
273 * Open and read the first argument on the command-line.
274 * If we don't have one, we default to stdin.
279 fd
= open(fn
, O_RDONLY
, 0);
282 return(EXIT_FAILURE
);
286 if ( ! read_whole_file(fn
, fd
, &buf
, &map
))
289 if (ENC__MAX
== enc
) {
290 /* TODO: search for BOM. */
294 * No encoding has been detected.
295 * Thus, we either fall into our default encoder, if specified,
296 * or use Latin-1 if all else fails.
300 enc
= ENC__MAX
== def
? ENC_LATIN_1
: def
;
302 if ( ! (*encs
[(int)enc
].conv
)(&buf
))
308 munmap(buf
.buf
, buf
.sz
);
312 if (fd
> STDIN_FILENO
)