xref: /minix3/lib/libcurses/curses.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: curses.c,v 1.25 2013/10/16 19:59:29 roy Exp $	*/
251ffecc1SBen Gras 
351ffecc1SBen Gras /*
451ffecc1SBen Gras  * Copyright (c) 1981, 1993, 1994
551ffecc1SBen Gras  *	The Regents of the University of California.  All rights reserved.
651ffecc1SBen Gras  *
751ffecc1SBen Gras  * Redistribution and use in source and binary forms, with or without
851ffecc1SBen Gras  * modification, are permitted provided that the following conditions
951ffecc1SBen Gras  * are met:
1051ffecc1SBen Gras  * 1. Redistributions of source code must retain the above copyright
1151ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer.
1251ffecc1SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
1351ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer in the
1451ffecc1SBen Gras  *    documentation and/or other materials provided with the distribution.
1551ffecc1SBen Gras  * 3. Neither the name of the University nor the names of its contributors
1651ffecc1SBen Gras  *    may be used to endorse or promote products derived from this software
1751ffecc1SBen Gras  *    without specific prior written permission.
1851ffecc1SBen Gras  *
1951ffecc1SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2051ffecc1SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2151ffecc1SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2251ffecc1SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2351ffecc1SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2451ffecc1SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2551ffecc1SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2651ffecc1SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2751ffecc1SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2851ffecc1SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2951ffecc1SBen Gras  * SUCH DAMAGE.
3051ffecc1SBen Gras  */
3151ffecc1SBen Gras 
3251ffecc1SBen Gras #include <sys/cdefs.h>
3351ffecc1SBen Gras #include <stdlib.h>
3451ffecc1SBen Gras #ifndef lint
3551ffecc1SBen Gras #if 0
3651ffecc1SBen Gras static char sccsid[] = "@(#)curses.c	8.3 (Berkeley) 5/4/94";
3751ffecc1SBen Gras #else
38*84d9c625SLionel Sambuc __RCSID("$NetBSD: curses.c,v 1.25 2013/10/16 19:59:29 roy Exp $");
3951ffecc1SBen Gras #endif
4051ffecc1SBen Gras #endif				/* not lint */
4151ffecc1SBen Gras 
4251ffecc1SBen Gras #include "curses.h"
4351ffecc1SBen Gras #include "curses_private.h"
4451ffecc1SBen Gras 
4551ffecc1SBen Gras /* Private. */
4651ffecc1SBen Gras int	__echoit = 1;			 /* If stty indicates ECHO. */
4751ffecc1SBen Gras int	__pfast;
4851ffecc1SBen Gras int	__rawmode = 0;			 /* If stty indicates RAW mode. */
4951ffecc1SBen Gras int	__noqch = 0;
5051ffecc1SBen Gras 					 /* If terminal doesn't have
5151ffecc1SBen Gras 					 * insert/delete line capabilities
5251ffecc1SBen Gras 					 * for quick change on refresh.
5351ffecc1SBen Gras 					 */
5451ffecc1SBen Gras char	__CA;
5551ffecc1SBen Gras 
5651ffecc1SBen Gras /*
5751ffecc1SBen Gras  * Public.
5851ffecc1SBen Gras  *
5951ffecc1SBen Gras  * XXX
6051ffecc1SBen Gras  * UPPERCASE isn't used by libcurses, and is left for backward
6151ffecc1SBen Gras  * compatibility only.
6251ffecc1SBen Gras  */
6351ffecc1SBen Gras WINDOW	*curscr;			/* Current screen. */
6451ffecc1SBen Gras WINDOW	*stdscr;			/* Standard screen. */
6551ffecc1SBen Gras WINDOW	*__virtscr;			/* Virtual screen (for doupdate()). */
6651ffecc1SBen Gras SCREEN  *_cursesi_screen;               /* the current screen we are using */
6751ffecc1SBen Gras int	 COLS;				/* Columns on the screen. */
6851ffecc1SBen Gras int	 LINES;				/* Lines on the screen. */
69*84d9c625SLionel Sambuc int	 TABSIZE;			/* Size of a tab. */
7051ffecc1SBen Gras int	 COLORS;			/* Maximum colors on the screen */
7151ffecc1SBen Gras int	 COLOR_PAIRS = 0;		/* Maximum color pairs on the screen */
7251ffecc1SBen Gras int	 My_term = 0;			/* Use Def_term regardless. */
7351ffecc1SBen Gras const char	*Def_term = "unknown";	/* Default terminal type. */
7451ffecc1SBen Gras char	 __GT;				/* Gtty indicates tabs. */
7551ffecc1SBen Gras char	 __NONL;			/* Term can't hack LF doing a CR. */
7651ffecc1SBen Gras char	 __UPPERCASE;			/* Terminal is uppercase only. */
7751ffecc1SBen Gras 
7851ffecc1SBen Gras #ifdef HAVE_WCHAR
7951ffecc1SBen Gras /*
8051ffecc1SBen Gras  * Copy the non-spacing character list (src_nsp) to the given character,
8151ffecc1SBen Gras  * allocate or free storage as required.
8251ffecc1SBen Gras  */
8351ffecc1SBen Gras int
_cursesi_copy_nsp(nschar_t * src_nsp,struct __ldata * ch)8451ffecc1SBen Gras _cursesi_copy_nsp(nschar_t *src_nsp, struct __ldata *ch)
8551ffecc1SBen Gras {
8651ffecc1SBen Gras 	nschar_t *np, *tnp, *pnp;
8751ffecc1SBen Gras 
8851ffecc1SBen Gras 	pnp = NULL;
8951ffecc1SBen Gras 	np = src_nsp;
9051ffecc1SBen Gras 	if (np) {
9151ffecc1SBen Gras 		tnp = ch->nsp;
9251ffecc1SBen Gras 		while (np) {
9351ffecc1SBen Gras 			if (tnp) {
9451ffecc1SBen Gras 				tnp->ch = np->ch;
9551ffecc1SBen Gras 				pnp = tnp;
9651ffecc1SBen Gras 				tnp = tnp->next;
9751ffecc1SBen Gras 			} else {
9851ffecc1SBen Gras 				tnp = (nschar_t *)malloc(sizeof(nschar_t));
9951ffecc1SBen Gras 				if (!tnp)
10051ffecc1SBen Gras 					return ERR;
10151ffecc1SBen Gras 				tnp->ch = np->ch;
10251ffecc1SBen Gras 				pnp->next = tnp;
10351ffecc1SBen Gras 				tnp->next = NULL;
10451ffecc1SBen Gras 				pnp = tnp;
10551ffecc1SBen Gras 				tnp = NULL;
10651ffecc1SBen Gras 			}
10751ffecc1SBen Gras 			np = np->next;
10851ffecc1SBen Gras 		}
10951ffecc1SBen Gras                 np = tnp;
11051ffecc1SBen Gras 		if (np) {
11151ffecc1SBen Gras 			pnp->next = NULL;
11251ffecc1SBen Gras 			__cursesi_free_nsp(np);
11351ffecc1SBen Gras 		}
11451ffecc1SBen Gras 	} else {
11551ffecc1SBen Gras 		if (ch->nsp) {
11651ffecc1SBen Gras 			__cursesi_free_nsp(ch->nsp);
11751ffecc1SBen Gras 			ch->nsp = NULL;
11851ffecc1SBen Gras 		}
11951ffecc1SBen Gras 	}
12051ffecc1SBen Gras 
12151ffecc1SBen Gras 	return OK;
12251ffecc1SBen Gras }
12351ffecc1SBen Gras 
12451ffecc1SBen Gras /*
12551ffecc1SBen Gras  * Free the storage associated with a non-spacing character - traverse the
12651ffecc1SBen Gras  * linked list until all storage is done.
12751ffecc1SBen Gras  */
12851ffecc1SBen Gras void
__cursesi_free_nsp(nschar_t * inp)12951ffecc1SBen Gras __cursesi_free_nsp(nschar_t *inp)
13051ffecc1SBen Gras {
13151ffecc1SBen Gras 	nschar_t *tnp, *np;
13251ffecc1SBen Gras 
13351ffecc1SBen Gras 	np = inp;
13451ffecc1SBen Gras 	if (np) {
13551ffecc1SBen Gras 		while (np) {
13651ffecc1SBen Gras 			tnp = np->next;
13751ffecc1SBen Gras 			free(np);
13851ffecc1SBen Gras 			np = tnp;
13951ffecc1SBen Gras 		}
14051ffecc1SBen Gras 	}
14151ffecc1SBen Gras }
14251ffecc1SBen Gras 
14351ffecc1SBen Gras /*
14451ffecc1SBen Gras  * Traverse all the cells in the given window free'ing the non-spacing
14551ffecc1SBen Gras  * character storage.
14651ffecc1SBen Gras  */
14751ffecc1SBen Gras void
__cursesi_win_free_nsp(WINDOW * win)14851ffecc1SBen Gras __cursesi_win_free_nsp(WINDOW *win)
14951ffecc1SBen Gras {
15051ffecc1SBen Gras 	int     i, j;
15151ffecc1SBen Gras 	__LDATA *sp;
15251ffecc1SBen Gras 
15351ffecc1SBen Gras 	for (i = 0; i < win->maxy; i++) {
15451ffecc1SBen Gras 		for (sp = win->alines[i]->line, j = 0; j < win->maxx;
15551ffecc1SBen Gras 		     j++, sp++) {
15651ffecc1SBen Gras 			__cursesi_free_nsp(sp->nsp);
15751ffecc1SBen Gras 		}
15851ffecc1SBen Gras 	}
15951ffecc1SBen Gras }
16051ffecc1SBen Gras 
16151ffecc1SBen Gras #endif
162