/* chset.c
 *
 * Reads a character set from stdin converts and outputs to stdout.
 */

#ifndef lint
static char *Rcsid = "@(#)$Header$";

#endif

/*
 *
 * $Log: chset.c,v $
 *
 */

/* #include "sendmail.h" */

#include <stdio.h>
#include <errno.h>
#include "iso646.h"
#include "charset.h"

#define SLEN	256		/* max length in octets of images */
static CHARSET *chset;
static CHARSET *ref;
extern struct chdbhdr hdr;

/* FILE *dfopen(); TW */

char *xalloc();

char s ARRAY(80);
char *
mnem(i)
int i;
{
	*(s+0)= *(ref->cs->out + hdr.basechrs + i%hdr.basechrs);
	*(s+1)= *(ref->cs->out + hdr.basechrs + i/hdr.basechrs);
	*(s+2)= 0;
	if (i > hdr.outsize) sprintf(s,"%d",i);
	return s;
}

void
main (argc, argv)
	int	 argc;
	char   **argv;
{
	/* CHAR8U  s ARRAY(SLEN), r ARRAY(SLEN); UNUSED! 941114 / TW */
	char us ARRAY(3);
	CHARTAB *t;
	unsigned int i,j,oc;

	(void) strcpy(us, "US");
	ref = getchset (us,DEFAULT_ESCAPE);

	switch (argc) {

	    case 2:
		chset = getchset (*(argv+1), DEFAULT_ESCAPE);
		break;

	    default:
		printf ("\n\nUsage: %s charset-in   \n\n", *argv);
		exit (1);
	}

	if (chset == NULL ) {
		printf ("\n\n*** Error: Unknown Charset/s\n\n");
		exit (1);
	}

	t= chset->cs;
	printf("name: %s ",t->name); /* %x -> %lx below 941114 / TW */
	printf("bits=%d  g0= %lx  comb=%d\n\n",t->bits,t->g0esc,t->combtabs);
	printf("input:\n");
	for (i = 0; i< 16*(t->combtabs+1); i++) {
		for (j=0; j<16; j++) {
			oc= *(t->in + i*16+j);
			printf("%2s ",mnem(oc));
		}
		printf("\n");
	}

	printf("output:\n");
	for (i=0; i < hdr.outsize; i++) {
		if (t->outbytes== 2) oc= *((INT16S *) t->out + i);
		else oc= *(t->out + i);
		if (oc) printf("%2.2s %.4x\n",mnem(i),oc bitand 0177777);
	}

	exit (0);
}
