aboutsummaryrefslogtreecommitdiff
path: root/sys/tty/ttygoto.x
blob: d88961257797172c91dcfaf38949c8bfc303d415 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<chars.h>
include	"tty.h"

# TTYGOTO -- Position the cursor to the given absolute screen coordinates.
# We do not require any knowledge of the current screen position; many programs
# will wish to do cursor positioning at a lower level to take advantage of
# knowledge of the current position.

procedure ttygoto (fd, tty, col, line)

int	fd			# output stream
pointer	tty			# termcap descriptor
int	col			# destination column
int	line			# destination line

pointer	sp, cm_in, cm_out
int	coords[2], nchars, i
int	ttygets(), ttysubi()
errchk	salloc, ttygets, ttyputs, ttywrite

begin
	call smark (sp)
	call salloc (cm_in, SZ_CTRLSTR, TY_CHAR)
	call salloc (cm_out, SZ_LINE, TY_CHAR)

	# Use the cursor motion control string to position cursor, if the
	# terminal has such a capability.  Otherwise try to use primitive
	# motion commands.

	if (ttygets (tty, "cm", Memc[cm_in], SZ_CTRLSTR) > 0) {
	    # Coordinates are ordered line,col in the termcap entry,
	    # whereas our args are ordered col,line (e.g., x,y).  Store
	    # in "coords" in the termcap order.

	    coords[1] = line
	    coords[2] = col
	    nchars = ttysubi (Memc[cm_in], Memc[cm_out], SZ_LINE, coords, 2)

	    # Output cursor motion control string to file.
	    call ttywrite (fd, tty, Memc[cm_out], nchars, 1)

	    # If unable to position to the exact coordinates, use primitive
	    # motion commands to step the rest of the way in.  Do not abort
	    # if unable to do so.

	    if (coords[2] > 0) {
		# Presumably we do not have to step in very often, so we do
		# nothing special to buffer the backspace control sequence.
		# Most terminals recognize BS in any case.

		if (T_BSOK(tty) == YES) {
		    Memc[cm_out] = BS
		    Memc[cm_out+1] = EOS
		} else if (ttygets (tty, "bc", Memc[cm_out], SZ_CTRLSTR) == 0)
		    Memc[cm_out] = EOS

		for (i=coords[2];  i > 0;  i=i-1)
		    call ttyputs (fd, tty, Memc[cm_out], 1)
	    }

	    # Now adjust vertical position if necessary.
	    if (coords[1] > 0) {
		if (ttygets (tty, "up", Memc[cm_out], SZ_CTRLSTR) > 0)
		    for (i=coords[1];  i > 0;  i=i-1)
			call ttyputs (fd, tty, Memc[cm_out], 1)
	    }

	} else {
	    # Terminal has no nifty cursor addressing capability; add code here
	    # to position cursor by generating a sequence of more primitive
	    # codes.  Not going to bother with this for now.
	    ;
	}

	call sfree (sp)
end