selection.c 9.8 KB

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