This source file includes following definitions.
- highlight
- highlight_pointer
- sel_pos
- clear_selection
- vc_is_sel
- inword
- sel_loadlut
- atedge
- store_utf8
- set_selection_user
- __set_selection_kernel
- set_selection_kernel
- paste_selection
1
2
3
4
5
6
7
8
9
10
11
12
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
36 #define isspace(c) ((c) == ' ')
37
38 extern void poke_blanked_console(void);
39
40
41
42
43 struct vc_data *sel_cons;
44 static int use_unicode;
45 static volatile int sel_start = -1;
46 static int sel_end;
47 static int sel_buffer_lth;
48 static char *sel_buffer;
49 static DEFINE_MUTEX(sel_lock);
50
51
52
53
54
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
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
77
78
79
80
81 void clear_selection(void)
82 {
83 highlight_pointer(-1);
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 bool vc_is_sel(struct vc_data *vc)
92 {
93 return vc == sel_cons;
94 }
95
96
97
98
99
100 static u32 inwordLut[]={
101 0x00000000,
102 0x03FFE000,
103 0x87FFFFFE,
104 0x07FFFFFE,
105 };
106
107 static inline int inword(const u32 c)
108 {
109 return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
110 }
111
112
113
114
115
116
117
118
119 int sel_loadlut(char __user *p)
120 {
121 u32 tmplut[ARRAY_SIZE(inwordLut)];
122 if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
123 return -EFAULT;
124 memcpy(inwordLut, tmplut, sizeof(inwordLut));
125 return 0;
126 }
127
128
129 static inline int atedge(const int p, int size_row)
130 {
131 return (!(p % size_row) || !((p + 2) % size_row));
132 }
133
134
135 static int store_utf8(u32 c, char *p)
136 {
137 if (c < 0x80) {
138
139 p[0] = c;
140 return 1;
141 } else if (c < 0x800) {
142
143 p[0] = 0xc0 | (c >> 6);
144 p[1] = 0x80 | (c & 0x3f);
145 return 2;
146 } else if (c < 0x10000) {
147
148 p[0] = 0xe0 | (c >> 12);
149 p[1] = 0x80 | ((c >> 6) & 0x3f);
150 p[2] = 0x80 | (c & 0x3f);
151 return 3;
152 } else if (c < 0x110000) {
153
154 p[0] = 0xf0 | (c >> 18);
155 p[1] = 0x80 | ((c >> 12) & 0x3f);
156 p[2] = 0x80 | ((c >> 6) & 0x3f);
157 p[3] = 0x80 | (c & 0x3f);
158 return 4;
159 } else {
160
161 p[0] = 0xef;
162 p[1] = 0xbf;
163 p[2] = 0xbd;
164 return 3;
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177
178 int set_selection_user(const struct tiocl_selection __user *sel,
179 struct tty_struct *tty)
180 {
181 struct tiocl_selection v;
182
183 if (copy_from_user(&v, sel, sizeof(*sel)))
184 return -EFAULT;
185
186 return set_selection_kernel(&v, tty);
187 }
188
189 static int __set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
190 {
191 struct vc_data *vc = vc_cons[fg_console].d;
192 int new_sel_start, new_sel_end, spc;
193 char *bp, *obp;
194 int i, ps, pe, multiplier;
195 u32 c;
196 int mode, ret = 0;
197
198 poke_blanked_console();
199
200 v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
201 v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
202 v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
203 v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
204 ps = v->ys * vc->vc_size_row + (v->xs << 1);
205 pe = v->ye * vc->vc_size_row + (v->xe << 1);
206
207 if (v->sel_mode == TIOCL_SELCLEAR) {
208
209 clear_selection();
210 return 0;
211 }
212
213 if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
214 mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
215 v->ys);
216 return 0;
217 }
218
219 if (ps > pe)
220 swap(ps, pe);
221
222 if (sel_cons != vc_cons[fg_console].d) {
223 clear_selection();
224 sel_cons = vc_cons[fg_console].d;
225 }
226 mode = vt_do_kdgkbmode(fg_console);
227 if (mode == K_UNICODE)
228 use_unicode = 1;
229 else
230 use_unicode = 0;
231
232 switch (v->sel_mode)
233 {
234 case TIOCL_SELCHAR:
235 new_sel_start = ps;
236 new_sel_end = pe;
237 break;
238 case TIOCL_SELWORD:
239 spc = isspace(sel_pos(ps));
240 for (new_sel_start = ps; ; ps -= 2)
241 {
242 if ((spc && !isspace(sel_pos(ps))) ||
243 (!spc && !inword(sel_pos(ps))))
244 break;
245 new_sel_start = ps;
246 if (!(ps % vc->vc_size_row))
247 break;
248 }
249 spc = isspace(sel_pos(pe));
250 for (new_sel_end = pe; ; pe += 2)
251 {
252 if ((spc && !isspace(sel_pos(pe))) ||
253 (!spc && !inword(sel_pos(pe))))
254 break;
255 new_sel_end = pe;
256 if (!((pe + 2) % vc->vc_size_row))
257 break;
258 }
259 break;
260 case TIOCL_SELLINE:
261 new_sel_start = ps - ps % vc->vc_size_row;
262 new_sel_end = pe + vc->vc_size_row
263 - pe % vc->vc_size_row - 2;
264 break;
265 case TIOCL_SELPOINTER:
266 highlight_pointer(pe);
267 return 0;
268 default:
269 return -EINVAL;
270 }
271
272
273 highlight_pointer(-1);
274
275
276 if (new_sel_end > new_sel_start &&
277 !atedge(new_sel_end, vc->vc_size_row) &&
278 isspace(sel_pos(new_sel_end))) {
279 for (pe = new_sel_end + 2; ; pe += 2)
280 if (!isspace(sel_pos(pe)) ||
281 atedge(pe, vc->vc_size_row))
282 break;
283 if (isspace(sel_pos(pe)))
284 new_sel_end = pe;
285 }
286 if (sel_start == -1)
287 highlight(new_sel_start, new_sel_end);
288 else if (new_sel_start == sel_start)
289 {
290 if (new_sel_end == sel_end)
291 return 0;
292 else if (new_sel_end > sel_end)
293 highlight(sel_end + 2, new_sel_end);
294 else
295 highlight(new_sel_end + 2, sel_end);
296 }
297 else if (new_sel_end == sel_end)
298 {
299 if (new_sel_start < sel_start)
300 highlight(new_sel_start, sel_start - 2);
301 else
302 highlight(sel_start, new_sel_start - 2);
303 }
304 else
305 {
306 clear_selection();
307 highlight(new_sel_start, new_sel_end);
308 }
309 sel_start = new_sel_start;
310 sel_end = new_sel_end;
311
312
313 multiplier = use_unicode ? 4 : 1;
314 bp = kmalloc_array((sel_end - sel_start) / 2 + 1, multiplier,
315 GFP_KERNEL);
316 if (!bp) {
317 printk(KERN_WARNING "selection: kmalloc() failed\n");
318 clear_selection();
319 return -ENOMEM;
320 }
321 kfree(sel_buffer);
322 sel_buffer = bp;
323
324 obp = bp;
325 for (i = sel_start; i <= sel_end; i += 2) {
326 c = sel_pos(i);
327 if (use_unicode)
328 bp += store_utf8(c, bp);
329 else
330 *bp++ = c;
331 if (!isspace(c))
332 obp = bp;
333 if (! ((i + 2) % vc->vc_size_row)) {
334
335
336 if (obp != bp) {
337 bp = obp;
338 *bp++ = '\r';
339 }
340 obp = bp;
341 }
342 }
343 sel_buffer_lth = bp - sel_buffer;
344
345 return ret;
346 }
347
348 int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
349 {
350 int ret;
351
352 mutex_lock(&sel_lock);
353 console_lock();
354 ret = __set_selection_kernel(v, tty);
355 console_unlock();
356 mutex_unlock(&sel_lock);
357
358 return ret;
359 }
360 EXPORT_SYMBOL_GPL(set_selection_kernel);
361
362
363
364
365
366
367
368
369 int paste_selection(struct tty_struct *tty)
370 {
371 struct vc_data *vc = tty->driver_data;
372 int pasted = 0;
373 unsigned int count;
374 struct tty_ldisc *ld;
375 DECLARE_WAITQUEUE(wait, current);
376 int ret = 0;
377
378 console_lock();
379 poke_blanked_console();
380 console_unlock();
381
382 ld = tty_ldisc_ref_wait(tty);
383 if (!ld)
384 return -EIO;
385 tty_buffer_lock_exclusive(&vc->port);
386
387 add_wait_queue(&vc->paste_wait, &wait);
388 mutex_lock(&sel_lock);
389 while (sel_buffer && sel_buffer_lth > pasted) {
390 set_current_state(TASK_INTERRUPTIBLE);
391 if (signal_pending(current)) {
392 ret = -EINTR;
393 break;
394 }
395 if (tty_throttled(tty)) {
396 mutex_unlock(&sel_lock);
397 schedule();
398 mutex_lock(&sel_lock);
399 continue;
400 }
401 __set_current_state(TASK_RUNNING);
402 count = sel_buffer_lth - pasted;
403 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
404 count);
405 pasted += count;
406 }
407 mutex_unlock(&sel_lock);
408 remove_wait_queue(&vc->paste_wait, &wait);
409 __set_current_state(TASK_RUNNING);
410
411 tty_buffer_unlock_exclusive(&vc->port);
412 tty_ldisc_deref(ld);
413 return ret;
414 }
415 EXPORT_SYMBOL_GPL(paste_selection);