termcap.c revision 22105
1/*
2 * Copyright (c) 1994, Paul Richards.
3 *
4 * All rights reserved.
5 *
6 * This software may be used, modified, copied, distributed, and sold, in both
7 * source and binary form provided that the above copyright and these terms
8 * are retained, verbatim, as the first lines of this file.  Under no
9 * circumstances is the author responsible for the proper functioning of this
10 * software, nor does the author assume any responsibility for damages
11 * incurred with its use.
12 */
13
14#include "sysinstall.h"
15#include <stdarg.h>
16#include <fcntl.h>
17#include <sys/errno.h>
18#include <sys/ioctl.h>
19#include <machine/console.h>
20
21#define VTY_STATUS_LINE    24
22#define TTY_STATUS_LINE    23
23
24int
25set_termcap(void)
26{
27    char           *term;
28    int		   stat;
29    struct ttysize ts;
30
31    term = getenv("TERM");
32    stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay);
33
34    if (getpid() != 1) {
35	DebugFD = open("sysinstall.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
36	if (DebugFD < 0)
37	    DebugFD = open("/dev/null", O_RDWR, 0);
38    }
39
40    if (!OnVTY || (stat < 0)) {
41	if (!term) {
42	    if (setenv("TERM", "vt100", 1) < 0)
43		return -1;
44	    if (setenv("TERMCAP", termcap_vt100, 1) < 0)
45		return -1;
46	}
47	if (DebugFD == -1)
48	    DebugFD = dup(1);
49    }
50    else {
51	int i, on;
52
53	if (getpid() == 1) {
54	    DebugFD = open("/dev/ttyv1", O_WRONLY);
55	    on = 1;
56	    i = ioctl(DebugFD, TIOCCONS, (char *)&on);
57	    msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n", DebugFD, i, !i ? "success" : strerror(errno));
58	}
59	if (ColorDisplay) {
60	    if (!term) {
61		if (setenv("TERM", "cons25", 1) < 0)
62		    return -1;
63		if (setenv("TERMCAP", termcap_cons25, 1) < 0)
64		    return -1;
65	    }
66	}
67	else {
68	    if (!term) {
69		if (setenv("TERM", "cons25-m", 1) < 0)
70		    return -1;
71		if (setenv("TERMCAP", termcap_cons25_m, 1) < 0)
72		    return -1;
73	    }
74	}
75    }
76    if (ioctl(0, TIOCGSIZE, &ts) == -1) {
77	msgDebug("Unable to get terminal size - errno %d\n", errno);
78	ts.ts_lines = 0;
79    }
80    StatusLine = ts.ts_lines ? ts.ts_lines - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
81    return 0;
82}
83