selection.c 8.7 KB

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