xref: /illumos-gate/usr/src/ucblib/libcurses/addch.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved.  The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #ifndef lint
18 static char
19 sccsid[] = "@(#)addch.c 1.6 88/02/08 SMI"; /* from UCB 5.1 85/06/07 */
20 #endif	/* not lint */
21 
22 #include	"curses.ext"
23 
24 /* forward declaration */
25 static void set_ch(WINDOW *, int, int, int);
26 
27 /*
28  *	This routine adds the character to the current position
29  */
30 
31 int
32 waddch(WINDOW *win, char c)
33 {
34 	int		x, y;
35 	int		newx;
36 
37 	x = win->_curx;
38 	y = win->_cury;
39 #ifdef FULLDEBUG
40 	fprintf(outf, "ADDCH('%c') at (%d, %d)\n", c, y, x);
41 #endif
42 	switch (c) {
43 	case '\t':
44 		for (newx = x + (8 - (x & 07)); x < newx; x++)
45 			if (waddch(win, ' ') == ERR)
46 				return (ERR);
47 		return (OK);
48 
49 	default:
50 #ifdef FULLDEBUG
51 		fprintf(outf, "ADDCH: 1: y = %d, x = %d, firstch = %d,"
52 		    " lastch = %d\n", y, x, win->_firstch[y],
53 		    win->_lastch[y]);
54 #endif
55 		if (win->_flags & _STANDOUT)
56 			c |= _STANDOUT;
57 		set_ch(win, y, x, c);
58 		win->_y[y][x++] = c;
59 		if (x >= win->_maxx) {
60 			x = 0;
61 newline:
62 			if (++y >= win->_maxy)
63 				if (win->_scroll) {
64 					(void) scroll(win);
65 					--y;
66 				}
67 				else
68 					return (ERR);
69 		}
70 #ifdef FULLDEBUG
71 		fprintf(outf, "ADDCH: 2: y = %d, x = %d, firstch = %d,"
72 		    " lastch = %d\n", y, x, win->_firstch[y],
73 		    win->_lastch[y]);
74 #endif
75 		break;
76 	case '\n':
77 		(void) wclrtoeol(win);
78 		if (!NONL)
79 			x = 0;
80 		goto newline;
81 	case '\r':
82 		x = 0;
83 		break;
84 	case '\b':
85 		if (--x < 0)
86 			x = 0;
87 		break;
88 	}
89 	win->_curx = (short)x;
90 	win->_cury = (short)y;
91 	return (OK);
92 }
93 
94 /*
95  * set_ch:
96  *	Set the first and last change flags for this window.
97  */
98 
99 static void
100 set_ch(WINDOW *win, int y, int x, int ch)
101 {
102 #ifdef	FULLDEBUG
103 	fprintf(outf, "SET_CH(%0.2o, %d, %d)\n", win, y, x);
104 #endif
105 	if (win->_y[y][x] != ch) {
106 		x += win->_ch_off;
107 		if (win->_firstch[y] == _NOCHANGE)
108 			win->_firstch[y] = win->_lastch[y] = (short)x;
109 		else if (x < win->_firstch[y])
110 			win->_firstch[y] = (short)x;
111 		else if (x > win->_lastch[y])
112 			win->_lastch[y] = (short)x;
113 #ifdef FULLDEBUG
114 		fprintf(outf, "SET_CH: change gives f/l: %d/%d [%d/%d]\n",
115 		    win->_firstch[y], win->_lastch[y],
116 		    win->_firstch[y] - win->_ch_off,
117 		    win->_lastch[y] - win->_ch_off);
118 #endif
119 	}
120 }
121