iso2022.c 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
Copyright (c) 2001 by Juliusz Chroboczek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
22 23 24 25

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "luit.h"
#include "sys.h"
#include "other.h"
#include "iso2022.h"

#define BUFFERED_INPUT_SIZE 4
41 42
static unsigned char buffered_input[BUFFERED_INPUT_SIZE];
static int buffered_input_count = 0;
43 44 45 46 47 48 49 50 51

#define OUTBUF_FREE(is, count) ((is)->outbuf_count + (count) <= BUFFER_SIZE)
#define OUTBUF_MAKE_FREE(is, fd, count) \
    if(!OUTBUF_FREE((is), (count))) outbuf_flush((is), (fd))

static void
outbuf_flush(Iso2022Ptr is, int fd)
{
    int rc;
52
    unsigned i = 0;
53

54
    if (olog >= 0)
55
	IGNORE_RC(write(olog, is->outbuf, is->outbuf_count));
56

57
    while (i < is->outbuf_count) {
58
	rc = (int) write(fd, is->outbuf + i, is->outbuf_count - i);
59
	if (rc > 0) {
60
	    i += (unsigned) rc;
61 62 63 64 65 66 67 68 69
	} else {
	    if (rc < 0 && errno == EINTR)
		continue;
	    else if ((rc == 0) || ((rc < 0) && (errno == EAGAIN))) {
		waitForOutput(fd);
		continue;
	    } else
		break;
	}
70 71 72 73 74 75 76 77
    }
    is->outbuf_count = 0;
}

static void
outbufOne(Iso2022Ptr is, int fd, unsigned c)
{
    OUTBUF_MAKE_FREE(is, fd, 1);
78
    is->outbuf[is->outbuf_count++] = UChar(c);
79 80 81 82 83 84
}

/* Discards null codepoints */
static void
outbufUTF8(Iso2022Ptr is, int fd, unsigned c)
{
85 86
    if (c == 0)
	return;
87

88 89
    if (c <= 0x7F) {
	OUTBUF_MAKE_FREE(is, fd, 1);
90
	is->outbuf[is->outbuf_count++] = UChar(c);
91 92
    } else if (c <= 0x7FF) {
	OUTBUF_MAKE_FREE(is, fd, 2);
93 94
	is->outbuf[is->outbuf_count++] = UChar(0xC0 | ((c >> 6) & 0x1F));
	is->outbuf[is->outbuf_count++] = UChar(0x80 | (c & 0x3F));
95
    } else {
96
	OUTBUF_MAKE_FREE(is, fd, 3);
97 98 99
	is->outbuf[is->outbuf_count++] = UChar(0xE0 | ((c >> 12) & 0x0F));
	is->outbuf[is->outbuf_count++] = UChar(0x80 | ((c >> 6) & 0x3F));
	is->outbuf[is->outbuf_count++] = UChar(0x80 | (c & 0x3F));
100 101 102 103
    }
}

static void
104
buffer(Iso2022Ptr is, unsigned c)
105
{
106
    if (is->buffered == NULL) {
107 108
	is->buffered_len = 10;
	is->buffered = malloc(is->buffered_len);
109 110
	if (is->buffered == NULL)
	    FatalError("Couldn't allocate buffered.\n");
111 112
    }

113 114 115 116 117 118
    if (is->buffered_count >= is->buffered_len) {
	is->buffered = realloc(is->buffered, 2 * is->buffered_len + 1);
	if (is->buffered == NULL) {
	    FatalError("Couldn't grow buffered.\n");
	}
	is->buffered_len = 2 * is->buffered_len + 1;
119 120
    }

121
    is->buffered[is->buffered_count++] = UChar(c);
122 123 124 125 126 127
}

static void
outbuf_buffered_carefully(Iso2022Ptr is, int fd)
{
    /* This should never happen in practice */
128
    unsigned i = 0;
129

130 131 132
    while (i < is->buffered_count) {
	OUTBUF_MAKE_FREE(is, fd, 1);
	is->outbuf[is->outbuf_count++] = is->buffered[i++];
133 134
    }
    is->buffered_count = 0;
135
}
136 137 138 139

