aboutsummaryrefslogtreecommitdiff
path: root/selection.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-01-26 18:31:31 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-01-26 18:31:31 +0000
commita6456911e71dae258f6529b08ac0597e6686feca (patch)
tree81604b554bdf2f4b75f3b534270ccfebc88c1f8a /selection.c
parentMerge branch 'ime-preedit-cursor-at-the-end-of-the-preedit-string' (diff)
parentselection: remove duplicate ‘ESC’ in switch case (diff)
Merge branch 'strip-control-characters-from-pasted-text'
Closes #306
Diffstat (limited to 'selection.c')
-rw-r--r--selection.c45
1 files changed, 41 insertions, 4 deletions
diff --git a/selection.c b/selection.c
index 00292541..8ed9b0cc 100644
--- a/selection.c
+++ b/selection.c
@@ -1559,7 +1559,7 @@ fdm_receive_decoder_uri(struct clipboard_receive *ctx, char *data, size_t size)
char *start = ctx->buf.data;
char *end = NULL;
- while ((end = memchr(start, '\n', ctx->buf.idx - (start - ctx->buf.data))) != NULL) {
+ while ((end = memchr(start, '\r', ctx->buf.idx - (start - ctx->buf.data))) != NULL) {
decode_one_uri(ctx, start, end - start);
start = end + 1;
}
@@ -1608,12 +1608,49 @@ fdm_receive(struct fdm *fdm, int fd, int events, void *data)
if (count == 0)
break;
- /* Call cb while at same time replacing \r\n with \n */
+ /*
+ * Call cb while at same time replace:
+ * - \r\n -> \r
+ * - \n -> \r
+ * - C0 -> <nothing> (strip non-formatting C0 characters)
+ * - \e -> <nothing> (i.e. strip ESC)
+ */
char *p = text;
size_t left = count;
again:
- for (size_t i = 0; i < left - 1; i++) {
- if (p[i] == '\r' && p[i + 1] == '\n') {
+ for (size_t i = 0; i < left; i++) {
+ switch (p[i]) {
+ default:
+ break;
+
+ case '\n':
+ p[i] = '\r';
+ break;
+
+ case '\r':
+ /* Convert \r\n -> \r */
+ if (i + 1 < left && p[i + 1] == '\n') {
+ ctx->decoder(ctx, p, i + 1);
+
+ xassert(i + 2 <= left);
+ p += i + 2;
+ left -= i + 2;
+ goto again;
+ }
+ break;
+
+ /* C0 non-formatting control characters (\b \t \n \r excluded) */
+ case '\x01': case '\x02': case '\x03': case '\x04': case '\x05':
+ case '\x06': case '\x07': case '\x0b': case '\x0c': case '\x0e':
+ case '\x0f': case '\x10': case '\x11': case '\x12': case '\x13':
+ case '\x14': case '\x15': case '\x16': case '\x17': case '\x18':
+ case '\x19': case '\x1a': case '\x1b': case '\x1c': case '\x1d':
+ case '\x1e': case '\x1f':
+ /* FALLTHROUGH */
+
+ /* Additional control characters stripped by default (but
+ * configurable) in XTerm: BS, HT, DEL */
+ case '\b': case '\t': case '\x7f':
ctx->decoder(ctx, p, i);
xassert(i + 1 <= left);