Coverage Report

Created: 2026-09-03 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/tty-term.c
Line
Count
Source
1
/* $OpenBSD: tty-term.c,v 1.109 2026/08/25 08:37:08 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
#if defined(HAVE_CURSES_H)
22
#include <curses.h>
23
#elif defined(HAVE_NCURSES_H)
24
#include <ncurses.h>
25
#endif
26
#include <fnmatch.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <term.h>
30
31
#include "tmux.h"
32
33
static char *tty_term_strip(const char *);
34
static void  tty_term_validate(struct tty_term *);
35
36
struct tty_terms tty_terms = LIST_HEAD_INITIALIZER(tty_terms);
37
38
enum tty_code_type {
39
  TTYCODE_NONE = 0,
40
  TTYCODE_STRING,
41
  TTYCODE_NUMBER,
42
  TTYCODE_FLAG,
43
};
44
45
struct tty_code {
46
  enum tty_code_type  type;
47
  union {
48
    char         *string;
49
    int   number;
50
    int   flag;
51
  } value;
52
};
53
54
struct tty_term_code_entry {
55
  enum tty_code_type  type;
56
  const char         *name;
57
};
58
59
static const struct tty_term_code_entry tty_term_codes[] = {
60
  [TTYC_ACSC] = { TTYCODE_STRING, "acsc" },
61
  [TTYC_AM] = { TTYCODE_FLAG, "am" },
62
  [TTYC_AX] = { TTYCODE_FLAG, "AX" },
63
  [TTYC_BCE] = { TTYCODE_FLAG, "bce" },
64
  [TTYC_BEL] = { TTYCODE_STRING, "bel" },
65
  [TTYC_BIDI] = { TTYCODE_STRING, "Bidi" },
66
  [TTYC_BLINK] = { TTYCODE_STRING, "blink" },
67
  [TTYC_BOLD] = { TTYCODE_STRING, "bold" },
68
  [TTYC_CIVIS] = { TTYCODE_STRING, "civis" },
69
  [TTYC_CLEAR] = { TTYCODE_STRING, "clear" },
70
  [TTYC_CLMG] = { TTYCODE_STRING, "Clmg" },
71
  [TTYC_CMG] = { TTYCODE_STRING, "Cmg" },
72
  [TTYC_CNORM] = { TTYCODE_STRING, "cnorm" },
73
  [TTYC_COLORS] = { TTYCODE_NUMBER, "colors" },
74
  [TTYC_CR] = { TTYCODE_STRING, "Cr" },
75
  [TTYC_CSR] = { TTYCODE_STRING, "csr" },
76
  [TTYC_CS] = { TTYCODE_STRING, "Cs" },
77
  [TTYC_CUB1] = { TTYCODE_STRING, "cub1" },
78
  [TTYC_CUB] = { TTYCODE_STRING, "cub" },
79
  [TTYC_CUD1] = { TTYCODE_STRING, "cud1" },
80
  [TTYC_CUD] = { TTYCODE_STRING, "cud" },
81
  [TTYC_CUF1] = { TTYCODE_STRING, "cuf1" },
82
  [TTYC_CUF] = { TTYCODE_STRING, "cuf" },
83
  [TTYC_CUP] = { TTYCODE_STRING, "cup" },
84
  [TTYC_CUU1] = { TTYCODE_STRING, "cuu1" },
85
  [TTYC_CUU] = { TTYCODE_STRING, "cuu" },
86
  [TTYC_CVVIS] = { TTYCODE_STRING, "cvvis" },
87
  [TTYC_DCH1] = { TTYCODE_STRING, "dch1" },
88
  [TTYC_DCH] = { TTYCODE_STRING, "dch" },
89
  [TTYC_DIM] = { TTYCODE_STRING, "dim" },
90
  [TTYC_DL1] = { TTYCODE_STRING, "dl1" },
91
  [TTYC_DL] = { TTYCODE_STRING, "dl" },
92
  [TTYC_DSEKS] = { TTYCODE_STRING, "Dseks" },
93
  [TTYC_DSFCS] = { TTYCODE_STRING, "Dsfcs" },
94
  [TTYC_DSBP] = { TTYCODE_STRING, "Dsbp" },
95
  [TTYC_DSMG] = { TTYCODE_STRING, "Dsmg" },
96
  [TTYC_E3] = { TTYCODE_STRING, "E3" },
97
  [TTYC_ECH] = { TTYCODE_STRING, "ech" },
98
  [TTYC_ED] = { TTYCODE_STRING, "ed" },
99
  [TTYC_EL1] = { TTYCODE_STRING, "el1" },
100
  [TTYC_EL] = { TTYCODE_STRING, "el" },
101
  [TTYC_ENACS] = { TTYCODE_STRING, "enacs" },
102
  [TTYC_ENBP] = { TTYCODE_STRING, "Enbp" },
103
  [TTYC_ENEKS] = { TTYCODE_STRING, "Eneks" },
104
  [TTYC_ENFCS] = { TTYCODE_STRING, "Enfcs" },
105
  [TTYC_ENMG] = { TTYCODE_STRING, "Enmg" },
106
  [TTYC_FSL] = { TTYCODE_STRING, "fsl" },
107
  [TTYC_HLS] = { TTYCODE_STRING, "Hls" },
108
  [TTYC_HOME] = { TTYCODE_STRING, "home" },
109
  [TTYC_HPA] = { TTYCODE_STRING, "hpa" },
110
  [TTYC_ICH1] = { TTYCODE_STRING, "ich1" },
111
  [TTYC_ICH] = { TTYCODE_STRING, "ich" },
112
  [TTYC_IL1] = { TTYCODE_STRING, "il1" },
113
  [TTYC_IL] = { TTYCODE_STRING, "il" },
114
  [TTYC_IND] = { TTYCODE_STRING, "ind" },
115
  [TTYC_INDN] = { TTYCODE_STRING, "indn" },
116
  [TTYC_INVIS] = { TTYCODE_STRING, "invis" },
117
  [TTYC_KCBT] = { TTYCODE_STRING, "kcbt" },
118
  [TTYC_KCUB1] = { TTYCODE_STRING, "kcub1" },
119
  [TTYC_KCUD1] = { TTYCODE_STRING, "kcud1" },
120
  [TTYC_KCUF1] = { TTYCODE_STRING, "kcuf1" },
121
  [TTYC_KCUU1] = { TTYCODE_STRING, "kcuu1" },
122
  [TTYC_KDC2] = { TTYCODE_STRING, "kDC" },
123
  [TTYC_KDC3] = { TTYCODE_STRING, "kDC3" },
124
  [TTYC_KDC4] = { TTYCODE_STRING, "kDC4" },
125
  [TTYC_KDC5] = { TTYCODE_STRING, "kDC5" },
126
  [TTYC_KDC6] = { TTYCODE_STRING, "kDC6" },
127
  [TTYC_KDC7] = { TTYCODE_STRING, "kDC7" },
128
  [TTYC_KDCH1] = { TTYCODE_STRING, "kdch1" },
129
  [TTYC_KDN2] = { TTYCODE_STRING, "kDN" }, /* not kDN2 */
