Home | History | Annotate | Line # | Download | only in libcurses
getstr.c revision 1.23
      1 /*	$NetBSD: getstr.c,v 1.23 2013/10/01 11:41:14 blymn Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1981, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <assert.h>
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 #if 0
     36 static char sccsid[] = "@(#)getstr.c	8.2 (Berkeley) 5/4/94";
     37 #else
     38 __RCSID("$NetBSD: getstr.c,v 1.23 2013/10/01 11:41:14 blymn Exp $");
     39 #endif
     40 #endif				/* not lint */
     41 
     42 #include <ctype.h>
     43 #include "curses.h"
     44 #include "curses_private.h"
     45 
     46 #ifndef _CURSES_USE_MACROS
     47 
     48 /*
     49  * getnstr --
     50  *	Get a string (of maximum n) characters from stdscr starting at
     51  *	(cury, curx).
     52  */
     53 int
     54 getnstr(char *str, int n)
     55 {
     56 	return wgetnstr(stdscr, str, n);
     57 }
     58 
     59 /*
     60  * getstr --
     61  *	Get a string from stdscr starting at (cury, curx).
     62  */
     63 __warn_references(getstr,
     64     "warning: this program uses getstr(), which is unsafe.")
     65 int
     66 getstr(char *str)
     67 {
     68 	return wgetstr(stdscr, str);
     69 }
     70 
     71 /*
     72  * mvgetnstr --
     73  *      Get a string (of maximum n) characters from stdscr starting at (y, x).
     74  */
     75 int
     76 mvgetnstr(int y, int x, char *str, int n)
     77 {
     78 	return mvwgetnstr(stdscr, y, x, str, n);
     79 }
     80 
     81 /*
     82  * mvgetstr --
     83  *      Get a string from stdscr starting at (y, x).
     84  */
     85 __warn_references(mvgetstr,
     86     "warning: this program uses mvgetstr(), which is unsafe.")
     87 int
     88 mvgetstr(int y, int x, char *str)
     89 {
     90 	return mvwgetstr(stdscr, y, x, str);
     91 }
     92 
     93 /*
     94  * mvwgetnstr --
     95  *      Get a string (of maximum n) characters from the given window starting
     96  *	at (y, x).
     97  */
     98 int
     99 mvwgetnstr(WINDOW *win, int y, int x, char *str, int n)
    100 {
    101 	if (wmove(win, y, x) == ERR)
    102 		return ERR;
    103 
    104 	return wgetnstr(win, str, n);
    105 }
    106 
    107 /*
    108  * mvwgetstr --
    109  *      Get a string from the given window starting at (y, x).
    110  */
    111 __warn_references(mvgetstr,
    112     "warning: this program uses mvgetstr(), which is unsafe.")
    113 int
    114 mvwgetstr(WINDOW *win, int y, int x, char *str)
    115 {
    116 	if (wmove(win, y, x) == ERR)
    117 		return ERR;
    118 
    119 	return wgetstr(win, str);
    120 }
    121 
    122 #endif
    123 
    124 /*
    125  * wgetstr --
    126  *	Get a string starting at (cury, curx).
    127  */
    128 __warn_references(wgetstr,
    129     "warning: this program uses wgetstr(), which is unsafe.")
    130 int
    131 wgetstr(WINDOW *win, char *str)
    132 {
    133 	return __wgetnstr(win, str, -1);
    134 }
    135 
    136 /*
    137  * wgetnstr --
    138  *	Get a string starting at (cury, curx).
    139  *	Note that n <  2 means that we return ERR (SUSv2 specification).
    140  */
    141 int
    142 wgetnstr(WINDOW *win, char *str, int n)
    143 {
    144 	if (n < 1)
    145 		return (ERR);
    146 	if (n == 1) {
    147 		str[0] = '\0';
    148 		return (ERR);
    149 	}
    150 	return __wgetnstr(win, str, n);
    151 }
    152 
    153 /*
    154  * __wgetnstr --
    155  *	The actual implementation.
    156  *	Note that we include a trailing '\0' for safety, so str will contain
    157  *	at most n - 1 other characters.
    158  *	XXX: character deletion from screen is based on how the characters
    159  *	are displayed by wgetch().
    160  */
    161 int
    162 __wgetnstr(WINDOW *win, char *str, int n)
    163 {
    164 	char *ostr, ec, kc;
    165 	int c, xpos, oldx, remain;
    166 
    167 	ostr = str;
    168 	ec = erasechar();
    169 	kc = killchar();
    170 	xpos = oldx = win->curx;
    171 	_DIAGASSERT(n == -1 || n > 1);
    172 	remain = n - 1;
    173 
    174 	while ((c = wgetch(win)) != ERR && c != '\n' && c != '\r') {
    175 #ifdef DEBUG
    176 		__CTRACE(__CTRACE_INPUT,
    177 		    "__wgetnstr: win %p, char 0x%x, remain %d\n",
    178 		    win, c, remain);
    179 #endif
    180 		*str = c;
    181 		__touchline(win, win->cury, 0, (int) win->maxx - 1);
    182 		if (c == ec || c == KEY_BACKSPACE || c == KEY_LEFT) {
    183 			*str = '\0';
    184 			if (str != ostr) {
    185 				if ((char) c == ec) {
    186 					mvwaddch(win, win->cury, xpos, ' ');
    187 					if (xpos > oldx)
    188 						mvwaddch(win, win->cury,
    189 						    xpos - 1, ' ');
    190 					if (win->curx > xpos - 1)
    191 						wmove(win, win->cury, xpos - 1);
    192 					xpos--;
    193 				}
    194 				if (c == KEY_BACKSPACE || c == KEY_LEFT) {
    195 					/* getch() displays the key sequence */
    196 					mvwaddch(win, win->cury, win->curx,
    197 					    ' ');
    198 					mvwaddch(win, win->cury, win->curx - 1,
    199 					    ' ');
    200 					if (win->curx > xpos)
    201 						wmove(win, win->cury, xpos - 1);
    202 					xpos--;
    203 				}
    204 				str--;
    205 				if (n != -1) {
    206 					/* We're counting chars */
    207 					remain++;
    208 				}
    209 			} else {        /* str == ostr */
    210 				/* getch() displays the other keys */
    211 				if (win->curx > oldx)
    212 					mvwaddch(win, win->cury, win->curx - 1,
    213 					    ' ');
    214 				wmove(win, win->cury, oldx);
    215 				xpos = oldx;
    216 			}
    217 		} else if (c == kc) {
    218 			*str = '\0';
    219 			if (str != ostr) {
    220 				/* getch() displays the kill character */
    221 				mvwaddch(win, win->cury, win->curx - 1, ' ');
    222 				/* Clear the characters from screen and str */
    223 				while (str != ostr) {
    224 					mvwaddch(win, win->cury, win->curx - 1,
    225 					    ' ');
    226 					wmove(win, win->cury, win->curx - 1);
    227 					str--;
    228 					if (n != -1)
    229 						/* We're counting chars */
    230 						remain++;
    231 				}
    232 				mvwaddch(win, win->cury, win->curx - 1, ' ');
    233 				wmove(win, win->cury, win->curx - 1);
    234 			} else
    235 				/* getch() displays the kill character */
    236 				mvwaddch(win, win->cury, oldx, ' ');
    237 			wmove(win, win->cury, oldx);
    238 		} else if (c >= KEY_MIN && c <= KEY_MAX) {
    239 			/* getch() displays these characters */
    240 			mvwaddch(win, win->cury, xpos, ' ');
    241 			wmove(win, win->cury, xpos);
    242 		} else {
    243 			if (remain) {
    244 				if (iscntrl((unsigned char)c))
    245 					mvwaddch(win, win->cury, xpos, ' ');
    246 				str++;
    247 				xpos++;
    248 				remain--;
    249 			} else
    250 				mvwaddch(win, win->cury, xpos, ' ');
    251 			wmove(win, win->cury, xpos);
    252 		}
    253 	}
    254 
    255 	if (c == ERR) {
    256 		*str = '\0';
    257 		return (ERR);
    258 	}
    259 	*str = '\0';
    260 	return (OK);
    261 }
    262