1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
23*0Sstevel@tonic-gate /* All Rights Reserved */
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate * Copyright (c) 1997, by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate * All rights reserved.
29*0Sstevel@tonic-gate */
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include <stdlib.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include "curses_inc.h"
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate * Translate process code to byte-equivalent
40*0Sstevel@tonic-gate * Return the length of the byte-equivalent string
41*0Sstevel@tonic-gate */
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate * use _curs_wctomb() instead of _code2byte(code, bytes)
45*0Sstevel@tonic-gate */
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate * Translate a set of byte to a single process code
50*0Sstevel@tonic-gate */
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate * use _curs_mbtowc() instead of wchar_t _byte2code(bytes)
54*0Sstevel@tonic-gate */
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate * Translate a string of wchar_t to a byte string.
59*0Sstevel@tonic-gate * code: the input code string
60*0Sstevel@tonic-gate * byte: if not NULL, space to store the output string
61*0Sstevel@tonic-gate * n: maximum number of codes to be translated.
62*0Sstevel@tonic-gate */
63*0Sstevel@tonic-gate char
_strcode2byte(wchar_t * code,char * byte,int n)64*0Sstevel@tonic-gate *_strcode2byte(wchar_t *code, char *byte, int n)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate char *bufp;
67*0Sstevel@tonic-gate wchar_t *endcode;
68*0Sstevel@tonic-gate static char *buf;
69*0Sstevel@tonic-gate static int bufsize;
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate /* compute the length of the code string */
72*0Sstevel@tonic-gate if (n < 0)
73*0Sstevel@tonic-gate for (n = 0; code[n] != 0; ++n)
74*0Sstevel@tonic-gate ;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate /* get space to store the translated string */
77*0Sstevel@tonic-gate if (!byte && (n*CSMAX+1) > bufsize) {
78*0Sstevel@tonic-gate if (buf)
79*0Sstevel@tonic-gate free(buf);
80*0Sstevel@tonic-gate bufsize = n * CSMAX + 1;
81*0Sstevel@tonic-gate if ((buf = malloc(bufsize * sizeof (char))) == NULL)
82*0Sstevel@tonic-gate bufsize = 0;
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate /* no space to do it */
86*0Sstevel@tonic-gate if (!byte && !buf)
87*0Sstevel@tonic-gate return (NULL);
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate /* start the translation */
90*0Sstevel@tonic-gate bufp = byte ? byte : buf;
91*0Sstevel@tonic-gate endcode = code+n;
92*0Sstevel@tonic-gate while (code < endcode && *code) {
93*0Sstevel@tonic-gate bufp += _curs_wctomb(bufp, *code & TRIM);
94*0Sstevel@tonic-gate ++code;
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate *bufp = '\0';
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate return (byte ? byte : buf);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate * Translate a byte-string to a wchar_t string.
105*0Sstevel@tonic-gate */
106*0Sstevel@tonic-gate wchar_t
_strbyte2code(char * byte,wchar_t * code,int n)107*0Sstevel@tonic-gate *_strbyte2code(char *byte, wchar_t *code, int n)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate char *endbyte;
110*0Sstevel@tonic-gate wchar_t *bufp;
111*0Sstevel@tonic-gate static wchar_t *buf;
112*0Sstevel@tonic-gate static int bufsize;
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate if (n < 0)
115*0Sstevel@tonic-gate for (n = 0; byte[n] != '\0'; ++n)
116*0Sstevel@tonic-gate ;
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate if (!code && (n + 1) > bufsize) {
119*0Sstevel@tonic-gate if (buf)
120*0Sstevel@tonic-gate free((char *)buf);
121*0Sstevel@tonic-gate bufsize = n + 1;
122*0Sstevel@tonic-gate if ((buf = (wchar_t *)malloc(bufsize * sizeof (wchar_t))) ==
123*0Sstevel@tonic-gate NULL)
124*0Sstevel@tonic-gate bufsize = 0;
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate if (!code && !buf)
128*0Sstevel@tonic-gate return (NULL);
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate bufp = code ? code : buf;
131*0Sstevel@tonic-gate endbyte = byte + n;
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate while (byte < endbyte && *byte) {
134*0Sstevel@tonic-gate int type, width;
135*0Sstevel@tonic-gate wchar_t wchar;
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate type = TYPE(*byte & 0377);
138*0Sstevel@tonic-gate width = cswidth[type];
139*0Sstevel@tonic-gate if (type == 1 || type == 2)
140*0Sstevel@tonic-gate width++;
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate if (byte + width <= endbyte) {
143*0Sstevel@tonic-gate (void) _curs_mbtowc(&wchar, byte, width);
144*0Sstevel@tonic-gate *bufp++ = wchar;
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate byte += width;
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate *bufp = 0;
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate return (code ? code : buf);
152*0Sstevel@tonic-gate }
153