annotate pwm-clip.c @ 37:e027dd4409c7

Handle empty fields correctly when filtering the list of entries
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Thu, 08 Aug 2019 10:31:06 +0200
parents 5bdea77d0c1d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1 /*
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
3 *
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
4 * Permission is hereby granted, free of charge, to any person obtaining
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
5 * a copy of this software and associated documentation files (the
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
6 * "Software"), to deal in the Software without restriction, including
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
7 * without limitation the rights to use, copy, modify, merge, publish,
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
8 * distribute, sublicense, and/or sell copies of the Software, and to
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
9 * permit persons to whom the Software is furnished to do so, subject to
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
10 * the following conditions:
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
11 *
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
13 * in all copies or substantial portions of the Software.
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
14 *
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
22 */
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
23
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
24 #include "compat.h"
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
25
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
26 #ifdef HAVE_ERR_H
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
27 #include <err.h>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
28 #endif /* HAVE_ERR_H */
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
29 #include <errno.h>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
30 #include <nettle/base64.h>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
31 #include <string.h>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
32 #include <stdlib.h>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
33 #include <stdio.h>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
34 #include <unistd.h>
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
35
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
36 #include "util.h"
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
37
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
38 #define CHUNK_SIZE 128
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
39
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
40 static char *
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
41 base64_encode(const unsigned char *src, size_t size)
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
42 {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
43 char *dst = NULL;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
44 struct base64_encode_ctx b64_ctx;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
45 size_t len = 0;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
46
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
47 dst = xmalloc(BASE64_ENCODE_LENGTH(size) +
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
48 BASE64_ENCODE_FINAL_LENGTH);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
49
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
50 base64_encode_init(&b64_ctx);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
51 len += base64_encode_update(&b64_ctx, (unsigned char *)dst, size, src);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
52 len += base64_encode_final(&b64_ctx, (unsigned char *)dst);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
53 dst[len] = '\0';
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
54
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
55 dst = xrealloc(dst, len + 1);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
56
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
57 return (dst);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
58 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
59
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
60 static void
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
61 set_clipboard(const char *s)
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
62 {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
63 char *b64 = NULL;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
64
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
65 b64 = base64_encode((const unsigned char *)s, strlen(s));
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
66 printf("\033]52;c;%s\a\n", b64);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
67 free(b64);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
68 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
69
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
70 static void
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
71 set_clipboard_dcs_tmux(const char *s)
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
72 {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
73 char *b64 = NULL;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
74
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
75 b64 = base64_encode((const unsigned char *)s, strlen(s));
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
76 printf("\033Ptmux;\033\033]52;c;%s\a\033\\", b64);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
77 free(b64);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
78 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
79
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
80 static void
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
81 set_clipboard_dcs_chunked(const char *s)
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
82 {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
83 char *b64 = NULL;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
84 size_t i;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
85
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
86 b64 = base64_encode((const unsigned char *)s, strlen(s));
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
87
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
88 printf("\033P\033]52;c;");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
89 for (i = 0; b64[i] != '\0'; i++) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
90 if ((i > 0) && (i % CHUNK_SIZE == 0)) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
91 printf("\033\\\033P");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
92 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
93 putchar(b64[i]);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
94 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
95 printf("\a\033\\");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
96
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
97 free(b64);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
98 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
99
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
100 static void
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
101 usage(void)
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
102 {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
103 fprintf(stderr, "usage: %s\n", getprogname());
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
104 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
105
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
106 int
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
107 main(int argc, char *argv[])
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
108 {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
109 int status = EXIT_FAILURE;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
110 char *term;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
111 void (*set_clipboard_func)(const char *);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
112 int errflag = 0;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
113 int c;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
114 ssize_t len;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
115 char *s = NULL;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
116 size_t size = 0;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
117
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
118 setprogname(argv[0]);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
119
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
120 term = getenv("TERM");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
121 if (term == NULL) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
122 fprintf(stderr, "error: unknown terminal\n");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
123 goto out;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
124 } else if (strncmp(term, "xterm", strlen("xterm")) == 0) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
125 set_clipboard_func = set_clipboard;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
126 } else if (strncmp(term, "tmux", strlen("tmux")) == 0) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
127 set_clipboard_func = set_clipboard_dcs_tmux;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
128 } else if (strncmp(term, "screen", strlen("screen")) == 0) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
129 if (getenv("TMUX") != NULL) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
130 set_clipboard_func = set_clipboard_dcs_tmux;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
131 } else {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
132 set_clipboard_func = set_clipboard_dcs_chunked;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
133 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
134 } else {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
135 fprintf(stderr, "terminal does not support setting "
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
136 "X11 CLIPBOARD selection\n");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
137 goto out;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
138 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
139
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
140 while (!errflag && (c = getopt(argc, argv, "h")) != -1) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
141 switch (c) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
142 case 'h':
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
143 usage();
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
144 exit(EXIT_SUCCESS);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
145 default:
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
146 errflag = 1;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
147 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
148 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
149 if (errflag || (optind + 1 < argc)) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
150 usage();
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
151 exit(EXIT_USAGE);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
152 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
153
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
154 errno = 0;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
155 len = getline(&s, &size, stdin);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
156 if (len < 0) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
157 if (errno == 0) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
158 fprintf(stderr, "failed to read line\n");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
159 goto out;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
160 } else {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
161 err(1, "getline");
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
162 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
163 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
164 if ((len > 0) && (s[len - 1] == '\n')) {
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
165 s[--len] = '\0';
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
166 }
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
167
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
168 set_clipboard_func(s);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
169
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
170 status = EXIT_SUCCESS;
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
171
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
172 out:
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
173 free(s);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
174
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
175 exit(status);
5bdea77d0c1d Add pwm-clip utility for setting the X11 CLIPBOARD selection
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
176 }