selection.c 11 KB

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