static void
outbuf_buffered(Iso2022Ptr is, int fd)
{
140 141
    if (is->buffered_count > BUFFER_SIZE)
	outbuf_buffered_carefully(is, fd);
142 143 144 145 146 147 148 149 150 151 152 153 154

    OUTBUF_MAKE_FREE(is, fd, is->buffered_count);
    memcpy(is->outbuf + is->outbuf_count, is->buffered, is->buffered_count);
    is->outbuf_count += is->buffered_count;
    is->buffered_count = 0;
}

static void
discard_buffered(Iso2022Ptr is)
{
    is->buffered_count = 0;
}

155
Iso2022Ptr
156 157 158 159
allocIso2022(void)
{
    Iso2022Ptr is;
    is = malloc(sizeof(Iso2022Rec));
160 161
    if (!is)
	return NULL;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    is->glp = is->grp = NULL;
    G0(is) = G1(is) = G2(is) = G3(is) = OTHER(is) = NULL;

    is->parserState = P_NORMAL;
    is->shiftState = S_NORMAL;

    is->inputFlags = IF_EIGHTBIT | IF_SS | IF_SSGR;
    is->outputFlags = OF_SS | OF_LS | OF_SELECT;

    is->buffered = NULL;
    is->buffered_len = 0;
    is->buffered_count = 0;

    is->buffered_ku = -1;

177
    is->outbuf = malloc((size_t) BUFFER_SIZE);
178 179 180
    if (!is->outbuf) {
	free(is);
	return NULL;
181 182 183 184 185 186 187 188 189
    }
    is->outbuf_count = 0;

    return is;
}

void
destroyIso2022(Iso2022Ptr is)
{
190 191 192 193
    if (is->buffered)
	free(is->buffered);
    if (is->outbuf)
	free(is->outbuf);
194 195 196 197
    free(is);
}

static int
198
identifyCharset(Iso2022Ptr i, const CharsetRec * *p)
199
{
200
    if (p == &G0(i)) {
201
	return 0;
202
    } else if (p == &G1(i)) {
203
	return 1;
204
    } else if (p == &G2(i)) {
205
	return 2;
206
    } else if (p == &G3(i)) {
207
	return 3;
208
    } else {
209
	abort();
210 211
	/* NOTREACHED */
    }
212 213 214 215 216
}

void
reportIso2022(Iso2022Ptr i)
{
217 218 219
    if (OTHER(i) != NULL) {
	fprintf(stderr, "%s, non-ISO-2022 encoding.\n", OTHER(i)->name);
	return;
220 221 222 223 224 225 226 227 228 229
    }
    fprintf(stderr, "G0 is %s, ", G0(i)->name);
    fprintf(stderr, "G1 is %s, ", G1(i)->name);
    fprintf(stderr, "G2 is %s, ", G2(i)->name);
    fprintf(stderr, "G3 is %s.\n", G3(i)->name);
    fprintf(stderr, "GL is G%d, ", identifyCharset(i, i->glp));
    fprintf(stderr, "GR is G%d.\n", identifyCharset(i, i->grp));
}

int
230
initIso2022(const char *locale, const char *charset, Iso2022Ptr i)
231 232
{
    int gl = 0, gr = 2;
233 234 235 236 237
    const CharsetRec *g0 = NULL;
    const CharsetRec *g1 = NULL;
    const CharsetRec *g2 = NULL;
    const CharsetRec *g3 = NULL;
    const CharsetRec *other = NULL;
238
    int rc;
239

240
    rc = getLocaleState(locale, charset, &gl, &gr, &g0, &g1, &g2, &g3, &other);
241 242 243 244 245 246 247
    if (rc < 0) {
	if (charset)
	    ErrorF("Warning: couldn't find charset %s; "
		   "using ISO 8859-1.\n", charset);
	else
	    ErrorF("Warning: couldn't find charset data for locale %s; "
		   "using ISO 8859-1.\n", locale);
248 249
    }

250 251
    if (g0)
	G0(i) = g0;
252
    else
253
	G0(i) = getCharsetByName("ASCII");
254

255 256
    if (g1)
	G1(i) = g1;
257
    else
258
	G1(i) = getUnknownCharset(T_94);
259

260 261
    if (g2)
	G2(i) = g2;
262
    else
263
	G2(i) = getCharsetByName("ISO 8859-1");
264

265 266
    if (g3)
	G3(i) = g3;
267
    else
268
	G3(i) = getUnknownCharset(T_94);
269

270 271
    if (other)
	OTHER(i) = other;
272
    else
273
	OTHER(i) = NULL;
274 275 276 277 278 279 280 281 282

    i->glp = &i->g[gl];
    i->grp = &i->g[gr];
    return 0;
}

