11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This module exports the functions:
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     'void clear_selection(void)'
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     'int paste_selection(struct tty_struct *)'
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     'int sel_loadlut(char __user *)'
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Now that /dev/vcs exists, most of this can disappear again.
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty.h>
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sched.h>
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/mm.h>
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/types.h>
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h>
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt#include <linux/kbd_kern.h>
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/vt_kern.h>
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/consolemap.h>
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/selection.h>
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tiocl.h>
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/console.h>
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define isspace(c)	((c) == ' ')
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsextern void poke_blanked_console(void);
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33079c9534a96da9a85a2a2f9715851050fbfbf749Alan Cox/* FIXME: all this needs locking */
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Variables for selection control. */
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Use a dynamic buffer, instead of static (Dec 1994) */
36ca9bda00b4aafc42cd3d1b9d32934463e2993b4cAlan Coxstruct vc_data *sel_cons;		/* must not be deallocated */
37759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardtstatic int use_unicode;
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic volatile int sel_start = -1; 	/* cleared by clear_selection */
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sel_end;
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int sel_buffer_lth;
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic char *sel_buffer;
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* clear_selection, highlight and highlight_pointer can be called
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   from interrupt (via scrollback/front) */
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* set reverse video on characters s-e of console with selection. */
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void highlight(const int s, const int e)
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	invert_screen(sel_cons, s, e-s+2, 1);
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* use complementary color to show the pointer */
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void highlight_pointer(const int where)
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	complement_pos(sel_cons, where);
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
58759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardtstatic u16
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldssel_pos(int n)
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
61759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt	return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
62759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt				use_unicode);
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
655289475d1375017ab4288b276383e9684280876dAlan Cox/**
665289475d1375017ab4288b276383e9684280876dAlan Cox *	clear_selection		-	remove current selection
675289475d1375017ab4288b276383e9684280876dAlan Cox *
685289475d1375017ab4288b276383e9684280876dAlan Cox *	Remove the current selection highlight, if any from the console
695289475d1375017ab4288b276383e9684280876dAlan Cox *	holding the selection. The caller must hold the console lock.
705289475d1375017ab4288b276383e9684280876dAlan Cox */
715289475d1375017ab4288b276383e9684280876dAlan Coxvoid clear_selection(void)
725289475d1375017ab4288b276383e9684280876dAlan Cox{
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	highlight_pointer(-1); /* hide the pointer */
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sel_start != -1) {
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		highlight(sel_start, sel_end);
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sel_start = -1;
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * User settable table: what characters are to be considered alphabetic?
825289475d1375017ab4288b276383e9684280876dAlan Cox * 256 bits. Locked by the console lock.
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic u32 inwordLut[8]={
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0x00000000, /* control chars     */
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0x03FF0000, /* digits            */
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0x87FFFFFE, /* uppercase and '_' */
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0x07FFFFFE, /* lowercase         */
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0x00000000,
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0x00000000,
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds  0xFF7FFFFF  /* latin-1 accented letters, not division sign */
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
95759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardtstatic inline int inword(const u16 c) {
96759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt	return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
995289475d1375017ab4288b276383e9684280876dAlan Cox/**
1005289475d1375017ab4288b276383e9684280876dAlan Cox *	set loadlut		-	load the LUT table
1015289475d1375017ab4288b276383e9684280876dAlan Cox *	@p: user table
1025289475d1375017ab4288b276383e9684280876dAlan Cox *
1035289475d1375017ab4288b276383e9684280876dAlan Cox *	Load the LUT table from user space. The caller must hold the console
1045289475d1375017ab4288b276383e9684280876dAlan Cox *	lock. Make a temporary copy so a partial update doesn't make a mess.
1055289475d1375017ab4288b276383e9684280876dAlan Cox */
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint sel_loadlut(char __user *p)
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1085289475d1375017ab4288b276383e9684280876dAlan Cox	u32 tmplut[8];
1095289475d1375017ab4288b276383e9684280876dAlan Cox	if (copy_from_user(tmplut, (u32 __user *)(p+4), 32))
1105289475d1375017ab4288b276383e9684280876dAlan Cox		return -EFAULT;
1115289475d1375017ab4288b276383e9684280876dAlan Cox	memcpy(inwordLut, tmplut, 32);
1125289475d1375017ab4288b276383e9684280876dAlan Cox	return 0;
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* does screen address p correspond to character at LH/RH edge of screen? */
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int atedge(const int p, int size_row)
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (!(p % size_row)	|| !((p + 2) % size_row));
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* constrain v such that v <= u */
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline unsigned short limit(const unsigned short v, const unsigned short u)
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return (v > u) ? u : v;
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
127759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt/* stores the char in UTF8 and returns the number of bytes used (1-3) */
128759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardtstatic int store_utf8(u16 c, char *p)
129759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt{
130759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt	if (c < 0x80) {
131759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		/*  0******* */
132759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		p[0] = c;
133759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		return 1;
134759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt	} else if (c < 0x800) {
135759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		/* 110***** 10****** */
136759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		p[0] = 0xc0 | (c >> 6);
137759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		p[1] = 0x80 | (c & 0x3f);
138759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		return 2;
139759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt    	} else {
140759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		/* 1110**** 10****** 10****** */
141759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		p[0] = 0xe0 | (c >> 12);
142759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		p[1] = 0x80 | ((c >> 6) & 0x3f);
143759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		p[2] = 0x80 | (c & 0x3f);
144759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		return 3;
145759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt    	}
146759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt}
147759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt
1485289475d1375017ab4288b276383e9684280876dAlan Cox/**
1495289475d1375017ab4288b276383e9684280876dAlan Cox *	set_selection		- 	set the current selection.
1505289475d1375017ab4288b276383e9684280876dAlan Cox *	@sel: user selection info
1515289475d1375017ab4288b276383e9684280876dAlan Cox *	@tty: the console tty
1525289475d1375017ab4288b276383e9684280876dAlan Cox *
1535289475d1375017ab4288b276383e9684280876dAlan Cox *	Invoked by the ioctl handle for the vt layer.
1545289475d1375017ab4288b276383e9684280876dAlan Cox *
1555289475d1375017ab4288b276383e9684280876dAlan Cox *	The entire selection process is managed under the console_lock. It's
1565289475d1375017ab4288b276383e9684280876dAlan Cox *	 a lot under the lock but its hardly a performance path
1575289475d1375017ab4288b276383e9684280876dAlan Cox */
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct vc_data *vc = vc_cons[fg_console].d;
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int sel_mode, new_sel_start, new_sel_end, spc;
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *bp, *obp;
163759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt	int i, ps, pe, multiplier;
164759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt	u16 c;
165079c9534a96da9a85a2a2f9715851050fbfbf749Alan Cox	int mode;
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	poke_blanked_console();
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ unsigned short xs, ys, xe, ye;
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EFAULT;
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  __get_user(xs, &sel->xs);
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  __get_user(ys, &sel->ys);
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  __get_user(xe, &sel->xe);
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  __get_user(ye, &sel->ye);
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  __get_user(sel_mode, &sel->sel_mode);
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  xs--; ys--; xe--; ye--;
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  xs = limit(xs, vc->vc_cols - 1);
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  ys = limit(ys, vc->vc_rows - 1);
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  xe = limit(xe, vc->vc_cols - 1);
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  ye = limit(ye, vc->vc_rows - 1);
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  ps = ys * vc->vc_size_row + (xs << 1);
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  pe = ye * vc->vc_size_row + (xe << 1);
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  if (sel_mode == TIOCL_SELCLEAR) {
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	      /* useful for screendump without selection highlights */
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	      clear_selection();
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	      return 0;
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  }
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	      mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	      return 0;
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	  }
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds        }
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ps > pe)	/* make sel_start <= sel_end */
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		int tmp = ps;
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ps = pe;
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		pe = tmp;
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sel_cons != vc_cons[fg_console].d) {
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		clear_selection();
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sel_cons = vc_cons[fg_console].d;
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
209079c9534a96da9a85a2a2f9715851050fbfbf749Alan Cox	mode = vt_do_kdgkbmode(fg_console);
210079c9534a96da9a85a2a2f9715851050fbfbf749Alan Cox	if (mode == K_UNICODE)
211079c9534a96da9a85a2a2f9715851050fbfbf749Alan Cox		use_unicode = 1;
212079c9534a96da9a85a2a2f9715851050fbfbf749Alan Cox	else
213079c9534a96da9a85a2a2f9715851050fbfbf749Alan Cox		use_unicode = 0;
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (sel_mode)
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case TIOCL_SELCHAR:	/* character-by-character selection */
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			new_sel_start = ps;
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			new_sel_end = pe;
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case TIOCL_SELWORD:	/* word-by-word selection */
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			spc = isspace(sel_pos(ps));
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			for (new_sel_start = ps; ; ps -= 2)
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			{
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if ((spc && !isspace(sel_pos(ps))) ||
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    (!spc && !inword(sel_pos(ps))))
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					break;
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				new_sel_start = ps;
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (!(ps % vc->vc_size_row))
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					break;
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			spc = isspace(sel_pos(pe));
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			for (new_sel_end = pe; ; pe += 2)
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			{
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if ((spc && !isspace(sel_pos(pe))) ||
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    (!spc && !inword(sel_pos(pe))))
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					break;
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				new_sel_end = pe;
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				if (!((pe + 2) % vc->vc_size_row))
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					break;
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case TIOCL_SELLINE:	/* line-by-line selection */
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			new_sel_start = ps - ps % vc->vc_size_row;
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			new_sel_end = pe + vc->vc_size_row
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    - pe % vc->vc_size_row - 2;
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case TIOCL_SELPOINTER:
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			highlight_pointer(pe);
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0;
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EINVAL;
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* remove the pointer */
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	highlight_pointer(-1);
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* select to end of line if on trailing space */
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (new_sel_end > new_sel_start &&
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		!atedge(new_sel_end, vc->vc_size_row) &&
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		isspace(sel_pos(new_sel_end))) {
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (pe = new_sel_end + 2; ; pe += 2)
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (!isspace(sel_pos(pe)) ||
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    atedge(pe, vc->vc_size_row))
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (isspace(sel_pos(pe)))
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			new_sel_end = pe;
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (sel_start == -1)	/* no current selection */
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		highlight(new_sel_start, new_sel_end);
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (new_sel_start == sel_start)
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (new_sel_end == sel_end)	/* no action required */
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0;
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (new_sel_end > sel_end)	/* extend to right */
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			highlight(sel_end + 2, new_sel_end);
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else				/* contract from right */
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			highlight(new_sel_end + 2, sel_end);
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (new_sel_end == sel_end)
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (new_sel_start < sel_start)	/* extend to left */
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			highlight(new_sel_start, sel_start - 2);
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else				/* contract from left */
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			highlight(sel_start, new_sel_start - 2);
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else	/* some other case; start selection from scratch */
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		clear_selection();
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		highlight(new_sel_start, new_sel_end);
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sel_start = new_sel_start;
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sel_end = new_sel_end;
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Allocate a new buffer before freeing the old one ... */
296759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt	multiplier = use_unicode ? 3 : 1;  /* chars can take up to 3 bytes */
297878b8619f711280fd05845e21956434b5e588cc4Mikulas Patocka	bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!bp) {
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_WARNING "selection: kmalloc() failed\n");
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		clear_selection();
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
303735d5661d5c5f023a78fbe68e771e261040ff1b7Jesper Juhl	kfree(sel_buffer);
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sel_buffer = bp;
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	obp = bp;
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = sel_start; i <= sel_end; i += 2) {
308759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		c = sel_pos(i);
309759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		if (use_unicode)
310759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt			bp += store_utf8(c, bp);
311759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		else
312759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt			*bp++ = c;
313759448f459234bfcf34b82471f0dba77a9aca498Jan Engelhardt		if (!isspace(c))
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			obp = bp;
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (! ((i + 2) % vc->vc_size_row)) {
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* strip trailing blanks from line and add newline,
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   unless non-space at end of line. */
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (obp != bp) {
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				bp = obp;
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				*bp++ = '\r';
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			obp = bp;
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sel_buffer_lth = bp - sel_buffer;
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Insert the contents of the selection buffer into the
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * queue of the tty associated with the current console.
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Invoked by ioctl().
332906cbe1364d94da7cbf74c1d05e3e78b2883f661Jiri Slaby *
33320f62579dccc84428554b914e24a312a6554f841Alan Cox * Locking: called without locks. Calls the ldisc wrongly with
33420f62579dccc84428554b914e24a312a6554f841Alan Cox * unsafe methods,
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint paste_selection(struct tty_struct *tty)
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
338c9f19e96a2f33cd56c2bd19f87a0c4982d011c2bAlan Cox	struct vc_data *vc = tty->driver_data;
33933f0f88f1c51ae5c2d593d26960c760ea154c2e2Alan Cox	int	pasted = 0;
34033f0f88f1c51ae5c2d593d26960c760ea154c2e2Alan Cox	unsigned int count;
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct  tty_ldisc *ld;
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DECLARE_WAITQUEUE(wait, current);
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
344e33ac1c10b6baaac68d18d931e120d8b96e8c5f8Alan Cox
345ac751efa6a0d70f2c9daef5c7e3a92270f5c2dffTorben Hohn	console_lock();
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	poke_blanked_console();
347ac751efa6a0d70f2c9daef5c7e3a92270f5c2dffTorben Hohn	console_unlock();
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
34920f62579dccc84428554b914e24a312a6554f841Alan Cox	/* FIXME: wtf is this supposed to achieve ? */
35060af22d2ed490554cc92c8d0fed0b5b9cf687568Arnd Bergmann	ld = tty_ldisc_ref(tty);
35120f62579dccc84428554b914e24a312a6554f841Alan Cox	if (!ld)
35260af22d2ed490554cc92c8d0fed0b5b9cf687568Arnd Bergmann		ld = tty_ldisc_ref_wait(tty);
35360af22d2ed490554cc92c8d0fed0b5b9cf687568Arnd Bergmann
35420f62579dccc84428554b914e24a312a6554f841Alan Cox	/* FIXME: this is completely unsafe */
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	add_wait_queue(&vc->paste_wait, &wait);
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (sel_buffer && sel_buffer_lth > pasted) {
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		set_current_state(TASK_INTERRUPTIBLE);
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (test_bit(TTY_THROTTLED, &tty->flags)) {
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			schedule();
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count = sel_buffer_lth - pasted;
36355db4c64eddf37e31279ec15fe90314713bc9cfaLinus Torvalds		count = min(count, tty->receive_room);
36455db4c64eddf37e31279ec15fe90314713bc9cfaLinus Torvalds		tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted,
365a352def21a642133758b868c71bee12ab34ad5c5Alan Cox								NULL, count);
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		pasted += count;
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	remove_wait_queue(&vc->paste_wait, &wait);
369cc0a8fbb7ce00f65dc337dd91389b7151f44ed30Milind Arun Choudhary	__set_current_state(TASK_RUNNING);
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty_ldisc_deref(ld);
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
374