blob: 1edc1a36db4acaf01d03552e4b13d88e08b38be7 [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 */
Jiri Slabyccd35862020-02-28 12:54:05 +0100161static int __set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
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
208 if (sel_cons != vc_cons[fg_console].d) {
209 clear_selection();
210 sel_cons = vc_cons[fg_console].d;
211 }
Alan Cox079c9532012-02-28 14:49:23 +0000212 mode = vt_do_kdgkbmode(fg_console);
213 if (mode == K_UNICODE)
214 use_unicode = 1;
215 else
216 use_unicode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 switch (sel_mode)
219 {
220 case TIOCL_SELCHAR: /* character-by-character selection */
221 new_sel_start = ps;
222 new_sel_end = pe;
223 break;
224 case TIOCL_SELWORD: /* word-by-word selection */
225 spc = isspace(sel_pos(ps));
226 for (new_sel_start = ps; ; ps -= 2)
227 {
228 if ((spc && !isspace(sel_pos(ps))) ||
229 (!spc && !inword(sel_pos(ps))))
230 break;
231 new_sel_start = ps;
232 if (!(ps % vc->vc_size_row))
233 break;
234 }
235 spc = isspace(sel_pos(pe));
236 for (new_sel_end = pe; ; pe += 2)
237 {
238 if ((spc && !isspace(sel_pos(pe))) ||
239 (!spc && !inword(sel_pos(pe))))
240 break;
241 new_sel_end = pe;
242 if (!((pe + 2) % vc->vc_size_row))
243 break;
244 }
245 break;
246 case TIOCL_SELLINE: /* line-by-line selection */
247 new_sel_start = ps - ps % vc->vc_size_row;
248 new_sel_end = pe + vc->vc_size_row
249 - pe % vc->vc_size_row - 2;
250 break;
251 case TIOCL_SELPOINTER:
252 highlight_pointer(pe);
Jiri Slabye5be0e22020-02-28 12:54:06 +0100253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 default:
Jiri Slabye5be0e22020-02-28 12:54:06 +0100255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
258 /* remove the pointer */
259 highlight_pointer(-1);
260
261 /* select to end of line if on trailing space */
262 if (new_sel_end > new_sel_start &&
263 !atedge(new_sel_end, vc->vc_size_row) &&
264 isspace(sel_pos(new_sel_end))) {
265 for (pe = new_sel_end + 2; ; pe += 2)
266 if (!isspace(sel_pos(pe)) ||
267 atedge(pe, vc->vc_size_row))
268 break;
269 if (isspace(sel_pos(pe)))
270 new_sel_end = pe;
271 }
272 if (sel_start == -1) /* no current selection */
273 highlight(new_sel_start, new_sel_end);
274 else if (new_sel_start == sel_start)
275 {
276 if (new_sel_end == sel_end) /* no action required */
Jiri Slabye5be0e22020-02-28 12:54:06 +0100277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 else if (new_sel_end > sel_end) /* extend to right */
279 highlight(sel_end + 2, new_sel_end);
280 else /* contract from right */
281 highlight(new_sel_end + 2, sel_end);
282 }
283 else if (new_sel_end == sel_end)
284 {
285 if (new_sel_start < sel_start) /* extend to left */
286 highlight(new_sel_start, sel_start - 2);
287 else /* contract from left */
288 highlight(sel_start, new_sel_start - 2);
289 }
290 else /* some other case; start selection from scratch */
291 {
292 clear_selection();
293 highlight(new_sel_start, new_sel_end);
294 }
295 sel_start = new_sel_start;
296 sel_end = new_sel_end;
297
298 /* Allocate a new buffer before freeing the old one ... */
Jan Engelhardt759448f2007-07-15 23:40:40 -0700299 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
Mikulas Patocka878b8612009-01-30 15:27:14 -0500300 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!bp) {
302 printk(KERN_WARNING "selection: kmalloc() failed\n");
303 clear_selection();
Jiri Slabye5be0e22020-02-28 12:54:06 +0100304 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Jesper Juhl735d5662005-11-07 01:01:29 -0800306 kfree(sel_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 sel_buffer = bp;
308
309 obp = bp;
310 for (i = sel_start; i <= sel_end; i += 2) {
Jan Engelhardt759448f2007-07-15 23:40:40 -0700311 c = sel_pos(i);
312 if (use_unicode)
313 bp += store_utf8(c, bp);
314 else
315 *bp++ = c;
316 if (!isspace(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 obp = bp;
318 if (! ((i + 2) % vc->vc_size_row)) {
319 /* strip trailing blanks from line and add newline,
320 unless non-space at end of line. */
321 if (obp != bp) {
322 bp = obp;
323 *bp++ = '\r';
324 }
325 obp = bp;
326 }
327 }
328 sel_buffer_lth = bp - sel_buffer;
Jiri Slabye5be0e22020-02-28 12:54:06 +0100329
Jiri Slaby290a9382020-02-10 09:11:31 +0100330 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Jiri Slabyccd35862020-02-28 12:54:05 +0100333int set_selection(const struct tiocl_selection __user *v, struct tty_struct *tty)
334{
335 int ret;
336
Jiri Slabye5be0e22020-02-28 12:54:06 +0100337 mutex_lock(&sel_lock);
Jiri Slabyccd35862020-02-28 12:54:05 +0100338 console_lock();
339 ret = __set_selection(v, tty);
340 console_unlock();
Jiri Slabye5be0e22020-02-28 12:54:06 +0100341 mutex_unlock(&sel_lock);
Jiri Slabyccd35862020-02-28 12:54:05 +0100342
343 return ret;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/* Insert the contents of the selection buffer into the
347 * queue of the tty associated with the current console.
348 * Invoked by ioctl().
Jiri Slaby906cbe12011-07-14 14:35:14 +0200349 *
Alan Cox20f62572012-03-02 14:59:37 +0000350 * Locking: called without locks. Calls the ldisc wrongly with
351 * unsafe methods,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
353int paste_selection(struct tty_struct *tty)
354{
Alan Coxc9f19e92009-01-02 13:47:26 +0000355 struct vc_data *vc = tty->driver_data;
Alan Cox33f0f882006-01-09 20:54:13 -0800356 int pasted = 0;
357 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct tty_ldisc *ld;
359 DECLARE_WAITQUEUE(wait, current);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100360 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Torben Hohnac751ef2011-01-25 15:07:35 -0800362 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 poke_blanked_console();
Torben Hohnac751ef2011-01-25 15:07:35 -0800364 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jiri Slaby7ee00fd2012-10-18 22:26:32 +0200366 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -0800367 if (!ld)
368 return -EIO; /* ldisc was hung up */
Peter Hurleya7c8d582013-06-15 09:36:15 -0400369 tty_buffer_lock_exclusive(&vc->port);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 add_wait_queue(&vc->paste_wait, &wait);
Jiri Slaby290a9382020-02-10 09:11:31 +0100372 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 while (sel_buffer && sel_buffer_lth > pasted) {
374 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100375 if (signal_pending(current)) {
376 ret = -EINTR;
377 break;
378 }
Peter Hurley97ef38b2016-04-09 17:11:36 -0700379 if (tty_throttled(tty)) {
Jiri Slaby290a9382020-02-10 09:11:31 +0100380 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 schedule();
Jiri Slaby290a9382020-02-10 09:11:31 +0100382 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 continue;
384 }
Peter Hurley61e86cc2015-07-12 20:47:35 -0400385 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 count = sel_buffer_lth - pasted;
Peter Hurley24a89d12013-06-15 09:14:15 -0400387 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
388 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 pasted += count;
390 }
Jiri Slaby290a9382020-02-10 09:11:31 +0100391 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 remove_wait_queue(&vc->paste_wait, &wait);
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700393 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Peter Hurleya7c8d582013-06-15 09:36:15 -0400395 tty_buffer_unlock_exclusive(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 tty_ldisc_deref(ld);
Jiri Slaby5827cbb2020-02-10 09:11:30 +0100397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}