130
  [TTYC_KDN3] = { TTYCODE_STRING, "kDN3" },
131
  [TTYC_KDN4] = { TTYCODE_STRING, "kDN4" },
132
  [TTYC_KDN5] = { TTYCODE_STRING, "kDN5" },
133
  [TTYC_KDN6] = { TTYCODE_STRING, "kDN6" },
134
  [TTYC_KDN7] = { TTYCODE_STRING, "kDN7" },
135
  [TTYC_KEND2] = { TTYCODE_STRING, "kEND" },
136
  [TTYC_KEND3] = { TTYCODE_STRING, "kEND3" },
137
  [TTYC_KEND4] = { TTYCODE_STRING, "kEND4" },
138
  [TTYC_KEND5] = { TTYCODE_STRING, "kEND5" },
139
  [TTYC_KEND6] = { TTYCODE_STRING, "kEND6" },
140
  [TTYC_KEND7] = { TTYCODE_STRING, "kEND7" },
141
  [TTYC_KEND] = { TTYCODE_STRING, "kend" },
142
  [TTYC_KF10] = { TTYCODE_STRING, "kf10" },
143
  [TTYC_KF11] = { TTYCODE_STRING, "kf11" },
144
  [TTYC_KF12] = { TTYCODE_STRING, "kf12" },
145
  [TTYC_KF13] = { TTYCODE_STRING, "kf13" },
146
  [TTYC_KF14] = { TTYCODE_STRING, "kf14" },
147
  [TTYC_KF15] = { TTYCODE_STRING, "kf15" },
148
  [TTYC_KF16] = { TTYCODE_STRING, "kf16" },
149
  [TTYC_KF17] = { TTYCODE_STRING, "kf17" },
150
  [TTYC_KF18] = { TTYCODE_STRING, "kf18" },
151
  [TTYC_KF19] = { TTYCODE_STRING, "kf19" },
152
  [TTYC_KF1] = { TTYCODE_STRING, "kf1" },
153
  [TTYC_KF20] = { TTYCODE_STRING, "kf20" },
154
  [TTYC_KF21] = { TTYCODE_STRING, "kf21" },
155
  [TTYC_KF22] = { TTYCODE_STRING, "kf22" },
156
  [TTYC_KF23] = { TTYCODE_STRING, "kf23" },
157
  [TTYC_KF24] = { TTYCODE_STRING, "kf24" },
158
  [TTYC_KF25] = { TTYCODE_STRING, "kf25" },
159
  [TTYC_KF26] = { TTYCODE_STRING, "kf26" },
160
  [TTYC_KF27] = { TTYCODE_STRING, "kf27" },
161
  [TTYC_KF28] = { TTYCODE_STRING, "kf28" },
162
  [TTYC_KF29] = { TTYCODE_STRING, "kf29" },
163
  [TTYC_KF2] = { TTYCODE_STRING, "kf2" },
164
  [TTYC_KF30] = { TTYCODE_STRING, "kf30" },
165
  [TTYC_KF31] = { TTYCODE_STRING, "kf31" },
166
  [TTYC_KF32] = { TTYCODE_STRING, "kf32" },
167
  [TTYC_KF33] = { TTYCODE_STRING, "kf33" },
168
  [TTYC_KF34] = { TTYCODE_STRING, "kf34" },
169
  [TTYC_KF35] = { TTYCODE_STRING, "kf35" },
170
  [TTYC_KF36] = { TTYCODE_STRING, "kf36" },
171
  [TTYC_KF37] = { TTYCODE_STRING, "kf37" },
172
  [TTYC_KF38] = { TTYCODE_STRING, "kf38" },
173
  [TTYC_KF39] = { TTYCODE_STRING, "kf39" },
174
  [TTYC_KF3] = { TTYCODE_STRING, "kf3" },
175
  [TTYC_KF40] = { TTYCODE_STRING, "kf40" },
176
  [TTYC_KF41] = { TTYCODE_STRING, "kf41" },
177
  [TTYC_KF42] = { TTYCODE_STRING, "kf42" },
178
  [TTYC_KF43] = { TTYCODE_STRING, "kf43" },
179
  [TTYC_KF44] = { TTYCODE_STRING, "kf44" },
180
  [TTYC_KF45] = { TTYCODE_STRING, "kf45" },
181
  [TTYC_KF46] = { TTYCODE_STRING, "kf46" },
182
  [TTYC_KF47] = { TTYCODE_STRING, "kf47" },
183
  [TTYC_KF48] = { TTYCODE_STRING, "kf48" },
184
  [TTYC_KF49] = { TTYCODE_STRING, "kf49" },
