]>
git.cameronkatri.com Git - mandoc.git/blob - preconv.c
1 /* $Id: preconv.c,v 1.8 2014/08/16 19:00:01 schwarze 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.
19 #include <sys/types.h>
33 * The read_whole_file() and resize_buf() functions are copied from
34 * read.c, including all dependency code.
38 ENC_UTF_8
, /* UTF-8 */
39 ENC_US_ASCII
, /* US-ASCII */
40 ENC_LATIN_1
, /* Latin-1 */
45 char *buf
; /* binary input buffer */
46 size_t sz
; /* size of binary buffer */
47 size_t offs
; /* starting buffer offset */
52 int (*conv
)(const struct buf
*);
55 static int cue_enc(const struct buf
*, size_t *, enum enc
*);
56 static int conv_latin_1(const struct buf
*);
57 static int conv_us_ascii(const struct buf
*);
58 static int conv_utf_8(const struct buf
*);
59 static int read_whole_file(const char *, int,
61 static void resize_buf(struct buf
*, size_t);
62 static void usage(void);
64 static const struct encode encs
[ENC__MAX
] = {
65 { "utf-8", conv_utf_8
}, /* ENC_UTF_8 */
66 { "us-ascii", conv_us_ascii
}, /* ENC_US_ASCII */
67 { "latin-1", conv_latin_1
}, /* ENC_LATIN_1 */
70 static const char *progname
;
76 fprintf(stderr
, "usage: %s "
79 "[file]\n", progname
);
83 conv_latin_1(const struct buf
*b
)
89 cp
= b
->buf
+ (int)b
->offs
;
92 * Latin-1 falls into the first 256 code-points of Unicode, so
93 * there's no need for any sort of translation. Just make the
94 * 8-bit characters use the Unicode escape.
95 * Note that binary values 128 < v < 160 are passed through
96 * unmodified to mandoc.
99 for (i
= b
->offs
; i
< b
->sz
; i
++) {
100 cu
= (unsigned char)*cp
++;
101 cu
< 160U ? putchar(cu
) : printf("\\[u%.4X]", cu
);
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
)
130 cp
= b
->buf
+ (int)b
->offs
;
135 /* Quick test for big-endian value. */
137 if ( ! (*((const char *)(&one
))))
140 for (i
= b
->offs
; i
< b
->sz
; i
++) {
141 cu
= (unsigned char)*cp
++;
143 if ( ! (cu
& 128) || (cu
& 64)) {
144 /* Bad sequence header. */
148 /* Accept only legitimate bit patterns. */
150 if (cu
> 191 || cu
< 128) {
151 /* Bad in-sequence bits. */
155 accum
|= (cu
& 63) << --state
* 6;
158 * Accum is held in little-endian order as
159 * stipulated by the UTF-8 sequence coding. We
160 * need to convert to a native big-endian if our
161 * architecture requires it.
164 if (0 == state
&& be
)
165 accum
= (accum
>> 24) |
166 ((accum
<< 8) & 0x00FF0000) |
167 ((accum
>> 8) & 0x0000FF00) |
171 accum
< 128U ? putchar(accum
) :
172 printf("\\[u%.4X]", accum
);
175 } else if (cu
& (1 << 7)) {
177 * Entering a UTF-8 state: if we encounter a
178 * UTF-8 bitmask, calculate the expected UTF-8
181 for (state
= 0; state
< 7; state
++)
182 if ( ! (cu
& (1 << (7 - state
))))
185 /* Accept only legitimate bit patterns. */
189 if (cu
<= 244 && cu
>= 240) {
190 accum
= (cu
& 7) << 18;
193 /* Bad 4-sequence start bits. */
196 if (cu
<= 239 && cu
>= 224) {
197 accum
= (cu
& 15) << 12;
200 /* Bad 3-sequence start bits. */
203 if (cu
<= 223 && cu
>= 194) {
204 accum
= (cu
& 31) << 6;
207 /* Bad 2-sequence start bits. */
210 /* Bad sequence bit mask. */
219 /* Bad trailing bits. */
227 resize_buf(struct buf
*buf
, size_t initial
)
230 buf
->sz
= buf
->sz
> initial
/ 2 ?
231 2 * buf
->sz
: initial
;
233 buf
->buf
= realloc(buf
->buf
, buf
->sz
);
234 if (NULL
== buf
->buf
) {
241 read_whole_file(const char *f
, int fd
,
242 struct buf
*fb
, int *with_mmap
)
249 if (-1 == fstat(fd
, &st
)) {
255 * If we're a regular file, try just reading in the whole entry
256 * via mmap(). This is faster than reading it into blocks, and
257 * since each file is only a few bytes to begin with, I'm not
258 * concerned that this is going to tank any machines.
261 if (S_ISREG(st
.st_mode
) && st
.st_size
>= (1U << 31)) {
262 fprintf(stderr
, "%s: input too large\n", f
);
266 if (S_ISREG(st
.st_mode
)) {
268 fb
->sz
= (size_t)st
.st_size
;
269 fb
->buf
= mmap(NULL
, fb
->sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
270 if (fb
->buf
!= MAP_FAILED
)
276 * If this isn't a regular file (like, say, stdin), then we must
277 * go the old way and just read things in bit by bit.
285 if (off
== fb
->sz
&& fb
->sz
== (1U << 31)) {
286 fprintf(stderr
, "%s: input too large\n", f
);
291 resize_buf(fb
, 65536);
293 ssz
= read(fd
, fb
->buf
+ (int)off
, fb
->sz
- off
);
311 cue_enc(const struct buf
*b
, size_t *offs
, enum enc
*enc
)
313 const char *ln
, *eoln
, *eoph
;
314 size_t sz
, phsz
, nsz
;
317 ln
= b
->buf
+ (int)*offs
;
320 /* Look for the end-of-line. */
322 if (NULL
== (eoln
= memchr(ln
, '\n', sz
)))
325 /* Set next-line marker. */
327 *offs
= (size_t)((eoln
+ 1) - b
->buf
);
329 /* Check if we have the correct header/trailer. */
331 if ((sz
= (size_t)(eoln
- ln
)) < 10 ||
332 memcmp(ln
, ".\\\" -*-", 7) ||
333 memcmp(eoln
- 3, "-*-", 3))
336 /* Move after the header and adjust for the trailer. */
342 while (sz
> 0 && ' ' == *ln
) {
349 /* Find the end-of-phrase marker (or eoln). */
351 if (NULL
== (eoph
= memchr(ln
, ';', sz
)))
356 /* Only account for the "coding" phrase. */
358 if ((phsz
= (size_t)(eoph
- ln
)) < 7 ||
359 strncasecmp(ln
, "coding:", 7)) {
368 while (sz
> 0 && ' ' == *ln
) {
375 /* Check us against known encodings. */
377 for (i
= 0; i
< (int)ENC__MAX
; i
++) {
378 nsz
= strlen(encs
[i
].name
);
381 if (strncasecmp(ln
, encs
[i
].name
, nsz
))
388 /* Unknown encoding. */
398 main(int argc
, char *argv
[])
400 int i
, ch
, map
, fd
, rc
;
404 unsigned char bom
[3] = { 0xEF, 0xBB, 0xBF };
409 progname
= strrchr(argv
[0], '/');
410 if (progname
== NULL
)
418 enc
= def
= ENC__MAX
;
421 memset(&b
, 0, sizeof(struct buf
));
423 while (-1 != (ch
= getopt(argc
, argv
, "D:e:rdvh")))
428 for (i
= 0; i
< (int)ENC__MAX
; i
++) {
429 if (strcasecmp(optarg
, encs
[i
].name
))
433 if (i
< (int)ENC__MAX
) {
441 fprintf(stderr
, "%s: Bad encoding\n", optarg
);
442 return(EXIT_FAILURE
);
448 /* Compatibility with GNU preconv. */
451 /* Compatibility with GNU preconv. */
455 return(EXIT_FAILURE
);
462 * Open and read the first argument on the command-line.
463 * If we don't have one, we default to stdin.
468 fd
= open(fn
, O_RDONLY
, 0);
471 return(EXIT_FAILURE
);
475 if ( ! read_whole_file(fn
, fd
, &b
, &map
))
478 /* Try to read the UTF-8 BOM. */
481 if (b
.sz
> 3 && 0 == memcmp(b
.buf
, bom
, 3)) {
486 /* Try reading from the "-*-" cue. */
488 if (ENC__MAX
== enc
) {
490 ch
= cue_enc(&b
, &offs
, &enc
);
492 ch
= cue_enc(&b
, &offs
, &enc
);
496 * No encoding has been detected.
497 * Thus, we either fall into our default encoder, if specified,
498 * or use Latin-1 if all else fails.
502 enc
= ENC__MAX
== def
? ENC_LATIN_1
: def
;
504 if ( ! (*encs
[(int)enc
].conv
)(&b
)) {
505 fprintf(stderr
, "%s: Bad encoding\n", fn
);
518 if (fd
> STDIN_FILENO
)