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/slab.h>
18 #include <linux/types.h>
20 #include <linux/uaccess.h>
22 #include <linux/kbd_kern.h>
23 #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>
28 #include <linux/tty_flip.h>
30 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
31 #define isspace(c) ((c) == ' ')
33 extern void poke_blanked_console(void);
35 /* FIXME: all this needs locking */
36 /* Variables for selection control. */
37 /* Use a dynamic buffer, instead of static (Dec 1994) */
38 struct vc_data *sel_cons; /* must not be deallocated */
39 static int use_unicode;
40 static volatile int sel_start = -1; /* cleared by clear_selection */
42 static int sel_buffer_lth;
43 static char *sel_buffer;
45 /* clear_selection, highlight and highlight_pointer can be called
46 from interrupt (via scrollback/front) */
48 /* set reverse video on characters s-e of console with selection. */
49 static inline void highlight(const int s, const int e)
51 invert_screen(sel_cons, s, e-s+2, 1);
54 /* use complementary color to show the pointer */
55 static inline void highlight_pointer(const int where)
57 complement_pos(sel_cons, where);
64 return screen_glyph_unicode(sel_cons, n / 2);
65 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
70 * clear_selection - remove current selection
72 * Remove the current selection highlight, if any from the console
73 * holding the selection. The caller must hold the console lock.
75 void clear_selection(void)
77 highlight_pointer(-1); /* hide the pointer */
78 if (sel_start != -1) {
79 highlight(sel_start, sel_end);
85 * User settable table: what characters are to be considered alphabetic?
86 * 128 bits. Locked by the console lock.
88 static u32 inwordLut[]={
89 0x00000000, /* control chars */
90 0x03FFE000, /* digits and "-./" */
91 0x87FFFFFE, /* uppercase and '_' */
92 0x07FFFFFE, /* lowercase */
95 static inline int inword(const u32 c)
97 return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
101 * set loadlut - load the LUT table
104 * Load the LUT table from user space. The caller must hold the console
105 * lock. Make a temporary copy so a partial update doesn't make a mess.
107 int sel_loadlut(char __user *p)
109 u32 tmplut[ARRAY_SIZE(inwordLut)];
110 if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
112 memcpy(inwordLut, tmplut, sizeof(inwordLut));
116 /* does screen address p correspond to character at LH/RH edge of screen? */
117 static inline int atedge(const int p, int size_row)
119 return (!(p % size_row) || !((p + 2) % size_row));
122 /* stores the char in UTF8 and returns the number of bytes used (1-4) */
123 static int store_utf8(u32 c, char *p)
129 } else if (c < 0x800) {
130 /* 110***** 10****** */
131 p[0] = 0xc0 | (c >> 6);
132 p[1] = 0x80 | (c & 0x3f);
134 } else if (c < 0x10000) {
135 /* 1110**** 10****** 10****** */
136 p[0] = 0xe0 | (c >> 12);
137 p[1] = 0x80 | ((c >> 6) & 0x3f);
138 p[2] = 0x80 | (c & 0x3f);
140 } else if (c < 0x110000) {
141 /* 11110*** 10****** 10****** 10****** */
142 p[0] = 0xf0 | (c >> 18);
143 p[1] = 0x80 | ((c >> 12) & 0x3f);
144 p[2] = 0x80 | ((c >> 6) & 0x3f);
145 p[3] = 0x80 | (c & 0x3f);
148 /* outside Unicode, replace with U+FFFD */
157 * set_selection - set the current selection.
158 * @sel: user selection info
159 * @tty: the console tty
161 * Invoked by the ioctl handle for the vt layer.
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
166 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
168 struct vc_data *vc = vc_cons[fg_console].d;
169 int new_sel_start, new_sel_end, spc;
170 struct tiocl_selection v;
172 int i, ps, pe, multiplier;
176 poke_blanked_console();
177 if (copy_from_user(&v, sel, sizeof(*sel)))
180 v.xs = min_t(u16, v.xs - 1, vc->vc_cols - 1);
181 v.ys = min_t(u16, v.ys - 1, vc->vc_rows - 1);
182 v.xe = min_t(u16, v.xe - 1, vc->vc_cols - 1);
183 v.ye = min_t(u16, v.ye - 1, vc->vc_rows - 1);
184 ps = v.ys * vc->vc_size_row + (v.xs << 1);
185 pe = v.ye * vc->vc_size_row + (v.xe << 1);
187 if (v.sel_mode == TIOCL_SELCLEAR) {
188 /* useful for screendump without selection highlights */
193 if (mouse_reporting() && (v.sel_mode & TIOCL_SELMOUSEREPORT)) {
194 mouse_report(tty, v.sel_mode & TIOCL_SELBUTTONMASK, v.xs, v.ys);
198 if (ps > pe) /* make sel_start <= sel_end */
201 if (sel_cons != vc_cons[fg_console].d) {
203 sel_cons = vc_cons[fg_console].d;
205 mode = vt_do_kdgkbmode(fg_console);
206 if (mode == K_UNICODE)
213 case TIOCL_SELCHAR: /* character-by-character selection */
217 case TIOCL_SELWORD: /* word-by-word selection */
218 spc = isspace(sel_pos(ps));
219 for (new_sel_start = ps; ; ps -= 2)
221 if ((spc && !isspace(sel_pos(ps))) ||
222 (!spc && !inword(sel_pos(ps))))
225 if (!(ps % vc->vc_size_row))
228 spc = isspace(sel_pos(pe));
229 for (new_sel_end = pe; ; pe += 2)
231 if ((spc && !isspace(sel_pos(pe))) ||
232 (!spc && !inword(sel_pos(pe))))
235 if (!((pe + 2) % vc->vc_size_row))
239 case TIOCL_SELLINE: /* line-by-line selection */
240 new_sel_start = ps - ps % vc->vc_size_row;
241 new_sel_end = pe + vc->vc_size_row
242 - pe % vc->vc_size_row - 2;
244 case TIOCL_SELPOINTER:
245 highlight_pointer(pe);
251 /* remove the pointer */
252 highlight_pointer(-1);
254 /* select to end of line if on trailing space */
255 if (new_sel_end > new_sel_start &&
256 !atedge(new_sel_end, vc->vc_size_row) &&
257 isspace(sel_pos(new_sel_end))) {
258 for (pe = new_sel_end + 2; ; pe += 2)
259 if (!isspace(sel_pos(pe)) ||
260 atedge(pe, vc->vc_size_row))
262 if (isspace(sel_pos(pe)))
265 if (sel_start == -1) /* no current selection */
266 highlight(new_sel_start, new_sel_end);
267 else if (new_sel_start == sel_start)
269 if (new_sel_end == sel_end) /* no action required */
271 else if (new_sel_end > sel_end) /* extend to right */
272 highlight(sel_end + 2, new_sel_end);
273 else /* contract from right */
274 highlight(new_sel_end + 2, sel_end);
276 else if (new_sel_end == sel_end)
278 if (new_sel_start < sel_start) /* extend to left */
279 highlight(new_sel_start, sel_start - 2);
280 else /* contract from left */
281 highlight(sel_start, new_sel_start - 2);
283 else /* some other case; start selection from scratch */
286 highlight(new_sel_start, new_sel_end);
288 sel_start = new_sel_start;
289 sel_end = new_sel_end;
291 /* Allocate a new buffer before freeing the old one ... */
292 multiplier = use_unicode ? 4 : 1; /* chars can take up to 4 bytes */
293 bp = kmalloc_array((sel_end - sel_start) / 2 + 1, multiplier,
296 printk(KERN_WARNING "selection: kmalloc() failed\n");
304 for (i = sel_start; i <= sel_end; i += 2) {
307 bp += store_utf8(c, bp);
312 if (! ((i + 2) % vc->vc_size_row)) {
313 /* strip trailing blanks from line and add newline,
314 unless non-space at end of line. */
322 sel_buffer_lth = bp - sel_buffer;
326 /* Insert the contents of the selection buffer into the
327 * queue of the tty associated with the current console.
328 * Invoked by ioctl().
330 * Locking: called without locks. Calls the ldisc wrongly with
333 int paste_selection(struct tty_struct *tty)
335 struct vc_data *vc = tty->driver_data;
338 struct tty_ldisc *ld;
339 DECLARE_WAITQUEUE(wait, current);
342 poke_blanked_console();
345 ld = tty_ldisc_ref_wait(tty);
347 return -EIO; /* ldisc was hung up */
348 tty_buffer_lock_exclusive(&vc->port);
350 add_wait_queue(&vc->paste_wait, &wait);
351 while (sel_buffer && sel_buffer_lth > pasted) {
352 set_current_state(TASK_INTERRUPTIBLE);
353 if (tty_throttled(tty)) {
357 __set_current_state(TASK_RUNNING);
358 count = sel_buffer_lth - pasted;
359 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
363 remove_wait_queue(&vc->paste_wait, &wait);
364 __set_current_state(TASK_RUNNING);
366 tty_buffer_unlock_exclusive(&vc->port);