xref: /illumos-gate/usr/src/ucblib/libcurses/addch.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 #ifndef lint
16 static char
17 sccsid[] = "@(#)addch.c 1.6 88/02/08 SMI"; /* from UCB 5.1 85/06/07 */
18 #endif	/* not lint */
19 
20 #include	"curses.ext"
21 
22 /* forward declaration */
23 static void set_ch(WINDOW *, int, int, int);
24 
25 /*
26  *	This routine adds the character to the current position
27  */
28 
29 int
30 waddch(WINDOW *win, char c)
31 {
32 	int		x, y;
33 	int		newx;
34 
35 	x = win->_curx;
36 	y = win->_cury;
37 #ifdef FULLDEBUG
38 	fprintf(outf, "ADDCH('%c') at (%d, %d)\n", c, y, x);
39 #endif
40 	switch (c) {
41 	case '\t':
42 		for (newx = x + (8 - (x & 07)); x < newx; x++)
43 			if (waddch(win, ' ') == ERR)
44 				return (ERR);
45 		return (OK);
46 
47 	default:
48 #ifdef FULLDEBUG
49 		fprintf(outf, "ADDCH: 1: y = %d, x = %d, firstch = %d,"
50 		    " lastch = %d\n", y, x, win->_firstch[y],
51 		    win->_lastch[y]);
52 #endif
53 		if (win->_flags & _STANDOUT)
54 			c |= _STANDOUT;
55 		set_ch(win, y, x, c);
56 		win->_y[y][x++] = c;
57 		if (x >= win->_maxx) {
58 			x = 0;
59 newline:
60 			if (++y >= win->_maxy)
61 				if (win->_scroll) {
62 					(void) scroll(win);
63 					--y;
64 				}
65 				else
66 					return (ERR);
67 		}
68 #ifdef FULLDEBUG
69 		fprintf(outf, "ADDCH: 2: y = %d, x = %d, firstch = %d,"
70 		    " lastch = %d\n", y, x, win->_firstch[y],
71 		    win->_lastch[y]);
72 #endif
73 		break;
74 	case '\n':
75 		(void) wclrtoeol(win);
76 		if (!NONL)
77 			x = 0;
78 		goto newline;
79 	case '\r':
80 		x = 0;
81 		break;
82 	case '\b':
83 		if (--x < 0)
84 			x = 0;
85 		break;
86 	}
87 	win->_curx = (short)x;
88 	win->_cury = (short)y;
89 	return (OK);
90 }
91 
92 /*
93  * set_ch:
94  *	Set the first and last change flags for this window.
95  */
96 
97 static void
98 set_ch(WINDOW *win, int y, int x, int ch)
99 {
100 #ifdef	FULLDEBUG
101 	fprintf(outf, "SET_CH(%0.2o, %d, %d)\n", win, y, x);
102 #endif
103 	if (win->_y[y][x] != ch) {
104 		x += win->_ch_off;
105 		if (win->_firstch[y] == _NOCHANGE)
106 			win->_firstch[y] = win->_lastch[y] = (short)x;
107 		else if (x < win->_firstch[y])
108 			win->_firstch[y] = (short)x;
109 		else if (x > win->_lastch[y])
110 			win->_lastch[y] = (short)x;
111 #ifdef FULLDEBUG
112 		fprintf(outf, "SET_CH: change gives f/l: %d/%d [%d/%d]\n",
113 		    win->_firstch[y], win->_lastch[y],
114 		    win->_firstch[y] - win->_ch_off,
115 		    win->_lastch[y] - win->_ch_off);
116 #endif
117 	}
118 }
119