185
  [TTYC_KF4] = { TTYCODE_STRING, "kf4" },
186
  [TTYC_KF50] = { TTYCODE_STRING, "kf50" },
187
  [TTYC_KF51] = { TTYCODE_STRING, "kf51" },
188
  [TTYC_KF52] = { TTYCODE_STRING, "kf52" },
189
  [TTYC_KF53] = { TTYCODE_STRING, "kf53" },
190
  [TTYC_KF54] = { TTYCODE_STRING, "kf54" },
191
  [TTYC_KF55] = { TTYCODE_STRING, "kf55" },
192
  [TTYC_KF56] = { TTYCODE_STRING, "kf56" },
193
  [TTYC_KF57] = { TTYCODE_STRING, "kf57" },
194
  [TTYC_KF58] = { TTYCODE_STRING, "kf58" },
195
  [TTYC_KF59] = { TTYCODE_STRING, "kf59" },
196
  [TTYC_KF5] = { TTYCODE_STRING, "kf5" },
197
  [TTYC_KF60] = { TTYCODE_STRING, "kf60" },
198
  [TTYC_KF61] = { TTYCODE_STRING, "kf61" },
199
  [TTYC_KF62] = { TTYCODE_STRING, "kf62" },
200
  [TTYC_KF63] = { TTYCODE_STRING, "kf63" },
201
  [TTYC_KF6] = { TTYCODE_STRING, "kf6" },
202
  [TTYC_KF7] = { TTYCODE_STRING, "kf7" },
203
  [TTYC_KF8] = { TTYCODE_STRING, "kf8" },
204
  [TTYC_KF9] = { TTYCODE_STRING, "kf9" },
205
  [TTYC_KHOM2] = { TTYCODE_STRING, "kHOM" },
206
  [TTYC_KHOM3] = { TTYCODE_STRING, "kHOM3" },
207
  [TTYC_KHOM4] = { TTYCODE_STRING, "kHOM4" },
208
  [TTYC_KHOM5] = { TTYCODE_STRING, "kHOM5" },
209
  [TTYC_KHOM6] = { TTYCODE_STRING, "kHOM6" },
210
  [TTYC_KHOM7] = { TTYCODE_STRING, "kHOM7" },
211
  [TTYC_KHOME] = { TTYCODE_STRING, "khome" },
212
  [TTYC_KIC2] = { TTYCODE_STRING, "kIC" },
213
  [TTYC_KIC3] = { TTYCODE_STRING, "kIC3" },
214
  [TTYC_KIC4] = { TTYCODE_STRING, "kIC4" },
215
  [TTYC_KIC5] = { TTYCODE_STRING, "kIC5" },
216
  [TTYC_KIC6] = { TTYCODE_STRING, "kIC6" },
217
  [TTYC_KIC7] = { TTYCODE_STRING, "kIC7" },
218
  [TTYC_KICH1] = { TTYCODE_STRING, "kich1" },
219
  [TTYC_KIND] = { TTYCODE_STRING, "kind" },
220
  [TTYC_KLFT2] = { TTYCODE_STRING, "kLFT" },
221
  [TTYC_KLFT3] = { TTYCODE_STRING, "kLFT3" },
222
  [TTYC_KLFT4] = { TTYCODE_STRING, "kLFT4" },
223
  [TTYC_KLFT5] = { TTYCODE_STRING, "kLFT5" },
224
  [TTYC_KLFT6] = { TTYCODE_STRING, "kLFT6" },
225
  [TTYC_KLFT7] = { TTYCODE_STRING, "kLFT7" },
226
  [TTYC_KMOUS] = { TTYCODE_STRING, "kmous" },
227
  [TTYC_KNP] = { TTYCODE_STRING, "knp" },
228
  [TTYC_KNXT2] = { TTYCODE_STRING, "kNXT" },
229
  [TTYC_KNXT3] = { TTYCODE_STRING, "kNXT3" },
230
  [TTYC_KNXT4] = { TTYCODE_STRING, "kNXT4" },
231
  [TTYC_KNXT5] = { TTYCODE_STRING, "kNXT5" },
232
  [TTYC_KNXT6] = { TTYCODE_STRING, "kNXT6" },
233
  [TTYC_KNXT7] = { TTYCODE_STRING, "kNXT7" },
234
  [TTYC_KPP] = { TTYCODE_STRING, "kpp" },
235
  [TTYC_KPRV2] = { TTYCODE_STRING, "kPRV" },
236
  [TTYC_KPRV3] = { TTYCODE_STRING, "kPRV3" },
237
  [TTYC_KPRV4] = { TTYCODE_STRING, "kPRV4" },
238
  [TTYC_KPRV5] = { TTYCODE_STRING, "kPRV5" },
239
  [TTYC_KPRV6] = { TTYCODE_STRING, "kPRV6" },
240
  [TTYC_KPRV7] = { TTYCODE_STRING, "kPRV7" },
241
  [TTYC_KRIT2] = { TTYCODE_STRING, "kRIT" },
242
  [TTYC_KRIT3] = { TTYCODE_STRING, "kRIT3" },
243
  [TTYC_KRIT4] = { TTYCODE_STRING, "kRIT4" },
244
  [TTYC_KRIT5] = { TTYCODE_STRING, "kRIT5" },
245
  [TTYC_KRIT6] = { TTYCODE_STRING, "kRIT6" },
246
  [TTYC_KRIT7] = { TTYCODE_STRING, "kRIT7" },
247
  [TTYC_KRI] = { TTYCODE_STRING, "kri" },
248
  [TTYC_KUP2] = { TTYCODE_STRING, "kUP" }, /* not kUP2 */
249
  [TTYC_KUP3] = { TTYCODE_STRING, "kUP3" },
250
  [TTYC_KUP4] = { TTYCODE_STRING, "kUP4" },
251
  [TTYC_KUP5] = { TTYCODE_STRING, "kUP5" },
