
/* 
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: iconv.c 1.1 90/01/11 $@(#)iconv.c	ES 3.2 1989/12"
/*
 * iconv.c	code set conversion
 */

#include <stdio.h>
#include <sys/errno.h>
#include <string.h>

#define DBASE 		"/usr/lib/kbd/iconv_data"	/* default database */
#define CMDFMT		"kbdpipe -t %s -f %s"		/* command format */
#define CMDSZ 		513			/* max command string size */
#define MAXLINE 	1282			/* max chars in database line */
#define MINFLDS 	4			/* min fields in database */
#define FLDSZ 		257			/* max field size in database */

extern int errno, optind, opterr;
extern char *optarg;

char *fcode, *tcode; 	/*required arguments fromcode and tocode*/
char *mode, *dbase;  	/*optional arguments mode and database */

char from[FLDSZ], to[FLDSZ], table[FLDSZ];
char file[FLDSZ], tmode[FLDSZ];		/* database field variables */

char *prog;

main(argc, argv)
int argc;
char **argv;
{
	register int c, v;
	char cmd[CMDSZ];

	fcode = tcode = mode = dbase = (char *) 0;
	cmd[CMDSZ-1] = '\0';
	prog = *argv;
	c = v = 0;

	while ((c = getopt(argc, argv, "f:t:m:d:v")) != EOF)
		switch (c) {
			case 'f':	fcode = optarg; break;	
			case 't':	tcode = optarg; break;
			case 'm':	mode = optarg; break;
			case 'd':	dbase = optarg; break;
			case 'v':	v = 1; break;
			default:	usage_iconv();
		}

	/* required arguments */
	if ( !v && (!fcode || !tcode))	
		usage_iconv();


	if (search_dbase(v)) {		/* search database */	

		(void) sprintf(cmd, CMDFMT, table, file);

		for ( ; optind < argc; optind++)
			(void) strcat(strcat(cmd, " "), argv[optind]);

		if (cmd[CMDSZ-1] != NULL) {
			(void) fprintf(stderr, "%s: %s: command string has more than %d characters\n",
					prog, CMDFMT, CMDSZ-1);
			exit(1);
		}

		if (system(cmd)) {
			(void) fprintf(stderr, "%s: cannot execute \"%s\"\n",
					prog, cmd);
			exit(1);
		}
	}

	else if (v)
		(void) printf("%s: %s: verified as correct\n", prog, dbase);

	else
		no_match();		/* no match found */
}


usage_iconv()
{
	(void) fprintf(stderr,
                	"Usage: %s -f fromcode -t tocode [-m mode] [-d database] [-v] [file(s)]\n",
                	prog);
	exit(1);
}


search_dbase(verify)
int verify;
{
	int fields, row;
	char buff[MAXLINE];
	FILE *dbfp;


	fields = 0;

	from[FLDSZ-1] = to[FLDSZ-1] = table[FLDSZ-1] = '\0';
	file[FLDSZ-1] = tmode[FLDSZ-1] = buff[MAXLINE-2] = '\0';


	/* open database for reading */

	if (! dbase)		/* use default database */
		dbase = DBASE;

	if ((dbfp = fopen(dbase, "r")) == NULL) {
		(void) fprintf(stderr,"%s: %s: cannot open for reading (%d)\n",
                                	prog, dbase, errno);
			exit(1);
	}


	/* start the search */

	for (row=1; ; row++) {

		/* fill buffer with line of database */
		if (fgets(buff, MAXLINE, dbfp) == NULL) {
			(void) fclose(dbfp);
			return(0);
		}

		if (buff[MAXLINE-2] != NULL) {
			(void) fprintf(stderr, "%s: %s: row %d has more than %d characters\n",
					prog, dbase, row, MAXLINE-2);
			exit(1);
		}

		if (! mode)
			fields = sscanf(buff, "%s %s %s %s",
					from, to, table, file);
		else
			fields = sscanf(buff, "%s %s %s %s %s",
					from, to, table, file, tmode);

		if (fields < MINFLDS) {
			(void) fprintf(stderr, "%s: %s: row %d cannot retrieve required %d fields\n",
					prog, dbase, row, MINFLDS);
			exit(1);
		}


		if ( (from[FLDSZ-1] != NULL) || (to[FLDSZ-1] != NULL) ||
		     (table[FLDSZ-1] != NULL) || (file[FLDSZ-1] != NULL) ||
		     (tmode[FLDSZ-1] != NULL) ) {
			(void) fprintf(stderr, "%s: %s: row %d has a field with more than %d characters\n",
					prog, dbase, row, FLDSZ-1);
			exit(1);
		}


		if (! verify) {
			if ( (strcmp(fcode, from) == NULL) &&
			     (strcmp(tcode, to) == NULL) ) {

				if ( (! mode) ||
				     (strcmp(mode, tmode) == NULL) ) {

					(void) fclose(dbfp);
					return(1);
				}
			}
		}
	}
}


no_match()		/* no match found in database */
{
	if (! mode)
		(void) fprintf(stderr, "%s: %s: no match found for fromcode: %s, tocode: %s\n",
				prog, dbase, fcode, tcode);
	else
		(void) fprintf(stderr, "%s: %s: no match found for fromcode: %s, tocode: %s, mode: %s\n",
				prog, dbase, fcode, tcode, mode);
}

