top/contents search
The old termcap library routines, and their later ``terminfo'' replacements, involve a database of device-dependent strings to be ``printed'' to achieve various results. A program first loads the description of the terminal currently in use (typically named by the environment variable TERM), then fetches the various control strings and other parameters it will need, and finally prints those strings as needed. When moving the cursor, the tgoto function takes care of encoding the desired row and column number using whatever device-dependent encoding scheme is described in the device-dependent cm (``cursor motion'') string.

Here is a minimal example, which omits error checking and other nuances, but should get you started:
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>

int main()
{
	char buf[1024];
	char buf2[30];
	char *ap = buf2;
	char *clearstr, *gotostr, *standstr, *stendstr;

	tgetent(buf, getenv("TERM"));

	clearstr = tgetstr("cl", &ap);
	gotostr = tgetstr("cm", &ap);
	standstr = tgetstr("so", &ap);
	stendstr = tgetstr("se", &ap);

	fputs(clearstr, stdout);
	fputs(tgoto(gotostr, 20, 10), stdout);
	printf("Hello, ");
	fputs(standstr, stdout);
	printf("world");
	fputs(stendstr, stdout);
	putchar('!');
}

back


contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North