selection.c 9.3 KB

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