-
Notifications
You must be signed in to change notification settings - Fork 0
/
getstr.c
473 lines (370 loc) · 11.1 KB
/
getstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* PDCurses */
#include "curspriv.h"
/*man-start**************************************************************
getstr
------
### Synopsis
int getstr(char *str);
int wgetstr(WINDOW *win, char *str);
int mvgetstr(int y, int x, char *str);
int mvwgetstr(WINDOW *win, int y, int x, char *str);
int getnstr(char *str, int n);
int wgetnstr(WINDOW *win, char *str, int n);
int mvgetnstr(int y, int x, char *str, int n);
int mvwgetnstr(WINDOW *win, int y, int x, char *str, int n);
int get_wstr(wint_t *wstr);
int wget_wstr(WINDOW *win, wint_t *wstr);
int mvget_wstr(int y, int x, wint_t *wstr);
int mvwget_wstr(WINDOW *win, int, int, wint_t *wstr);
int getn_wstr(wint_t *wstr, int n);
int wgetn_wstr(WINDOW *win, wint_t *wstr, int n);
int mvgetn_wstr(int y, int x, wint_t *wstr, int n);
int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n);
### Description
These routines call wgetch() repeatedly to build a string,
interpreting erase and kill characters along the way, until a newline
or carriage return is received. When PDCurses is built with wide-
character support enabled, the narrow-character functions convert the
wgetch()'d values into a multibyte string in the current locale
before returning it. The resulting string is placed in the area
pointed to by *str. The routines with n as the last argument read at
most n characters.
Note that there's no way to know how long the buffer passed to
wgetstr() is, so use wgetnstr() to avoid buffer overflows.
### Return Value
These functions return ERR on failure or any other value on success.
### Portability
X/Open ncurses NetBSD
getstr Y Y Y
wgetstr Y Y Y
mvgetstr Y Y Y
mvwgetstr Y Y Y
getnstr Y Y Y
wgetnstr Y Y Y
mvgetnstr Y Y Y
mvwgetnstr Y Y Y
get_wstr Y Y Y
wget_wstr Y Y Y
mvget_wstr Y Y Y
mvwget_wstr Y Y Y
getn_wstr Y Y Y
wgetn_wstr Y Y Y
mvgetn_wstr Y Y Y
mvwgetn_wstr Y Y Y
**man-end****************************************************************/
#define MAXLINE 255
int wgetnstr(WINDOW *win, char *str, int n)
{
#ifdef PDC_WIDE
wchar_t wstr[MAXLINE + 1];
if (n < 0 || n > MAXLINE)
n = MAXLINE;
if (wgetn_wstr(win, (wint_t *)wstr, n) == ERR)
return ERR;
return PDC_wcstombs(str, wstr, n);
#else
int ch, i, num, x, chars;
char *p;
bool stop, oldecho, oldcbreak, oldnodelay;
PDC_LOG(("wgetnstr() - called\n"));
if (!win || !str)
return ERR;
chars = 0;
p = str;
stop = FALSE;
x = win->_curx;
oldcbreak = SP->cbreak; /* remember states */
oldecho = SP->echo;
oldnodelay = win->_nodelay;
SP->echo = FALSE; /* we do echo ourselves */
cbreak(); /* ensure each key is returned immediately */
win->_nodelay = FALSE; /* don't return -1 */
wrefresh(win);
while (!stop)
{
ch = wgetch(win);
switch (ch)
{
case '\t':
ch = ' ';
num = TABSIZE - (win->_curx - x) % TABSIZE;
for (i = 0; i < num; i++)
{
if (chars < n)
{
if (oldecho)
waddch(win, ch);
*p++ = ch;
++chars;
}
else
beep();
}
break;
case _ECHAR: /* CTRL-H -- Delete character */
if (p > str)
{
if (oldecho)
waddstr(win, "\b \b");
ch = (unsigned char)(*--p);
if ((ch < ' ') && (oldecho))
waddstr(win, "\b \b");
chars--;
}
break;
case _DLCHAR: /* CTRL-U -- Delete line */
while (p > str)
{
if (oldecho)
waddstr(win, "\b \b");
ch = (unsigned char)(*--p);
if ((ch < ' ') && (oldecho))
waddstr(win, "\b \b");
}
chars = 0;
break;
case _DWCHAR: /* CTRL-W -- Delete word */
while ((p > str) && (*(p - 1) == ' '))
{
if (oldecho)
waddstr(win, "\b \b");
--p; /* remove space */
chars--;
}
while ((p > str) && (*(p - 1) != ' '))
{
if (oldecho)
waddstr(win, "\b \b");
ch = (unsigned char)(*--p);
if ((ch < ' ') && (oldecho))
waddstr(win, "\b \b");
chars--;
}
break;
case '\n':
case '\r':
stop = TRUE;
if (oldecho)
waddch(win, '\n');
break;
default:
if (chars < n)
{
if (!SP->key_code && ch < 0x100)
{
*p++ = ch;
if (oldecho)
waddch(win, ch);
chars++;
}
}
else
beep();
break;
}
wrefresh(win);
}
*p = '\0';
SP->echo = oldecho; /* restore old settings */
SP->cbreak = oldcbreak;
win->_nodelay = oldnodelay;
return OK;
#endif
}
int getstr(char *str)
{
PDC_LOG(("getstr() - called\n"));
return wgetnstr(stdscr, str, MAXLINE);
}
int wgetstr(WINDOW *win, char *str)
{
PDC_LOG(("wgetstr() - called\n"));
return wgetnstr(win, str, MAXLINE);
}
int mvgetstr(int y, int x, char *str)
{
PDC_LOG(("mvgetstr() - called\n"));
if (move(y, x) == ERR)
return ERR;
return wgetnstr(stdscr, str, MAXLINE);
}
int mvwgetstr(WINDOW *win, int y, int x, char *str)
{
PDC_LOG(("mvwgetstr() - called\n"));
if (wmove(win, y, x) == ERR)
return ERR;
return wgetnstr(win, str, MAXLINE);
}
int getnstr(char *str, int n)
{
PDC_LOG(("getnstr() - called\n"));
return wgetnstr(stdscr, str, n);
}
int mvgetnstr(int y, int x, char *str, int n)
{
PDC_LOG(("mvgetnstr() - called\n"));
if (move(y, x) == ERR)
return ERR;
return wgetnstr(stdscr, str, n);
}
int mvwgetnstr(WINDOW *win, int y, int x, char *str, int n)
{
PDC_LOG(("mvwgetnstr() - called\n"));
if (wmove(win, y, x) == ERR)
return ERR;
return wgetnstr(win, str, n);
}
#ifdef PDC_WIDE
int wgetn_wstr(WINDOW *win, wint_t *wstr, int n)
{
int ch, i, num, x, chars;
wint_t *p;
bool stop, oldecho, oldcbreak, oldnodelay;
PDC_LOG(("wgetn_wstr() - called\n"));
if (!win || !wstr)
return ERR;
chars = 0;
p = wstr;
stop = FALSE;
x = win->_curx;
oldcbreak = SP->cbreak; /* remember states */
oldecho = SP->echo;
oldnodelay = win->_nodelay;
SP->echo = FALSE; /* we do echo ourselves */
cbreak(); /* ensure each key is returned immediately */
win->_nodelay = FALSE; /* don't return -1 */
wrefresh(win);
while (!stop)
{
ch = wgetch(win);
switch (ch)
{
case '\t':
ch = ' ';
num = TABSIZE - (win->_curx - x) % TABSIZE;
for (i = 0; i < num; i++)
{
if (chars < n)
{
if (oldecho)
waddch(win, ch);
*p++ = ch;
++chars;
}
else
beep();
}
break;
case _ECHAR: /* CTRL-H -- Delete character */
if (p > wstr)
{
if (oldecho)
waddstr(win, "\b \b");
ch = *--p;
if ((ch < ' ') && (oldecho))
waddstr(win, "\b \b");
chars--;
}
break;
case _DLCHAR: /* CTRL-U -- Delete line */
while (p > wstr)
{
if (oldecho)
waddstr(win, "\b \b");
ch = *--p;
if ((ch < ' ') && (oldecho))
waddstr(win, "\b \b");
}
chars = 0;
break;
case _DWCHAR: /* CTRL-W -- Delete word */
while ((p > wstr) && (*(p - 1) == ' '))
{
if (oldecho)
waddstr(win, "\b \b");
--p; /* remove space */
chars--;
}
while ((p > wstr) && (*(p - 1) != ' '))
{
if (oldecho)
waddstr(win, "\b \b");
ch = *--p;
if ((ch < ' ') && (oldecho))
waddstr(win, "\b \b");
chars--;
}
break;
case '\n':
case '\r':
stop = TRUE;
if (oldecho)
waddch(win, '\n');
break;
default:
if (chars < n)
{
if (!SP->key_code)
{
*p++ = ch;
if (oldecho)
waddch(win, ch);
chars++;
}
}
else
beep();
break;
}
wrefresh(win);
}
*p = '\0';
SP->echo = oldecho; /* restore old settings */
SP->cbreak = oldcbreak;
win->_nodelay = oldnodelay;
return OK;
}
int get_wstr(wint_t *wstr)
{
PDC_LOG(("get_wstr() - called\n"));
return wgetn_wstr(stdscr, wstr, MAXLINE);
}
int wget_wstr(WINDOW *win, wint_t *wstr)
{
PDC_LOG(("wget_wstr() - called\n"));
return wgetn_wstr(win, wstr, MAXLINE);
}
int mvget_wstr(int y, int x, wint_t *wstr)
{
PDC_LOG(("mvget_wstr() - called\n"));
if (move(y, x) == ERR)
return ERR;
return wgetn_wstr(stdscr, wstr, MAXLINE);
}
int mvwget_wstr(WINDOW *win, int y, int x, wint_t *wstr)
{
PDC_LOG(("mvwget_wstr() - called\n"));
if (wmove(win, y, x) == ERR)
return ERR;
return wgetn_wstr(win, wstr, MAXLINE);
}
int getn_wstr(wint_t *wstr, int n)
{
PDC_LOG(("getn_wstr() - called\n"));
return wgetn_wstr(stdscr, wstr, n);
}
int mvgetn_wstr(int y, int x, wint_t *wstr, int n)
{
PDC_LOG(("mvgetn_wstr() - called\n"));
if (move(y, x) == ERR)
return ERR;
return wgetn_wstr(stdscr, wstr, n);
}
int mvwgetn_wstr(WINDOW *win, int y, int x, wint_t *wstr, int n)
{
PDC_LOG(("mvwgetn_wstr() - called\n"));
if (wmove(win, y, x) == ERR)
return ERR;
return wgetn_wstr(win, wstr, n);
}
#endif