1 |
|
|
/* $OpenBSD: tty-term.c,v 1.45 2016/01/29 11:13:56 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
15 |
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
16 |
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
|
21 |
|
|
#include <curses.h> |
22 |
|
|
#include <fnmatch.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
#include <term.h> |
26 |
|
|
#include <vis.h> |
27 |
|
|
|
28 |
|
|
#include "tmux.h" |
29 |
|
|
|
30 |
|
|
void tty_term_override(struct tty_term *, const char *); |
31 |
|
|
char *tty_term_strip(const char *); |
32 |
|
|
|
33 |
|
|
struct tty_terms tty_terms = LIST_HEAD_INITIALIZER(tty_terms); |
34 |
|
|
|
35 |
|
|
enum tty_code_type { |
36 |
|
|
TTYCODE_NONE = 0, |
37 |
|
|
TTYCODE_STRING, |
38 |
|
|
TTYCODE_NUMBER, |
39 |
|
|
TTYCODE_FLAG, |
40 |
|
|
}; |
41 |
|
|
|
42 |
|
|
struct tty_code { |
43 |
|
|
enum tty_code_type type; |
44 |
|
|
union { |
45 |
|
|
char *string; |
46 |
|
|
int number; |
47 |
|
|
int flag; |
48 |
|
|
} value; |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
struct tty_term_code_entry { |
52 |
|
|
enum tty_code_type type; |
53 |
|
|
const char *name; |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
const struct tty_term_code_entry tty_term_codes[] = { |
57 |
|
|
[TTYC_ACSC] = { TTYCODE_STRING, "acsc" }, |
58 |
|
|
[TTYC_AX] = { TTYCODE_FLAG, "AX" }, |
59 |
|
|
[TTYC_BCE] = { TTYCODE_FLAG, "bce" }, |
60 |
|
|
[TTYC_BEL] = { TTYCODE_STRING, "bel" }, |
61 |
|
|
[TTYC_BLINK] = { TTYCODE_STRING, "blink" }, |
62 |
|
|
[TTYC_BOLD] = { TTYCODE_STRING, "bold" }, |
63 |
|
|
[TTYC_CIVIS] = { TTYCODE_STRING, "civis" }, |
64 |
|
|
[TTYC_CLEAR] = { TTYCODE_STRING, "clear" }, |
65 |
|
|
[TTYC_CNORM] = { TTYCODE_STRING, "cnorm" }, |
66 |
|
|
[TTYC_COLORS] = { TTYCODE_NUMBER, "colors" }, |
67 |
|
|
[TTYC_CR] = { TTYCODE_STRING, "Cr" }, |
68 |
|
|
[TTYC_CS] = { TTYCODE_STRING, "Cs" }, |
69 |
|
|
[TTYC_CSR] = { TTYCODE_STRING, "csr" }, |
70 |
|
|
[TTYC_CUB] = { TTYCODE_STRING, "cub" }, |
71 |
|
|
[TTYC_CUB1] = { TTYCODE_STRING, "cub1" }, |
72 |
|
|
[TTYC_CUD] = { TTYCODE_STRING, "cud" }, |
73 |
|
|
[TTYC_CUD1] = { TTYCODE_STRING, "cud1" }, |
74 |
|
|
[TTYC_CUF] = { TTYCODE_STRING, "cuf" }, |
75 |
|
|
[TTYC_CUF1] = { TTYCODE_STRING, "cuf1" }, |
76 |
|
|
[TTYC_CUP] = { TTYCODE_STRING, "cup" }, |
77 |
|
|
[TTYC_CUU] = { TTYCODE_STRING, "cuu" }, |
78 |
|
|
[TTYC_CUU1] = { TTYCODE_STRING, "cuu1" }, |
79 |
|
|
[TTYC_CVVIS] = { TTYCODE_STRING, "cvvis" }, |
80 |
|
|
[TTYC_DCH] = { TTYCODE_STRING, "dch" }, |
81 |
|
|
[TTYC_DCH1] = { TTYCODE_STRING, "dch1" }, |
82 |
|
|
[TTYC_DIM] = { TTYCODE_STRING, "dim" }, |
83 |
|
|
[TTYC_DL] = { TTYCODE_STRING, "dl" }, |
84 |
|
|
[TTYC_DL1] = { TTYCODE_STRING, "dl1" }, |
85 |
|
|
[TTYC_E3] = { TTYCODE_STRING, "E3" }, |
86 |
|
|
[TTYC_ECH] = { TTYCODE_STRING, "ech" }, |
87 |
|
|
[TTYC_EL] = { TTYCODE_STRING, "el" }, |
88 |
|
|
[TTYC_EL1] = { TTYCODE_STRING, "el1" }, |
89 |
|
|
[TTYC_ENACS] = { TTYCODE_STRING, "enacs" }, |
90 |
|
|
[TTYC_FSL] = { TTYCODE_STRING, "fsl" }, |
91 |
|
|
[TTYC_HOME] = { TTYCODE_STRING, "home" }, |
92 |
|
|
[TTYC_HPA] = { TTYCODE_STRING, "hpa" }, |
93 |
|
|
[TTYC_ICH] = { TTYCODE_STRING, "ich" }, |
94 |
|
|
[TTYC_ICH1] = { TTYCODE_STRING, "ich1" }, |
95 |
|
|
[TTYC_IL] = { TTYCODE_STRING, "il" }, |
96 |
|
|
[TTYC_IL1] = { TTYCODE_STRING, "il1" }, |
97 |
|
|
[TTYC_INVIS] = { TTYCODE_STRING, "invis" }, |
98 |
|
|
[TTYC_IS1] = { TTYCODE_STRING, "is1" }, |
99 |
|
|
[TTYC_IS2] = { TTYCODE_STRING, "is2" }, |
100 |
|
|
[TTYC_IS3] = { TTYCODE_STRING, "is3" }, |
101 |
|
|
[TTYC_KCBT] = { TTYCODE_STRING, "kcbt" }, |
102 |
|
|
[TTYC_KCUB1] = { TTYCODE_STRING, "kcub1" }, |
103 |
|
|
[TTYC_KCUD1] = { TTYCODE_STRING, "kcud1" }, |
104 |
|
|
[TTYC_KCUF1] = { TTYCODE_STRING, "kcuf1" }, |
105 |
|
|
[TTYC_KCUU1] = { TTYCODE_STRING, "kcuu1" }, |
106 |
|
|
[TTYC_KDC2] = { TTYCODE_STRING, "kDC" }, |
107 |
|
|
[TTYC_KDC3] = { TTYCODE_STRING, "kDC3" }, |
108 |
|
|
[TTYC_KDC4] = { TTYCODE_STRING, "kDC4" }, |
109 |
|
|
[TTYC_KDC5] = { TTYCODE_STRING, "kDC5" }, |
110 |
|
|
[TTYC_KDC6] = { TTYCODE_STRING, "kDC6" }, |
111 |
|
|
[TTYC_KDC7] = { TTYCODE_STRING, "kDC7" }, |
112 |
|
|
[TTYC_KDCH1] = { TTYCODE_STRING, "kdch1" }, |
113 |
|
|
[TTYC_KDN2] = { TTYCODE_STRING, "kDN" }, |
114 |
|
|
[TTYC_KDN3] = { TTYCODE_STRING, "kDN3" }, |
115 |
|
|
[TTYC_KDN4] = { TTYCODE_STRING, "kDN4" }, |
116 |
|
|
[TTYC_KDN5] = { TTYCODE_STRING, "kDN5" }, |
117 |
|
|
[TTYC_KDN6] = { TTYCODE_STRING, "kDN6" }, |
118 |
|
|
[TTYC_KDN7] = { TTYCODE_STRING, "kDN7" }, |
119 |
|
|
[TTYC_KEND] = { TTYCODE_STRING, "kend" }, |
120 |
|
|
[TTYC_KEND2] = { TTYCODE_STRING, "kEND" }, |
121 |
|
|
[TTYC_KEND3] = { TTYCODE_STRING, "kEND3" }, |
122 |
|
|
[TTYC_KEND4] = { TTYCODE_STRING, "kEND4" }, |
123 |
|
|
[TTYC_KEND5] = { TTYCODE_STRING, "kEND5" }, |
124 |
|
|
[TTYC_KEND6] = { TTYCODE_STRING, "kEND6" }, |
125 |
|
|
[TTYC_KEND7] = { TTYCODE_STRING, "kEND7" }, |
126 |
|
|
[TTYC_KF1] = { TTYCODE_STRING, "kf1" }, |
127 |
|
|
[TTYC_KF10] = { TTYCODE_STRING, "kf10" }, |
128 |
|
|
[TTYC_KF11] = { TTYCODE_STRING, "kf11" }, |
129 |
|
|
[TTYC_KF12] = { TTYCODE_STRING, "kf12" }, |
130 |
|
|
[TTYC_KF13] = { TTYCODE_STRING, "kf13" }, |
131 |
|
|
[TTYC_KF14] = { TTYCODE_STRING, "kf14" }, |
132 |
|
|
[TTYC_KF15] = { TTYCODE_STRING, "kf15" }, |
133 |
|
|
[TTYC_KF16] = { TTYCODE_STRING, "kf16" }, |
134 |
|
|
[TTYC_KF17] = { TTYCODE_STRING, "kf17" }, |
135 |
|
|
[TTYC_KF18] = { TTYCODE_STRING, "kf18" }, |
136 |
|
|
[TTYC_KF19] = { TTYCODE_STRING, "kf19" }, |
137 |
|
|
[TTYC_KF2] = { TTYCODE_STRING, "kf2" }, |
138 |
|
|
[TTYC_KF20] = { TTYCODE_STRING, "kf20" }, |
139 |
|
|
[TTYC_KF21] = { TTYCODE_STRING, "kf21" }, |
140 |
|
|
[TTYC_KF22] = { TTYCODE_STRING, "kf22" }, |
141 |
|
|
[TTYC_KF23] = { TTYCODE_STRING, "kf23" }, |
142 |
|
|
[TTYC_KF24] = { TTYCODE_STRING, "kf24" }, |
143 |
|
|
[TTYC_KF25] = { TTYCODE_STRING, "kf25" }, |
144 |
|
|
[TTYC_KF26] = { TTYCODE_STRING, "kf26" }, |
145 |
|
|
[TTYC_KF27] = { TTYCODE_STRING, "kf27" }, |
146 |
|
|
[TTYC_KF28] = { TTYCODE_STRING, "kf28" }, |
147 |
|
|
[TTYC_KF29] = { TTYCODE_STRING, "kf29" }, |
148 |
|
|
[TTYC_KF3] = { TTYCODE_STRING, "kf3" }, |
149 |
|
|
[TTYC_KF30] = { TTYCODE_STRING, "kf30" }, |
150 |
|
|
[TTYC_KF31] = { TTYCODE_STRING, "kf31" }, |
151 |
|
|
[TTYC_KF32] = { TTYCODE_STRING, "kf32" }, |
152 |
|
|
[TTYC_KF33] = { TTYCODE_STRING, "kf33" }, |
153 |
|
|
[TTYC_KF34] = { TTYCODE_STRING, "kf34" }, |
154 |
|
|
[TTYC_KF35] = { TTYCODE_STRING, "kf35" }, |
155 |
|
|
[TTYC_KF36] = { TTYCODE_STRING, "kf36" }, |
156 |
|
|
[TTYC_KF37] = { TTYCODE_STRING, "kf37" }, |
157 |
|
|
[TTYC_KF38] = { TTYCODE_STRING, "kf38" }, |
158 |
|
|
[TTYC_KF39] = { TTYCODE_STRING, "kf39" }, |
159 |
|
|
[TTYC_KF4] = { TTYCODE_STRING, "kf4" }, |
160 |
|
|
[TTYC_KF40] = { TTYCODE_STRING, "kf40" }, |
161 |
|
|
[TTYC_KF41] = { TTYCODE_STRING, "kf41" }, |
162 |
|
|
[TTYC_KF42] = { TTYCODE_STRING, "kf42" }, |
163 |
|
|
[TTYC_KF43] = { TTYCODE_STRING, "kf43" }, |
164 |
|
|
[TTYC_KF44] = { TTYCODE_STRING, "kf44" }, |
165 |
|
|
[TTYC_KF45] = { TTYCODE_STRING, "kf45" }, |
166 |
|
|
[TTYC_KF46] = { TTYCODE_STRING, "kf46" }, |
167 |
|
|
[TTYC_KF47] = { TTYCODE_STRING, "kf47" }, |
168 |
|
|
[TTYC_KF48] = { TTYCODE_STRING, "kf48" }, |
169 |
|
|
[TTYC_KF49] = { TTYCODE_STRING, "kf49" }, |
170 |
|
|
[TTYC_KF5] = { TTYCODE_STRING, "kf5" }, |
171 |
|
|
[TTYC_KF50] = { TTYCODE_STRING, "kf50" }, |
172 |
|
|
[TTYC_KF51] = { TTYCODE_STRING, "kf51" }, |
173 |
|
|
[TTYC_KF52] = { TTYCODE_STRING, "kf52" }, |
174 |
|
|
[TTYC_KF53] = { TTYCODE_STRING, "kf53" }, |
175 |
|
|
[TTYC_KF54] = { TTYCODE_STRING, "kf54" }, |
176 |
|
|
[TTYC_KF55] = { TTYCODE_STRING, "kf55" }, |
177 |
|
|
[TTYC_KF56] = { TTYCODE_STRING, "kf56" }, |
178 |
|
|
[TTYC_KF57] = { TTYCODE_STRING, "kf57" }, |
179 |
|
|
[TTYC_KF58] = { TTYCODE_STRING, "kf58" }, |
180 |
|
|
[TTYC_KF59] = { TTYCODE_STRING, "kf59" }, |
181 |
|
|
[TTYC_KF6] = { TTYCODE_STRING, "kf6" }, |
182 |
|
|
[TTYC_KF60] = { TTYCODE_STRING, "kf60" }, |
183 |
|
|
[TTYC_KF61] = { TTYCODE_STRING, "kf61" }, |
184 |
|
|
[TTYC_KF62] = { TTYCODE_STRING, "kf62" }, |
185 |
|
|
[TTYC_KF63] = { TTYCODE_STRING, "kf63" }, |
186 |
|
|
[TTYC_KF7] = { TTYCODE_STRING, "kf7" }, |
187 |
|
|
[TTYC_KF8] = { TTYCODE_STRING, "kf8" }, |
188 |
|
|
[TTYC_KF9] = { TTYCODE_STRING, "kf9" }, |
189 |
|
|
[TTYC_KHOM2] = { TTYCODE_STRING, "kHOM" }, |
190 |
|
|
[TTYC_KHOM3] = { TTYCODE_STRING, "kHOM3" }, |
191 |
|
|
[TTYC_KHOM4] = { TTYCODE_STRING, "kHOM4" }, |
192 |
|
|
[TTYC_KHOM5] = { TTYCODE_STRING, "kHOM5" }, |
193 |
|
|
[TTYC_KHOM6] = { TTYCODE_STRING, "kHOM6" }, |
194 |
|
|
[TTYC_KHOM7] = { TTYCODE_STRING, "kHOM7" }, |
195 |
|
|
[TTYC_KHOME] = { TTYCODE_STRING, "khome" }, |
196 |
|
|
[TTYC_KIC2] = { TTYCODE_STRING, "kIC" }, |
197 |
|
|
[TTYC_KIC3] = { TTYCODE_STRING, "kIC3" }, |
198 |
|
|
[TTYC_KIC4] = { TTYCODE_STRING, "kIC4" }, |
199 |
|
|
[TTYC_KIC5] = { TTYCODE_STRING, "kIC5" }, |
200 |
|
|
[TTYC_KIC6] = { TTYCODE_STRING, "kIC6" }, |
201 |
|
|
[TTYC_KIC7] = { TTYCODE_STRING, "kIC7" }, |
202 |
|
|
[TTYC_KICH1] = { TTYCODE_STRING, "kich1" }, |
203 |
|
|
[TTYC_KLFT2] = { TTYCODE_STRING, "kLFT" }, |
204 |
|
|
[TTYC_KLFT3] = { TTYCODE_STRING, "kLFT3" }, |
205 |
|
|
[TTYC_KLFT4] = { TTYCODE_STRING, "kLFT4" }, |
206 |
|
|
[TTYC_KLFT5] = { TTYCODE_STRING, "kLFT5" }, |
207 |
|
|
[TTYC_KLFT6] = { TTYCODE_STRING, "kLFT6" }, |
208 |
|
|
[TTYC_KLFT7] = { TTYCODE_STRING, "kLFT7" }, |
209 |
|
|
[TTYC_KMOUS] = { TTYCODE_STRING, "kmous" }, |
210 |
|
|
[TTYC_KNP] = { TTYCODE_STRING, "knp" }, |
211 |
|
|
[TTYC_KNXT2] = { TTYCODE_STRING, "kNXT" }, |
212 |
|
|
[TTYC_KNXT3] = { TTYCODE_STRING, "kNXT3" }, |
213 |
|
|
[TTYC_KNXT4] = { TTYCODE_STRING, "kNXT4" }, |
214 |
|
|
[TTYC_KNXT5] = { TTYCODE_STRING, "kNXT5" }, |
215 |
|
|
[TTYC_KNXT6] = { TTYCODE_STRING, "kNXT6" }, |
216 |
|
|
[TTYC_KNXT7] = { TTYCODE_STRING, "kNXT7" }, |
217 |
|
|
[TTYC_KPP] = { TTYCODE_STRING, "kpp" }, |
218 |
|
|
[TTYC_KPRV2] = { TTYCODE_STRING, "kPRV" }, |
219 |
|
|
[TTYC_KPRV3] = { TTYCODE_STRING, "kPRV3" }, |
220 |
|
|
[TTYC_KPRV4] = { TTYCODE_STRING, "kPRV4" }, |
221 |
|
|
[TTYC_KPRV5] = { TTYCODE_STRING, "kPRV5" }, |
222 |
|
|
[TTYC_KPRV6] = { TTYCODE_STRING, "kPRV6" }, |
223 |
|
|
[TTYC_KPRV7] = { TTYCODE_STRING, "kPRV7" }, |
224 |
|
|
[TTYC_KRIT2] = { TTYCODE_STRING, "kRIT" }, |
225 |
|
|
[TTYC_KRIT3] = { TTYCODE_STRING, "kRIT3" }, |
226 |
|
|
[TTYC_KRIT4] = { TTYCODE_STRING, "kRIT4" }, |
227 |
|
|
[TTYC_KRIT5] = { TTYCODE_STRING, "kRIT5" }, |
228 |
|
|
[TTYC_KRIT6] = { TTYCODE_STRING, "kRIT6" }, |
229 |
|
|
[TTYC_KRIT7] = { TTYCODE_STRING, "kRIT7" }, |
230 |
|
|
[TTYC_KUP2] = { TTYCODE_STRING, "kUP" }, |
231 |
|
|
[TTYC_KUP3] = { TTYCODE_STRING, "kUP3" }, |
232 |
|
|
[TTYC_KUP4] = { TTYCODE_STRING, "kUP4" }, |
233 |
|
|
[TTYC_KUP5] = { TTYCODE_STRING, "kUP5" }, |
234 |
|
|
[TTYC_KUP6] = { TTYCODE_STRING, "kUP6" }, |
235 |
|
|
[TTYC_KUP7] = { TTYCODE_STRING, "kUP7" }, |
236 |
|
|
[TTYC_MS] = { TTYCODE_STRING, "Ms" }, |
237 |
|
|
[TTYC_OP] = { TTYCODE_STRING, "op" }, |
238 |
|
|
[TTYC_REV] = { TTYCODE_STRING, "rev" }, |
239 |
|
|
[TTYC_RI] = { TTYCODE_STRING, "ri" }, |
240 |
|
|
[TTYC_RMACS] = { TTYCODE_STRING, "rmacs" }, |
241 |
|
|
[TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" }, |
242 |
|
|
[TTYC_RMKX] = { TTYCODE_STRING, "rmkx" }, |
243 |
|
|
[TTYC_SE] = { TTYCODE_STRING, "Se" }, |
244 |
|
|
[TTYC_SETAB] = { TTYCODE_STRING, "setab" }, |
245 |
|
|
[TTYC_SETAF] = { TTYCODE_STRING, "setaf" }, |
246 |
|
|
[TTYC_SGR0] = { TTYCODE_STRING, "sgr0" }, |
247 |
|
|
[TTYC_SITM] = { TTYCODE_STRING, "sitm" }, |
248 |
|
|
[TTYC_SMACS] = { TTYCODE_STRING, "smacs" }, |
249 |
|
|
[TTYC_SMCUP] = { TTYCODE_STRING, "smcup" }, |
250 |
|
|
[TTYC_SMKX] = { TTYCODE_STRING, "smkx" }, |
251 |
|
|
[TTYC_SMSO] = { TTYCODE_STRING, "smso" }, |
252 |
|
|
[TTYC_SMUL] = { TTYCODE_STRING, "smul" }, |
253 |
|
|
[TTYC_SS] = { TTYCODE_STRING, "Ss" }, |
254 |
|
|
[TTYC_TC] = { TTYCODE_FLAG, "Tc" }, |
255 |
|
|
[TTYC_TSL] = { TTYCODE_STRING, "tsl" }, |
256 |
|
|
[TTYC_VPA] = { TTYCODE_STRING, "vpa" }, |
257 |
|
|
[TTYC_XENL] = { TTYCODE_FLAG, "xenl" }, |
258 |
|
|
[TTYC_XT] = { TTYCODE_FLAG, "XT" }, |
259 |
|
|
}; |
260 |
|
|
|
261 |
|
|
u_int |
262 |
|
|
tty_term_ncodes(void) |
263 |
|
|
{ |
264 |
|
|
return (nitems(tty_term_codes)); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
char * |
268 |
|
|
tty_term_strip(const char *s) |
269 |
|
|
{ |
270 |
|
|
const char *ptr; |
271 |
|
|
static char buf[BUFSIZ]; |
272 |
|
|
size_t len; |
273 |
|
|
|
274 |
|
|
/* Ignore strings with no padding. */ |
275 |
|
|
if (strchr(s, '$') == NULL) |
276 |
|
|
return (xstrdup(s)); |
277 |
|
|
|
278 |
|
|
len = 0; |
279 |
|
|
for (ptr = s; *ptr != '\0'; ptr++) { |
280 |
|
|
if (*ptr == '$' && *(ptr + 1) == '<') { |
281 |
|
|
while (*ptr != '\0' && *ptr != '>') |
282 |
|
|
ptr++; |
283 |
|
|
if (*ptr == '>') |
284 |
|
|
ptr++; |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
buf[len++] = *ptr; |
288 |
|
|
if (len == (sizeof buf) - 1) |
289 |
|
|
break; |
290 |
|
|
} |
291 |
|
|
buf[len] = '\0'; |
292 |
|
|
|
293 |
|
|
return (xstrdup(buf)); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
void |
297 |
|
|
tty_term_override(struct tty_term *term, const char *overrides) |
298 |
|
|
{ |
299 |
|
|
const struct tty_term_code_entry *ent; |
300 |
|
|
struct tty_code *code; |
301 |
|
|
char *termnext, *termstr; |
302 |
|
|
char *entnext, *entstr; |
303 |
|
|
char *s, *ptr, *val; |
304 |
|
|
const char *errstr; |
305 |
|
|
u_int i; |
306 |
|
|
int n, removeflag; |
307 |
|
|
|
308 |
|
|
s = xstrdup(overrides); |
309 |
|
|
|
310 |
|
|
termnext = s; |
311 |
|
|
while ((termstr = strsep(&termnext, ",")) != NULL) { |
312 |
|
|
entnext = termstr; |
313 |
|
|
|
314 |
|
|
entstr = strsep(&entnext, ":"); |
315 |
|
|
if (entstr == NULL || entnext == NULL) |
316 |
|
|
continue; |
317 |
|
|
if (fnmatch(entstr, term->name, 0) != 0) |
318 |
|
|
continue; |
319 |
|
|
while ((entstr = strsep(&entnext, ":")) != NULL) { |
320 |
|
|
if (*entstr == '\0') |
321 |
|
|
continue; |
322 |
|
|
|
323 |
|
|
val = NULL; |
324 |
|
|
removeflag = 0; |
325 |
|
|
if ((ptr = strchr(entstr, '=')) != NULL) { |
326 |
|
|
*ptr++ = '\0'; |
327 |
|
|
val = xstrdup(ptr); |
328 |
|
|
if (strunvis(val, ptr) == -1) { |
329 |
|
|
free(val); |
330 |
|
|
val = xstrdup(ptr); |
331 |
|
|
} |
332 |
|
|
} else if (entstr[strlen(entstr) - 1] == '@') { |
333 |
|
|
entstr[strlen(entstr) - 1] = '\0'; |
334 |
|
|
removeflag = 1; |
335 |
|
|
} else |
336 |
|
|
val = xstrdup(""); |
337 |
|
|
|
338 |
|
|
log_debug("%s override: %s %s", |
339 |
|
|
term->name, entstr, removeflag ? "@" : val); |
340 |
|
|
for (i = 0; i < tty_term_ncodes(); i++) { |
341 |
|
|
ent = &tty_term_codes[i]; |
342 |
|
|
if (strcmp(entstr, ent->name) != 0) |
343 |
|
|
continue; |
344 |
|
|
code = &term->codes[i]; |
345 |
|
|
|
346 |
|
|
if (removeflag) { |
347 |
|
|
code->type = TTYCODE_NONE; |
348 |
|
|
continue; |
349 |
|
|
} |
350 |
|
|
switch (ent->type) { |
351 |
|
|
case TTYCODE_NONE: |
352 |
|
|
break; |
353 |
|
|
case TTYCODE_STRING: |
354 |
|
|
if (code->type == TTYCODE_STRING) |
355 |
|
|
free(code->value.string); |
356 |
|
|
code->value.string = xstrdup(val); |
357 |
|
|
code->type = ent->type; |
358 |
|
|
break; |
359 |
|
|
case TTYCODE_NUMBER: |
360 |
|
|
n = strtonum(val, 0, INT_MAX, &errstr); |
361 |
|
|
if (errstr != NULL) |
362 |
|
|
break; |
363 |
|
|
code->value.number = n; |
364 |
|
|
code->type = ent->type; |
365 |
|
|
break; |
366 |
|
|
case TTYCODE_FLAG: |
367 |
|
|
code->value.flag = 1; |
368 |
|
|
code->type = ent->type; |
369 |
|
|
break; |
370 |
|
|
} |
371 |
|
|
} |
372 |
|
|
|
373 |
|
|
free(val); |
374 |
|
|
} |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
free(s); |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
struct tty_term * |
381 |
|
|
tty_term_find(char *name, int fd, char **cause) |
382 |
|
|
{ |
383 |
|
|
struct tty_term *term; |
384 |
|
|
const struct tty_term_code_entry *ent; |
385 |
|
|
struct tty_code *code; |
386 |
|
|
u_int i; |
387 |
|
|
int n, error; |
388 |
|
|
char *s; |
389 |
|
|
const char *acs; |
390 |
|
|
|
391 |
|
|
LIST_FOREACH(term, &tty_terms, entry) { |
392 |
|
|
if (strcmp(term->name, name) == 0) { |
393 |
|
|
term->references++; |
394 |
|
|
return (term); |
395 |
|
|
} |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
log_debug("new term: %s", name); |
399 |
|
|
term = xmalloc(sizeof *term); |
400 |
|
|
term->name = xstrdup(name); |
401 |
|
|
term->references = 1; |
402 |
|
|
term->flags = 0; |
403 |
|
|
term->codes = xcalloc (tty_term_ncodes(), sizeof *term->codes); |
404 |
|
|
LIST_INSERT_HEAD(&tty_terms, term, entry); |
405 |
|
|
|
406 |
|
|
/* Set up curses terminal. */ |
407 |
|
|
if (setupterm(name, fd, &error) != OK) { |
408 |
|
|
switch (error) { |
409 |
|
|
case 1: |
410 |
|
|
xasprintf(cause, "can't use hardcopy terminal: %s", |
411 |
|
|
name); |
412 |
|
|
break; |
413 |
|
|
case 0: |
414 |
|
|
xasprintf(cause, "missing or unsuitable terminal: %s", |
415 |
|
|
name); |
416 |
|
|
break; |
417 |
|
|
case -1: |
418 |
|
|
xasprintf(cause, "can't find terminfo database"); |
419 |
|
|
break; |
420 |
|
|
default: |
421 |
|
|
xasprintf(cause, "unknown error"); |
422 |
|
|
break; |
423 |
|
|
} |
424 |
|
|
goto error; |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
/* Fill in codes. */ |
428 |
|
|
for (i = 0; i < tty_term_ncodes(); i++) { |
429 |
|
|
ent = &tty_term_codes[i]; |
430 |
|
|
|
431 |
|
|
code = &term->codes[i]; |
432 |
|
|
code->type = TTYCODE_NONE; |
433 |
|
|
switch (ent->type) { |
434 |
|
|
case TTYCODE_NONE: |
435 |
|
|
break; |
436 |
|
|
case TTYCODE_STRING: |
437 |
|
|
s = tigetstr((char *) ent->name); |
438 |
|
|
if (s == NULL || s == (char *) -1) |
439 |
|
|
break; |
440 |
|
|
code->type = TTYCODE_STRING; |
441 |
|
|
code->value.string = tty_term_strip(s); |
442 |
|
|
break; |
443 |
|
|
case TTYCODE_NUMBER: |
444 |
|
|
n = tigetnum((char *) ent->name); |
445 |
|
|
if (n == -1 || n == -2) |
446 |
|
|
break; |
447 |
|
|
code->type = TTYCODE_NUMBER; |
448 |
|
|
code->value.number = n; |
449 |
|
|
break; |
450 |
|
|
case TTYCODE_FLAG: |
451 |
|
|
n = tigetflag((char *) ent->name); |
452 |
|
|
if (n == -1) |
453 |
|
|
break; |
454 |
|
|
code->type = TTYCODE_FLAG; |
455 |
|
|
code->value.flag = n; |
456 |
|
|
break; |
457 |
|
|
} |
458 |
|
|
} |
459 |
|
|
|
460 |
|
|
/* Apply terminal overrides. */ |
461 |
|
|
s = options_get_string(global_options, "terminal-overrides"); |
462 |
|
|
tty_term_override(term, s); |
463 |
|
|
|
464 |
|
|
/* Delete curses data. */ |
465 |
|
|
del_curterm(cur_term); |
466 |
|
|
|
467 |
|
|
/* These are always required. */ |
468 |
|
|
if (!tty_term_has(term, TTYC_CLEAR)) { |
469 |
|
|
xasprintf(cause, "terminal does not support clear"); |
470 |
|
|
goto error; |
471 |
|
|
} |
472 |
|
|
if (!tty_term_has(term, TTYC_CUP)) { |
473 |
|
|
xasprintf(cause, "terminal does not support cup"); |
474 |
|
|
goto error; |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
/* These can be emulated so one of the two is required. */ |
478 |
|
|
if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) { |
479 |
|
|
xasprintf(cause, "terminal does not support cud1 or cud"); |
480 |
|
|
goto error; |
481 |
|
|
} |
482 |
|
|
|
483 |
|
|
/* Figure out if we have 256. */ |
484 |
|
|
if (tty_term_number(term, TTYC_COLORS) == 256) |
485 |
|
|
term->flags |= TERM_256COLOURS; |
486 |
|
|
|
487 |
|
|
/* |
488 |
|
|
* Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1 |
489 |
|
|
* rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1). |
490 |
|
|
* |
491 |
|
|
* This is irritating, most notably because it is impossible to write |
492 |
|
|
* to the very bottom-right of the screen without scrolling. |
493 |
|
|
* |
494 |
|
|
* Flag the terminal here and apply some workarounds in other places to |
495 |
|
|
* do the best possible. |
496 |
|
|
*/ |
497 |
|
|
if (!tty_term_flag(term, TTYC_XENL)) |
498 |
|
|
term->flags |= TERM_EARLYWRAP; |
499 |
|
|
|
500 |
|
|
/* Generate ACS table. If none is present, use nearest ASCII. */ |
501 |
|
|
memset(term->acs, 0, sizeof term->acs); |
502 |
|
|
if (tty_term_has(term, TTYC_ACSC)) |
503 |
|
|
acs = tty_term_string(term, TTYC_ACSC); |
504 |
|
|
else |
505 |
|
|
acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~."; |
506 |
|
|
for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2) |
507 |
|
|
term->acs[(u_char) acs[0]][0] = acs[1]; |
508 |
|
|
|
509 |
|
|
/* On terminals with xterm titles (XT), fill in tsl and fsl. */ |
510 |
|
|
if (tty_term_flag(term, TTYC_XT) && |
511 |
|
|
!tty_term_has(term, TTYC_TSL) && |
512 |
|
|
!tty_term_has(term, TTYC_FSL)) { |
513 |
|
|
code = &term->codes[TTYC_TSL]; |
514 |
|
|
code->value.string = xstrdup("\033]0;"); |
515 |
|
|
code->type = TTYCODE_STRING; |
516 |
|
|
code = &term->codes[TTYC_FSL]; |
517 |
|
|
code->value.string = xstrdup("\007"); |
518 |
|
|
code->type = TTYCODE_STRING; |
519 |
|
|
} |
520 |
|
|
|
521 |
|
|
return (term); |
522 |
|
|
|
523 |
|
|
error: |
524 |
|
|
tty_term_free(term); |
525 |
|
|
return (NULL); |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
void |
529 |
|
|
tty_term_free(struct tty_term *term) |
530 |
|
|
{ |
531 |
|
|
u_int i; |
532 |
|
|
|
533 |
|
|
if (--term->references != 0) |
534 |
|
|
return; |
535 |
|
|
|
536 |
|
|
LIST_REMOVE(term, entry); |
537 |
|
|
|
538 |
|
|
for (i = 0; i < tty_term_ncodes(); i++) { |
539 |
|
|
if (term->codes[i].type == TTYCODE_STRING) |
540 |
|
|
free(term->codes[i].value.string); |
541 |
|
|
} |
542 |
|
|
free(term->codes); |
543 |
|
|
|
544 |
|
|
free(term->name); |
545 |
|
|
free(term); |
546 |
|
|
} |
547 |
|
|
|
548 |
|
|
int |
549 |
|
|
tty_term_has(struct tty_term *term, enum tty_code_code code) |
550 |
|
|
{ |
551 |
|
|
return (term->codes[code].type != TTYCODE_NONE); |
552 |
|
|
} |
553 |
|
|
|
554 |
|
|
const char * |
555 |
|
|
tty_term_string(struct tty_term *term, enum tty_code_code code) |
556 |
|
|
{ |
557 |
|
|
if (!tty_term_has(term, code)) |
558 |
|
|
return (""); |
559 |
|
|
if (term->codes[code].type != TTYCODE_STRING) |
560 |
|
|
fatalx("not a string: %d", code); |
561 |
|
|
return (term->codes[code].value.string); |
562 |
|
|
} |
563 |
|
|
|
564 |
|
|
const char * |
565 |
|
|
tty_term_string1(struct tty_term *term, enum tty_code_code code, int a) |
566 |
|
|
{ |
567 |
|
|
return (tparm((char *) tty_term_string(term, code), a)); |
568 |
|
|
} |
569 |
|
|
|
570 |
|
|
const char * |
571 |
|
|
tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b) |
572 |
|
|
{ |
573 |
|
|
return (tparm((char *) tty_term_string(term, code), a, b)); |
574 |
|
|
} |
575 |
|
|
|
576 |
|
|
const char * |
577 |
|
|
tty_term_ptr1(struct tty_term *term, enum tty_code_code code, const void *a) |
578 |
|
|
{ |
579 |
|
|
return (tparm((char *) tty_term_string(term, code), a)); |
580 |
|
|
} |
581 |
|
|
|
582 |
|
|
const char * |
583 |
|
|
tty_term_ptr2(struct tty_term *term, enum tty_code_code code, const void *a, |
584 |
|
|
const void *b) |
585 |
|
|
{ |
586 |
|
|
return (tparm((char *) tty_term_string(term, code), a, b)); |
587 |
|
|
} |
588 |
|
|
|
589 |
|
|
int |
590 |
|
|
tty_term_number(struct tty_term *term, enum tty_code_code code) |
591 |
|
|
{ |
592 |
|
|
if (!tty_term_has(term, code)) |
593 |
|
|
return (0); |
594 |
|
|
if (term->codes[code].type != TTYCODE_NUMBER) |
595 |
|
|
fatalx("not a number: %d", code); |
596 |
|
|
return (term->codes[code].value.number); |
597 |
|
|
} |
598 |
|
|
|
599 |
|
|
int |
600 |
|
|
tty_term_flag(struct tty_term *term, enum tty_code_code code) |
601 |
|
|
{ |
602 |
|
|
if (!tty_term_has(term, code)) |
603 |
|
|
return (0); |
604 |
|
|
if (term->codes[code].type != TTYCODE_FLAG) |
605 |
|
|
fatalx("not a flag: %d", code); |
606 |
|
|
return (term->codes[code].value.flag); |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
const char * |
610 |
|
|
tty_term_describe(struct tty_term *term, enum tty_code_code code) |
611 |
|
|
{ |
612 |
|
|
static char s[256]; |
613 |
|
|
char out[128]; |
614 |
|
|
|
615 |
|
|
switch (term->codes[code].type) { |
616 |
|
|
case TTYCODE_NONE: |
617 |
|
|
xsnprintf(s, sizeof s, "%4u: %s: [missing]", |
618 |
|
|
code, tty_term_codes[code].name); |
619 |
|
|
break; |
620 |
|
|
case TTYCODE_STRING: |
621 |
|
|
strnvis(out, term->codes[code].value.string, sizeof out, |
622 |
|
|
VIS_OCTAL|VIS_TAB|VIS_NL); |
623 |
|
|
xsnprintf(s, sizeof s, "%4u: %s: (string) %s", |
624 |
|
|
code, tty_term_codes[code].name, |
625 |
|
|
out); |
626 |
|
|
break; |
627 |
|
|
case TTYCODE_NUMBER: |
628 |
|
|
xsnprintf(s, sizeof s, "%4u: %s: (number) %d", |
629 |
|
|
code, tty_term_codes[code].name, |
630 |
|
|
term->codes[code].value.number); |
631 |
|
|
break; |
632 |
|
|
case TTYCODE_FLAG: |
633 |
|
|
xsnprintf(s, sizeof s, "%4u: %s: (flag) %s", |
634 |
|
|
code, tty_term_codes[code].name, |
635 |
|
|
term->codes[code].value.flag ? "true" : "false"); |
636 |
|
|
break; |
637 |
|
|
} |
638 |
|
|
return (s); |
639 |
|
|
} |