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