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