252
  [TTYC_KUP6] = { TTYCODE_STRING, "kUP6" },
253
  [TTYC_KUP7] = { TTYCODE_STRING, "kUP7" },
254
  [TTYC_MS] = { TTYCODE_STRING, "Ms" },
255
  [TTYC_NOBR] = { TTYCODE_STRING, "Nobr" },
256
  [TTYC_OL] = { TTYCODE_STRING, "ol" },
257
  [TTYC_OP] = { TTYCODE_STRING, "op" },
258
  [TTYC_RECT] = { TTYCODE_STRING, "Rect" },
259
  [TTYC_REV] = { TTYCODE_STRING, "rev" },
260
  [TTYC_RGB] = { TTYCODE_FLAG, "RGB" },
261
  [TTYC_RIN] = { TTYCODE_STRING, "rin" },
262
  [TTYC_RI] = { TTYCODE_STRING, "ri" },
263
  [TTYC_RMACS] = { TTYCODE_STRING, "rmacs" },
264
  [TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" },
265
  [TTYC_RMKX] = { TTYCODE_STRING, "rmkx" },
266
  [TTYC_SETAB] = { TTYCODE_STRING, "setab" },
267
  [TTYC_SETAF] = { TTYCODE_STRING, "setaf" },
268
  [TTYC_SETAL] = { TTYCODE_STRING, "setal" },
269
  [TTYC_SETRGBB] = { TTYCODE_STRING, "setrgbb" },
270
  [TTYC_SETRGBF] = { TTYCODE_STRING, "setrgbf" },
271
  [TTYC_SETULC] = { TTYCODE_STRING, "Setulc" },
272
  [TTYC_SETULC1] = { TTYCODE_STRING, "Setulc1" },
273
  [TTYC_SE] = { TTYCODE_STRING, "Se" },
274
  [TTYC_SXL] =  { TTYCODE_FLAG, "Sxl" },
275
  [TTYC_SGR0] = { TTYCODE_STRING, "sgr0" },
276
  [TTYC_SITM] = { TTYCODE_STRING, "sitm" },
277
  [TTYC_SMACS] = { TTYCODE_STRING, "smacs" },
278
  [TTYC_SMCUP] = { TTYCODE_STRING, "smcup" },
279
  [TTYC_SMKX] = { TTYCODE_STRING, "smkx" },
280
  [TTYC_SMOL] = { TTYCODE_STRING, "Smol" },
281
  [TTYC_SMSO] = { TTYCODE_STRING, "smso" },
282
  [TTYC_SMULX] = { TTYCODE_STRING, "Smulx" },
283
  [TTYC_SMUL] = { TTYCODE_STRING, "smul" },
284
  [TTYC_SMXX] =  { TTYCODE_STRING, "smxx" },
285
  [TTYC_SPB] = { TTYCODE_STRING, "Spb" },
286
  [TTYC_SS] = { TTYCODE_STRING, "Ss" },
287
  [TTYC_SWD] = { TTYCODE_STRING, "Swd" },
288
  [TTYC_SYNC] = { TTYCODE_STRING, "Sync" },
289
  [TTYC_TC] = { TTYCODE_FLAG, "Tc" },
290
  [TTYC_TSL] = { TTYCODE_STRING, "tsl" },
291
  [TTYC_U8] = { TTYCODE_NUMBER, "U8" },
292
  [TTYC_VPA] = { TTYCODE_STRING, "vpa" },
293
  [TTYC_XT] = { TTYCODE_FLAG, "XT" }
294
};
295
296
u_int
297
tty_term_ncodes(void)
298
0
{
299
0
  return (nitems(tty_term_codes));
300
0
}
301
302
static char *
303
tty_term_strip(const char *s)
304
0
{
305
0
  const char     *ptr;
306
0
  static char buf[8192];
307
0
  size_t    len;
308
309
  /* Ignore strings with no padding. */
310
0
  if (strchr(s, '$') == NULL)
311
0
    return (xstrdup(s));
312
313
0
  len = 0;
314
0
  for (ptr = s; *ptr != '\0'; ptr++) {
315
0
    if (*ptr == '$' && *(ptr + 1) == '<') {
316
0
      while (*ptr != '\0' && *ptr != '>')
317
0
        ptr++;
318
0
      if (*ptr == '>')
319
0
        ptr++;
320
0
      if (*ptr == '\0')
321
0
        break;
322
0
    }
323
324
0
    buf[len++] = *ptr;
325
0
    if (len == (sizeof buf) - 1)
326
0
      break;
327
0
  }
328
0
  buf[len] = '\0';
329
330
0
  return (xstrdup(buf));
331
0
}
332
333
static char *
334
tty_term_override_next(const char *s, size_t *offset)
335
0
{
336
0
  static char value[8192];
337
0
  size_t    n = 0, at = *offset;
338
339
0
  if (s[at] == '\0')
340
0
    return (NULL);
341
342
0
  while (s[at] != '\0') {
343
0
    if (s[at] == ':') {
344
0
      if (s[at + 1] == ':') {
345
0
        value[n++] = ':';
346
0
        at += 2;
347
0
      } else
348
0
        break;
349
0
    } else {
350
0
      value[n++] = s[at];
351
0
      at++;
352
0
    }
353
0
    if (n == (sizeof value) - 1)
354
0
      return (NULL);
355
0
  }
356
0
  if (s[at] != '\0')
357
0
    *offset = at + 1;
358
0
  else
359
0
    *offset = at;
360
0
  value[n] = '\0';
361
0
  return (value);
362
0
}
363
364
void
365
tty_term_apply(struct tty_term *term, const char *capabilities, int quiet)
366
0
{
367
0
  const struct tty_term_code_entry  *ent;
368
0
  struct tty_code       *code;
369
0
  size_t                                   offset = 0;
370
0
  char          *cp, *value, *s;
371
0
  const char        *errstr, *name = term->name;
372
0
  u_int          i;
373
0
  int          n, remove;
374
375
0
  while ((s = tty_term_override_next(capabilities, &offset)) != NULL) {
376
0
    if (*s == '\0')
377
0
      continue;
378
0
    value = NULL;
379
380
0
    remove = 0;
381
0
    if ((cp = strchr(s, '=')) != NULL) {
382
0
      *cp++ = '\0';
383
0
      value = xstrdup(cp);
384
0
      if (strunvis(value, cp) == -1) {
385
0
        free(value);
386
0
        value = xstrdup(cp);
387
0
      }
388
0
    } else if (s[strlen(s) - 1] == '@') {
389
0
      s[strlen(s) - 1] = '\0';
390
0
      remove = 1;
391
0
    } else
392
0
      value = xstrdup("");
393
394
0
    if (!quiet) {
395
0
      if (remove)
396
0
        log_debug("%s override: %s@", name, s);
397
0
      else if (*value == '\0')
398
0
        log_debug("%s override: %s", name, s);
399
0
      else
400
0
        log_debug("%s override: %s=%s", name, s, value);
401
0
    }
402
403
0
    for (i = 0; i < tty_term_ncodes(); i++) {
404
0
      ent = &tty_term_codes[i];
405
0
      if (strcmp(s, ent->name) != 0)
406
0
        continue;
407
0
      code = &term->codes[i];
408
409
0
      if (remove) {
410
0
        code->type = TTYCODE_NONE;
411
0
        continue;
412
0
      }
413
0
      switch (ent->type) {
414
0
      case TTYCODE_NONE:
415
0
        break;
416
0
      case TTYCODE_STRING:
417
0
        if (code->type == TTYCODE_STRING)
418
0
          free(code->value.string);
419
0
        code->value.string = xstrdup(value);
420
0
        code->type = ent->type;
421
0
        break;
422
0
      case TTYCODE_NUMBER:
423
0
        n = strtonum(value, 0, INT_MAX, &errstr);
424
0
        if (errstr != NULL)
425
0
          break;
426
0
        code->value.number = n;
427
0
        code->type = ent->type;
428
0
        break;
429
0
      case TTYCODE_FLAG:
430
0
        code->value.flag = 1;
431
0
        code->type = ent->type;
432
0
        break;
433
0
      }
434
0
    }
435
436
0
    free(value);
437
0
  }
438
0
}
439
440
void
441
tty_term_apply_overrides(struct tty_term *term)
442
0
{
443
0
  struct options_entry    *o;
444
0
  struct options_array_item *a;
445
0
  union options_value   *ov;
446
0
  const char      *s, *acs;
447
0
  size_t         offset;
448
0
  char        *first;
449
450
  /* Update capabilities from the option. */
451
0
  o = options_get_only(global_options, "terminal-overrides");
452
0
  a = options_array_first(o);
453
0
  while (a != NULL) {
454
0
    ov = options_array_item_value(a);
455
0
    s = ov->string;
456
457
0
    offset = 0;
458
0
    first = tty_term_override_next(s, &offset);
459
0
    if (first != NULL && fnmatch(first, term->name, 0) == 0)
460
0
      tty_term_apply(term, s + offset, 0);
461
0
    a = options_array_next(a);
462
0
  }
463
464
  /* Log the SIXEL flag. */
465
0
  log_debug("SIXEL flag is %d", !!(term->flags & TERM_SIXEL));
466
467
  /* Update the RGB flag if the terminal has RGB colours. */
468
0
  if (tty_term_has(term, TTYC_SETRGBF) &&
469
0
      tty_term_has(term, TTYC_SETRGBB))
470
0
    term->flags |= TERM_RGBCOLOURS;
471
0
  else
472
0
    term->flags &= ~TERM_RGBCOLOURS;
473
0
  log_debug("RGBCOLOURS flag is %d", !!(term->flags & TERM_RGBCOLOURS));
474
475
  /*
476
   * Set or clear the DECSLRM flag if the terminal has the margin
477
   * capabilities.
478
   */
479
0
  if (tty_term_has(term, TTYC_CMG) && tty_term_has(term, TTYC_CLMG))
480
0
    term->flags |= TERM_DECSLRM;
481
0
  else
482
0
    term->flags &= ~TERM_DECSLRM;
483
0
  log_debug("DECSLRM flag is %d", !!(term->flags & TERM_DECSLRM));
484
485
  /*
486
   * Set or clear the DECFRA flag if the terminal has the rectangle
487
   * capability.
488
   */
489
0
  if (tty_term_has(term, TTYC_RECT))
490
0
    term->flags |= TERM_DECFRA;
491
0
  else
492
0
    term->flags &= ~TERM_DECFRA;
493
0
  log_debug("DECFRA flag is %d", !!(term->flags & TERM_DECFRA));
494
495
  /*
496
   * Terminals without am (auto right margin) wrap at at $COLUMNS - 1
497
   * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
498
   *
499
   * Terminals without xenl (eat newline glitch) ignore a newline beyond
500
   * the right edge of the terminal, but tmux doesn't care about this -
501
   * it always uses absolute only moves the cursor with a newline when
502
   * also sending a linefeed.
503
   *
504
   * This is irritating, most notably because it is painful to write to
505
   * the very bottom-right of the screen without scrolling.
506
   *
507
   * Flag the terminal here and apply some workarounds in other places to
508
   * do the best possible.
509
   */
510
0
  if (!tty_term_flag(term, TTYC_AM))
511
0
    term->flags |= TERM_NOAM;
512
0
  else
513
0
    term->flags &= ~TERM_NOAM;
514
0
  log_debug("NOAM flag is %d", !!(term->flags & TERM_NOAM));
515
516
  /* Generate ACS table. If none is present, use nearest ASCII. */
517
0
  memset(term->acs, 0, sizeof term->acs);
518
0
  if (tty_term_has(term, TTYC_ACSC))
519
0
    acs = tty_term_string(term, TTYC_ACSC);
520
0
  else
521
0
    acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~.";
522
0
  for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2)
