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$
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
27161115Snetchildprompt_term(char **termp)
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");
33167260Skevlo	fgets(str, sizeof(str), stdin);	/* Just to make it interactive */
3427798Sjkh	*termp = (char *)"ansi";
3527798Sjkh}
3627798Sjkh
378097Sjkhint
388097Sjkhset_termcap(void)
398097Sjkh{
408097Sjkh    char           *term;
418601Sjkh    int		   stat;
42179244Sed    struct winsize ws;
438097Sjkh
449202Srgrimes    term = getenv("TERM");
459202Srgrimes    stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay);
469202Srgrimes
4783819Smurray	if (isDebug())
48178940Sobrien	    DebugFD = open("sade.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
4922757Sjkh	else
5022757Sjkh	    DebugFD = -1;
5116293Sjkh	if (DebugFD < 0)
5216293Sjkh	    DebugFD = open("/dev/null", O_RDWR, 0);
538770Sjkh
5418502Spst    if (!OnVTY || (stat < 0)) {
558601Sjkh	if (!term) {
56161115Snetchild	    char *term;
5727798Sjkh
58161115Snetchild	    prompt_term(&term);
5927798Sjkh	    if (setenv("TERM", term, 1) < 0)
608097Sjkh		return -1;
618601Sjkh	}
6229501Sjkh	if (DebugFD < 0)
6329501Sjkh	    DebugFD = open("/dev/null", O_RDWR, 0);
648097Sjkh    }
658302Sjkh    else {
669202Srgrimes	int i, on;
679202Srgrimes
6814738Sjkh	if (getpid() == 1) {
6914738Sjkh	    DebugFD = open("/dev/ttyv1", O_WRONLY);
7029501Sjkh	    if (DebugFD != -1) {
7129501Sjkh		on = 1;
7229501Sjkh		i = ioctl(DebugFD, TIOCCONS, (char *)&on);
7329501Sjkh		msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n",
7429501Sjkh			 DebugFD, i, !i ? "success" : strerror(errno));
7529501Sjkh	    }
7614738Sjkh	}
7761277Snyan
7861277Snyan#ifdef PC98
7961277Snyan	if (!term) {
8061277Snyan	    if (setenv("TERM", "cons25w", 1) < 0)
8161277Snyan		return -1;
8261277Snyan	}
8361277Snyan#else
848601Sjkh	if (ColorDisplay) {
858601Sjkh	    if (!term) {
86199243Sed		if (setenv("TERM", "xterm", 1) < 0)
878601Sjkh		    return -1;
888601Sjkh	    }
898601Sjkh	}
908601Sjkh	else {
918601Sjkh	    if (!term) {
92199250Sed		if (setenv("TERM", "vt100", 1) < 0)
938601Sjkh		    return -1;
948601Sjkh	    }
958601Sjkh	}
9661277Snyan#endif
978302Sjkh    }
98179244Sed    if (ioctl(0, TIOCGWINSZ, &ws) == -1) {
9917375Sjkh	msgDebug("Unable to get terminal size - errno %d\n", errno);
100179244Sed	ws.ws_row = 0;
10117375Sjkh    }
102179244Sed    StatusLine = ws.ws_row ? ws.ws_row - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
1038097Sjkh    return 0;
1048097Sjkh}
105