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