wmii/include/stuff/util.h

157 lines
3.9 KiB
C
Raw Normal View History

/* Copyright ©2007-2010 Kris Maglione <maglione.k at Gmail>
2010-05-22 06:52:47 +04:00
* See LICENSE file for license details.
*/
#include <stuff/geom.h>
2010-06-06 13:07:24 +04:00
#include <langinfo.h>
2010-05-22 06:52:47 +04:00
#include <stdarg.h>
2010-06-06 13:07:24 +04:00
#include <bio.h>
#include <fmt.h>
2010-05-22 06:52:47 +04:00
#include <regexp9.h>
/* Types */
typedef struct Regex Regex;
struct Regex {
char* regex;
Reprog* regc;
};
enum {
CLeft = 1<<0,
CCenter = 1<<1,
CRight = 1<<2,
};
2010-05-27 11:58:02 +04:00
2010-05-22 06:52:47 +04:00
enum {
GInvert = 1<<0,
};
2010-06-06 13:07:24 +04:00
#define utf8locale() (!strcmp(nl_langinfo(CODESET), "UTF-8"))
2010-05-22 06:52:47 +04:00
#ifdef VARARGCK
# pragma varargck argpos _die 3
# pragma varargck argpos fatal 1
# pragma varargck argpos sxprint 1
#endif
#define strlcat stuff_strlcat
#define strcasestr stuff_strcasestr
2010-06-12 07:48:29 +04:00
2010-06-06 13:07:24 +04:00
int Blprint(Biobuf*, const char*, ...);
int Bvlprint(Biobuf*, const char*, va_list);
2010-05-22 06:52:47 +04:00
void _die(char*, int, char*, ...);
void backtrace(char*);
void closeexec(int);
char** comm(int, char**, char**);
int doublefork(void);
void* emalloc(uint);
void* emallocz(uint);
void* erealloc(void*, uint);
char* estrdup(const char*);
char* estrndup(const char*, uint);
void fatal(const char*, ...);
Fmt fmtbuf(char*, int);
2010-05-22 06:52:47 +04:00
void* freelater(void*);
2010-06-06 13:07:24 +04:00
int getbase(const char**, long*);
bool getint(const char*, int*);
bool getlong(const char*, long*);
bool getulong(const char*, ulong*);
2010-05-22 06:52:47 +04:00
void grep(char**, Reprog*, int);
char* join(char**, char*, Fmt*);
2010-06-06 13:07:24 +04:00
int localefmt(Fmt*);
void localefmtinstall(void);
int localelen(char*, char*);
int lprint(int, const char*, ...);
2010-05-22 06:52:47 +04:00
int max(int, int);
int min(int, int);
uvlong nsec(void);
2010-05-22 06:52:47 +04:00
char* pathsearch(const char*, const char*, bool);
void refree(Regex*);
void reinit(Regex*, char*);
int spawn3(int[3], const char*, char*[]);
int spawn3l(int[3], const char*, ...);
uint stokenize(char**, uint, char*, char*);
char* strcasestr(const char*, const char*);
2010-06-06 13:07:24 +04:00
char* strend(char*, int);
2010-05-22 06:52:47 +04:00
uint strlcat(char*, const char*, uint);
int strlcatprint(char*, int, const char*, ...);
char* sxprint(const char*, ...);
uint tokenize(char**, uint, char*, char);
2010-06-05 00:46:25 +04:00
void trim(char *str, const char *chars);
2010-05-22 06:52:47 +04:00
void uniq(char**);
2010-06-12 07:48:29 +04:00
int unmask(Fmt*, long, char**, long);
2010-05-22 06:52:47 +04:00
int unquote(char*, char*[], int);
int utflcpy(char*, const char*, int);
2010-06-06 13:07:24 +04:00
int vlprint(int, const char*, va_list);
2010-05-22 06:52:47 +04:00
char* vsxprint(const char*, va_list);
2010-10-08 00:31:41 +04:00
extern char* _buffer;
extern char buffer[8092];
extern char* const _buf_end;
2010-05-22 06:52:47 +04:00
#define bufclear() \
BLOCK( _buffer = buffer; _buffer[0] = '\0' )
#define bufprint(...) \
_buffer = seprint(_buffer, _buf_end, __VA_ARGS__)
#define die(...) \
_die(__FILE__, __LINE__, __VA_ARGS__)
char *argv0;
#undef ARGBEGIN
#undef ARGEND
#undef ARGF
#undef EARGF
#define ARGBEGIN \
int _argtmp=0, _inargv; char *_argv=nil; \
if(!argv0) argv0=*argv; argv++, argc--; \
_inargv=1; USED(_inargv); \
while(argc && argv[0][0] == '-') { \
_argv=&argv[0][1]; argv++; argc--; \
if(_argv[0] == '-' && _argv[1] == '\0') \
break; \
while(*_argv) switch(*_argv++)
#define ARGEND }_inargv=0;USED(_argtmp, _argv, _inargv)
#define EARGF(f) ((_inargv && *_argv) ? \
(_argtmp=strlen(_argv), _argv+=_argtmp, _argv-_argtmp) \
: ((argc > 0) ? \
(--argc, ++argv, _used(argc), *(argv-1)) \
: ((f), (char*)0)))
#define ARGF() EARGF(_used(0))
/* map.c */
typedef struct Map Map;
typedef struct MapEnt MapEnt;
struct Map {
MapEnt**bucket;
uint nhash;
uint nmemb;
2010-05-22 06:52:47 +04:00
};
void** hash_get(Map*, const char*, bool create);
void* hash_rm(Map*, const char*);
void** map_get(Map*, ulong, bool create);
void* map_rm(Map*, ulong);
/* Yuck. */
#define VECTOR(type, nam, c) \
typedef struct Vector_##nam Vector_##nam; \
struct Vector_##nam { \
type* ary; \
long n; \
long size; \
}; \
void vector_##c##free(Vector_##nam*); \
void vector_##c##init(Vector_##nam*); \
void vector_##c##push(Vector_##nam*, type); \
VECTOR(long, long, l)
VECTOR(Rectangle, rect, r)
VECTOR(void*, ptr, p)
2010-08-10 13:46:00 +04:00
#undef VECTOR
2010-05-22 06:52:47 +04:00