termcap.c revision 161099
18097Sjkh/*
28097Sjkh * Copyright (c) 1994, Paul Richards.
38881Srgrimes *
48097Sjkh * All rights reserved.
58881Srgrimes *
68097Sjkh * This software may be used, modified, copied, distributed, and sold, in both
78097Sjkh * source and binary form provided that the above copyright and these terms
88097Sjkh * are retained, verbatim, as the first lines of this file.  Under no
98097Sjkh * circumstances is the author responsible for the proper functioning of this
108097Sjkh * software, nor does the author assume any responsibility for damages
118097Sjkh * incurred with its use.
1276436Sache *
1376436Sache * $FreeBSD: head/usr.sbin/sade/termcap.c 161099 2006-08-08 13:45:46Z delphij $
148097Sjkh */
158097Sjkh
16161060Snetchild#include "sade.h"
178097Sjkh#include <stdarg.h>
188097Sjkh#include <fcntl.h>
198756Sjkh#include <sys/errno.h>
208097Sjkh#include <sys/ioctl.h>
2166834Sphk#include <sys/consio.h>
228097Sjkh
2317375Sjkh#define VTY_STATUS_LINE    24
2417375Sjkh#define TTY_STATUS_LINE    23
2517375Sjkh
2627798Sjkhstatic void
2727798Sjkhprompt_term(char **termp, char **termcapp)
2827798Sjkh{
29161099Sdelphij	char str[80];
3027798Sjkh
3127798Sjkh	printf("\nPlease set your TERM variable before running this program.\n");
3227798Sjkh	printf("Defaulting to an ANSI compatible terminal - please press RETURN\n");
3327798Sjkh	fgets(str, 80, stdin);	/* Just to make it interactive */
3427798Sjkh	*termp = (char *)"ansi";
3527798Sjkh	*termcapp = (char *)termcap_ansi;
3627798Sjkh}
3727798Sjkh
388097Sjkhint
398097Sjkhset_termcap(void)
408097Sjkh{
418097Sjkh    char           *term;
428601Sjkh    int		   stat;
4317375Sjkh    struct ttysize ts;
448097Sjkh
459202Srgrimes    term = getenv("TERM");
469202Srgrimes    stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay);
479202Srgrimes
4883819Smurray	if (isDebug())
4922757Sjkh	    DebugFD = open("sysinstall.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
5022757Sjkh	else
5122757Sjkh	    DebugFD = -1;
5216293Sjkh	if (DebugFD < 0)
5316293Sjkh	    DebugFD = open("/dev/null", O_RDWR, 0);
548770Sjkh
5518502Spst    if (!OnVTY || (stat < 0)) {
568601Sjkh	if (!term) {
5727798Sjkh	    char *term, *termcap;
5827798Sjkh
5927798Sjkh	    prompt_term(&term, &termcap);
6027798Sjkh	    if (setenv("TERM", term, 1) < 0)
618097Sjkh		return -1;
6227798Sjkh	    if (setenv("TERMCAP", termcap, 1) < 0)
638097Sjkh		return -1;
648601Sjkh	}
6529501Sjkh	if (DebugFD < 0)
6629501Sjkh	    DebugFD = open("/dev/null", O_RDWR, 0);
678097Sjkh    }
688302Sjkh    else {
699202Srgrimes	int i, on;
709202Srgrimes
7114738Sjkh	if (getpid() == 1) {
7214738Sjkh	    DebugFD = open("/dev/ttyv1", O_WRONLY);
7329501Sjkh	    if (DebugFD != -1) {
7429501Sjkh		on = 1;
7529501Sjkh		i = ioctl(DebugFD, TIOCCONS, (char *)&on);
7629501Sjkh		msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n",
7729501Sjkh			 DebugFD, i, !i ? "success" : strerror(errno));
7829501Sjkh	    }
7914738Sjkh	}
8061277Snyan
8161277Snyan#ifdef PC98
8261277Snyan	if (!term) {
8361277Snyan	    if (setenv("TERM", "cons25w", 1) < 0)
8461277Snyan		return -1;
8561277Snyan	    if (setenv("TERMCAP", termcap_cons25w, 1) < 0)
8661277Snyan		return -1;
8761277Snyan	}
8861277Snyan#else
898601Sjkh	if (ColorDisplay) {
908601Sjkh	    if (!term) {
918601Sjkh		if (setenv("TERM", "cons25", 1) < 0)
928601Sjkh		    return -1;
938601Sjkh		if (setenv("TERMCAP", termcap_cons25, 1) < 0)
948601Sjkh		    return -1;
958601Sjkh	    }
968601Sjkh	}
978601Sjkh	else {
988601Sjkh	    if (!term) {
998601Sjkh		if (setenv("TERM", "cons25-m", 1) < 0)
1008601Sjkh		    return -1;
1018601Sjkh		if (setenv("TERMCAP", termcap_cons25_m, 1) < 0)
1028601Sjkh		    return -1;
1038601Sjkh	    }
1048601Sjkh	}
10561277Snyan#endif
1068302Sjkh    }
10717375Sjkh    if (ioctl(0, TIOCGSIZE, &ts) == -1) {
10817375Sjkh	msgDebug("Unable to get terminal size - errno %d\n", errno);
10917382Sjkh	ts.ts_lines = 0;
11017375Sjkh    }
11122105Sjkh    StatusLine = ts.ts_lines ? ts.ts_lines - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
1128097Sjkh    return 0;
1138097Sjkh}
114