2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <widec.h>
2N/A#include <ctype.h>
2N/A#include <sys/types.h>
2N/A#include "curses_wchar.h"
2N/A
2N/Aint
2N/A_curs_mbtowc(wchar_t *wchar, const char *s, size_t n)
2N/A{
2N/A int length, c;
2N/A wchar_t intcode;
2N/A char *olds = (char *)s;
2N/A wchar_t mask;
2N/A
2N/A if (s == (char *)0)
2N/A return (0);
2N/A if (n == 0)
2N/A return (-1);
2N/A c = (unsigned char)*s++;
2N/A if (c < 0200) {
2N/A if (wchar)
2N/A *wchar = c;
2N/A return (c ? 1 : 0);
2N/A }
2N/A intcode = 0;
2N/A if (c == SS2) {
2N/A if ((length = eucw2) == 0)
2N/A goto lab1;
2N/A mask = P01;
2N/A goto lab2;
2N/A } else if (c == SS3) {
2N/A if ((length = eucw3) == 0)
2N/A goto lab1;
2N/A mask = P10;
2N/A goto lab2;
2N/A }
2N/Alab1:
2N/A if (iscntrl(c)) {
2N/A if (wchar)
2N/A *wchar = c;
2N/A return (1);
2N/A }
2N/A length = eucw1 - 1;
2N/A mask = P11;
2N/A intcode = c & 0177;
2N/Alab2:
2N/A if (length + 1 > n || length < 0)
2N/A return (-1);
2N/A while (length--) {
2N/A if ((c = (unsigned char)*s++) < 0200 || iscntrl(c))
2N/A return (-1);
2N/A intcode = (intcode << 7) | (c & 0x7F);
2N/A }
2N/A if (wchar)
2N/A *wchar = intcode | mask;
2N/A /*LINTED*/
2N/A return ((int)(s - olds));
2N/A}