blob: 225689c766237d8536904902c2a6d9c2773b486d [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
Jiri Slabyfc3d6dd2020-02-19 08:39:43 +010083bool vc_is_sel(struct vc_data *vc)
84{
85 return vc == sel_cons;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * User settable table: what characters are to be considered alphabetic?
Alan Cox52894752012-03-02 15:00:02 +000090 * 256 bits. Locked by the console lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92static u32 inwordLut[8]={
93 0x00000000, /* control chars */
94 0x03FF0000, /* digits */
95 0x87FFFFFE, /* uppercase and '_' */
96 0x07FFFFFE, /* lowercase */
97 0x00000000,
98 0x00000000,
99 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
100 0xFF7FFFFF /* latin-1 accented letters, not division sign */
101};
102
Jan Engelhardt759448f2007-07-15 23:40:40 -0700103static inline int inword(const u16 c) {
104 return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Alan Cox52894752012-03-02 15:00:02 +0000107/**
108 * set loadlut - load the LUT table
109 * @p: user table
110 *
111 * Load the LUT table from user space. The caller must hold the console
112 * lock. Make a temporary copy so a partial update doesn't make a mess.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114int sel_loadlut(char __user *p)
115{
Alan Cox52894752012-03-02 15:00:02 +0000116 u32 tmplut[8];
117 if (copy_from_user(tmplut, (u32 __user *)(p+4), 32))
118 return -EFAULT;
119 memcpy(inwordLut, tmplut, 32);
120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/* does screen address p correspond to character at LH/RH edge of screen? */
124static inline int atedge(const int p, int size_row)
125{
126 return (!(p % size_row) || !((p + 2) % size_row));
127}
128
129/* constrain v such that v <= u */
130static inline unsigned short limit(const unsigned short v, const unsigned short u)
131{
132 return (v > u) ? u : v;
133}
134
Jan Engelhardt759448f2007-07-15 23:40:40 -0700135/* stores the char in UTF8 and returns the number of bytes used (1-3) */
136static int store_utf8(u16 c, char *p)
137{
138 if (c < 0x80) {
139 /* 0******* */
140 p[0] = c;
141 return 1;
142 } else if (c < 0x800) {
143 /* 110***** 10****** */
144 p[0] = 0xc0 | (c >> 6);
145 p[1] = 0x80 | (c & 0x3f);
146 return 2;
147 } else {
148 /* 1110**** 10****** 10****** */
149 p[0] = 0xe0 | (c >> 12);
150 p[1] = 0x80 | ((c >> 6) & 0x3f);
151 p[2] = 0x80 | (c & 0x3f);
152 return 3;
153 }
154}
155
Alan Cox52894752012-03-02 15:00:02 +0000156/**
157 * set_selection - set the current selection.
158 * @sel: user selection info
159 * @tty: the console tty
160 *
161 * Invoked by the ioctl handle for the vt layer.
162 *
163 * The entire selection process is managed under the console_lock. It's
164 * a lot under the lock but its hardly a performance path
165 */
Jiri Slabyccd35862020-02-28 12:54:05 +0100166static int __set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct vc_data *vc = vc_cons[fg_console].d;
169 int sel_mode, new_sel_start, new_sel_end, spc;
170 char *bp, *obp;
Jan Engelhardt759448f2007-07-15 23:40:40 -0700171 int i, ps, pe, multiplier;
172 u16 c;
Jiri Slaby290a9382020-02-10 09:11:31 +0100173 int mode, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 poke_blanked_console();
176
177 { unsigned short xs, ys, xe, ye;
178
179 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
180 return -EFAULT;
181 __get_user(xs, &sel->xs);
182 __get_user(ys, &sel->ys);
183 __get_user(xe, &sel->xe);
184 __get_user(ye, &sel->ye);
185 __get_user(sel_mode, &sel->sel_mode);
186 xs--; ys--; xe--; ye--;
187 xs = limit(xs, vc->vc_cols - 1);
188 ys = limit(ys, vc->vc_rows - 1);
189 xe = limit(xe, vc->vc_cols - 1);
190 ye = limit(ye, vc->vc_rows - 1);
191 ps = ys * vc->vc_size_row + (xs << 1);
192 pe = ye * vc->vc_size_row + (xe << 1);
193
194 if (sel_mode == TIOCL_SELCLEAR) {
195 /* useful for screendump without selection highlights */
196 clear_selection();
197 return 0;
198 }
199
200 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
201 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
202 return 0;
203 }
204 }
205
206 if (ps > pe) /* make sel_start <= sel_end */
207 {
208 int tmp = ps;
209 ps = pe;
210 pe = tmp;
211 }
212
213 if (sel_cons != vc_cons[fg_console].d) {
214 clear_selection();
215 sel_cons = vc_cons[fg_console].d;
216 }
Alan Cox079c9532012-02-28 14:49:23 +0000217 mode = vt_do_kdgkbmode(fg_console);
218 if (mode == K_UNICODE)
219 use_unicode = 1;
220 else
221 use_unicode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 switch (sel_mode)
224 {
225 case TIOCL_SELCHAR: /* character-by-character selection */
226 new_sel_start = ps;
227 new_sel_end = pe;
228 break;
229 case TIOCL_SELWORD: /* word-by-word selection */
230 spc = isspace(sel_pos(ps));
231 for (new_sel_start = ps; ; ps -= 2)
232 {
233 if ((spc && !isspace(sel_pos(ps))) ||
234 (!spc && !inword(sel_pos(ps))))
235 break;
236 new_sel_start = ps;
237 if (!(ps % vc->vc_size_row))
238 break;
239 }
240 spc = isspace(sel_pos(pe));
241 for (new_sel_end = pe; ; pe += 2)
242 {
243 if ((spc && !isspace(sel_pos(pe))) ||
244 (!spc && !inword(sel_pos(pe))))
245 break;
246 new_sel_end = pe;
247 if (!((pe + 2) % vc->vc_size_row))
248 break;
249 }
250 break;
251 case TIOCL_SELLINE: /* line-by-line selection */
252 new_sel_start = ps - ps % vc->vc_size_row;
253 new_sel_end = pe + vc->vc_size_row
254 - pe % vc->vc_size_row - 2;
255 break;
256 case TIOCL_SELPOINTER:
257 highlight_pointer(pe);
Jiri Slabye5be0e22020-02-28 12:54:06 +0100258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 default:
Jiri Slabye5be0e22020-02-28 12:54:06 +0100260 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
263 /* remove the pointer */
264 highlight_pointer(-1);
265
266 /* select to end of line if on trailing space */
267 if (new_sel_end > new_sel_start &&
268 !atedge(new_sel_end, vc->vc_size_row) &&
269 isspace(sel_pos(new_sel_end))) {
270 for (pe = new_sel_end + 2; ; pe += 2)
271 if (!isspace(sel_pos(pe)) ||
272 atedge(pe, vc->vc_size_row))
273 break;
274 if (isspace(sel_pos(pe)))
275 new_sel_end = pe;
276 }
277 if (sel_start == -1) /* no current selection */
278 highlight(new_sel_start, new_sel_end);
279 else if (new_sel_start == sel_start)
280 {
281 if (new_sel_end == sel_end) /* no action required */
Jiri Slabye5be0e22020-02-28 12:54:06 +0100282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 else if (new_sel_end > sel_end) /* extend to right */
284 highlight(sel_end + 2, new_sel_end);
285 else /* contract from right */
286 highlight(new_sel_end + 2, sel_end);
287 }
288 else if (new_sel_end == sel_end)
289 {
290 if (new_sel_start < sel_start) /* extend to left */
291 highlight(new_sel_start, sel_start - 2);
292 else /* contract from left */
293 highlight(sel_start, new_sel_start - 2);
294 }
295 else /* some other case; start selection from scratch */
296 {
297 clear_selection();
298 highlight(new_sel_start, new_sel_end);
299 }
300 sel_start = new_sel_start;
301 sel_end = new_sel_end;
302
303 /* Allocate a new buffer before freeing the old one ... */
Jan Engelhardt759448f2007-07-15 23:40:40 -0700304 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
Mikulas Patocka878b8612009-01-30 15:27:14 -0500305 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!bp) {
307 printk(KERN_WARNING "selection: kmalloc() failed\n");
308 clear_selection();
Jiri Slabye5be0e22020-02-28 12:54:06 +0100309 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Jesper Juhl735d5662005-11-07 01:01:29 -0800311 kfree(sel_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 sel_buffer = bp;
313
314 obp = bp;
315 for (i = sel_start; i <= sel_end; i += 2) {
Jan Engelhardt759448f2007-07-15 23:40:40 -0700316 c = sel_pos(i);
317 if (use_unicode)
318 bp += store_utf8(c, bp);
319 else
320 *bp++ = c;
321 if (!isspace(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 obp = bp;
323 if (! ((i + 2) % vc->vc_size_row)) {
324 /* strip trailing blanks from line and add newline,
325 unless non-space at end of line. */
326 if (obp != bp) {
327 bp = obp;
328 *bp++ = '\r';
329 }
330 obp = bp;
331 }
332 }
333 sel_buffer_lth = bp - sel_buffer;
Jiri Slabye5be0e22020-02-28 12:54:06 +0100334
Jiri Slaby290a9382020-02-10 09:11:31 +0100335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Jiri Slabyccd35862020-02-28 12:54:05 +0100338int set_selection(const struct tiocl_selection __user *v, struct tty_struct *tty)
339{
340 int ret;
341
Jiri Slabye5be0e22020-02-28 12:54:06 +0100342 mutex_lock(&sel_lock);
Jiri Slabyccd35862020-02-28 12:54:05 +0100343 console_lock();
344 ret = __set_selection(v, tty);
345 console_unlock();
Jiri Slabye5be0e22020-02-28 12:54:06 +0100346 mutex_unlock(&sel_lock);
Jiri Slabyccd35862020-02-28 12:54:05 +0100347
348 return ret;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/* Insert the contents of the selection buffer into the
352 * queue of the tty associated with the current console.
353 * Invoked by ioctl().
Jiri Slaby906cbe12011-07-14 14:35:14 +0200354 *
Alan Cox20f62572012-03-02 14:59:37 +0000355 * Locking: called without locks. Calls the ldisc wrongly with
356 * unsafe methods,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 */
358int paste_selection(struct tty_struct *tty)
359{
Alan Coxc9f19e92009-01-02 13:47:26 +0000360 struct vc_data *vc = tty->driver_data;
Alan Cox33f0f882006-01-09 20:54:13 -0800361 int pasted = 0;
362 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct tty_ldisc *ld;
364 DECLARE_WAITQUEUE(wait, current);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100365 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Torben Hohnac751ef2011-01-25 15:07:35 -0800367 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 poke_blanked_console();
Torben Hohnac751ef2011-01-25 15:07:35 -0800369 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jiri Slaby7ee00fd2012-10-18 22:26:32 +0200371 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -0800372 if (!ld)
373 return -EIO; /* ldisc was hung up */
Peter Hurleya7c8d582013-06-15 09:36:15 -0400374 tty_buffer_lock_exclusive(&vc->port);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 add_wait_queue(&vc->paste_wait, &wait);
Jiri Slaby290a9382020-02-10 09:11:31 +0100377 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 while (sel_buffer && sel_buffer_lth > pasted) {
379 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100380 if (signal_pending(current)) {
381 ret = -EINTR;
382 break;
383 }
Peter Hurley97ef38b2016-04-09 17:11:36 -0700384 if (tty_throttled(tty)) {
Jiri Slaby290a9382020-02-10 09:11:31 +0100385 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 schedule();
Jiri Slaby290a9382020-02-10 09:11:31 +0100387 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 continue;
389 }
Peter Hurley61e86cc2015-07-12 20:47:35 -0400390 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 count = sel_buffer_lth - pasted;
Peter Hurley24a89d12013-06-15 09:14:15 -0400392 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
393 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 pasted += count;
395 }
Jiri Slaby290a9382020-02-10 09:11:31 +0100396 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 remove_wait_queue(&vc->paste_wait, &wait);
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700398 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Peter Hurleya7c8d582013-06-15 09:36:15 -0400400 tty_buffer_unlock_exclusive(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 tty_ldisc_deref(ld);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}