int
mergeIso2022(Iso2022Ptr d, Iso2022Ptr s)
{
283 284 285 286 287 288 289 290 291 292 293 294 295 296
    if (G0(d) == NULL)
	G0(d) = G0(s);
    if (G1(d) == NULL)
	G1(d) = G1(s);
    if (G2(d) == NULL)
	G2(d) = G2(s);
    if (G3(d) == NULL)
	G3(d) = G3(s);
    if (OTHER(d) == NULL)
	OTHER(d) = OTHER(s);
    if (d->glp == NULL)
	d->glp = &(d->g[identifyCharset(s, s->glp)]);
    if (d->grp == NULL)
	d->grp = &(d->g[identifyCharset(s, s->grp)]);
297 298 299 300
    return 0;
}

static int
301
utf8Count(unsigned c)
302 303
{
    /* All return values must be less than BUFFERED_INPUT_SIZE */
304 305 306 307 308 309 310 311 312 313
    if ((c & 0x80) == 0)
	return 1;
    else if ((c & 0x40) == 0)
	return 1;		/* incorrect UTF-8 */
    else if ((c & 0x60) == 0x40)
	return 2;
    else if ((c & 0x70) == 0x60)
	return 3;
    else if ((c & 0x78) == 0x70)
	return 4;
314
    else
315
	return 1;
316 317 318 319 320
}

static int
fromUtf8(unsigned char *b)
{
321 322 323 324 325 326 327
    if ((b[0] & 0x80) == 0)
	return b[0];
    else if ((b[0] & 0x40) == 0)
	return -1;		/* incorrect UTF-8 */
    else if ((b[0] & 0x60) == 0x40)
	return ((b[0] & 0x1F) << 6) | (b[1] & 0x3F);
    else if ((b[0] & 0x70) == 0x60)
328 329 330
	return (((b[0] & 0x0F) << 12) |
		((b[1] & 0x3F) << 6) |
		((b[2] & 0x3F)));
331
    else if ((b[0] & 0x78) == 0x70)
332 333 334 335
	return (((b[0] & 0x03) << 18) |
		((b[1] & 0x3F) << 12) |
		((b[2] & 0x3F) << 6) |
		((b[3] & 0x3F)));
336
    else
337
	return -1;
338 339 340 341 342 343 344 345 346 347 348 349 350
}

void
copyIn(Iso2022Ptr is, int fd, unsigned char *buf, int count)
{
    unsigned char *c;
    int codepoint, rem;

    c = buf;
    rem = count;

#define NEXT do {c++; rem--;} while(0)

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    while (rem) {
	codepoint = -1;
	if (is->parserState == P_ESC) {
	    assert(buffered_input_count == 0);
	    codepoint = *c;
	    NEXT;
	    if (*c == CSI_7)
		is->parserState = P_CSI;
	    else if (IS_FINAL_ESC(codepoint))
		is->parserState = P_NORMAL;
	} else if (is->parserState == P_CSI) {
	    assert(buffered_input_count == 0);
	    codepoint = *c;
	    NEXT;
	    if (IS_FINAL_CSI(codepoint))
		is->parserState = P_NORMAL;
	} else if (!(*c & 0x80)) {
	    if (buffered_input_count > 0) {
		buffered_input_count = 0;
		continue;
	    } else {
		codepoint = *c;
		NEXT;
		if (codepoint == ESC)
		    is->parserState = P_ESC;
	    }
	} else if ((*c & 0x40)) {
	    if (buffered_input_count > 0) {
		buffered_input_count = 0;
		continue;
	    } else {
		buffered_input[buffered_input_count] = *c;
		buffered_input_count++;
		NEXT;
	    }
	} else {
	    if (buffered_input_count <= 0) {
		buffered_input_count = 0;
		NEXT;
		continue;
	    } else {
		buffered_input[buffered_input_count] = *c;
		buffered_input_count++;
		NEXT;
		if (buffered_input_count >= utf8Count(buffered_input[0])) {
		    codepoint = fromUtf8(buffered_input);
		    buffered_input_count = 0;
		    if (codepoint == CSI)
			is->parserState = P_CSI;
		}
	    }
	}
403 404
#undef NEXT

405 406
	if (codepoint >= 0) {
	    int i;
407
	    unsigned ucode = (unsigned) codepoint;
408
	    unsigned char obuf[4];
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

#define WRITE_1(i) do { \
	    obuf[0] = UChar(i); \
	    IGNORE_RC(write(fd, obuf, (size_t) 1)); \
	} while(0)
#define WRITE_2(i) do { \
	    obuf[0] = UChar(((i) >> 8) & 0xFF); \
	    obuf[1] = UChar((i) & 0xFF); \
	    IGNORE_RC(write(fd, obuf, (size_t) 2)); \
	} while(0)

#define WRITE_3(i) do { \
	    obuf[0] = UChar(((i) >> 16) & 0xFF); \
	    obuf[1] = UChar(((i) >>  8) & 0xFF); \
	    obuf[2] = UChar((i) & 0xFF); \
	    IGNORE_RC(write(fd, obuf, (size_t) 3)); \
	} while(0)