523
0
    term->acs[(u_char) acs[0]][0] = acs[1];
524
525
0
  tty_term_validate(term);
526
0
}
527
528
static void
529
tty_term_validate(struct tty_term *term)
530
0
{
531
0
  struct tty_code *code = &term->codes[TTYC_MS];
532
533
0
  if (code->type != TTYCODE_STRING)
534
0
    return;
535
0
  if (*tty_term_string_ss(term, TTYC_MS, "c", "?") != '\0') {
536
0
    term->flags &= ~TERM_INVALIDMS;
537
0
    return;
538
0
  }
539
540
0
  log_debug("removing invalid Ms capability");
541
0
  term->flags |= TERM_INVALIDMS;
542
0
  free(code->value.string);
543
0
  code->type = TTYCODE_NONE;
544
0
}
545
546
struct tty_term *
547
tty_term_create(struct tty *tty, char *name, char **caps, u_int ncaps,
548
    char **cause)
549
0
{
550
0
  struct client       *c = tty->client;
551
0
  struct tty_term       *term;
552
0
  const struct tty_term_code_entry  *ent;
553
0
  struct tty_code       *code;
554
0
  struct options_entry      *o;
555
0
  struct options_array_item   *a;
556
0
  union options_value     *ov;
557
0
  u_int          i, j;
558
0
  const char        *s, *value, *errstr;
559
0
  size_t           offset, namelen;
560
0
  char          *first;
561
0
  int          n;
562
0
  struct environ_entry      *envent;
563
564
0
  log_debug("adding term %s", name);
565
566
0
  term = xcalloc(1, sizeof *term);
567
0
  term->tty = tty;
568
0
  term->name = xstrdup(name);
569
0
  term->codes = xcalloc(tty_term_ncodes(), sizeof *term->codes);
570
0
  LIST_INSERT_HEAD(&tty_terms, term, entry);
571
572
  /* Fill in codes. */
573
0
  for (i = 0; i < ncaps; i++) {
574
0
    namelen = strcspn(caps[i], "=");
575
0
    if (namelen == 0)
576
0
      continue;
577
0
    value = caps[i] + namelen + 1;
578
579
0
    for (j = 0; j < tty_term_ncodes(); j++) {
580
0
      ent = &tty_term_codes[j];
581
0
      if (strncmp(ent->name, caps[i], namelen) != 0)
582
0
        continue;
583
0
      if (ent->name[namelen] != '\0')
584
0
        continue;
585
586
0
      code = &term->codes[j];
587
0
      code->type = TTYCODE_NONE;
588
0
      switch (ent->type) {
589
0
      case TTYCODE_NONE:
590
0
        break;
591
0
      case TTYCODE_STRING:
592
0
        code->type = TTYCODE_STRING;
593
0
        code->value.string = tty_term_strip(value);
594
0
        break;
595
0
      case TTYCODE_NUMBER:
596
0
        n = strtonum(value, 0, INT_MAX, &errstr);
597
0
        if (errstr != NULL)
598
0
          log_debug("%s: %s", ent->name, errstr);
599
0
        else {
600
0
          code->type = TTYCODE_NUMBER;
601
0
          code->value.number = n;
602
0
        }
603
0
        break;
604
0
      case TTYCODE_FLAG:
605
0
        code->type = TTYCODE_FLAG;
606
0
        code->value.flag = (*value == '1');
607
0
        break;
608
0
      }
609
0
    }
610
0
  }
611
612
  /* Apply terminal features. */
613
0
  o = options_get_only(global_options, "terminal-features");
614
0
  a = options_array_first(o);
615
0
  while (a != NULL) {
616
0
    ov = options_array_item_value(a);
617
0
    s = ov->string;
618
619
0
    offset = 0;
620
0
    first = tty_term_override_next(s, &offset);
621
0
    if (first != NULL && fnmatch(first, term->name, 0) == 0)
622
0
      tty_parse_client_features(c, s + offset, ":");
623
0
    a = options_array_next(a);
624
0
  }
625
626
  /* Delete curses data. */
627
0
#if !defined(NCURSES_VERSION_MAJOR) || NCURSES_VERSION_MAJOR > 5 || \
628
0
    (NCURSES_VERSION_MAJOR == 5 && NCURSES_VERSION_MINOR > 6)
629
0
  del_curterm(cur_term);
630
0
#endif
631
  /* Check for COLORTERM. */
632
0
  envent = environ_find(c->environ, "COLORTERM");
633
0
  if (envent != NULL) {
634
0
    log_debug("%s COLORTERM=%s", c->name, envent->value);
635
0
    if (strcasecmp(envent->value, "truecolor") == 0 ||
636
0
        strcasecmp(envent->value, "24bit") == 0)
637
0
      tty_parse_client_features(c, "RGB", ",");
638
0
    else if (strstr(envent->value, "256") != NULL)
639
0
      tty_parse_client_features(c, "256", ",");
640
0
  }
641
642
  /* Apply overrides so any capabilities used for features are changed. */
643
0
  tty_term_apply_overrides(term);
644
645
  /* These are always required. */
646
0
  if (!tty_term_has(term, TTYC_CLEAR)) {
647
0
    xasprintf(cause, "terminal does not support clear");
648
0
    goto error;
649
0
  }
650
0
  if (!tty_term_has(term, TTYC_CUP)) {
651
0
    xasprintf(cause, "terminal does not support cup");
652
0
    goto error;
653
0
  }
654
655
  /*
656
   * If TERM has XT or clear starts with CSI then it is safe to assume
657
   * the terminal is derived from the VT100. This controls whether device
658
   * attributes requests are sent to get more information.
659
   *
660
   * This is a bit of a hack but there aren't that many alternatives.
661
   * Worst case tmux will just fall back to using whatever terminfo(5)
662
   * says without trying to correct anything that is missing.
663
   *
664
   * Also add few features that VT100-like terminals should either
665
   * support or safely ignore.
666
   */
667
0
  s = tty_term_string(term, TTYC_CLEAR);
668
0
  if (tty_term_flag(term, TTYC_XT) || strncmp(s, "\033[", 2) == 0) {
669
0
    term->flags |= TERM_VT100LIKE;
670
0
    tty_parse_client_features(c, "bpaste,focus,title", ",");
671
0
  }
672
673
  /* Add RGB feature if terminal has RGB colours. */
674
0
  if ((tty_term_flag(term, TTYC_TC) || tty_term_has(term, TTYC_RGB)) &&
675
0
      (!tty_term_has(term, TTYC_SETRGBF) ||
676
0
      !tty_term_has(term, TTYC_SETRGBB)))
677
0
    tty_parse_client_features(c, "RGB", ",");
678
679
  /* Apply the features and overrides again. */
680
0
  if (tty_apply_features(term))
681
0
    tty_term_apply_overrides(term);
682
683
  /* Log the capabilities. */
684
0
  for (i = 0; i < tty_term_ncodes(); i++)
685
0
    log_debug("%s%s", name, tty_term_describe(term, i));
686
687
0
  return (term);
688
689
0
error:
690
0
  tty_term_free(term);
691
0
  return (NULL);
692
0
}
693
694
void
695
tty_term_free(struct tty_term *term)
696
0
{
697
0
  u_int i;
698
699
0
  log_debug("removing term %s", term->name);
700
701
0
  for (i = 0; i < tty_term_ncodes(); i++) {
702
0
    if (term->codes[i].type == TTYCODE_STRING)
703
0
      free(term->codes[i].value.string);
704
0
  }
705
0
  free(term->codes);
706
707
0
  LIST_REMOVE(term, entry);
708
0
  free(term->name);
709
0
  free(term);
710
0
}
711
712
int
713
tty_term_read_list(const char *name, int fd, char ***caps, u_int *ncaps,
714
    char **cause)
