[net.sources] `clear

gam@proper.UUCP (Gordon Moffett) (02/12/84)

The following C program is a replacement for the 4.1 BSD clear(1)
command.  It clears the screen, and if given the ``-l'' option will
also clear the status line.  (The capability to do this is called
``ds'' in termcap(5)).

Comments appreciated!  Man page will follow.
----------------------------------------------------------------------
/*
 * clear -- clear terminal screen and maybe status line
 *	usage:  clear [-l]
 */

#include <stdio.h>

#define CLRSCRN "cl"	/* termcap id for `clear screen' */
#define CLRSTAT "ds"	/* termcap id for `disable status line */
#define TBUFSIZ 1024	/* size of termcap buffer */

char *cmdname;		/* should point to argv[0] */
extern int errno;	/* UNIX error number */

int statln;		/* !=0 means clear status line, if possible */
char *term;		/* terminal name from environment var TERM */
char bp[TBUFSIZ];	/* buffer pointer for termcap entry */
char cleararea[32];	/* area for clear-strings */

struct {		/* pointers to clear-strings */
	char *screen;
	char *statln;
} clrs;

main(argc, argv)
	int argc;
	char *argv[];
{
	char *getenv();

	cmdname = argv[0];

	/* check option count */

	if (argc > 2) {
		usage();
	}

	/* check options */

	if (argc == 2) {
		if (strcmp(argv[1], "-l") == 0) {
			statln++;
		} else {
			usage();
		}
	}

	/* check terminal name (if any) */

	term = getenv("TERM");

	if (term == NULL) {
		fprintf(stderr, "%s: unknown terminal\n", cmdname);
		exit(1);
	}

	/* look up termcap info for this terminal */

	termchk();

	/* set up clear-strings */

	setclrs();

	/* clear screen */

	clearscrn();

	/* clear status line, if wanted */

	if (statln) {
		clearstln();
	}
}

setclrs()	/* set clear-strings from termcap entry */
{
	char *clearbuf;
	char *tgetstr();

	clearbuf = cleararea;

	clrs.screen = tgetstr(CLRSCRN, &clearbuf);
	if (clrs.screen == NULL) {
		clrs.screen = "";
	}

	clrs.statln = tgetstr(CLRSTAT, &clearbuf);
	if (clrs.statln == NULL) {
		clrs.statln = "";
	}
}

clearscrn()	/* clear screen */
{
	printf(clrs.screen);
}

clearstln()	/* turn off status line */
{
	printf(clrs.statln);
}

termchk()	/* get termcap entry -- die if not found */
{
	int v;	/* returned from tgetent() */

	switch (v = tgetent(bp, term)) {
		case 1:		/* got termcap entry OK */
			break;
		case -1:
			fprintf(stderr, "%s: can't open termcap file\n",
				cmdname);
			exit(errno);
			break;
		case 0:
			fprintf(stderr, "%s: no termcap entry for %s\n",
				cmdname, term);
			exit(1);
			break;
		default:
			fprintf(stderr, "%s: tgetent() returned unknown value: %d\n",
				cmdname, v);
			exit(1);
			break;
	}
}

usage()		/* tell them how this is used */
{
	fprintf(stderr, "usage: %s [-l]\n", cmdname);
	exit(1);
}
-- 
Gordon A. Moffett
	{ allegra, decvax!decwrl } !amd70!proper
	hplabs!intelca!proper!gam