xref: /openbmc/linux/drivers/tty/vt/selection.c (revision a7c8d58c79853adeebf0a1ddc9c63e433b4d97f1)
160d4ae8dSGreg Kroah-Hartman /*
260d4ae8dSGreg Kroah-Hartman  * This module exports the functions:
360d4ae8dSGreg Kroah-Hartman  *
460d4ae8dSGreg Kroah-Hartman  *     'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
560d4ae8dSGreg Kroah-Hartman  *     'void clear_selection(void)'
660d4ae8dSGreg Kroah-Hartman  *     'int paste_selection(struct tty_struct *)'
760d4ae8dSGreg Kroah-Hartman  *     'int sel_loadlut(char __user *)'
860d4ae8dSGreg Kroah-Hartman  *
960d4ae8dSGreg Kroah-Hartman  * Now that /dev/vcs exists, most of this can disappear again.
1060d4ae8dSGreg Kroah-Hartman  */
1160d4ae8dSGreg Kroah-Hartman 
1260d4ae8dSGreg Kroah-Hartman #include <linux/module.h>
1360d4ae8dSGreg Kroah-Hartman #include <linux/tty.h>
1460d4ae8dSGreg Kroah-Hartman #include <linux/sched.h>
1560d4ae8dSGreg Kroah-Hartman #include <linux/mm.h>
1660d4ae8dSGreg Kroah-Hartman #include <linux/slab.h>
1760d4ae8dSGreg Kroah-Hartman #include <linux/types.h>
1860d4ae8dSGreg Kroah-Hartman 
1960d4ae8dSGreg Kroah-Hartman #include <asm/uaccess.h>
2060d4ae8dSGreg Kroah-Hartman 
2160d4ae8dSGreg Kroah-Hartman #include <linux/kbd_kern.h>
2260d4ae8dSGreg Kroah-Hartman #include <linux/vt_kern.h>
2360d4ae8dSGreg Kroah-Hartman #include <linux/consolemap.h>
2460d4ae8dSGreg Kroah-Hartman #include <linux/selection.h>
2560d4ae8dSGreg Kroah-Hartman #include <linux/tiocl.h>
2660d4ae8dSGreg Kroah-Hartman #include <linux/console.h>
27*a7c8d58cSPeter Hurley #include <linux/tty_flip.h>
2860d4ae8dSGreg Kroah-Hartman 
2960d4ae8dSGreg Kroah-Hartman /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
3060d4ae8dSGreg Kroah-Hartman #define isspace(c)	((c) == ' ')
3160d4ae8dSGreg Kroah-Hartman 
3260d4ae8dSGreg Kroah-Hartman extern void poke_blanked_console(void);
3360d4ae8dSGreg Kroah-Hartman 
34079c9534SAlan Cox /* FIXME: all this needs locking */
3560d4ae8dSGreg Kroah-Hartman /* Variables for selection control. */
3660d4ae8dSGreg Kroah-Hartman /* Use a dynamic buffer, instead of static (Dec 1994) */
3760d4ae8dSGreg Kroah-Hartman struct vc_data *sel_cons;		/* must not be deallocated */
3860d4ae8dSGreg Kroah-Hartman static int use_unicode;
3960d4ae8dSGreg Kroah-Hartman static volatile int sel_start = -1; 	/* cleared by clear_selection */
4060d4ae8dSGreg Kroah-Hartman static int sel_end;
4160d4ae8dSGreg Kroah-Hartman static int sel_buffer_lth;
4260d4ae8dSGreg Kroah-Hartman static char *sel_buffer;
4360d4ae8dSGreg Kroah-Hartman 
4460d4ae8dSGreg Kroah-Hartman /* clear_selection, highlight and highlight_pointer can be called
4560d4ae8dSGreg Kroah-Hartman    from interrupt (via scrollback/front) */
4660d4ae8dSGreg Kroah-Hartman 
4760d4ae8dSGreg Kroah-Hartman /* set reverse video on characters s-e of console with selection. */
4860d4ae8dSGreg Kroah-Hartman static inline void highlight(const int s, const int e)
4960d4ae8dSGreg Kroah-Hartman {
5060d4ae8dSGreg Kroah-Hartman 	invert_screen(sel_cons, s, e-s+2, 1);
5160d4ae8dSGreg Kroah-Hartman }
5260d4ae8dSGreg Kroah-Hartman 
5360d4ae8dSGreg Kroah-Hartman /* use complementary color to show the pointer */
5460d4ae8dSGreg Kroah-Hartman static inline void highlight_pointer(const int where)
5560d4ae8dSGreg Kroah-Hartman {
5660d4ae8dSGreg Kroah-Hartman 	complement_pos(sel_cons, where);
5760d4ae8dSGreg Kroah-Hartman }
5860d4ae8dSGreg Kroah-Hartman 
5960d4ae8dSGreg Kroah-Hartman static u16
6060d4ae8dSGreg Kroah-Hartman sel_pos(int n)
6160d4ae8dSGreg Kroah-Hartman {
6260d4ae8dSGreg Kroah-Hartman 	return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
6360d4ae8dSGreg Kroah-Hartman 				use_unicode);
6460d4ae8dSGreg Kroah-Hartman }
6560d4ae8dSGreg Kroah-Hartman 
665289475dSAlan Cox /**
675289475dSAlan Cox  *	clear_selection		-	remove current selection
685289475dSAlan Cox  *
695289475dSAlan Cox  *	Remove the current selection highlight, if any from the console
705289475dSAlan Cox  *	holding the selection. The caller must hold the console lock.
715289475dSAlan Cox  */
725289475dSAlan Cox void clear_selection(void)
735289475dSAlan Cox {
7460d4ae8dSGreg Kroah-Hartman 	highlight_pointer(-1); /* hide the pointer */
7560d4ae8dSGreg Kroah-Hartman 	if (sel_start != -1) {
7660d4ae8dSGreg Kroah-Hartman 		highlight(sel_start, sel_end);
7760d4ae8dSGreg Kroah-Hartman 		sel_start = -1;
7860d4ae8dSGreg Kroah-Hartman 	}
7960d4ae8dSGreg Kroah-Hartman }
8060d4ae8dSGreg Kroah-Hartman 
8160d4ae8dSGreg Kroah-Hartman /*
8260d4ae8dSGreg Kroah-Hartman  * User settable table: what characters are to be considered alphabetic?
835289475dSAlan Cox  * 256 bits. Locked by the console lock.
8460d4ae8dSGreg Kroah-Hartman  */
8560d4ae8dSGreg Kroah-Hartman static u32 inwordLut[8]={
8660d4ae8dSGreg Kroah-Hartman   0x00000000, /* control chars     */
8760d4ae8dSGreg Kroah-Hartman   0x03FF0000, /* digits            */
8860d4ae8dSGreg Kroah-Hartman   0x87FFFFFE, /* uppercase and '_' */
8960d4ae8dSGreg Kroah-Hartman   0x07FFFFFE, /* lowercase         */
9060d4ae8dSGreg Kroah-Hartman   0x00000000,
9160d4ae8dSGreg Kroah-Hartman   0x00000000,
9260d4ae8dSGreg Kroah-Hartman   0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
9360d4ae8dSGreg Kroah-Hartman   0xFF7FFFFF  /* latin-1 accented letters, not division sign */
9460d4ae8dSGreg Kroah-Hartman };
9560d4ae8dSGreg Kroah-Hartman 
9660d4ae8dSGreg Kroah-Hartman static inline int inword(const u16 c) {
9760d4ae8dSGreg Kroah-Hartman 	return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
9860d4ae8dSGreg Kroah-Hartman }
9960d4ae8dSGreg Kroah-Hartman 
1005289475dSAlan Cox /**
1015289475dSAlan Cox  *	set loadlut		-	load the LUT table
1025289475dSAlan Cox  *	@p: user table
1035289475dSAlan Cox  *
1045289475dSAlan Cox  *	Load the LUT table from user space. The caller must hold the console
1055289475dSAlan Cox  *	lock. Make a temporary copy so a partial update doesn't make a mess.
1065289475dSAlan Cox  */
10760d4ae8dSGreg Kroah-Hartman int sel_loadlut(char __user *p)
10860d4ae8dSGreg Kroah-Hartman {
1095289475dSAlan Cox 	u32 tmplut[8];
1105289475dSAlan Cox 	if (copy_from_user(tmplut, (u32 __user *)(p+4), 32))
1115289475dSAlan Cox 		return -EFAULT;
1125289475dSAlan Cox 	memcpy(inwordLut, tmplut, 32);
1135289475dSAlan Cox 	return 0;
11460d4ae8dSGreg Kroah-Hartman }
11560d4ae8dSGreg Kroah-Hartman 
11660d4ae8dSGreg Kroah-Hartman /* does screen address p correspond to character at LH/RH edge of screen? */
11760d4ae8dSGreg Kroah-Hartman static inline int atedge(const int p, int size_row)
11860d4ae8dSGreg Kroah-Hartman {
11960d4ae8dSGreg Kroah-Hartman 	return (!(p % size_row)	|| !((p + 2) % size_row));
12060d4ae8dSGreg Kroah-Hartman }
12160d4ae8dSGreg Kroah-Hartman 
12260d4ae8dSGreg Kroah-Hartman /* constrain v such that v <= u */
12360d4ae8dSGreg Kroah-Hartman static inline unsigned short limit(const unsigned short v, const unsigned short u)
12460d4ae8dSGreg Kroah-Hartman {
12560d4ae8dSGreg Kroah-Hartman 	return (v > u) ? u : v;
12660d4ae8dSGreg Kroah-Hartman }
12760d4ae8dSGreg Kroah-Hartman 
12860d4ae8dSGreg Kroah-Hartman /* stores the char in UTF8 and returns the number of bytes used (1-3) */
12960d4ae8dSGreg Kroah-Hartman static int store_utf8(u16 c, char *p)
13060d4ae8dSGreg Kroah-Hartman {
13160d4ae8dSGreg Kroah-Hartman 	if (c < 0x80) {
13260d4ae8dSGreg Kroah-Hartman 		/*  0******* */
13360d4ae8dSGreg Kroah-Hartman 		p[0] = c;
13460d4ae8dSGreg Kroah-Hartman 		return 1;
13560d4ae8dSGreg Kroah-Hartman 	} else if (c < 0x800) {
13660d4ae8dSGreg Kroah-Hartman 		/* 110***** 10****** */
13760d4ae8dSGreg Kroah-Hartman 		p[0] = 0xc0 | (c >> 6);
13860d4ae8dSGreg Kroah-Hartman 		p[1] = 0x80 | (c & 0x3f);
13960d4ae8dSGreg Kroah-Hartman 		return 2;
14060d4ae8dSGreg Kroah-Hartman     	} else {
14160d4ae8dSGreg Kroah-Hartman 		/* 1110**** 10****** 10****** */
14260d4ae8dSGreg Kroah-Hartman 		p[0] = 0xe0 | (c >> 12);
14360d4ae8dSGreg Kroah-Hartman 		p[1] = 0x80 | ((c >> 6) & 0x3f);
14460d4ae8dSGreg Kroah-Hartman 		p[2] = 0x80 | (c & 0x3f);
14560d4ae8dSGreg Kroah-Hartman 		return 3;
14660d4ae8dSGreg Kroah-Hartman     	}
14760d4ae8dSGreg Kroah-Hartman }
14860d4ae8dSGreg Kroah-Hartman 
1495289475dSAlan Cox /**
1505289475dSAlan Cox  *	set_selection		- 	set the current selection.
1515289475dSAlan Cox  *	@sel: user selection info
1525289475dSAlan Cox  *	@tty: the console tty
1535289475dSAlan Cox  *
1545289475dSAlan Cox  *	Invoked by the ioctl handle for the vt layer.
1555289475dSAlan Cox  *
1565289475dSAlan Cox  *	The entire selection process is managed under the console_lock. It's
1575289475dSAlan Cox  *	 a lot under the lock but its hardly a performance path
1585289475dSAlan Cox  */
15960d4ae8dSGreg Kroah-Hartman int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
16060d4ae8dSGreg Kroah-Hartman {
16160d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = vc_cons[fg_console].d;
16260d4ae8dSGreg Kroah-Hartman 	int sel_mode, new_sel_start, new_sel_end, spc;
16360d4ae8dSGreg Kroah-Hartman 	char *bp, *obp;
16460d4ae8dSGreg Kroah-Hartman 	int i, ps, pe, multiplier;
16560d4ae8dSGreg Kroah-Hartman 	u16 c;
166079c9534SAlan Cox 	int mode;
16760d4ae8dSGreg Kroah-Hartman 
16860d4ae8dSGreg Kroah-Hartman 	poke_blanked_console();
16960d4ae8dSGreg Kroah-Hartman 
17060d4ae8dSGreg Kroah-Hartman 	{ unsigned short xs, ys, xe, ye;
17160d4ae8dSGreg Kroah-Hartman 
17260d4ae8dSGreg Kroah-Hartman 	  if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
17360d4ae8dSGreg Kroah-Hartman 		return -EFAULT;
17460d4ae8dSGreg Kroah-Hartman 	  __get_user(xs, &sel->xs);
17560d4ae8dSGreg Kroah-Hartman 	  __get_user(ys, &sel->ys);
17660d4ae8dSGreg Kroah-Hartman 	  __get_user(xe, &sel->xe);
17760d4ae8dSGreg Kroah-Hartman 	  __get_user(ye, &sel->ye);
17860d4ae8dSGreg Kroah-Hartman 	  __get_user(sel_mode, &sel->sel_mode);
17960d4ae8dSGreg Kroah-Hartman 	  xs--; ys--; xe--; ye--;
18060d4ae8dSGreg Kroah-Hartman 	  xs = limit(xs, vc->vc_cols - 1);
18160d4ae8dSGreg Kroah-Hartman 	  ys = limit(ys, vc->vc_rows - 1);
18260d4ae8dSGreg Kroah-Hartman 	  xe = limit(xe, vc->vc_cols - 1);
18360d4ae8dSGreg Kroah-Hartman 	  ye = limit(ye, vc->vc_rows - 1);
18460d4ae8dSGreg Kroah-Hartman 	  ps = ys * vc->vc_size_row + (xs << 1);
18560d4ae8dSGreg Kroah-Hartman 	  pe = ye * vc->vc_size_row + (xe << 1);
18660d4ae8dSGreg Kroah-Hartman 
18760d4ae8dSGreg Kroah-Hartman 	  if (sel_mode == TIOCL_SELCLEAR) {
18860d4ae8dSGreg Kroah-Hartman 	      /* useful for screendump without selection highlights */
18960d4ae8dSGreg Kroah-Hartman 	      clear_selection();
19060d4ae8dSGreg Kroah-Hartman 	      return 0;
19160d4ae8dSGreg Kroah-Hartman 	  }
19260d4ae8dSGreg Kroah-Hartman 
19360d4ae8dSGreg Kroah-Hartman 	  if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
19460d4ae8dSGreg Kroah-Hartman 	      mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
19560d4ae8dSGreg Kroah-Hartman 	      return 0;
19660d4ae8dSGreg Kroah-Hartman 	  }
19760d4ae8dSGreg Kroah-Hartman         }
19860d4ae8dSGreg Kroah-Hartman 
19960d4ae8dSGreg Kroah-Hartman 	if (ps > pe)	/* make sel_start <= sel_end */
20060d4ae8dSGreg Kroah-Hartman 	{
20160d4ae8dSGreg Kroah-Hartman 		int tmp = ps;
20260d4ae8dSGreg Kroah-Hartman 		ps = pe;
20360d4ae8dSGreg Kroah-Hartman 		pe = tmp;
20460d4ae8dSGreg Kroah-Hartman 	}
20560d4ae8dSGreg Kroah-Hartman 
20660d4ae8dSGreg Kroah-Hartman 	if (sel_cons != vc_cons[fg_console].d) {
20760d4ae8dSGreg Kroah-Hartman 		clear_selection();
20860d4ae8dSGreg Kroah-Hartman 		sel_cons = vc_cons[fg_console].d;
20960d4ae8dSGreg Kroah-Hartman 	}
210079c9534SAlan Cox 	mode = vt_do_kdgkbmode(fg_console);
211079c9534SAlan Cox 	if (mode == K_UNICODE)
212079c9534SAlan Cox 		use_unicode = 1;
213079c9534SAlan Cox 	else
214079c9534SAlan Cox 		use_unicode = 0;
21560d4ae8dSGreg Kroah-Hartman 
21660d4ae8dSGreg Kroah-Hartman 	switch (sel_mode)
21760d4ae8dSGreg Kroah-Hartman 	{
21860d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELCHAR:	/* character-by-character selection */
21960d4ae8dSGreg Kroah-Hartman 			new_sel_start = ps;
22060d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe;
22160d4ae8dSGreg Kroah-Hartman 			break;
22260d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELWORD:	/* word-by-word selection */
22360d4ae8dSGreg Kroah-Hartman 			spc = isspace(sel_pos(ps));
22460d4ae8dSGreg Kroah-Hartman 			for (new_sel_start = ps; ; ps -= 2)
22560d4ae8dSGreg Kroah-Hartman 			{
22660d4ae8dSGreg Kroah-Hartman 				if ((spc && !isspace(sel_pos(ps))) ||
22760d4ae8dSGreg Kroah-Hartman 				    (!spc && !inword(sel_pos(ps))))
22860d4ae8dSGreg Kroah-Hartman 					break;
22960d4ae8dSGreg Kroah-Hartman 				new_sel_start = ps;
23060d4ae8dSGreg Kroah-Hartman 				if (!(ps % vc->vc_size_row))
23160d4ae8dSGreg Kroah-Hartman 					break;
23260d4ae8dSGreg Kroah-Hartman 			}
23360d4ae8dSGreg Kroah-Hartman 			spc = isspace(sel_pos(pe));
23460d4ae8dSGreg Kroah-Hartman 			for (new_sel_end = pe; ; pe += 2)
23560d4ae8dSGreg Kroah-Hartman 			{
23660d4ae8dSGreg Kroah-Hartman 				if ((spc && !isspace(sel_pos(pe))) ||
23760d4ae8dSGreg Kroah-Hartman 				    (!spc && !inword(sel_pos(pe))))
23860d4ae8dSGreg Kroah-Hartman 					break;
23960d4ae8dSGreg Kroah-Hartman 				new_sel_end = pe;
24060d4ae8dSGreg Kroah-Hartman 				if (!((pe + 2) % vc->vc_size_row))
24160d4ae8dSGreg Kroah-Hartman 					break;
24260d4ae8dSGreg Kroah-Hartman 			}
24360d4ae8dSGreg Kroah-Hartman 			break;
24460d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELLINE:	/* line-by-line selection */
24560d4ae8dSGreg Kroah-Hartman 			new_sel_start = ps - ps % vc->vc_size_row;
24660d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe + vc->vc_size_row
24760d4ae8dSGreg Kroah-Hartman 				    - pe % vc->vc_size_row - 2;
24860d4ae8dSGreg Kroah-Hartman 			break;
24960d4ae8dSGreg Kroah-Hartman 		case TIOCL_SELPOINTER:
25060d4ae8dSGreg Kroah-Hartman 			highlight_pointer(pe);
25160d4ae8dSGreg Kroah-Hartman 			return 0;
25260d4ae8dSGreg Kroah-Hartman 		default:
25360d4ae8dSGreg Kroah-Hartman 			return -EINVAL;
25460d4ae8dSGreg Kroah-Hartman 	}
25560d4ae8dSGreg Kroah-Hartman 
25660d4ae8dSGreg Kroah-Hartman 	/* remove the pointer */
25760d4ae8dSGreg Kroah-Hartman 	highlight_pointer(-1);
25860d4ae8dSGreg Kroah-Hartman 
25960d4ae8dSGreg Kroah-Hartman 	/* select to end of line if on trailing space */
26060d4ae8dSGreg Kroah-Hartman 	if (new_sel_end > new_sel_start &&
26160d4ae8dSGreg Kroah-Hartman 		!atedge(new_sel_end, vc->vc_size_row) &&
26260d4ae8dSGreg Kroah-Hartman 		isspace(sel_pos(new_sel_end))) {
26360d4ae8dSGreg Kroah-Hartman 		for (pe = new_sel_end + 2; ; pe += 2)
26460d4ae8dSGreg Kroah-Hartman 			if (!isspace(sel_pos(pe)) ||
26560d4ae8dSGreg Kroah-Hartman 			    atedge(pe, vc->vc_size_row))
26660d4ae8dSGreg Kroah-Hartman 				break;
26760d4ae8dSGreg Kroah-Hartman 		if (isspace(sel_pos(pe)))
26860d4ae8dSGreg Kroah-Hartman 			new_sel_end = pe;
26960d4ae8dSGreg Kroah-Hartman 	}
27060d4ae8dSGreg Kroah-Hartman 	if (sel_start == -1)	/* no current selection */
27160d4ae8dSGreg Kroah-Hartman 		highlight(new_sel_start, new_sel_end);
27260d4ae8dSGreg Kroah-Hartman 	else if (new_sel_start == sel_start)
27360d4ae8dSGreg Kroah-Hartman 	{
27460d4ae8dSGreg Kroah-Hartman 		if (new_sel_end == sel_end)	/* no action required */
27560d4ae8dSGreg Kroah-Hartman 			return 0;
27660d4ae8dSGreg Kroah-Hartman 		else if (new_sel_end > sel_end)	/* extend to right */
27760d4ae8dSGreg Kroah-Hartman 			highlight(sel_end + 2, new_sel_end);
27860d4ae8dSGreg Kroah-Hartman 		else				/* contract from right */
27960d4ae8dSGreg Kroah-Hartman 			highlight(new_sel_end + 2, sel_end);
28060d4ae8dSGreg Kroah-Hartman 	}
28160d4ae8dSGreg Kroah-Hartman 	else if (new_sel_end == sel_end)
28260d4ae8dSGreg Kroah-Hartman 	{
28360d4ae8dSGreg Kroah-Hartman 		if (new_sel_start < sel_start)	/* extend to left */
28460d4ae8dSGreg Kroah-Hartman 			highlight(new_sel_start, sel_start - 2);
28560d4ae8dSGreg Kroah-Hartman 		else				/* contract from left */
28660d4ae8dSGreg Kroah-Hartman 			highlight(sel_start, new_sel_start - 2);
28760d4ae8dSGreg Kroah-Hartman 	}
28860d4ae8dSGreg Kroah-Hartman 	else	/* some other case; start selection from scratch */
28960d4ae8dSGreg Kroah-Hartman 	{
29060d4ae8dSGreg Kroah-Hartman 		clear_selection();
29160d4ae8dSGreg Kroah-Hartman 		highlight(new_sel_start, new_sel_end);
29260d4ae8dSGreg Kroah-Hartman 	}
29360d4ae8dSGreg Kroah-Hartman 	sel_start = new_sel_start;
29460d4ae8dSGreg Kroah-Hartman 	sel_end = new_sel_end;
29560d4ae8dSGreg Kroah-Hartman 
29660d4ae8dSGreg Kroah-Hartman 	/* Allocate a new buffer before freeing the old one ... */
29760d4ae8dSGreg Kroah-Hartman 	multiplier = use_unicode ? 3 : 1;  /* chars can take up to 3 bytes */
29860d4ae8dSGreg Kroah-Hartman 	bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
29960d4ae8dSGreg Kroah-Hartman 	if (!bp) {
30060d4ae8dSGreg Kroah-Hartman 		printk(KERN_WARNING "selection: kmalloc() failed\n");
30160d4ae8dSGreg Kroah-Hartman 		clear_selection();
30260d4ae8dSGreg Kroah-Hartman 		return -ENOMEM;
30360d4ae8dSGreg Kroah-Hartman 	}
30460d4ae8dSGreg Kroah-Hartman 	kfree(sel_buffer);
30560d4ae8dSGreg Kroah-Hartman 	sel_buffer = bp;
30660d4ae8dSGreg Kroah-Hartman 
30760d4ae8dSGreg Kroah-Hartman 	obp = bp;
30860d4ae8dSGreg Kroah-Hartman 	for (i = sel_start; i <= sel_end; i += 2) {
30960d4ae8dSGreg Kroah-Hartman 		c = sel_pos(i);
31060d4ae8dSGreg Kroah-Hartman 		if (use_unicode)
31160d4ae8dSGreg Kroah-Hartman 			bp += store_utf8(c, bp);
31260d4ae8dSGreg Kroah-Hartman 		else
31360d4ae8dSGreg Kroah-Hartman 			*bp++ = c;
31460d4ae8dSGreg Kroah-Hartman 		if (!isspace(c))
31560d4ae8dSGreg Kroah-Hartman 			obp = bp;
31660d4ae8dSGreg Kroah-Hartman 		if (! ((i + 2) % vc->vc_size_row)) {
31760d4ae8dSGreg Kroah-Hartman 			/* strip trailing blanks from line and add newline,
31860d4ae8dSGreg Kroah-Hartman 			   unless non-space at end of line. */
31960d4ae8dSGreg Kroah-Hartman 			if (obp != bp) {
32060d4ae8dSGreg Kroah-Hartman 				bp = obp;
32160d4ae8dSGreg Kroah-Hartman 				*bp++ = '\r';
32260d4ae8dSGreg Kroah-Hartman 			}
32360d4ae8dSGreg Kroah-Hartman 			obp = bp;
32460d4ae8dSGreg Kroah-Hartman 		}
32560d4ae8dSGreg Kroah-Hartman 	}
32660d4ae8dSGreg Kroah-Hartman 	sel_buffer_lth = bp - sel_buffer;
32760d4ae8dSGreg Kroah-Hartman 	return 0;
32860d4ae8dSGreg Kroah-Hartman }
32960d4ae8dSGreg Kroah-Hartman 
33060d4ae8dSGreg Kroah-Hartman /* Insert the contents of the selection buffer into the
33160d4ae8dSGreg Kroah-Hartman  * queue of the tty associated with the current console.
33260d4ae8dSGreg Kroah-Hartman  * Invoked by ioctl().
333906cbe13SJiri Slaby  *
33420f62579SAlan Cox  * Locking: called without locks. Calls the ldisc wrongly with
33520f62579SAlan Cox  * unsafe methods,
33660d4ae8dSGreg Kroah-Hartman  */
33760d4ae8dSGreg Kroah-Hartman int paste_selection(struct tty_struct *tty)
33860d4ae8dSGreg Kroah-Hartman {
33960d4ae8dSGreg Kroah-Hartman 	struct vc_data *vc = tty->driver_data;
34060d4ae8dSGreg Kroah-Hartman 	int	pasted = 0;
34160d4ae8dSGreg Kroah-Hartman 	unsigned int count;
34260d4ae8dSGreg Kroah-Hartman 	struct  tty_ldisc *ld;
34360d4ae8dSGreg Kroah-Hartman 	DECLARE_WAITQUEUE(wait, current);
34460d4ae8dSGreg Kroah-Hartman 
345ac751efaSTorben Hohn 	console_lock();
34660d4ae8dSGreg Kroah-Hartman 	poke_blanked_console();
347ac751efaSTorben Hohn 	console_unlock();
34860d4ae8dSGreg Kroah-Hartman 
34960d4ae8dSGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
350*a7c8d58cSPeter Hurley 	tty_buffer_lock_exclusive(&vc->port);
35160d4ae8dSGreg Kroah-Hartman 
35260d4ae8dSGreg Kroah-Hartman 	add_wait_queue(&vc->paste_wait, &wait);
35360d4ae8dSGreg Kroah-Hartman 	while (sel_buffer && sel_buffer_lth > pasted) {
35460d4ae8dSGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
35560d4ae8dSGreg Kroah-Hartman 		if (test_bit(TTY_THROTTLED, &tty->flags)) {
35660d4ae8dSGreg Kroah-Hartman 			schedule();
35760d4ae8dSGreg Kroah-Hartman 			continue;
35860d4ae8dSGreg Kroah-Hartman 		}
35960d4ae8dSGreg Kroah-Hartman 		count = sel_buffer_lth - pasted;
36024a89d1cSPeter Hurley 		count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
36124a89d1cSPeter Hurley 					      count);
36260d4ae8dSGreg Kroah-Hartman 		pasted += count;
36360d4ae8dSGreg Kroah-Hartman 	}
36460d4ae8dSGreg Kroah-Hartman 	remove_wait_queue(&vc->paste_wait, &wait);
36560d4ae8dSGreg Kroah-Hartman 	__set_current_state(TASK_RUNNING);
36660d4ae8dSGreg Kroah-Hartman 
367*a7c8d58cSPeter Hurley 	tty_buffer_unlock_exclusive(&vc->port);
36860d4ae8dSGreg Kroah-Hartman 	tty_ldisc_deref(ld);
36960d4ae8dSGreg Kroah-Hartman 	return 0;
37060d4ae8dSGreg Kroah-Hartman }
371