715
0
{
716
0
  const struct tty_term_code_entry  *ent;
717
0
  int          error, n;
718
0
  u_int          i;
719
0
  const char        *s;
720
0
  char           tmp[11];
721
722
0
  if (setupterm((char *)name, fd, &error) != OK) {
723
0
    switch (error) {
724
0
    case 1:
725
0
      xasprintf(cause, "can't use hardcopy terminal: %s",
726
0
          name);
727
0
      break;
728
0
    case 0:
729
0
      xasprintf(cause, "missing or unsuitable terminal: %s",
730
0
          name);
731
0
      break;
732
0
    case -1:
733
0
      xasprintf(cause, "can't find terminfo database");
734
0
      break;
735
0
    default:
736
0
      xasprintf(cause, "unknown error");
737
0
      break;
738
0
    }
739
0
    return (-1);
740
0
  }
741
742
0
  *ncaps = 0;
743
0
  *caps = NULL;
744
745
0
  for (i = 0; i < tty_term_ncodes(); i++) {
746
0
    ent = &tty_term_codes[i];
747
0
    switch (ent->type) {
748
0
    case TTYCODE_NONE:
749
0
      continue;
750
0
    case TTYCODE_STRING:
751
0
      s = tigetstr((char *)ent->name);
752
0
      if (s == NULL || s == (char *)-1)
753
0
        continue;
754
0
      break;
755
0
    case TTYCODE_NUMBER:
756
0
      n = tigetnum((char *)ent->name);
757
0
      if (n == -1 || n == -2)
758
0
        continue;
759
0
      xsnprintf(tmp, sizeof tmp, "%d", n);
760
0
      s = tmp;
761
0
      break;
762
0
    case TTYCODE_FLAG:
763
0
      n = tigetflag((char *)ent->name);
764
0
      if (n == -1)
765
0
        continue;
766
0
      if (n)
767
0
        s = "1";
768
0
      else
769
0
        s = "0";
770
0
      break;
771
0
    default:
772
0
      fatalx("unknown capability type");
773
0
    }
774
0
    *caps = xreallocarray(*caps, (*ncaps) + 1, sizeof **caps);
775
0
    xasprintf(&(*caps)[*ncaps], "%s=%s", ent->name, s);
776
0
    (*ncaps)++;
777
0
  }
778
779
0
#if !defined(NCURSES_VERSION_MAJOR) || NCURSES_VERSION_MAJOR > 5 || \
780
0
    (NCURSES_VERSION_MAJOR == 5 && NCURSES_VERSION_MINOR > 6)
781
0
  del_curterm(cur_term);
782
0
#endif
783
0
  return (0);
784
0
}
785
786
void
787
tty_term_free_list(char **caps, u_int ncaps)
788
0
{
789
0
  u_int i;
790
791
0
  for (i = 0; i < ncaps; i++)
792
0
    free(caps[i]);
793
0
  free(caps);
794
0
}
795
796
int
797
tty_term_has(struct tty_term *term, enum tty_code_code code)
798
0
{
799
0
  return (term->codes[code].type != TTYCODE_NONE);
800
0
}
801
802
int
803
tty_term_has_name(struct tty_term *term, const char *name)
804
0
{
805
0
  u_int i;
806
807
0
  for (i = 0; i < tty_term_ncodes(); i++) {
808
0
    if (strcmp(tty_term_codes[i].name, name) == 0)
809
0
      return (tty_term_has(term, i));
810
0
  }
811
0
  return (0);
812
0
}
813
814
const char *
815
tty_term_string(struct tty_term *term, enum tty_code_code code)
816
0
{
817
0
  if (!tty_term_has(term, code))
818
0
    return ("");
819
0
  if (term->codes[code].type != TTYCODE_STRING)
820
0
    fatalx("not a string: %d", code);
821
0
  return (term->codes[code].value.string);
822
0
}
823
824
const char *
825
tty_term_string_i(struct tty_term *term, enum tty_code_code code, int a)
826
0
{
827
0
  const char  *x = tty_term_string(term, code), *s;
828
829
#if defined(HAVE_TIPARM_S)
830
  s = tiparm_s(1, 0, x, a);
831
#elif defined(HAVE_TIPARM)
832
  s = tiparm(x, a);
833
#else
834
  s = tparm((char *)x, a, 0, 0, 0, 0, 0, 0, 0, 0);
835
#endif
836
0
  if (s == NULL) {
837
0
    log_debug("could not expand %s", tty_term_codes[code].name);
838
0
    return ("");
839
0
  }
840
0
  return (s);
841
0
}
842
843
const char *
844
tty_term_string_ii(struct tty_term *term, enum tty_code_code code, int a, int b)
845
0
{
846
0
  const char  *x = tty_term_string(term, code), *s;
847
848
#if defined(HAVE_TIPARM_S)
849
  s = tiparm_s(2, 0, x, a, b);
850
#elif defined(HAVE_TIPARM)
851
  s = tiparm(x, a, b);
852
#else
853
  s = tparm((char *)x, a, b, 0, 0, 0, 0, 0, 0, 0);
854
#endif
855
0
  if (s == NULL) {
856
0
    log_debug("could not expand %s", tty_term_codes[code].name);
857
0
    return ("");
858
0
  }
859
0
  return (s);
860
0
}
861
862
const char *
863
tty_term_string_iii(struct tty_term *term, enum tty_code_code code, int a,
864
    int b, int c)
