Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
99 7u83 1
/*
2
 * This code contains changes by
3
 *      Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
4
 *
5
 * Conditions 1, 2, and 4 and the no-warranty notice below apply
6
 * to these changes.
7
 *
8
 *
9
 * Copyright (c) 1980, 1993
10
 *	The Regents of the University of California.  All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 * 1. Redistributions of source code must retain the above copyright
16
 *    notice, this list of conditions and the following disclaimer.
17
 * 2. Redistributions in binary form must reproduce the above copyright
18
 *    notice, this list of conditions and the following disclaimer in the
19
 *    documentation and/or other materials provided with the distribution.
20
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgement:
22
 *	This product includes software developed by the University of
23
 *	California, Berkeley and its contributors.
24
 * 4. Neither the name of the University nor the names of its contributors
25
 *    may be used to endorse or promote products derived from this software
26
 *    without specific prior written permission.
27
 *
28
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
 * SUCH DAMAGE.
39
 */
40
 
41
#ifndef	lint
42
#ifdef	DOSCCS
43
static char *sccsid = "@(#)tgoto.c	1.3 (gritter) 11/23/04";
44
#endif
45
#endif
46
 
47
/* from tgoto.c	5.1 (Berkeley) 6/5/85 */
48
 
49
#include "libterm.h"
50
 
51
#define	CTRL(c)	(c & 037)
52
 
53
#define MAXRETURNSIZE 64
54
 
55
#ifdef	__STDC__
56
#include <string.h>
57
#endif
58
 
59
char	*UP;
60
char	*BC;
61
 
62
/*
63
 * Routine to perform cursor addressing.
64
 * CM is a string containing printf type escapes to allow
65
 * cursor addressing.  We start out ready to print the destination
66
 * line, and switch each time we print row or column.
67
 * The following escapes are defined for substituting row/column:
68
 *
69
 *	%d	as in printf
70
 *	%2	like %2d
71
 *	%3	like %3d
72
 *	%.	gives %c hacking special case characters
73
 *	%+x	like %c but adding x first
74
 *
75
 *	The codes below affect the state but don't use up a value.
76
 *
77
 *	%>xy	if value > x add y
78
 *	%r	reverses row/column
79
 *	%i	increments row/column (for one origin indexing)
80
 *	%%	gives %
81
 *	%B	BCD (2 decimal digits encoded in one byte)
82
 *	%D	Delta Data (backwards bcd)
83
 *
84
 * all other characters are ``self-inserting''.
85
 */
86
char *
87
tgoto(char *CM, int destcol, int destline)
88
{
89
	static char result[MAXRETURNSIZE];
90
	static char added[10];
91
	char *cp = CM;
92
	register char *dp = result;
93
	register int c;
94
	int oncol = 0;
95
	register int which = destline;
96
 
97
	if (cp == 0) {
98
toohard:
99
		/*
100
		 * ``We don't do that under BOZO's big top''
101
		 */
102
		return ("OOPS");
103
	}
104
	added[0] = 0;
105
	while (c = *cp++) {
106
		if (c != '%') {
107
			*dp++ = c;
108
			continue;
109
		}
110
		switch (c = *cp++) {
111
 
112
#ifdef CM_N
113
		case 'n':
114
			destcol ^= 0140;
115
			destline ^= 0140;
116
			goto setwhich;
117
#endif
118
 
119
		case 'd':
120
			if (which < 10)
121
				goto one;
122
			if (which < 100)
123
				goto two;
124
			/* fall into... */
125
 
126
		case '3':
127
			*dp++ = (which / 100) | '0';
128
			which %= 100;
129
			/* fall into... */
130
 
131
		case '2':
132
two:	
133
			*dp++ = which / 10 | '0';
134
one:
135
			*dp++ = which % 10 | '0';
136
swap:
137
			oncol = 1 - oncol;
138
setwhich:
139
			which = oncol ? destcol : destline;
140
			continue;
141
 
142
#ifdef CM_GT
143
		case '>':
144
			if (which > *cp++)
145
				which += *cp++;
146
			else
147
				cp++;
148
			continue;
149
#endif
150
 
151
		case '+':
152
			which += *cp++;
153
			/* fall into... */
154
 
155
		case '.':
156
/* casedot: */
157
			/*
158
			 * This code is worth scratching your head at for a
159
			 * while.  The idea is that various weird things can
160
			 * happen to nulls, EOT's, tabs, and newlines by the
161
			 * tty driver, arpanet, and so on, so we don't send
162
			 * them if we can help it.
163
			 *
164
			 * Tab is taken out to get Ann Arbors to work, otherwise
165
			 * when they go to column 9 we increment which is wrong
166
			 * because bcd isn't continuous.  We should take out
167
			 * the rest too, or run the thing through more than
168
			 * once until it doesn't make any of these, but that
169
			 * would make termlib (and hence pdp-11 ex) bigger,
170
			 * and also somewhat slower.  This requires all
171
			 * programs which use termlib to stty tabs so they
172
			 * don't get expanded.  They should do this anyway
173
			 * because some terminals use ^I for other things,
174
			 * like nondestructive space.
175
			 */
176
			if (which == 0 || which == CTRL('d') || /* which == '\t' || */ which == '\n') {
177
				if (oncol || UP) /* Assumption: backspace works */
178
					/*
179
					 * Loop needed because newline happens
180
					 * to be the successor of tab.
181
					 */
182
					do {
183
						strcat(added, oncol ? (BC ? BC : "\b") : UP);
184
						which++;
185
					} while (which == '\n');
186
			}
187
			*dp++ = which;
188
			goto swap;
189
 
190
		case 'r':
191
			oncol = 1;
192
			goto setwhich;
193
 
194
		case 'i':
195
			destcol++;
196
			destline++;
197
			which++;
198
			continue;
199
 
200
		case '%':
201
			*dp++ = c;
202
			continue;
203
 
204
#ifdef CM_B
205
		case 'B':
206
			which = (which/10 << 4) + which%10;
207
			continue;
208
#endif
209
 
210
#ifdef CM_D
211
		case 'D':
212
			which = which - 2 * (which%16);
213
			continue;
214
#endif
215
 
216
		default:
217
			goto toohard;
218
		}
219
	}
220
	strcpy(dp, added);
221
	return (result);
222
}