[PATCH] qlogic lockup fix
[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/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 /* 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 /* Variables for selection control. */
35 /* Use a dynamic buffer, instead of static (Dec 1994) */
36 struct vc_data *sel_cons;               /* must not be disallocated */
37 static volatile int sel_start = -1;     /* cleared by clear_selection */
38 static int sel_end;
39 static int sel_buffer_lth;
40 static char *sel_buffer;
41
42 /* clear_selection, highlight and highlight_pointer can be called
43    from interrupt (via scrollback/front) */
44
45 /* set reverse video on characters s-e of console with selection. */
46 static inline void highlight(const int s, const int e)
47 {
48         invert_screen(sel_cons, s, e-s+2, 1);
49 }
50
51 /* use complementary color to show the pointer */
52 static inline void highlight_pointer(const int where)
53 {
54         complement_pos(sel_cons, where);
55 }
56
57 static unsigned char
58 sel_pos(int n)
59 {
60         return inverse_translate(sel_cons, screen_glyph(sel_cons, n));
61 }
62
63 /* remove the current selection highlight, if any,
64    from the console holding the selection. */
65 void
66 clear_selection(void) {
67         highlight_pointer(-1); /* hide the pointer */
68         if (sel_start != -1) {
69                 highlight(sel_start, sel_end);
70                 sel_start = -1;
71         }
72 }
73
74 /*
75  * User settable table: what characters are to be considered alphabetic?
76  * 256 bits
77  */
78 static u32 inwordLut[8]={
79   0x00000000, /* control chars     */
80   0x03FF0000, /* digits            */
81   0x87FFFFFE, /* uppercase and '_' */
82   0x07FFFFFE, /* lowercase         */
83   0x00000000,
84   0x00000000,
85   0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
86   0xFF7FFFFF  /* latin-1 accented letters, not division sign */
87 };
88
89 static inline int inword(const unsigned char c) {
90         return ( inwordLut[c>>5] >> (c & 0x1F) ) & 1;
91 }
92
93 /* set inwordLut contents. Invoked by ioctl(). */
94 int sel_loadlut(char __user *p)
95 {
96         return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
97 }
98
99 /* does screen address p correspond to character at LH/RH edge of screen? */
100 static inline int atedge(const int p, int size_row)
101 {
102         return (!(p % size_row) || !((p + 2) % size_row));
103 }
104
105 /* constrain v such that v <= u */
106 static inline unsigned short limit(const unsigned short v, const unsigned short u)
107 {
108         return (v > u) ? u : v;
109 }
110
111 /* set the current selection. Invoked by ioctl() or by kernel code. */
112 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
113 {
114         struct vc_data *vc = vc_cons[fg_console].d;
115         int sel_mode, new_sel_start, new_sel_end, spc;
116         char *bp, *obp;
117         int i, ps, pe;
118
119         poke_blanked_console();
120
121         { unsigned short xs, ys, xe, ye;
122
123           if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
124                 return -EFAULT;
125           __get_user(xs, &sel->xs);
126           __get_user(ys, &sel->ys);
127           __get_user(xe, &sel->xe);
128           __get_user(ye, &sel->ye);
129           __get_user(sel_mode, &sel->sel_mode);
130           xs--; ys--; xe--; ye--;
131           xs = limit(xs, vc->vc_cols - 1);
132           ys = limit(ys, vc->vc_rows - 1);
133           xe = limit(xe, vc->vc_cols - 1);
134           ye = limit(ye, vc->vc_rows - 1);
135           ps = ys * vc->vc_size_row + (xs << 1);
136           pe = ye * vc->vc_size_row + (xe << 1);
137
138           if (sel_mode == TIOCL_SELCLEAR) {
139               /* useful for screendump without selection highlights */
140               clear_selection();
141               return 0;
142           }
143
144           if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
145               mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
146               return 0;
147           }
148         }
149
150         if (ps > pe)    /* make sel_start <= sel_end */
151         {
152                 int tmp = ps;
153                 ps = pe;
154                 pe = tmp;
155         }
156
157         if (sel_cons != vc_cons[fg_console].d) {
158                 clear_selection();
159                 sel_cons = vc_cons[fg_console].d;
160         }
161
162         switch (sel_mode)
163         {
164                 case TIOCL_SELCHAR:     /* character-by-character selection */
165                         new_sel_start = ps;
166                         new_sel_end = pe;
167                         break;
168                 case TIOCL_SELWORD:     /* word-by-word selection */
169                         spc = isspace(sel_pos(ps));
170                         for (new_sel_start = ps; ; ps -= 2)
171                         {
172                                 if ((spc && !isspace(sel_pos(ps))) ||
173                                     (!spc && !inword(sel_pos(ps))))
174                                         break;
175                                 new_sel_start = ps;
176                                 if (!(ps % vc->vc_size_row))
177                                         break;
178                         }
179                         spc = isspace(sel_pos(pe));
180                         for (new_sel_end = pe; ; pe += 2)
181                         {
182                                 if ((spc && !isspace(sel_pos(pe))) ||
183                                     (!spc && !inword(sel_pos(pe))))
184                                         break;
185                                 new_sel_end = pe;
186                                 if (!((pe + 2) % vc->vc_size_row))
187                                         break;
188                         }
189                         break;
190                 case TIOCL_SELLINE:     /* line-by-line selection */
191                         new_sel_start = ps - ps % vc->vc_size_row;
192                         new_sel_end = pe + vc->vc_size_row
193                                     - pe % vc->vc_size_row - 2;
194                         break;
195                 case TIOCL_SELPOINTER:
196                         highlight_pointer(pe);
197                         return 0;
198                 default:
199                         return -EINVAL;
200         }
201
202         /* remove the pointer */
203         highlight_pointer(-1);
204
205         /* select to end of line if on trailing space */
206         if (new_sel_end > new_sel_start &&
207                 !atedge(new_sel_end, vc->vc_size_row) &&
208                 isspace(sel_pos(new_sel_end))) {
209                 for (pe = new_sel_end + 2; ; pe += 2)
210                         if (!isspace(sel_pos(pe)) ||
211                             atedge(pe, vc->vc_size_row))
212                                 break;
213                 if (isspace(sel_pos(pe)))
214                         new_sel_end = pe;
215         }
216         if (sel_start == -1)    /* no current selection */
217                 highlight(new_sel_start, new_sel_end);
218         else if (new_sel_start == sel_start)
219         {
220                 if (new_sel_end == sel_end)     /* no action required */
221                         return 0;
222                 else if (new_sel_end > sel_end) /* extend to right */
223                         highlight(sel_end + 2, new_sel_end);
224                 else                            /* contract from right */
225                         highlight(new_sel_end + 2, sel_end);
226         }
227         else if (new_sel_end == sel_end)
228         {
229                 if (new_sel_start < sel_start)  /* extend to left */
230                         highlight(new_sel_start, sel_start - 2);
231                 else                            /* contract from left */
232                         highlight(sel_start, new_sel_start - 2);
233         }
234         else    /* some other case; start selection from scratch */
235         {
236                 clear_selection();
237                 highlight(new_sel_start, new_sel_end);
238         }
239         sel_start = new_sel_start;
240         sel_end = new_sel_end;
241
242         /* Allocate a new buffer before freeing the old one ... */
243         bp = kmalloc((sel_end-sel_start)/2+1, GFP_KERNEL);
244         if (!bp) {
245                 printk(KERN_WARNING "selection: kmalloc() failed\n");
246                 clear_selection();
247                 return -ENOMEM;
248         }
249         if (sel_buffer)
250                 kfree(sel_buffer);
251         sel_buffer = bp;
252
253         obp = bp;
254         for (i = sel_start; i <= sel_end; i += 2) {
255                 *bp = sel_pos(i);
256                 if (!isspace(*bp++))
257                         obp = bp;
258                 if (! ((i + 2) % vc->vc_size_row)) {
259                         /* strip trailing blanks from line and add newline,
260                            unless non-space at end of line. */
261                         if (obp != bp) {
262                                 bp = obp;
263                                 *bp++ = '\r';
264                         }
265                         obp = bp;
266                 }
267         }
268         sel_buffer_lth = bp - sel_buffer;
269         return 0;
270 }
271
272 /* Insert the contents of the selection buffer into the
273  * queue of the tty associated with the current console.
274  * Invoked by ioctl().
275  */
276 int paste_selection(struct tty_struct *tty)
277 {
278         struct vc_data *vc = (struct vc_data *)tty->driver_data;
279         int     pasted = 0, count;
280         struct  tty_ldisc *ld;
281         DECLARE_WAITQUEUE(wait, current);
282
283         acquire_console_sem();
284         poke_blanked_console();
285         release_console_sem();
286
287         ld = tty_ldisc_ref_wait(tty);
288         
289         add_wait_queue(&vc->paste_wait, &wait);
290         while (sel_buffer && sel_buffer_lth > pasted) {
291                 set_current_state(TASK_INTERRUPTIBLE);
292                 if (test_bit(TTY_THROTTLED, &tty->flags)) {
293                         schedule();
294                         continue;
295                 }
296                 count = sel_buffer_lth - pasted;
297                 count = min(count, tty->ldisc.receive_room(tty));
298                 tty->ldisc.receive_buf(tty, sel_buffer + pasted, NULL, count);
299                 pasted += count;
300         }
301         remove_wait_queue(&vc->paste_wait, &wait);
302         current->state = TASK_RUNNING;
303
304         tty_ldisc_deref(ld);
305         return 0;
306 }