1 // SPDX-License-Identifier: GPL-2.0
3 * This module exports the functions:
5 * 'int set_selection_user(struct tiocl_selection __user *,
6 * struct tty_struct *)'
7 * 'int set_selection_kernel(struct tiocl_selection *, struct tty_struct *)'
8 * 'void clear_selection(void)'
9 * 'int paste_selection(struct tty_struct *)'
10 * 'int sel_loadlut(char __user *)'
12 * Now that /dev/vcs exists, most of this can disappear again.
15 #include <linux/module.h>
16 #include <linux/tty.h>
17 #include <linux/sched.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
23 #include <linux/uaccess.h>
25 #include <linux/kbd_kern.h>
26 #include <linux/vt_kern.h>
27 #include <linux/consolemap.h>
28 #include <linux/selection.h>
29 #include <linux/tiocl.h>
30 #include <linux/console.h>
31 #include <linux/tty_flip.h>
33 #include <linux/sched/signal.h>
35 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
36 #define isspace(c) ((c) == ' ')
38 extern void poke_blanked_console(void);
40 /* FIXME: all this needs locking */
41 /* Variables for selection control. */
42 /* Use a dynamic buffer, instead of static (Dec 1994) */
43 struct vc_data *sel_cons; /* must not be deallocated */
44 static int use_unicode;
45 static volatile int sel_start = -1; /* cleared by clear_selection */
47 static int sel_buffer_lth;
48 static char *sel_buffer;
49 static DEFINE_MUTEX(sel_lock);
51 /* clear_selection, highlight and highlight_pointer can be called
52 from interrupt (via scrollback/front) */
54 /* set reverse video on characters s-e of console with selection. */
55 static inline void highlight(const int s, const int e)
57 invert_screen(sel_cons, s, e-s+2, 1);
60 /* use complementary color to show the pointer */
61 static inline void highlight_pointer(const int where)
63 complement_pos(sel_cons, where);
70 return screen_glyph_unicode(sel_cons, n / 2);
71 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
76 * clear_selection - remove current selection
78 * Remove the current selection highlight, if any from the console
79 * holding the selection. The caller must hold the console lock.
81 void clear_selection(void)
83 highlight_pointer(-1); /* hide the pointer */
84 if (sel_start != -1) {
85 highlight(sel_start, sel_end);
89 EXPORT_SYMBOL_GPL(clear_selection);
92 * User settable table: what characters are to be considered alphabetic?
93 * 128 bits. Locked by the console lock.
95 static u32 inwordLut[]={
96 0x00000000, /* control chars */
97 0x03FFE000, /* digits and "-./" */
98 0x87FFFFFE, /* uppercase and '_' */
99 0x07FFFFFE, /* lowercase */
102 static inline int inword(const u32 c)
104 return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
108 * set loadlut - load the LUT table
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.
114 int sel_loadlut(char __user *p)
116 u32 tmplut[ARRAY_SIZE(inwordLut)];
117 if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
119 memcpy(inwordLut, tmplut, sizeof(inwordLut));
123 /* does screen address p correspond to character at LH/RH edge of screen? */
124 static inline int atedge(const int p, int size_row)
126 return (!(p % size_row) || !((p + 2) % size_row));
129 /* stores the char in UTF8 and returns the number of bytes used (1-4) */
130 static int store_utf8(u32 c, char *p)
136 } else if (c < 0x800) {
137 /* 110***** 10****** */
138 p[0] = 0xc0 | (c >> 6);
139 p[1] = 0x80 | (c & 0x3f);
141 } else if (c < 0x10000) {
142 /* 1110**** 10****** 10****** */
143 p[0] = 0xe0 | (c >> 12);
144 p[1] = 0x80 | ((c >> 6) & 0x3f);
145 p[2] = 0x80 | (c & 0x3f);
147 } else if (c < 0x110000) {
148 /* 11110*** 10****** 10****** 10****** */
149 p[0] = 0xf0 | (c >> 18);
150 p[1] = 0x80 | ((c >> 12) & 0x3f);
151 p[2] = 0x80 | ((c >> 6) & 0x3f);
152 p[3] = 0x80 | (c & 0x3f);
155 /* outside Unicode, replace with U+FFFD */
164 * set_selection_user - set the current selection.
165 * @sel: user selection info
166 * @tty: the console tty
168 * Invoked by the ioctl handle for the vt layer.
170 * The entire selection process is managed under the console_lock. It's
171 * a lot under the lock but its hardly a performance path
173 int set_selection_user(const struct tiocl_selection __user *sel,
174 struct tty_struct *tty)
176 struct tiocl_selection v;
178 if (copy_from_user(&v, sel, sizeof(*sel)))
181 return set_selection_kernel(&v, tty);
184 int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
186 struct vc_data *vc = vc_cons[fg_console].d;
187 int new_sel_start, new_sel_end, spc;
189 int i, ps, pe, multiplier;
193 poke_blanked_console();
195 v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
196 v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
197 v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
198 v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
199 ps = v->ys * vc->vc_size_row + (v->xs << 1);
200 pe = v->ye * vc->vc_size_row + (v->xe << 1);
202 if (v->sel_mode == TIOCL_SELCLEAR) {
203 /* useful for screendump without selection highlights */
208 if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
209 mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
214 if (ps > pe) /* make sel_start <= sel_end */
217 mutex_lock(&sel_lock);
218 if (sel_cons != vc_cons[fg_console].d) {
220 sel_cons = vc_cons[fg_console].d;
222 mode = vt_do_kdgkbmode(fg_console);
223 if (mode == K_UNICODE)
230 case TIOCL_SELCHAR: /* character-by-character selection */
234 case TIOCL_SELWORD: /* word-by-word selection */
235 spc = isspace(sel_pos(ps));
236 for (new_sel_start = ps; ; ps -= 2)
238 if ((spc && !isspace(sel_pos(ps))) ||
239 (!spc && !inword(sel_pos(ps))))
242 if (!(ps % vc->vc_size_row))
245 spc = isspace(sel_pos(pe));
246 for (new_sel_end = pe; ; pe += 2)
248 if ((spc && !isspace(sel_pos(pe))) ||
249 (!spc && !inword(sel_pos(pe))))
252 if (!((pe + 2) % vc->vc_size_row))
256 case TIOCL_SELLINE: /* line-by-line selection */
257 new_sel_start = ps - ps % vc->vc_size_row;
258 new_sel_end = pe + vc->vc_size_row
259 - pe % vc->vc_size_row - 2;
261 case TIOCL_SELPOINTER:
262 highlight_pointer(pe);
269 /* remove the pointer */
270 highlight_pointer(-1);
272 /* select to end of line if on trailing space */
273 if (new_sel_end > new_sel_start &&
274 !atedge(new_sel_end, vc->vc_size_row) &&
275 isspace(sel_pos(new_sel_end))) {
276 for (pe = new_sel_end + 2; ; pe += 2)
277 if (!isspace(sel_pos(pe)) ||
278 atedge(pe, vc->vc_size_row))
280 if (isspace(sel_pos(pe)))
283 if (sel_start == -1) /* no current selection */
284 highlight(new_sel_start, new_sel_end);
285 else if (new_sel_start == sel_start)
287 if (new_sel_end == sel_end) /* no action required */
289 else if (new_sel_end > sel_end) /* extend to right */
290 highlight(sel_end + 2, new_sel_end);
291 else /* contract from right */
292 highlight(new_sel_end + 2, sel_end);
294 else if (new_sel_end == sel_end)
296 if (new_sel_start < sel_start) /* extend to left */
297 highlight(new_sel_start, sel_start - 2);
298 else /* contract from left */
299 highlight(sel_start, new_sel_start - 2);
301 else /* some other case; start selection from scratch */
304 highlight(new_sel_start, new_sel_end);
306 sel_start = new_sel_start;
307 sel_end = new_sel_end;
309 /* Allocate a new buffer before freeing the old one ... */
310 multiplier = use_unicode ? 4 : 1; /* chars can take up to 4 bytes */
311 bp = kmalloc_array((sel_end - sel_start) / 2 + 1, multiplier,
314 printk(KERN_WARNING "selection: kmalloc() failed\n");
323 for (i = sel_start; i <= sel_end; i += 2) {
326 bp += store_utf8(c, bp);
331 if (! ((i + 2) % vc->vc_size_row)) {
332 /* strip trailing blanks from line and add newline,
333 unless non-space at end of line. */
341 sel_buffer_lth = bp - sel_buffer;
343 mutex_unlock(&sel_lock);
346 EXPORT_SYMBOL_GPL(set_selection_kernel);
348 /* Insert the contents of the selection buffer into the
349 * queue of the tty associated with the current console.
350 * Invoked by ioctl().
352 * Locking: called without locks. Calls the ldisc wrongly with
355 int paste_selection(struct tty_struct *tty)
357 struct vc_data *vc = tty->driver_data;
360 struct tty_ldisc *ld;
361 DECLARE_WAITQUEUE(wait, current);
365 poke_blanked_console();
368 ld = tty_ldisc_ref_wait(tty);
370 return -EIO; /* ldisc was hung up */
371 tty_buffer_lock_exclusive(&vc->port);
373 add_wait_queue(&vc->paste_wait, &wait);
374 mutex_lock(&sel_lock);
375 while (sel_buffer && sel_buffer_lth > pasted) {
376 set_current_state(TASK_INTERRUPTIBLE);
377 if (signal_pending(current)) {
381 if (tty_throttled(tty)) {
382 mutex_unlock(&sel_lock);
384 mutex_lock(&sel_lock);
387 __set_current_state(TASK_RUNNING);
388 count = sel_buffer_lth - pasted;
389 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
393 mutex_unlock(&sel_lock);
394 remove_wait_queue(&vc->paste_wait, &wait);
395 __set_current_state(TASK_RUNNING);
397 tty_buffer_unlock_exclusive(&vc->port);
401 EXPORT_SYMBOL_GPL(paste_selection);