aewl

annotate util.c @ 11:ea9c08ec4b48

added gridsel to gridwm
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 13:21:57 +0200
parents d567f430a81d
children 5c078b66347b
rev   line source
garbeam@2 1 /*
garbeam@2 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@2 3 * See LICENSE file for license details.
garbeam@2 4 */
garbeam@2 5
garbeam@2 6 #include <stdarg.h>
garbeam@2 7 #include <stdio.h>
garbeam@2 8 #include <stdlib.h>
garbeam@3 9 #include <string.h>
garbeam@5 10 #include <sys/types.h>
garbeam@5 11 #include <sys/wait.h>
garbeam@5 12 #include <unistd.h>
garbeam@11 13 #include <X11/Xatom.h>
garbeam@5 14
garbeam@5 15 #include "util.h"
garbeam@2 16
garbeam@6 17 static char *shell = NULL;
garbeam@6 18
garbeam@2 19 void
garbeam@2 20 error(char *errstr, ...) {
garbeam@2 21 va_list ap;
garbeam@2 22 va_start(ap, errstr);
garbeam@2 23 vfprintf(stderr, errstr, ap);
garbeam@2 24 va_end(ap);
garbeam@2 25 exit(1);
garbeam@2 26 }
garbeam@2 27
garbeam@3 28 static void
garbeam@3 29 bad_malloc(unsigned int size)
garbeam@3 30 {
garbeam@3 31 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
garbeam@3 32 (int) size);
garbeam@3 33 exit(1);
garbeam@3 34 }
garbeam@3 35
garbeam@3 36 void *
garbeam@3 37 emallocz(unsigned int size)
garbeam@3 38 {
garbeam@3 39 void *res = calloc(1, size);
garbeam@3 40 if(!res)
garbeam@3 41 bad_malloc(size);
garbeam@3 42 return res;
garbeam@3 43 }
garbeam@3 44
garbeam@3 45 void *
garbeam@3 46 emalloc(unsigned int size)
garbeam@3 47 {
garbeam@3 48 void *res = malloc(size);
garbeam@3 49 if(!res)
garbeam@3 50 bad_malloc(size);
garbeam@3 51 return res;
garbeam@3 52 }
garbeam@3 53
garbeam@3 54 void *
garbeam@3 55 erealloc(void *ptr, unsigned int size)
garbeam@3 56 {
garbeam@3 57 void *res = realloc(ptr, size);
garbeam@3 58 if(!res)
garbeam@3 59 bad_malloc(size);
garbeam@3 60 return res;
garbeam@3 61 }
garbeam@3 62
garbeam@3 63 char *
garbeam@3 64 estrdup(const char *str)
garbeam@3 65 {
garbeam@3 66 void *res = strdup(str);
garbeam@3 67 if(!res)
garbeam@3 68 bad_malloc(strlen(str));
garbeam@3 69 return res;
garbeam@3 70 }
garbeam@3 71
garbeam@3 72 void
garbeam@3 73 failed_assert(char *a, char *file, int line)
garbeam@3 74 {
garbeam@3 75 fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
garbeam@3 76 abort();
garbeam@3 77 }
garbeam@3 78
garbeam@3 79 void
garbeam@3 80 swap(void **p1, void **p2)
garbeam@3 81 {
garbeam@3 82 void *tmp = *p1;
garbeam@3 83 *p1 = *p2;
garbeam@3 84 *p2 = tmp;
garbeam@3 85 }
garbeam@5 86
garbeam@5 87 void
garbeam@6 88 spawn(Display *dpy, const char *cmd)
garbeam@5 89 {
garbeam@6 90 if(!shell && !(shell = getenv("SHELL")))
garbeam@6 91 shell = "/bin/sh";
garbeam@6 92
garbeam@6 93 if(!cmd)
garbeam@5 94 return;
garbeam@5 95 if(fork() == 0) {
garbeam@5 96 if(fork() == 0) {
garbeam@5 97 if(dpy)
garbeam@5 98 close(ConnectionNumber(dpy));
garbeam@9 99 setsid();
garbeam@9 100 fprintf(stderr, "gridwm: execlp %s %s -c %s", shell, shell, cmd);
garbeam@9 101 execlp(shell, shell, "-c", cmd, NULL);
garbeam@9 102 fprintf(stderr, "gridwm: execlp %s", cmd);
garbeam@5 103 perror(" failed");
garbeam@5 104 }
garbeam@5 105 exit (0);
garbeam@5 106 }
garbeam@5 107 wait(0);
garbeam@5 108 }
garbeam@6 109
garbeam@6 110 void
garbeam@6 111 pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
garbeam@6 112 {
garbeam@6 113 unsigned int l, n;
garbeam@6 114 int pfd[2];
garbeam@6 115
garbeam@6 116 if(!shell && !(shell = getenv("SHELL")))
garbeam@6 117 shell = "/bin/sh";
garbeam@6 118
garbeam@6 119 if(!cmd)
garbeam@6 120 return;
garbeam@6 121
garbeam@6 122 if(pipe(pfd) == -1) {
garbeam@6 123 perror("pipe");
garbeam@6 124 exit(1);
garbeam@6 125 }
garbeam@6 126
garbeam@6 127 if(fork() == 0) {
garbeam@6 128 if(dpy)
garbeam@6 129 close(ConnectionNumber(dpy));
garbeam@9 130 setsid();
garbeam@6 131 dup2(pfd[1], STDOUT_FILENO);
garbeam@6 132 close(pfd[0]);
garbeam@6 133 close(pfd[1]);
garbeam@9 134 execlp(shell, shell, "-c", cmd, NULL);
garbeam@9 135 fprintf(stderr, "gridwm: execlp %s", cmd);
garbeam@6 136 perror(" failed");
garbeam@6 137 }
garbeam@6 138 else {
garbeam@6 139 n = 0;
garbeam@6 140 close(pfd[1]);
garbeam@6 141 while(l > n) {
garbeam@6 142 if((l = read(pfd[0], buf + n, len - n)) < 1)
garbeam@6 143 break;
garbeam@6 144 n += l;
garbeam@6 145 }
garbeam@6 146 close(pfd[0]);
garbeam@6 147 buf[n - 1] = 0;
garbeam@6 148 }
garbeam@6 149 wait(0);
garbeam@6 150 }
garbeam@11 151
garbeam@11 152
garbeam@11 153 unsigned char *
garbeam@11 154 getselection(unsigned long offset, unsigned long *len, unsigned long *remain)
garbeam@11 155 {
garbeam@11 156 Display *dpy;
garbeam@11 157 Atom xa_clip_string;
garbeam@11 158 Window w;
garbeam@11 159 XEvent ev;
garbeam@11 160 Atom typeret;
garbeam@11 161 int format;
garbeam@11 162 unsigned char *data;
garbeam@11 163 unsigned char *result = NULL;
garbeam@11 164
garbeam@11 165 dpy = XOpenDisplay(0);
garbeam@11 166 if(!dpy)
garbeam@11 167 return NULL;
garbeam@11 168 xa_clip_string = XInternAtom(dpy, "_SEL_STRING", False);
garbeam@11 169 w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 200, 200,
garbeam@11 170 1, CopyFromParent, CopyFromParent);
garbeam@11 171 XConvertSelection(dpy, XA_PRIMARY, XA_STRING, xa_clip_string,
garbeam@11 172 w, CurrentTime);
garbeam@11 173 XFlush(dpy);
garbeam@11 174 XNextEvent(dpy, &ev);
garbeam@11 175 if(ev.type == SelectionNotify && ev.xselection.property != None) {
garbeam@11 176 XGetWindowProperty(dpy, w, ev.xselection.property, offset, 4096L, False,
garbeam@11 177 AnyPropertyType, &typeret, &format, len, remain, &data);
garbeam@11 178 if(*len) {
garbeam@11 179 result = emalloc(sizeof(unsigned char) * *len);
garbeam@11 180 memcpy(result, data, *len);
garbeam@11 181 }
garbeam@11 182 XDeleteProperty(dpy, w, ev.xselection.property);
garbeam@11 183 }
garbeam@11 184 XDestroyWindow(dpy, w);
garbeam@11 185 XCloseDisplay(dpy);
garbeam@11 186 return result;
garbeam@11 187 }