xref: /openbmc/linux/drivers/tty/vt/selection.c (revision 9256d09f1da178f67d37b054cb4aaadf816bf05b)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
260d4ae8dSGreg Kroah-Hartman /*
360d4ae8dSGreg Kroah-Hartman  * This module exports the functions:
460d4ae8dSGreg Kroah-Hartman  *
5496124e5SOkash Khawaja  *     'int set_selection_user(struct tiocl_selection __user *,
6496124e5SOkash Khawaja  *			       struct tty_struct *)'
7496124e5SOkash Khawaja  *     'int set_selection_kernel(struct tiocl_selection *, struct tty_struct *)'
860d4ae8dSGreg Kroah-Hartman  *     'void clear_selection(void)'
960d4ae8dSGreg Kroah-Hartman  *     'int paste_selection(struct tty_struct *)'
1060d4ae8dSGreg Kroah-Hartman  *     'int sel_loadlut(char __user *)'
1160d4ae8dSGreg Kroah-Hartman  *
1260d4ae8dSGreg Kroah-Hartman  * Now that /dev/vcs exists, most of this can disappear again.
1360d4ae8dSGreg Kroah-Hartman  */
1460d4ae8dSGreg Kroah-Hartman 
1560d4ae8dSGreg Kroah-Hartman #include <linux/module.h>
1660d4ae8dSGreg Kroah-Hartman #include <linux/tty.h>
1760d4ae8dSGreg Kroah-Hartman #include <linux/sched.h>
1860d4ae8dSGreg Kroah-Hartman #include <linux/mm.h>
1907e6124aSJiri Slaby #include <linux/mutex.h>
2060d4ae8dSGreg Kroah-Hartman #include <linux/slab.h>
2160d4ae8dSGreg Kroah-Hartman #include <linux/types.h>
2260d4ae8dSGreg Kroah-Hartman 
237c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
2460d4ae8dSGreg Kroah-Hartman 
2560d4ae8dSGreg Kroah-Hartman #include <linux/kbd_kern.h>
2660d4ae8dSGreg Kroah-Hartman #include <linux/vt_kern.h>
2760d4ae8dSGreg Kroah-Hartman #include <linux/consolemap.h>
2860d4ae8dSGreg Kroah-Hartman #include <linux/selection.h>
2960d4ae8dSGreg Kroah-Hartman #include <linux/tiocl.h>
3060d4ae8dSGreg Kroah-Hartman #include <linux/console.h>
31a7c8d58cSPeter Hurley #include <linux/tty_flip.h>
3260d4ae8dSGreg Kroah-Hartman 
33687bff0cSJiri Slaby #include <linux/sched/signal.h>
34687bff0cSJiri Slaby 
3560d4ae8dSGreg Kroah-Hartman /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
3660d4ae8dSGreg Kroah-Hartman #define isspace(c)	((c) == ' ')
3760d4ae8dSGreg Kroah-Hartman 
3860d4ae8dSGreg Kroah-Hartman extern void poke_blanked_console(void);
3960d4ae8dSGreg Kroah-Hartman 
40079c9534SAlan Cox /* FIXME: all this needs locking */
41*9256d09fSJiri Slaby static struct vc_selection {
42*9256d09fSJiri Slaby 	struct mutex lock;
43*9256d09fSJiri Slaby 	struct vc_data *cons;			/* must not be deallocated */
44*9256d09fSJiri Slaby 	char *buffer;
45*9256d09fSJiri Slaby 	unsigned int buf_len;
46*9256d09fSJiri Slaby 	volatile int start;			/* cleared by clear_selection */
47*9256d09fSJiri Slaby 	int end;
48*9256d09fSJiri Slaby } vc_sel = {
49*9256d09fSJiri Slaby 	.lock = __MUTEX_INITIALIZER(vc_sel.lock),
50*9256d09fSJiri Slaby 	.start = -1,
51*9256d09fSJiri Slaby };
5260d4ae8dSGreg Kroah-Hartman 
5360d4ae8dSGreg Kroah-Hartman /* clear_selection, highlight and highlight_pointer can be called
5460d4ae8dSGreg Kroah-Hartman    from interrupt (via scrollback/front) */
5560d4ae8dSGreg Kroah-Hartman 
5660d4ae8dSGreg Kroah-Hartman /* set reverse video on characters s-e of console with selection. */
5760d4ae8dSGreg Kroah-Hartman static inline void highlight(const int s, const int e)
5860d4ae8dSGreg Kroah-Hartman {
59*9256d09fSJiri Slaby 	invert_screen(vc_sel.cons, s, e-s+2, 1);
6060d4ae8dSGreg Kroah-Hartman }
6160d4ae8dSGreg Kroah-Hartman 
6260d4ae8dSGreg Kroah-Hartman /* use complementary color to show the pointer */
6360d4ae8dSGreg Kroah-Hartman static inline void highlight_pointer(const int where)
6460d4ae8dSGreg Kroah-Hartman {
65*9256d09fSJiri Slaby 	complement_pos(vc_sel.cons, where);
6660d4ae8dSGreg Kroah-Hartman }
6760d4ae8dSGreg Kroah-Hartman 
689bfdc261SAdam Borowski static u32
69555b4ef7SJiri Slaby sel_pos(int n, bool unicode)
7060d4ae8dSGreg Kroah-Hartman {
71555b4ef7SJiri Slaby 	if (unicode)
72*9256d09fSJiri Slaby 		return screen_glyph_unicode(vc_sel.cons, n / 2);
73*9256d09fSJiri Slaby 	return inverse_translate(vc_sel.cons, screen_glyph(vc_sel.cons, n), 0);
7460d4ae8dSGreg Kroah-Hartman }
7560d4ae8dSGreg Kroah-Hartman 
765289475dSAlan Cox /**
775289475dSAlan Cox  *	clear_selection		-	remove current selection
785289475dSAlan Cox  *
795289475dSAlan Cox  *	Remove the current selection highlight, if any from the console
805289475dSAlan Cox  *	holding the selection. The caller must hold the console lock.
815289475dSAlan Cox  */
825289475dSAlan Cox void clear_selection(void)
835289475dSAlan Cox {
8460d4ae8dSGreg Kroah-Hartman 	highlight_pointer(-1); /* hide the pointer */
85*9256d09fSJiri Slaby 	if (vc_sel.start != -1) {
86*9256d09fSJiri Slaby 		highlight(vc_sel.start, vc_sel.end);
87*9256d09fSJiri Slaby 		vc_sel.start = -1;
8860d4ae8dSGreg Kroah-Hartman 	}
8960d4ae8dSGreg Kroah-Hartman }
90496124e5SOkash Khawaja EXPORT_SYMBOL_GPL(clear_selection);
9160d4ae8dSGreg Kroah-Hartman 
92dce05aa6SJiri Slaby bool vc_is_sel(struct vc_data *vc)
93dce05aa6SJiri Slaby {
94*9256d09fSJiri Slaby 	return vc == vc_sel.cons;
95dce05aa6SJiri Slaby }
96dce05aa6SJiri Slaby 
9760d4ae8dSGreg Kroah-Hartman /*
9860d4ae8dSGreg Kroah-Hartman  * User settable table: what characters are to be considered alphabetic?
997f1534e1SAdam Borowski  * 128 bits. Locked by the console lock.
10060d4ae8dSGreg Kroah-Hartman  */
1017f1534e1SAdam Borowski static u32 inwordLut[]={
10260d4ae8dSGreg Kroah-Hartman   0x00000000, /* control chars     */
1037d6d44aeSAdam Borowski   0x03FFE000, /* digits and "-./"  */
10460d4ae8dSGreg Kroah-Hartman   0x87FFFFFE, /* uppercase and '_' */
10560d4ae8dSGreg Kroah-Hartman   0x07FFFFFE, /* lowercase         */
10660d4ae8dSGreg Kroah-Hartman };
10760d4ae8dSGreg Kroah-Hartman 
1089bfdc261SAdam Borowski static inline int inword(const u32 c)
1099bfdc261SAdam Borowski {
1107f1534e1SAdam Borowski 	return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
11160d4ae8dSGreg Kroah-Hartman }
11260d4ae8dSGreg Kroah-Hartman 
1135289475dSAlan Cox /**
1145289475dSAlan Cox  *	set loadlut		-	load the LUT table
1155289475dSAlan Cox  *	@p: user table
1165289475dSAlan Cox  *
1175289475dSAlan Cox  *	Load the LUT table from user space. The caller must hold the console
1185289475dSAlan Cox  *	lock. Make a temporary copy so a partial update doesn't make a mess.
1195289475dSAlan Cox  */
12060d4ae8dSGreg Kroah-Hartman int sel_loadlut(char __user *p)
12160d4ae8dSGreg Kroah-Hartman {
1227f1534e1SAdam Borowski 	u32 tmplut[ARRAY_SIZE(inwordLut)];
1237f1534e1SAdam Borowski 	if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
1245289475dSAlan Cox 		return -EFAULT;
1257f1534e1SAdam Borowski 	memcpy(inwordLut, tmplut, sizeof(inwordLut));
1265289475dSAlan Cox 	return 0;
12760d4ae8dSGreg Kroah-Hartman }
12860d4ae8dSGreg Kroah-Hartman 
12960d4ae8dSGreg Kroah-Hartman /* does screen address p correspond to character at LH/RH edge of screen? */
13060d4ae8dSGreg Kroah-Hartman static inline int atedge(const int p, int size_row)
13160d4ae8dSGreg Kroah-Hartman {
13260d4ae8dSGreg Kroah-Hartman 	return (!(p % size_row)	|| !((p + 2) % size_row));
13360d4ae8dSGreg Kroah-Hartman }
13460d4ae8dSGreg Kroah-Hartman 
135df155d2dSAdam Borowski /* stores the char in UTF8 and returns the number of bytes used (1-4) */
136df155d2dSAdam Borowski static int store_utf8(u32 c, char *p)
13760d4ae8dSGreg Kroah-Hartman {
13860d4ae8dSGreg Kroah-Hartman 	if (c < 0x80) {
13960d4ae8dSGreg Kroah-Hartman 		/*  0******* */
14060d4ae8dSGreg Kroah-Hartman 		p[0] = c;
14160d4ae8dSGreg Kroah-Hartman 		return 1;
14260d4ae8dSGreg Kroah-Hartman 	} else if (c < 0x800) {
14360d4ae8dSGreg Kroah-Hartman 		/* 110***** 10****** */
14460d4ae8dSGreg Kroah-Hartman 		p[0] = 0xc0 | (c >> 6);
14560d4ae8dSGreg Kroah-Hartman 		p[1] = 0x80 | (c & 0x3f);
14660d4ae8dSGreg Kroah-Hartman 		return 2;
147df155d2dSAdam Borowski 	} else if (c < 0x10000) {
14860d4ae8dSGreg Kroah-Hartman 		/* 1110**** 10****** 10****** */
14960d4ae8dSGreg Kroah-Hartman 		p[0] = 0xe0 | (c >> 12);
15060d4ae8dSGreg Kroah-Hartman 		p[1] = 0x80 | ((c >> 6) & 0x3f);
15160d4ae8dSGreg Kroah-Hartman 		p[2] = 0x80 | (c & 0x3f);
15260d4ae8dSGreg Kroah-Hartman 		return 3;
153df155d2dSAdam Borowski 	} else if (c < 0x110000) {
154df155d2dSAdam Borowski 		/* 11110*** 10****** 10****** 10****** */
155df155d2dSAdam Borowski 		p[0] = 0xf0 | (c >> 18);
156df155d2dSAdam Borowski 		p[1] = 0x80 | ((c >> 12) & 0x3f);
157df155d2dSAdam Borowski 		p[2] = 0x80 | ((c >> 6) & 0x3f);
158df155d2dSAdam Borowski 		p[3] = 0x80 | (c & 0x3f);
159df155d2dSAdam Borowski 		return 4;
160df155d2dSAdam Borowski 	} else {
161df155d2dSAdam Borowski 		/* outside Unicode, replace with U+FFFD */
162df155d2dSAdam Borowski 		p[0] = 0xef;
163df155d2dSAdam Borowski 		p[1] = 0xbf;
164df155d2dSAdam Borowski 		p[2] = 0xbd;
165df155d2dSAdam Borowski 		return 3;
16660d4ae8dSGreg Kroah-Hartman 	}
16760d4ae8dSGreg Kroah-Hartman }
16860d4ae8dSGreg Kroah-Hartman 
1695289475dSAlan Cox /**
170496124e5SOkash Khawaja  *	set_selection_user	-	set the current selection.
1715289475dSAlan Cox  *	@sel: user selection info
1725289475dSAlan Cox  *	@tty: the console tty
1735289475dSAlan Cox  *
1745289475dSAlan Cox  *	Invoked by the ioctl handle for the vt layer.
1755289475dSAlan Cox  *
1765289475dSAlan Cox  *	The entire selection process is managed under the console_lock. It's
1775289475dSAlan Cox  *	 a lot under the lock but its hardly a performance path
1785289475dSAlan Cox  */
179496124e5SOkash Khawaja int set_selection_user(const struct tiocl_selection __user *sel,
180496124e5SOkash Khawaja 		       struct tty_struct *tty)
181496124e5SOkash Khawaja {
182496124e5SOkash Khawaja 	struct tiocl_selection v;
183496124e5SOkash Khawaja 
184496124e5SOkash Khawaja 	if (copy_from_user(&v, sel, sizeof(*sel)))
185496124e5SOkash Khawaja 		return -EFAULT;
186496124e5SOkash Khawaja 
187496124e5SOkash Khawaja 	return set_selection_kernel(&v, tty);
188496124e5SOkash Khawaja }
189496124e5SOkash Khawaja 
190496124e5SOkash Khawaja int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
19160d4ae8dSGreg Kroah-Hartman {
19260d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = vc_cons[fg_console].d;
1932a479aa8SAl Viro 	int new_sel_start, new_sel_end, spc;
19460d4ae8dSGreg Kroah-Hartman 	char *bp, *obp;
195101f227cSJiri Slaby 	int i, ps, pe;
1969bfdc261SAdam Borowski 	u32 c;
197101f227cSJiri Slaby 	int ret = 0;
198555b4ef7SJiri Slaby 	bool unicode;
19960d4ae8dSGreg Kroah-Hartman 
20060d4ae8dSGreg Kroah-Hartman 	poke_blanked_console();
20160d4ae8dSGreg Kroah-Hartman 
202496124e5SOkash Khawaja 	v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
203496124e5SOkash Khawaja 	v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
204496124e5SOkash Khawaja 	v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
205496124e5SOkash Khawaja 	v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
206496124e5SOkash Khawaja 	ps = v->ys * vc->vc_size_row + (v->xs << 1);
207496124e5SOkash Khawaja 	pe = v->ye * vc->vc_size_row + (v->xe << 1);
2082a479aa8SAl Viro 
209496124e5SOkash Khawaja 	if (v->sel_mode == TIOCL_SELCLEAR) {
21060d4ae8dSGreg Kroah-Hartman 		/* useful for screendump without selection highlights */
21160d4ae8dSGreg Kroah-Hartman 		clear_selection();
21260d4ae8dSGreg Kroah-Hartman 		return 0;
21360d4ae8dSGreg Kroah-Hartman 	}
21460d4ae8dSGreg Kroah-Hartman 
215496124e5SOkash Khawaja 	if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
216496124e5SOkash Khawaja 		mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
217496124e5SOkash Khawaja 			     v->ys);
21860d4ae8dSGreg Kroah-Hartman 		return 0;
21960d4ae8dSGreg Kroah-Hartman 	}
22060d4ae8dSGreg Kroah-Hartman 
221*9256d09fSJiri Slaby 	if (ps > pe)	/* make vc_sel.start <= vc_sel.end */
222df3d9a5bSGustavo A. R. Silva 		swap(ps, pe);
22360d4ae8dSGreg Kroah-Hartman 
224*9256d09fSJiri Slaby 	mutex_lock(&vc_sel.lock);
225*9256d09fSJiri Slaby 	if (vc_sel.cons != vc_cons[fg_console].d) {
22660d4ae8dSGreg Kroah-Hartman 		clear_selection();
227*9256d09fSJiri Slaby 		vc_sel.cons = vc_cons[fg_console].d;
22860d4ae8dSGreg Kroah-Hartman 	}
229555b4ef7SJiri Slaby 	unicode = vt_do_kdgkbmode(fg_console) == K_UNICODE;
23060d4ae8dSGreg Kroah-Hartman 
231496124e5SOkash Khawaja 	switch (v->sel_mode)
23260d4ae8dSGreg Kroah-Hartman 	{
23360d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELCHAR:	/* character-by-character selection */
23460d4ae8dSGreg Kroah-Hartman 			new_sel_start = ps;
23560d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe;
23660d4ae8dSGreg Kroah-Hartman 			break;
23760d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELWORD:	/* word-by-word selection */
238555b4ef7SJiri Slaby 			spc = isspace(sel_pos(ps, unicode));
23960d4ae8dSGreg Kroah-Hartman 			for (new_sel_start = ps; ; ps -= 2)
24060d4ae8dSGreg Kroah-Hartman 			{
241555b4ef7SJiri Slaby 				if ((spc && !isspace(sel_pos(ps, unicode))) ||
242555b4ef7SJiri Slaby 				    (!spc && !inword(sel_pos(ps, unicode))))
24360d4ae8dSGreg Kroah-Hartman 					break;
24460d4ae8dSGreg Kroah-Hartman 				new_sel_start = ps;
24560d4ae8dSGreg Kroah-Hartman 				if (!(ps % vc->vc_size_row))
24660d4ae8dSGreg Kroah-Hartman 					break;
24760d4ae8dSGreg Kroah-Hartman 			}
248555b4ef7SJiri Slaby 			spc = isspace(sel_pos(pe, unicode));
24960d4ae8dSGreg Kroah-Hartman 			for (new_sel_end = pe; ; pe += 2)
25060d4ae8dSGreg Kroah-Hartman 			{
251555b4ef7SJiri Slaby 				if ((spc && !isspace(sel_pos(pe, unicode))) ||
252555b4ef7SJiri Slaby 				    (!spc && !inword(sel_pos(pe, unicode))))
25360d4ae8dSGreg Kroah-Hartman 					break;
25460d4ae8dSGreg Kroah-Hartman 				new_sel_end = pe;
25560d4ae8dSGreg Kroah-Hartman 				if (!((pe + 2) % vc->vc_size_row))
25660d4ae8dSGreg Kroah-Hartman 					break;
25760d4ae8dSGreg Kroah-Hartman 			}
25860d4ae8dSGreg Kroah-Hartman 			break;
25960d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELLINE:	/* line-by-line selection */
26060d4ae8dSGreg Kroah-Hartman 			new_sel_start = ps - ps % vc->vc_size_row;
26160d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe + vc->vc_size_row
26260d4ae8dSGreg Kroah-Hartman 				    - pe % vc->vc_size_row - 2;
26360d4ae8dSGreg Kroah-Hartman 			break;
26460d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELPOINTER:
26560d4ae8dSGreg Kroah-Hartman 			highlight_pointer(pe);
26607e6124aSJiri Slaby 			goto unlock;
26760d4ae8dSGreg Kroah-Hartman 		default:
26807e6124aSJiri Slaby 			ret = -EINVAL;
26907e6124aSJiri Slaby 			goto unlock;
27060d4ae8dSGreg Kroah-Hartman 	}
27160d4ae8dSGreg Kroah-Hartman 
27260d4ae8dSGreg Kroah-Hartman 	/* remove the pointer */
27360d4ae8dSGreg Kroah-Hartman 	highlight_pointer(-1);
27460d4ae8dSGreg Kroah-Hartman 
27560d4ae8dSGreg Kroah-Hartman 	/* select to end of line if on trailing space */
27660d4ae8dSGreg Kroah-Hartman 	if (new_sel_end > new_sel_start &&
27760d4ae8dSGreg Kroah-Hartman 		!atedge(new_sel_end, vc->vc_size_row) &&
278555b4ef7SJiri Slaby 		isspace(sel_pos(new_sel_end, unicode))) {
27960d4ae8dSGreg Kroah-Hartman 		for (pe = new_sel_end + 2; ; pe += 2)
280555b4ef7SJiri Slaby 			if (!isspace(sel_pos(pe, unicode)) ||
28160d4ae8dSGreg Kroah-Hartman 			    atedge(pe, vc->vc_size_row))
28260d4ae8dSGreg Kroah-Hartman 				break;
283555b4ef7SJiri Slaby 		if (isspace(sel_pos(pe, unicode)))
28460d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe;
28560d4ae8dSGreg Kroah-Hartman 	}
286*9256d09fSJiri Slaby 	if (vc_sel.start == -1)	/* no current selection */
28760d4ae8dSGreg Kroah-Hartman 		highlight(new_sel_start, new_sel_end);
288*9256d09fSJiri Slaby 	else if (new_sel_start == vc_sel.start)
28960d4ae8dSGreg Kroah-Hartman 	{
290*9256d09fSJiri Slaby 		if (new_sel_end == vc_sel.end)	/* no action required */
29107e6124aSJiri Slaby 			goto unlock;
292*9256d09fSJiri Slaby 		else if (new_sel_end > vc_sel.end)	/* extend to right */
293*9256d09fSJiri Slaby 			highlight(vc_sel.end + 2, new_sel_end);
29460d4ae8dSGreg Kroah-Hartman 		else				/* contract from right */
295*9256d09fSJiri Slaby 			highlight(new_sel_end + 2, vc_sel.end);
29660d4ae8dSGreg Kroah-Hartman 	}
297*9256d09fSJiri Slaby 	else if (new_sel_end == vc_sel.end)
29860d4ae8dSGreg Kroah-Hartman 	{
299*9256d09fSJiri Slaby 		if (new_sel_start < vc_sel.start) /* extend to left */
300*9256d09fSJiri Slaby 			highlight(new_sel_start, vc_sel.start - 2);
30160d4ae8dSGreg Kroah-Hartman 		else				/* contract from left */
302*9256d09fSJiri Slaby 			highlight(vc_sel.start, new_sel_start - 2);
30360d4ae8dSGreg Kroah-Hartman 	}
30460d4ae8dSGreg Kroah-Hartman 	else	/* some other case; start selection from scratch */
30560d4ae8dSGreg Kroah-Hartman 	{
30660d4ae8dSGreg Kroah-Hartman 		clear_selection();
30760d4ae8dSGreg Kroah-Hartman 		highlight(new_sel_start, new_sel_end);
30860d4ae8dSGreg Kroah-Hartman 	}
309*9256d09fSJiri Slaby 	vc_sel.start = new_sel_start;
310*9256d09fSJiri Slaby 	vc_sel.end = new_sel_end;
31160d4ae8dSGreg Kroah-Hartman 
31260d4ae8dSGreg Kroah-Hartman 	/* Allocate a new buffer before freeing the old one ... */
313101f227cSJiri Slaby 	/* chars can take up to 4 bytes with unicode */
314*9256d09fSJiri Slaby 	bp = kmalloc_array((vc_sel.end - vc_sel.start) / 2 + 1, unicode ? 4 : 1,
3156da2ec56SKees Cook 			   GFP_KERNEL);
31660d4ae8dSGreg Kroah-Hartman 	if (!bp) {
31760d4ae8dSGreg Kroah-Hartman 		printk(KERN_WARNING "selection: kmalloc() failed\n");
31860d4ae8dSGreg Kroah-Hartman 		clear_selection();
31907e6124aSJiri Slaby 		ret = -ENOMEM;
32007e6124aSJiri Slaby 		goto unlock;
32160d4ae8dSGreg Kroah-Hartman 	}
322*9256d09fSJiri Slaby 	kfree(vc_sel.buffer);
323*9256d09fSJiri Slaby 	vc_sel.buffer = bp;
32460d4ae8dSGreg Kroah-Hartman 
32560d4ae8dSGreg Kroah-Hartman 	obp = bp;
326*9256d09fSJiri Slaby 	for (i = vc_sel.start; i <= vc_sel.end; i += 2) {
327555b4ef7SJiri Slaby 		c = sel_pos(i, unicode);
328555b4ef7SJiri Slaby 		if (unicode)
32960d4ae8dSGreg Kroah-Hartman 			bp += store_utf8(c, bp);
33060d4ae8dSGreg Kroah-Hartman 		else
33160d4ae8dSGreg Kroah-Hartman 			*bp++ = c;
33260d4ae8dSGreg Kroah-Hartman 		if (!isspace(c))
33360d4ae8dSGreg Kroah-Hartman 			obp = bp;
33460d4ae8dSGreg Kroah-Hartman 		if (! ((i + 2) % vc->vc_size_row)) {
33560d4ae8dSGreg Kroah-Hartman 			/* strip trailing blanks from line and add newline,
33660d4ae8dSGreg Kroah-Hartman 			   unless non-space at end of line. */
33760d4ae8dSGreg Kroah-Hartman 			if (obp != bp) {
33860d4ae8dSGreg Kroah-Hartman 				bp = obp;
33960d4ae8dSGreg Kroah-Hartman 				*bp++ = '\r';
34060d4ae8dSGreg Kroah-Hartman 			}
34160d4ae8dSGreg Kroah-Hartman 			obp = bp;
34260d4ae8dSGreg Kroah-Hartman 		}
34360d4ae8dSGreg Kroah-Hartman 	}
344*9256d09fSJiri Slaby 	vc_sel.buf_len = bp - vc_sel.buffer;
34507e6124aSJiri Slaby unlock:
346*9256d09fSJiri Slaby 	mutex_unlock(&vc_sel.lock);
34707e6124aSJiri Slaby 	return ret;
34860d4ae8dSGreg Kroah-Hartman }
349496124e5SOkash Khawaja EXPORT_SYMBOL_GPL(set_selection_kernel);
35060d4ae8dSGreg Kroah-Hartman 
35160d4ae8dSGreg Kroah-Hartman /* Insert the contents of the selection buffer into the
35260d4ae8dSGreg Kroah-Hartman  * queue of the tty associated with the current console.
35360d4ae8dSGreg Kroah-Hartman  * Invoked by ioctl().
354906cbe13SJiri Slaby  *
35520f62579SAlan Cox  * Locking: called without locks. Calls the ldisc wrongly with
35620f62579SAlan Cox  * unsafe methods,
35760d4ae8dSGreg Kroah-Hartman  */
35860d4ae8dSGreg Kroah-Hartman int paste_selection(struct tty_struct *tty)
35960d4ae8dSGreg Kroah-Hartman {
36060d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = tty->driver_data;
36160d4ae8dSGreg Kroah-Hartman 	int	pasted = 0;
36260d4ae8dSGreg Kroah-Hartman 	unsigned int count;
36360d4ae8dSGreg Kroah-Hartman 	struct  tty_ldisc *ld;
36460d4ae8dSGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
365687bff0cSJiri Slaby 	int ret = 0;
36660d4ae8dSGreg Kroah-Hartman 
367ac751efaSTorben Hohn 	console_lock();
36860d4ae8dSGreg Kroah-Hartman 	poke_blanked_console();
369ac751efaSTorben Hohn 	console_unlock();
37060d4ae8dSGreg Kroah-Hartman 
37160d4ae8dSGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
372e55afd11SPeter Hurley 	if (!ld)
373e55afd11SPeter Hurley 		return -EIO;	/* ldisc was hung up */
374a7c8d58cSPeter Hurley 	tty_buffer_lock_exclusive(&vc->port);
37560d4ae8dSGreg Kroah-Hartman 
37660d4ae8dSGreg Kroah-Hartman 	add_wait_queue(&vc->paste_wait, &wait);
377*9256d09fSJiri Slaby 	mutex_lock(&vc_sel.lock);
378*9256d09fSJiri Slaby 	while (vc_sel.buffer && vc_sel.buf_len > pasted) {
37960d4ae8dSGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
380687bff0cSJiri Slaby 		if (signal_pending(current)) {
381687bff0cSJiri Slaby 			ret = -EINTR;
382687bff0cSJiri Slaby 			break;
383687bff0cSJiri Slaby 		}
38497ef38b8SPeter Hurley 		if (tty_throttled(tty)) {
385*9256d09fSJiri Slaby 			mutex_unlock(&vc_sel.lock);
38660d4ae8dSGreg Kroah-Hartman 			schedule();
387*9256d09fSJiri Slaby 			mutex_lock(&vc_sel.lock);
38860d4ae8dSGreg Kroah-Hartman 			continue;
38960d4ae8dSGreg Kroah-Hartman 		}
39061e86cc9SPeter Hurley 		__set_current_state(TASK_RUNNING);
391*9256d09fSJiri Slaby 		count = vc_sel.buf_len - pasted;
392*9256d09fSJiri Slaby 		count = tty_ldisc_receive_buf(ld, vc_sel.buffer + pasted, NULL,
39324a89d1cSPeter Hurley 					      count);
39460d4ae8dSGreg Kroah-Hartman 		pasted += count;
39560d4ae8dSGreg Kroah-Hartman 	}
396*9256d09fSJiri Slaby 	mutex_unlock(&vc_sel.lock);
39760d4ae8dSGreg Kroah-Hartman 	remove_wait_queue(&vc->paste_wait, &wait);
39860d4ae8dSGreg Kroah-Hartman 	__set_current_state(TASK_RUNNING);
39960d4ae8dSGreg Kroah-Hartman 
400a7c8d58cSPeter Hurley 	tty_buffer_unlock_exclusive(&vc->port);
40160d4ae8dSGreg Kroah-Hartman 	tty_ldisc_deref(ld);
402687bff0cSJiri Slaby 	return ret;
40360d4ae8dSGreg Kroah-Hartman }
404496124e5SOkash Khawaja EXPORT_SYMBOL_GPL(paste_selection);
405