sselp

simple X selection printer to stdout
git clone git://git.suckless.org/sselp
Log | Files | Refs | README | LICENSE

commit 68c91d7186f7450b7f6fe62b8dbef8d1804e930b
Author: arg@suckless.org <unknown>
Date:   Tue, 10 Oct 2006 09:07:59 +0200

new stuff

Diffstat:
LICENSE | 22++++++++++++++++++++++
Makefile | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
README | 24++++++++++++++++++++++++
config.mk | 25+++++++++++++++++++++++++
spsel.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 204 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,22 @@ +MIT/X Consortium License + +(C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com> +(C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,51 @@ +# spsel - simple print selection +# (C)opyright MMVI Anselm R. Garbe + +include config.mk + +SRC = spsel.c +OBJ = ${SRC:.c=.o} + +all: options spsel + +options: + @echo spsel build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + @echo "LD = ${LD}" + +.c.o: + @echo CC $< + @${CC} -c ${CFLAGS} $< + +${OBJ}: config.mk + +spsel: ${OBJ} + @echo LD $@ + @${LD} -o $@ ${OBJ} ${LDFLAGS} + @strip $@ + +clean: + @echo cleaning + @rm -f spsel ${OBJ} spsel-${VERSION}.tar.gz + +dist: clean + @echo creating dist tarball + @mkdir -p spsel-${VERSION} + @cp -R LICENSE Makefile README config.mk ${SRC} spsel-${VERSION} + @tar -cf spsel-${VERSION}.tar spsel-${VERSION} + @gzip spsel-${VERSION}.tar + @rm -rf spsel-${VERSION} + +install: all + @echo installing executable file to ${DESTDIR}${PREFIX}/bin + @mkdir -p ${DESTDIR}${PREFIX}/bin + @cp -f spsel ${DESTDIR}${PREFIX}/bin + @chmod 755 ${DESTDIR}${PREFIX}/bin/spsel + +uninstall: + @echo removing executable file from ${DESTDIR}${PREFIX}/bin + @rm -f ${DESTDIR}${PREFIX}/bin/spsel + +.PHONY: all options clean dist install uninstall diff --git a/README b/README @@ -0,0 +1,24 @@ +spsel - simple print selection +============================== +Prints X selection to standard out. + + +Requirements +------------ +In order to build spsel you need the Xlib header files. + + +Installation +------------ +Edit config.mk to match your local setup (swarp is installed into +the /usr/local namespace by default). + +Afterwards enter the following command to build and install swarp (if +necessary as root): + + make clean install + + +Running spsel +------------- +Simply invoke the 'spsel' command. diff --git a/config.mk b/config.mk @@ -0,0 +1,25 @@ +# spsel version +VERSION = 0.1 + +# Customize below to fit your system + +# paths +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/share/man + +X11INC = /usr/X11R6/include +X11LIB = /usr/X11R6/lib + +# includes and libs +INCS = -I. -I/usr/include -I${X11INC} +LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 + +# flags +CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\" +LDFLAGS = ${LIBS} +#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\" +#LDFLAGS = -g ${LIBS} + +# compiler and linker +CC = cc +LD = ${CC} diff --git a/spsel.c b/spsel.c @@ -0,0 +1,82 @@ +/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> + * See LICENSE file for license details. + */ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <X11/Xlib.h> +#include <X11/Xatom.h> +#include <X11/Xutil.h> + +/* static */ + +static void * +emallocz(unsigned int size) { + void *res = calloc(1, size); + + if(!res) { + fprintf(stderr, "fatal: could not malloc() %u bytes\n", size); + exit(EXIT_FAILURE); + } + return res; +} + +static unsigned char * +getselection(unsigned long offset, unsigned long *len, unsigned long *remain) { + Display *dpy; + Atom xa_clip_string; + Window w; + XEvent ev; + Atom typeret; + int format; + unsigned char *data; + unsigned char *result = NULL; + + dpy = XOpenDisplay(NULL); + if(!dpy) + return NULL; + xa_clip_string = XInternAtom(dpy, "BLITZ_SEL_STRING", False); + w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200, + 1, CopyFromParent, CopyFromParent); + XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string, + w, CurrentTime); + XFlush(dpy); + XNextEvent(dpy, &ev); + if(ev.type == SelectionNotify && ev.xselection.property != None) { + XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False, + AnyPropertyType, &typeret, &format, len, remain, &data); + if(*len) { + result = emallocz(sizeof(unsigned char) * *len); + memcpy(result, data, *len); + } + XDeleteProperty(dpy, w, ev.xselection.property); + } + XDestroyWindow(dpy, w); + XCloseDisplay(dpy); + return result; +} + +/* extern */ + +int +main(int argc, char **argv) { + unsigned char *data; + unsigned long i, offset, len, remain; + + if((argc > 1) && !strncmp(argv[1], "-v", 3)) { + fputs("spsel-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); + exit(EXIT_SUCCESS); + } + len = offset = remain = 0; + do { + data = getselection(offset, &len, &remain); + for(i = 0; i < len; i++) + putchar(data[i]); + offset += len; + free(data); + } + while(remain); + if(offset) + putchar('\n'); + return 0; +}