xref: /illumos-gate/usr/src/ucblib/libtermcap/tgoto.c (revision 8c0b080c8ed055a259d8cd26b9f005211c6a9753)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1979 Regents of the University of California */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/ttychars.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #if 0
39*7c478bd9Sstevel@tonic-gate static char
40*7c478bd9Sstevel@tonic-gate sccsid[] = "@(#)tgoto.c 1.5 88/02/08 SMI"; /* from UCB 4.1 6/27/83 */
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	MAXRETURNSIZE 64
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate char	*UP;
46*7c478bd9Sstevel@tonic-gate char	*BC;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * Routine to perform cursor addressing.
50*7c478bd9Sstevel@tonic-gate  * CM is a string containing printf type escapes to allow
51*7c478bd9Sstevel@tonic-gate  * cursor addressing.  We start out ready to print the destination
52*7c478bd9Sstevel@tonic-gate  * line, and switch each time we print row or column.
53*7c478bd9Sstevel@tonic-gate  * The following escapes are defined for substituting row/column:
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  *	%d	as in printf
56*7c478bd9Sstevel@tonic-gate  *	%2	like %2d
57*7c478bd9Sstevel@tonic-gate  *	%3	like %3d
58*7c478bd9Sstevel@tonic-gate  *	%.	gives %c hacking special case characters
59*7c478bd9Sstevel@tonic-gate  *	%+x	like %c but adding x first
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  *	The codes below affect the state but don't use up a value.
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  *	%>xy	if value > x add y
64*7c478bd9Sstevel@tonic-gate  *	%r	reverses row/column
65*7c478bd9Sstevel@tonic-gate  *	%i	increments row/column (for one origin indexing)
66*7c478bd9Sstevel@tonic-gate  *	%%	gives %
67*7c478bd9Sstevel@tonic-gate  *	%B	BCD (2 decimal digits encoded in one byte)
68*7c478bd9Sstevel@tonic-gate  *	%D	Delta Data (backwards bcd)
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * all other characters are ``self-inserting''.
71*7c478bd9Sstevel@tonic-gate  */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate char *
tgoto(char * CM,int destcol,int destline)74*7c478bd9Sstevel@tonic-gate tgoto(char *CM, int destcol, int destline)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	static char result[MAXRETURNSIZE];
77*7c478bd9Sstevel@tonic-gate 	static char added[10];
78*7c478bd9Sstevel@tonic-gate 	char *cp = CM;
79*7c478bd9Sstevel@tonic-gate 	char *dp = result;
80*7c478bd9Sstevel@tonic-gate 	int c;
81*7c478bd9Sstevel@tonic-gate 	int oncol = 0;
82*7c478bd9Sstevel@tonic-gate 	int which = destline;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (cp == 0) {
85*7c478bd9Sstevel@tonic-gate toohard:
86*7c478bd9Sstevel@tonic-gate 		/*
87*7c478bd9Sstevel@tonic-gate 		 * ``We don't do that under BOZO's big top''
88*7c478bd9Sstevel@tonic-gate 		 */
89*7c478bd9Sstevel@tonic-gate 		return ("OOPS");
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 	added[0] = 0;
92*7c478bd9Sstevel@tonic-gate 	while ((c = *cp++) != 0) {
93*7c478bd9Sstevel@tonic-gate 		if (c != '%') {
94*7c478bd9Sstevel@tonic-gate 			*dp++ = (char) c;
95*7c478bd9Sstevel@tonic-gate 			continue;
96*7c478bd9Sstevel@tonic-gate 		}
97*7c478bd9Sstevel@tonic-gate 		switch (c = *cp++) {
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate #ifdef CM_N
100*7c478bd9Sstevel@tonic-gate 		case 'n':
101*7c478bd9Sstevel@tonic-gate 			destcol ^= 0140;
102*7c478bd9Sstevel@tonic-gate 			destline ^= 0140;
103*7c478bd9Sstevel@tonic-gate 			goto setwhich;
104*7c478bd9Sstevel@tonic-gate #endif
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 		case 'd':
107*7c478bd9Sstevel@tonic-gate 			if (which < 10)
108*7c478bd9Sstevel@tonic-gate 				goto one;
109*7c478bd9Sstevel@tonic-gate 			if (which < 100)
110*7c478bd9Sstevel@tonic-gate 				goto two;
111*7c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 		case '3':
114*7c478bd9Sstevel@tonic-gate 			*dp++ = (which / 100) | '0';
115*7c478bd9Sstevel@tonic-gate 			which %= 100;
116*7c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 		case '2':
119*7c478bd9Sstevel@tonic-gate two:
120*7c478bd9Sstevel@tonic-gate 			*dp++ = which / 10 | '0';
121*7c478bd9Sstevel@tonic-gate one:
122*7c478bd9Sstevel@tonic-gate 			*dp++ = which % 10 | '0';
123*7c478bd9Sstevel@tonic-gate swap:
124*7c478bd9Sstevel@tonic-gate 			oncol = 1 - oncol;
125*7c478bd9Sstevel@tonic-gate setwhich:
126*7c478bd9Sstevel@tonic-gate 			which = oncol ? destcol : destline;
127*7c478bd9Sstevel@tonic-gate 			continue;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate #ifdef CM_GT
130*7c478bd9Sstevel@tonic-gate 		case '>':
131*7c478bd9Sstevel@tonic-gate 			if (which > *cp++)
132*7c478bd9Sstevel@tonic-gate 				which += *cp++;
133*7c478bd9Sstevel@tonic-gate 			else
134*7c478bd9Sstevel@tonic-gate 				cp++;
135*7c478bd9Sstevel@tonic-gate 			continue;
136*7c478bd9Sstevel@tonic-gate #endif
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 		case '+':
139*7c478bd9Sstevel@tonic-gate 			which += *cp++;
140*7c478bd9Sstevel@tonic-gate 			/*FALLTHRU*/
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 		case '.':
143*7c478bd9Sstevel@tonic-gate 			/*
144*7c478bd9Sstevel@tonic-gate 			 * This code is worth scratching your head at for a
145*7c478bd9Sstevel@tonic-gate 			 * while.  The idea is that various weird things can
146*7c478bd9Sstevel@tonic-gate 			 * happen to nulls, EOT's, tabs, and newlines by the
147*7c478bd9Sstevel@tonic-gate 			 * tty driver, arpanet, and so on, so we don't send
148*7c478bd9Sstevel@tonic-gate 			 * them if we can help it.
149*7c478bd9Sstevel@tonic-gate 			 *
150*7c478bd9Sstevel@tonic-gate 			 * Tab is taken out to get Ann Arbors to work, otherwise
151*7c478bd9Sstevel@tonic-gate 			 * when they go to column 9 we increment which is wrong
152*7c478bd9Sstevel@tonic-gate 			 * because bcd isn't continuous.  We should take out
153*7c478bd9Sstevel@tonic-gate 			 * the rest too, or run the thing through more than
154*7c478bd9Sstevel@tonic-gate 			 * once until it doesn't make any of these, but that
155*7c478bd9Sstevel@tonic-gate 			 * would make termlib (and hence pdp-11 ex) bigger,
156*7c478bd9Sstevel@tonic-gate 			 * and also somewhat slower.  This requires all
157*7c478bd9Sstevel@tonic-gate 			 * programs which use termlib to stty tabs so they
158*7c478bd9Sstevel@tonic-gate 			 * don't get expanded.  They should do this anyway
159*7c478bd9Sstevel@tonic-gate 			 * because some terminals use ^I for other things,
160*7c478bd9Sstevel@tonic-gate 			 * like nondestructive space.
161*7c478bd9Sstevel@tonic-gate 			 */
162*7c478bd9Sstevel@tonic-gate 			if (which == 0 || which == CTRL('d') || which == '\n') {
163*7c478bd9Sstevel@tonic-gate 				if (oncol || UP)
164*7c478bd9Sstevel@tonic-gate 					/* Assumption: backspace works */
165*7c478bd9Sstevel@tonic-gate 					/*
166*7c478bd9Sstevel@tonic-gate 					 * Loop needed because newline happens
167*7c478bd9Sstevel@tonic-gate 					 * to be the successor of tab.
168*7c478bd9Sstevel@tonic-gate 					 */
169*7c478bd9Sstevel@tonic-gate 					do {
170*7c478bd9Sstevel@tonic-gate 						(void) strcat(added, oncol ?
171*7c478bd9Sstevel@tonic-gate 							(BC ? BC : "\b") : UP);
172*7c478bd9Sstevel@tonic-gate 						which++;
173*7c478bd9Sstevel@tonic-gate 					} while (which == '\n');
174*7c478bd9Sstevel@tonic-gate 			}
175*7c478bd9Sstevel@tonic-gate 			*dp++ = (char) which;
176*7c478bd9Sstevel@tonic-gate 			goto swap;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 		case 'r':
179*7c478bd9Sstevel@tonic-gate 			oncol = 1;
180*7c478bd9Sstevel@tonic-gate 			goto setwhich;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 		case 'i':
183*7c478bd9Sstevel@tonic-gate 			destcol++;
184*7c478bd9Sstevel@tonic-gate 			destline++;
185*7c478bd9Sstevel@tonic-gate 			which++;
186*7c478bd9Sstevel@tonic-gate 			continue;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		case '%':
189*7c478bd9Sstevel@tonic-gate 			*dp++ = (char) c;
190*7c478bd9Sstevel@tonic-gate 			continue;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate #ifdef CM_B
193*7c478bd9Sstevel@tonic-gate 		case 'B':
194*7c478bd9Sstevel@tonic-gate 			which = (which/10 << 4) + which%10;
195*7c478bd9Sstevel@tonic-gate 			continue;
196*7c478bd9Sstevel@tonic-gate #endif
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate #ifdef CM_D
199*7c478bd9Sstevel@tonic-gate 		case 'D':
200*7c478bd9Sstevel@tonic-gate 			which = which - 2 * (which%16);
201*7c478bd9Sstevel@tonic-gate 			continue;
202*7c478bd9Sstevel@tonic-gate #endif
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 		default:
205*7c478bd9Sstevel@tonic-gate 			goto toohard;
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 	(void) strcpy(dp, added);
209*7c478bd9Sstevel@tonic-gate 	return (result);
210*7c478bd9Sstevel@tonic-gate }
211