selection.c 8.6 KB

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