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