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