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/*
2N/A * Copyright (c) 1995, 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/*
2N/A * wget_wch.c
2N/A *
2N/A * XCurses Library
2N/A *
2N/A * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
2N/A *
2N/A */
2N/A
2N/A#ifdef M_RCSID
2N/A#ifndef lint
2N/Astatic char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/wget_wch.c 1.2 1995/06/08 19:00:47 ant Exp $";
2N/A#endif
2N/A#endif
2N/A
2N/A#include <private.h>
2N/A#include <m_wio.h>
2N/A#include <stdlib.h>
2N/A
2N/A/*
2N/A * Push a wide character back onto the input queue.
2N/A *
2N/A * XPG4 is silent as to whether wget_wch() and wgetch()
2N/A * can both be used in an applicaton. Assume they can,
2N/A * in which case we have to push the multibyte equivalent
2N/A * back onto the input queue.
2N/A */
2N/Aint
2N/Aunget_wch(const wchar_t wc)
2N/A{
2N/A int i, len;
2N/A char mb[MB_LEN_MAX];
2N/A
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace("unget_wch(%ld)", (long) wc);
2N/A#endif
2N/A
2N/A if (!ISEMPTY() || (len = wctomb(mb, wc)) < 0)
2N/A return __m_return_code("unget_wch", ERR);
2N/A
2N/A for (i = 0; i < len; ++i)
2N/A PUSH(mb[i]);
2N/A
2N/A return __m_return_code("unget_wch", OK);
2N/A}
2N/A
2N/Aint
2N/Awget_wch(w, wcp)
2N/AWINDOW *w;
2N/Awint_t *wcp;
2N/A{
2N/A cchar_t cc;
2N/A int ch, oecho;
2N/A t_wide_io *wio;
2N/A
2N/A#ifdef M_CURSES_TRACE
2N/A __m_trace("wget_wch(%p, %p)", w, wcp);
2N/A#endif
2N/A
2N/A /* Disable echo temporarily, because we're using
2N/A * wgetch() to read in individual bytes and only
2N/A * want echo the resulting character, not the
2N/A * individual bytes composing the character.
2N/A */
2N/A oecho = __m_set_echo(0);
2N/A
2N/A /* Input function is wgetch(), which takes a WINDOW * for
2N/A * a parameter. The WINDOW * is used to set the "focus" by
2N/A * updatng and position the cursor in the relevant window and
2N/A * provide window specific settings. Input for all windows
2N/A * comes from one stream (__m_screen->_if), which is normally
2N/A * the terminal, but can be redirected.
2N/A */
2N/A wio = (t_wide_io *) __m_screen->_in;
2N/A wio->object = w;
2N/A
2N/A /* Get the first byte or KEY_ value. */
2N/A if ((ch = wgetch(w)) < __KEY_BASE) {
2N/A *wcp = ch;
2N/A (void) __m_set_echo(oecho);
2N/A return __m_return_code("wget_wch", KEY_CODE_YES);
2N/A }
2N/A
2N/A /* Push the byte back onto the input stream so that
2N/A * it can be processed by __m_wio_get().
2N/A */
2N/A (void) ungetc(ch, __m_screen->_if);
2N/A
2N/A /* Fetch a wide character from a narrow input stream.
2N/A * Invalid sequences are preserved as individual bytes.
2N/A * Handles insignificant and redundant shifts in the input
2N/A * stream.
2N/A */
2N/A *wcp = m_wio_get(wio);
2N/A
2N/A /* Restore echo. */
2N/A (void) __m_set_echo(oecho);
2N/A
2N/A /* Push any invalid multibyte sequence back onto the
2N/A * input stack, so that no data is lost, just in case
2N/A * the application mixes wide (wget_wch()) and narrow
2N/A * (wgetch()) input methods.
2N/A */
2N/A while (wio->_next < wio->_size)
2N/A PUSH(wio->_mb[--wio->_size]);
2N/A
2N/A /* Now echo wide character if necessary. */
2N/A if ((__m_screen->_flags & S_ECHO) && *wcp != WEOF) {
2N/A (void) __m_wc_cc(*wcp, &cc);
2N/A (void) wadd_wch(w, &cc);
2N/A (void) wrefresh(w);
2N/A }
2N/A
2N/A return __m_return_code("wget_wch", OK);
2N/A}