#define WRITE_4(i) do { \
	    obuf[0] = UChar(((i) >> 24) & 0xFF); \
	    obuf[1] = UChar(((i) >> 16) & 0xFF); \
	    obuf[2] = UChar(((i) >>  8) & 0xFF); \
	    obuf[3] = UChar((i) & 0xFF); \
	    IGNORE_RC(write(fd, obuf, (size_t) 4)); \
       } while(0)

#define WRITE_1_P_8bit(p, i) { \
	    obuf[0] = UChar(p); \
	    obuf[1] = UChar(i); \
	    IGNORE_RC(write(fd, obuf, (size_t) 2)); \
	}

#define WRITE_1_P_7bit(p, i) { \
	    obuf[0] = ESC; \
	    obuf[1] = UChar((p) - 0x40); \
	    obuf[2] = UChar(i); \
	    IGNORE_RC(write(fd, obuf, (size_t) 3)); \
	}

#define WRITE_1_P(p,i) do { \
	if(is->inputFlags & IF_EIGHTBIT) \
450
	    WRITE_1_P_8bit(p,i) else \
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	    WRITE_1_P_7bit(p,i) \
	} while(0)

#define WRITE_2_P_8bit(p, i) { \
	    obuf[0] = UChar(p); \
	    obuf[1] = UChar(((i) >> 8) & 0xFF); \
	    obuf[2] = UChar((i) & 0xFF); \
	    IGNORE_RC(write(fd, obuf, (size_t) 3)); \
	}

#define WRITE_2_P_7bit(p, i) { \
	    obuf[0] = ESC; \
	    obuf[1] = UChar((p) - 0x40); \
	    obuf[2] = UChar(((i) >> 8) & 0xFF); \
	    obuf[3] = UChar((i) & 0xFF); \
	    IGNORE_RC(write(fd, obuf, (size_t) 4)); \
	}

#define WRITE_2_P(p,i) do { \
	    if(is->inputFlags & IF_EIGHTBIT) \
		WRITE_2_P_8bit(p,i) \
	    else \
		WRITE_2_P_7bit(p,i) \
	} while(0)

#define WRITE_1_P_S(p,i,s) do { \
	    obuf[0] = UChar(p); \
	    obuf[1] = UChar((i) & 0xFF); \
	    obuf[2] = UChar(s); \
	    IGNORE_RC(write(fd, obuf, (size_t) 3)); \
	} while(0)

#define WRITE_2_P_S(p,i,s) do { \
	    obuf[0] = UChar(p); \
	    obuf[1] = UChar(((i) >> 8) & 0xFF); \
	    obuf[2] = UChar((i) & 0xFF); \
	    obuf[3] = UChar(s); \
	    IGNORE_RC(write(fd, obuf, (size_t) 4)); \
	} while(0)

	    if (ucode < 0x20 ||
492
		(OTHER(is) == NULL && CHARSET_REGULAR(GR(is)) &&
493 494
		 (ucode >= 0x80 && ucode < 0xA0))) {
		WRITE_1(ucode);
495 496 497
		continue;
	    }
	    if (OTHER(is) != NULL) {
498 499 500 501 502 503 504 505 506 507
		unsigned int c2;
		c2 = OTHER(is)->other_reverse(ucode, OTHER(is)->other_aux);
		if (c2 >> 24)
		    WRITE_4(c2);
		else if (c2 >> 16)
		    WRITE_3(c2);
		else if (c2 >> 8)
		    WRITE_2(c2);
		else if (c2)
		    WRITE_1(c2);
508 509
		continue;
	    }
