blob: 0e5f4f706f01931ee768bbdd93172bc5d9efaa70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
12#include <linux/module.h>
13#include <linux/tty.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
Jiri Slaby290a9382020-02-10 09:11:31 +010016#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/types.h>
19
20#include <asm/uaccess.h>
21
Jan Engelhardt759448f2007-07-15 23:40:40 -070022#include <linux/kbd_kern.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/vt_kern.h>
24#include <linux/consolemap.h>
25#include <linux/selection.h>
26#include <linux/tiocl.h>
27#include <linux/console.h>
Peter Hurleya7c8d582013-06-15 09:36:15 -040028#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
31#define isspace(c) ((c) == ' ')
32
33extern void poke_blanked_console(void);
34
Alan Cox079c9532012-02-28 14:49:23 +000035/* FIXME: all this needs locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Variables for selection control. */
37/* Use a dynamic buffer, instead of static (Dec 1994) */
Alan Coxca9bda02006-09-29 02:00:03 -070038struct vc_data *sel_cons; /* must not be deallocated */
Jan Engelhardt759448f2007-07-15 23:40:40 -070039static int use_unicode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static volatile int sel_start = -1; /* cleared by clear_selection */
41static int sel_end;
42static int sel_buffer_lth;
43static char *sel_buffer;
Jiri Slaby290a9382020-02-10 09:11:31 +010044static DEFINE_MUTEX(sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* clear_selection, highlight and highlight_pointer can be called
47 from interrupt (via scrollback/front) */
48
49/* set reverse video on characters s-e of console with selection. */
50static inline void highlight(const int s, const int e)
51{
52 invert_screen(sel_cons, s, e-s+2, 1);
53}
54
55/* use complementary color to show the pointer */
56static inline void highlight_pointer(const int where)
57{
58 complement_pos(sel_cons, where);
59}
60
Jan Engelhardt759448f2007-07-15 23:40:40 -070061static u16
Linus Torvalds1da177e2005-04-16 15:20:36 -070062sel_pos(int n)
63{
Jan Engelhardt759448f2007-07-15 23:40:40 -070064 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
65 use_unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Alan Cox52894752012-03-02 15:00:02 +000068/**
69 * clear_selection - remove current selection
70 *
71 * Remove the current selection highlight, if any from the console
72 * holding the selection. The caller must hold the console lock.
73 */
74void clear_selection(void)
75{
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 highlight_pointer(-1); /* hide the pointer */
77 if (sel_start != -1) {
78 highlight(sel_start, sel_end);
79 sel_start = -1;
80 }
81}
82
83/*
84 * User settable table: what characters are to be considered alphabetic?
Alan Cox52894752012-03-02 15:00:02 +000085 * 256 bits. Locked by the console lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87static u32 inwordLut[8]={
88 0x00000000, /* control chars */
89 0x03FF0000, /* digits */
90 0x87FFFFFE, /* uppercase and '_' */
91 0x07FFFFFE, /* lowercase */
92 0x00000000,
93 0x00000000,
94 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
95 0xFF7FFFFF /* latin-1 accented letters, not division sign */
96};
97
Jan Engelhardt759448f2007-07-15 23:40:40 -070098static inline int inword(const u16 c) {
99 return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Alan Cox52894752012-03-02 15:00:02 +0000102/**
103 * set loadlut - load the LUT table
104 * @p: user table
105 *
106 * Load the LUT table from user space. The caller must hold the console
107 * lock. Make a temporary copy so a partial update doesn't make a mess.
108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109int sel_loadlut(char __user *p)
110{
Alan Cox52894752012-03-02 15:00:02 +0000111 u32 tmplut[8];
112 if (copy_from_user(tmplut, (u32 __user *)(p+4), 32))
113 return -EFAULT;
114 memcpy(inwordLut, tmplut, 32);
115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/* does screen address p correspond to character at LH/RH edge of screen? */
119static inline int atedge(const int p, int size_row)
120{
121 return (!(p % size_row) || !((p + 2) % size_row));
122}
123
124/* constrain v such that v <= u */
125static inline unsigned short limit(const unsigned short v, const unsigned short u)
126{
127 return (v > u) ? u : v;
128}
129
Jan Engelhardt759448f2007-07-15 23:40:40 -0700130/* stores the char in UTF8 and returns the number of bytes used (1-3) */
131static int store_utf8(u16 c, char *p)
132{
133 if (c < 0x80) {
134 /* 0******* */
135 p[0] = c;
136 return 1;
137 } else if (c < 0x800) {
138 /* 110***** 10****** */
139 p[0] = 0xc0 | (c >> 6);
140 p[1] = 0x80 | (c & 0x3f);
141 return 2;
142 } else {
143 /* 1110**** 10****** 10****** */
144 p[0] = 0xe0 | (c >> 12);
145 p[1] = 0x80 | ((c >> 6) & 0x3f);
146 p[2] = 0x80 | (c & 0x3f);
147 return 3;
148 }
149}
150
Alan Cox52894752012-03-02 15:00:02 +0000151/**
152 * set_selection - set the current selection.
153 * @sel: user selection info
154 * @tty: the console tty
155 *
156 * Invoked by the ioctl handle for the vt layer.
157 *
158 * The entire selection process is managed under the console_lock. It's
159 * a lot under the lock but its hardly a performance path
160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
162{
163 struct vc_data *vc = vc_cons[fg_console].d;
164 int sel_mode, new_sel_start, new_sel_end, spc;
165 char *bp, *obp;
Jan Engelhardt759448f2007-07-15 23:40:40 -0700166 int i, ps, pe, multiplier;
167 u16 c;
Jiri Slaby290a9382020-02-10 09:11:31 +0100168 int mode, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 poke_blanked_console();
171
172 { unsigned short xs, ys, xe, ye;
173
174 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
175 return -EFAULT;
176 __get_user(xs, &sel->xs);
177 __get_user(ys, &sel->ys);
178 __get_user(xe, &sel->xe);
179 __get_user(ye, &sel->ye);
180 __get_user(sel_mode, &sel->sel_mode);
181 xs--; ys--; xe--; ye--;
182 xs = limit(xs, vc->vc_cols - 1);
183 ys = limit(ys, vc->vc_rows - 1);
184 xe = limit(xe, vc->vc_cols - 1);
185 ye = limit(ye, vc->vc_rows - 1);
186 ps = ys * vc->vc_size_row + (xs << 1);
187 pe = ye * vc->vc_size_row + (xe << 1);
188
189 if (sel_mode == TIOCL_SELCLEAR) {
190 /* useful for screendump without selection highlights */
191 clear_selection();
192 return 0;
193 }
194
195 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
196 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
197 return 0;
198 }
199 }
200
201 if (ps > pe) /* make sel_start <= sel_end */
202 {
203 int tmp = ps;
204 ps = pe;
205 pe = tmp;
206 }
207
Jiri Slaby290a9382020-02-10 09:11:31 +0100208 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (sel_cons != vc_cons[fg_console].d) {
210 clear_selection();
211 sel_cons = vc_cons[fg_console].d;
212 }
Alan Cox079c9532012-02-28 14:49:23 +0000213 mode = vt_do_kdgkbmode(fg_console);
214 if (mode == K_UNICODE)
215 use_unicode = 1;
216 else
217 use_unicode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 switch (sel_mode)
220 {
221 case TIOCL_SELCHAR: /* character-by-character selection */
222 new_sel_start = ps;
223 new_sel_end = pe;
224 break;
225 case TIOCL_SELWORD: /* word-by-word selection */
226 spc = isspace(sel_pos(ps));
227 for (new_sel_start = ps; ; ps -= 2)
228 {
229 if ((spc && !isspace(sel_pos(ps))) ||
230 (!spc && !inword(sel_pos(ps))))
231 break;
232 new_sel_start = ps;
233 if (!(ps % vc->vc_size_row))
234 break;
235 }
236 spc = isspace(sel_pos(pe));
237 for (new_sel_end = pe; ; pe += 2)
238 {
239 if ((spc && !isspace(sel_pos(pe))) ||
240 (!spc && !inword(sel_pos(pe))))
241 break;
242 new_sel_end = pe;
243 if (!((pe + 2) % vc->vc_size_row))
244 break;
245 }
246 break;
247 case TIOCL_SELLINE: /* line-by-line selection */
248 new_sel_start = ps - ps % vc->vc_size_row;
249 new_sel_end = pe + vc->vc_size_row
250 - pe % vc->vc_size_row - 2;
251 break;
252 case TIOCL_SELPOINTER:
253 highlight_pointer(pe);
Jiri Slaby290a9382020-02-10 09:11:31 +0100254 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 default:
Jiri Slaby290a9382020-02-10 09:11:31 +0100256 ret = -EINVAL;
257 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
260 /* remove the pointer */
261 highlight_pointer(-1);
262
263 /* select to end of line if on trailing space */
264 if (new_sel_end > new_sel_start &&
265 !atedge(new_sel_end, vc->vc_size_row) &&
266 isspace(sel_pos(new_sel_end))) {
267 for (pe = new_sel_end + 2; ; pe += 2)
268 if (!isspace(sel_pos(pe)) ||
269 atedge(pe, vc->vc_size_row))
270 break;
271 if (isspace(sel_pos(pe)))
272 new_sel_end = pe;
273 }
274 if (sel_start == -1) /* no current selection */
275 highlight(new_sel_start, new_sel_end);
276 else if (new_sel_start == sel_start)
277 {
278 if (new_sel_end == sel_end) /* no action required */
Jiri Slaby290a9382020-02-10 09:11:31 +0100279 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 else if (new_sel_end > sel_end) /* extend to right */
281 highlight(sel_end + 2, new_sel_end);
282 else /* contract from right */
283 highlight(new_sel_end + 2, sel_end);
284 }
285 else if (new_sel_end == sel_end)
286 {
287 if (new_sel_start < sel_start) /* extend to left */
288 highlight(new_sel_start, sel_start - 2);
289 else /* contract from left */
290 highlight(sel_start, new_sel_start - 2);
291 }
292 else /* some other case; start selection from scratch */
293 {
294 clear_selection();
295 highlight(new_sel_start, new_sel_end);
296 }
297 sel_start = new_sel_start;
298 sel_end = new_sel_end;
299
300 /* Allocate a new buffer before freeing the old one ... */
Jan Engelhardt759448f2007-07-15 23:40:40 -0700301 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
Mikulas Patocka878b8612009-01-30 15:27:14 -0500302 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!bp) {
304 printk(KERN_WARNING "selection: kmalloc() failed\n");
305 clear_selection();
Jiri Slaby290a9382020-02-10 09:11:31 +0100306 ret = -ENOMEM;
307 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Jesper Juhl735d5662005-11-07 01:01:29 -0800309 kfree(sel_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 sel_buffer = bp;
311
312 obp = bp;
313 for (i = sel_start; i <= sel_end; i += 2) {
Jan Engelhardt759448f2007-07-15 23:40:40 -0700314 c = sel_pos(i);
315 if (use_unicode)
316 bp += store_utf8(c, bp);
317 else
318 *bp++ = c;
319 if (!isspace(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 obp = bp;
321 if (! ((i + 2) % vc->vc_size_row)) {
322 /* strip trailing blanks from line and add newline,
323 unless non-space at end of line. */
324 if (obp != bp) {
325 bp = obp;
326 *bp++ = '\r';
327 }
328 obp = bp;
329 }
330 }
331 sel_buffer_lth = bp - sel_buffer;
Jiri Slaby290a9382020-02-10 09:11:31 +0100332unlock:
333 mutex_unlock(&sel_lock);
334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/* Insert the contents of the selection buffer into the
338 * queue of the tty associated with the current console.
339 * Invoked by ioctl().
Jiri Slaby906cbe12011-07-14 14:35:14 +0200340 *
Alan Cox20f62572012-03-02 14:59:37 +0000341 * Locking: called without locks. Calls the ldisc wrongly with
342 * unsafe methods,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344int paste_selection(struct tty_struct *tty)
345{
Alan Coxc9f19e92009-01-02 13:47:26 +0000346 struct vc_data *vc = tty->driver_data;
Alan Cox33f0f882006-01-09 20:54:13 -0800347 int pasted = 0;
348 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct tty_ldisc *ld;
350 DECLARE_WAITQUEUE(wait, current);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100351 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Torben Hohnac751ef2011-01-25 15:07:35 -0800353 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 poke_blanked_console();
Torben Hohnac751ef2011-01-25 15:07:35 -0800355 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jiri Slaby7ee00fd2012-10-18 22:26:32 +0200357 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -0800358 if (!ld)
359 return -EIO; /* ldisc was hung up */
Peter Hurleya7c8d582013-06-15 09:36:15 -0400360 tty_buffer_lock_exclusive(&vc->port);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 add_wait_queue(&vc->paste_wait, &wait);
Jiri Slaby290a9382020-02-10 09:11:31 +0100363 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 while (sel_buffer && sel_buffer_lth > pasted) {
365 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100366 if (signal_pending(current)) {
367 ret = -EINTR;
368 break;
369 }
Peter Hurley97ef38b2016-04-09 17:11:36 -0700370 if (tty_throttled(tty)) {
Jiri Slaby290a9382020-02-10 09:11:31 +0100371 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 schedule();
Jiri Slaby290a9382020-02-10 09:11:31 +0100373 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 continue;
375 }
Peter Hurley61e86cc2015-07-12 20:47:35 -0400376 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 count = sel_buffer_lth - pasted;
Peter Hurley24a89d12013-06-15 09:14:15 -0400378 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
379 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 pasted += count;
381 }
Jiri Slaby290a9382020-02-10 09:11:31 +0100382 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 remove_wait_queue(&vc->paste_wait, &wait);
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700384 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Peter Hurleya7c8d582013-06-15 09:36:15 -0400386 tty_buffer_unlock_exclusive(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 tty_ldisc_deref(ld);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100388 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}