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