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