lchat

A line oriented chat front end for ii.
git clone git://git.suckless.org/lchat
Log | Files | Refs | README

util.c (1700B)


      1 /*
      2  * Copyright (c) 2018-2023 Jan Klemkow <j.klemkow@wemelug.de>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include <stdarg.h>
     18 #include <stdbool.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #include <unistd.h>
     23 
     24 void
     25 die(const char *fmt, ...)
     26 {
     27 	va_list ap;
     28 
     29 	va_start(ap, fmt);
     30 	vfprintf(stderr, fmt, ap);
     31 	va_end(ap);
     32 
     33 	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
     34 		fputc(' ', stderr);
     35 		perror(NULL);
     36 	} else {
     37 		fputc('\n', stderr);
     38 	}
     39 
     40 	exit(EXIT_FAILURE);
     41 }
     42 
     43 bool
     44 bell_match(const char *str, const char *regex_file)
     45 {
     46 	FILE *fh = NULL;
     47 	char cmd[BUFSIZ];
     48 
     49 	if (access(regex_file, R_OK) == -1)
     50 		return true;
     51 
     52 	snprintf(cmd, sizeof cmd, "exec grep -qf %s", regex_file);
     53 
     54 	if ((fh = popen(cmd, "w")) == NULL)
     55 		die("popen:");
     56 
     57 	if (fputs(str, fh) == EOF)
     58 		die("fputs:");
     59 
     60 	if (pclose(fh) == 0)
     61 		return true;
     62 
     63 	return false;
     64 }
     65 
     66 void
     67 set_title(const char *term, char *title)
     68 {
     69 	if (strncmp(term, "screen", 6) == 0)
     70 		printf("\033k%s\033\\", title);
     71 	else
     72 		printf("\033]0;%s\a", title);
     73 }