1 // SPDX-License-Identifier: GPL-2.0
3 * This module exports the functions:
5 * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
6 * 'void clear_selection(void)'
7 * 'int paste_selection(struct tty_struct *)'
8 * 'int sel_loadlut(char __user *)'
10 * Now that /dev/vcs exists, most of this can disappear again.
13 #include <linux/module.h>
14 #include <linux/tty.h>
15 #include <linux/sched.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
21 #include <linux/uaccess.h>
23 #include <linux/kbd_kern.h>
24 #include <linux/vt_kern.h>
25 #include <linux/consolemap.h>
26 #include <linux/selection.h>
27 #include <linux/tiocl.h>
28 #include <linux/console.h>
29 #include <linux/tty_flip.h>
31 #include <linux/sched/signal.h>
33 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
34 #define isspace(c) ((c) == ' ')
36 extern void poke_blanked_console(void);
38 /* FIXME: all this needs locking */
39 /* Variables for selection control. */
40 /* Use a dynamic buffer, instead of static (Dec 1994) */
41 struct vc_data *sel_cons; /* must not be deallocated */
42 static int use_unicode;
43 static volatile int sel_start = -1; /* cleared by clear_selection */
45 static int sel_buffer_lth;
46 static char *sel_buffer;
47 static DEFINE_MUTEX(sel_lock);
49 /* clear_selection, highlight and highlight_pointer can be called
50 from interrupt (via scrollback/front) */
52 /* set reverse video on characters s-e of console with selection. */
53 static inline void highlight(const int s, const int e)
55 invert_screen(sel_cons, s, e-s+2, 1);
58 /* use complementary color to show the pointer */
59 static inline void highlight_pointer(const int where)
61 complement_pos(sel_cons, where);
68 return screen_glyph_unicode(sel_cons, n / 2);
69 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
74 * clear_selection - remove current selection
76 * Remove the current selection highlight, if any from the console
77 * holding the selection. The caller must hold the console lock.
79 void clear_selection(void)
81 highlight_pointer(-1); /* hide the pointer */
82 if (sel_start != -1) {
83 highlight(sel_start, sel_end);
88 bool vc_is_sel(struct vc_data *vc)
90 return vc == sel_cons;
94 * User settable table: what characters are to be considered alphabetic?
95 * 128 bits. Locked by the console lock.
97 static u32 inwordLut[]={
98 0x00000000, /* control chars */
99 0x03FFE000, /* digits and "-./" */
100 0x87FFFFFE, /* uppercase and '_' */
101 0x07FFFFFE, /* lowercase */
104 static inline int inword(const u32 c)
106 return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
110 * set loadlut - load the LUT table
113 * Load the LUT table from user space. The caller must hold the console
114 * lock. Make a temporary copy so a partial update doesn't make a mess.
116 int sel_loadlut(char __user *p)
118 u32 tmplut[ARRAY_SIZE(inwordLut)];
119 if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
121 memcpy(inwordLut, tmplut, sizeof(inwordLut));
125 /* does screen address p correspond to character at LH/RH edge of screen? */
126 static inline int atedge(const int p, int size_row)
128 return (!(p % size_row) || !((p + 2) % size_row));
131 /* stores the char in UTF8 and returns the number of bytes used (1-4) */
132 static int store_utf8(u32 c, char *p)
138 } else if (c < 0x800) {
139 /* 110***** 10****** */
140 p[0] = 0xc0 | (c >> 6);
141 p[1] = 0x80 | (c & 0x3f);
143 } else if (c < 0x10000) {
144 /* 1110**** 10****** 10****** */
145 p[0] = 0xe0 | (c >> 12);
146 p[1] = 0x80 | ((c >> 6) & 0x3f);
147 p[2] = 0x80 | (c & 0x3f);
149 } else if (c < 0x110000) {
150 /* 11110*** 10****** 10****** 10****** */
151 p[0] = 0xf0 | (c >> 18);
152 p[1] = 0x80 | ((c >> 12) & 0x3f);
153 p[2] = 0x80 | ((c >> 6) & 0x3f);
154 p[3] = 0x80 | (c & 0x3f);
157 /* outside Unicode, replace with U+FFFD */
166 * set_selection - set the current selection.
167 * @sel: user selection info
168 * @tty: the console tty
170 * Invoked by the ioctl handle for the vt layer.
172 * The entire selection process is managed under the console_lock. It's
173 * a lot under the lock but its hardly a performance path
175 static int __set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
177 struct vc_data *vc = vc_cons[fg_console].d;
178 int new_sel_start, new_sel_end, spc;
179 struct tiocl_selection v;
181 int i, ps, pe, multiplier;
185 poke_blanked_console();
186 if (copy_from_user(&v, sel, sizeof(*sel)))
189 v.xs = min_t(u16, v.xs - 1, vc->vc_cols - 1);
190 v.ys = min_t(u16, v.ys - 1, vc->vc_rows - 1);
191 v.xe = min_t(u16, v.xe - 1, vc->vc_cols - 1);
192 v.ye = min_t(u16, v.ye - 1, vc->vc_rows - 1);
193 ps = v.ys * vc->vc_size_row + (v.xs << 1);
194 pe = v.ye * vc->vc_size_row + (v.xe << 1);
196 if (v.sel_mode == TIOCL_SELCLEAR) {
197 /* useful for screendump without selection highlights */
202 if (mouse_reporting() && (v.sel_mode & TIOCL_SELMOUSEREPORT)) {
203 mouse_report(tty, v.sel_mode & TIOCL_SELBUTTONMASK, v.xs, v.ys);
207 if (ps > pe) /* make sel_start <= sel_end */
210 if (sel_cons != vc_cons[fg_console].d) {
212 sel_cons = vc_cons[fg_console].d;
214 mode = vt_do_kdgkbmode(fg_console);
215 if (mode == K_UNICODE)
222 case TIOCL_SELCHAR: /* character-by-character selection */
226 case TIOCL_SELWORD: /* word-by-word selection */
227 spc = isspace(sel_pos(ps));
228 for (new_sel_start = ps; ; ps -= 2)
230 if ((spc && !isspace(sel_pos(ps))) ||
231 (!spc && !inword(sel_pos(ps))))
234 if (!(ps % vc->vc_size_row))
237 spc = isspace(sel_pos(pe));
238 for (new_sel_end = pe; ; pe += 2)
240 if ((spc && !isspace(sel_pos(pe))) ||
241 (!spc && !inword(sel_pos(pe))))
244 if (!((pe + 2) % vc->vc_size_row))
248 case TIOCL_SELLINE: /* line-by-line selection */
249 new_sel_start = ps - ps % vc->vc_size_row;
250 new_sel_end = pe + vc->vc_size_row
251 - pe % vc->vc_size_row - 2;
253 case TIOCL_SELPOINTER:
254 highlight_pointer(pe);
260 /* remove the pointer */
261 highlight_pointer(-1);
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))
271 if (isspace(sel_pos(pe)))
274 if (sel_start == -1) /* no current selection */
275 highlight(new_sel_start, new_sel_end);
276 else if (new_sel_start == sel_start)
278 if (new_sel_end == sel_end) /* no action required */
280 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);
285 else if (new_sel_end == sel_end)
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);
292 else /* some other case; start selection from scratch */
295 highlight(new_sel_start, new_sel_end);
297 sel_start = new_sel_start;
298 sel_end = new_sel_end;
300 /* Allocate a new buffer before freeing the old one ... */
301 multiplier = use_unicode ? 4 : 1; /* chars can take up to 4 bytes */
302 bp = kmalloc_array((sel_end - sel_start) / 2 + 1, multiplier,
305 printk(KERN_WARNING "selection: kmalloc() failed\n");
313 for (i = sel_start; i <= sel_end; i += 2) {
316 bp += store_utf8(c, 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. */
331 sel_buffer_lth = bp - sel_buffer;
336 int set_selection(const struct tiocl_selection __user *v, struct tty_struct *tty)
340 mutex_lock(&sel_lock);
342 ret = __set_selection(v, tty);
344 mutex_unlock(&sel_lock);
349 /* Insert the contents of the selection buffer into the
350 * queue of the tty associated with the current console.
351 * Invoked by ioctl().
353 * Locking: called without locks. Calls the ldisc wrongly with
356 int paste_selection(struct tty_struct *tty)
358 struct vc_data *vc = tty->driver_data;
361 struct tty_ldisc *ld;
362 DECLARE_WAITQUEUE(wait, current);
366 poke_blanked_console();
369 ld = tty_ldisc_ref_wait(tty);
371 return -EIO; /* ldisc was hung up */
372 tty_buffer_lock_exclusive(&vc->port);
374 add_wait_queue(&vc->paste_wait, &wait);
375 mutex_lock(&sel_lock);
376 while (sel_buffer && sel_buffer_lth > pasted) {
377 set_current_state(TASK_INTERRUPTIBLE);
378 if (signal_pending(current)) {
382 if (tty_throttled(tty)) {
383 mutex_unlock(&sel_lock);
385 mutex_lock(&sel_lock);
388 __set_current_state(TASK_RUNNING);
389 count = sel_buffer_lth - pasted;
390 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
394 mutex_unlock(&sel_lock);
395 remove_wait_queue(&vc->paste_wait, &wait);
396 __set_current_state(TASK_RUNNING);
398 tty_buffer_unlock_exclusive(&vc->port);