projects/pwm

diff pwm-clip.c @ 26:5bdea77d0c1d

Add pwm-clip utility for setting the X11 CLIPBOARD selection
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Thu Sep 21 09:45:59 2017 +0200 (2017-09-21)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pwm-clip.c	Thu Sep 21 09:45:59 2017 +0200
     1.3 @@ -0,0 +1,176 @@
     1.4 +/*
     1.5 + * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
     1.6 + *
     1.7 + * Permission is hereby granted, free of charge, to any person obtaining
     1.8 + * a copy of this software and associated documentation files (the
     1.9 + * "Software"), to deal in the Software without restriction, including
    1.10 + * without limitation the rights to use, copy, modify, merge, publish,
    1.11 + * distribute, sublicense, and/or sell copies of the Software, and to
    1.12 + * permit persons to whom the Software is furnished to do so, subject to
    1.13 + * the following conditions:
    1.14 + *
    1.15 + * The above copyright notice and this permission notice shall be included
    1.16 + * in all copies or substantial portions of the Software.
    1.17 + *
    1.18 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    1.19 + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.20 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.21 + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.22 + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.23 + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.24 + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.25 + */
    1.26 +
    1.27 +#include "compat.h"
    1.28 +
    1.29 +#ifdef	HAVE_ERR_H
    1.30 +#include <err.h>
    1.31 +#endif /* HAVE_ERR_H */
    1.32 +#include <errno.h>
    1.33 +#include <nettle/base64.h>
    1.34 +#include <string.h>
    1.35 +#include <stdlib.h>
    1.36 +#include <stdio.h>
    1.37 +#include <unistd.h>
    1.38 +
    1.39 +#include "util.h"
    1.40 +
    1.41 +#define	CHUNK_SIZE	128
    1.42 +
    1.43 +static char *
    1.44 +base64_encode(const unsigned char *src, size_t size)
    1.45 +{
    1.46 +	char		*dst = NULL;
    1.47 +	struct base64_encode_ctx b64_ctx;
    1.48 +	size_t		len = 0;
    1.49 +
    1.50 +	dst = xmalloc(BASE64_ENCODE_LENGTH(size) +
    1.51 +	    BASE64_ENCODE_FINAL_LENGTH);
    1.52 +
    1.53 +	base64_encode_init(&b64_ctx);
    1.54 +	len += base64_encode_update(&b64_ctx, (unsigned char *)dst, size, src);
    1.55 +	len += base64_encode_final(&b64_ctx, (unsigned char *)dst);
    1.56 +	dst[len] = '\0';
    1.57 +
    1.58 +	dst = xrealloc(dst, len + 1);
    1.59 +
    1.60 +	return (dst);
    1.61 +}
    1.62 +
    1.63 +static void
    1.64 +set_clipboard(const char *s)
    1.65 +{
    1.66 +	char	*b64 = NULL;
    1.67 +
    1.68 +	b64 = base64_encode((const unsigned char *)s, strlen(s));
    1.69 +	printf("\033]52;c;%s\a\n", b64);
    1.70 +	free(b64);
    1.71 +}
    1.72 +
    1.73 +static void
    1.74 +set_clipboard_dcs_tmux(const char *s)
    1.75 +{
    1.76 +	char	*b64 = NULL;
    1.77 +
    1.78 +	b64 = base64_encode((const unsigned char *)s, strlen(s));
    1.79 +	printf("\033Ptmux;\033\033]52;c;%s\a\033\\", b64);
    1.80 +	free(b64);
    1.81 +}
    1.82 +
    1.83 +static void
    1.84 +set_clipboard_dcs_chunked(const char *s)
    1.85 +{
    1.86 +	char	*b64 = NULL;
    1.87 +	size_t	i;
    1.88 +
    1.89 +	b64 = base64_encode((const unsigned char *)s, strlen(s));
    1.90 +
    1.91 +	printf("\033P\033]52;c;");
    1.92 +	for (i = 0; b64[i] != '\0'; i++) {
    1.93 +		if ((i > 0) && (i % CHUNK_SIZE == 0)) {
    1.94 +			printf("\033\\\033P");
    1.95 +		}
    1.96 +		putchar(b64[i]);
    1.97 +	}
    1.98 +	printf("\a\033\\");
    1.99 +
   1.100 +	free(b64);
   1.101 +}
   1.102 +
   1.103 +static void
   1.104 +usage(void)
   1.105 +{
   1.106 +	fprintf(stderr, "usage: %s\n", getprogname());
   1.107 +}
   1.108 +
   1.109 +int
   1.110 +main(int argc, char *argv[])
   1.111 +{
   1.112 +	int		status = EXIT_FAILURE;
   1.113 +	char		*term;
   1.114 +	void (*set_clipboard_func)(const char *);
   1.115 +	int		errflag = 0;
   1.116 +	int		c;
   1.117 +	ssize_t		len;
   1.118 +	char		*s = NULL;
   1.119 +	size_t		size = 0;
   1.120 +
   1.121 +	setprogname(argv[0]);
   1.122 +
   1.123 +	term = getenv("TERM");
   1.124 +	if (term == NULL) {
   1.125 +		fprintf(stderr, "error: unknown terminal\n");
   1.126 +		goto out;
   1.127 +	} else if (strncmp(term, "xterm", strlen("xterm")) == 0) {
   1.128 +		set_clipboard_func = set_clipboard;
   1.129 +	} else if (strncmp(term, "tmux", strlen("tmux")) == 0) {
   1.130 +		set_clipboard_func = set_clipboard_dcs_tmux;
   1.131 +	} else if (strncmp(term, "screen", strlen("screen")) == 0) {
   1.132 +		if (getenv("TMUX") != NULL) {
   1.133 +			set_clipboard_func = set_clipboard_dcs_tmux;
   1.134 +		} else {
   1.135 +			set_clipboard_func = set_clipboard_dcs_chunked;
   1.136 +		}
   1.137 +	} else {
   1.138 +		fprintf(stderr, "terminal does not support setting "
   1.139 +		    "X11 CLIPBOARD selection\n");
   1.140 +		goto out;
   1.141 +	}
   1.142 +
   1.143 +	while (!errflag && (c = getopt(argc, argv, "h")) != -1) {
   1.144 +		switch (c) {
   1.145 +		case 'h':
   1.146 +			usage();
   1.147 +			exit(EXIT_SUCCESS);
   1.148 +		default:
   1.149 +			errflag = 1;
   1.150 +		}
   1.151 +	}
   1.152 +	if (errflag || (optind + 1 < argc)) {
   1.153 +		usage();
   1.154 +		exit(EXIT_USAGE);
   1.155 +	}
   1.156 +
   1.157 +	errno = 0;
   1.158 +	len = getline(&s, &size, stdin);
   1.159 +	if (len < 0) {
   1.160 +		if (errno == 0) {
   1.161 +			fprintf(stderr, "failed to read line\n");
   1.162 +			goto out;
   1.163 +		} else {
   1.164 +			err(1, "getline");
   1.165 +		}
   1.166 +	}
   1.167 +	if ((len > 0) && (s[len - 1] == '\n')) {
   1.168 +		s[--len] = '\0';
   1.169 +	}
   1.170 +
   1.171 +	set_clipboard_func(s);
   1.172 +
   1.173 +	status = EXIT_SUCCESS;
   1.174 +
   1.175 +out:
   1.176 +	free(s);
   1.177 +
   1.178 +	exit(status);
   1.179 +}