/*
 * RJIS ( Recover JIS code from broken file )
 * $Header: rjis.c,v 0.2 92/09/04 takahasi Exp $
 * Copyright (C) 1992
 * Hironobu Takahashi (takahasi@tiny.or.jp)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either versions 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KAKASI, see the file COPYING.  If not, write to the Free
 * Software Foundation Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/* $Log:	rjis.c,v $
 *
 */

#include <stdio.h>

FILE *input, *output;
extern void exit();

main(argc, argv)
int argc;
char *argv[];
{
    int i;

    input = stdin;
    output = stdout;

    for (i = 1; i < argc; ++ i) {
	if (argv[i][0] == '-') {
	    usage(argv);
	    exit(0);
	} else {
	    if (input == stdin) {
		if ((input = fopen(argv[i], "r")) == NULL) {
		    perror(argv[i]);
		    exit(1);
		}
	    } else {
		if ((output = fopen(argv[i], "w")) == NULL) {
		    perror(argv[i]);
		    exit(1);
		}
		break;
	    }
	}
    }

    process();
    return 0;
}

usage(argv)
char *argv[];
{
    fprintf(stderr, "Usage:\n");
    fprintf(stderr, "\n");
    fprintf(stderr, " Recover: %s [input_file [output_file]]\n", argv[0]);
    fprintf(stderr, "\n");
}

process()
{
    int c1, c2, c3, c4;

    for(;;) {
	/* stage 1 (ascii or jisroman) */
	while((c1 = getc(input)) != EOF) {
	    if (c1 == '$') {
		c2 = getc(input);
		if ((c2 == '@') || (c2 == 'B')) {
		    /* start kanji proc */
		    c3 = getc(input);
		    c4 = getc(input);
		    if (maybekanji(c3, c4)) {
			putc('\033', output);
			putc(c1, output);
			putc(c2, output);
			putc(c3, output);
			putc(c4, output);
			break;
		    } else {
			putc(c1, output);
			putc(c2, output);
			putc(c3, output);
			putc(c4, output);
		    }			
		} else {
		    putc(c1, output);
		    putc(c2, output);
		}
	    } else {
		putc(c1, output);
	    }
	}
	if (c1 == EOF) return;

	/* stage 2 (kanji) */
	while((c1 = getc(input)) != EOF) {
	    c2 = getc(input);
	    if ((c1 == '(') &&
		((c2 == 'B') || (c2 == 'J'))) {
		putc('\033', output);
		putc(c1, output);
		putc(c2, output);
		break;
	    }
	    putc(c1, output);
	    putc(c2, output);
	}
	if (c1 == EOF) return;
    }
}

int maybekanji(c1, c2)
int c1, c2;
{

    if ((c2 < 33) || (c2 > 126)) return 0;
    if ((c1 < 33) || ((40 < c1) && (c1 < 48)) || (116 < c1)) return 0;
    c2 -= 32;
    switch(c1-32) {
      case 2:
	if ((14 < c2) && ( c2 < 26)) return 0;
	if ((33 < c2) && ( c2 < 42)) return 0;
	if ((48 < c2) && ( c2 < 60)) return 0;
	if ((74 < c2) && ( c2 < 82)) return 0;
	if ((89 < c2) && ( c2 < 94)) return 0;
	break;
      case 3:
	if (c2 < 16) return 0;
	if ((25 < c2) && ( c2 < 33)) return 0;
	if ((58 < c2) && ( c2 < 65)) return 0;
	if (90 < c2) return 0;
	break;
      case 4:
	if (83 < c2) return 0;
	break;
      case 5:
	if (86 < c2) return 0;
	break;
      case 6:
	if ((24 < c2) && ( c2 < 33)) return 0;
	if (56 < c2) return 0;
	break;
      case 7:
	if ((33 < c2) && ( c2 < 49)) return 0;
	if (81 < c2) return 0;
	break;
      case 8:
	if (32 < c2) return 0;
	break;
      case 47:
	if (51 < c2) return 0;
	break;
      case 84:
	if (6 < c2) return 0;
	break;
    }
    return 1;
}


syntax highlighted by Code2HTML, v. 0.9.1