Coverage Report

Created: 2026-04-27 07:08

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