510
	    i = (GL(is)->reverse) (ucode, GL(is));
511 512
	    if (i >= 0) {
		switch (GL(is)->type) {
513 514 515
		case T_94:
		case T_96:
		case T_128:
516 517 518
		    if (i >= 0x20)
			WRITE_1(i);
		    break;
519 520 521
		case T_9494:
		case T_9696:
		case T_94192:
522 523 524 525 526
		    if (i >= 0x2020)
			WRITE_2(i);
		    break;
		default:
		    abort();
527
		    /* NOTREACHED */
528 529 530 531
		}
		continue;
	    }
	    if (is->inputFlags & IF_EIGHTBIT) {
532
		i = GR(is)->reverse(ucode, GR(is));
533 534
		if (i >= 0) {
		    switch (GR(is)->type) {
535 536 537
		    case T_94:
		    case T_96:
		    case T_128:
538 539 540
			/* we allow C1 characters if T_128 in GR */
			WRITE_1(i | 0x80);
			break;
541 542
		    case T_9494:
		    case T_9696:
543 544 545 546 547 548 549
			WRITE_2(i | 0x8080);
			break;
		    case T_94192:
			WRITE_2(i | 0x8000);
			break;
		    default:
			abort();
550
			/* NOTREACHED */
551 552 553 554 555
		    }
		    continue;
		}
	    }
	    if (is->inputFlags & IF_SS) {
556
		i = G2(is)->reverse(ucode, G2(is));
557 558
		if (i >= 0) {
		    switch (GR(is)->type) {
559 560 561
		    case T_94:
		    case T_96:
		    case T_128:
562 563 564 565 566 567 568
			if (i >= 0x20) {
			    if ((is->inputFlags & IF_EIGHTBIT) &&
				(is->inputFlags & IF_SSGR))
				i |= 0x80;
			    WRITE_1_P(SS2, i);
			}
			break;
569 570
		    case T_9494:
		    case T_9696:
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
			if (i >= 0x2020) {
			    if ((is->inputFlags & IF_EIGHTBIT) &&
				(is->inputFlags & IF_SSGR))
				i |= 0x8080;
			    WRITE_2_P(SS2, i);
			}
			break;
		    case T_94192:
			if (i >= 0x2020) {
			    if ((is->inputFlags & IF_EIGHTBIT) &&
				(is->inputFlags & IF_SSGR))
				i |= 0x8000;
			    WRITE_2_P(SS2, i);
			}
			break;
		    default:
			abort();
588
			/* NOTREACHED */
589 590 591 592 593
		    }
		    continue;
		}
	    }
	    if (is->inputFlags & IF_SS) {
594
		i = G3(is)->reverse(ucode, G3(is));
595
		switch (GR(is)->type) {
596 597 598
		case T_94:
		case T_96:
		case T_128:
599 600 601 602 603 604 605
		    if (i >= 0x20) {
			if ((is->inputFlags & IF_EIGHTBIT) &&
			    (is->inputFlags & IF_SSGR))
			    i |= 0x80;
			WRITE_1_P(SS3, i);
		    }
		    break;
606 607
		case T_9494:
		case T_9696:
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
		    if (i >= 0x2020) {
			if ((is->inputFlags & IF_EIGHTBIT) &&
			    (is->inputFlags & IF_SSGR))
			    i |= 0x8080;
			WRITE_2_P(SS3, i);
		    }
		    break;
		case T_94192:
		    if (i >= 0x2020) {
			if ((is->inputFlags & IF_EIGHTBIT) &&
			    (is->inputFlags & IF_SSGR))
			    i |= 0x8000;
			WRITE_2_P(SS3, i);
		    }
		    break;
		default:
		    abort();
625
		    /* NOTREACHED */
626 627 628 629
		}
		continue;
	    }
	    if (is->inputFlags & IF_LS) {
630
		i = GR(is)->reverse(ucode, GR(is));
631 632
		if (i >= 0) {
		    switch (GR(is)->type) {
633 634 635
		    case T_94:
		    case T_96:
		    case T_128:
636 637
			WRITE_1_P_S(LS1, i, LS0);
			break;
638 639
		    case T_9494:
		    case T_9696:
640 641 642 643 644 645 646
			WRITE_2_P_S(LS1, i, LS0);
			break;
		    case T_94192:
			WRITE_2_P_S(LS1, i, LS0);
			break;
		    default:
			abort();
647
			/* NOTREACHED */
648 649 650 651
		    }
		    continue;
		}
	    }
