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