1 /* $NetBSD: tgoto.c,v 1.24 2006/08/27 08:28:38 christos Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)tgoto.c 8.1 (Berkeley) 6/4/93";
37 __RCSID("$NetBSD: tgoto.c,v 1.24 2006/08/27 08:28:38 christos Exp $");
44 #include <termcap_private.h>
49 #define CTRL(c) ((c) & 037)
51 #define MAXRETURNSIZE 128
57 * Routine to perform cursor addressing.
58 * CM is a string containing printf type escapes to allow
59 * cursor addressing. We start out ready to print the destination
60 * line, and switch each time we print row or column.
61 * The following escapes are defined for substituting row/column:
66 * %. gives %c hacking special case characters
67 * %+x like %c but adding x first
69 * The codes below affect the state but don't use up a value.
71 * %>xy if value > x add y
72 * %r reverses row/column
73 * %i increments row/column (for one origin indexing)
75 * %B BCD (2 decimal digits encoded in one byte)
76 * %D Delta Data (backwards bcd)
78 * all other characters are ``self-inserting''.
81 tgoto(const char *CM
, int destcol
, int destline
)
83 static char result
[MAXRETURNSIZE
];
85 (void)t_goto(NULL
, CM
, destcol
, destline
, result
, sizeof(result
));
90 * New interface. Functionally the same as tgoto but uses the tinfo struct
91 * to set UP and BC. The arg buffer is filled with the result string, limit
92 * defines the maximum number of chars allowed in buffer. The function
93 * returns 0 on success, -1 otherwise, the result string contains an error
97 t_goto(struct tinfo
*info
, const char *CM
, int destcol
, int destline
,
98 char *buffer
, size_t limit
)
105 int which
= destline
;
106 char *buf_lim
= buffer
+ limit
;
109 char *eap
= &added
[sizeof(added
) / sizeof(added
[0])];
112 /* CM is checked below */
113 _DIAGASSERT(buffer
!= NULL
);
123 (void)strlcpy(buffer
, "no fmt", limit
);
128 while ((c
= *cp
++) != '\0') {
129 if (c
!= '%' || ((c
= *cp
++) == '%')) {
132 (void)strlcpy(buffer
, "no space copying %",
145 /* flip oncol here so it doesn't actually change */
153 /* Generate digits into temp buffer in reverse order */
156 dig_buf
[k
++] = which
% 10 | '0';
157 while ((which
/= 10) != 0);
162 (void)snprintf(buffer
, limit
,
163 "digit buf overflow %d %d",
172 if (dp
+ k
>= buf_lim
) {
173 (void)strlcpy(buffer
, "digit buf copy", limit
);
177 /* then unwind into callers buffer */
179 *dp
++ = dig_buf
[--k
];
198 * This code is worth scratching your head at for a
199 * while. The idea is that various weird things can
200 * happen to nulls, EOT's, tabs, and newlines by the
201 * tty driver, arpanet, and so on, so we don't send
202 * them if we can help it.
204 * Tab is taken out to get Ann Arbors to work, otherwise
205 * when they go to column 9 we increment which is wrong
206 * because bcd isn't continuous. We should take out
207 * the rest too, or run the thing through more than
208 * once until it doesn't make any of these, but that
209 * would make termlib (and hence pdp-11 ex) bigger,
210 * and also somewhat slower. This requires all
211 * programs which use termlib to stty tabs so they
212 * don't get expanded. They should do this anyway
213 * because some terminals use ^I for other things,
214 * like nondestructive space.
216 if (which
== 0 || which
== CTRL('d') ||
217 /* which == '\t' || */ which
== '\n') {
218 if (oncol
|| UP
) { /* Assumption: backspace works */
219 char *add
= oncol
? (BC
? BC
: "\b") : UP
;
222 * Loop needed because newline happens
223 * to be the successor of tab.
228 while ((*ap
++ = *as
++) != '\0')
238 } while (which
== '\n');
243 (void)strlcpy(buffer
, "dot copy", limit
);
261 which
= (which
/ 10 << 4) + which
% 10;
267 which
= which
- 2 * (which
% 16);
272 (void)snprintf(buffer
, limit
, "bad format `%c'", c
);
277 /* flip to other number... */
279 which
= oncol
? destcol
: destline
;
281 if (dp
+ (ap
- added
) >= buf_lim
) {
282 (void)strlcpy(buffer
, "big added", limit
);
288 for (ap
= added
; (*dp
++ = *ap
++) != '\0';)