8fe5f46ff7bbb6a0dd18e2e3c094567f817e2694
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
);
67 return inverse_translate(sel_cons
, screen_glyph(sel_cons
, n
),
72 * clear_selection - remove current selection
74 * Remove the current selection highlight, if any from the console
75 * holding the selection. The caller must hold the console lock.
77 void clear_selection(void)
79 highlight_pointer(-1); /* hide the pointer */
80 if (sel_start
!= -1) {
81 highlight(sel_start
, sel_end
);
87 * User settable table: what characters are to be considered alphabetic?
88 * 128 bits. Locked by the console lock.
90 static u32 inwordLut
[]={
91 0x00000000, /* control chars */
92 0x03FFE000, /* digits and "-./" */
93 0x87FFFFFE, /* uppercase and '_' */
94 0x07FFFFFE, /* lowercase */
97 static inline int inword(const u16 c
) {
98 return c
> 0x7f || (( inwordLut
[c
>>5] >> (c
& 0x1F) ) & 1);
102 * set loadlut - load the LUT table
105 * Load the LUT table from user space. The caller must hold the console
106 * lock. Make a temporary copy so a partial update doesn't make a mess.
108 int sel_loadlut(char __user
*p
)
110 u32 tmplut
[ARRAY_SIZE(inwordLut
)];
111 if (copy_from_user(tmplut
, (u32 __user
*)(p
+4), sizeof(inwordLut
)))
113 memcpy(inwordLut
, tmplut
, sizeof(inwordLut
));
117 /* does screen address p correspond to character at LH/RH edge of screen? */
118 static inline int atedge(const int p
, int size_row
)
120 return (!(p
% size_row
) || !((p
+ 2) % size_row
));
123 /* constrain v such that v <= u */
124 static inline unsigned short limit(const unsigned short v
, const unsigned short u
)
126 return (v
> u
) ? u
: v
;
129 /* stores the char in UTF8 and returns the number of bytes used (1-3) */
130 static int store_utf8(u16 c
, char *p
)
136 } else if (c
< 0x800) {
137 /* 110***** 10****** */
138 p
[0] = 0xc0 | (c
>> 6);
139 p
[1] = 0x80 | (c
& 0x3f);
142 /* 1110**** 10****** 10****** */
143 p
[0] = 0xe0 | (c
>> 12);
144 p
[1] = 0x80 | ((c
>> 6) & 0x3f);
145 p
[2] = 0x80 | (c
& 0x3f);
151 * set_selection - set the current selection.
152 * @sel: user selection info
153 * @tty: the console tty
155 * Invoked by the ioctl handle for the vt layer.
157 * The entire selection process is managed under the console_lock. It's
158 * a lot under the lock but its hardly a performance path
160 static int __set_selection(const struct tiocl_selection __user
*sel
, struct tty_struct
*tty
)
162 struct vc_data
*vc
= vc_cons
[fg_console
].d
;
163 int sel_mode
, new_sel_start
, new_sel_end
, spc
;
165 int i
, ps
, pe
, multiplier
;
169 poke_blanked_console();
171 { unsigned short xs
, ys
, xe
, ye
;
173 if (!access_ok(VERIFY_READ
, sel
, sizeof(*sel
)))
175 __get_user(xs
, &sel
->xs
);
176 __get_user(ys
, &sel
->ys
);
177 __get_user(xe
, &sel
->xe
);
178 __get_user(ye
, &sel
->ye
);
179 __get_user(sel_mode
, &sel
->sel_mode
);
180 xs
--; ys
--; xe
--; ye
--;
181 xs
= limit(xs
, vc
->vc_cols
- 1);
182 ys
= limit(ys
, vc
->vc_rows
- 1);
183 xe
= limit(xe
, vc
->vc_cols
- 1);
184 ye
= limit(ye
, vc
->vc_rows
- 1);
185 ps
= ys
* vc
->vc_size_row
+ (xs
<< 1);
186 pe
= ye
* vc
->vc_size_row
+ (xe
<< 1);
188 if (sel_mode
== TIOCL_SELCLEAR
) {
189 /* useful for screendump without selection highlights */
194 if (mouse_reporting() && (sel_mode
& TIOCL_SELMOUSEREPORT
)) {
195 mouse_report(tty
, sel_mode
& TIOCL_SELBUTTONMASK
, xs
, ys
);
200 if (ps
> pe
) /* make sel_start <= sel_end */
207 mutex_lock(&sel_lock
);
208 if (sel_cons
!= vc_cons
[fg_console
].d
) {
210 sel_cons
= vc_cons
[fg_console
].d
;
212 mode
= vt_do_kdgkbmode(fg_console
);
213 if (mode
== K_UNICODE
)
220 case TIOCL_SELCHAR
: /* character-by-character selection */
224 case TIOCL_SELWORD
: /* word-by-word selection */
225 spc
= isspace(sel_pos(ps
));
226 for (new_sel_start
= ps
; ; ps
-= 2)
228 if ((spc
&& !isspace(sel_pos(ps
))) ||
229 (!spc
&& !inword(sel_pos(ps
))))
232 if (!(ps
% vc
->vc_size_row
))
235 spc
= isspace(sel_pos(pe
));
236 for (new_sel_end
= pe
; ; pe
+= 2)
238 if ((spc
&& !isspace(sel_pos(pe
))) ||
239 (!spc
&& !inword(sel_pos(pe
))))
242 if (!((pe
+ 2) % vc
->vc_size_row
))
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;
251 case TIOCL_SELPOINTER
:
252 highlight_pointer(pe
);
259 /* remove the pointer */
260 highlight_pointer(-1);
262 /* select to end of line if on trailing space */
263 if (new_sel_end
> new_sel_start
&&
264 !atedge(new_sel_end
, vc
->vc_size_row
) &&
265 isspace(sel_pos(new_sel_end
))) {
266 for (pe
= new_sel_end
+ 2; ; pe
+= 2)
267 if (!isspace(sel_pos(pe
)) ||
268 atedge(pe
, vc
->vc_size_row
))
270 if (isspace(sel_pos(pe
)))
273 if (sel_start
== -1) /* no current selection */
274 highlight(new_sel_start
, new_sel_end
);
275 else if (new_sel_start
== sel_start
)
277 if (new_sel_end
== sel_end
) /* no action required */
279 else if (new_sel_end
> sel_end
) /* extend to right */
280 highlight(sel_end
+ 2, new_sel_end
);
281 else /* contract from right */
282 highlight(new_sel_end
+ 2, sel_end
);
284 else if (new_sel_end
== sel_end
)
286 if (new_sel_start
< sel_start
) /* extend to left */
287 highlight(new_sel_start
, sel_start
- 2);
288 else /* contract from left */
289 highlight(sel_start
, new_sel_start
- 2);
291 else /* some other case; start selection from scratch */
294 highlight(new_sel_start
, new_sel_end
);
296 sel_start
= new_sel_start
;
297 sel_end
= new_sel_end
;
299 /* Allocate a new buffer before freeing the old one ... */
300 multiplier
= use_unicode
? 3 : 1; /* chars can take up to 3 bytes */
301 bp
= kmalloc(((sel_end
-sel_start
)/2+1)*multiplier
, GFP_KERNEL
);
303 printk(KERN_WARNING
"selection: kmalloc() failed\n");
312 for (i
= sel_start
; i
<= sel_end
; i
+= 2) {
315 bp
+= store_utf8(c
, bp
);
320 if (! ((i
+ 2) % vc
->vc_size_row
)) {
321 /* strip trailing blanks from line and add newline,
322 unless non-space at end of line. */
330 sel_buffer_lth
= bp
- sel_buffer
;
332 mutex_unlock(&sel_lock
);
336 int set_selection(const struct tiocl_selection __user
*v
, struct tty_struct
*tty
)
341 ret
= __set_selection(v
, tty
);
347 /* Insert the contents of the selection buffer into the
348 * queue of the tty associated with the current console.
349 * Invoked by ioctl().
351 * Locking: called without locks. Calls the ldisc wrongly with
354 int paste_selection(struct tty_struct
*tty
)
356 struct vc_data
*vc
= tty
->driver_data
;
359 struct tty_ldisc
*ld
;
360 DECLARE_WAITQUEUE(wait
, current
);
364 poke_blanked_console();
367 ld
= tty_ldisc_ref_wait(tty
);
369 return -EIO
; /* ldisc was hung up */
370 tty_buffer_lock_exclusive(&vc
->port
);
372 add_wait_queue(&vc
->paste_wait
, &wait
);
373 mutex_lock(&sel_lock
);
374 while (sel_buffer
&& sel_buffer_lth
> pasted
) {
375 set_current_state(TASK_INTERRUPTIBLE
);
376 if (signal_pending(current
)) {
380 if (tty_throttled(tty
)) {
381 mutex_unlock(&sel_lock
);
383 mutex_lock(&sel_lock
);
386 __set_current_state(TASK_RUNNING
);
387 count
= sel_buffer_lth
- pasted
;
388 count
= tty_ldisc_receive_buf(ld
, sel_buffer
+ pasted
, NULL
,
392 mutex_unlock(&sel_lock
);
393 remove_wait_queue(&vc
->paste_wait
, &wait
);
394 __set_current_state(TASK_RUNNING
);
396 tty_buffer_unlock_exclusive(&vc
->port
);