652 653 654 655 656 657 658 659
#undef WRITE_1
#undef WRITE_2
#undef WRITE_1_P
#undef WRITE_1_P_7bit
#undef WRITE_1_P_8bit
#undef WRITE_2_P
#undef WRITE_2_P_7bit
#undef WRITE_2_P_8bit
660
	}
661 662 663
    }
}

664 665
#define PAIR(a,b) ((unsigned) ((a) << 8) | (b))

666
void
667
copyOut(Iso2022Ptr is, int fd, unsigned char *buf, unsigned count)
668 669 670
{
    unsigned char *s = buf;

671
    if (ilog >= 0)
672
	IGNORE_RC(write(ilog, buf, (size_t) count));
673

674 675 676 677 678 679 680 681 682 683 684
    while (s < buf + count) {
	switch (is->parserState) {
	case P_NORMAL:
	  resynch:
	    if (is->buffered_ku < 0) {
		if (*s == ESC) {
		    buffer(is, *s++);
		    is->parserState = P_ESC;
		} else if (OTHER(is) != NULL) {
		    int c = OTHER(is)->other_stack(*s, OTHER(is)->other_aux);
		    if (c >= 0) {
685 686 687
			unsigned ucode = (unsigned) c;
			outbufUTF8(is, fd,
				   OTHER(is)->other_recode(ucode, OTHER(is)->other_aux));
688 689 690 691 692 693
			is->shiftState = S_NORMAL;
		    }
		    s++;
		} else if (*s == CSI && CHARSET_REGULAR(GR(is))) {
		    buffer(is, *s++);
		    is->parserState = P_CSI;
694 695 696 697
		} else if ((*s == SS2 ||
			    *s == SS3 ||
			    *s == LS0 ||
			    *s == LS1) &&
698 699 700 701 702 703 704 705 706
			   CHARSET_REGULAR(GR(is))) {
		    buffer(is, *s++);
		    terminate(is, fd);
		    is->parserState = P_NORMAL;
		} else if (*s <= 0x20 && is->shiftState == S_NORMAL) {
		    /* Pass through C0 when GL is not regular */
		    outbufOne(is, fd, *s);
		    s++;
		} else {
707
		    const CharsetRec *charset;
708 709 710
		    unsigned char code = 0;
		    if (*s <= 0x7F) {
			switch (is->shiftState) {
711 712 713 714 715 716 717 718 719 720 721 722
			case S_NORMAL:
			    charset = GL(is);
			    break;
			case S_SS2:
			    charset = G2(is);
			    break;
			case S_SS3:
			    charset = G3(is);
			    break;
			default:
			    abort();
			    /* NOTREACHED */
723 724 725 726
			}
			code = *s;
		    } else {
			switch (is->shiftState) {
727 728 729 730 731 732 733 734 735 736 737 738
			case S_NORMAL:
			    charset = GR(is);
			    break;
			case S_SS2:
			    charset = G2(is);
			    break;
			case S_SS3:
			    charset = G3(is);
			    break;
			default:
			    abort();
			    /* NOTREACHED */
739
			}
740
			code = UChar(*s - 0x80);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
		    }

		    switch (charset->type) {
		    case T_94:
			if (code >= 0x21 && code <= 0x7E)
			    outbufUTF8(is, fd, charset->recode(code, charset));
			else
			    outbufUTF8(is, fd, *s);
			s++;
			is->shiftState = S_NORMAL;
			break;
		    case T_96:
			if (code >= 0x20)
			    outbufUTF8(is, fd, charset->recode(code, charset));
			else
			    outbufUTF8(is, fd, *s);
			is->shiftState = S_NORMAL;
			s++;
			break;
		    case T_128:
			outbufUTF8(is, fd, charset->recode(code, charset));
			is->shiftState = S_NORMAL;
			s++;
			break;
		    default:
			/* First byte of a multibyte sequence */
			is->buffered_ku = *s;
			s++;
		    }
		}
	    } else {		/* buffered_ku */
772
		const CharsetRec *charset;
773 774 775 776
		unsigned char ku_code;
		unsigned code = 0;
		if (is->buffered_ku <= 0x7F) {
		    switch (is->shiftState) {
777 778 779 780 781 782 783 784 785 786 787 788
		    case S_NORMAL:
			charset = GL(is);
			break;
		    case S_SS2:
			charset = G2(is);
			break;
		    case S_SS3:
			charset = G3(is);
			break;
		    default:
			abort();
			/* NOTREACHED */
789
		    }
790
		    ku_code = UChar(is->buffered_ku);
791 792 793 794
		    if (*s < 0x80)
			code = *s;
		} else {
		    switch (is->shiftState) {
795 796 797 798 799 800 801 802 803 804 805 806
		    case S_NORMAL:
			charset = GR(is);
			break;
		    case S_SS2:
			charset = G2(is);
			break;
		    case S_SS3:
			charset = G3(is);
			break;
		    default:
			abort();
			/* NOTREACHED */
807
		    }
808
		    ku_code = UChar(is->buffered_ku - 0x80);
809
		    if (*s >= 0x80)
810
			code = UChar(*s - 0x80);
811 812 813 814 815 816
		}
		switch (charset->type) {
		case T_94:
		case T_96:
		case T_128:
		    abort();
817
		    /* NOTREACHED */
818 819 820 821
		    break;
		case T_9494:
		    if (code >= 0x21 && code <= 0x7E) {
			outbufUTF8(is, fd,
822
				   charset->recode(PAIR(ku_code, code), charset));
823 824 825 826 827 828 829 830 831 832 833 834
			is->buffered_ku = -1;
			is->shiftState = S_NORMAL;
		    } else {
			is->buffered_ku = -1;
			is->shiftState = S_NORMAL;
			goto resynch;
		    }
		    s++;
		    break;
		case T_9696:
		    if (code >= 0x20) {
			outbufUTF8(is, fd,
835
				   charset->recode(PAIR(ku_code, code), charset));
836 837 838 839 840 841 842 843 844 845 846 847 848
			is->buffered_ku = -1;
			is->shiftState = S_NORMAL;
		    } else {
			is->buffered_ku = -1;
			is->shiftState = S_NORMAL;
			goto resynch;
		    }
		    s++;
		    break;
		case T_94192:
		    /* Use *s, not code */
		    if (((*s >= 0x21) && (*s <= 0x7E)) ||
			((*s >= 0xA1) && (*s <= 0xFE))) {
849
			unsigned ucode = PAIR(ku_code, *s);
850
			outbufUTF8(is, fd,
851
				   charset->recode(ucode, charset));
852 853 854 855 856 857 858 859 860 861 862
			is->buffered_ku = -1;
			is->shiftState = S_NORMAL;
		    } else {
			is->buffered_ku = -1;
			is->shiftState = S_NORMAL;
			goto resynch;
		    }
		    s++;
		    break;
		default:
		    abort();
863
		    /* NOTREACHED */
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
		}
	    }
	    break;
	case P_ESC:
	    assert(is->buffered_ku == -1);
	    if (*s == CSI_7) {
		buffer(is, *s++);
		is->parserState = P_CSI;
	    } else if (IS_FINAL_ESC(*s)) {
		buffer(is, *s++);
		terminate(is, fd);
		is->parserState = P_NORMAL;
	    } else {
		buffer(is, *s++);
	    }
	    break;
	case P_CSI:
	    if (IS_FINAL_CSI(*s)) {
		buffer(is, *s++);
		terminate(is, fd);
		is->parserState = P_NORMAL;
	    } else {
		buffer(is, *s++);
	    }
	    break;
	default:
	    abort();
891
	    /* NOTREACHED */
892
	}
893 894 895 896
    }
    outbuf_flush(is, fd);
}