865
0
{
866
0
  const char  *x = tty_term_string(term, code), *s;
867
868
#if defined(HAVE_TIPARM_S)
869
  s = tiparm_s(3, 0, x, a, b, c);
870
#elif defined(HAVE_TIPARM)
871
  s = tiparm(x, a, b, c);
872
#else
873
  s = tparm((char *)x, a, b, c, 0, 0, 0, 0, 0, 0);
874
#endif
875
0
  if (s == NULL) {
876
0
    log_debug("could not expand %s", tty_term_codes[code].name);
877
0
    return ("");
878
0
  }
879
0
  return (s);
880
0
}
881
882
const char *
883
tty_term_string_s(struct tty_term *term, enum tty_code_code code, const char *a)
884
0
{
885
0
  const char  *x = tty_term_string(term, code), *s;
886
887
#if defined(HAVE_TIPARM_S)
888
  s = tiparm_s(1, 1, x, a);
889
#elif defined(HAVE_TIPARM)
890
  s = tiparm(x, a);
891
#else
892
  s = tparm((char *)x, (long)a, 0, 0, 0, 0, 0, 0, 0, 0);
893
#endif
894
0
  if (s == NULL) {
895
0
    log_debug("could not expand %s", tty_term_codes[code].name);
896
0
    return ("");
897
0
  }
898
0
  return (s);
899
0
}
900
901
const char *
902
tty_term_string_ss(struct tty_term *term, enum tty_code_code code,
903
    const char *a, const char *b)
