selection.c 9.3 KB

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