Subversion Repositories planix.SVN

Rev

Rev 36 | Details | Compare with Previous | Last modification | View Log | RSS feed

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