termcap.c revision 161060
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 161060 2006-08-07 23:35:49Z netchild $
14 */
15
16#include "sade.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	printf("\nPlease set your TERM variable before running this program.\n");
40	printf("Defaulting to an ANSI compatible terminal - please press RETURN\n");
41	fgets(str, 80, stdin);	/* Just to make it interactive */
42	*termp = (char *)"ansi";
43	*termcapp = (char *)termcap_ansi;
44}
45
46int
47set_termcap(void)
48{
49    char           *term;
50    int		   stat;
51    struct ttysize ts;
52
53    term = getenv("TERM");
54    stat = ioctl(STDERR_FILENO, GIO_COLOR, &ColorDisplay);
55
56	if (isDebug())
57	    DebugFD = open("sysinstall.debug", O_WRONLY|O_CREAT|O_TRUNC, 0644);
58	else
59	    DebugFD = -1;
60	if (DebugFD < 0)
61	    DebugFD = open("/dev/null", O_RDWR, 0);
62
63    if (!OnVTY || (stat < 0)) {
64	if (!term) {
65	    char *term, *termcap;
66
67	    prompt_term(&term, &termcap);
68	    if (setenv("TERM", term, 1) < 0)
69		return -1;
70	    if (setenv("TERMCAP", termcap, 1) < 0)
71		return -1;
72	}
73	if (DebugFD < 0)
74	    DebugFD = open("/dev/null", O_RDWR, 0);
75    }
76    else {
77	int i, on;
78
79	if (getpid() == 1) {
80	    DebugFD = open("/dev/ttyv1", O_WRONLY);
81	    if (DebugFD != -1) {
82		on = 1;
83		i = ioctl(DebugFD, TIOCCONS, (char *)&on);
84		msgDebug("ioctl(%d, TIOCCONS, NULL) = %d (%s)\n",
85			 DebugFD, i, !i ? "success" : strerror(errno));
86	    }
87	}
88
89#ifdef PC98
90	if (!term) {
91	    if (setenv("TERM", "cons25w", 1) < 0)
92		return -1;
93	    if (setenv("TERMCAP", termcap_cons25w, 1) < 0)
94		return -1;
95	}
96#else
97	if (ColorDisplay) {
98	    if (!term) {
99		if (setenv("TERM", "cons25", 1) < 0)
100		    return -1;
101		if (setenv("TERMCAP", termcap_cons25, 1) < 0)
102		    return -1;
103	    }
104	}
105	else {
106	    if (!term) {
107		if (setenv("TERM", "cons25-m", 1) < 0)
108		    return -1;
109		if (setenv("TERMCAP", termcap_cons25_m, 1) < 0)
110		    return -1;
111	    }
112	}
113#endif
114    }
115    if (ioctl(0, TIOCGSIZE, &ts) == -1) {
116	msgDebug("Unable to get terminal size - errno %d\n", errno);
117	ts.ts_lines = 0;
118    }
119    StatusLine = ts.ts_lines ? ts.ts_lines - 1: (OnVTY ? VTY_STATUS_LINE : TTY_STATUS_LINE);
120    return 0;
121}
122