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 /* 23 * Copyright (c) 1996-1998 by Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 27 /* LINTLIBRARY */ 28 29 /* 30 * wio_get.c 31 * 32 * Wide I/O Library 33 * 34 * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved. 35 * 36 */ 37 38 #if M_RCSID 39 #ifndef lint 40 static char rcsID[] = 41 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/" 42 "libxcurses/src/libc/wide/rcs/wio_get.c 1.3 1998/05/22 17:56:47 " 43 "cbates Exp $"; 44 #endif 45 #endif 46 47 #include <mks.h> 48 #include <errno.h> 49 #include <m_wio.h> 50 51 /* 52 * Return a wide character or WEOF for EOF or error. 53 * 54 * The function referenced by "get" is passed the pointer "object" 55 * and returns an input byte or EOF if no further data available. 56 * 57 * This mechanism is used to do conversions of byte strings or 58 * streams into wide characters without loss of information in the 59 * case of a bad multibyte character conversion. The bad multibyte 60 * sequence is passed through as individual bytes. 61 */ 62 wint_t m_wio_get(t_wide_io * wio)63m_wio_get(t_wide_io *wio) 64 { 65 int ch; 66 wchar_t wc; 67 68 if (wio == NULL || wio->get == (int (*)(void *)) NULL) { 69 errno = EINVAL; 70 return (-1); 71 } 72 73 /* Do still have bytes available? */ 74 if (wio->_next < wio->_size) { 75 return ((wint_t)wio->_mb[wio->_next++]); 76 } 77 78 /* Read in enough bytes to convert a multibyte character. */ 79 wio->_size = 0; 80 for (wio->_next = 0; wio->_next < (int)MB_CUR_MAX; ) { 81 if ((ch = (*wio->get)(wio->object)) == EOF) { 82 break; 83 } 84 85 wio->_mb[wio->_next] = (unsigned char)ch; 86 87 wio->_size = mbtowc(&wc, (char *)wio->_mb, wio->_next + 1); 88 89 ++wio->_next; 90 91 if (0 <= wio->_size) { 92 /* Remember the number of bytes converted. */ 93 wio->_size = wio->_next; 94 95 return ((wint_t) wc); 96 } 97 } 98 99 /* 100 * If we fill the multibyte character buffer or receive an 101 * EOF without recognising a multibyte character, then we 102 * will return individual bytes from the buffer. The buffer 103 * is restored to its state before the bogus byte sequence 104 * was read. 105 */ 106 wio->_size = wio->_next; 107 wio->_next = 0; 108 109 return (0 < wio->_size ? (wint_t) wio->_mb[wio->_next++] : WEOF); 110 } 111