[unix-pc.general] terminal initialization

egray@killer.DALLAS.TX.US (Emmet Gray) (09/09/88)

Hum....  I've got a lot of mail about this, so I guess it's time for
an explaination.

I had included a Readme.7300 file in the recent Pcomm release that
gives an example of how to configure the /etc/profile to send the
terminal initializaion strings (and set the tabs) at login time.

There two bugs in the example,

was	if [ -n "`tput tab`" ]	should be:	if [ -n "`tput hts`" ]
was	tpuf is3		should be:	tput is3

The whole thing should have read:

	eval `tput iprog`
	tput is1
	tput is2
	if [ -n "`tput hts`" ] ;then
		stty tabs
	else
		stty -tabs
	fi
	tabs
	cat -s "`tput if`"
	tput is3
	echo "\r\c"

But what's more interesting is that *some* of the Unix 3.51 releases on
actualy have the "init" option to tput (which makes this all a moot
point).  If you've got one that works, all of the above can be substituted
with:

	tput init
	tabs

And then, there's the brute force approach.... write a C program (that's
why I didn't catch the bug... *I* don't either of the above).  I've 
included the source to "initterm" that I have in my /etc/profile.


Emmet P. Gray				US Army, HQ III Corps & Fort Hood
...!uunet!uiucuxc!fthood!egray		Attn: AFZF-DE-ENV
					Directorate of Engineering & Housing
					Environmental Management Office
					Fort Hood, TX 76544-5057

----------------------------------------------------------------------------
/*
 * A program to initialize the terminal.  Future version of Unix will
 * an option to "tput" to perform this function with only one argument.
 */

#include <stdio.h>
#include <curses.h>
#include <term.h>

main(argc, argv)
int argc;
char *argv[];
{
	FILE *fp;
	int i, tab_width;
	char *term, *getenv(), buf[BUFSIZ];
	void exit();
					/* get terminal type */
	if (argc > 1)
		term = argv[1];
	else {
		term = getenv("TERM");
		if (term == NULL || *term == NULL) {
			fprintf(stderr, "initterm: No TERM environmental variable\n");
			exit(1);
		}
	}
					/* see if terminfo entry exists */
	setupterm(term, 1, &i);
	if (i != 1) {
		fprintf(stderr, "initterm: Can't find terminal type \"%s\"\n", term);
		exit(1);
	}

	/*
	 * The order for these initializations is right out of the SVID
	 */
					/* init program */
	if (init_prog != NULL)
		system(init_prog);
					/* 1st init string */
	if (init_1string != NULL)
		putp(init_1string);
					/* 2nd init string */
	if (init_2string != NULL)
		putp(init_2string);
					/* set tabs stops */
	if (clear_all_tabs != NULL && set_tab != NULL) {
		putp(clear_all_tabs);
		putchar('\r');

		if (init_tabs <= 0)
			tab_width = 8;
		else
			tab_width = init_tabs;

		for (i=0; i<columns; i+=tab_width) {
			printf("        ");
			putp(set_tab);
		}
		putchar('\r');
	}
					/* file for long init strings */
	if (init_file != NULL) {
		fp = fopen(init_file, "r");
					/* cat the file */
		while ((i = fread(buf, sizeof(buf[0]), BUFSIZ, fp)) > 0)
			if (fwrite(buf, sizeof(buf[0]), i, stdout) != i)

		fclose(fp);
	}
					/* 3rd init string */
	if (init_3string != NULL)
		putp(init_3string);

	putchar('\r');
	exit(0);
}