xref: /openbmc/linux/drivers/tty/vt/selection.c (revision 555b4ef7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This module exports the functions:
4  *
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 *)'
11  *
12  * Now that /dev/vcs exists, most of this can disappear again.
13  */
14 
15 #include <linux/module.h>
16 #include <linux/tty.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 
23 #include <linux/uaccess.h>
24 
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>
32 
33 #include <linux/sched/signal.h>
34 
35 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
36 #define isspace(c)	((c) == ' ')
37 
38 extern void poke_blanked_console(void);
39 
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 volatile int sel_start = -1; 	/* cleared by clear_selection */
45 static int sel_end;
46 static int sel_buffer_lth;
47 static char *sel_buffer;
48 static DEFINE_MUTEX(sel_lock);
49 
50 /* clear_selection, highlight and highlight_pointer can be called
51    from interrupt (via scrollback/front) */
52 
53 /* set reverse video on characters s-e of console with selection. */
54 static inline void highlight(const int s, const int e)
55 {
56 	invert_screen(sel_cons, s, e-s+2, 1);
57 }
58 
59 /* use complementary color to show the pointer */
60 static inline void highlight_pointer(const int where)
61 {
62 	complement_pos(sel_cons, where);
63 }
64 
65 static u32
66 sel_pos(int n, bool unicode)
67 {
68 	if (unicode)
69 		return screen_glyph_unicode(sel_cons, n / 2);
70 	return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
71 				0);
72 }
73 
74 /**
75  *	clear_selection		-	remove current selection
76  *
77  *	Remove the current selection highlight, if any from the console
78  *	holding the selection. The caller must hold the console lock.
79  */
80 void clear_selection(void)
81 {
82 	highlight_pointer(-1); /* hide the pointer */
83 	if (sel_start != -1) {
84 		highlight(sel_start, sel_end);
85 		sel_start = -1;
86 	}
87 }
88 EXPORT_SYMBOL_GPL(clear_selection);
89 
90 bool vc_is_sel(struct vc_data *vc)
91 {
92 	return vc == sel_cons;
93 }
94 
95 /*
96  * User settable table: what characters are to be considered alphabetic?
97  * 128 bits. Locked by the console lock.
98  */
99 static u32 inwordLut[]={
100   0x00000000, /* control chars     */
101   0x03FFE000, /* digits and "-./"  */
102   0x87FFFFFE, /* uppercase and '_' */
103   0x07FFFFFE, /* lowercase         */
104 };
105 
106 static inline int inword(const u32 c)
107 {
108 	return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
109 }
110 
111 /**
112  *	set loadlut		-	load the LUT table
113  *	@p: user table
114  *
115  *	Load the LUT table from user space. The caller must hold the console
116  *	lock. Make a temporary copy so a partial update doesn't make a mess.
117  */
118 int sel_loadlut(char __user *p)
119 {
120 	u32 tmplut[ARRAY_SIZE(inwordLut)];
121 	if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
122 		return -EFAULT;
123 	memcpy(inwordLut, tmplut, sizeof(inwordLut));
124 	return 0;
125 }
126 
127 /* does screen address p correspond to character at LH/RH edge of screen? */
128 static inline int atedge(const int p, int size_row)
129 {
130 	return (!(p % size_row)	|| !((p + 2) % size_row));
131 }
132 
133 /* stores the char in UTF8 and returns the number of bytes used (1-4) */
134 static int store_utf8(u32 c, char *p)
135 {
136 	if (c < 0x80) {
137 		/*  0******* */
138 		p[0] = c;
139 		return 1;
140 	} else if (c < 0x800) {
141 		/* 110***** 10****** */
142 		p[0] = 0xc0 | (c >> 6);
143 		p[1] = 0x80 | (c & 0x3f);
144 		return 2;
145 	} else if (c < 0x10000) {
146 		/* 1110**** 10****** 10****** */
147 		p[0] = 0xe0 | (c >> 12);
148 		p[1] = 0x80 | ((c >> 6) & 0x3f);
149 		p[2] = 0x80 | (c & 0x3f);
150 		return 3;
151 	} else if (c < 0x110000) {
152 		/* 11110*** 10****** 10****** 10****** */
153 		p[0] = 0xf0 | (c >> 18);
154 		p[1] = 0x80 | ((c >> 12) & 0x3f);
155 		p[2] = 0x80 | ((c >> 6) & 0x3f);
156 		p[3] = 0x80 | (c & 0x3f);
157 		return 4;
158 	} else {
159 		/* outside Unicode, replace with U+FFFD */
160 		p[0] = 0xef;
161 		p[1] = 0xbf;
162 		p[2] = 0xbd;
163 		return 3;
164 	}
165 }
166 
167 /**
168  *	set_selection_user	-	set the current selection.
169  *	@sel: user selection info
170  *	@tty: the console tty
171  *
172  *	Invoked by the ioctl handle for the vt layer.
173  *
174  *	The entire selection process is managed under the console_lock. It's
175  *	 a lot under the lock but its hardly a performance path
176  */
177 int set_selection_user(const struct tiocl_selection __user *sel,
178 		       struct tty_struct *tty)
179 {
180 	struct tiocl_selection v;
181 
182 	if (copy_from_user(&v, sel, sizeof(*sel)))
183 		return -EFAULT;
184 
185 	return set_selection_kernel(&v, tty);
186 }
187 
188 int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
189 {
190 	struct vc_data *vc = vc_cons[fg_console].d;
191 	int new_sel_start, new_sel_end, spc;
192 	char *bp, *obp;
193 	int i, ps, pe;
194 	u32 c;
195 	int ret = 0;
196 	bool unicode;
197 
198 	poke_blanked_console();
199 
200 	v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
201 	v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
202 	v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
203 	v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
204 	ps = v->ys * vc->vc_size_row + (v->xs << 1);
205 	pe = v->ye * vc->vc_size_row + (v->xe << 1);
206 
207 	if (v->sel_mode == TIOCL_SELCLEAR) {
208 		/* useful for screendump without selection highlights */
209 		clear_selection();
210 		return 0;
211 	}
212 
213 	if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
214 		mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
215 			     v->ys);
216 		return 0;
217 	}
218 
219 	if (ps > pe)	/* make sel_start <= sel_end */
220 		swap(ps, pe);
221 
222 	mutex_lock(&sel_lock);
223 	if (sel_cons != vc_cons[fg_console].d) {
224 		clear_selection();
225 		sel_cons = vc_cons[fg_console].d;
226 	}
227 	unicode = vt_do_kdgkbmode(fg_console) == K_UNICODE;
228 
229 	switch (v->sel_mode)
230 	{
231 		case TIOCL_SELCHAR:	/* character-by-character selection */
232 			new_sel_start = ps;
233 			new_sel_end = pe;
234 			break;
235 		case TIOCL_SELWORD:	/* word-by-word selection */
236 			spc = isspace(sel_pos(ps, unicode));
237 			for (new_sel_start = ps; ; ps -= 2)
238 			{
239 				if ((spc && !isspace(sel_pos(ps, unicode))) ||
240 				    (!spc && !inword(sel_pos(ps, unicode))))
241 					break;
242 				new_sel_start = ps;
243 				if (!(ps % vc->vc_size_row))
244 					break;
245 			}
246 			spc = isspace(sel_pos(pe, unicode));
247 			for (new_sel_end = pe; ; pe += 2)
248 			{
249 				if ((spc && !isspace(sel_pos(pe, unicode))) ||
250 				    (!spc && !inword(sel_pos(pe, unicode))))
251 					break;
252 				new_sel_end = pe;
253 				if (!((pe + 2) % vc->vc_size_row))
254 					break;
255 			}
256 			break;
257 		case TIOCL_SELLINE:	/* line-by-line selection */
258 			new_sel_start = ps - ps % vc->vc_size_row;
259 			new_sel_end = pe + vc->vc_size_row
260 				    - pe % vc->vc_size_row - 2;
261 			break;
262 		case TIOCL_SELPOINTER:
263 			highlight_pointer(pe);
264 			goto unlock;
265 		default:
266 			ret = -EINVAL;
267 			goto unlock;
268 	}
269 
270 	/* remove the pointer */
271 	highlight_pointer(-1);
272 
273 	/* select to end of line if on trailing space */
274 	if (new_sel_end > new_sel_start &&
275 		!atedge(new_sel_end, vc->vc_size_row) &&
276 		isspace(sel_pos(new_sel_end, unicode))) {
277 		for (pe = new_sel_end + 2; ; pe += 2)
278 			if (!isspace(sel_pos(pe, unicode)) ||
279 			    atedge(pe, vc->vc_size_row))
280 				break;
281 		if (isspace(sel_pos(pe, unicode)))
282 			new_sel_end = pe;
283 	}
284 	if (sel_start == -1)	/* no current selection */
285 		highlight(new_sel_start, new_sel_end);
286 	else if (new_sel_start == sel_start)
287 	{
288 		if (new_sel_end == sel_end)	/* no action required */
289 			goto unlock;
290 		else if (new_sel_end > sel_end)	/* extend to right */
291 			highlight(sel_end + 2, new_sel_end);
292 		else				/* contract from right */
293 			highlight(new_sel_end + 2, sel_end);
294 	}
295 	else if (new_sel_end == sel_end)
296 	{
297 		if (new_sel_start < sel_start)	/* extend to left */
298 			highlight(new_sel_start, sel_start - 2);
299 		else				/* contract from left */
300 			highlight(sel_start, new_sel_start - 2);
301 	}
302 	else	/* some other case; start selection from scratch */
303 	{
304 		clear_selection();
305 		highlight(new_sel_start, new_sel_end);
306 	}
307 	sel_start = new_sel_start;
308 	sel_end = new_sel_end;
309 
310 	/* Allocate a new buffer before freeing the old one ... */
311 	/* chars can take up to 4 bytes with unicode */
312 	bp = kmalloc_array((sel_end - sel_start) / 2 + 1, unicode ? 4 : 1,
313 			   GFP_KERNEL);
314 	if (!bp) {
315 		printk(KERN_WARNING "selection: kmalloc() failed\n");
316 		clear_selection();
317 		ret = -ENOMEM;
318 		goto unlock;
319 	}
320 	kfree(sel_buffer);
321 	sel_buffer = bp;
322 
323 	obp = bp;
324 	for (i = sel_start; i <= sel_end; i += 2) {
325 		c = sel_pos(i, unicode);
326 		if (unicode)
327 			bp += store_utf8(c, bp);
328 		else
329 			*bp++ = c;
330 		if (!isspace(c))
331 			obp = bp;
332 		if (! ((i + 2) % vc->vc_size_row)) {
333 			/* strip trailing blanks from line and add newline,
334 			   unless non-space at end of line. */
335 			if (obp != bp) {
336 				bp = obp;
337 				*bp++ = '\r';
338 			}
339 			obp = bp;
340 		}
341 	}
342 	sel_buffer_lth = bp - sel_buffer;
343 unlock:
344 	mutex_unlock(&sel_lock);
345 	return ret;
346 }
347 EXPORT_SYMBOL_GPL(set_selection_kernel);
348 
349 /* Insert the contents of the selection buffer into the
350  * queue of the tty associated with the current console.
351  * Invoked by ioctl().
352  *
353  * Locking: called without locks. Calls the ldisc wrongly with
354  * unsafe methods,
355  */
356 int paste_selection(struct tty_struct *tty)
357 {
358 	struct vc_data *vc = tty->driver_data;
359 	int	pasted = 0;
360 	unsigned int count;
361 	struct  tty_ldisc *ld;
362 	DECLARE_WAITQUEUE(wait, current);
363 	int ret = 0;
364 
365 	console_lock();
366 	poke_blanked_console();
367 	console_unlock();
368 
369 	ld = tty_ldisc_ref_wait(tty);
370 	if (!ld)
371 		return -EIO;	/* ldisc was hung up */
372 	tty_buffer_lock_exclusive(&vc->port);
373 
374 	add_wait_queue(&vc->paste_wait, &wait);
375 	mutex_lock(&sel_lock);
376 	while (sel_buffer && sel_buffer_lth > pasted) {
377 		set_current_state(TASK_INTERRUPTIBLE);
378 		if (signal_pending(current)) {
379 			ret = -EINTR;
380 			break;
381 		}
382 		if (tty_throttled(tty)) {
383 			mutex_unlock(&sel_lock);
384 			schedule();
385 			mutex_lock(&sel_lock);
386 			continue;
387 		}
388 		__set_current_state(TASK_RUNNING);
389 		count = sel_buffer_lth - pasted;
390 		count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
391 					      count);
392 		pasted += count;
393 	}
394 	mutex_unlock(&sel_lock);
395 	remove_wait_queue(&vc->paste_wait, &wait);
396 	__set_current_state(TASK_RUNNING);
397 
398 	tty_buffer_unlock_exclusive(&vc->port);
399 	tty_ldisc_deref(ld);
400 	return ret;
401 }
402 EXPORT_SYMBOL_GPL(paste_selection);
403