Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This module exports the functions:
0004  *
0005  *     'int set_selection_user(struct tiocl_selection __user *,
0006  *                 struct tty_struct *)'
0007  *     'int set_selection_kernel(struct tiocl_selection *, struct tty_struct *)'
0008  *     'void clear_selection(void)'
0009  *     'int paste_selection(struct tty_struct *)'
0010  *     'int sel_loadlut(char __user *)'
0011  *
0012  * Now that /dev/vcs exists, most of this can disappear again.
0013  */
0014 
0015 #include <linux/module.h>
0016 #include <linux/tty.h>
0017 #include <linux/sched.h>
0018 #include <linux/mm.h>
0019 #include <linux/mutex.h>
0020 #include <linux/slab.h>
0021 #include <linux/types.h>
0022 
0023 #include <linux/uaccess.h>
0024 
0025 #include <linux/kbd_kern.h>
0026 #include <linux/vt_kern.h>
0027 #include <linux/consolemap.h>
0028 #include <linux/selection.h>
0029 #include <linux/tiocl.h>
0030 #include <linux/console.h>
0031 #include <linux/tty_flip.h>
0032 
0033 #include <linux/sched/signal.h>
0034 
0035 /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
0036 #define is_space_on_vt(c)   ((c) == ' ')
0037 
0038 /* FIXME: all this needs locking */
0039 static struct vc_selection {
0040     struct mutex lock;
0041     struct vc_data *cons;           /* must not be deallocated */
0042     char *buffer;
0043     unsigned int buf_len;
0044     volatile int start;         /* cleared by clear_selection */
0045     int end;
0046 } vc_sel = {
0047     .lock = __MUTEX_INITIALIZER(vc_sel.lock),
0048     .start = -1,
0049 };
0050 
0051 /* clear_selection, highlight and highlight_pointer can be called
0052    from interrupt (via scrollback/front) */
0053 
0054 /* set reverse video on characters s-e of console with selection. */
0055 static inline void highlight(const int s, const int e)
0056 {
0057     invert_screen(vc_sel.cons, s, e-s+2, true);
0058 }
0059 
0060 /* use complementary color to show the pointer */
0061 static inline void highlight_pointer(const int where)
0062 {
0063     complement_pos(vc_sel.cons, where);
0064 }
0065 
0066 static u32
0067 sel_pos(int n, bool unicode)
0068 {
0069     if (unicode)
0070         return screen_glyph_unicode(vc_sel.cons, n / 2);
0071     return inverse_translate(vc_sel.cons, screen_glyph(vc_sel.cons, n),
0072             false);
0073 }
0074 
0075 /**
0076  *  clear_selection     -   remove current selection
0077  *
0078  *  Remove the current selection highlight, if any from the console
0079  *  holding the selection. The caller must hold the console lock.
0080  */
0081 void clear_selection(void)
0082 {
0083     highlight_pointer(-1); /* hide the pointer */
0084     if (vc_sel.start != -1) {
0085         highlight(vc_sel.start, vc_sel.end);
0086         vc_sel.start = -1;
0087     }
0088 }
0089 EXPORT_SYMBOL_GPL(clear_selection);
0090 
0091 bool vc_is_sel(struct vc_data *vc)
0092 {
0093     return vc == vc_sel.cons;
0094 }
0095 
0096 /*
0097  * User settable table: what characters are to be considered alphabetic?
0098  * 128 bits. Locked by the console lock.
0099  */
0100 static u32 inwordLut[]={
0101   0x00000000, /* control chars     */
0102   0x03FFE000, /* digits and "-./"  */
0103   0x87FFFFFE, /* uppercase and '_' */
0104   0x07FFFFFE, /* lowercase         */
0105 };
0106 
0107 static inline int inword(const u32 c)
0108 {
0109     return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
0110 }
0111 
0112 /**
0113  *  sel_loadlut()       -   load the LUT table
0114  *  @p: user table
0115  *
0116  *  Load the LUT table from user space. The caller must hold the console
0117  *  lock. Make a temporary copy so a partial update doesn't make a mess.
0118  */
0119 int sel_loadlut(char __user *p)
0120 {
0121     u32 tmplut[ARRAY_SIZE(inwordLut)];
0122     if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
0123         return -EFAULT;
0124     memcpy(inwordLut, tmplut, sizeof(inwordLut));
0125     return 0;
0126 }
0127 
0128 /* does screen address p correspond to character at LH/RH edge of screen? */
0129 static inline int atedge(const int p, int size_row)
0130 {
0131     return (!(p % size_row) || !((p + 2) % size_row));
0132 }
0133 
0134 /* stores the char in UTF8 and returns the number of bytes used (1-4) */
0135 static int store_utf8(u32 c, char *p)
0136 {
0137     if (c < 0x80) {
0138         /*  0******* */
0139         p[0] = c;
0140         return 1;
0141     } else if (c < 0x800) {
0142         /* 110***** 10****** */
0143         p[0] = 0xc0 | (c >> 6);
0144         p[1] = 0x80 | (c & 0x3f);
0145         return 2;
0146     } else if (c < 0x10000) {
0147         /* 1110**** 10****** 10****** */
0148         p[0] = 0xe0 | (c >> 12);
0149         p[1] = 0x80 | ((c >> 6) & 0x3f);
0150         p[2] = 0x80 | (c & 0x3f);
0151         return 3;
0152     } else if (c < 0x110000) {
0153         /* 11110*** 10****** 10****** 10****** */
0154         p[0] = 0xf0 | (c >> 18);
0155         p[1] = 0x80 | ((c >> 12) & 0x3f);
0156         p[2] = 0x80 | ((c >> 6) & 0x3f);
0157         p[3] = 0x80 | (c & 0x3f);
0158         return 4;
0159     } else {
0160         /* outside Unicode, replace with U+FFFD */
0161         p[0] = 0xef;
0162         p[1] = 0xbf;
0163         p[2] = 0xbd;
0164         return 3;
0165     }
0166 }
0167 
0168 /**
0169  *  set_selection_user  -   set the current selection.
0170  *  @sel: user selection info
0171  *  @tty: the console tty
0172  *
0173  *  Invoked by the ioctl handle for the vt layer.
0174  *
0175  *  The entire selection process is managed under the console_lock. It's
0176  *   a lot under the lock but its hardly a performance path
0177  */
0178 int set_selection_user(const struct tiocl_selection __user *sel,
0179                struct tty_struct *tty)
0180 {
0181     struct tiocl_selection v;
0182 
0183     if (copy_from_user(&v, sel, sizeof(*sel)))
0184         return -EFAULT;
0185 
0186     return set_selection_kernel(&v, tty);
0187 }
0188 
0189 static int vc_selection_store_chars(struct vc_data *vc, bool unicode)
0190 {
0191     char *bp, *obp;
0192     unsigned int i;
0193 
0194     /* Allocate a new buffer before freeing the old one ... */
0195     /* chars can take up to 4 bytes with unicode */
0196     bp = kmalloc_array((vc_sel.end - vc_sel.start) / 2 + 1, unicode ? 4 : 1,
0197                GFP_KERNEL | __GFP_NOWARN);
0198     if (!bp) {
0199         printk(KERN_WARNING "selection: kmalloc() failed\n");
0200         clear_selection();
0201         return -ENOMEM;
0202     }
0203     kfree(vc_sel.buffer);
0204     vc_sel.buffer = bp;
0205 
0206     obp = bp;
0207     for (i = vc_sel.start; i <= vc_sel.end; i += 2) {
0208         u32 c = sel_pos(i, unicode);
0209         if (unicode)
0210             bp += store_utf8(c, bp);
0211         else
0212             *bp++ = c;
0213         if (!is_space_on_vt(c))
0214             obp = bp;
0215         if (!((i + 2) % vc->vc_size_row)) {
0216             /* strip trailing blanks from line and add newline,
0217                unless non-space at end of line. */
0218             if (obp != bp) {
0219                 bp = obp;
0220                 *bp++ = '\r';
0221             }
0222             obp = bp;
0223         }
0224     }
0225     vc_sel.buf_len = bp - vc_sel.buffer;
0226 
0227     return 0;
0228 }
0229 
0230 static int vc_do_selection(struct vc_data *vc, unsigned short mode, int ps,
0231         int pe)
0232 {
0233     int new_sel_start, new_sel_end, spc;
0234     bool unicode = vt_do_kdgkbmode(fg_console) == K_UNICODE;
0235 
0236     switch (mode) {
0237     case TIOCL_SELCHAR: /* character-by-character selection */
0238         new_sel_start = ps;
0239         new_sel_end = pe;
0240         break;
0241     case TIOCL_SELWORD: /* word-by-word selection */
0242         spc = is_space_on_vt(sel_pos(ps, unicode));
0243         for (new_sel_start = ps; ; ps -= 2) {
0244             if ((spc && !is_space_on_vt(sel_pos(ps, unicode))) ||
0245                 (!spc && !inword(sel_pos(ps, unicode))))
0246                 break;
0247             new_sel_start = ps;
0248             if (!(ps % vc->vc_size_row))
0249                 break;
0250         }
0251 
0252         spc = is_space_on_vt(sel_pos(pe, unicode));
0253         for (new_sel_end = pe; ; pe += 2) {
0254             if ((spc && !is_space_on_vt(sel_pos(pe, unicode))) ||
0255                 (!spc && !inword(sel_pos(pe, unicode))))
0256                 break;
0257             new_sel_end = pe;
0258             if (!((pe + 2) % vc->vc_size_row))
0259                 break;
0260         }
0261         break;
0262     case TIOCL_SELLINE: /* line-by-line selection */
0263         new_sel_start = rounddown(ps, vc->vc_size_row);
0264         new_sel_end = rounddown(pe, vc->vc_size_row) +
0265             vc->vc_size_row - 2;
0266         break;
0267     case TIOCL_SELPOINTER:
0268         highlight_pointer(pe);
0269         return 0;
0270     default:
0271         return -EINVAL;
0272     }
0273 
0274     /* remove the pointer */
0275     highlight_pointer(-1);
0276 
0277     /* select to end of line if on trailing space */
0278     if (new_sel_end > new_sel_start &&
0279         !atedge(new_sel_end, vc->vc_size_row) &&
0280         is_space_on_vt(sel_pos(new_sel_end, unicode))) {
0281         for (pe = new_sel_end + 2; ; pe += 2)
0282             if (!is_space_on_vt(sel_pos(pe, unicode)) ||
0283                 atedge(pe, vc->vc_size_row))
0284                 break;
0285         if (is_space_on_vt(sel_pos(pe, unicode)))
0286             new_sel_end = pe;
0287     }
0288     if (vc_sel.start == -1) /* no current selection */
0289         highlight(new_sel_start, new_sel_end);
0290     else if (new_sel_start == vc_sel.start)
0291     {
0292         if (new_sel_end == vc_sel.end)  /* no action required */
0293             return 0;
0294         else if (new_sel_end > vc_sel.end)  /* extend to right */
0295             highlight(vc_sel.end + 2, new_sel_end);
0296         else                /* contract from right */
0297             highlight(new_sel_end + 2, vc_sel.end);
0298     }
0299     else if (new_sel_end == vc_sel.end)
0300     {
0301         if (new_sel_start < vc_sel.start) /* extend to left */
0302             highlight(new_sel_start, vc_sel.start - 2);
0303         else                /* contract from left */
0304             highlight(vc_sel.start, new_sel_start - 2);
0305     }
0306     else    /* some other case; start selection from scratch */
0307     {
0308         clear_selection();
0309         highlight(new_sel_start, new_sel_end);
0310     }
0311     vc_sel.start = new_sel_start;
0312     vc_sel.end = new_sel_end;
0313 
0314     return vc_selection_store_chars(vc, unicode);
0315 }
0316 
0317 static int vc_selection(struct vc_data *vc, struct tiocl_selection *v,
0318         struct tty_struct *tty)
0319 {
0320     int ps, pe;
0321 
0322     poke_blanked_console();
0323 
0324     if (v->sel_mode == TIOCL_SELCLEAR) {
0325         /* useful for screendump without selection highlights */
0326         clear_selection();
0327         return 0;
0328     }
0329 
0330     v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
0331     v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
0332     v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
0333     v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
0334 
0335     if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
0336         mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
0337                  v->ys);
0338         return 0;
0339     }
0340 
0341     ps = v->ys * vc->vc_size_row + (v->xs << 1);
0342     pe = v->ye * vc->vc_size_row + (v->xe << 1);
0343     if (ps > pe)    /* make vc_sel.start <= vc_sel.end */
0344         swap(ps, pe);
0345 
0346     if (vc_sel.cons != vc) {
0347         clear_selection();
0348         vc_sel.cons = vc;
0349     }
0350 
0351     return vc_do_selection(vc, v->sel_mode, ps, pe);
0352 }
0353 
0354 int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
0355 {
0356     int ret;
0357 
0358     mutex_lock(&vc_sel.lock);
0359     console_lock();
0360     ret = vc_selection(vc_cons[fg_console].d, v, tty);
0361     console_unlock();
0362     mutex_unlock(&vc_sel.lock);
0363 
0364     return ret;
0365 }
0366 EXPORT_SYMBOL_GPL(set_selection_kernel);
0367 
0368 /* Insert the contents of the selection buffer into the
0369  * queue of the tty associated with the current console.
0370  * Invoked by ioctl().
0371  *
0372  * Locking: called without locks. Calls the ldisc wrongly with
0373  * unsafe methods,
0374  */
0375 int paste_selection(struct tty_struct *tty)
0376 {
0377     struct vc_data *vc = tty->driver_data;
0378     int pasted = 0;
0379     unsigned int count;
0380     struct  tty_ldisc *ld;
0381     DECLARE_WAITQUEUE(wait, current);
0382     int ret = 0;
0383 
0384     console_lock();
0385     poke_blanked_console();
0386     console_unlock();
0387 
0388     ld = tty_ldisc_ref_wait(tty);
0389     if (!ld)
0390         return -EIO;    /* ldisc was hung up */
0391     tty_buffer_lock_exclusive(&vc->port);
0392 
0393     add_wait_queue(&vc->paste_wait, &wait);
0394     mutex_lock(&vc_sel.lock);
0395     while (vc_sel.buffer && vc_sel.buf_len > pasted) {
0396         set_current_state(TASK_INTERRUPTIBLE);
0397         if (signal_pending(current)) {
0398             ret = -EINTR;
0399             break;
0400         }
0401         if (tty_throttled(tty)) {
0402             mutex_unlock(&vc_sel.lock);
0403             schedule();
0404             mutex_lock(&vc_sel.lock);
0405             continue;
0406         }
0407         __set_current_state(TASK_RUNNING);
0408         count = vc_sel.buf_len - pasted;
0409         count = tty_ldisc_receive_buf(ld, vc_sel.buffer + pasted, NULL,
0410                           count);
0411         pasted += count;
0412     }
0413     mutex_unlock(&vc_sel.lock);
0414     remove_wait_queue(&vc->paste_wait, &wait);
0415     __set_current_state(TASK_RUNNING);
0416 
0417     tty_buffer_unlock_exclusive(&vc->port);
0418     tty_ldisc_deref(ld);
0419     return ret;
0420 }
0421 EXPORT_SYMBOL_GPL(paste_selection);