xref: /titanic_50/usr/src/lib/libcurses/screen/mbtowc.c (revision 67318e4a54c292d543e6b077199ce492b3d3a049)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*  Copyright (c) 1988 AT&T */
28 /*    All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <widec.h>
33 #include <ctype.h>
34 #include <sys/types.h>
35 #include "curses_wchar.h"
36 
37 int
38 _curs_mbtowc(wchar_t *wchar, const char *s, size_t n)
39 {
40 	int length, c;
41 	wchar_t intcode;
42 	char *olds = (char *)s;
43 	wchar_t mask;
44 
45 	if (s == (char *)0)
46 		return (0);
47 	if (n == 0)
48 		return (-1);
49 	c = (unsigned char)*s++;
50 	if (c < 0200) {
51 		if (wchar)
52 			*wchar = c;
53 		return (c ? 1 : 0);
54 	}
55 	intcode = 0;
56 	if (c == SS2) {
57 		if ((length = eucw2) == 0)
58 			goto lab1;
59 		mask = P01;
60 		goto lab2;
61 	} else if (c == SS3) {
62 		if ((length = eucw3) == 0)
63 			goto lab1;
64 		mask = P10;
65 		goto lab2;
66 	}
67 lab1:
68 	if (iscntrl(c)) {
69 		if (wchar)
70 			*wchar = c;
71 		return (1);
72 	}
73 	length = eucw1 - 1;
74 	mask = P11;
75 	intcode = c & 0177;
76 lab2:
77 	if (length + 1 > n || length < 0)
78 		return (-1);
79 	while (length--) {
80 		if ((c = (unsigned char)*s++) < 0200 || iscntrl(c))
81 			return (-1);
82 		intcode = (intcode << 7) | (c & 0x7F);
83 	}
84 	if (wchar)
85 		*wchar = intcode | mask;
86 	/*LINTED*/
87 	return ((int)(s - olds));
88 }
89