summaryrefslogtreecommitdiff
path: root/lib/libcurses/lib_options.c
blob: 53408b37918626887640bbee7982b6582bb2b458 (plain)
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
/*	$OpenBSD: lib_options.c,v 1.4 1998/01/17 16:27:34 millert Exp $	*/


/***************************************************************************
*                            COPYRIGHT NOTICE                              *
****************************************************************************
*                ncurses is copyright (C) 1992-1995                        *
*                          Zeyd M. Ben-Halim                               *
*                          zmbenhal@netcom.com                             *
*                          Eric S. Raymond                                 *
*                          esr@snark.thyrsus.com                           *
*                                                                          *
*        Permission is hereby granted to reproduce and distribute ncurses  *
*        by any means and for any fee, whether alone or as part of a       *
*        larger distribution, in source or in binary form, PROVIDED        *
*        this notice is included with any such distribution, and is not    *
*        removed from any of its header files. Mention of ncurses in any   *
*        applications linked with it is highly appreciated.                *
*                                                                          *
*        ncurses comes AS IS with no warranty, implied or expressed.       *
*                                                                          *
***************************************************************************/


/*
**	lib_options.c
**
**	The routines to handle option setting.
**
*/

#include <curses.priv.h>

#include <term.h>	/* keypad_xmit, keypad_local, meta_on, meta_off */
			/* cursor_visible,cursor_normal,cursor_invisible */

MODULE_ID("Id: lib_options.c,v 1.26 1997/09/20 15:02:34 juergen Exp $")

static void add_to_try(char *, short);

int has_ic(void)
{
	T((T_CALLED("has_ic()")));
	returnCode((insert_character || parm_ich
	   ||  (enter_insert_mode && exit_insert_mode))
	   &&  (delete_character || parm_dch));
}

int has_il(void)
{
	T((T_CALLED("has_il()")));
	returnCode((insert_line || parm_insert_line)
		&& (delete_line || parm_delete_line));
}

int idlok(WINDOW *win,  bool flag)
{
	T((T_CALLED("idlok(%p,%d)"), win, flag));

	if (win) {
	  _nc_idlok = win->_idlok = flag && (has_il() || change_scroll_region);
	  returnCode(OK);
	}
	else
	  returnCode(ERR);
}


void idcok(WINDOW *win, bool flag)
{
	T((T_CALLED("idcok(%p,%d)"), win, flag));

	if (win)
	  _nc_idcok = win->_idcok = flag && has_ic();

	returnVoid;
}


int clearok(WINDOW *win, bool flag)
{
	T((T_CALLED("clearok(%p,%d)"), win, flag));

	if (win) {
	  win->_clear = flag;
	  returnCode(OK);
	}
	else
	  returnCode(ERR);
}


void immedok(WINDOW *win, bool flag)
{
	T((T_CALLED("immedok(%p,%d)"), win, flag));

	if (win)
	  win->_immed = flag;

	returnVoid;
}

int leaveok(WINDOW *win, bool flag)
{
	T((T_CALLED("leaveok(%p,%d)"), win, flag));

	if (win) {
	  win->_leaveok = flag;
	  if (flag == TRUE)
	    curs_set(0);
	  else
	    curs_set(1);
	  returnCode(OK);
	}
	else
	  returnCode(ERR);
}


int scrollok(WINDOW *win, bool flag)
{
	T((T_CALLED("scrollok(%p,%d)"), win, flag));

	if (win) {
	  win->_scroll = flag;
	  returnCode(OK);
	}
	else
	  returnCode(ERR);
}

int halfdelay(int t)
{
	T((T_CALLED("halfdelay(%d)"), t));

	if (t < 1 || t > 255)
		returnCode(ERR);

	cbreak();
	SP->_cbreak = t+1;
	returnCode(OK);
}

int nodelay(WINDOW *win, bool flag)
{
	T((T_CALLED("nodelay(%p,%d)"), win, flag));

	if (win) {
	  if (flag == TRUE)
	    win->_delay = 0;
	  else win->_delay = -1;
	  returnCode(OK);
	}
	else
	  returnCode(ERR);
}

int notimeout(WINDOW *win, bool f)
{
	T((T_CALLED("notimout(%p,%d)"), win, f));

	if (win) {
	  win->_notimeout = f;
	  returnCode(OK);
	}
	else
	  returnCode(ERR);
}

int wtimeout(WINDOW *win, int delay)
{
	T((T_CALLED("wtimeout(%p,%d)"), win, delay));

	if (win) {
	  win->_delay = delay;
	  returnCode(OK);
	}
	else
	  returnCode(ERR);
}

int keypad(WINDOW *win, bool flag)
{
	T((T_CALLED("keypad(%p,%d)"), win, flag));

	if (win) {
	  win->_use_keypad = flag;
	  returnCode(_nc_keypad(flag));
	}
	else
	  returnCode(ERR);
}


int meta(WINDOW *win GCC_UNUSED, bool flag)
{
	/* Ok, we stay relaxed and don't signal an error if win is NULL */
	T((T_CALLED("meta(%p,%d)"), win, flag));

	SP->_use_meta = flag;

	if (flag  &&  meta_on)
	{
	    TPUTS_TRACE("meta_on");
	    putp(meta_on);
	}
	else if (! flag  &&  meta_off)
	{
	    TPUTS_TRACE("meta_off");
	    putp(meta_off);
	}
	returnCode(OK);
}

