/*
 * $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) 1984 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: set_charset.c 1.2 89/10/10 $@(#)nlp:lib/lp/set_charset.c	1.5"
/* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */

#include "stdio.h"
#include "string.h"
#include "errno.h"

#include "lp.h"
#include "lp.set.h"

extern char		*getenv(),
			*malloc(),
			*tparm();

extern void		free();

extern long		strtol();

static int		cat_charset();

/**
 ** set_charset()
 **/

int			set_charset (char_set, putout, type)
	char			*char_set;
	int			putout;
	char			*type;
{
	int			cs,
				ret;

	char			*rest,
				*char_set_nm,
				*char_set_names,
				*select_char_set,
				*start_char_set_def,
				*p,
				*q;

	unsigned short		has_print_wheel;


	tidbit ((char *)0, "daisy", &has_print_wheel);
	if (has_print_wheel)
		return (E_SUCCESS);

	tidbit ((char *)0, "csnm", &char_set_names);
	if (
		strlen(char_set) > 2
	     && char_set[0] == 'c'
	     && char_set[1] == 's'
	     && char_set[2]
	     && 0 <= (cs = strtol(char_set + 2, &rest, 10)) && cs <= 63
	     && !*rest
	)
		char_set_nm = tparm(char_set_names, cs);

	else {
		for (cs = 0; cs <= 63; cs++)
			if (
				(char_set_nm = tparm(char_set_names, cs))
			     && *char_set_nm
			     && STREQU(char_set_nm, char_set)
			)
				break;
		if (cs > 63)
			return (E_FAILURE);
	}

	if (cs == 0)
		return (E_SUCCESS);

	if (char_set_nm)
		if (!(char_set_nm = strdup(char_set_nm))) {
			errno = ENOMEM;
			ret = E_FAILURE;
			goto Return;
		}

	tidbit ((char *)0, "scs", &select_char_set);
	p = q = 0;
	if ((p = tparm(select_char_set, cs)) && *p && (p = strdup(p))) {

		tidbit ((char *)0, "scsd", &start_char_set_def);
		if ((q = tparm(start_char_set_def, cs)) && *q) {
			/*
			 * The ``start char. set def'n.'' capability
			 * is defined and set, so assume we MUST
			 * download the character set before using it.
			 */
			if (
				OKAY(char_set_nm)
			     && cat_charset(char_set_nm, 0, type, putout) != -1
			     || cat_charset((char *)0, cs, type, putout) != -1
			     || cat_charset(char_set, 0, type, putout) != -1
			)
				;
			else {
				ret = E_FAILURE;
				goto Return;
			}

		} else {
			/*
			 * The ``start char. set def'n.'' capability
			 * is not defined and or set, so assume we MAY
			 * download the character set before using it.
			 */
			if (
				OKAY(char_set_nm)
			     && cat_charset(char_set_nm, 0, type, putout) != -1
			     || cat_charset((char *)0, cs, type, putout) != -1
			     || cat_charset(char_set, 0, type, putout) != -1
			)
				;
		}

		if (putout)
			putp (p);
		ret = E_SUCCESS;

	} else
		ret = E_FAILURE;

Return:	if (p)
		free (p);
	if (q)
		free (q);
	if (char_set_nm)
		free (char_set_nm);
	return (ret);
}

/**
 ** cat_charset() - DUMP CONTENT OF CHARACTER SET DEF'N FILE
 **/

static int		cat_charset (name, number, type, putout)
	char			*name;
	int			number,
				putout;
	char			*type;
{
	FILE			*fp;

	char			*p,
				*parent,
				*T,
				buf[BUFSIZ];

	int			n,
				ret;

	if (!name)
		sprintf ((name = "63"), "%d", number);

	if (!(parent = getenv("CHARSETDIR")))
		parent = CHARSETDIR;

	(T = "x")[0] = type[0];
	p = makepath(parent, T, type, name, (char *)0);
	if (!p)
		return (-1);
	if (!(fp = open_lpfile(p, "r"))) {
		free (p);
		return (-1);
	}
	free (p);

	if (putout) {

		while ((n = fread(buf, 1, BUFSIZ, fp)))
			fwrite (buf, 1, n, stdout);

		if (ferror(fp))
			ret = -1;
		else
			ret = 0;

	}
	close_lpfile (fp);
	return (ret);
}
