selection.c 8.5 KB

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