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