termcap.c revision 22105
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.
128097Sjkh */
138097Sjkh
1421243Sjkh#include "sysinstall.h"
158097Sjkh#include <stdarg.h>
168097Sjkh#include <fcntl.h>
178756Sjkh#include <sys/errno.h>
188097Sjkh#include <sys/ioctl.h>
198097Sjkh#include <machine/console.h>
208097Sjkh
2117375Sjkh#define VTY_STATUS_LINE    24
2217375Sjkh#define TTY_STATUS_LINE    23
2317375Sjkh
248097Sjkhint
258097Sjkhset_termcap(void)
268097Sjkh{
278097Sjkh    char           *term;
288601Sjkh    int		   stat;
2917375Sjkh    struct ttysize ts;
308097Sjkh
319202Srgrimes    term = getenv("TERM");
329202Srgrimes    stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay);
339202Srgrimes
3416293Sjkh    if (getpid() != 1) {
358601Sjkh	DebugFD = open("sysinstall.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
3616293Sjkh	if (DebugFD < 0)
3716293Sjkh	    DebugFD = open("/dev/null", O_RDWR, 0);
3816293Sjkh    }
398770Sjkh
4018502Spst    if (!OnVTY || (stat < 0)) {
418601Sjkh	if (!term) {
428097Sjkh	    if (setenv("TERM", "vt100", 1) < 0)
438097Sjkh		return -1;
448097Sjkh	    if (setenv("TERMCAP", termcap_vt100, 1) < 0)
458097Sjkh		return -1;
468601Sjkh	}
478601Sjkh	if (DebugFD == -1)
488097Sjkh	    DebugFD = dup(1);
498097Sjkh    }
508302Sjkh    else {
519202Srgrimes	int i, on;
529202Srgrimes
5314738Sjkh	if (getpid() == 1) {
5414738Sjkh	    DebugFD = open("/dev/ttyv1", O_WRONLY);
5514738Sjkh	    on = 1;
5614738Sjkh	    i = ioctl(DebugFD, TIOCCONS, (char *)&on);
5714738Sjkh	    msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n", DebugFD, i, !i ? "success" : strerror(errno));
5814738Sjkh	}
598601Sjkh	if (ColorDisplay) {
608601Sjkh	    if (!term) {
618601Sjkh		if (setenv("TERM", "cons25", 1) < 0)
628601Sjkh		    return -1;
638601Sjkh		if (setenv("TERMCAP", termcap_cons25, 1) < 0)
648601Sjkh		    return -1;
658601Sjkh	    }
668601Sjkh	}
678601Sjkh	else {
688601Sjkh	    if (!term) {
698601Sjkh		if (setenv("TERM", "cons25-m", 1) < 0)
708601Sjkh		    return -1;
718601Sjkh		if (setenv("TERMCAP", termcap_cons25_m, 1) < 0)
728601Sjkh		    return -1;
738601Sjkh	    }
748601Sjkh	}
758302Sjkh    }
7617375Sjkh    if (ioctl(0, TIOCGSIZE, &ts) == -1) {
7717375Sjkh	msgDebug("Unable to get terminal size - errno %d\n", errno);
7817382Sjkh	ts.ts_lines = 0;
7917375Sjkh    }
8022105Sjkh    StatusLine = ts.ts_lines ? ts.ts_lines - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
818097Sjkh    return 0;
828097Sjkh}
83