input.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 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. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include "tmux.h"
  22. /*
  23. * Based on the description by Paul Williams at:
  24. *
  25. * http://vt100.net/emu/dec_ansi_parser
  26. *
  27. * With the following changes:
  28. *
  29. * - 7-bit only.
  30. *
  31. * - Support for UTF-8.
  32. *
  33. * - OSC (but not APC) may be terminated by \007 as well as ST.
  34. *
  35. * - A state for APC similar to OSC. Some terminals appear to use this to set
  36. * the title.
  37. *
  38. * - A state for the screen \033k...\033\\ sequence to rename a window. This is
  39. * pretty stupid but not supporting it is more trouble than it is worth.
  40. *
  41. * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
  42. * be passed to the underlying terminals.
  43. */
  44. /* Input parser cell. */
  45. struct input_cell {
  46. struct grid_cell cell;
  47. int set;
  48. int g0set; /* 1 if ACS */
  49. int g1set; /* 1 if ACS */
  50. };
  51. /* Input parser context. */
  52. struct input_ctx {
  53. struct window_pane *wp;
  54. struct screen_write_ctx ctx;
  55. struct input_cell cell;
  56. struct input_cell old_cell;
  57. u_int old_cx;
  58. u_int old_cy;
  59. u_char interm_buf[4];
  60. size_t interm_len;
  61. u_char param_buf[64];
  62. size_t param_len;
  63. #define INPUT_BUF_START 32
  64. #define INPUT_BUF_LIMIT 1048576
  65. u_char *input_buf;
  66. size_t input_len;
  67. size_t input_space;
  68. int param_list[24]; /* -1 not present */
  69. u_int param_list_len;
  70. struct utf8_data utf8data;
  71. int ch;
  72. int flags;
  73. #define INPUT_DISCARD 0x1
  74. const struct input_state *state;
  75. /*
  76. * All input received since we were last in the ground state. Sent to
  77. * control clients on connection.
  78. */
  79. struct evbuffer *since_ground;
  80. };
  81. /* Helper functions. */
  82. struct input_transition;
  83. int input_split(struct input_ctx *);
  84. int input_get(struct input_ctx *, u_int, int, int);
  85. void printflike(2, 3) input_reply(struct input_ctx *, const char *, ...);
  86. void input_set_state(struct window_pane *, const struct input_transition *);
  87. void input_reset_cell(struct input_ctx *);
  88. /* Transition entry/exit handlers. */
  89. void input_clear(struct input_ctx *);
  90. void input_ground(struct input_ctx *);
  91. void input_enter_osc(struct input_ctx *);
  92. void input_exit_osc(struct input_ctx *);
  93. void input_enter_apc(struct input_ctx *);
  94. void input_exit_apc(struct input_ctx *);
  95. void input_enter_rename(struct input_ctx *);
  96. void input_exit_rename(struct input_ctx *);
  97. /* Input state handlers. */
  98. int input_print(struct input_ctx *);
  99. int input_intermediate(struct input_ctx *);
  100. int input_parameter(struct input_ctx *);
  101. int input_input(struct input_ctx *);
  102. int input_c0_dispatch(struct input_ctx *);
  103. int input_esc_dispatch(struct input_ctx *);
  104. int input_csi_dispatch(struct input_ctx *);
  105. void input_csi_dispatch_rm(struct input_ctx *);
  106. void input_csi_dispatch_rm_private(struct input_ctx *);
  107. void input_csi_dispatch_sm(struct input_ctx *);
  108. void input_csi_dispatch_sm_private(struct input_ctx *);
  109. void input_csi_dispatch_winops(struct input_ctx *);
  110. void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
  111. void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
  112. void input_csi_dispatch_sgr(struct input_ctx *);
  113. int input_dcs_dispatch(struct input_ctx *);
  114. int input_utf8_open(struct input_ctx *);
  115. int input_utf8_add(struct input_ctx *);
  116. int input_utf8_close(struct input_ctx *);
  117. /* Command table comparison function. */
  118. int input_table_compare(const void *, const void *);
  119. /* Command table entry. */
  120. struct input_table_entry {
  121. int ch;
  122. const char *interm;
  123. int type;
  124. };
  125. /* Escape commands. */
  126. enum input_esc_type {
  127. INPUT_ESC_DECALN,
  128. INPUT_ESC_DECKPAM,
  129. INPUT_ESC_DECKPNM,
  130. INPUT_ESC_DECRC,
  131. INPUT_ESC_DECSC,
  132. INPUT_ESC_HTS,
  133. INPUT_ESC_IND,
  134. INPUT_ESC_NEL,
  135. INPUT_ESC_RI,
  136. INPUT_ESC_RIS,
  137. INPUT_ESC_SCSG0_OFF,
  138. INPUT_ESC_SCSG0_ON,
  139. INPUT_ESC_SCSG1_OFF,
  140. INPUT_ESC_SCSG1_ON,
  141. };
  142. /* Escape command table. */
  143. const struct input_table_entry input_esc_table[] = {
  144. { '0', "(", INPUT_ESC_SCSG0_ON },
  145. { '0', ")", INPUT_ESC_SCSG1_ON },
  146. { '7', "", INPUT_ESC_DECSC },
  147. { '8', "", INPUT_ESC_DECRC },
  148. { '8', "#", INPUT_ESC_DECALN },
  149. { '=', "", INPUT_ESC_DECKPAM },
  150. { '>', "", INPUT_ESC_DECKPNM },
  151. { 'B', "(", INPUT_ESC_SCSG0_OFF },
  152. { 'B', ")", INPUT_ESC_SCSG1_OFF },
  153. { 'D', "", INPUT_ESC_IND },
  154. { 'E', "", INPUT_ESC_NEL },
  155. { 'H', "", INPUT_ESC_HTS },
  156. { 'M', "", INPUT_ESC_RI },
  157. { 'c', "", INPUT_ESC_RIS },
  158. };
  159. /* Control (CSI) commands. */
  160. enum input_csi_type {
  161. INPUT_CSI_CBT,
  162. INPUT_CSI_CNL,
  163. INPUT_CSI_CPL,
  164. INPUT_CSI_CUB,
  165. INPUT_CSI_CUD,
  166. INPUT_CSI_CUF,
  167. INPUT_CSI_CUP,
  168. INPUT_CSI_CUU,
  169. INPUT_CSI_DA,
  170. INPUT_CSI_DA_TWO,
  171. INPUT_CSI_DCH,
  172. INPUT_CSI_DECSCUSR,
  173. INPUT_CSI_DECSTBM,
  174. INPUT_CSI_DL,
  175. INPUT_CSI_DSR,
  176. INPUT_CSI_ECH,
  177. INPUT_CSI_ED,
  178. INPUT_CSI_EL,
  179. INPUT_CSI_HPA,
  180. INPUT_CSI_ICH,
  181. INPUT_CSI_IL,
  182. INPUT_CSI_RCP,
  183. INPUT_CSI_RM,
  184. INPUT_CSI_RM_PRIVATE,
  185. INPUT_CSI_SCP,
  186. INPUT_CSI_SGR,
  187. INPUT_CSI_SM,
  188. INPUT_CSI_SM_PRIVATE,
  189. INPUT_CSI_TBC,
  190. INPUT_CSI_VPA,
  191. INPUT_CSI_WINOPS,
  192. };
  193. /* Control (CSI) command table. */
  194. const struct input_table_entry input_csi_table[] = {
  195. { '@', "", INPUT_CSI_ICH },
  196. { 'A', "", INPUT_CSI_CUU },
  197. { 'B', "", INPUT_CSI_CUD },
  198. { 'C', "", INPUT_CSI_CUF },
  199. { 'D', "", INPUT_CSI_CUB },
  200. { 'E', "", INPUT_CSI_CNL },
  201. { 'F', "", INPUT_CSI_CPL },
  202. { 'G', "", INPUT_CSI_HPA },
  203. { 'H', "", INPUT_CSI_CUP },
  204. { 'J', "", INPUT_CSI_ED },
  205. { 'K', "", INPUT_CSI_EL },
  206. { 'L', "", INPUT_CSI_IL },
  207. { 'M', "", INPUT_CSI_DL },
  208. { 'P', "", INPUT_CSI_DCH },
  209. { 'X', "", INPUT_CSI_ECH },
  210. { 'Z', "", INPUT_CSI_CBT },
  211. { 'c', "", INPUT_CSI_DA },
  212. { 'c', ">", INPUT_CSI_DA_TWO },
  213. { 'd', "", INPUT_CSI_VPA },
  214. { 'f', "", INPUT_CSI_CUP },
  215. { 'g', "", INPUT_CSI_TBC },
  216. { 'h', "", INPUT_CSI_SM },
  217. { 'h', "?", INPUT_CSI_SM_PRIVATE },
  218. { 'l', "", INPUT_CSI_RM },
  219. { 'l', "?", INPUT_CSI_RM_PRIVATE },
  220. { 'm', "", INPUT_CSI_SGR },
  221. { 'n', "", INPUT_CSI_DSR },
  222. { 'q', " ", INPUT_CSI_DECSCUSR },
  223. { 'r', "", INPUT_CSI_DECSTBM },
  224. { 's', "", INPUT_CSI_SCP },
  225. { 't', "", INPUT_CSI_WINOPS },
  226. { 'u', "", INPUT_CSI_RCP },
  227. };
  228. /* Input transition. */
  229. struct input_transition {
  230. int first;
  231. int last;
  232. int (*handler)(struct input_ctx *);
  233. const struct input_state *state;
  234. };
  235. /* Input state. */
  236. struct input_state {
  237. const char *name;
  238. void (*enter)(struct input_ctx *);
  239. void (*exit)(struct input_ctx *);
  240. const struct input_transition *transitions;
  241. };
  242. /* State transitions available from all states. */
  243. #define INPUT_STATE_ANYWHERE \
  244. { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
  245. { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
  246. { 0x1b, 0x1b, NULL, &input_state_esc_enter }
  247. /* Forward declarations of state tables. */
  248. const struct input_transition input_state_ground_table[];
  249. const struct input_transition input_state_esc_enter_table[];
  250. const struct input_transition input_state_esc_intermediate_table[];
  251. const struct input_transition input_state_csi_enter_table[];
  252. const struct input_transition input_state_csi_parameter_table[];
  253. const struct input_transition input_state_csi_intermediate_table[];
  254. const struct input_transition input_state_csi_ignore_table[];
  255. const struct input_transition input_state_dcs_enter_table[];
  256. const struct input_transition input_state_dcs_parameter_table[];
  257. const struct input_transition input_state_dcs_intermediate_table[];
  258. const struct input_transition input_state_dcs_handler_table[];
  259. const struct input_transition input_state_dcs_escape_table[];
  260. const struct input_transition input_state_dcs_ignore_table[];
  261. const struct input_transition input_state_osc_string_table[];
  262. const struct input_transition input_state_apc_string_table[];
  263. const struct input_transition input_state_rename_string_table[];
  264. const struct input_transition input_state_consume_st_table[];
  265. const struct input_transition input_state_utf8_three_table[];
  266. const struct input_transition input_state_utf8_two_table[];
  267. const struct input_transition input_state_utf8_one_table[];
  268. /* ground state definition. */
  269. const struct input_state input_state_ground = {
  270. "ground",
  271. input_ground, NULL,
  272. input_state_ground_table
  273. };
  274. /* esc_enter state definition. */
  275. const struct input_state input_state_esc_enter = {
  276. "esc_enter",
  277. input_clear, NULL,
  278. input_state_esc_enter_table
  279. };
  280. /* esc_intermediate state definition. */
  281. const struct input_state input_state_esc_intermediate = {
  282. "esc_intermediate",
  283. NULL, NULL,
  284. input_state_esc_intermediate_table
  285. };
  286. /* csi_enter state definition. */
  287. const struct input_state input_state_csi_enter = {
  288. "csi_enter",
  289. input_clear, NULL,
  290. input_state_csi_enter_table
  291. };
  292. /* csi_parameter state definition. */
  293. const struct input_state input_state_csi_parameter = {
  294. "csi_parameter",
  295. NULL, NULL,
  296. input_state_csi_parameter_table
  297. };
  298. /* csi_intermediate state definition. */
  299. const struct input_state input_state_csi_intermediate = {
  300. "csi_intermediate",
  301. NULL, NULL,
  302. input_state_csi_intermediate_table
  303. };
  304. /* csi_ignore state definition. */
  305. const struct input_state input_state_csi_ignore = {
  306. "csi_ignore",
  307. NULL, NULL,
  308. input_state_csi_ignore_table
  309. };
  310. /* dcs_enter state definition. */
  311. const struct input_state input_state_dcs_enter = {
  312. "dcs_enter",
  313. input_clear, NULL,
  314. input_state_dcs_enter_table
  315. };
  316. /* dcs_parameter state definition. */
  317. const struct input_state input_state_dcs_parameter = {
  318. "dcs_parameter",
  319. NULL, NULL,
  320. input_state_dcs_parameter_table
  321. };
  322. /* dcs_intermediate state definition. */
  323. const struct input_state input_state_dcs_intermediate = {
  324. "dcs_intermediate",
  325. NULL, NULL,
  326. input_state_dcs_intermediate_table
  327. };
  328. /* dcs_handler state definition. */
  329. const struct input_state input_state_dcs_handler = {
  330. "dcs_handler",
  331. NULL, NULL,
  332. input_state_dcs_handler_table
  333. };
  334. /* dcs_escape state definition. */
  335. const struct input_state input_state_dcs_escape = {
  336. "dcs_escape",
  337. NULL, NULL,
  338. input_state_dcs_escape_table
  339. };
  340. /* dcs_ignore state definition. */
  341. const struct input_state input_state_dcs_ignore = {
  342. "dcs_ignore",
  343. NULL, NULL,
  344. input_state_dcs_ignore_table
  345. };
  346. /* osc_string state definition. */
  347. const struct input_state input_state_osc_string = {
  348. "osc_string",
  349. input_enter_osc, input_exit_osc,
  350. input_state_osc_string_table
  351. };
  352. /* apc_string state definition. */
  353. const struct input_state input_state_apc_string = {
  354. "apc_string",
  355. input_enter_apc, input_exit_apc,
  356. input_state_apc_string_table
  357. };
  358. /* rename_string state definition. */
  359. const struct input_state input_state_rename_string = {
  360. "rename_string",
  361. input_enter_rename, input_exit_rename,
  362. input_state_rename_string_table
  363. };
  364. /* consume_st state definition. */
  365. const struct input_state input_state_consume_st = {
  366. "consume_st",
  367. NULL, NULL,
  368. input_state_consume_st_table
  369. };
  370. /* utf8_three state definition. */
  371. const struct input_state input_state_utf8_three = {
  372. "utf8_three",
  373. NULL, NULL,
  374. input_state_utf8_three_table
  375. };
  376. /* utf8_two state definition. */
  377. const struct input_state input_state_utf8_two = {
  378. "utf8_two",
  379. NULL, NULL,
  380. input_state_utf8_two_table
  381. };
  382. /* utf8_one state definition. */
  383. const struct input_state input_state_utf8_one = {
  384. "utf8_one",
  385. NULL, NULL,
  386. input_state_utf8_one_table
  387. };
  388. /* ground state table. */
  389. const struct input_transition input_state_ground_table[] = {
  390. INPUT_STATE_ANYWHERE,
  391. { 0x00, 0x17, input_c0_dispatch, NULL },
  392. { 0x19, 0x19, input_c0_dispatch, NULL },
  393. { 0x1c, 0x1f, input_c0_dispatch, NULL },
  394. { 0x20, 0x7e, input_print, NULL },
  395. { 0x7f, 0x7f, NULL, NULL },
  396. { 0x80, 0xc1, NULL, NULL },
  397. { 0xc2, 0xdf, input_utf8_open, &input_state_utf8_one },
  398. { 0xe0, 0xef, input_utf8_open, &input_state_utf8_two },
  399. { 0xf0, 0xf4, input_utf8_open, &input_state_utf8_three },
  400. { 0xf5, 0xff, NULL, NULL },
  401. { -1, -1, NULL, NULL }
  402. };
  403. /* esc_enter state table. */
  404. const struct input_transition input_state_esc_enter_table[] = {
  405. INPUT_STATE_ANYWHERE,
  406. { 0x00, 0x17, input_c0_dispatch, NULL },
  407. { 0x19, 0x19, input_c0_dispatch, NULL },
  408. { 0x1c, 0x1f, input_c0_dispatch, NULL },
  409. { 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
  410. { 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
  411. { 0x50, 0x50, NULL, &input_state_dcs_enter },
  412. { 0x51, 0x57, input_esc_dispatch, &input_state_ground },
  413. { 0x58, 0x58, NULL, &input_state_consume_st },
  414. { 0x59, 0x59, input_esc_dispatch, &input_state_ground },
  415. { 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
  416. { 0x5b, 0x5b, NULL, &input_state_csi_enter },
  417. { 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
  418. { 0x5d, 0x5d, NULL, &input_state_osc_string },
  419. { 0x5e, 0x5e, NULL, &input_state_consume_st },
  420. { 0x5f, 0x5f, NULL, &input_state_apc_string },
  421. { 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
  422. { 0x6b, 0x6b, NULL, &input_state_rename_string },
  423. { 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
  424. { 0x7f, 0xff, NULL, NULL },
  425. { -1, -1, NULL, NULL }
  426. };
  427. /* esc_interm state table. */
  428. const struct input_transition input_state_esc_intermediate_table[] = {
  429. INPUT_STATE_ANYWHERE,
  430. { 0x00, 0x17, input_c0_dispatch, NULL },
  431. { 0x19, 0x19, input_c0_dispatch, NULL },
  432. { 0x1c, 0x1f, input_c0_dispatch, NULL },
  433. { 0x20, 0x2f, input_intermediate, NULL },
  434. { 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
  435. { 0x7f, 0xff, NULL, NULL },
  436. { -1, -1, NULL, NULL }
  437. };
  438. /* csi_enter state table. */
  439. const struct input_transition input_state_csi_enter_table[] = {
  440. INPUT_STATE_ANYWHERE,
  441. { 0x00, 0x17, input_c0_dispatch, NULL },
  442. { 0x19, 0x19, input_c0_dispatch, NULL },
  443. { 0x1c, 0x1f, input_c0_dispatch, NULL },
  444. { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
  445. { 0x30, 0x39, input_parameter, &input_state_csi_parameter },
  446. { 0x3a, 0x3a, NULL, &input_state_csi_ignore },
  447. { 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
  448. { 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
  449. { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
  450. { 0x7f, 0xff, NULL, NULL },
  451. { -1, -1, NULL, NULL }
  452. };
  453. /* csi_parameter state table. */
  454. const struct input_transition input_state_csi_parameter_table[] = {
  455. INPUT_STATE_ANYWHERE,
  456. { 0x00, 0x17, input_c0_dispatch, NULL },
  457. { 0x19, 0x19, input_c0_dispatch, NULL },
  458. { 0x1c, 0x1f, input_c0_dispatch, NULL },
  459. { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
  460. { 0x30, 0x39, input_parameter, NULL },
  461. { 0x3a, 0x3a, NULL, &input_state_csi_ignore },
  462. { 0x3b, 0x3b, input_parameter, NULL },
  463. { 0x3c, 0x3f, NULL, &input_state_csi_ignore },
  464. { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
  465. { 0x7f, 0xff, NULL, NULL },
  466. { -1, -1, NULL, NULL }
  467. };
  468. /* csi_intermediate state table. */
  469. const struct input_transition input_state_csi_intermediate_table[] = {
  470. INPUT_STATE_ANYWHERE,
  471. { 0x00, 0x17, input_c0_dispatch, NULL },
  472. { 0x19, 0x19, input_c0_dispatch, NULL },
  473. { 0x1c, 0x1f, input_c0_dispatch, NULL },
  474. { 0x20, 0x2f, input_intermediate, NULL },
  475. { 0x30, 0x3f, NULL, &input_state_csi_ignore },
  476. { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
  477. { 0x7f, 0xff, NULL, NULL },
  478. { -1, -1, NULL, NULL }
  479. };
  480. /* csi_ignore state table. */
  481. const struct input_transition input_state_csi_ignore_table[] = {
  482. INPUT_STATE_ANYWHERE,
  483. { 0x00, 0x17, input_c0_dispatch, NULL },
  484. { 0x19, 0x19, input_c0_dispatch, NULL },
  485. { 0x1c, 0x1f, input_c0_dispatch, NULL },
  486. { 0x20, 0x3f, NULL, NULL },
  487. { 0x40, 0x7e, NULL, &input_state_ground },
  488. { 0x7f, 0xff, NULL, NULL },
  489. { -1, -1, NULL, NULL }
  490. };
  491. /* dcs_enter state table. */
  492. const struct input_transition input_state_dcs_enter_table[] = {
  493. INPUT_STATE_ANYWHERE,
  494. { 0x00, 0x17, NULL, NULL },
  495. { 0x19, 0x19, NULL, NULL },
  496. { 0x1c, 0x1f, NULL, NULL },
  497. { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
  498. { 0x30, 0x39, input_parameter, &input_state_dcs_parameter },
  499. { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
  500. { 0x3b, 0x3b, input_parameter, &input_state_dcs_parameter },
  501. { 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
  502. { 0x40, 0x7e, input_input, &input_state_dcs_handler },
  503. { 0x7f, 0xff, NULL, NULL },
  504. { -1, -1, NULL, NULL }
  505. };
  506. /* dcs_parameter state table. */
  507. const struct input_transition input_state_dcs_parameter_table[] = {
  508. INPUT_STATE_ANYWHERE,
  509. { 0x00, 0x17, NULL, NULL },
  510. { 0x19, 0x19, NULL, NULL },
  511. { 0x1c, 0x1f, NULL, NULL },
  512. { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
  513. { 0x30, 0x39, input_parameter, NULL },
  514. { 0x3a, 0x3a, NULL, &input_state_dcs_ignore },
  515. { 0x3b, 0x3b, input_parameter, NULL },
  516. { 0x3c, 0x3f, NULL, &input_state_dcs_ignore },
  517. { 0x40, 0x7e, input_input, &input_state_dcs_handler },
  518. { 0x7f, 0xff, NULL, NULL },
  519. { -1, -1, NULL, NULL }
  520. };
  521. /* dcs_interm state table. */
  522. const struct input_transition input_state_dcs_intermediate_table[] = {
  523. INPUT_STATE_ANYWHERE,
  524. { 0x00, 0x17, NULL, NULL },
  525. { 0x19, 0x19, NULL, NULL },
  526. { 0x1c, 0x1f, NULL, NULL },
  527. { 0x20, 0x2f, input_intermediate, NULL },
  528. { 0x30, 0x3f, NULL, &input_state_dcs_ignore },
  529. { 0x40, 0x7e, input_input, &input_state_dcs_handler },
  530. { 0x7f, 0xff, NULL, NULL },
  531. { -1, -1, NULL, NULL }
  532. };
  533. /* dcs_handler state table. */
  534. const struct input_transition input_state_dcs_handler_table[] = {
  535. /* No INPUT_STATE_ANYWHERE */
  536. { 0x00, 0x1a, input_input, NULL },
  537. { 0x1b, 0x1b, NULL, &input_state_dcs_escape },
  538. { 0x1c, 0xff, input_input, NULL },
  539. { -1, -1, NULL, NULL }
  540. };
  541. /* dcs_escape state table. */
  542. const struct input_transition input_state_dcs_escape_table[] = {
  543. /* No INPUT_STATE_ANYWHERE */
  544. { 0x00, 0x5b, input_input, &input_state_dcs_handler },
  545. { 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
  546. { 0x5d, 0xff, input_input, &input_state_dcs_handler },
  547. { -1, -1, NULL, NULL }
  548. };
  549. /* dcs_ignore state table. */
  550. const struct input_transition input_state_dcs_ignore_table[] = {
  551. INPUT_STATE_ANYWHERE,
  552. { 0x00, 0x17, NULL, NULL },
  553. { 0x19, 0x19, NULL, NULL },
  554. { 0x1c, 0x1f, NULL, NULL },
  555. { 0x20, 0xff, NULL, NULL },
  556. { -1, -1, NULL, NULL }
  557. };
  558. /* osc_string state table. */
  559. const struct input_transition input_state_osc_string_table[] = {
  560. INPUT_STATE_ANYWHERE,
  561. { 0x00, 0x06, NULL, NULL },
  562. { 0x07, 0x07, NULL, &input_state_ground },
  563. { 0x08, 0x17, NULL, NULL },
  564. { 0x19, 0x19, NULL, NULL },
  565. { 0x1c, 0x1f, NULL, NULL },
  566. { 0x20, 0xff, input_input, NULL },
  567. { -1, -1, NULL, NULL }
  568. };
  569. /* apc_string state table. */
  570. const struct input_transition input_state_apc_string_table[] = {
  571. INPUT_STATE_ANYWHERE,
  572. { 0x00, 0x17, NULL, NULL },
  573. { 0x19, 0x19, NULL, NULL },
  574. { 0x1c, 0x1f, NULL, NULL },
  575. { 0x20, 0xff, input_input, NULL },
  576. { -1, -1, NULL, NULL }
  577. };
  578. /* rename_string state table. */
  579. const struct input_transition input_state_rename_string_table[] = {
  580. INPUT_STATE_ANYWHERE,
  581. { 0x00, 0x17, NULL, NULL },
  582. { 0x19, 0x19, NULL, NULL },
  583. { 0x1c, 0x1f, NULL, NULL },
  584. { 0x20, 0xff, input_input, NULL },
  585. { -1, -1, NULL, NULL }
  586. };
  587. /* consume_st state table. */
  588. const struct input_transition input_state_consume_st_table[] = {
  589. INPUT_STATE_ANYWHERE,
  590. { 0x00, 0x17, NULL, NULL },
  591. { 0x19, 0x19, NULL, NULL },
  592. { 0x1c, 0x1f, NULL, NULL },
  593. { 0x20, 0xff, NULL, NULL },
  594. { -1, -1, NULL, NULL }
  595. };
  596. /* utf8_three state table. */
  597. const struct input_transition input_state_utf8_three_table[] = {
  598. /* No INPUT_STATE_ANYWHERE */
  599. { 0x00, 0x7f, NULL, &input_state_ground },
  600. { 0x80, 0xbf, input_utf8_add, &input_state_utf8_two },
  601. { 0xc0, 0xff, NULL, &input_state_ground },
  602. { -1, -1, NULL, NULL }
  603. };
  604. /* utf8_two state table. */
  605. const struct input_transition input_state_utf8_two_table[] = {
  606. /* No INPUT_STATE_ANYWHERE */
  607. { 0x00, 0x7f, NULL, &input_state_ground },
  608. { 0x80, 0xbf, input_utf8_add, &input_state_utf8_one },
  609. { 0xc0, 0xff, NULL, &input_state_ground },
  610. { -1, -1, NULL, NULL }
  611. };
  612. /* utf8_one state table. */
  613. const struct input_transition input_state_utf8_one_table[] = {
  614. /* No INPUT_STATE_ANYWHERE */
  615. { 0x00, 0x7f, NULL, &input_state_ground },
  616. { 0x80, 0xbf, input_utf8_close, &input_state_ground },
  617. { 0xc0, 0xff, NULL, &input_state_ground },
  618. { -1, -1, NULL, NULL }
  619. };
  620. /* Input table compare. */
  621. int
  622. input_table_compare(const void *key, const void *value)
  623. {
  624. const struct input_ctx *ictx = key;
  625. const struct input_table_entry *entry = value;
  626. if (ictx->ch != entry->ch)
  627. return (ictx->ch - entry->ch);
  628. return (strcmp(ictx->interm_buf, entry->interm));
  629. }
  630. /* Reset cell state to default. */
  631. void
  632. input_reset_cell(struct input_ctx *ictx)
  633. {
  634. memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
  635. ictx->cell.set = 0;
  636. ictx->cell.g0set = ictx->cell.g1set = 0;
  637. memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
  638. ictx->old_cx = 0;
  639. ictx->old_cy = 0;
  640. }
  641. /* Initialise input parser. */
  642. void
  643. input_init(struct window_pane *wp)
  644. {
  645. struct input_ctx *ictx;
  646. ictx = wp->ictx = xcalloc(1, sizeof *ictx);
  647. ictx->input_space = INPUT_BUF_START;
  648. ictx->input_buf = xmalloc(INPUT_BUF_START);
  649. ictx->since_ground = evbuffer_new();
  650. input_reset(wp, 0);
  651. }
  652. /* Destroy input parser. */
  653. void
  654. input_free(struct window_pane *wp)
  655. {
  656. struct input_ctx *ictx = wp->ictx;
  657. free(ictx->input_buf);
  658. evbuffer_free(ictx->since_ground);
  659. free (ictx);
  660. wp->ictx = NULL;
  661. }
  662. /* Reset input state and clear screen. */
  663. void
  664. input_reset(struct window_pane *wp, int clear)
  665. {
  666. struct input_ctx *ictx = wp->ictx;
  667. input_reset_cell(ictx);
  668. if (clear) {
  669. if (wp->mode == NULL)
  670. screen_write_start(&ictx->ctx, wp, &wp->base);
  671. else
  672. screen_write_start(&ictx->ctx, NULL, &wp->base);
  673. screen_write_reset(&ictx->ctx);
  674. screen_write_stop(&ictx->ctx);
  675. }
  676. *ictx->interm_buf = '\0';
  677. ictx->interm_len = 0;
  678. *ictx->param_buf = '\0';
  679. ictx->param_len = 0;
  680. *ictx->input_buf = '\0';
  681. ictx->input_len = 0;
  682. ictx->state = &input_state_ground;
  683. ictx->flags = 0;
  684. }
  685. /* Return pending data. */
  686. struct evbuffer *
  687. input_pending(struct window_pane *wp)
  688. {
  689. return (wp->ictx->since_ground);
  690. }
  691. /* Change input state. */
  692. void
  693. input_set_state(struct window_pane *wp, const struct input_transition *itr)
  694. {
  695. struct input_ctx *ictx = wp->ictx;
  696. if (ictx->state->exit != NULL)
  697. ictx->state->exit(ictx);
  698. ictx->state = itr->state;
  699. if (ictx->state->enter != NULL)
  700. ictx->state->enter(ictx);
  701. }
  702. /* Parse input. */
  703. void
  704. input_parse(struct window_pane *wp)
  705. {
  706. struct input_ctx *ictx = wp->ictx;
  707. const struct input_transition *itr;
  708. struct evbuffer *evb = wp->event->input;
  709. u_char *buf;
  710. size_t len, off;
  711. if (EVBUFFER_LENGTH(evb) == 0)
  712. return;
  713. window_update_activity(wp->window);
  714. wp->flags |= PANE_CHANGED;
  715. /*
  716. * Open the screen. Use NULL wp if there is a mode set as don't want to
  717. * update the tty.
  718. */
  719. if (wp->mode == NULL)
  720. screen_write_start(&ictx->ctx, wp, &wp->base);
  721. else
  722. screen_write_start(&ictx->ctx, NULL, &wp->base);
  723. ictx->wp = wp;
  724. buf = EVBUFFER_DATA(evb);
  725. len = EVBUFFER_LENGTH(evb);
  726. notify_input(wp, evb);
  727. off = 0;
  728. log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
  729. ictx->state->name, len, (int)len, buf);
  730. /* Parse the input. */
  731. while (off < len) {
  732. ictx->ch = buf[off++];
  733. /* Find the transition. */
  734. itr = ictx->state->transitions;
  735. while (itr->first != -1 && itr->last != -1) {
  736. if (ictx->ch >= itr->first && ictx->ch <= itr->last)
  737. break;
  738. itr++;
  739. }
  740. if (itr->first == -1 || itr->last == -1) {
  741. /* No transition? Eh? */
  742. fatalx("no transition from state");
  743. }
  744. /*
  745. * Execute the handler, if any. Don't switch state if it
  746. * returns non-zero.
  747. */
  748. if (itr->handler != NULL && itr->handler(ictx) != 0)
  749. continue;
  750. /* And switch state, if necessary. */
  751. if (itr->state != NULL)
  752. input_set_state(wp, itr);
  753. /* If not in ground state, save input. */
  754. if (ictx->state != &input_state_ground)
  755. evbuffer_add(ictx->since_ground, &ictx->ch, 1);
  756. }
  757. /* Close the screen. */
  758. screen_write_stop(&ictx->ctx);
  759. evbuffer_drain(evb, len);
  760. }
  761. /* Split the parameter list (if any). */
  762. int
  763. input_split(struct input_ctx *ictx)
  764. {
  765. const char *errstr;
  766. char *ptr, *out;
  767. int n;
  768. ictx->param_list_len = 0;
  769. if (ictx->param_len == 0)
  770. return (0);
  771. ptr = ictx->param_buf;
  772. while ((out = strsep(&ptr, ";")) != NULL) {
  773. if (*out == '\0')
  774. n = -1;
  775. else {
  776. n = strtonum(out, 0, INT_MAX, &errstr);
  777. if (errstr != NULL)
  778. return (-1);
  779. }
  780. ictx->param_list[ictx->param_list_len++] = n;
  781. if (ictx->param_list_len == nitems(ictx->param_list))
  782. return (-1);
  783. }
  784. return (0);
  785. }
  786. /* Get an argument or return default value. */
  787. int
  788. input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
  789. {
  790. int retval;
  791. if (validx >= ictx->param_list_len)
  792. return (defval);
  793. retval = ictx->param_list[validx];
  794. if (retval == -1)
  795. return (defval);
  796. if (retval < minval)
  797. return (minval);
  798. return (retval);
  799. }
  800. /* Reply to terminal query. */
  801. void
  802. input_reply(struct input_ctx *ictx, const char *fmt, ...)
  803. {
  804. va_list ap;
  805. char *reply;
  806. va_start(ap, fmt);
  807. vasprintf(&reply, fmt, ap);
  808. va_end(ap);
  809. bufferevent_write(ictx->wp->event, reply, strlen(reply));
  810. free(reply);
  811. }
  812. /* Clear saved state. */
  813. void
  814. input_clear(struct input_ctx *ictx)
  815. {
  816. *ictx->interm_buf = '\0';
  817. ictx->interm_len = 0;
  818. *ictx->param_buf = '\0';
  819. ictx->param_len = 0;
  820. *ictx->input_buf = '\0';
  821. ictx->input_len = 0;
  822. ictx->flags &= ~INPUT_DISCARD;
  823. }
  824. /* Reset for ground state. */
  825. void
  826. input_ground(struct input_ctx *ictx)
  827. {
  828. evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
  829. if (ictx->input_space > INPUT_BUF_START) {
  830. ictx->input_space = INPUT_BUF_START;
  831. ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
  832. }
  833. }
  834. /* Output this character to the screen. */
  835. int
  836. input_print(struct input_ctx *ictx)
  837. {
  838. int set;
  839. set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
  840. if (set == 1)
  841. ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
  842. else
  843. ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
  844. utf8_set(&ictx->cell.cell.data, ictx->ch);
  845. screen_write_cell(&ictx->ctx, &ictx->cell.cell);
  846. ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
  847. return (0);
  848. }
  849. /* Collect intermediate string. */
  850. int
  851. input_intermediate(struct input_ctx *ictx)
  852. {
  853. if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
  854. ictx->flags |= INPUT_DISCARD;
  855. else {
  856. ictx->interm_buf[ictx->interm_len++] = ictx->ch;
  857. ictx->interm_buf[ictx->interm_len] = '\0';
  858. }
  859. return (0);
  860. }
  861. /* Collect parameter string. */
  862. int
  863. input_parameter(struct input_ctx *ictx)
  864. {
  865. if (ictx->param_len == (sizeof ictx->param_buf) - 1)
  866. ictx->flags |= INPUT_DISCARD;
  867. else {
  868. ictx->param_buf[ictx->param_len++] = ictx->ch;
  869. ictx->param_buf[ictx->param_len] = '\0';
  870. }
  871. return (0);
  872. }
  873. /* Collect input string. */
  874. int
  875. input_input(struct input_ctx *ictx)
  876. {
  877. size_t available;
  878. available = ictx->input_space;
  879. while (ictx->input_len + 1 >= available) {
  880. available *= 2;
  881. if (available > INPUT_BUF_LIMIT) {
  882. ictx->flags |= INPUT_DISCARD;
  883. return (0);
  884. }
  885. ictx->input_buf = xrealloc(ictx->input_buf, available);
  886. ictx->input_space = available;
  887. }
  888. ictx->input_buf[ictx->input_len++] = ictx->ch;
  889. ictx->input_buf[ictx->input_len] = '\0';
  890. return (0);
  891. }
  892. /* Execute C0 control sequence. */
  893. int
  894. input_c0_dispatch(struct input_ctx *ictx)
  895. {
  896. struct screen_write_ctx *sctx = &ictx->ctx;
  897. struct window_pane *wp = ictx->wp;
  898. struct screen *s = sctx->s;
  899. log_debug("%s: '%c'", __func__, ictx->ch);
  900. switch (ictx->ch) {
  901. case '\000': /* NUL */
  902. break;
  903. case '\007': /* BEL */
  904. alerts_queue(wp->window, WINDOW_BELL);
  905. break;
  906. case '\010': /* BS */
  907. screen_write_backspace(sctx);
  908. break;
  909. case '\011': /* HT */
  910. /* Don't tab beyond the end of the line. */
  911. if (s->cx >= screen_size_x(s) - 1)
  912. break;
  913. /* Find the next tab point, or use the last column if none. */
  914. do {
  915. s->cx++;
  916. if (bit_test(s->tabs, s->cx))
  917. break;
  918. } while (s->cx < screen_size_x(s) - 1);
  919. break;
  920. case '\012': /* LF */
  921. case '\013': /* VT */
  922. case '\014': /* FF */
  923. screen_write_linefeed(sctx, 0);
  924. break;
  925. case '\015': /* CR */
  926. screen_write_carriagereturn(sctx);
  927. break;
  928. case '\016': /* SO */
  929. ictx->cell.set = 1;
  930. break;
  931. case '\017': /* SI */
  932. ictx->cell.set = 0;
  933. break;
  934. default:
  935. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  936. break;
  937. }
  938. return (0);
  939. }
  940. /* Execute escape sequence. */
  941. int
  942. input_esc_dispatch(struct input_ctx *ictx)
  943. {
  944. struct screen_write_ctx *sctx = &ictx->ctx;
  945. struct screen *s = sctx->s;
  946. struct input_table_entry *entry;
  947. if (ictx->flags & INPUT_DISCARD)
  948. return (0);
  949. log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
  950. entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
  951. sizeof input_esc_table[0], input_table_compare);
  952. if (entry == NULL) {
  953. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  954. return (0);
  955. }
  956. switch (entry->type) {
  957. case INPUT_ESC_RIS:
  958. input_reset_cell(ictx);
  959. screen_write_reset(sctx);
  960. break;
  961. case INPUT_ESC_IND:
  962. screen_write_linefeed(sctx, 0);
  963. break;
  964. case INPUT_ESC_NEL:
  965. screen_write_carriagereturn(sctx);
  966. screen_write_linefeed(sctx, 0);
  967. break;
  968. case INPUT_ESC_HTS:
  969. if (s->cx < screen_size_x(s))
  970. bit_set(s->tabs, s->cx);
  971. break;
  972. case INPUT_ESC_RI:
  973. screen_write_reverseindex(sctx);
  974. break;
  975. case INPUT_ESC_DECKPAM:
  976. screen_write_mode_set(sctx, MODE_KKEYPAD);
  977. break;
  978. case INPUT_ESC_DECKPNM:
  979. screen_write_mode_clear(sctx, MODE_KKEYPAD);
  980. break;
  981. case INPUT_ESC_DECSC:
  982. memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
  983. ictx->old_cx = s->cx;
  984. ictx->old_cy = s->cy;
  985. break;
  986. case INPUT_ESC_DECRC:
  987. memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
  988. screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
  989. break;
  990. case INPUT_ESC_DECALN:
  991. screen_write_alignmenttest(sctx);
  992. break;
  993. case INPUT_ESC_SCSG0_ON:
  994. ictx->cell.g0set = 1;
  995. break;
  996. case INPUT_ESC_SCSG0_OFF:
  997. ictx->cell.g0set = 0;
  998. break;
  999. case INPUT_ESC_SCSG1_ON:
  1000. ictx->cell.g1set = 1;
  1001. break;
  1002. case INPUT_ESC_SCSG1_OFF:
  1003. ictx->cell.g1set = 0;
  1004. break;
  1005. }
  1006. return (0);
  1007. }
  1008. /* Execute control sequence. */
  1009. int
  1010. input_csi_dispatch(struct input_ctx *ictx)
  1011. {
  1012. struct screen_write_ctx *sctx = &ictx->ctx;
  1013. struct screen *s = sctx->s;
  1014. struct input_table_entry *entry;
  1015. int n, m;
  1016. u_int cx;
  1017. if (ictx->flags & INPUT_DISCARD)
  1018. return (0);
  1019. if (input_split(ictx) != 0)
  1020. return (0);
  1021. log_debug("%s: '%c' \"%s\" \"%s\"",
  1022. __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
  1023. entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
  1024. sizeof input_csi_table[0], input_table_compare);
  1025. if (entry == NULL) {
  1026. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1027. return (0);
  1028. }
  1029. switch (entry->type) {
  1030. case INPUT_CSI_CBT:
  1031. /* Find the previous tab point, n times. */
  1032. cx = s->cx;
  1033. if (cx > screen_size_x(s) - 1)
  1034. cx = screen_size_x(s) - 1;
  1035. n = input_get(ictx, 0, 1, 1);
  1036. while (cx > 0 && n-- > 0) {
  1037. do
  1038. cx--;
  1039. while (cx > 0 && !bit_test(s->tabs, cx));
  1040. }
  1041. s->cx = cx;
  1042. break;
  1043. case INPUT_CSI_CUB:
  1044. screen_write_cursorleft(sctx, input_get(ictx, 0, 1, 1));
  1045. break;
  1046. case INPUT_CSI_CUD:
  1047. screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
  1048. break;
  1049. case INPUT_CSI_CUF:
  1050. screen_write_cursorright(sctx, input_get(ictx, 0, 1, 1));
  1051. break;
  1052. case INPUT_CSI_CUP:
  1053. n = input_get(ictx, 0, 1, 1);
  1054. m = input_get(ictx, 1, 1, 1);
  1055. screen_write_cursormove(sctx, m - 1, n - 1);
  1056. break;
  1057. case INPUT_CSI_WINOPS:
  1058. input_csi_dispatch_winops(ictx);
  1059. break;
  1060. case INPUT_CSI_CUU:
  1061. screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
  1062. break;
  1063. case INPUT_CSI_CNL:
  1064. screen_write_carriagereturn(sctx);
  1065. screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
  1066. break;
  1067. case INPUT_CSI_CPL:
  1068. screen_write_carriagereturn(sctx);
  1069. screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
  1070. break;
  1071. case INPUT_CSI_DA:
  1072. switch (input_get(ictx, 0, 0, 0)) {
  1073. case 0:
  1074. input_reply(ictx, "\033[?1;2c");
  1075. break;
  1076. default:
  1077. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1078. break;
  1079. }
  1080. break;
  1081. case INPUT_CSI_DA_TWO:
  1082. switch (input_get(ictx, 0, 0, 0)) {
  1083. case 0:
  1084. input_reply(ictx, "\033[>84;0;0c");
  1085. break;
  1086. default:
  1087. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1088. break;
  1089. }
  1090. break;
  1091. case INPUT_CSI_ECH:
  1092. screen_write_clearcharacter(sctx, input_get(ictx, 0, 1, 1));
  1093. break;
  1094. case INPUT_CSI_DCH:
  1095. screen_write_deletecharacter(sctx, input_get(ictx, 0, 1, 1));
  1096. break;
  1097. case INPUT_CSI_DECSTBM:
  1098. n = input_get(ictx, 0, 1, 1);
  1099. m = input_get(ictx, 1, 1, screen_size_y(s));
  1100. screen_write_scrollregion(sctx, n - 1, m - 1);
  1101. break;
  1102. case INPUT_CSI_DL:
  1103. screen_write_deleteline(sctx, input_get(ictx, 0, 1, 1));
  1104. break;
  1105. case INPUT_CSI_DSR:
  1106. switch (input_get(ictx, 0, 0, 0)) {
  1107. case 5:
  1108. input_reply(ictx, "\033[0n");
  1109. break;
  1110. case 6:
  1111. input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
  1112. break;
  1113. default:
  1114. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1115. break;
  1116. }
  1117. break;
  1118. case INPUT_CSI_ED:
  1119. switch (input_get(ictx, 0, 0, 0)) {
  1120. case 0:
  1121. screen_write_clearendofscreen(sctx);
  1122. break;
  1123. case 1:
  1124. screen_write_clearstartofscreen(sctx);
  1125. break;
  1126. case 2:
  1127. screen_write_clearscreen(sctx);
  1128. break;
  1129. case 3:
  1130. switch (input_get(ictx, 1, 0, 0)) {
  1131. case 0:
  1132. /*
  1133. * Linux console extension to clear history
  1134. * (for example before locking the screen).
  1135. */
  1136. screen_write_clearhistory(sctx);
  1137. break;
  1138. }
  1139. break;
  1140. default:
  1141. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1142. break;
  1143. }
  1144. break;
  1145. case INPUT_CSI_EL:
  1146. switch (input_get(ictx, 0, 0, 0)) {
  1147. case 0:
  1148. screen_write_clearendofline(sctx);
  1149. break;
  1150. case 1:
  1151. screen_write_clearstartofline(sctx);
  1152. break;
  1153. case 2:
  1154. screen_write_clearline(sctx);
  1155. break;
  1156. default:
  1157. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1158. break;
  1159. }
  1160. break;
  1161. case INPUT_CSI_HPA:
  1162. n = input_get(ictx, 0, 1, 1);
  1163. screen_write_cursormove(sctx, n - 1, s->cy);
  1164. break;
  1165. case INPUT_CSI_ICH:
  1166. screen_write_insertcharacter(sctx, input_get(ictx, 0, 1, 1));
  1167. break;
  1168. case INPUT_CSI_IL:
  1169. screen_write_insertline(sctx, input_get(ictx, 0, 1, 1));
  1170. break;
  1171. case INPUT_CSI_RCP:
  1172. memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
  1173. screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
  1174. break;
  1175. case INPUT_CSI_RM:
  1176. input_csi_dispatch_rm(ictx);
  1177. break;
  1178. case INPUT_CSI_RM_PRIVATE:
  1179. input_csi_dispatch_rm_private(ictx);
  1180. break;
  1181. case INPUT_CSI_SCP:
  1182. memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
  1183. ictx->old_cx = s->cx;
  1184. ictx->old_cy = s->cy;
  1185. break;
  1186. case INPUT_CSI_SGR:
  1187. input_csi_dispatch_sgr(ictx);
  1188. break;
  1189. case INPUT_CSI_SM:
  1190. input_csi_dispatch_sm(ictx);
  1191. break;
  1192. case INPUT_CSI_SM_PRIVATE:
  1193. input_csi_dispatch_sm_private(ictx);
  1194. break;
  1195. case INPUT_CSI_TBC:
  1196. switch (input_get(ictx, 0, 0, 0)) {
  1197. case 0:
  1198. if (s->cx < screen_size_x(s))
  1199. bit_clear(s->tabs, s->cx);
  1200. break;
  1201. case 3:
  1202. bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
  1203. break;
  1204. default:
  1205. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1206. break;
  1207. }
  1208. break;
  1209. case INPUT_CSI_VPA:
  1210. n = input_get(ictx, 0, 1, 1);
  1211. screen_write_cursormove(sctx, s->cx, n - 1);
  1212. break;
  1213. case INPUT_CSI_DECSCUSR:
  1214. n = input_get(ictx, 0, 0, 0);
  1215. screen_set_cursor_style(s, n);
  1216. break;
  1217. }
  1218. return (0);
  1219. }
  1220. /* Handle CSI RM. */
  1221. void
  1222. input_csi_dispatch_rm(struct input_ctx *ictx)
  1223. {
  1224. u_int i;
  1225. for (i = 0; i < ictx->param_list_len; i++) {
  1226. switch (input_get(ictx, i, 0, -1)) {
  1227. case 4: /* IRM */
  1228. screen_write_mode_clear(&ictx->ctx, MODE_INSERT);
  1229. break;
  1230. case 34:
  1231. screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
  1232. break;
  1233. default:
  1234. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1235. break;
  1236. }
  1237. }
  1238. }
  1239. /* Handle CSI private RM. */
  1240. void
  1241. input_csi_dispatch_rm_private(struct input_ctx *ictx)
  1242. {
  1243. struct window_pane *wp = ictx->wp;
  1244. u_int i;
  1245. for (i = 0; i < ictx->param_list_len; i++) {
  1246. switch (input_get(ictx, i, 0, -1)) {
  1247. case 1: /* DECCKM */
  1248. screen_write_mode_clear(&ictx->ctx, MODE_KCURSOR);
  1249. break;
  1250. case 3: /* DECCOLM */
  1251. screen_write_cursormove(&ictx->ctx, 0, 0);
  1252. screen_write_clearscreen(&ictx->ctx);
  1253. break;
  1254. case 7: /* DECAWM */
  1255. screen_write_mode_clear(&ictx->ctx, MODE_WRAP);
  1256. break;
  1257. case 12:
  1258. screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
  1259. break;
  1260. case 25: /* TCEM */
  1261. screen_write_mode_clear(&ictx->ctx, MODE_CURSOR);
  1262. break;
  1263. case 1000:
  1264. case 1001:
  1265. case 1002:
  1266. screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
  1267. break;
  1268. case 1004:
  1269. screen_write_mode_clear(&ictx->ctx, MODE_FOCUSON);
  1270. break;
  1271. case 1005:
  1272. screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_UTF8);
  1273. break;
  1274. case 1006:
  1275. screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_SGR);
  1276. break;
  1277. case 47:
  1278. case 1047:
  1279. window_pane_alternate_off(wp, &ictx->cell.cell, 0);
  1280. break;
  1281. case 1049:
  1282. window_pane_alternate_off(wp, &ictx->cell.cell, 1);
  1283. break;
  1284. case 2004:
  1285. screen_write_mode_clear(&ictx->ctx, MODE_BRACKETPASTE);
  1286. break;
  1287. default:
  1288. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. /* Handle CSI SM. */
  1294. void
  1295. input_csi_dispatch_sm(struct input_ctx *ictx)
  1296. {
  1297. u_int i;
  1298. for (i = 0; i < ictx->param_list_len; i++) {
  1299. switch (input_get(ictx, i, 0, -1)) {
  1300. case 4: /* IRM */
  1301. screen_write_mode_set(&ictx->ctx, MODE_INSERT);
  1302. break;
  1303. case 34:
  1304. screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
  1305. break;
  1306. default:
  1307. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1308. break;
  1309. }
  1310. }
  1311. }
  1312. /* Handle CSI private SM. */
  1313. void
  1314. input_csi_dispatch_sm_private(struct input_ctx *ictx)
  1315. {
  1316. struct window_pane *wp = ictx->wp;
  1317. u_int i;
  1318. for (i = 0; i < ictx->param_list_len; i++) {
  1319. switch (input_get(ictx, i, 0, -1)) {
  1320. case 1: /* DECCKM */
  1321. screen_write_mode_set(&ictx->ctx, MODE_KCURSOR);
  1322. break;
  1323. case 3: /* DECCOLM */
  1324. screen_write_cursormove(&ictx->ctx, 0, 0);
  1325. screen_write_clearscreen(&ictx->ctx);
  1326. break;
  1327. case 7: /* DECAWM */
  1328. screen_write_mode_set(&ictx->ctx, MODE_WRAP);
  1329. break;
  1330. case 12:
  1331. screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
  1332. break;
  1333. case 25: /* TCEM */
  1334. screen_write_mode_set(&ictx->ctx, MODE_CURSOR);
  1335. break;
  1336. case 1000:
  1337. screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
  1338. screen_write_mode_set(&ictx->ctx, MODE_MOUSE_STANDARD);
  1339. break;
  1340. case 1002:
  1341. screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
  1342. screen_write_mode_set(&ictx->ctx, MODE_MOUSE_BUTTON);
  1343. break;
  1344. case 1004:
  1345. if (ictx->ctx.s->mode & MODE_FOCUSON)
  1346. break;
  1347. screen_write_mode_set(&ictx->ctx, MODE_FOCUSON);
  1348. wp->flags |= PANE_FOCUSPUSH; /* force update */
  1349. break;
  1350. case 1005:
  1351. screen_write_mode_set(&ictx->ctx, MODE_MOUSE_UTF8);
  1352. break;
  1353. case 1006:
  1354. screen_write_mode_set(&ictx->ctx, MODE_MOUSE_SGR);
  1355. break;
  1356. case 47:
  1357. case 1047:
  1358. window_pane_alternate_on(wp, &ictx->cell.cell, 0);
  1359. break;
  1360. case 1049:
  1361. window_pane_alternate_on(wp, &ictx->cell.cell, 1);
  1362. break;
  1363. case 2004:
  1364. screen_write_mode_set(&ictx->ctx, MODE_BRACKETPASTE);
  1365. break;
  1366. default:
  1367. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1368. break;
  1369. }
  1370. }
  1371. }
  1372. /* Handle CSI window operations. */
  1373. void
  1374. input_csi_dispatch_winops(struct input_ctx *ictx)
  1375. {
  1376. struct window_pane *wp = ictx->wp;
  1377. int n, m;
  1378. m = 0;
  1379. while ((n = input_get(ictx, m, 0, -1)) != -1) {
  1380. switch (n) {
  1381. case 1:
  1382. case 2:
  1383. case 5:
  1384. case 6:
  1385. case 7:
  1386. case 11:
  1387. case 13:
  1388. case 14:
  1389. case 19:
  1390. case 20:
  1391. case 21:
  1392. case 24:
  1393. break;
  1394. case 3:
  1395. case 4:
  1396. case 8:
  1397. m++;
  1398. if (input_get(ictx, m, 0, -1) == -1)
  1399. return;
  1400. /* FALLTHROUGH */
  1401. case 9:
  1402. case 10:
  1403. case 22:
  1404. case 23:
  1405. m++;
  1406. if (input_get(ictx, m, 0, -1) == -1)
  1407. return;
  1408. break;
  1409. case 18:
  1410. input_reply(ictx, "\033[8;%u;%ut", wp->sy, wp->sx);
  1411. break;
  1412. default:
  1413. log_debug("%s: unknown '%c'", __func__, ictx->ch);
  1414. break;
  1415. }
  1416. m++;
  1417. }
  1418. }
  1419. /* Handle CSI SGR for 256 colours. */
  1420. void
  1421. input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
  1422. {
  1423. struct grid_cell *gc = &ictx->cell.cell;
  1424. int c;
  1425. (*i)++;
  1426. c = input_get(ictx, *i, 0, -1);
  1427. if (c == -1) {
  1428. if (fgbg == 38) {
  1429. gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
  1430. gc->fg = 8;
  1431. } else if (fgbg == 48) {
  1432. gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
  1433. gc->bg = 8;
  1434. }
  1435. } else {
  1436. if (fgbg == 38) {
  1437. gc->flags |= GRID_FLAG_FG256;
  1438. gc->flags &= ~GRID_FLAG_FGRGB;
  1439. gc->fg = c;
  1440. } else if (fgbg == 48) {
  1441. gc->flags |= GRID_FLAG_BG256;
  1442. gc->flags &= ~GRID_FLAG_BGRGB;
  1443. gc->bg = c;
  1444. }
  1445. }
  1446. }
  1447. /* Handle CSI SGR for RGB colours. */
  1448. void
  1449. input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
  1450. {
  1451. struct grid_cell *gc = &ictx->cell.cell;
  1452. int r, g, b;
  1453. (*i)++;
  1454. r = input_get(ictx, *i, 0, -1);
  1455. if (r == -1 || r > 255)
  1456. return;
  1457. (*i)++;
  1458. g = input_get(ictx, *i, 0, -1);
  1459. if (g == -1 || g > 255)
  1460. return;
  1461. (*i)++;
  1462. b = input_get(ictx, *i, 0, -1);
  1463. if (b == -1 || b > 255)
  1464. return;
  1465. if (fgbg == 38) {
  1466. gc->flags &= ~GRID_FLAG_FG256;
  1467. gc->flags |= GRID_FLAG_FGRGB;
  1468. gc->fg_rgb.r = r;
  1469. gc->fg_rgb.g = g;
  1470. gc->fg_rgb.b = b;
  1471. } else if (fgbg == 48) {
  1472. gc->flags &= ~GRID_FLAG_BG256;
  1473. gc->flags |= GRID_FLAG_BGRGB;
  1474. gc->bg_rgb.r = r;
  1475. gc->bg_rgb.g = g;
  1476. gc->bg_rgb.b = b;
  1477. }
  1478. }
  1479. /* Handle CSI SGR. */
  1480. void
  1481. input_csi_dispatch_sgr(struct input_ctx *ictx)
  1482. {
  1483. struct grid_cell *gc = &ictx->cell.cell;
  1484. u_int i;
  1485. int n;
  1486. if (ictx->param_list_len == 0) {
  1487. memcpy(gc, &grid_default_cell, sizeof *gc);
  1488. return;
  1489. }
  1490. for (i = 0; i < ictx->param_list_len; i++) {
  1491. n = input_get(ictx, i, 0, 0);
  1492. if (n == 38 || n == 48) {
  1493. i++;
  1494. switch (input_get(ictx, i, 0, -1)) {
  1495. case 2:
  1496. input_csi_dispatch_sgr_rgb(ictx, n, &i);
  1497. break;
  1498. case 5:
  1499. input_csi_dispatch_sgr_256(ictx, n, &i);
  1500. break;
  1501. }
  1502. continue;
  1503. }
  1504. switch (n) {
  1505. case 0:
  1506. case 10:
  1507. memcpy(gc, &grid_default_cell, sizeof *gc);
  1508. break;
  1509. case 1:
  1510. gc->attr |= GRID_ATTR_BRIGHT;
  1511. break;
  1512. case 2:
  1513. gc->attr |= GRID_ATTR_DIM;
  1514. break;
  1515. case 3:
  1516. gc->attr |= GRID_ATTR_ITALICS;
  1517. break;
  1518. case 4:
  1519. gc->attr |= GRID_ATTR_UNDERSCORE;
  1520. break;
  1521. case 5:
  1522. gc->attr |= GRID_ATTR_BLINK;
  1523. break;
  1524. case 7:
  1525. gc->attr |= GRID_ATTR_REVERSE;
  1526. break;
  1527. case 8:
  1528. gc->attr |= GRID_ATTR_HIDDEN;
  1529. break;
  1530. case 22:
  1531. gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
  1532. break;
  1533. case 23:
  1534. gc->attr &= ~GRID_ATTR_ITALICS;
  1535. break;
  1536. case 24:
  1537. gc->attr &= ~GRID_ATTR_UNDERSCORE;
  1538. break;
  1539. case 25:
  1540. gc->attr &= ~GRID_ATTR_BLINK;
  1541. break;
  1542. case 27:
  1543. gc->attr &= ~GRID_ATTR_REVERSE;
  1544. break;
  1545. case 30:
  1546. case 31:
  1547. case 32:
  1548. case 33:
  1549. case 34:
  1550. case 35:
  1551. case 36:
  1552. case 37:
  1553. gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
  1554. gc->fg = n - 30;
  1555. break;
  1556. case 39:
  1557. gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
  1558. gc->fg = 8;
  1559. break;
  1560. case 40:
  1561. case 41:
  1562. case 42:
  1563. case 43:
  1564. case 44:
  1565. case 45:
  1566. case 46:
  1567. case 47:
  1568. gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
  1569. gc->bg = n - 40;
  1570. break;
  1571. case 49:
  1572. gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
  1573. gc->bg = 8;
  1574. break;
  1575. case 90:
  1576. case 91:
  1577. case 92:
  1578. case 93:
  1579. case 94:
  1580. case 95:
  1581. case 96:
  1582. case 97:
  1583. gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
  1584. gc->fg = n;
  1585. break;
  1586. case 100:
  1587. case 101:
  1588. case 102:
  1589. case 103:
  1590. case 104:
  1591. case 105:
  1592. case 106:
  1593. case 107:
  1594. gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
  1595. gc->bg = n - 10;
  1596. break;
  1597. }
  1598. }
  1599. }
  1600. /* DCS terminator (ST) received. */
  1601. int
  1602. input_dcs_dispatch(struct input_ctx *ictx)
  1603. {
  1604. const char prefix[] = "tmux;";
  1605. const u_int prefix_len = (sizeof prefix) - 1;
  1606. if (ictx->flags & INPUT_DISCARD)
  1607. return (0);
  1608. log_debug("%s: \"%s\"", __func__, ictx->input_buf);
  1609. /* Check for tmux prefix. */
  1610. if (ictx->input_len >= prefix_len &&
  1611. strncmp(ictx->input_buf, prefix, prefix_len) == 0) {
  1612. screen_write_rawstring(&ictx->ctx,
  1613. ictx->input_buf + prefix_len, ictx->input_len - prefix_len);
  1614. }
  1615. return (0);
  1616. }
  1617. /* OSC string started. */
  1618. void
  1619. input_enter_osc(struct input_ctx *ictx)
  1620. {
  1621. log_debug("%s", __func__);
  1622. input_clear(ictx);
  1623. }
  1624. /* OSC terminator (ST) received. */
  1625. void
  1626. input_exit_osc(struct input_ctx *ictx)
  1627. {
  1628. u_char *p = ictx->input_buf;
  1629. u_int option;
  1630. if (ictx->flags & INPUT_DISCARD)
  1631. return;
  1632. if (ictx->input_len < 1 || *p < '0' || *p > '9')
  1633. return;
  1634. log_debug("%s: \"%s\"", __func__, p);
  1635. option = 0;
  1636. while (*p >= '0' && *p <= '9')
  1637. option = option * 10 + *p++ - '0';
  1638. if (*p == ';')
  1639. p++;
  1640. switch (option) {
  1641. case 0:
  1642. case 2:
  1643. screen_set_title(ictx->ctx.s, p);
  1644. server_status_window(ictx->wp->window);
  1645. break;
  1646. case 12:
  1647. if (*p != '?') /* ? is colour request */
  1648. screen_set_cursor_colour(ictx->ctx.s, p);
  1649. break;
  1650. case 112:
  1651. if (*p == '\0') /* no arguments allowed */
  1652. screen_set_cursor_colour(ictx->ctx.s, "");
  1653. break;
  1654. default:
  1655. log_debug("%s: unknown '%u'", __func__, option);
  1656. break;
  1657. }
  1658. }
  1659. /* APC string started. */
  1660. void
  1661. input_enter_apc(struct input_ctx *ictx)
  1662. {
  1663. log_debug("%s", __func__);
  1664. input_clear(ictx);
  1665. }
  1666. /* APC terminator (ST) received. */
  1667. void
  1668. input_exit_apc(struct input_ctx *ictx)
  1669. {
  1670. if (ictx->flags & INPUT_DISCARD)
  1671. return;
  1672. log_debug("%s: \"%s\"", __func__, ictx->input_buf);
  1673. screen_set_title(ictx->ctx.s, ictx->input_buf);
  1674. server_status_window(ictx->wp->window);
  1675. }
  1676. /* Rename string started. */
  1677. void
  1678. input_enter_rename(struct input_ctx *ictx)
  1679. {
  1680. log_debug("%s", __func__);
  1681. input_clear(ictx);
  1682. }
  1683. /* Rename terminator (ST) received. */
  1684. void
  1685. input_exit_rename(struct input_ctx *ictx)
  1686. {
  1687. if (ictx->flags & INPUT_DISCARD)
  1688. return;
  1689. if (!options_get_number(ictx->wp->window->options, "allow-rename"))
  1690. return;
  1691. log_debug("%s: \"%s\"", __func__, ictx->input_buf);
  1692. window_set_name(ictx->wp->window, ictx->input_buf);
  1693. options_set_number(ictx->wp->window->options, "automatic-rename", 0);
  1694. server_status_window(ictx->wp->window);
  1695. }
  1696. /* Open UTF-8 character. */
  1697. int
  1698. input_utf8_open(struct input_ctx *ictx)
  1699. {
  1700. struct utf8_data *ud = &ictx->utf8data;
  1701. if (utf8_open(ud, ictx->ch) != UTF8_MORE)
  1702. fatalx("UTF-8 open invalid %#x", ictx->ch);
  1703. log_debug("%s %hhu", __func__, ud->size);
  1704. return (0);
  1705. }
  1706. /* Append to UTF-8 character. */
  1707. int
  1708. input_utf8_add(struct input_ctx *ictx)
  1709. {
  1710. struct utf8_data *ud = &ictx->utf8data;
  1711. if (utf8_append(ud, ictx->ch) != UTF8_MORE)
  1712. fatalx("UTF-8 add invalid %#x", ictx->ch);
  1713. log_debug("%s", __func__);
  1714. return (0);
  1715. }
  1716. /* Close UTF-8 string. */
  1717. int
  1718. input_utf8_close(struct input_ctx *ictx)
  1719. {
  1720. struct utf8_data *ud = &ictx->utf8data;
  1721. if (utf8_append(ud, ictx->ch) != UTF8_DONE) {
  1722. /*
  1723. * An error here could be invalid UTF-8 or it could be a
  1724. * nonprintable character for which we can't get the
  1725. * width. Drop it.
  1726. */
  1727. return (0);
  1728. }
  1729. log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
  1730. (int)ud->size, ud->data, ud->width);
  1731. utf8_copy(&ictx->cell.cell.data, ud);
  1732. screen_write_cell(&ictx->ctx, &ictx->cell.cell);
  1733. return (0);
  1734. }