]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/tty/vt/selection.c
Merge branch 'ida-4.19' of git://git.infradead.org/users/willy/linux-dax
[mirror_ubuntu-jammy-kernel.git] / drivers / tty / vt / selection.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This module exports the functions:
4 *
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 *)'
9 *
10 * Now that /dev/vcs exists, most of this can disappear again.
11 */
12
13 #include <linux/module.h>
14 #include <linux/tty.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19
20 #include <linux/uaccess.h>
21
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>
29
30 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
31 #define isspace(c) ((c) == ' ')
32
33 extern void poke_blanked_console(void);
34
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 */
41 static int sel_end;
42 static int sel_buffer_lth;
43 static char *sel_buffer;
44
45 /* clear_selection, highlight and highlight_pointer can be called
46 from interrupt (via scrollback/front) */
47
48 /* set reverse video on characters s-e of console with selection. */
49 static inline void highlight(const int s, const int e)
50 {
51 invert_screen(sel_cons, s, e-s+2, 1);
52 }
53
54 /* use complementary color to show the pointer */
55 static inline void highlight_pointer(const int where)
56 {
57 complement_pos(sel_cons, where);
58 }
59
60 static u32
61 sel_pos(int n)
62 {
63 if (use_unicode)
64 return screen_glyph_unicode(sel_cons, n / 2);
65 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
66 0);
67 }
68
69 /**
70 * clear_selection - remove current selection
71 *
72 * Remove the current selection highlight, if any from the console
73 * holding the selection. The caller must hold the console lock.
74 */
75 void clear_selection(void)
76 {
77 highlight_pointer(-1); /* hide the pointer */
78 if (sel_start != -1) {
79 highlight(sel_start, sel_end);
80 sel_start = -1;
81 }
82 }
83
84 /*
85 * User settable table: what characters are to be considered alphabetic?
86 * 128 bits. Locked by the console lock.
87 */
88 static u32 inwordLut[]={
89 0x00000000, /* control chars */
90 0x03FFE000, /* digits and "-./" */
91 0x87FFFFFE, /* uppercase and '_' */
92 0x07FFFFFE, /* lowercase */
93 };
94
95 static inline int inword(const u32 c)
96 {
97 return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
98 }
99
100 /**
101 * set loadlut - load the LUT table
102 * @p: user table
103 *
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.
106 */
107 int sel_loadlut(char __user *p)
108 {
109 u32 tmplut[ARRAY_SIZE(inwordLut)];
110 if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
111 return -EFAULT;
112 memcpy(inwordLut, tmplut, sizeof(inwordLut));
113 return 0;
114 }
115
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)
118 {
119 return (!(p % size_row) || !((p + 2) % size_row));
120 }
121
122 /* stores the char in UTF8 and returns the number of bytes used (1-4) */
123 static int store_utf8(u32 c, char *p)
124 {
125 if (c < 0x80) {
126 /* 0******* */
127 p[0] = c;
128 return 1;
129 } else if (c < 0x800) {
130 /* 110***** 10****** */
131 p[0] = 0xc0 | (c >> 6);
132 p[1] = 0x80 | (c & 0x3f);
133 return 2;
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);
139 return 3;
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);
146 return 4;
147 } else {
148 /* outside Unicode, replace with U+FFFD */
149 p[0] = 0xef;
150 p[1] = 0xbf;
151 p[2] = 0xbd;
152 return 3;
153 }
154 }
155
156 /**
157 * set_selection - set the current selection.
158 * @sel: user selection info
159 * @tty: the console tty
160 *
161 * Invoked by the ioctl handle for the vt layer.
162 *
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
165 */
166 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
167 {
168 struct vc_data *vc = vc_cons[fg_console].d;
169 int new_sel_start, new_sel_end, spc;
170 struct tiocl_selection v;
171 char *bp, *obp;
172 int i, ps, pe, multiplier;
173 u32 c;
174 int mode;
175
176 poke_blanked_console();
177 if (copy_from_user(&v, sel, sizeof(*sel)))
178 return -EFAULT;
179
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);
186
187 if (v.sel_mode == TIOCL_SELCLEAR) {
188 /* useful for screendump without selection highlights */
189 clear_selection();
190 return 0;
191 }
192
193 if (mouse_reporting() && (v.sel_mode & TIOCL_SELMOUSEREPORT)) {
194 mouse_report(tty, v.sel_mode & TIOCL_SELBUTTONMASK, v.xs, v.ys);
195 return 0;
196 }
197
198 if (ps > pe) /* make sel_start <= sel_end */
199 swap(ps, pe);
200
201 if (sel_cons != vc_cons[fg_console].d) {
202 clear_selection();
203 sel_cons = vc_cons[fg_console].d;
204 }
205 mode = vt_do_kdgkbmode(fg_console);
206 if (mode == K_UNICODE)
207 use_unicode = 1;
208 else
209 use_unicode = 0;
210
211 switch (v.sel_mode)
212 {
213 case TIOCL_SELCHAR: /* character-by-character selection */
214 new_sel_start = ps;
215 new_sel_end = pe;
216 break;
217 case TIOCL_SELWORD: /* word-by-word selection */
218 spc = isspace(sel_pos(ps));
219 for (new_sel_start = ps; ; ps -= 2)
220 {
221 if ((spc && !isspace(sel_pos(ps))) ||
222 (!spc && !inword(sel_pos(ps))))
223 break;
224 new_sel_start = ps;
225 if (!(ps % vc->vc_size_row))
226 break;
227 }
228 spc = isspace(sel_pos(pe));
229 for (new_sel_end = pe; ; pe += 2)
230 {
231 if ((spc && !isspace(sel_pos(pe))) ||
232 (!spc && !inword(sel_pos(pe))))
233 break;
234 new_sel_end = pe;
235 if (!((pe + 2) % vc->vc_size_row))
236 break;
237 }
238 break;
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;
243 break;
244 case TIOCL_SELPOINTER:
245 highlight_pointer(pe);
246 return 0;
247 default:
248 return -EINVAL;
249 }
250
251 /* remove the pointer */
252 highlight_pointer(-1);
253
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))
261 break;
262 if (isspace(sel_pos(pe)))
263 new_sel_end = pe;
264 }
265 if (sel_start == -1) /* no current selection */
266 highlight(new_sel_start, new_sel_end);
267 else if (new_sel_start == sel_start)
268 {
269 if (new_sel_end == sel_end) /* no action required */
270 return 0;
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);
275 }
276 else if (new_sel_end == sel_end)
277 {
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);
282 }
283 else /* some other case; start selection from scratch */
284 {
285 clear_selection();
286 highlight(new_sel_start, new_sel_end);
287 }
288 sel_start = new_sel_start;
289 sel_end = new_sel_end;
290
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,
294 GFP_KERNEL);
295 if (!bp) {
296 printk(KERN_WARNING "selection: kmalloc() failed\n");
297 clear_selection();
298 return -ENOMEM;
299 }
300 kfree(sel_buffer);
301 sel_buffer = bp;
302
303 obp = bp;
304 for (i = sel_start; i <= sel_end; i += 2) {
305 c = sel_pos(i);
306 if (use_unicode)
307 bp += store_utf8(c, bp);
308 else
309 *bp++ = c;
310 if (!isspace(c))
311 obp = 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. */
315 if (obp != bp) {
316 bp = obp;
317 *bp++ = '\r';
318 }
319 obp = bp;
320 }
321 }
322 sel_buffer_lth = bp - sel_buffer;
323 return 0;
324 }
325
326 /* Insert the contents of the selection buffer into the
327 * queue of the tty associated with the current console.
328 * Invoked by ioctl().
329 *
330 * Locking: called without locks. Calls the ldisc wrongly with
331 * unsafe methods,
332 */
333 int paste_selection(struct tty_struct *tty)
334 {
335 struct vc_data *vc = tty->driver_data;
336 int pasted = 0;
337 unsigned int count;
338 struct tty_ldisc *ld;
339 DECLARE_WAITQUEUE(wait, current);
340
341 console_lock();
342 poke_blanked_console();
343 console_unlock();
344
345 ld = tty_ldisc_ref_wait(tty);
346 if (!ld)
347 return -EIO; /* ldisc was hung up */
348 tty_buffer_lock_exclusive(&vc->port);
349
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)) {
354 schedule();
355 continue;
356 }
357 __set_current_state(TASK_RUNNING);
358 count = sel_buffer_lth - pasted;
359 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
360 count);
361 pasted += count;
362 }
363 remove_wait_queue(&vc->paste_wait, &wait);
364 __set_current_state(TASK_RUNNING);
365
366 tty_buffer_unlock_exclusive(&vc->port);
367 tty_ldisc_deref(ld);
368 return 0;
369 }