tty-term.c 19 KB

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