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