sxhkd-rc

[fork] simple X hotkey daemon (but for the rc shell)
git clone https://hhvn.uk/sxhkd-rc
git clone git://hhvn.uk/sxhkd-rc
Log | Files | Refs | README | LICENSE

helpers.c (2655B)


      1 /* Copyright (c) 2013, Bastien Dejean
      2  * All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are met:
      6  *
      7  * 1. Redistributions of source code must retain the above copyright notice, this
      8  *    list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright notice,
     10  *    this list of conditions and the following disclaimer in the documentation
     11  *    and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
     17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23  */
     24 
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <stdarg.h>
     28 #include <string.h>
     29 #include <unistd.h>
     30 #include <ctype.h>
     31 #include <sys/types.h>
     32 #include <sys/wait.h>
     33 #include "sxhkd.h"
     34 
     35 void warn(char *fmt, ...)
     36 {
     37 	va_list ap;
     38 	va_start(ap, fmt);
     39 	vfprintf(stderr, fmt, ap);
     40 	va_end(ap);
     41 }
     42 
     43 __attribute__((noreturn))
     44 void err(char *fmt, ...)
     45 {
     46 	va_list ap;
     47 	va_start(ap, fmt);
     48 	vfprintf(stderr, fmt, ap);
     49 	va_end(ap);
     50 	exit(EXIT_FAILURE);
     51 }
     52 
     53 void run(char *command, bool sync)
     54 {
     55 	char *cmd[] = {shell, "-c", command, NULL};
     56 	spawn(cmd, sync);
     57 }
     58 
     59 void spawn(char *cmd[], bool sync)
     60 {
     61 	if (fork() == 0) {
     62 		if (dpy != NULL)
     63 			close(xcb_get_file_descriptor(dpy));
     64 		if (sync) {
     65 			execute(cmd);
     66 		} else {
     67 			if (fork() == 0) {
     68 				execute(cmd);
     69 			}
     70 			exit(EXIT_SUCCESS);
     71 		}
     72 	}
     73 	wait(NULL);
     74 }
     75 
     76 void execute(char *cmd[])
     77 {
     78 	setsid();
     79 	if (redir_fd != -1) {
     80 		dup2(redir_fd, STDOUT_FILENO);
     81 		dup2(redir_fd, STDERR_FILENO);
     82 	}
     83 	execvp(cmd[0], cmd);
     84 	err("Spawning failed.\n");
     85 }
     86 
     87 char *lgraph(char *s)
     88 {
     89 	size_t len = strlen(s);
     90 	unsigned int i = 0;
     91 	while (i < len && !isgraph(s[i]))
     92 		i++;
     93 	if (i < len)
     94 		return (s + i);
     95 	else
     96 		return NULL;
     97 }
     98 
     99 char *rgraph(char *s)
    100 {
    101 	int i = strlen(s) - 1;
    102 	while (i >= 0 && !isgraph(s[i]))
    103 		i--;
    104 	if (i >= 0)
    105 		return (s + i);
    106 	else
    107 		return NULL;
    108 }