/* $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, 1986, 1987, 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: mbftowc.c 1.3 90/05/03 $@(#)libc-port:locale/mbftowc.c	1.0"

#include <ctype.h>
#include <stdlib.h>
#define SS2     0x8e
#define SS3     0x8f
#define EUCMASK 0x30000000
#define P00     0x00000000      /* Code Set 0 */
#define P11     0x30000000      /* Code Set 1 */
#define P01     0x10000000      /* Code Set 2 */
#define P10     0x20000000      /* Code Set 3 */
#define multibyte       (_ctype[520]>1)
#define eucw1   _ctype[514]
#define eucw2   _ctype[515]
#define eucw3   _ctype[516]
/*
 *	The followings are for 2 bytes of multi-byte code definition.
 */
#define H_EUCMASK 0x8080	/* mask bit for 16 bits */
#define H_P00     0x0000	/* Code Set 0 for 2 bytes code */
#define H_P11     0x8080	/* Code Set 1 for 2 bytes code */
#define H_P01     0x0080	/* Code Set 2 for 2 bytes code */
#define H_P10     0x8000	/* Code Set 3 for 2 bytes code */


int mbftowc(s, wchar, f, peekc)
char	*s;
wchar_t *wchar;
int	(*f)();
int	*peekc;
{
        register int length;
        register wchar_t intcode;
        register c;
        char *olds = (char *)s;
        wchar_t mask;
	short	pcw;	/* process code width */
        
	if((c = (*f)()) < 0)
		return 0;
	*s++ = c;
        if(!multibyte || c < 0200) {
                if(wchar)
                        *wchar = c;
                return(c ? 1 : 0);
        }
	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 */
        intcode = 0;
        if (c == SS2) {
                if(!(length = eucw2)) 
                        goto lab1;
		if ( pcw > sizeof(unsigned short))
                	mask = P01;
		else
			mask = H_P01;
                goto lab2;
        } else if(c == SS3) {
                if(!(length = eucw3)) 
                        goto lab1;
		if ( pcw > sizeof(unsigned short))
                	mask = P10;
		else
			mask = H_P10;
                goto lab2;
        } 
lab1:					 /*MNLS defines space as cntrl*/
        if ((iscntrl(c & 0177)) || ((c & 0177) == 040)) { 
                if(wchar)
                        *wchar = c;
                return(1);
        }
        length = eucw1 - 1;
	if ( pcw > sizeof(unsigned short))
               	mask = P11;
	else
		mask = H_P11;
        intcode = c & 0177;
lab2:
        while(length--) {
		*s++ =  c = (*f)();		/*MNLS defines space as cntrl*/
                if((c < 0200) || (iscntrl(c & 0177)) || ((c & 0177) == 040)) {
			if(c >= 0)
				*peekc = c;
			--s;
			return(-(s - olds));
		}
		if ( pcw > sizeof(unsigned short))
	                intcode = (intcode << 7) | (c & 0177);
		else
                	intcode = (intcode << 8) | (c & 0177);
        }
        if(wchar)
                *wchar = intcode | mask;
        return((char *)s - olds);
}       
