patch-2_6_7-vs1_9_1_12
[linux-2.6.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/vt_kern.h>
24 #include <linux/consolemap.h>
25 #include <linux/selection.h>
26 #include <linux/tiocl.h>
27 #include <linux/console.h>
28
29 #ifndef MIN
30 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
31 #endif
32
33 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
34 #define isspace(c)      ((c) == ' ')
35
36 extern void poke_blanked_console(void);
37
38 /* Variables for selection control. */
39 /* Use a dynamic buffer, instead of static (Dec 1994) */
40        int sel_cons;            /* must not be disallocated */
41 static volatile int sel_start = -1;     /* cleared by clear_selection */
42 static int sel_end;
43 static int sel_buffer_lth;
44 static char *sel_buffer;
45
46 /* clear_selection, highlight and highlight_pointer can be called
47    from interrupt (via scrollback/front) */
48
49 /* set reverse video on characters s-e of console with selection. */
50 inline static void
51 highlight(const int s, const int e) {
52         invert_screen(sel_cons, s, e-s+2, 1);
53 }
54
55 /* use complementary color to show the pointer */
56 inline static void
57 highlight_pointer(const int where) {
58         complement_pos(sel_cons, where);
59 }
60
61 static unsigned char
62 sel_pos(int n)
63 {
64         return inverse_translate(vc_cons[sel_cons].d, screen_glyph(sel_cons, n));
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 unsigned char c) {
94         return ( 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 /* set the current selection. Invoked by ioctl() or by kernel code. */
116 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
117 {
118         int sel_mode, new_sel_start, new_sel_end, spc;
119         char *bp, *obp;
120         int i, ps, pe;
121         unsigned int currcons = fg_console;
122
123         poke_blanked_console();
124
125         { unsigned short xs, ys, xe, ye;
126
127           if (verify_area(VERIFY_READ, sel, sizeof(*sel)))
128                 return -EFAULT;
129           __get_user(xs, &sel->xs);
130           __get_user(ys, &sel->ys);
131           __get_user(xe, &sel->xe);
132           __get_user(ye, &sel->ye);
133           __get_user(sel_mode, &sel->sel_mode);
134           xs--; ys--; xe--; ye--;
135           xs = limit(xs, video_num_columns - 1);
136           ys = limit(ys, video_num_lines - 1);
137           xe = limit(xe, video_num_columns - 1);
138           ye = limit(ye, video_num_lines - 1);
139           ps = ys * video_size_row + (xs << 1);
140           pe = ye * video_size_row + (xe << 1);
141
142           if (sel_mode == TIOCL_SELCLEAR) {
143               /* useful for screendump without selection highlights */
144               clear_selection();
145               return 0;
146           }
147
148           if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
149               mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
150               return 0;
151           }
152         }
153
154         if (ps > pe)    /* make sel_start <= sel_end */
155         {
156                 int tmp = ps;
157                 ps = pe;
158                 pe = tmp;
159         }
160
161         if (sel_cons != fg_console) {
162                 clear_selection();
163                 sel_cons = fg_console;
164         }
165
166         switch (sel_mode)
167         {
168                 case TIOCL_SELCHAR:     /* character-by-character selection */
169                         new_sel_start = ps;
170                         new_sel_end = pe;
171                         break;
172                 case TIOCL_SELWORD:     /* word-by-word selection */
173                         spc = isspace(sel_pos(ps));
174                         for (new_sel_start = ps; ; ps -= 2)
175                         {
176                                 if ((spc && !isspace(sel_pos(ps))) ||
177                                     (!spc && !inword(sel_pos(ps))))
178                                         break;
179                                 new_sel_start = ps;
180                                 if (!(ps % video_size_row))
181                                         break;
182                         }
183                         spc = isspace(sel_pos(pe));
184                         for (new_sel_end = pe; ; pe += 2)
185                         {
186                                 if ((spc && !isspace(sel_pos(pe))) ||
187                                     (!spc && !inword(sel_pos(pe))))
188                                         break;
189                                 new_sel_end = pe;
190                                 if (!((pe + 2) % video_size_row))
191                                         break;
192                         }
193                         break;
194                 case TIOCL_SELLINE:     /* line-by-line selection */
195                         new_sel_start = ps - ps % video_size_row;
196                         new_sel_end = pe + video_size_row
197                                     - pe % video_size_row - 2;
198                         break;
199                 case TIOCL_SELPOINTER:
200                         highlight_pointer(pe);
201                         return 0;
202                 default:
203                         return -EINVAL;
204         }
205
206         /* remove the pointer */
207         highlight_pointer(-1);
208
209         /* select to end of line if on trailing space */
210         if (new_sel_end > new_sel_start &&
211                 !atedge(new_sel_end, video_size_row) &&
212                 isspace(sel_pos(new_sel_end))) {
213                 for (pe = new_sel_end + 2; ; pe += 2)
214                         if (!isspace(sel_pos(pe)) ||
215                             atedge(pe, video_size_row))
216                                 break;
217                 if (isspace(sel_pos(pe)))
218                         new_sel_end = pe;
219         }
220         if (sel_start == -1)    /* no current selection */
221                 highlight(new_sel_start, new_sel_end);
222         else if (new_sel_start == sel_start)
223         {
224                 if (new_sel_end == sel_end)     /* no action required */
225                         return 0;
226                 else if (new_sel_end > sel_end) /* extend to right */
227                         highlight(sel_end + 2, new_sel_end);
228                 else                            /* contract from right */
229                         highlight(new_sel_end + 2, sel_end);
230         }
231         else if (new_sel_end == sel_end)
232         {
233                 if (new_sel_start < sel_start)  /* extend to left */
234                         highlight(new_sel_start, sel_start - 2);
235                 else                            /* contract from left */
236                         highlight(sel_start, new_sel_start - 2);
237         }
238         else    /* some other case; start selection from scratch */
239         {
240                 clear_selection();
241                 highlight(new_sel_start, new_sel_end);
242         }
243         sel_start = new_sel_start;
244         sel_end = new_sel_end;
245
246         /* Allocate a new buffer before freeing the old one ... */
247         bp = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
248         if (!bp) {
249                 printk(KERN_WARNING "selection: kmalloc() failed\n");
250                 clear_selection();
251                 return -ENOMEM;
252         }
253         if (sel_buffer)
254                 kfree(sel_buffer);
255         sel_buffer = bp;
256
257         obp = bp;
258         for (i = sel_start; i <= sel_end; i += 2) {
259                 *bp = sel_pos(i);
260                 if (!isspace(*bp++))
261                         obp = bp;
262                 if (! ((i + 2) % video_size_row)) {
263                         /* strip trailing blanks from line and add newline,
264                            unless non-space at end of line. */
265                         if (obp != bp) {
266                                 bp = obp;
267                                 *bp++ = '\r';
268                         }
269                         obp = bp;
270                 }
271         }
272         sel_buffer_lth = bp - sel_buffer;
273         return 0;
274 }
275
276 /* Insert the contents of the selection buffer into the
277  * queue of the tty associated with the current console.
278  * Invoked by ioctl().
279  */
280 int paste_selection(struct tty_struct *tty)
281 {
282         struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
283         int     pasted = 0, count;
284         DECLARE_WAITQUEUE(wait, current);
285
286         acquire_console_sem();
287         poke_blanked_console();
288         release_console_sem();
289
290         add_wait_queue(&vt->paste_wait, &wait);
291         while (sel_buffer && sel_buffer_lth > pasted) {
292                 set_current_state(TASK_INTERRUPTIBLE);
293                 if (test_bit(TTY_THROTTLED, &tty->flags)) {
294                         schedule();
295                         continue;
296                 }
297                 count = sel_buffer_lth - pasted;
298                 count = MIN(count, tty->ldisc.receive_room(tty));
299                 tty->ldisc.receive_buf(tty, sel_buffer + pasted, 0, count);
300                 pasted += count;
301         }
302         remove_wait_queue(&vt->paste_wait, &wait);
303         current->state = TASK_RUNNING;
304         return 0;
305 }
306
307 EXPORT_SYMBOL(set_selection);
308 EXPORT_SYMBOL(paste_selection);