17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*  Copyright (c) 1988 AT&T */
237c478bdstevel@tonic-gate/*    All Rights Reserved   */
247c478bdstevel@tonic-gate
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*
277c478bdstevel@tonic-gate *      Copyright (c) 1997, by Sun Microsystems, Inc.
287c478bdstevel@tonic-gate *      All rights reserved.
297c478bdstevel@tonic-gate */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include	<stdlib.h>
347c478bdstevel@tonic-gate#include	<sys/types.h>
357c478bdstevel@tonic-gate#include	"curses_inc.h"
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate/*
397c478bdstevel@tonic-gate *	Translate process code to byte-equivalent
407c478bdstevel@tonic-gate *	Return the length of the byte-equivalent string
417c478bdstevel@tonic-gate */
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate/*
447c478bdstevel@tonic-gate *	use _curs_wctomb() instead of _code2byte(code, bytes)
457c478bdstevel@tonic-gate */
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate/*
497c478bdstevel@tonic-gate *	Translate a set of byte to a single process code
507c478bdstevel@tonic-gate */
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate/*
537c478bdstevel@tonic-gate *	use _curs_mbtowc() instead of wchar_t _byte2code(bytes)
547c478bdstevel@tonic-gate */
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate/*
587c478bdstevel@tonic-gate *	Translate a string of wchar_t to a byte string.
597c478bdstevel@tonic-gate *	code: the input code string
607c478bdstevel@tonic-gate *	byte: if not NULL, space to store the output string
617c478bdstevel@tonic-gate *	n: maximum number of codes to be translated.
627c478bdstevel@tonic-gate */
637c478bdstevel@tonic-gatechar
647c478bdstevel@tonic-gate*_strcode2byte(wchar_t *code, char *byte, int n)
657c478bdstevel@tonic-gate{
667c478bdstevel@tonic-gate	char		*bufp;
677c478bdstevel@tonic-gate	wchar_t		*endcode;
687c478bdstevel@tonic-gate	static char	*buf;
697c478bdstevel@tonic-gate	static int	bufsize;
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate	/* compute the length of the code string */
727c478bdstevel@tonic-gate	if (n < 0)
737c478bdstevel@tonic-gate		for (n = 0; code[n] != 0; ++n)
747c478bdstevel@tonic-gate			;
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate	/* get space to store the translated string */
777c478bdstevel@tonic-gate	if (!byte && (n*CSMAX+1) > bufsize) {
787c478bdstevel@tonic-gate		if (buf)
797c478bdstevel@tonic-gate			free(buf);
807c478bdstevel@tonic-gate		bufsize = n * CSMAX + 1;
817c478bdstevel@tonic-gate		if ((buf = malloc(bufsize * sizeof (char))) == NULL)
827c478bdstevel@tonic-gate			bufsize = 0;
837c478bdstevel@tonic-gate		}
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate	/* no space to do it */
867c478bdstevel@tonic-gate	if (!byte && !buf)
877c478bdstevel@tonic-gate		return (NULL);
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate	/* start the translation */
907c478bdstevel@tonic-gate	bufp = byte ? byte : buf;
917c478bdstevel@tonic-gate	endcode = code+n;
927c478bdstevel@tonic-gate	while (code < endcode && *code) {
937c478bdstevel@tonic-gate		bufp += _curs_wctomb(bufp, *code & TRIM);
947c478bdstevel@tonic-gate		++code;
957c478bdstevel@tonic-gate	}
967c478bdstevel@tonic-gate	*bufp = '\0';
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate	return (byte ? byte : buf);
997c478bdstevel@tonic-gate}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gate/*
1047c478bdstevel@tonic-gate *	Translate a byte-string to a wchar_t string.
1057c478bdstevel@tonic-gate */
1067c478bdstevel@tonic-gatewchar_t
1077c478bdstevel@tonic-gate*_strbyte2code(char *byte, wchar_t *code, int n)
1087c478bdstevel@tonic-gate{
1097c478bdstevel@tonic-gate	char		*endbyte;
1107c478bdstevel@tonic-gate	wchar_t		*bufp;
1117c478bdstevel@tonic-gate	static wchar_t	*buf;
1127c478bdstevel@tonic-gate	static int	bufsize;
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate	if (n < 0)
1157c478bdstevel@tonic-gate		for (n = 0; byte[n] != '\0'; ++n)
1167c478bdstevel@tonic-gate			;
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gate	if (!code && (n + 1) > bufsize) {
1197c478bdstevel@tonic-gate		if (buf)
1207c478bdstevel@tonic-gate			free((char *)buf);
1217c478bdstevel@tonic-gate		bufsize = n + 1;
1227c478bdstevel@tonic-gate		if ((buf = (wchar_t *)malloc(bufsize * sizeof (wchar_t))) ==
1237c478bdstevel@tonic-gate		    NULL)
1247c478bdstevel@tonic-gate			bufsize = 0;
1257c478bdstevel@tonic-gate	}
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate	if (!code && !buf)
1287c478bdstevel@tonic-gate		return (NULL);
1297c478bdstevel@tonic-gate
1307c478bdstevel@tonic-gate	bufp = code ? code : buf;
1317c478bdstevel@tonic-gate	endbyte = byte + n;
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate	while (byte < endbyte && *byte) {
1347c478bdstevel@tonic-gate		int		type, width;
1357c478bdstevel@tonic-gate		wchar_t		wchar;
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate		type = TYPE(*byte & 0377);
1387c478bdstevel@tonic-gate		width = cswidth[type];
1397c478bdstevel@tonic-gate		if (type == 1 || type == 2)
1407c478bdstevel@tonic-gate			width++;
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate		if (byte + width <= endbyte) {
1437c478bdstevel@tonic-gate			(void) _curs_mbtowc(&wchar, byte, width);
1447c478bdstevel@tonic-gate			*bufp++ = wchar;
1457c478bdstevel@tonic-gate		}
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate		byte += width;
1487c478bdstevel@tonic-gate	}
1497c478bdstevel@tonic-gate	*bufp = 0;
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gate	return (code ? code : buf);
1527c478bdstevel@tonic-gate}
153