ldisc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * ldisc.c: PuTTY line discipline. Sits between the input coming
  3. * from keypresses in the window, and the output channel leading to
  4. * the back end. Implements echo and/or local line editing,
  5. * depending on what's currently configured.
  6. */
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <assert.h>
  10. #include "putty.h"
  11. #include "terminal.h"
  12. typedef enum InputType { NORMAL, DEDICATED, NONINTERACTIVE } InputType;
  13. struct input_chunk {
  14. struct input_chunk *next;
  15. InputType type;
  16. size_t size;
  17. };
  18. struct Ldisc_tag {
  19. Terminal *term;
  20. Backend *backend;
  21. Seat *seat;
  22. /*
  23. * When the backend is not reporting true from sendok(), we must
  24. * buffer the input received by ldisc_send(). It's stored in the
  25. * bufchain below, together with a linked list of input_chunk
  26. * blocks storing the extra metadata about special keys and
  27. * interactivity that ldisc_send() receives.
  28. *
  29. * All input is added to this buffer initially, but we then
  30. * process as much of it as possible immediately and hand it off
  31. * to the backend or a TermLineEditor. Anything left stays in this
  32. * buffer until ldisc_check_sendok() is next called, triggering a
  33. * run of the callback that tries again to process the queue.
  34. */
  35. bufchain input_queue;
  36. struct input_chunk *inchunk_head, *inchunk_tail;
  37. IdempotentCallback input_queue_callback;
  38. /*
  39. * Values cached out of conf.
  40. */
  41. bool telnet_keyboard, telnet_newline;
  42. int protocol, localecho, localedit;
  43. TermLineEditor *le;
  44. TermLineEditorCallbackReceiver le_rcv;
  45. /* We get one of these communicated to us by
  46. * term_get_userpass_input while it's reading a prompt, so that we
  47. * can push data straight into it */
  48. TermLineEditor *userpass_le;
  49. };
  50. #define ECHOING (ldisc->localecho == FORCE_ON || \
  51. (ldisc->localecho == AUTO && \
  52. (backend_ldisc_option_state(ldisc->backend, LD_ECHO))))
  53. #define EDITING (ldisc->localedit == FORCE_ON || \
  54. (ldisc->localedit == AUTO && \
  55. (backend_ldisc_option_state(ldisc->backend, LD_EDIT))))
  56. static void ldisc_input_queue_callback(void *ctx);
  57. static const TermLineEditorCallbackReceiverVtable ldisc_lineedit_receiver_vt;
  58. #define CTRL(x) (x^'@')
  59. Ldisc *ldisc_create(Conf *conf, Terminal *term, Backend *backend, Seat *seat)
  60. {
  61. Ldisc *ldisc = snew(Ldisc);
  62. memset(ldisc, 0, sizeof(Ldisc));
  63. ldisc->backend = backend;
  64. ldisc->term = term;
  65. ldisc->seat = seat;
  66. bufchain_init(&ldisc->input_queue);
  67. ldisc->input_queue_callback.fn = ldisc_input_queue_callback;
  68. ldisc->input_queue_callback.ctx = ldisc;
  69. bufchain_set_callback(&ldisc->input_queue, &ldisc->input_queue_callback);
  70. if (ldisc->term) {
  71. ldisc->le_rcv.vt = &ldisc_lineedit_receiver_vt;
  72. ldisc->le = lineedit_new(ldisc->term, 0, &ldisc->le_rcv);
  73. }
  74. ldisc_configure(ldisc, conf);
  75. /* Link ourselves into the backend and the terminal */
  76. if (term)
  77. term->ldisc = ldisc;
  78. if (backend)
  79. backend_provide_ldisc(backend, ldisc);
  80. return ldisc;
  81. }
  82. void ldisc_configure(Ldisc *ldisc, Conf *conf)
  83. {
  84. ldisc->telnet_keyboard = conf_get_bool(conf, CONF_telnet_keyboard);
  85. ldisc->telnet_newline = conf_get_bool(conf, CONF_telnet_newline);
  86. ldisc->protocol = conf_get_int(conf, CONF_protocol);
  87. ldisc->localecho = conf_get_int(conf, CONF_localecho);
  88. ldisc->localedit = conf_get_int(conf, CONF_localedit);
  89. unsigned flags = 0;
  90. if (ldisc->protocol == PROT_RAW)
  91. flags |= LE_CRLF_NEWLINE;
  92. if (ldisc->telnet_keyboard)
  93. flags |= LE_INTERRUPT | LE_SUSPEND | LE_ABORT;
  94. lineedit_modify_flags(ldisc->le, ~0U, flags);
  95. }
  96. void ldisc_free(Ldisc *ldisc)
  97. {
  98. bufchain_clear(&ldisc->input_queue);
  99. while (ldisc->inchunk_head) {
  100. struct input_chunk *oldhead = ldisc->inchunk_head;
  101. ldisc->inchunk_head = ldisc->inchunk_head->next;
  102. sfree(oldhead);
  103. }
  104. lineedit_free(ldisc->le);
  105. if (ldisc->term)
  106. ldisc->term->ldisc = NULL;
  107. if (ldisc->backend)
  108. backend_provide_ldisc(ldisc->backend, NULL);
  109. delete_callbacks_for_context(ldisc);
  110. sfree(ldisc);
  111. }
  112. void ldisc_echoedit_update(Ldisc *ldisc)
  113. {
  114. seat_echoedit_update(ldisc->seat, ECHOING, EDITING);
  115. /*
  116. * If we've just turned off local line editing mode, and our
  117. * TermLineEditor had a partial buffer, then send the contents of
  118. * the buffer. Rationale: (a) otherwise you lose data; (b) the
  119. * user quite likely typed the buffer contents _anticipating_ that
  120. * local editing would be turned off shortly, and the event was
  121. * slow arriving.
  122. */
  123. if (!EDITING)
  124. lineedit_send_line(ldisc->le);
  125. }
  126. void ldisc_provide_userpass_le(Ldisc *ldisc, TermLineEditor *le)
  127. {
  128. /*
  129. * Called by term_get_userpass_input to tell us when it has its
  130. * own TermLineEditor processing a password prompt, so that we can
  131. * inject our input into that instead of putting it into our own
  132. * TermLineEditor or sending it straight to the backend.
  133. */
  134. ldisc->userpass_le = le;
  135. }
  136. static inline bool is_dedicated_byte(char c, InputType type)
  137. {
  138. switch (type) {
  139. case DEDICATED:
  140. return true;
  141. case NORMAL:
  142. return false;
  143. case NONINTERACTIVE:
  144. /*
  145. * Non-interactive input (e.g. from a paste) doesn't come with
  146. * the ability to distinguish dedicated keypresses like Return
  147. * from generic ones like Ctrl+M. So we just have to make up
  148. * an answer to this question. In particular, we _must_ treat
  149. * Ctrl+M as the Return key, because that's the only way a
  150. * newline can be pasted at all.
  151. */
  152. return c == '\r';
  153. default:
  154. unreachable("those values should be exhaustive");
  155. }
  156. }
  157. static void ldisc_input_queue_consume(Ldisc *ldisc, size_t size)
  158. {
  159. bufchain_consume(&ldisc->input_queue, size);
  160. while (size > 0) {
  161. size_t thissize = (size < ldisc->inchunk_head->size ?
  162. size : ldisc->inchunk_head->size);
  163. ldisc->inchunk_head->size -= thissize;
  164. size -= thissize;
  165. if (!ldisc->inchunk_head->size) {
  166. struct input_chunk *oldhead = ldisc->inchunk_head;
  167. ldisc->inchunk_head = ldisc->inchunk_head->next;
  168. if (!ldisc->inchunk_head)
  169. ldisc->inchunk_tail = NULL;
  170. sfree(oldhead);
  171. }
  172. }
  173. }
  174. static void ldisc_lineedit_to_terminal(
  175. TermLineEditorCallbackReceiver *rcv, ptrlen data)
  176. {
  177. Ldisc *ldisc = container_of(rcv, Ldisc, le_rcv);
  178. if (ECHOING)
  179. seat_stdout(ldisc->seat, data.ptr, data.len);
  180. }
  181. static void ldisc_lineedit_to_backend(
  182. TermLineEditorCallbackReceiver *rcv, ptrlen data)
  183. {
  184. Ldisc *ldisc = container_of(rcv, Ldisc, le_rcv);
  185. backend_send(ldisc->backend, data.ptr, data.len);
  186. }
  187. static void ldisc_lineedit_special(
  188. TermLineEditorCallbackReceiver *rcv, SessionSpecialCode code, int arg)
  189. {
  190. Ldisc *ldisc = container_of(rcv, Ldisc, le_rcv);
  191. backend_special(ldisc->backend, code, arg);
  192. }
  193. static void ldisc_lineedit_newline(TermLineEditorCallbackReceiver *rcv)
  194. {
  195. Ldisc *ldisc = container_of(rcv, Ldisc, le_rcv);
  196. if (ldisc->protocol == PROT_RAW)
  197. backend_send(ldisc->backend, "\r\n", 2);
  198. else if (ldisc->protocol == PROT_TELNET && ldisc->telnet_newline)
  199. backend_special(ldisc->backend, SS_EOL, 0);
  200. else
  201. backend_send(ldisc->backend, "\r", 1);
  202. }
  203. static const TermLineEditorCallbackReceiverVtable
  204. ldisc_lineedit_receiver_vt = {
  205. .to_terminal = ldisc_lineedit_to_terminal,
  206. .to_backend = ldisc_lineedit_to_backend,
  207. .special = ldisc_lineedit_special,
  208. .newline = ldisc_lineedit_newline,
  209. };
  210. void ldisc_check_sendok(Ldisc *ldisc)
  211. {
  212. queue_idempotent_callback(&ldisc->input_queue_callback);
  213. }
  214. void ldisc_send(Ldisc *ldisc, const void *vbuf, int len, bool interactive)
  215. {
  216. assert(ldisc->term);
  217. if (interactive) {
  218. /*
  219. * Interrupt a paste from the clipboard, if one was in
  220. * progress when the user pressed a key. This is easier than
  221. * buffering the current piece of data and saving it until the
  222. * terminal has finished pasting, and has the potential side
  223. * benefit of permitting a user to cancel an accidental huge
  224. * paste.
  225. */
  226. term_nopaste(ldisc->term);
  227. }
  228. InputType type;
  229. if (len < 0) {
  230. /*
  231. * Less than zero means null terminated special string.
  232. */
  233. len = strlen(vbuf);
  234. type = DEDICATED;
  235. } else {
  236. type = interactive ? NORMAL : NONINTERACTIVE;
  237. }
  238. /*
  239. * Append our data to input_queue, and ensure it's marked with the
  240. * right type.
  241. */
  242. bufchain_add(&ldisc->input_queue, vbuf, len);
  243. if (!(ldisc->inchunk_tail && ldisc->inchunk_tail->type == type)) {
  244. struct input_chunk *new_chunk = snew(struct input_chunk);
  245. new_chunk->type = type;
  246. new_chunk->size = 0;
  247. new_chunk->next = NULL;
  248. if (ldisc->inchunk_tail)
  249. ldisc->inchunk_tail->next = new_chunk;
  250. else
  251. ldisc->inchunk_head = new_chunk;
  252. ldisc->inchunk_tail = new_chunk;
  253. }
  254. ldisc->inchunk_tail->size += len;
  255. /*
  256. * And process as much of the data immediately as we can.
  257. */
  258. ldisc_input_queue_callback(ldisc);
  259. }
  260. static void ldisc_input_queue_callback(void *ctx)
  261. {
  262. Ldisc *ldisc = (Ldisc *)ctx;
  263. /*
  264. * Toplevel callback that is triggered whenever the input queue
  265. * lengthens.
  266. */
  267. while (bufchain_size(&ldisc->input_queue)) {
  268. ptrlen pl = bufchain_prefix(&ldisc->input_queue);
  269. const char *start = pl.ptr, *buf = pl.ptr;
  270. size_t len = (pl.len < ldisc->inchunk_head->size ?
  271. pl.len : ldisc->inchunk_head->size);
  272. InputType type = ldisc->inchunk_head->type;
  273. while (len > 0 && ldisc->userpass_le) {
  274. char c = *buf++;
  275. len--;
  276. bool dedicated = is_dedicated_byte(c, type);
  277. lineedit_input(ldisc->userpass_le, c, dedicated);
  278. }
  279. if (!backend_sendok(ldisc->backend)) {
  280. ldisc_input_queue_consume(ldisc, buf - start);
  281. break;
  282. }
  283. /*
  284. * Either perform local editing, or just send characters.
  285. */
  286. if (EDITING) {
  287. while (len > 0) {
  288. char c = *buf++;
  289. len--;
  290. bool dedicated = is_dedicated_byte(c, type);
  291. lineedit_input(ldisc->le, c, dedicated);
  292. }
  293. ldisc_input_queue_consume(ldisc, buf - start);
  294. } else {
  295. if (ECHOING)
  296. seat_stdout(ldisc->seat, buf, len);
  297. if (type == DEDICATED && ldisc->protocol == PROT_TELNET) {
  298. while (len > 0) {
  299. char c = *buf++;
  300. len--;
  301. switch (c) {
  302. case CTRL('M'):
  303. if (ldisc->telnet_newline)
  304. backend_special(ldisc->backend, SS_EOL, 0);
  305. else
  306. backend_send(ldisc->backend, "\r", 1);
  307. break;
  308. case CTRL('?'):
  309. case CTRL('H'):
  310. if (ldisc->telnet_keyboard) {
  311. backend_special(ldisc->backend, SS_EC, 0);
  312. break;
  313. }
  314. case CTRL('C'):
  315. if (ldisc->telnet_keyboard) {
  316. backend_special(ldisc->backend, SS_IP, 0);
  317. break;
  318. }
  319. case CTRL('Z'):
  320. if (ldisc->telnet_keyboard) {
  321. backend_special(ldisc->backend, SS_SUSP, 0);
  322. break;
  323. }
  324. default:
  325. backend_send(ldisc->backend, &c, 1);
  326. break;
  327. }
  328. }
  329. ldisc_input_queue_consume(ldisc, buf - start);
  330. } else {
  331. backend_send(ldisc->backend, buf, len);
  332. ldisc_input_queue_consume(ldisc, len);
  333. }
  334. }
  335. }
  336. }