Back to index

tetex-bin  3.0
lib_getstr.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2000,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /*
00035 **     lib_getstr.c
00036 **
00037 **     The routine wgetstr().
00038 **
00039 */
00040 
00041 #include <curses.priv.h>
00042 #include <term.h>
00043 
00044 MODULE_ID("$Id: lib_getstr.c,v 1.24 2002/09/01 17:31:12 tom Exp $")
00045 
00046 /*
00047  * This wipes out the last character, no matter whether it was a tab, control
00048  * or other character, and handles reverse wraparound.
00049  */
00050 static char *
00051 WipeOut(WINDOW *win, int y, int x, char *first, char *last, bool echoed)
00052 {
00053     if (last > first) {
00054        *--last = '\0';
00055        if (echoed) {
00056            int y1 = win->_cury;
00057            int x1 = win->_curx;
00058 
00059            wmove(win, y, x);
00060            waddstr(win, first);
00061            getyx(win, y, x);
00062            while (win->_cury < y1
00063                  || (win->_cury == y1 && win->_curx < x1))
00064               waddch(win, (chtype) ' ');
00065 
00066            wmove(win, y, x);
00067        }
00068     }
00069     return last;
00070 }
00071 
00072 NCURSES_EXPORT(int)
00073 wgetnstr_events(WINDOW *win,
00074               char *str,
00075               int maxlen,
00076               EVENTLIST_1st(_nc_eventlist * evl))
00077 {
00078     TTY buf;
00079     bool oldnl, oldecho, oldraw, oldcbreak;
00080     char erasec;
00081     char killc;
00082     char *oldstr;
00083     int ch;
00084     int y, x;
00085 
00086     T((T_CALLED("wgetnstr(%p,%p, %d)"), win, str, maxlen));
00087 
00088     if (!win)
00089        returnCode(ERR);
00090 
00091     _nc_get_tty_mode(&buf);
00092 
00093     oldnl = SP->_nl;
00094     oldecho = SP->_echo;
00095     oldraw = SP->_raw;
00096     oldcbreak = SP->_cbreak;
00097     nl();
00098     noecho();
00099     noraw();
00100     cbreak();
00101 
00102     erasec = erasechar();
00103     killc = killchar();
00104 
00105     oldstr = str;
00106     getyx(win, y, x);
00107 
00108     if (is_wintouched(win) || (win->_flags & _HASMOVED))
00109        wrefresh(win);
00110 
00111     while ((ch = wgetch_events(win, evl)) != ERR) {
00112        /*
00113         * Some terminals (the Wyse-50 is the most common) generate
00114         * a \n from the down-arrow key.  With this logic, it's the
00115         * user's choice whether to set kcud=\n for wgetch();
00116         * terminating *getstr() with \n should work either way.
00117         */
00118        if (ch == '\n'
00119            || ch == '\r'
00120            || ch == KEY_DOWN
00121            || ch == KEY_ENTER) {
00122            if (oldecho == TRUE
00123               && win->_cury == win->_maxy
00124               && win->_scroll)
00125               wechochar(win, (chtype) '\n');
00126            break;
00127        }
00128 #ifdef KEY_EVENT
00129        if (ch == KEY_EVENT)
00130            break;
00131 #endif
00132        if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
00133            if (str > oldstr) {
00134               str = WipeOut(win, y, x, oldstr, str, oldecho);
00135            }
00136        } else if (ch == killc) {
00137            while (str > oldstr) {
00138               str = WipeOut(win, y, x, oldstr, str, oldecho);
00139            }
00140        } else if (ch >= KEY_MIN
00141                  || (maxlen >= 0 && str - oldstr >= maxlen)) {
00142            beep();
00143        } else {
00144            *str++ = ch;
00145            if (oldecho == TRUE) {
00146               int oldy = win->_cury;
00147               if (waddch(win, (chtype) ch) == ERR) {
00148                   /*
00149                    * We can't really use the lower-right
00150                    * corner for input, since it'll mess
00151                    * up bookkeeping for erases.
00152                    */
00153                   win->_flags &= ~_WRAPPED;
00154                   waddch(win, (chtype) ' ');
00155                   str = WipeOut(win, y, x, oldstr, str, oldecho);
00156                   continue;
00157               } else if (win->_flags & _WRAPPED) {
00158                   /*
00159                    * If the last waddch forced a wrap &
00160                    * scroll, adjust our reference point
00161                    * for erasures.
00162                    */
00163                   if (win->_scroll
00164                      && oldy == win->_maxy
00165                      && win->_cury == win->_maxy) {
00166                      if (--y <= 0) {
00167                          y = 0;
00168                      }
00169                   }
00170                   win->_flags &= ~_WRAPPED;
00171               }
00172               wrefresh(win);
00173            }
00174        }
00175     }
00176 
00177     win->_curx = 0;
00178     win->_flags &= ~_WRAPPED;
00179     if (win->_cury < win->_maxy)
00180        win->_cury++;
00181     wrefresh(win);
00182 
00183     /* Restore with a single I/O call, to fix minor asymmetry between
00184      * raw/noraw, etc.
00185      */
00186     SP->_nl = oldnl;
00187     SP->_echo = oldecho;
00188     SP->_raw = oldraw;
00189     SP->_cbreak = oldcbreak;
00190 
00191     _nc_set_tty_mode(&buf);
00192 
00193     *str = '\0';
00194     if (ch == ERR)
00195        returnCode(ch);
00196 #ifdef KEY_EVENT
00197     if (ch == KEY_EVENT)
00198        returnCode(ch);
00199 #endif
00200 
00201     T(("wgetnstr returns %s", _nc_visbuf(oldstr)));
00202 
00203     returnCode(OK);
00204 }
00205 
00206 #ifdef NCURSES_WGETCH_EVENTS
00207 NCURSES_EXPORT(int)
00208 wgetnstr(WINDOW *win, char *str, int maxlen)
00209 {
00210     returnCode(wgetnstr_events(win,
00211                             str,
00212                             maxlen,
00213                             EVENTLIST_1st((_nc_eventlist *) 0)));
00214 }
00215 #endif