selection.c 10 KB

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