904
0
{
905
0
  const char  *x = tty_term_string(term, code), *s;
906
907
#if defined(HAVE_TIPARM_S)
908
  s = tiparm_s(2, 3, x, a, b);
909
#elif defined(HAVE_TIPARM)
910
  s = tiparm(x, a, b);
911
#else
912
  s = tparm((char *)x, (long)a, (long)b, 0, 0, 0, 0, 0, 0, 0);
913
#endif
914
0
  if (s == NULL) {
915
0
    log_debug("could not expand %s", tty_term_codes[code].name);
916
0
    return ("");
917
0
  }
918
0
  return (s);
919
0
}
920
921
int
922
tty_term_number(struct tty_term *term, enum tty_code_code code)
923
0
{
924
0
  if (!tty_term_has(term, code))
925
0
    return (0);
926
0
  if (term->codes[code].type != TTYCODE_NUMBER)
927
0
    fatalx("not a number: %d", code);
928
0
  return (term->codes[code].value.number);
929
0
}
930
931
int
932
tty_term_flag(struct tty_term *term, enum tty_code_code code)
933
0
{
934
0
  if (!tty_term_has(term, code))
935
0
    return (0);
936
0
  if (term->codes[code].type != TTYCODE_FLAG)
937
0
    fatalx("not a flag: %d", code);
938
0
  return (term->codes[code].value.flag);
939
0
}
940
941
const char *
942
tty_term_describe(struct tty_term *term, enum tty_code_code code)
943
0
{
944
0
  static char  s[256];
945
0
  char     out[128];
946
947
0
  switch (term->codes[code].type) {
948
0
  case TTYCODE_NONE:
949
0
    xsnprintf(s, sizeof s, "%4u: %s: [missing]",
950
0
        code, tty_term_codes[code].name);
951
0
    break;
952
0
  case TTYCODE_STRING:
953
0
    strnvis(out, term->codes[code].value.string, sizeof out,
954
0
        VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
955
0
    xsnprintf(s, sizeof s, "%4u: %s: (string) %s",
956
0
        code, tty_term_codes[code].name,
957
0
        out);
958
0
    break;
959
0
  case TTYCODE_NUMBER:
960
0
    xsnprintf(s, sizeof s, "%4u: %s: (number) %d",
961
0
        code, tty_term_codes[code].name,
962
0
        term->codes[code].value.number);
963
0
    break;
964
0
  case TTYCODE_FLAG:
965
0
    xsnprintf(s, sizeof s, "%4u: %s: (flag) %s",
966
0
        code, tty_term_codes[code].name,
967
0
        term->codes[code].value.flag ? "true" : "false");
968
0
    break;
969
0
  }
970
0
  return (s);
971
0
}