1  | 
     | 
     | 
    /* $OpenBSD: lib_getstr.c,v 1.3 2010/01/12 23:22:05 nicm Exp $ */  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /****************************************************************************  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 1998-2006,2008 Free Software Foundation, Inc.              *  | 
    
    
    5  | 
     | 
     | 
     *                                                                          *  | 
    
    
    6  | 
     | 
     | 
     * Permission is hereby granted, free of charge, to any person obtaining a  *  | 
    
    
    7  | 
     | 
     | 
     * copy of this software and associated documentation files (the            *  | 
    
    
    8  | 
     | 
     | 
     * "Software"), to deal in the Software without restriction, including      *  | 
    
    
    9  | 
     | 
     | 
     * without limitation the rights to use, copy, modify, merge, publish,      *  | 
    
    
    10  | 
     | 
     | 
     * distribute, distribute with modifications, sublicense, and/or sell       *  | 
    
    
    11  | 
     | 
     | 
     * copies of the Software, and to permit persons to whom the Software is    *  | 
    
    
    12  | 
     | 
     | 
     * furnished to do so, subject to the following conditions:                 *  | 
    
    
    13  | 
     | 
     | 
     *                                                                          *  | 
    
    
    14  | 
     | 
     | 
     * The above copyright notice and this permission notice shall be included  *  | 
    
    
    15  | 
     | 
     | 
     * in all copies or substantial portions of the Software.                   *  | 
    
    
    16  | 
     | 
     | 
     *                                                                          *  | 
    
    
    17  | 
     | 
     | 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *  | 
    
    
    18  | 
     | 
     | 
     * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *  | 
    
    
    19  | 
     | 
     | 
     * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *  | 
    
    
    20  | 
     | 
     | 
     * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *  | 
    
    
    21  | 
     | 
     | 
     * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *  | 
    
    
    22  | 
     | 
     | 
     * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *  | 
    
    
    23  | 
     | 
     | 
     * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *  | 
    
    
    24  | 
     | 
     | 
     *                                                                          *  | 
    
    
    25  | 
     | 
     | 
     * Except as contained in this notice, the name(s) of the above copyright   *  | 
    
    
    26  | 
     | 
     | 
     * holders shall not be used in advertising or otherwise to promote the     *  | 
    
    
    27  | 
     | 
     | 
     * sale, use or other dealings in this Software without prior written       *  | 
    
    
    28  | 
     | 
     | 
     * authorization.                                                           *  | 
    
    
    29  | 
     | 
     | 
     ****************************************************************************/  | 
    
    
    30  | 
     | 
     | 
     | 
    
    
    31  | 
     | 
     | 
    /****************************************************************************  | 
    
    
    32  | 
     | 
     | 
     *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *  | 
    
    
    33  | 
     | 
     | 
     *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *  | 
    
    
    34  | 
     | 
     | 
     ****************************************************************************/  | 
    
    
    35  | 
     | 
     | 
     | 
    
    
    36  | 
     | 
     | 
    /*  | 
    
    
    37  | 
     | 
     | 
    **	lib_getstr.c  | 
    
    
    38  | 
     | 
     | 
    **  | 
    
    
    39  | 
     | 
     | 
    **	The routine wgetstr().  | 
    
    
    40  | 
     | 
     | 
    **  | 
    
    
    41  | 
     | 
     | 
    */  | 
    
    
    42  | 
     | 
     | 
     | 
    
    
    43  | 
     | 
     | 
    #include <curses.priv.h>  | 
    
    
    44  | 
     | 
     | 
    #include <term.h>  | 
    
    
    45  | 
     | 
     | 
     | 
    
    
    46  | 
     | 
     | 
    MODULE_ID("$Id: lib_getstr.c,v 1.3 2010/01/12 23:22:05 nicm Exp $") | 
    
    
    47  | 
     | 
     | 
     | 
    
    
    48  | 
     | 
     | 
    /*  | 
    
    
    49  | 
     | 
     | 
     * This wipes out the last character, no matter whether it was a tab, control  | 
    
    
    50  | 
     | 
     | 
     * or other character, and handles reverse wraparound.  | 
    
    
    51  | 
     | 
     | 
     */  | 
    
    
    52  | 
     | 
     | 
    static char *  | 
    
    
    53  | 
     | 
     | 
    WipeOut(WINDOW *win, int y, int x, char *first, char *last, bool echoed)  | 
    
    
    54  | 
     | 
     | 
    { | 
    
    
    55  | 
     | 
     | 
        if (last > first) { | 
    
    
    56  | 
     | 
     | 
    	*--last = '\0';  | 
    
    
    57  | 
     | 
     | 
    	if (echoed) { | 
    
    
    58  | 
     | 
     | 
    	    int y1 = win->_cury;  | 
    
    
    59  | 
     | 
     | 
    	    int x1 = win->_curx;  | 
    
    
    60  | 
     | 
     | 
     | 
    
    
    61  | 
     | 
     | 
    	    wmove(win, y, x);  | 
    
    
    62  | 
     | 
     | 
    	    waddstr(win, first);  | 
    
    
    63  | 
     | 
     | 
    	    getyx(win, y, x);  | 
    
    
    64  | 
     | 
     | 
    	    while (win->_cury < y1  | 
    
    
    65  | 
     | 
     | 
    		   || (win->_cury == y1 && win->_curx < x1))  | 
    
    
    66  | 
     | 
     | 
    		waddch(win, (chtype) ' ');  | 
    
    
    67  | 
     | 
     | 
     | 
    
    
    68  | 
     | 
     | 
    	    wmove(win, y, x);  | 
    
    
    69  | 
     | 
     | 
    	}  | 
    
    
    70  | 
     | 
     | 
        }  | 
    
    
    71  | 
     | 
     | 
        return last;  | 
    
    
    72  | 
     | 
     | 
    }  | 
    
    
    73  | 
     | 
     | 
     | 
    
    
    74  | 
     | 
     | 
    NCURSES_EXPORT(int)  | 
    
    
    75  | 
     | 
     | 
    wgetnstr_events(WINDOW *win,  | 
    
    
    76  | 
     | 
     | 
    		char *str,  | 
    
    
    77  | 
     | 
     | 
    		int maxlen,  | 
    
    
    78  | 
     | 
     | 
    		EVENTLIST_1st(_nc_eventlist * evl))  | 
    
    
    79  | 
     | 
     | 
    { | 
    
    
    80  | 
     | 
     | 
        SCREEN *sp = _nc_screen_of(win);  | 
    
    
    81  | 
     | 
     | 
        TTY buf;  | 
    
    
    82  | 
     | 
     | 
        bool oldnl, oldecho, oldraw, oldcbreak;  | 
    
    
    83  | 
     | 
     | 
        char erasec;  | 
    
    
    84  | 
     | 
     | 
        char killc;  | 
    
    
    85  | 
     | 
     | 
        char *oldstr;  | 
    
    
    86  | 
     | 
     | 
        int ch;  | 
    
    
    87  | 
     | 
     | 
        int y, x;  | 
    
    
    88  | 
     | 
     | 
     | 
    
    
    89  | 
     | 
     | 
        T((T_CALLED("wgetnstr(%p,%p, %d)"), win, str, maxlen)); | 
    
    
    90  | 
     | 
     | 
     | 
    
    
    91  | 
     | 
     | 
        if (!win)  | 
    
    
    92  | 
     | 
     | 
    	returnCode(ERR);  | 
    
    
    93  | 
     | 
     | 
     | 
    
    
    94  | 
     | 
     | 
        _nc_get_tty_mode(&buf);  | 
    
    
    95  | 
     | 
     | 
     | 
    
    
    96  | 
     | 
     | 
        oldnl = sp->_nl;  | 
    
    
    97  | 
     | 
     | 
        oldecho = sp->_echo;  | 
    
    
    98  | 
     | 
     | 
        oldraw = sp->_raw;  | 
    
    
    99  | 
     | 
     | 
        oldcbreak = sp->_cbreak;  | 
    
    
    100  | 
     | 
     | 
        nl();  | 
    
    
    101  | 
     | 
     | 
        noecho();  | 
    
    
    102  | 
     | 
     | 
        noraw();  | 
    
    
    103  | 
     | 
     | 
        cbreak();  | 
    
    
    104  | 
     | 
     | 
     | 
    
    
    105  | 
     | 
     | 
        erasec = erasechar();  | 
    
    
    106  | 
     | 
     | 
        killc = killchar();  | 
    
    
    107  | 
     | 
     | 
     | 
    
    
    108  | 
     | 
     | 
        oldstr = str;  | 
    
    
    109  | 
     | 
     | 
        getyx(win, y, x);  | 
    
    
    110  | 
     | 
     | 
     | 
    
    
    111  | 
     | 
     | 
        if (is_wintouched(win) || (win->_flags & _HASMOVED))  | 
    
    
    112  | 
     | 
     | 
    	wrefresh(win);  | 
    
    
    113  | 
     | 
     | 
     | 
    
    
    114  | 
     | 
     | 
        while ((ch = wgetch_events(win, evl)) != ERR) { | 
    
    
    115  | 
     | 
     | 
    	/*  | 
    
    
    116  | 
     | 
     | 
    	 * Some terminals (the Wyse-50 is the most common) generate  | 
    
    
    117  | 
     | 
     | 
    	 * a \n from the down-arrow key.  With this logic, it's the  | 
    
    
    118  | 
     | 
     | 
    	 * user's choice whether to set kcud=\n for wgetch();  | 
    
    
    119  | 
     | 
     | 
    	 * terminating *getstr() with \n should work either way.  | 
    
    
    120  | 
     | 
     | 
    	 */  | 
    
    
    121  | 
     | 
     | 
    	if (ch == '\n'  | 
    
    
    122  | 
     | 
     | 
    	    || ch == '\r'  | 
    
    
    123  | 
     | 
     | 
    	    || ch == KEY_DOWN  | 
    
    
    124  | 
     | 
     | 
    	    || ch == KEY_ENTER) { | 
    
    
    125  | 
     | 
     | 
    	    if (oldecho == TRUE  | 
    
    
    126  | 
     | 
     | 
    		&& win->_cury == win->_maxy  | 
    
    
    127  | 
     | 
     | 
    		&& win->_scroll)  | 
    
    
    128  | 
     | 
     | 
    		wechochar(win, (chtype) '\n');  | 
    
    
    129  | 
     | 
     | 
    	    break;  | 
    
    
    130  | 
     | 
     | 
    	}  | 
    
    
    131  | 
     | 
     | 
    #ifdef KEY_EVENT  | 
    
    
    132  | 
     | 
     | 
    	if (ch == KEY_EVENT)  | 
    
    
    133  | 
     | 
     | 
    	    break;  | 
    
    
    134  | 
     | 
     | 
    #endif  | 
    
    
    135  | 
     | 
     | 
    #ifdef KEY_RESIZE  | 
    
    
    136  | 
     | 
     | 
    	if (ch == KEY_RESIZE)  | 
    
    
    137  | 
     | 
     | 
    	    break;  | 
    
    
    138  | 
     | 
     | 
    #endif  | 
    
    
    139  | 
     | 
     | 
    	if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) { | 
    
    
    140  | 
     | 
     | 
    	    if (str > oldstr) { | 
    
    
    141  | 
     | 
     | 
    		str = WipeOut(win, y, x, oldstr, str, oldecho);  | 
    
    
    142  | 
     | 
     | 
    	    }  | 
    
    
    143  | 
     | 
     | 
    	} else if (ch == killc) { | 
    
    
    144  | 
     | 
     | 
    	    while (str > oldstr) { | 
    
    
    145  | 
     | 
     | 
    		str = WipeOut(win, y, x, oldstr, str, oldecho);  | 
    
    
    146  | 
     | 
     | 
    	    }  | 
    
    
    147  | 
     | 
     | 
    	} else if (ch >= KEY_MIN  | 
    
    
    148  | 
     | 
     | 
    		   || (maxlen >= 0 && str - oldstr >= maxlen)) { | 
    
    
    149  | 
     | 
     | 
    	    beep();  | 
    
    
    150  | 
     | 
     | 
    	} else { | 
    
    
    151  | 
     | 
     | 
    	    *str++ = (char) ch;  | 
    
    
    152  | 
     | 
     | 
    	    if (oldecho == TRUE) { | 
    
    
    153  | 
     | 
     | 
    		int oldy = win->_cury;  | 
    
    
    154  | 
     | 
     | 
    		if (waddch(win, (chtype) ch) == ERR) { | 
    
    
    155  | 
     | 
     | 
    		    /*  | 
    
    
    156  | 
     | 
     | 
    		     * We can't really use the lower-right  | 
    
    
    157  | 
     | 
     | 
    		     * corner for input, since it'll mess  | 
    
    
    158  | 
     | 
     | 
    		     * up bookkeeping for erases.  | 
    
    
    159  | 
     | 
     | 
    		     */  | 
    
    
    160  | 
     | 
     | 
    		    win->_flags &= ~_WRAPPED;  | 
    
    
    161  | 
     | 
     | 
    		    waddch(win, (chtype) ' ');  | 
    
    
    162  | 
     | 
     | 
    		    str = WipeOut(win, y, x, oldstr, str, oldecho);  | 
    
    
    163  | 
     | 
     | 
    		    continue;  | 
    
    
    164  | 
     | 
     | 
    		} else if (win->_flags & _WRAPPED) { | 
    
    
    165  | 
     | 
     | 
    		    /*  | 
    
    
    166  | 
     | 
     | 
    		     * If the last waddch forced a wrap &  | 
    
    
    167  | 
     | 
     | 
    		     * scroll, adjust our reference point  | 
    
    
    168  | 
     | 
     | 
    		     * for erasures.  | 
    
    
    169  | 
     | 
     | 
    		     */  | 
    
    
    170  | 
     | 
     | 
    		    if (win->_scroll  | 
    
    
    171  | 
     | 
     | 
    			&& oldy == win->_maxy  | 
    
    
    172  | 
     | 
     | 
    			&& win->_cury == win->_maxy) { | 
    
    
    173  | 
     | 
     | 
    			if (--y <= 0) { | 
    
    
    174  | 
     | 
     | 
    			    y = 0;  | 
    
    
    175  | 
     | 
     | 
    			}  | 
    
    
    176  | 
     | 
     | 
    		    }  | 
    
    
    177  | 
     | 
     | 
    		    win->_flags &= ~_WRAPPED;  | 
    
    
    178  | 
     | 
     | 
    		}  | 
    
    
    179  | 
     | 
     | 
    		wrefresh(win);  | 
    
    
    180  | 
     | 
     | 
    	    }  | 
    
    
    181  | 
     | 
     | 
    	}  | 
    
    
    182  | 
     | 
     | 
        }  | 
    
    
    183  | 
     | 
     | 
     | 
    
    
    184  | 
     | 
     | 
        win->_curx = 0;  | 
    
    
    185  | 
     | 
     | 
        win->_flags &= ~_WRAPPED;  | 
    
    
    186  | 
     | 
     | 
        if (win->_cury < win->_maxy)  | 
    
    
    187  | 
     | 
     | 
    	win->_cury++;  | 
    
    
    188  | 
     | 
     | 
        wrefresh(win);  | 
    
    
    189  | 
     | 
     | 
     | 
    
    
    190  | 
     | 
     | 
        /* Restore with a single I/O call, to fix minor asymmetry between  | 
    
    
    191  | 
     | 
     | 
         * raw/noraw, etc.  | 
    
    
    192  | 
     | 
     | 
         */  | 
    
    
    193  | 
     | 
     | 
        sp->_nl = oldnl;  | 
    
    
    194  | 
     | 
     | 
        sp->_echo = oldecho;  | 
    
    
    195  | 
     | 
     | 
        sp->_raw = oldraw;  | 
    
    
    196  | 
     | 
     | 
        sp->_cbreak = oldcbreak;  | 
    
    
    197  | 
     | 
     | 
     | 
    
    
    198  | 
     | 
     | 
        _nc_set_tty_mode(&buf);  | 
    
    
    199  | 
     | 
     | 
     | 
    
    
    200  | 
     | 
     | 
        *str = '\0';  | 
    
    
    201  | 
     | 
     | 
        if (ch == ERR)  | 
    
    
    202  | 
     | 
     | 
    	returnCode(ch);  | 
    
    
    203  | 
     | 
     | 
     | 
    
    
    204  | 
     | 
     | 
        T(("wgetnstr returns %s", _nc_visbuf(oldstr))); | 
    
    
    205  | 
     | 
     | 
     | 
    
    
    206  | 
     | 
     | 
    #ifdef KEY_EVENT  | 
    
    
    207  | 
     | 
     | 
        if (ch == KEY_EVENT)  | 
    
    
    208  | 
     | 
     | 
    	returnCode(ch);  | 
    
    
    209  | 
     | 
     | 
    #endif  | 
    
    
    210  | 
     | 
     | 
    #ifdef KEY_RESIZE  | 
    
    
    211  | 
     | 
     | 
        if (ch == KEY_RESIZE)  | 
    
    
    212  | 
     | 
     | 
    	returnCode(ch);  | 
    
    
    213  | 
     | 
     | 
    #endif  | 
    
    
    214  | 
     | 
     | 
     | 
    
    
    215  | 
     | 
     | 
        returnCode(OK);  | 
    
    
    216  | 
     | 
     | 
    }  | 
    
    
    217  | 
     | 
     | 
     | 
    
    
    218  | 
     | 
     | 
    #ifdef NCURSES_WGETCH_EVENTS  | 
    
    
    219  | 
     | 
     | 
    NCURSES_EXPORT(int)  | 
    
    
    220  | 
     | 
     | 
    wgetnstr(WINDOW *win, char *str, int maxlen)  | 
    
    
    221  | 
     | 
     | 
    { | 
    
    
    222  | 
     | 
     | 
        returnCode(wgetnstr_events(win,  | 
    
    
    223  | 
     | 
     | 
    			       str,  | 
    
    
    224  | 
     | 
     | 
    			       maxlen,  | 
    
    
    225  | 
     | 
     | 
    			       EVENTLIST_1st((_nc_eventlist *) 0)));  | 
    
    
    226  | 
     | 
     | 
    }  | 
    
    
    227  | 
     | 
     | 
    #endif  |