897 898
void
terminate(Iso2022Ptr is, int fd)
899
{
900 901 902
    if (is->outputFlags & OF_PASSTHRU) {
	outbuf_buffered(is, fd);
	return;
903 904
    }

905
    switch (is->buffered[0]) {
906
    case SS2:
907 908 909 910
	if (is->outputFlags & OF_SS)
	    is->shiftState = S_SS2;
	discard_buffered(is);
	return;
911
    case SS3:
912 913 914 915
	if (is->outputFlags & OF_SS)
	    is->shiftState = S_SS3;
	discard_buffered(is);
	return;
916
    case LS0:
917 918 919 920
	if (is->outputFlags & OF_LS)
	    is->glp = &G0(is);
	discard_buffered(is);
	return;
921
    case LS1:
922 923 924 925
	if (is->outputFlags & OF_LS)
	    is->glp = &G1(is);
	discard_buffered(is);
	return;
926
    case ESC:
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	assert(is->buffered_count >= 2);
	switch (is->buffered[1]) {
	case SS2_7:
	    if (is->outputFlags & OF_SS)
		is->shiftState = S_SS2;
	    discard_buffered(is);
	    return;
	case SS3_7:
	    if (is->outputFlags & OF_SS)
		is->shiftState = S_SS3;
	    discard_buffered(is);
	    return;
	case LS2_7:
	    if (is->outputFlags & OF_SS)
		is->glp = &G2(is);
	    discard_buffered(is);
	    return;
	case LS3_7:
	    if (is->outputFlags & OF_LS)
		is->glp = &G3(is);
	    discard_buffered(is);
	    return;
	case LS1R_7:
	    if (is->outputFlags & OF_LS)
		is->grp = &G1(is);
	    discard_buffered(is);
	    return;
	case LS2R_7:
	    if (is->outputFlags & OF_LS)
		is->grp = &G2(is);
	    discard_buffered(is);
	    return;
	case LS3R_7:
	    if (is->outputFlags & OF_LS)
		is->grp = &G3(is);
	    discard_buffered(is);
	    return;
	default:
965 966 967 968
	    terminateEsc(is, fd,
			 is->buffered + 1,
			 (unsigned) (is->buffered_count - 1));
	    break;
969 970
	}
	return;
971
    default:
972
	outbuf_buffered(is, fd);
973 974 975 976
    }
}

