/* $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: scrwidth.c 1.2 90/04/21 $@(#)libw-port:wmisc/scrwidth.c 2.0 AT&T-UP"

#include	<libw.h>
#include	<ctype.h>
#define eucw1 _ctype[514]	/* EUC width for code set 1 */
#define eucw2 _ctype[515]	/* EUC width for code set 2 */
#define eucw3 _ctype[516]	/* EUC width for code set 3 */
#define scrw1 _ctype[517]
#define scrw2 _ctype[518]
#define scrw3 _ctype[519]
#define multibyte (_ctype[520]>1)
#define	EUCMASK	0x8080
#define P00	0x0000		/* Code Set 0 */
#define P11	0x8080		/* Code Set 1 */
#define P01	0x0080		/* Code Set 2 */
#define P10	0x8000		/* Code Set 3 */

/*
 *	The followings are for 4 bytes of multi-byte code definition.
 */
#define F_EUCMASK 0x30000000	/* mask bit for 32 bits */
#define F_P00     0x00000000	/* Code Set 0 for 4 bytes code */
#define F_P11     0x30000000	/* Code Set 1 for 4 bytes code */
#define F_P01     0x10000000	/* Code Set 2 for 4 bytes code */
#define F_P10     0x20000000	/* Code Set 3 for 4 bytes code */

int scrwidth(c)
wchar_t c;
{
	short pcw;	/* process code width */
	if(!wisprint(c))
		return(0);
	/*
	 *	check process code width with _ctype[]
	 */
	if ( eucw1 > sizeof(unsigned short ) ||
	     eucw2 > sizeof(unsigned short ) ||
	     eucw3 > sizeof(unsigned short ))
		pcw = sizeof(unsigned long);	/* 32 bits process code */
	else
		pcw = sizeof(unsigned short);	/* 16 bits process code */

	if (pcw > sizeof(unsigned short)) {
		return(!multibyte || c <= 0177 ? 1 : (c & F_P11) == F_P11 ? scrw1 :
			(c & F_P01) ? scrw2 : scrw3);
	} else {

		return(!multibyte || c <= 0177 ? 1 : (c & P11) == P11 ? scrw1 :
			(c & P01) ? scrw2 : scrw3);
	}
}
