selection.c 10 KB

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