xref: /illumos-gate/usr/src/lib/libcurses/screen/mbgetwidth.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*  Copyright (c) 1988 AT&T */
23 /*    All Rights Reserved   */
24 
25 
26 /*
27  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28  *      All rights reserved.
29  */
30 
31 /*LINTLIBRARY*/
32 
33 #include	"curses_inc.h"
34 #include	<sys/types.h>
35 #include	<ctype.h>
36 
37 #define	CSWIDTH	514
38 
39 short		cswidth[4] = {-1, 1, 1, 1};	/* character length */
40 short		_curs_scrwidth[4] = {1, 1, 1, 1};	/* screen width */
41 
42 /*
43  * This function is called only once in a program.
44  * Before cgetwidth() is called, setlocale() must be called.
45  */
46 
47 void
48 mbgetwidth(void)
49 {
50 	unsigned char *cp = &__ctype[CSWIDTH];
51 
52 	cswidth[0] = cp[0];
53 	cswidth[1] = cp[1];
54 	cswidth[2] = cp[2];
55 	_curs_scrwidth[0] = cp[3];
56 	_curs_scrwidth[1] = cp[4];
57 	_curs_scrwidth[2] = cp[5];
58 
59 }
60 
61 int
62 mbeucw(int c)
63 {
64 	c &= 0xFF;
65 
66 	if (c & 0x80) {
67 		if (c == SS2) {
68 			return (cswidth[1]);
69 		} else if (c == SS3) {
70 			return (cswidth[2]);
71 		}
72 		return (cswidth[0]);
73 	}
74 	return (1);
75 }
76 
77 int
78 mbscrw(int c)
79 {
80 	c &= 0xFF;
81 
82 	if (c & 0x80) {
83 		if (c == SS2) {
84 			return (_curs_scrwidth[1]);
85 		} else if (c == SS3) {
86 			return (_curs_scrwidth[2]);
87 		}
88 		return (_curs_scrwidth[0]);
89 	}
90 	return (1);
91 }
92 
93 int
94 wcscrw(wchar_t wc)
95 {
96 	int	rv;
97 
98 	switch (wc & EUCMASK) {
99 	case	P11:	/* Code set 1 */
100 		rv = _curs_scrwidth[0];
101 		break;
102 	case	P01:	/* Code set 2 */
103 		rv = _curs_scrwidth[1];
104 		break;
105 	case	P10:	/* Code set 3 */
106 		rv = _curs_scrwidth[2];
107 		break;
108 	default	:	/* Code set 0 */
109 		rv = 1;
110 		break;
111 	}
112 
113 	return (rv);
114 }
115