/* curs_set() moved here to narrow the kernel interface */

int curs_set(int vis)
{
int cursor = SP->_cursor;

	T((T_CALLED("curs_set(%d)"), vis));

	if (vis < 0 || vis > 2)
		returnCode(ERR);

	if (vis == cursor)
		returnCode(cursor);

	switch(vis) {
	case 2:
		if (cursor_visible)
		{
			TPUTS_TRACE("cursor_visible");
			putp(cursor_visible);
		}
		else
			returnCode(ERR);
		break;
	case 1:
		if (cursor_normal)
		{
			TPUTS_TRACE("cursor_normal");
			putp(cursor_normal);
		}
		else
			returnCode(ERR);
		break;
	case 0:
		if (cursor_invisible)
		{
			TPUTS_TRACE("cursor_invisible");
			putp(cursor_invisible);
		}
		else
			returnCode(ERR);
		break;
	}
	SP->_cursor = vis;
	(void) fflush(SP->_ofp);

	returnCode(cursor==-1 ? 1 : cursor);
}

int typeahead(int fd)
{
	T((T_CALLED("typeahead(%d)"), fd));
	SP->_checkfd = fd;
	returnCode(OK);
}

/*
**      has_key()
**
**      Return TRUE if the current terminal has the given key
**
*/


static int has_key_internal(int keycode, struct tries *tp)
{
    if (tp == 0)
        return(FALSE);
    else if (tp->value == keycode)
        return(TRUE);
    else
        return(has_key_internal(keycode, tp->child)
               || has_key_internal(keycode, tp->sibling));
}

int has_key(int keycode)
{
    T((T_CALLED("has_key(%d)"), keycode));
    returnCode(has_key_internal(keycode, SP->_keytry));
}

/*
**      init_keytry()
**
**      Construct the try for the current terminal's keypad keys.
**
*/


static struct  tries *newtry;

static void init_keytry(void)
{
	newtry = 0;

/* LINT_PREPRO
#if 0*/
#include <keys.tries>
/* LINT_PREPRO
#endif*/

	SP->_keytry = newtry;
}


static void add_to_try(char *str, short code)
{
static bool     out_of_memory = FALSE;
struct tries    *ptr, *savedptr;

	if (! str  ||  out_of_memory)
		return;

	if (newtry != 0) {
		ptr = savedptr = newtry;

		for (;;) {
			while (ptr->ch != (unsigned char) *str
			       &&  ptr->sibling != 0)
				ptr = ptr->sibling;
	
			if (ptr->ch == (unsigned char) *str) {
				if (*(++str)) {
					if (ptr->child != 0)
						ptr = ptr->child;
					else
						break;
				} else {
					ptr->value = code;
					return;
				}
			} else {
				if ((ptr->sibling = typeCalloc(struct tries,1)) == 0) {
					out_of_memory = TRUE;
					return;
				}

				savedptr = ptr = ptr->sibling;
				if (*str == '\200')
					ptr->ch = '\0';
				else
					ptr->ch = (unsigned char) *str;
				str++;
				ptr->value = 0;

				break;
			}
		} /* end for (;;) */
	} else {   /* newtry == 0 :: First sequence to be added */
		savedptr = ptr = newtry = typeCalloc(struct tries,1);

		if (ptr == 0) {
			out_of_memory = TRUE;
				return;
		}

		if (*str == '\200')
			ptr->ch = '\0';
		else
			ptr->ch = (unsigned char) *str;
		str++;
		ptr->value = 0;
	}

	    /* at this point, we are adding to the try.  ptr->child == 0 */

	while (*str) {
		ptr->child = typeCalloc(struct tries,1);

		ptr = ptr->child;

		if (ptr == 0) {
			out_of_memory = TRUE;

			ptr = savedptr;
			while (ptr != 0) {
				savedptr = ptr->child;
				free(ptr);
				ptr = savedptr;
			}

			return;
		}

		if (*str == '\200')
			ptr->ch = '\0';
		else
			ptr->ch = (unsigned char) *str;
		str++;
		ptr->value = 0;
	}

	ptr->value = code;
	return;
}

/* Turn the keypad on/off
 *
 * Note:  we flush the output because changing this mode causes some terminals
 * to emit different escape sequences for cursor and keypad keys.  If we don't
 * flush, then the next wgetch may get the escape sequence that corresponds to
 * the terminal state _before_ switching modes.
 */
int _nc_keypad(bool flag)
{
	if (flag  &&  keypad_xmit)
	{
	    TPUTS_TRACE("keypad_xmit");
	    putp(keypad_xmit);
	    (void) fflush(SP->_ofp);
	}
	else if (! flag  &&  keypad_local)
	{
	    TPUTS_TRACE("keypad_local");
	    putp(keypad_local);
	    (void) fflush(SP->_ofp);
	}

	if (SP->_keytry == UNINITIALISED)
	    init_keytry();
	return(OK);
}