termcap.c revision 66834
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 <sys/consio.h>
20
21#define VTY_STATUS_LINE    24
22#define TTY_STATUS_LINE    23
23
24static void
25prompt_term(char **termp, char **termcapp)
26{
27    char str[80];
28    static struct {
29	const char *term, *termcap;
30    } lookup[] = { { "ansi", termcap_ansi },
31		   { "vt100", termcap_vt100 },
32		   { "cons25w", termcap_cons25w },
33		   { "cons25", termcap_cons25 },
34		   { "cons25-m", termcap_cons25_m },
35		   { "xterm", termcap_xterm } };
36
37    if (RunningAsInit) {
38	while (1) {
39	    int i;
40
41	    printf("\nThese are the predefined terminal types available to\n");
42	    printf("sysinstall when running stand-alone.  Please choose the\n");
43	    printf("closest match for your particular terminal.\n\n");
44	    printf("1 ...................... Standard ANSI terminal.\n");
45	    printf("2 ...................... VT100 or compatible terminal.\n");
46	    printf("3 ...................... FreeBSD system console (color).\n");
47	    printf("4 ...................... FreeBSD system console (monochrome).\n\n");
48	    printf("5 ...................... xterm terminal emulator.\n\n");
49	    printf("Your choice: (1-5) ");
50	    fflush(stdout);
51	    fgets(str, 80, stdin);
52	    i = str[0] - '0';
53	    if (i > 0 && i < 6) {
54		*termp = (char *)lookup[i - 1].term;
55		*termcapp = (char *)lookup[i - 1].termcap;
56		break;
57	    }
58	    else
59		printf("\007Invalid choice, please try again.\n\n");
60	}
61    }
62    else {
63	printf("\nPlease set your TERM variable before running this program.\n");
64	printf("Defaulting to an ANSI compatible terminal - please press RETURN\n");
65	fgets(str, 80, stdin);	/* Just to make it interactive */
66	*termp = (char *)"ansi";
67	*termcapp = (char *)termcap_ansi;
68    }
69}
70
71int
72set_termcap(void)
73{
74    char           *term;
75    int		   stat;
76    struct ttysize ts;
77
78    term = getenv("TERM");
79    stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay);
80
81    if (!RunningAsInit) {
82	if (getenv("SYSINSTALL_DEBUG"))
83	    DebugFD = open("sysinstall.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
84	else
85	    DebugFD = -1;
86	if (DebugFD < 0)
87	    DebugFD = open("/dev/null", O_RDWR, 0);
88    }
89
90    if (!OnVTY || (stat < 0)) {
91	if (!term) {
92	    char *term, *termcap;
93
94	    prompt_term(&term, &termcap);
95	    if (setenv("TERM", term, 1) < 0)
96		return -1;
97	    if (setenv("TERMCAP", termcap, 1) < 0)
98		return -1;
99	}
100	if (DebugFD < 0)
101	    DebugFD = open("/dev/null", O_RDWR, 0);
102    }
103    else {
104	int i, on;
105
106	if (getpid() == 1) {
107	    DebugFD = open("/dev/ttyv1", O_WRONLY);
108	    if (DebugFD != -1) {
109		on = 1;
110		i = ioctl(DebugFD, TIOCCONS, (char *)&on);
111		msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n",
112			 DebugFD, i, !i ? "success" : strerror(errno));
113	    }
114	}
115
116#ifdef PC98
117	if (!term) {
118	    if (setenv("TERM", "cons25w", 1) < 0)
119		return -1;
120	    if (setenv("TERMCAP", termcap_cons25w, 1) < 0)
121		return -1;
122	}
123#else
124	if (ColorDisplay) {
125	    if (!term) {
126		if (setenv("TERM", "cons25", 1) < 0)
127		    return -1;
128		if (setenv("TERMCAP", termcap_cons25, 1) < 0)
129		    return -1;
130	    }
131	}
132	else {
133	    if (!term) {
134		if (setenv("TERM", "cons25-m", 1) < 0)
135		    return -1;
136		if (setenv("TERMCAP", termcap_cons25_m, 1) < 0)
137		    return -1;
138	    }
139	}
140#endif
141    }
142    if (ioctl(0, TIOCGSIZE, &ts) == -1) {
143	msgDebug("Unable to get terminal size - errno %d\n", errno);
144	ts.ts_lines = 0;
145    }
146    StatusLine = ts.ts_lines ? ts.ts_lines - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
147    return 0;
148}
149