selection.c 9.0 KB

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