tput
is left vague and minimal in POSIX because there is a more detailed specification in X/Open Curses:
There appears to be no direct link to an HTML version of the latter (in particular the command-line tput
), but it is more detailed (about twice as long). Quoting from the description in X/Open Curses:
7319 When XCURSES is supported, this description for the tput utility replaces that in the XC
7320 specification.
7321 The tput utility uses the terminfo database to make the values of terminal-dependen
7322 capabilities and information available to the shell (see sh in the XCU specification); to clear
7323 initialize, or reset the terminal; or to return the long name of the requested terminal type. Th
7324 tput utility outputs a string if the capability attribute (capname) is of type string, or an integer i
7325 the attribute is of type integer. If the attribute is of type boolean, tput simply sets the exit statu
7326 (0 for TRUE if the terminal has the capability, 1 for FALSE if it does not), and produces n
7327 output.
the program will retrieve any value from the terminal database. Most of the platforms which you use provide an implementation of X/Open Curses. The fine details may differ, of course. On some platforms you may encounter a version of tput
which uses termcap names rather than terminfo. But you are not likely to encounter that on an assignment using "POSIX", and in any case, you can accomplish the same goal, using a slightly different vocabulary.
However, neither curses nor ANSI escape sequences are part of POSIX. Escape sequences are standardized in ECMA-48:
As a rule, POSIX does not overlap much with other standards (you will find most exceptions to that rule versus the C standard). Likewise, X/Open Curses does not overlap much with ECMA-48: the form and content of escape sequences are not detailed in that document.
Strictly speaking, you cannot do your assignment using only POSIX. You can only do this using POSIX plus the usual assortment of related standards for which there is an implementation on your system.
The reason for applications such as tput
(and libraries such as curses
) is to provide a layer to hide details and inconsistencies across implementations. POSIX only goes so far, and omits most of the interesting features of an operating system, e.g., user management, security, and of course managing terminals. Even with escape sequences, there are several ways to move the cursor on various terminals. Here are a few terminfo summaries of those:
carriage_return cr cr carriage return (P*)
(P*)
column_address hpa ch horizontal position
#1, absolute (P)
cursor_address cup cm move to row #1 col-
umns #2
cursor_down cud1 do down one line
cursor_home home ho home cursor (if no
cup)
cursor_left cub1 le move left one space
cursor_mem_address mrcup CM memory relative cur-
sor addressing, move
to row #1 columns #2
cursor_right cuf1 nd non-destructive
cursor_to_ll ll ll last line, first
column (if no cup)
cursor_up cuu1 up up one line
space (move right
one space)
parm_left_cursor cub LE move #1 characters
to the left (P)
parm_right_cursor cuf RI move #1 characters
to the right (P*)
restore_cursor rc rc restore cursor to
position of last
row_address vpa cv vertical position #1
absolute (P)
save_cursor sc sc save current cursor
position (P)
tab ht ta tab to next 8-space
hardware tab stop
save_cursor
tput
is POSIX, but it might just be the least standardized of their listed tools. Basically you can do:printf \\33\[
?[ABCD]
, where ? is a number for how many cursor positions you want to move - A goes up, B down, C right, D left. And use\\337
or\\338
for saving/restoring current cursor state respectively.tput
allowed by POSIX don't include anything that does absolute or relative cursor motion. I think you'll need to write C code, using the curses or terminfo libraries, if you want to do that.tput
are not constrained by POSIX. In fact, they're deliberately unspecified: Thetput
utility shall display terminal-dependent information. The manner in which this information is retrieved is unspecified. The information displayed shall clear the terminal screen, initialize the user's terminal, or reset the user's terminal, depending on the operand given. The exact consequences of displaying this information are unspecified.\\33\[r;cH
wherer
is the desired row andc
the desired column, both starting at 1. It's widely supported but isn't POSIX.\\33\[6n
.