termcap.c revision 22757
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	if (getenv("SYSINSTALL_DEBUG"))
36	    DebugFD = open("sysinstall.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
37	else
38	    DebugFD = -1;
39	if (DebugFD < 0)
40	    DebugFD = open("/dev/null", O_RDWR, 0);
41    }
42
43    if (!OnVTY || (stat < 0)) {
44	if (!term) {
45	    if (setenv("TERM", "vt100", 1) < 0)
46		return -1;
47	    if (setenv("TERMCAP", termcap_vt100, 1) < 0)
48		return -1;
49	}
50	if (DebugFD == -1)
51	    DebugFD = dup(1);
52    }
53    else {
54	int i, on;
55
56	if (getpid() == 1) {
57	    DebugFD = open("/dev/ttyv1", O_WRONLY);
58	    on = 1;
59	    i = ioctl(DebugFD, TIOCCONS, (char *)&on);
60	    msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n", DebugFD, i, !i ? "success" : strerror(errno));
61	}
62	if (ColorDisplay) {
63	    if (!term) {
64		if (setenv("TERM", "cons25", 1) < 0)
65		    return -1;
66		if (setenv("TERMCAP", termcap_cons25, 1) < 0)
67		    return -1;
68	    }
69	}
70	else {
71	    if (!term) {
72		if (setenv("TERM", "cons25-m", 1) < 0)
73		    return -1;
74		if (setenv("TERMCAP", termcap_cons25_m, 1) < 0)
75		    return -1;
76	    }
77	}
78    }
79    if (ioctl(0, TIOCGSIZE, &ts) == -1) {
80	msgDebug("Unable to get terminal size - errno %d\n", errno);
81	ts.ts_lines = 0;
82    }
83    StatusLine = ts.ts_lines ? ts.ts_lines - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
84    return 0;
85}
86