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