wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

util.h (3967B)


      1 /* Copyright ©2007-2010 Kris Maglione <maglione.k at Gmail>
      2  * See LICENSE file for license details.
      3  */
      4 
      5 #include <stuff/geom.h>
      6 #include <langinfo.h>
      7 #include <stdarg.h>
      8 #include <bio.h>
      9 #include <fmt.h>
     10 #include <regexp9.h>
     11 
     12 /* Types */
     13 
     14 typedef struct Regex Regex;
     15 
     16 struct Regex {
     17 	char*	regex;
     18 	Reprog*	regc;
     19 };
     20 
     21 enum {
     22 	CLeft = 1<<0,
     23 	CCenter = 1<<1,
     24 	CRight = 1<<2,
     25 };
     26 
     27 enum {
     28 	GInvert = 1<<0,
     29 };
     30 
     31 #define utf8locale() (!strcmp(nl_langinfo(CODESET), "UTF-8"))
     32 
     33 #ifdef VARARGCK
     34 # pragma varargck	argpos	_die	3
     35 # pragma varargck	argpos	fatal	1
     36 # pragma varargck	argpos	sxprint	1
     37 #endif
     38 
     39 #define strlcat stuff_strlcat
     40 #define strcasestr stuff_strcasestr
     41 
     42 
     43 int	Blprint(Biobuf*, const char*, ...);
     44 int	Bvlprint(Biobuf*, const char*, va_list);
     45 void	_die(char*, int, char*, ...);
     46 void	backtrace(char*);
     47 void	closeexec(int);
     48 char**	comm(int, char**, char**);
     49 int	doublefork(void);
     50 void*	emalloc(uint);
     51 void*	emallocz(uint);
     52 void*	erealloc(void*, uint);
     53 char*	estrdup(const char*);
     54 char*	estrndup(const char*, uint);
     55 void	fatal(const char*, ...);
     56 Fmt	fmtbuf(char*, int);
     57 void*	freelater(void*);
     58 int	getbase(const char**, long*);
     59 bool	getint(const char*, int*);
     60 bool	getlong(const char*, long*);
     61 bool	getulong(const char*, ulong*);
     62 void	grep(char**, Reprog*, int);
     63 char*	join(char**, char*, Fmt*);
     64 int	localefmt(Fmt*);
     65 void	localefmtinstall(void);
     66 int	localelen(char*, char*);
     67 int	lprint(int, const char*, ...);
     68 int	max(int, int);
     69 int	min(int, int);
     70 uvlong	nsec(void);
     71 char*	pathsearch(const char*, const char*, bool);
     72 void	refree(Regex*);
     73 void	reinit(Regex*, char*);
     74 int	spawn3(int[3], const char*, char*[]);
     75 int	spawn3l(int[3], const char*, ...);
     76 uint	stokenize(char**, uint, char*, char*);
     77 char*	strcasestr(const char*, const char*);
     78 char*	strend(char*, int);
     79 uint	strlcat(char*, const char*, uint);
     80 int	strlcatprint(char*, int, const char*, ...);
     81 char*	sxprint(const char*, ...);
     82 uint	tokenize(char**, uint, char*, char);
     83 void	trim(char *str, const char *chars);
     84 void	uniq(char**);
     85 int	unmask(Fmt*, long, char**, long);
     86 int	unquote(char*, char*[], int);
     87 int	utflcpy(char*, const char*, int);
     88 int	vlprint(int, const char*, va_list);
     89 char*	vsxprint(const char*, va_list);
     90 
     91 extern char*	_buffer;
     92 extern char	buffer[8092];
     93 extern char*	const _buf_end;
     94 #define bufclear() \
     95 	BLOCK( _buffer = buffer; _buffer[0] = '\0' )
     96 #define bufprint(...) \
     97 	_buffer = seprint(_buffer, _buf_end, __VA_ARGS__)
     98 
     99 #define die(...) \
    100 	_die(__FILE__, __LINE__, __VA_ARGS__)
    101 
    102 char *argv0;
    103 #undef ARGBEGIN
    104 #undef ARGEND
    105 #undef ARGF
    106 #undef EARGF
    107 #define ARGBEGIN \
    108 		int _argtmp=0, _inargv; char *_argv=nil;        \
    109 		if(!argv0) argv0=*argv; argv++, argc--;         \
    110 		_inargv=1; USED(_inargv);		        \
    111 		while(argc && argv[0][0] == '-') {              \
    112 			_argv=&argv[0][1]; argv++; argc--;      \
    113 			if(_argv[0] == '-' && _argv[1] == '\0') \
    114 				break; \
    115 			while(*_argv) switch(*_argv++)
    116 #define ARGEND }_inargv=0;USED(_argtmp, _argv, _inargv)
    117 
    118 #define EARGF(f) ((_inargv && *_argv) ? \
    119 			(_argtmp=strlen(_argv), _argv+=_argtmp, _argv-_argtmp) \
    120 			: ((argc > 0) ? \
    121 				(--argc, ++argv, _used(argc), *(argv-1)) \
    122 				: ((f), (char*)0)))
    123 #define ARGF() EARGF(_used(0))
    124 
    125 /* map.c */
    126 typedef struct Map Map;
    127 typedef struct MapEnt MapEnt;
    128 
    129 struct Map {
    130 	MapEnt**bucket;
    131 	uint	nhash;
    132 	uint	nmemb;
    133 };
    134 
    135 void**	hash_get(Map*, const char*, bool create);
    136 void*	hash_rm(Map*, const char*);
    137 void**	map_get(Map*, ulong, bool create);
    138 void*	map_rm(Map*, ulong);
    139 
    140 /* Yuck. */
    141 #define VECTOR(type, nam, c) \
    142 typedef struct Vector_##nam Vector_##nam;      \
    143 struct Vector_##nam {                          \
    144 	type*	ary;                           \
    145 	long	n;                             \
    146 	long	size;                          \
    147 };                                             \
    148 void	vector_##c##free(Vector_##nam*);       \
    149 void	vector_##c##init(Vector_##nam*);       \
    150 void	vector_##c##push(Vector_##nam*, type); \
    151 
    152 VECTOR(long, long, l)
    153 VECTOR(Rectangle, rect, r)
    154 VECTOR(void*, ptr, p)
    155 #undef VECTOR
    156