8fe5f46ff7bbb6a0dd18e2e3c094567f817e2694
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20
21 #include <linux/uaccess.h>
22
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>
30
31 #include <linux/sched/signal.h>
32
33 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
34 #define isspace(c) ((c) == ' ')
35
36 extern void poke_blanked_console(void);
37
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 */
44 static int sel_end;
45 static int sel_buffer_lth;
46 static char *sel_buffer;
47 static DEFINE_MUTEX(sel_lock);
48
49 /* clear_selection, highlight and highlight_pointer can be called
50 from interrupt (via scrollback/front) */
51
52 /* set reverse video on characters s-e of console with selection. */
53 static inline void highlight(const int s, const int e)
54 {
55 invert_screen(sel_cons, s, e-s+2, 1);
56 }
57
58 /* use complementary color to show the pointer */
59 static inline void highlight_pointer(const int where)
60 {
61 complement_pos(sel_cons, where);
62 }
63
64 static u16
65 sel_pos(int n)
66 {
67 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
68 use_unicode);
69 }
70
71 /**
72 * clear_selection - remove current selection
73 *
74 * Remove the current selection highlight, if any from the console
75 * holding the selection. The caller must hold the console lock.
76 */
77 void clear_selection(void)
78 {
79 highlight_pointer(-1); /* hide the pointer */
80 if (sel_start != -1) {
81 highlight(sel_start, sel_end);
82 sel_start = -1;
83 }
84 }
85
86 /*
87 * User settable table: what characters are to be considered alphabetic?
88 * 128 bits. Locked by the console lock.
89 */
90 static u32 inwordLut[]={
91 0x00000000, /* control chars */
92 0x03FFE000, /* digits and "-./" */
93 0x87FFFFFE, /* uppercase and '_' */
94 0x07FFFFFE, /* lowercase */
95 };
96
97 static inline int inword(const u16 c) {
98 return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
99 }
100
101 /**
102 * set loadlut - load the LUT table
103 * @p: user table
104 *
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.
107 */
108 int sel_loadlut(char __user *p)
109 {
110 u32 tmplut[ARRAY_SIZE(inwordLut)];
111 if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
112 return -EFAULT;
113 memcpy(inwordLut, tmplut, sizeof(inwordLut));
114 return 0;
115 }
116
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)
119 {
120 return (!(p % size_row) || !((p + 2) % size_row));
121 }
122
123 /* constrain v such that v <= u */
124 static inline unsigned short limit(const unsigned short v, const unsigned short u)
125 {
126 return (v > u) ? u : v;
127 }
128
129 /* stores the char in UTF8 and returns the number of bytes used (1-3) */
130 static int store_utf8(u16 c, char *p)
131 {
132 if (c < 0x80) {
133 /* 0******* */
134 p[0] = c;
135 return 1;
136 } else if (c < 0x800) {
137 /* 110***** 10****** */
138 p[0] = 0xc0 | (c >> 6);
139 p[1] = 0x80 | (c & 0x3f);
140 return 2;
141 } else {
142 /* 1110**** 10****** 10****** */
143 p[0] = 0xe0 | (c >> 12);
144 p[1] = 0x80 | ((c >> 6) & 0x3f);
145 p[2] = 0x80 | (c & 0x3f);
146 return 3;
147 }
148 }
149
150 /**
151 * set_selection - set the current selection.
152 * @sel: user selection info
153 * @tty: the console tty
154 *
155 * Invoked by the ioctl handle for the vt layer.
156 *
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
159 */
160 static int __set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
161 {
162 struct vc_data *vc = vc_cons[fg_console].d;
163 int sel_mode, new_sel_start, new_sel_end, spc;
164 char *bp, *obp;
165 int i, ps, pe, multiplier;
166 u16 c;
167 int mode, ret = 0;
168
169 poke_blanked_console();
170
171 { unsigned short xs, ys, xe, ye;
172
173 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
174 return -EFAULT;
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);
187
188 if (sel_mode == TIOCL_SELCLEAR) {
189 /* useful for screendump without selection highlights */
190 clear_selection();
191 return 0;
192 }
193
194 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
195 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
196 return 0;
197 }
198 }
199
200 if (ps > pe) /* make sel_start <= sel_end */
201 {
202 int tmp = ps;
203 ps = pe;
204 pe = tmp;
205 }
206
207 mutex_lock(&sel_lock);
208 if (sel_cons != vc_cons[fg_console].d) {
209 clear_selection();
210 sel_cons = vc_cons[fg_console].d;
211 }
212 mode = vt_do_kdgkbmode(fg_console);
213 if (mode == K_UNICODE)
214 use_unicode = 1;
215 else
216 use_unicode = 0;
217
218 switch (sel_mode)
219 {
220 case TIOCL_SELCHAR: /* character-by-character selection */
221 new_sel_start = ps;
222 new_sel_end = pe;
223 break;
224 case TIOCL_SELWORD: /* word-by-word selection */
225 spc = isspace(sel_pos(ps));
226 for (new_sel_start = ps; ; ps -= 2)
227 {
228 if ((spc && !isspace(sel_pos(ps))) ||
229 (!spc && !inword(sel_pos(ps))))
230 break;
231 new_sel_start = ps;
232 if (!(ps % vc->vc_size_row))
233 break;
234 }
235 spc = isspace(sel_pos(pe));
236 for (new_sel_end = pe; ; pe += 2)
237 {
238 if ((spc && !isspace(sel_pos(pe))) ||
239 (!spc && !inword(sel_pos(pe))))
240 break;
241 new_sel_end = pe;
242 if (!((pe + 2) % vc->vc_size_row))
243 break;
244 }
245 break;
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;
250 break;
251 case TIOCL_SELPOINTER:
252 highlight_pointer(pe);
253 goto unlock;
254 default:
255 ret = -EINVAL;
256 goto unlock;
257 }
258
259 /* remove the pointer */
260 highlight_pointer(-1);
261
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))
269 break;
270 if (isspace(sel_pos(pe)))
271 new_sel_end = pe;
272 }
273 if (sel_start == -1) /* no current selection */
274 highlight(new_sel_start, new_sel_end);
275 else if (new_sel_start == sel_start)
276 {
277 if (new_sel_end == sel_end) /* no action required */
278 goto unlock;
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);
283 }
284 else if (new_sel_end == sel_end)
285 {
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);
290 }
291 else /* some other case; start selection from scratch */
292 {
293 clear_selection();
294 highlight(new_sel_start, new_sel_end);
295 }
296 sel_start = new_sel_start;
297 sel_end = new_sel_end;
298
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);
302 if (!bp) {
303 printk(KERN_WARNING "selection: kmalloc() failed\n");
304 clear_selection();
305 ret = -ENOMEM;
306 goto unlock;
307 }
308 kfree(sel_buffer);
309 sel_buffer = bp;
310
311 obp = bp;
312 for (i = sel_start; i <= sel_end; i += 2) {
313 c = sel_pos(i);
314 if (use_unicode)
315 bp += store_utf8(c, bp);
316 else
317 *bp++ = c;
318 if (!isspace(c))
319 obp = 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. */
323 if (obp != bp) {
324 bp = obp;
325 *bp++ = '\r';
326 }
327 obp = bp;
328 }
329 }
330 sel_buffer_lth = bp - sel_buffer;
331 unlock:
332 mutex_unlock(&sel_lock);
333 return ret;
334 }
335
336 int set_selection(const struct tiocl_selection __user *v, struct tty_struct *tty)
337 {
338 int ret;
339
340 console_lock();
341 ret = __set_selection(v, tty);
342 console_unlock();
343
344 return ret;
345 }
346
347 /* Insert the contents of the selection buffer into the
348 * queue of the tty associated with the current console.
349 * Invoked by ioctl().
350 *
351 * Locking: called without locks. Calls the ldisc wrongly with
352 * unsafe methods,
353 */
354 int paste_selection(struct tty_struct *tty)
355 {
356 struct vc_data *vc = tty->driver_data;
357 int pasted = 0;
358 unsigned int count;
359 struct tty_ldisc *ld;
360 DECLARE_WAITQUEUE(wait, current);
361 int ret = 0;
362
363 console_lock();
364 poke_blanked_console();
365 console_unlock();
366
367 ld = tty_ldisc_ref_wait(tty);
368 if (!ld)
369 return -EIO; /* ldisc was hung up */
370 tty_buffer_lock_exclusive(&vc->port);
371
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)) {
377 ret = -EINTR;
378 break;
379 }
380 if (tty_throttled(tty)) {
381 mutex_unlock(&sel_lock);
382 schedule();
383 mutex_lock(&sel_lock);
384 continue;
385 }
386 __set_current_state(TASK_RUNNING);
387 count = sel_buffer_lth - pasted;
388 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
389 count);
390 pasted += count;
391 }
392 mutex_unlock(&sel_lock);
393 remove_wait_queue(&vc->paste_wait, &wait);
394 __set_current_state(TASK_RUNNING);
395
396 tty_buffer_unlock_exclusive(&vc->port);
397 tty_ldisc_deref(ld);
398 return ret;
399 }