selection.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. /* remove the current selection highlight, if any,
  55. from the console holding the selection. */
  56. void
  57. clear_selection(void) {
  58. highlight_pointer(-1); /* hide the pointer */
  59. if (sel_start != -1) {
  60. highlight(sel_start, sel_end);
  61. sel_start = -1;
  62. }
  63. }
  64. /*
  65. * User settable table: what characters are to be considered alphabetic?
  66. * 256 bits
  67. */
  68. static u32 inwordLut[8]={
  69. 0x00000000, /* control chars */
  70. 0x03FF0000, /* digits */
  71. 0x87FFFFFE, /* uppercase and '_' */
  72. 0x07FFFFFE, /* lowercase */
  73. 0x00000000,
  74. 0x00000000,
  75. 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
  76. 0xFF7FFFFF /* latin-1 accented letters, not division sign */
  77. };
  78. static inline int inword(const u16 c) {
  79. return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
  80. }
  81. /* set inwordLut contents. Invoked by ioctl(). */
  82. int sel_loadlut(char __user *p)
  83. {
  84. return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
  85. }
  86. /* does screen address p correspond to character at LH/RH edge of screen? */
  87. static inline int atedge(const int p, int size_row)
  88. {
  89. return (!(p % size_row) || !((p + 2) % size_row));
  90. }
  91. /* constrain v such that v <= u */
  92. static inline unsigned short limit(const unsigned short v, const unsigned short u)
  93. {
  94. return (v > u) ? u : v;
  95. }
  96. /* stores the char in UTF8 and returns the number of bytes used (1-3) */
  97. static int store_utf8(u16 c, char *p)
  98. {
  99. if (c < 0x80) {
  100. /* 0******* */
  101. p[0] = c;
  102. return 1;
  103. } else if (c < 0x800) {
  104. /* 110***** 10****** */
  105. p[0] = 0xc0 | (c >> 6);
  106. p[1] = 0x80 | (c & 0x3f);
  107. return 2;
  108. } else {
  109. /* 1110**** 10****** 10****** */
  110. p[0] = 0xe0 | (c >> 12);
  111. p[1] = 0x80 | ((c >> 6) & 0x3f);
  112. p[2] = 0x80 | (c & 0x3f);
  113. return 3;
  114. }
  115. }
  116. /* set the current selection. Invoked by ioctl() or by kernel code. */
  117. int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
  118. {
  119. struct vc_data *vc = vc_cons[fg_console].d;
  120. int sel_mode, new_sel_start, new_sel_end, spc;
  121. char *bp, *obp;
  122. int i, ps, pe, multiplier;
  123. u16 c;
  124. int mode;
  125. poke_blanked_console();
  126. { unsigned short xs, ys, xe, ye;
  127. if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
  128. return -EFAULT;
  129. __get_user(xs, &sel->xs);
  130. __get_user(ys, &sel->ys);
  131. __get_user(xe, &sel->xe);
  132. __get_user(ye, &sel->ye);
  133. __get_user(sel_mode, &sel->sel_mode);
  134. xs--; ys--; xe--; ye--;
  135. xs = limit(xs, vc->vc_cols - 1);
  136. ys = limit(ys, vc->vc_rows - 1);
  137. xe = limit(xe, vc->vc_cols - 1);
  138. ye = limit(ye, vc->vc_rows - 1);
  139. ps = ys * vc->vc_size_row + (xs << 1);
  140. pe = ye * vc->vc_size_row + (xe << 1);
  141. if (sel_mode == TIOCL_SELCLEAR) {
  142. /* useful for screendump without selection highlights */
  143. clear_selection();
  144. return 0;
  145. }
  146. if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
  147. mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
  148. return 0;
  149. }
  150. }
  151. if (ps > pe) /* make sel_start <= sel_end */
  152. {
  153. int tmp = ps;
  154. ps = pe;
  155. pe = tmp;
  156. }
  157. if (sel_cons != vc_cons[fg_console].d) {
  158. clear_selection();
  159. sel_cons = vc_cons[fg_console].d;
  160. }
  161. mode = vt_do_kdgkbmode(fg_console);
  162. if (mode == K_UNICODE)
  163. use_unicode = 1;
  164. else
  165. use_unicode = 0;
  166. switch (sel_mode)
  167. {
  168. case TIOCL_SELCHAR: /* character-by-character selection */
  169. new_sel_start = ps;
  170. new_sel_end = pe;
  171. break;
  172. case TIOCL_SELWORD: /* word-by-word selection */
  173. spc = isspace(sel_pos(ps));
  174. for (new_sel_start = ps; ; ps -= 2)
  175. {
  176. if ((spc && !isspace(sel_pos(ps))) ||
  177. (!spc && !inword(sel_pos(ps))))
  178. break;
  179. new_sel_start = ps;
  180. if (!(ps % vc->vc_size_row))
  181. break;
  182. }
  183. spc = isspace(sel_pos(pe));
  184. for (new_sel_end = pe; ; pe += 2)
  185. {
  186. if ((spc && !isspace(sel_pos(pe))) ||
  187. (!spc && !inword(sel_pos(pe))))
  188. break;
  189. new_sel_end = pe;
  190. if (!((pe + 2) % vc->vc_size_row))
  191. break;
  192. }
  193. break;
  194. case TIOCL_SELLINE: /* line-by-line selection */
  195. new_sel_start = ps - ps % vc->vc_size_row;
  196. new_sel_end = pe + vc->vc_size_row
  197. - pe % vc->vc_size_row - 2;
  198. break;
  199. case TIOCL_SELPOINTER:
  200. highlight_pointer(pe);
  201. return 0;
  202. default:
  203. return -EINVAL;
  204. }
  205. /* remove the pointer */
  206. highlight_pointer(-1);
  207. /* select to end of line if on trailing space */
  208. if (new_sel_end > new_sel_start &&
  209. !atedge(new_sel_end, vc->vc_size_row) &&
  210. isspace(sel_pos(new_sel_end))) {
  211. for (pe = new_sel_end + 2; ; pe += 2)
  212. if (!isspace(sel_pos(pe)) ||
  213. atedge(pe, vc->vc_size_row))
  214. break;
  215. if (isspace(sel_pos(pe)))
  216. new_sel_end = pe;
  217. }
  218. if (sel_start == -1) /* no current selection */
  219. highlight(new_sel_start, new_sel_end);
  220. else if (new_sel_start == sel_start)
  221. {
  222. if (new_sel_end == sel_end) /* no action required */
  223. return 0;
  224. else if (new_sel_end > sel_end) /* extend to right */
  225. highlight(sel_end + 2, new_sel_end);
  226. else /* contract from right */
  227. highlight(new_sel_end + 2, sel_end);
  228. }
  229. else if (new_sel_end == sel_end)
  230. {
  231. if (new_sel_start < sel_start) /* extend to left */
  232. highlight(new_sel_start, sel_start - 2);
  233. else /* contract from left */
  234. highlight(sel_start, new_sel_start - 2);
  235. }
  236. else /* some other case; start selection from scratch */
  237. {
  238. clear_selection();
  239. highlight(new_sel_start, new_sel_end);
  240. }
  241. sel_start = new_sel_start;
  242. sel_end = new_sel_end;
  243. /* Allocate a new buffer before freeing the old one ... */
  244. multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
  245. bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
  246. if (!bp) {
  247. printk(KERN_WARNING "selection: kmalloc() failed\n");
  248. clear_selection();
  249. return -ENOMEM;
  250. }
  251. kfree(sel_buffer);
  252. sel_buffer = bp;
  253. obp = bp;
  254. for (i = sel_start; i <= sel_end; i += 2) {
  255. c = sel_pos(i);
  256. if (use_unicode)
  257. bp += store_utf8(c, bp);
  258. else
  259. *bp++ = c;
  260. if (!isspace(c))
  261. obp = bp;
  262. if (! ((i + 2) % vc->vc_size_row)) {
  263. /* strip trailing blanks from line and add newline,
  264. unless non-space at end of line. */
  265. if (obp != bp) {
  266. bp = obp;
  267. *bp++ = '\r';
  268. }
  269. obp = bp;
  270. }
  271. }
  272. sel_buffer_lth = bp - sel_buffer;
  273. return 0;
  274. }
  275. /* Insert the contents of the selection buffer into the
  276. * queue of the tty associated with the current console.
  277. * Invoked by ioctl().
  278. *
  279. * Locking: always called with BTM from vt_ioctl
  280. */
  281. int paste_selection(struct tty_struct *tty)
  282. {
  283. struct vc_data *vc = tty->driver_data;
  284. int pasted = 0;
  285. unsigned int count;
  286. struct tty_ldisc *ld;
  287. DECLARE_WAITQUEUE(wait, current);
  288. console_lock();
  289. poke_blanked_console();
  290. console_unlock();
  291. ld = tty_ldisc_ref(tty);
  292. if (!ld) {
  293. tty_unlock();
  294. ld = tty_ldisc_ref_wait(tty);
  295. tty_lock();
  296. }
  297. add_wait_queue(&vc->paste_wait, &wait);
  298. while (sel_buffer && sel_buffer_lth > pasted) {
  299. set_current_state(TASK_INTERRUPTIBLE);
  300. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  301. schedule();
  302. continue;
  303. }
  304. count = sel_buffer_lth - pasted;
  305. count = min(count, tty->receive_room);
  306. tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted,
  307. NULL, count);
  308. pasted += count;
  309. }
  310. remove_wait_queue(&vc->paste_wait, &wait);
  311. __set_current_state(TASK_RUNNING);
  312. tty_ldisc_deref(ld);
  313. return 0;
  314. }