xref: /openbmc/linux/drivers/tty/vt/selection.c (revision 687bff0cd08f790d540cfb7b2349f0d876cdddec)
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>
1960d4ae8dSGreg Kroah-Hartman #include <linux/slab.h>
2060d4ae8dSGreg Kroah-Hartman #include <linux/types.h>
2160d4ae8dSGreg Kroah-Hartman 
227c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
2360d4ae8dSGreg Kroah-Hartman 
2460d4ae8dSGreg Kroah-Hartman #include <linux/kbd_kern.h>
2560d4ae8dSGreg Kroah-Hartman #include <linux/vt_kern.h>
2660d4ae8dSGreg Kroah-Hartman #include <linux/consolemap.h>
2760d4ae8dSGreg Kroah-Hartman #include <linux/selection.h>
2860d4ae8dSGreg Kroah-Hartman #include <linux/tiocl.h>
2960d4ae8dSGreg Kroah-Hartman #include <linux/console.h>
30a7c8d58cSPeter Hurley #include <linux/tty_flip.h>
3160d4ae8dSGreg Kroah-Hartman 
32*687bff0cSJiri Slaby #include <linux/sched/signal.h>
33*687bff0cSJiri Slaby 
3460d4ae8dSGreg Kroah-Hartman /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
3560d4ae8dSGreg Kroah-Hartman #define isspace(c)	((c) == ' ')
3660d4ae8dSGreg Kroah-Hartman 
3760d4ae8dSGreg Kroah-Hartman extern void poke_blanked_console(void);
3860d4ae8dSGreg Kroah-Hartman 
39079c9534SAlan Cox /* FIXME: all this needs locking */
4060d4ae8dSGreg Kroah-Hartman /* Variables for selection control. */
4160d4ae8dSGreg Kroah-Hartman /* Use a dynamic buffer, instead of static (Dec 1994) */
4260d4ae8dSGreg Kroah-Hartman struct vc_data *sel_cons;		/* must not be deallocated */
4360d4ae8dSGreg Kroah-Hartman static int use_unicode;
4460d4ae8dSGreg Kroah-Hartman static volatile int sel_start = -1; 	/* cleared by clear_selection */
4560d4ae8dSGreg Kroah-Hartman static int sel_end;
4660d4ae8dSGreg Kroah-Hartman static int sel_buffer_lth;
4760d4ae8dSGreg Kroah-Hartman static char *sel_buffer;
4860d4ae8dSGreg Kroah-Hartman 
4960d4ae8dSGreg Kroah-Hartman /* clear_selection, highlight and highlight_pointer can be called
5060d4ae8dSGreg Kroah-Hartman    from interrupt (via scrollback/front) */
5160d4ae8dSGreg Kroah-Hartman 
5260d4ae8dSGreg Kroah-Hartman /* set reverse video on characters s-e of console with selection. */
5360d4ae8dSGreg Kroah-Hartman static inline void highlight(const int s, const int e)
5460d4ae8dSGreg Kroah-Hartman {
5560d4ae8dSGreg Kroah-Hartman 	invert_screen(sel_cons, s, e-s+2, 1);
5660d4ae8dSGreg Kroah-Hartman }
5760d4ae8dSGreg Kroah-Hartman 
5860d4ae8dSGreg Kroah-Hartman /* use complementary color to show the pointer */
5960d4ae8dSGreg Kroah-Hartman static inline void highlight_pointer(const int where)
6060d4ae8dSGreg Kroah-Hartman {
6160d4ae8dSGreg Kroah-Hartman 	complement_pos(sel_cons, where);
6260d4ae8dSGreg Kroah-Hartman }
6360d4ae8dSGreg Kroah-Hartman 
649bfdc261SAdam Borowski static u32
6560d4ae8dSGreg Kroah-Hartman sel_pos(int n)
6660d4ae8dSGreg Kroah-Hartman {
679bfdc261SAdam Borowski 	if (use_unicode)
689bfdc261SAdam Borowski 		return screen_glyph_unicode(sel_cons, n / 2);
6960d4ae8dSGreg Kroah-Hartman 	return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
709bfdc261SAdam Borowski 				0);
7160d4ae8dSGreg Kroah-Hartman }
7260d4ae8dSGreg Kroah-Hartman 
735289475dSAlan Cox /**
745289475dSAlan Cox  *	clear_selection		-	remove current selection
755289475dSAlan Cox  *
765289475dSAlan Cox  *	Remove the current selection highlight, if any from the console
775289475dSAlan Cox  *	holding the selection. The caller must hold the console lock.
785289475dSAlan Cox  */
795289475dSAlan Cox void clear_selection(void)
805289475dSAlan Cox {
8160d4ae8dSGreg Kroah-Hartman 	highlight_pointer(-1); /* hide the pointer */
8260d4ae8dSGreg Kroah-Hartman 	if (sel_start != -1) {
8360d4ae8dSGreg Kroah-Hartman 		highlight(sel_start, sel_end);
8460d4ae8dSGreg Kroah-Hartman 		sel_start = -1;
8560d4ae8dSGreg Kroah-Hartman 	}
8660d4ae8dSGreg Kroah-Hartman }
87496124e5SOkash Khawaja EXPORT_SYMBOL_GPL(clear_selection);
8860d4ae8dSGreg Kroah-Hartman 
8960d4ae8dSGreg Kroah-Hartman /*
9060d4ae8dSGreg Kroah-Hartman  * User settable table: what characters are to be considered alphabetic?
917f1534e1SAdam Borowski  * 128 bits. Locked by the console lock.
9260d4ae8dSGreg Kroah-Hartman  */
937f1534e1SAdam Borowski static u32 inwordLut[]={
9460d4ae8dSGreg Kroah-Hartman   0x00000000, /* control chars     */
957d6d44aeSAdam Borowski   0x03FFE000, /* digits and "-./"  */
9660d4ae8dSGreg Kroah-Hartman   0x87FFFFFE, /* uppercase and '_' */
9760d4ae8dSGreg Kroah-Hartman   0x07FFFFFE, /* lowercase         */
9860d4ae8dSGreg Kroah-Hartman };
9960d4ae8dSGreg Kroah-Hartman 
1009bfdc261SAdam Borowski static inline int inword(const u32 c)
1019bfdc261SAdam Borowski {
1027f1534e1SAdam Borowski 	return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
10360d4ae8dSGreg Kroah-Hartman }
10460d4ae8dSGreg Kroah-Hartman 
1055289475dSAlan Cox /**
1065289475dSAlan Cox  *	set loadlut		-	load the LUT table
1075289475dSAlan Cox  *	@p: user table
1085289475dSAlan Cox  *
1095289475dSAlan Cox  *	Load the LUT table from user space. The caller must hold the console
1105289475dSAlan Cox  *	lock. Make a temporary copy so a partial update doesn't make a mess.
1115289475dSAlan Cox  */
11260d4ae8dSGreg Kroah-Hartman int sel_loadlut(char __user *p)
11360d4ae8dSGreg Kroah-Hartman {
1147f1534e1SAdam Borowski 	u32 tmplut[ARRAY_SIZE(inwordLut)];
1157f1534e1SAdam Borowski 	if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
1165289475dSAlan Cox 		return -EFAULT;
1177f1534e1SAdam Borowski 	memcpy(inwordLut, tmplut, sizeof(inwordLut));
1185289475dSAlan Cox 	return 0;
11960d4ae8dSGreg Kroah-Hartman }
12060d4ae8dSGreg Kroah-Hartman 
12160d4ae8dSGreg Kroah-Hartman /* does screen address p correspond to character at LH/RH edge of screen? */
12260d4ae8dSGreg Kroah-Hartman static inline int atedge(const int p, int size_row)
12360d4ae8dSGreg Kroah-Hartman {
12460d4ae8dSGreg Kroah-Hartman 	return (!(p % size_row)	|| !((p + 2) % size_row));
12560d4ae8dSGreg Kroah-Hartman }
12660d4ae8dSGreg Kroah-Hartman 
127df155d2dSAdam Borowski /* stores the char in UTF8 and returns the number of bytes used (1-4) */
128df155d2dSAdam Borowski static int store_utf8(u32 c, char *p)
12960d4ae8dSGreg Kroah-Hartman {
13060d4ae8dSGreg Kroah-Hartman 	if (c < 0x80) {
13160d4ae8dSGreg Kroah-Hartman 		/*  0******* */
13260d4ae8dSGreg Kroah-Hartman 		p[0] = c;
13360d4ae8dSGreg Kroah-Hartman 		return 1;
13460d4ae8dSGreg Kroah-Hartman 	} else if (c < 0x800) {
13560d4ae8dSGreg Kroah-Hartman 		/* 110***** 10****** */
13660d4ae8dSGreg Kroah-Hartman 		p[0] = 0xc0 | (c >> 6);
13760d4ae8dSGreg Kroah-Hartman 		p[1] = 0x80 | (c & 0x3f);
13860d4ae8dSGreg Kroah-Hartman 		return 2;
139df155d2dSAdam Borowski 	} else if (c < 0x10000) {
14060d4ae8dSGreg Kroah-Hartman 		/* 1110**** 10****** 10****** */
14160d4ae8dSGreg Kroah-Hartman 		p[0] = 0xe0 | (c >> 12);
14260d4ae8dSGreg Kroah-Hartman 		p[1] = 0x80 | ((c >> 6) & 0x3f);
14360d4ae8dSGreg Kroah-Hartman 		p[2] = 0x80 | (c & 0x3f);
14460d4ae8dSGreg Kroah-Hartman 		return 3;
145df155d2dSAdam Borowski 	} else if (c < 0x110000) {
146df155d2dSAdam Borowski 		/* 11110*** 10****** 10****** 10****** */
147df155d2dSAdam Borowski 		p[0] = 0xf0 | (c >> 18);
148df155d2dSAdam Borowski 		p[1] = 0x80 | ((c >> 12) & 0x3f);
149df155d2dSAdam Borowski 		p[2] = 0x80 | ((c >> 6) & 0x3f);
150df155d2dSAdam Borowski 		p[3] = 0x80 | (c & 0x3f);
151df155d2dSAdam Borowski 		return 4;
152df155d2dSAdam Borowski 	} else {
153df155d2dSAdam Borowski 		/* outside Unicode, replace with U+FFFD */
154df155d2dSAdam Borowski 		p[0] = 0xef;
155df155d2dSAdam Borowski 		p[1] = 0xbf;
156df155d2dSAdam Borowski 		p[2] = 0xbd;
157df155d2dSAdam Borowski 		return 3;
15860d4ae8dSGreg Kroah-Hartman 	}
15960d4ae8dSGreg Kroah-Hartman }
16060d4ae8dSGreg Kroah-Hartman 
1615289475dSAlan Cox /**
162496124e5SOkash Khawaja  *	set_selection_user	-	set the current selection.
1635289475dSAlan Cox  *	@sel: user selection info
1645289475dSAlan Cox  *	@tty: the console tty
1655289475dSAlan Cox  *
1665289475dSAlan Cox  *	Invoked by the ioctl handle for the vt layer.
1675289475dSAlan Cox  *
1685289475dSAlan Cox  *	The entire selection process is managed under the console_lock. It's
1695289475dSAlan Cox  *	 a lot under the lock but its hardly a performance path
1705289475dSAlan Cox  */
171496124e5SOkash Khawaja int set_selection_user(const struct tiocl_selection __user *sel,
172496124e5SOkash Khawaja 		       struct tty_struct *tty)
173496124e5SOkash Khawaja {
174496124e5SOkash Khawaja 	struct tiocl_selection v;
175496124e5SOkash Khawaja 
176496124e5SOkash Khawaja 	if (copy_from_user(&v, sel, sizeof(*sel)))
177496124e5SOkash Khawaja 		return -EFAULT;
178496124e5SOkash Khawaja 
179496124e5SOkash Khawaja 	return set_selection_kernel(&v, tty);
180496124e5SOkash Khawaja }
181496124e5SOkash Khawaja 
182496124e5SOkash Khawaja int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
18360d4ae8dSGreg Kroah-Hartman {
18460d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = vc_cons[fg_console].d;
1852a479aa8SAl Viro 	int new_sel_start, new_sel_end, spc;
18660d4ae8dSGreg Kroah-Hartman 	char *bp, *obp;
18760d4ae8dSGreg Kroah-Hartman 	int i, ps, pe, multiplier;
1889bfdc261SAdam Borowski 	u32 c;
189079c9534SAlan Cox 	int mode;
19060d4ae8dSGreg Kroah-Hartman 
19160d4ae8dSGreg Kroah-Hartman 	poke_blanked_console();
19260d4ae8dSGreg Kroah-Hartman 
193496124e5SOkash Khawaja 	v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
194496124e5SOkash Khawaja 	v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
195496124e5SOkash Khawaja 	v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
196496124e5SOkash Khawaja 	v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
197496124e5SOkash Khawaja 	ps = v->ys * vc->vc_size_row + (v->xs << 1);
198496124e5SOkash Khawaja 	pe = v->ye * vc->vc_size_row + (v->xe << 1);
1992a479aa8SAl Viro 
200496124e5SOkash Khawaja 	if (v->sel_mode == TIOCL_SELCLEAR) {
20160d4ae8dSGreg Kroah-Hartman 		/* useful for screendump without selection highlights */
20260d4ae8dSGreg Kroah-Hartman 		clear_selection();
20360d4ae8dSGreg Kroah-Hartman 		return 0;
20460d4ae8dSGreg Kroah-Hartman 	}
20560d4ae8dSGreg Kroah-Hartman 
206496124e5SOkash Khawaja 	if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
207496124e5SOkash Khawaja 		mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
208496124e5SOkash Khawaja 			     v->ys);
20960d4ae8dSGreg Kroah-Hartman 		return 0;
21060d4ae8dSGreg Kroah-Hartman 	}
21160d4ae8dSGreg Kroah-Hartman 
21260d4ae8dSGreg Kroah-Hartman 	if (ps > pe)	/* make sel_start <= sel_end */
213df3d9a5bSGustavo A. R. Silva 		swap(ps, pe);
21460d4ae8dSGreg Kroah-Hartman 
21560d4ae8dSGreg Kroah-Hartman 	if (sel_cons != vc_cons[fg_console].d) {
21660d4ae8dSGreg Kroah-Hartman 		clear_selection();
21760d4ae8dSGreg Kroah-Hartman 		sel_cons = vc_cons[fg_console].d;
21860d4ae8dSGreg Kroah-Hartman 	}
219079c9534SAlan Cox 	mode = vt_do_kdgkbmode(fg_console);
220079c9534SAlan Cox 	if (mode == K_UNICODE)
221079c9534SAlan Cox 		use_unicode = 1;
222079c9534SAlan Cox 	else
223079c9534SAlan Cox 		use_unicode = 0;
22460d4ae8dSGreg Kroah-Hartman 
225496124e5SOkash Khawaja 	switch (v->sel_mode)
22660d4ae8dSGreg Kroah-Hartman 	{
22760d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELCHAR:	/* character-by-character selection */
22860d4ae8dSGreg Kroah-Hartman 			new_sel_start = ps;
22960d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe;
23060d4ae8dSGreg Kroah-Hartman 			break;
23160d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELWORD:	/* word-by-word selection */
23260d4ae8dSGreg Kroah-Hartman 			spc = isspace(sel_pos(ps));
23360d4ae8dSGreg Kroah-Hartman 			for (new_sel_start = ps; ; ps -= 2)
23460d4ae8dSGreg Kroah-Hartman 			{
23560d4ae8dSGreg Kroah-Hartman 				if ((spc && !isspace(sel_pos(ps))) ||
23660d4ae8dSGreg Kroah-Hartman 				    (!spc && !inword(sel_pos(ps))))
23760d4ae8dSGreg Kroah-Hartman 					break;
23860d4ae8dSGreg Kroah-Hartman 				new_sel_start = ps;
23960d4ae8dSGreg Kroah-Hartman 				if (!(ps % vc->vc_size_row))
24060d4ae8dSGreg Kroah-Hartman 					break;
24160d4ae8dSGreg Kroah-Hartman 			}
24260d4ae8dSGreg Kroah-Hartman 			spc = isspace(sel_pos(pe));
24360d4ae8dSGreg Kroah-Hartman 			for (new_sel_end = pe; ; pe += 2)
24460d4ae8dSGreg Kroah-Hartman 			{
24560d4ae8dSGreg Kroah-Hartman 				if ((spc && !isspace(sel_pos(pe))) ||
24660d4ae8dSGreg Kroah-Hartman 				    (!spc && !inword(sel_pos(pe))))
24760d4ae8dSGreg Kroah-Hartman 					break;
24860d4ae8dSGreg Kroah-Hartman 				new_sel_end = pe;
24960d4ae8dSGreg Kroah-Hartman 				if (!((pe + 2) % vc->vc_size_row))
25060d4ae8dSGreg Kroah-Hartman 					break;
25160d4ae8dSGreg Kroah-Hartman 			}
25260d4ae8dSGreg Kroah-Hartman 			break;
25360d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELLINE:	/* line-by-line selection */
25460d4ae8dSGreg Kroah-Hartman 			new_sel_start = ps - ps % vc->vc_size_row;
25560d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe + vc->vc_size_row
25660d4ae8dSGreg Kroah-Hartman 				    - pe % vc->vc_size_row - 2;
25760d4ae8dSGreg Kroah-Hartman 			break;
25860d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELPOINTER:
25960d4ae8dSGreg Kroah-Hartman 			highlight_pointer(pe);
26060d4ae8dSGreg Kroah-Hartman 			return 0;
26160d4ae8dSGreg Kroah-Hartman 		default:
26260d4ae8dSGreg Kroah-Hartman 			return -EINVAL;
26360d4ae8dSGreg Kroah-Hartman 	}
26460d4ae8dSGreg Kroah-Hartman 
26560d4ae8dSGreg Kroah-Hartman 	/* remove the pointer */
26660d4ae8dSGreg Kroah-Hartman 	highlight_pointer(-1);
26760d4ae8dSGreg Kroah-Hartman 
26860d4ae8dSGreg Kroah-Hartman 	/* select to end of line if on trailing space */
26960d4ae8dSGreg Kroah-Hartman 	if (new_sel_end > new_sel_start &&
27060d4ae8dSGreg Kroah-Hartman 		!atedge(new_sel_end, vc->vc_size_row) &&
27160d4ae8dSGreg Kroah-Hartman 		isspace(sel_pos(new_sel_end))) {
27260d4ae8dSGreg Kroah-Hartman 		for (pe = new_sel_end + 2; ; pe += 2)
27360d4ae8dSGreg Kroah-Hartman 			if (!isspace(sel_pos(pe)) ||
27460d4ae8dSGreg Kroah-Hartman 			    atedge(pe, vc->vc_size_row))
27560d4ae8dSGreg Kroah-Hartman 				break;
27660d4ae8dSGreg Kroah-Hartman 		if (isspace(sel_pos(pe)))
27760d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe;
27860d4ae8dSGreg Kroah-Hartman 	}
27960d4ae8dSGreg Kroah-Hartman 	if (sel_start == -1)	/* no current selection */
28060d4ae8dSGreg Kroah-Hartman 		highlight(new_sel_start, new_sel_end);
28160d4ae8dSGreg Kroah-Hartman 	else if (new_sel_start == sel_start)
28260d4ae8dSGreg Kroah-Hartman 	{
28360d4ae8dSGreg Kroah-Hartman 		if (new_sel_end == sel_end)	/* no action required */
28460d4ae8dSGreg Kroah-Hartman 			return 0;
28560d4ae8dSGreg Kroah-Hartman 		else if (new_sel_end > sel_end)	/* extend to right */
28660d4ae8dSGreg Kroah-Hartman 			highlight(sel_end + 2, new_sel_end);
28760d4ae8dSGreg Kroah-Hartman 		else				/* contract from right */
28860d4ae8dSGreg Kroah-Hartman 			highlight(new_sel_end + 2, sel_end);
28960d4ae8dSGreg Kroah-Hartman 	}
29060d4ae8dSGreg Kroah-Hartman 	else if (new_sel_end == sel_end)
29160d4ae8dSGreg Kroah-Hartman 	{
29260d4ae8dSGreg Kroah-Hartman 		if (new_sel_start < sel_start)	/* extend to left */
29360d4ae8dSGreg Kroah-Hartman 			highlight(new_sel_start, sel_start - 2);
29460d4ae8dSGreg Kroah-Hartman 		else				/* contract from left */
29560d4ae8dSGreg Kroah-Hartman 			highlight(sel_start, new_sel_start - 2);
29660d4ae8dSGreg Kroah-Hartman 	}
29760d4ae8dSGreg Kroah-Hartman 	else	/* some other case; start selection from scratch */
29860d4ae8dSGreg Kroah-Hartman 	{
29960d4ae8dSGreg Kroah-Hartman 		clear_selection();
30060d4ae8dSGreg Kroah-Hartman 		highlight(new_sel_start, new_sel_end);
30160d4ae8dSGreg Kroah-Hartman 	}
30260d4ae8dSGreg Kroah-Hartman 	sel_start = new_sel_start;
30360d4ae8dSGreg Kroah-Hartman 	sel_end = new_sel_end;
30460d4ae8dSGreg Kroah-Hartman 
30560d4ae8dSGreg Kroah-Hartman 	/* Allocate a new buffer before freeing the old one ... */
306df155d2dSAdam Borowski 	multiplier = use_unicode ? 4 : 1;  /* chars can take up to 4 bytes */
3076da2ec56SKees Cook 	bp = kmalloc_array((sel_end - sel_start) / 2 + 1, multiplier,
3086da2ec56SKees Cook 			   GFP_KERNEL);
30960d4ae8dSGreg Kroah-Hartman 	if (!bp) {
31060d4ae8dSGreg Kroah-Hartman 		printk(KERN_WARNING "selection: kmalloc() failed\n");
31160d4ae8dSGreg Kroah-Hartman 		clear_selection();
31260d4ae8dSGreg Kroah-Hartman 		return -ENOMEM;
31360d4ae8dSGreg Kroah-Hartman 	}
31460d4ae8dSGreg Kroah-Hartman 	kfree(sel_buffer);
31560d4ae8dSGreg Kroah-Hartman 	sel_buffer = bp;
31660d4ae8dSGreg Kroah-Hartman 
31760d4ae8dSGreg Kroah-Hartman 	obp = bp;
31860d4ae8dSGreg Kroah-Hartman 	for (i = sel_start; i <= sel_end; i += 2) {
31960d4ae8dSGreg Kroah-Hartman 		c = sel_pos(i);
32060d4ae8dSGreg Kroah-Hartman 		if (use_unicode)
32160d4ae8dSGreg Kroah-Hartman 			bp += store_utf8(c, bp);
32260d4ae8dSGreg Kroah-Hartman 		else
32360d4ae8dSGreg Kroah-Hartman 			*bp++ = c;
32460d4ae8dSGreg Kroah-Hartman 		if (!isspace(c))
32560d4ae8dSGreg Kroah-Hartman 			obp = bp;
32660d4ae8dSGreg Kroah-Hartman 		if (! ((i + 2) % vc->vc_size_row)) {
32760d4ae8dSGreg Kroah-Hartman 			/* strip trailing blanks from line and add newline,
32860d4ae8dSGreg Kroah-Hartman 			   unless non-space at end of line. */
32960d4ae8dSGreg Kroah-Hartman 			if (obp != bp) {
33060d4ae8dSGreg Kroah-Hartman 				bp = obp;
33160d4ae8dSGreg Kroah-Hartman 				*bp++ = '\r';
33260d4ae8dSGreg Kroah-Hartman 			}
33360d4ae8dSGreg Kroah-Hartman 			obp = bp;
33460d4ae8dSGreg Kroah-Hartman 		}
33560d4ae8dSGreg Kroah-Hartman 	}
33660d4ae8dSGreg Kroah-Hartman 	sel_buffer_lth = bp - sel_buffer;
33760d4ae8dSGreg Kroah-Hartman 	return 0;
33860d4ae8dSGreg Kroah-Hartman }
339496124e5SOkash Khawaja EXPORT_SYMBOL_GPL(set_selection_kernel);
34060d4ae8dSGreg Kroah-Hartman 
34160d4ae8dSGreg Kroah-Hartman /* Insert the contents of the selection buffer into the
34260d4ae8dSGreg Kroah-Hartman  * queue of the tty associated with the current console.
34360d4ae8dSGreg Kroah-Hartman  * Invoked by ioctl().
344906cbe13SJiri Slaby  *
34520f62579SAlan Cox  * Locking: called without locks. Calls the ldisc wrongly with
34620f62579SAlan Cox  * unsafe methods,
34760d4ae8dSGreg Kroah-Hartman  */
34860d4ae8dSGreg Kroah-Hartman int paste_selection(struct tty_struct *tty)
34960d4ae8dSGreg Kroah-Hartman {
35060d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = tty->driver_data;
35160d4ae8dSGreg Kroah-Hartman 	int	pasted = 0;
35260d4ae8dSGreg Kroah-Hartman 	unsigned int count;
35360d4ae8dSGreg Kroah-Hartman 	struct  tty_ldisc *ld;
35460d4ae8dSGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
355*687bff0cSJiri Slaby 	int ret = 0;
35660d4ae8dSGreg Kroah-Hartman 
357ac751efaSTorben Hohn 	console_lock();
35860d4ae8dSGreg Kroah-Hartman 	poke_blanked_console();
359ac751efaSTorben Hohn 	console_unlock();
36060d4ae8dSGreg Kroah-Hartman 
36160d4ae8dSGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
362e55afd11SPeter Hurley 	if (!ld)
363e55afd11SPeter Hurley 		return -EIO;	/* ldisc was hung up */
364a7c8d58cSPeter Hurley 	tty_buffer_lock_exclusive(&vc->port);
36560d4ae8dSGreg Kroah-Hartman 
36660d4ae8dSGreg Kroah-Hartman 	add_wait_queue(&vc->paste_wait, &wait);
36760d4ae8dSGreg Kroah-Hartman 	while (sel_buffer && sel_buffer_lth > pasted) {
36860d4ae8dSGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
369*687bff0cSJiri Slaby 		if (signal_pending(current)) {
370*687bff0cSJiri Slaby 			ret = -EINTR;
371*687bff0cSJiri Slaby 			break;
372*687bff0cSJiri Slaby 		}
37397ef38b8SPeter Hurley 		if (tty_throttled(tty)) {
37460d4ae8dSGreg Kroah-Hartman 			schedule();
37560d4ae8dSGreg Kroah-Hartman 			continue;
37660d4ae8dSGreg Kroah-Hartman 		}
37761e86cc9SPeter Hurley 		__set_current_state(TASK_RUNNING);
37860d4ae8dSGreg Kroah-Hartman 		count = sel_buffer_lth - pasted;
37924a89d1cSPeter Hurley 		count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
38024a89d1cSPeter Hurley 					      count);
38160d4ae8dSGreg Kroah-Hartman 		pasted += count;
38260d4ae8dSGreg Kroah-Hartman 	}
38360d4ae8dSGreg Kroah-Hartman 	remove_wait_queue(&vc->paste_wait, &wait);
38460d4ae8dSGreg Kroah-Hartman 	__set_current_state(TASK_RUNNING);
38560d4ae8dSGreg Kroah-Hartman 
386a7c8d58cSPeter Hurley 	tty_buffer_unlock_exclusive(&vc->port);
38760d4ae8dSGreg Kroah-Hartman 	tty_ldisc_deref(ld);
388*687bff0cSJiri Slaby 	return ret;
38960d4ae8dSGreg Kroah-Hartman }
390496124e5SOkash Khawaja EXPORT_SYMBOL_GPL(paste_selection);
391