/*
 * |-----------------------------------------------------------|
 * | Copyright (c) 1990 MIPS Computer Systems, Inc.            |
 * | All Rights Reserved                                       |
 * |-----------------------------------------------------------|
 * |          Restricted Rights Legend                         |
 * | Use, duplication, or disclosure by the Government is      |
 * | subject to restrictions as set forth in                   |
 * | subparagraph (c)(1)(ii) of the Rights in Technical        |
 * | Data and Computer Software Clause of DFARS 52.227-7013.   |
 * |         MIPS Computer Systems, Inc.                       |
 * |         928 Arques Avenue                                 |
 * |         Sunnyvale, CA 94086                               |
 * |-----------------------------------------------------------|
 */
#ident	"$Header: fgetwc.c,v 1.1.2.1 92/06/24 14:10:57 marker Exp $"

/*	Copyright (c) 1986 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.	*/

/*	This module is created for MNLS on Sep.03.86		*/
/*	This module is modified for MNLS 3.2 on Jan.18.1989		*/

#ident	"@(#)libw-port:wstdio/fgetwc.c	1.0"

/*
 * Fgetwc transforms the next character in EUC from the named input
 * "iop" into the Process Code, and returns it as an integer.
 * The character widths in EUC are determined from an environment
 * variable CSWIDTH.
 */

#include "shlib.h"
#include <stdio.h>
#include <errno.h>
/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989	*/
#include <euc.h>
/*	end for MNLS3.2	*/
#include "pcode.h"

extern int  _filbuf();
extern void _xgetwidth();
extern int  _cswidth[];
extern int  _pcmask[];
/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989	*/
extern eucwidth_t _eucwidth;
#define pcw	_eucwidth._pcw
/*	end for MNLS3.2	*/

int
fgetwc(iop)
FILE *iop;
{
/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989	*/
/*	register byte, size, temp = 0, cset;	*/
	register byte, size, temp = 0, cset, mask;
/*	end for MNLS3.2	*/

	if ( (byte = getc(iop)) == EOF )
		return(EOF);

	if ( !(byte & 0x80) )	/* ASCII code */
		return(byte);

	if (! *_cswidth)
		_xgetwidth(); /* get char width from CSWIDTH */
	/*
	 * "Size" tells us how many bytes to expect following.
	 * Value returned by "_xgetwidth" for Set 1 is the number of
	 * bytes; subtract 1 to get number of bytes to expect.
	 */
	switch (byte) {
		case SS2: /* set 2 */
		/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989  */
			if(pcw > sizeof(unsigned short))
				mask = F_P01;
			else
				mask = P01;
		/*	end for MNLS3.2	*/
			cset = 2;
			size = _cswidth[2];
			goto testsize;
		case SS3: /* set 3 */
		/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989  */
			if(pcw > sizeof(unsigned short))
				mask = F_P10;
			else
				mask = P10;
		/*	end for MNLS3.2	*/
			cset = 3;
			size = _cswidth[3];
		testsize: /* EUC size is zero */
			if (size == 0)
				return(byte);
			break;
		default:  /* set 1 */
			if (control1(byte)) /* control character 1 */
				return(byte);

		/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989  */
			if(pcw > sizeof(unsigned short))
				mask = F_P11;
			else
				mask = P11;
		/*	end for MNLS3.2	*/

			cset = 1;
			if ((size = _cswidth[1] - 1) >= 0)
				temp = byte;
			else { /* EUC size is zero */
				ungetc(byte, iop);
				errno = EILSEQ;
				return(EOF);
			}
	}
	/*
	 * This loop shifts previous bytes left, sticking the
	 * new byte into the low order byte of temp. If unexpected
	 * EOF or byte detects in this loop, return EOF
	 * for the whole thing.
	 */
	while (size--) {
		if ((byte = getc(iop)) == EOF || !graphic(byte)) {
			ungetc(byte, iop);
			errno = EILSEQ;
			return(EOF);
		}
	/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989	*/
	/*	temp = (temp << 8) | byte;	*/
		if( pcw > sizeof(unsigned short) )
{
			temp = ((temp << 7) | (byte & 0177));
}
		else
			temp = ((temp << 8) | (byte & 0177));
	/*	end for MNLS3.2	*/
	}
	/*
	 * Mask the value so that it fits into the proper character
	 * set pattern for the high order bits of each byte:
	 *     CS0-0000   CS1=0011   CS2=0001   CS3=0010
	 */
/*	start for MNLS3.2 by Unix Pacific on Jan.18.1989	*/
/*	return((temp | EUCMASK) & _pcmask[cset]);	*/
	return(temp | mask);
/*	end for MNLS3.2	*/
}
