selection.c 9.2 KB

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