void
977
terminateEsc(Iso2022Ptr is, int fd, unsigned char *s_start, unsigned count)
978
{
979
    const CharsetRec *charset;
980 981 982 983

    /* ISO 2022 doesn't allow 2C, but Emacs/MULE uses it in 7-bit
       mode */

984 985 986 987 988 989 990 991 992 993 994
    if ((s_start[0] == 0x28 || s_start[0] == 0x29 ||
	 s_start[0] == 0x2A || s_start[0] == 0x2B ||
	 s_start[0] == 0x2C || s_start[0] == 0x2D ||
	 s_start[0] == 0x2E || s_start[0] == 0x2F) &&
	count >= 2) {
	if (is->outputFlags & OF_SELECT) {
	    if (s_start[0] <= 0x2B)
		charset = getCharset(s_start[1], T_94);
	    else
		charset = getCharset(s_start[1], T_96);
	    switch (s_start[0]) {
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	    case 0x28:
	    case 0x2C:
		G0(is) = charset;
		break;
	    case 0x29:
	    case 0x2D:
		G1(is) = charset;
		break;
	    case 0x2A:
	    case 0x2E:
		G2(is) = charset;
		break;
	    case 0x2B:
	    case 0x2F:
		G3(is) = charset;
		break;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
	    }
	}
	discard_buffered(is);
    } else if (s_start[0] == 0x24 && count == 2) {
	if (is->outputFlags & OF_SELECT) {
	    charset = getCharset(s_start[1], T_9494);
	    G0(is) = charset;
	}
	discard_buffered(is);
    } else if (s_start[0] == 0x24 && count >= 2 &&
	       (s_start[1] == 0x28 || s_start[1] == 0x29 ||
		s_start[1] == 0x2A || s_start[1] == 0x2B ||
		s_start[1] == 0x2D || s_start[1] == 0x2E ||
		s_start[1] == 0x2F) &&
	       count >= 3) {
	if (is->outputFlags & OF_SELECT) {
	    if (s_start[1] <= 0x2B)
		charset = getCharset(s_start[2], T_9494);
	    else
		charset = getCharset(s_start[2], T_9696);
	    switch (s_start[1]) {
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
	    case 0x28:
		G0(is) = charset;
		break;
	    case 0x29:
	    case 0x2D:
		G1(is) = charset;
		break;
	    case 0x2A:
	    case 0x2E:
		G2(is) = charset;
		break;
	    case 0x2B:
	    case 0x2F:
		G3(is) = charset;
		break;
1047 1048 1049
	    }
	}
	discard_buffered(is);
1050
    } else
1051
	outbuf_buffered(is, fd);
1052
}
1053 1054 1055 1056 1057 1058 1059

#ifdef NO_LEAKS
void
iso2022_leaks(void)
{
}
#endif