selection.c 7.8 KB

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