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, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * 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/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A
2N/A/*
2N/A * Copyright (c) 1997, by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdlib.h>
2N/A#include <sys/types.h>
2N/A#include "curses_inc.h"
2N/A
2N/A
2N/A/*
2N/A * Translate process code to byte-equivalent
2N/A * Return the length of the byte-equivalent string
2N/A */
2N/A
2N/A/*
2N/A * use _curs_wctomb() instead of _code2byte(code, bytes)
2N/A */
2N/A
2N/A
2N/A/*
2N/A * Translate a set of byte to a single process code
2N/A */
2N/A
2N/A/*
2N/A * use _curs_mbtowc() instead of wchar_t _byte2code(bytes)
2N/A */
2N/A
2N/A
2N/A/*
2N/A * Translate a string of wchar_t to a byte string.
2N/A * code: the input code string
2N/A * byte: if not NULL, space to store the output string
2N/A * n: maximum number of codes to be translated.
2N/A */
2N/Achar
2N/A*_strcode2byte(wchar_t *code, char *byte, int n)
2N/A{
2N/A char *bufp;
2N/A wchar_t *endcode;
2N/A static char *buf;
2N/A static int bufsize;
2N/A
2N/A /* compute the length of the code string */
2N/A if (n < 0)
2N/A for (n = 0; code[n] != 0; ++n)
2N/A ;
2N/A
2N/A /* get space to store the translated string */
2N/A if (!byte && (n*CSMAX+1) > bufsize) {
2N/A if (buf)
2N/A free(buf);
2N/A bufsize = n * CSMAX + 1;
2N/A if ((buf = malloc(bufsize * sizeof (char))) == NULL)
2N/A bufsize = 0;
2N/A }
2N/A
2N/A /* no space to do it */
2N/A if (!byte && !buf)
2N/A return (NULL);
2N/A
2N/A /* start the translation */
2N/A bufp = byte ? byte : buf;
2N/A endcode = code+n;
2N/A while (code < endcode && *code) {
2N/A bufp += _curs_wctomb(bufp, *code & TRIM);
2N/A ++code;
2N/A }
2N/A *bufp = '\0';
2N/A
2N/A return (byte ? byte : buf);
2N/A}
2N/A
2N/A
2N/A
2N/A/*
2N/A * Translate a byte-string to a wchar_t string.
2N/A */
2N/Awchar_t
2N/A*_strbyte2code(char *byte, wchar_t *code, int n)
2N/A{
2N/A char *endbyte;
2N/A wchar_t *bufp;
2N/A static wchar_t *buf;
2N/A static int bufsize;
2N/A
2N/A if (n < 0)
2N/A for (n = 0; byte[n] != '\0'; ++n)
2N/A ;
2N/A
2N/A if (!code && (n + 1) > bufsize) {
2N/A if (buf)
2N/A free((char *)buf);
2N/A bufsize = n + 1;
2N/A if ((buf = (wchar_t *)malloc(bufsize * sizeof (wchar_t))) ==
2N/A NULL)
2N/A bufsize = 0;
2N/A }
2N/A
2N/A if (!code && !buf)
2N/A return (NULL);
2N/A
2N/A bufp = code ? code : buf;
2N/A endbyte = byte + n;
2N/A
2N/A while (byte < endbyte && *byte) {
2N/A int type, width;
2N/A wchar_t wchar;
2N/A
2N/A type = TYPE(*byte & 0377);
2N/A width = cswidth[type];
2N/A if (type == 1 || type == 2)
2N/A width++;
2N/A
2N/A if (byte + width <= endbyte) {
2N/A (void) _curs_mbtowc(&wchar, byte, width);
2N/A *bufp++ = wchar;
2N/A }
2N/A
2N/A byte += width;
2N/A }
2N/A *bufp = 0;
2N/A
2N/A return (code ? code : buf);
2N/A}