selection.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This module exports the functions:
  4. *
  5. * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
  6. * 'void clear_selection(void)'
  7. * 'int paste_selection(struct tty_struct *)'
  8. * 'int sel_loadlut(char __user *)'
  9. *
  10. * Now that /dev/vcs exists, most of this can disappear again.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/tty.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/kbd_kern.h>
  20. #include <linux/vt_kern.h>
  21. #include <linux/consolemap.h>
  22. #include <linux/selection.h>
  23. #include <linux/tiocl.h>
  24. #include <linux/console.h>
  25. #include <linux/tty_flip.h>
  26. /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
  27. #define isspace(c) ((c) == ' ')
  28. extern void poke_blanked_console(void);
  29. /* FIXME: all this needs locking */
  30. /* Variables for selection control. */
  31. /* Use a dynamic buffer, instead of static (Dec 1994) */
  32. struct vc_data *sel_cons; /* must not be deallocated */
  33. static int use_unicode;
  34. static volatile int sel_start = -1; /* cleared by clear_selection */
  35. static int sel_end;
  36. static int sel_buffer_lth;
  37. static char *sel_buffer;
  38. /* clear_selection, highlight and highlight_pointer can be called
  39. from interrupt (via scrollback/front) */
  40. /* set reverse video on characters s-e of console with selection. */
  41. static inline void highlight(const int s, const int e)
  42. {
  43. invert_screen(sel_cons, s, e-s+2, 1);
  44. }
  45. /* use complementary color to show the pointer */
  46. static inline void highlight_pointer(const int where)
  47. {
  48. complement_pos(sel_cons, where);
  49. }
  50. static u32
  51. sel_pos(int n)
  52. {
  53. if (use_unicode)
  54. return screen_glyph_unicode(sel_cons, n / 2);
  55. return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
  56. 0);
  57. }
  58. /**
  59. * clear_selection - remove current selection
  60. *
  61. * Remove the current selection highlight, if any from the console
  62. * holding the selection. The caller must hold the console lock.
  63. */
  64. void clear_selection(void)
  65. {
  66. highlight_pointer(-1); /* hide the pointer */
  67. if (sel_start != -1) {
  68. highlight(sel_start, sel_end);
  69. sel_start = -1;
  70. }
  71. }
  72. /*
  73. * User settable table: what characters are to be considered alphabetic?
  74. * 128 bits. Locked by the console lock.
  75. */
  76. static u32 inwordLut[]={
  77. 0x00000000, /* control chars */
  78. 0x03FFE000, /* digits and "-./" */
  79. 0x87FFFFFE, /* uppercase and '_' */
  80. 0x07FFFFFE, /* lowercase */
  81. };
  82. static inline int inword(const u32 c)
  83. {
  84. return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
  85. }
  86. /**
  87. * set loadlut - load the LUT table
  88. * @p: user table
  89. *
  90. * Load the LUT table from user space. The caller must hold the console
  91. * lock. Make a temporary copy so a partial update doesn't make a mess.
  92. */
  93. int sel_loadlut(char __user *p)
  94. {
  95. u32 tmplut[ARRAY_SIZE(inwordLut)];
  96. if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
  97. return -EFAULT;
  98. memcpy(inwordLut, tmplut, sizeof(inwordLut));
  99. return 0;
  100. }
  101. /* does screen address p correspond to character at LH/RH edge of screen? */
  102. static inline int atedge(const int p, int size_row)
  103. {
  104. return (!(p % size_row) || !((p + 2) % size_row));
  105. }
  106. /* stores the char in UTF8 and returns the number of bytes used (1-4) */
  107. static int store_utf8(u32 c, char *p)
  108. {
  109. if (c < 0x80) {
  110. /* 0******* */
  111. p[0] = c;
  112. return 1;
  113. } else if (c < 0x800) {
  114. /* 110***** 10****** */
  115. p[0] = 0xc0 | (c >> 6);
  116. p[1] = 0x80 | (c & 0x3f);
  117. return 2;
  118. } else if (c < 0x10000) {
  119. /* 1110**** 10****** 10****** */
  120. p[0] = 0xe0 | (c >> 12);
  121. p[1] = 0x80 | ((c >> 6) & 0x3f);
  122. p[2] = 0x80 | (c & 0x3f);
  123. return 3;
  124. } else if (c < 0x110000) {
  125. /* 11110*** 10****** 10****** 10****** */
  126. p[0] = 0xf0 | (c >> 18);
  127. p[1] = 0x80 | ((c >> 12) & 0x3f);
  128. p[2] = 0x80 | ((c >> 6) & 0x3f);
  129. p[3] = 0x80 | (c & 0x3f);
  130. return 4;
  131. } else {
  132. /* outside Unicode, replace with U+FFFD */
  133. p[0] = 0xef;
  134. p[1] = 0xbf;
  135. p[2] = 0xbd;
  136. return 3;
  137. }
  138. }
  139. /**
  140. * set_selection - set the current selection.
  141. * @sel: user selection info
  142. * @tty: the console tty
  143. *
  144. * Invoked by the ioctl handle for the vt layer.
  145. *
  146. * The entire selection process is managed under the console_lock. It's
  147. * a lot under the lock but its hardly a performance path
  148. */
  149. int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
  150. {
  151. struct vc_data *vc = vc_cons[fg_console].d;
  152. int new_sel_start, new_sel_end, spc;
  153. struct tiocl_selection v;
  154. char *bp, *obp;
  155. int i, ps, pe, multiplier;
  156. u32 c;
  157. int mode;
  158. poke_blanked_console();
  159. if (copy_from_user(&v, sel, sizeof(*sel)))
  160. return -EFAULT;
  161. v.xs = min_t(u16, v.xs - 1, vc->vc_cols - 1);
  162. v.ys = min_t(u16, v.ys - 1, vc->vc_rows - 1);
  163. v.xe = min_t(u16, v.xe - 1, vc->vc_cols - 1);
  164. v.ye = min_t(u16, v.ye - 1, vc->vc_rows - 1);
  165. ps = v.ys * vc->vc_size_row + (v.xs << 1);
  166. pe = v.ye * vc->vc_size_row + (v.xe << 1);
  167. if (v.sel_mode == TIOCL_SELCLEAR) {
  168. /* useful for screendump without selection highlights */
  169. clear_selection();
  170. return 0;
  171. }
  172. if (mouse_reporting() && (v.sel_mode & TIOCL_SELMOUSEREPORT)) {
  173. mouse_report(tty, v.sel_mode & TIOCL_SELBUTTONMASK, v.xs, v.ys);
  174. return 0;
  175. }
  176. if (ps > pe) /* make sel_start <= sel_end */
  177. swap(ps, pe);
  178. if (sel_cons != vc_cons[fg_console].d) {
  179. clear_selection();
  180. sel_cons = vc_cons[fg_console].d;
  181. }
  182. mode = vt_do_kdgkbmode(fg_console);
  183. if (mode == K_UNICODE)
  184. use_unicode = 1;
  185. else
  186. use_unicode = 0;
  187. switch (v.sel_mode)
  188. {
  189. case TIOCL_SELCHAR: /* character-by-character selection */
  190. new_sel_start = ps;
  191. new_sel_end = pe;
  192. break;
  193. case TIOCL_SELWORD: /* word-by-word selection */
  194. spc = isspace(sel_pos(ps));
  195. for (new_sel_start = ps; ; ps -= 2)
  196. {
  197. if ((spc && !isspace(sel_pos(ps))) ||
  198. (!spc && !inword(sel_pos(ps))))
  199. break;
  200. new_sel_start = ps;
  201. if (!(ps % vc->vc_size_row))
  202. break;
  203. }
  204. spc = isspace(sel_pos(pe));
  205. for (new_sel_end = pe; ; pe += 2)
  206. {
  207. if ((spc && !isspace(sel_pos(pe))) ||
  208. (!spc && !inword(sel_pos(pe))))
  209. break;
  210. new_sel_end = pe;
  211. if (!((pe + 2) % vc->vc_size_row))
  212. break;
  213. }
  214. break;
  215. case TIOCL_SELLINE: /* line-by-line selection */
  216. new_sel_start = ps - ps % vc->vc_size_row;
  217. new_sel_end = pe + vc->vc_size_row
  218. - pe % vc->vc_size_row - 2;
  219. break;
  220. case TIOCL_SELPOINTER:
  221. highlight_pointer(pe);
  222. return 0;
  223. default:
  224. return -EINVAL;
  225. }
  226. /* remove the pointer */
  227. highlight_pointer(-1);
  228. /* select to end of line if on trailing space */
  229. if (new_sel_end > new_sel_start &&
  230. !atedge(new_sel_end, vc->vc_size_row) &&
  231. isspace(sel_pos(new_sel_end))) {
  232. for (pe = new_sel_end + 2; ; pe += 2)
  233. if (!isspace(sel_pos(pe)) ||
  234. atedge(pe, vc->vc_size_row))
  235. break;
  236. if (isspace(sel_pos(pe)))
  237. new_sel_end = pe;
  238. }
  239. if (sel_start == -1) /* no current selection */
  240. highlight(new_sel_start, new_sel_end);
  241. else if (new_sel_start == sel_start)
  242. {
  243. if (new_sel_end == sel_end) /* no action required */
  244. return 0;
  245. else if (new_sel_end > sel_end) /* extend to right */
  246. highlight(sel_end + 2, new_sel_end);
  247. else /* contract from right */
  248. highlight(new_sel_end + 2, sel_end);
  249. }
  250. else if (new_sel_end == sel_end)
  251. {
  252. if (new_sel_start < sel_start) /* extend to left */
  253. highlight(new_sel_start, sel_start - 2);
  254. else /* contract from left */
  255. highlight(sel_start, new_sel_start - 2);
  256. }
  257. else /* some other case; start selection from scratch */
  258. {
  259. clear_selection();
  260. highlight(new_sel_start, new_sel_end);
  261. }
  262. sel_start = new_sel_start;
  263. sel_end = new_sel_end;
  264. /* Allocate a new buffer before freeing the old one ... */
  265. multiplier = use_unicode ? 4 : 1; /* chars can take up to 4 bytes */
  266. bp = kmalloc_array((sel_end - sel_start) / 2 + 1, multiplier,
  267. GFP_KERNEL);
  268. if (!bp) {
  269. printk(KERN_WARNING "selection: kmalloc() failed\n");
  270. clear_selection();
  271. return -ENOMEM;
  272. }
  273. kfree(sel_buffer);
  274. sel_buffer = bp;
  275. obp = bp;
  276. for (i = sel_start; i <= sel_end; i += 2) {
  277. c = sel_pos(i);
  278. if (use_unicode)
  279. bp += store_utf8(c, bp);
  280. else
  281. *bp++ = c;
  282. if (!isspace(c))
  283. obp = bp;
  284. if (! ((i + 2) % vc->vc_size_row)) {
  285. /* strip trailing blanks from line and add newline,
  286. unless non-space at end of line. */
  287. if (obp != bp) {
  288. bp = obp;
  289. *bp++ = '\r';
  290. }
  291. obp = bp;
  292. }
  293. }
  294. sel_buffer_lth = bp - sel_buffer;
  295. return 0;
  296. }
  297. /* Insert the contents of the selection buffer into the
  298. * queue of the tty associated with the current console.
  299. * Invoked by ioctl().
  300. *
  301. * Locking: called without locks. Calls the ldisc wrongly with
  302. * unsafe methods,
  303. */
  304. int paste_selection(struct tty_struct *tty)
  305. {
  306. struct vc_data *vc = tty->driver_data;
  307. int pasted = 0;
  308. unsigned int count;
  309. struct tty_ldisc *ld;
  310. DECLARE_WAITQUEUE(wait, current);
  311. console_lock();
  312. poke_blanked_console();
  313. console_unlock();
  314. ld = tty_ldisc_ref_wait(tty);
  315. if (!ld)
  316. return -EIO; /* ldisc was hung up */
  317. tty_buffer_lock_exclusive(&vc->port);
  318. add_wait_queue(&vc->paste_wait, &wait);
  319. while (sel_buffer && sel_buffer_lth > pasted) {
  320. set_current_state(TASK_INTERRUPTIBLE);
  321. if (tty_throttled(tty)) {
  322. schedule();
  323. continue;
  324. }
  325. __set_current_state(TASK_RUNNING);
  326. count = sel_buffer_lth - pasted;
  327. count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
  328. count);
  329. pasted += count;
  330. }
  331. remove_wait_queue(&vc->paste_wait, &wait);
  332. __set_current_state(TASK_RUNNING);
  333. tty_buffer_unlock_exclusive(&vc->port);
  334. tty_ldisc_deref(ld);
  335. return 0;
  336. }