1da2e3ebdSchin /*********************************************************************** 2da2e3ebdSchin * * 3da2e3ebdSchin * This software is part of the ast package * 4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property * 5da2e3ebdSchin * and is licensed under the * 6da2e3ebdSchin * Common Public License, Version 1.0 * 77c2fbfb3SApril Chin * by AT&T Intellectual Property * 8da2e3ebdSchin * * 9da2e3ebdSchin * A copy of the License is available at * 10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt * 11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12da2e3ebdSchin * * 13da2e3ebdSchin * Information and Software Systems Research * 14da2e3ebdSchin * AT&T Research * 15da2e3ebdSchin * Florham Park NJ * 16da2e3ebdSchin * * 17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> * 18da2e3ebdSchin * David Korn <dgk@research.att.com> * 19da2e3ebdSchin * Phong Vo <kpv@research.att.com> * 20da2e3ebdSchin * * 21da2e3ebdSchin ***********************************************************************/ 22da2e3ebdSchin #pragma prototyped 23da2e3ebdSchin /* 24da2e3ebdSchin * Advanced Software Technology Library 25da2e3ebdSchin * AT&T Research 26da2e3ebdSchin * 27da2e3ebdSchin * std + posix + ast 28da2e3ebdSchin */ 29da2e3ebdSchin 30da2e3ebdSchin #ifndef _AST_H 31da2e3ebdSchin #define _AST_H 32da2e3ebdSchin 33da2e3ebdSchin #include <ast_version.h> 34da2e3ebdSchin 35da2e3ebdSchin #ifndef _AST_STD_H 36da2e3ebdSchin #include <ast_std.h> 37da2e3ebdSchin #endif 38da2e3ebdSchin 39da2e3ebdSchin #ifndef _SFIO_H 40da2e3ebdSchin #include <sfio.h> 41da2e3ebdSchin #endif 42da2e3ebdSchin 43da2e3ebdSchin #ifndef ast 44da2e3ebdSchin #define ast _ast_info 45da2e3ebdSchin #endif 46da2e3ebdSchin 47da2e3ebdSchin #ifndef PATH_MAX 48da2e3ebdSchin #define PATH_MAX 1024 49da2e3ebdSchin #endif 50da2e3ebdSchin 51da2e3ebdSchin /* 52da2e3ebdSchin * workaround botched headers that assume <stdio.h> 53da2e3ebdSchin */ 54da2e3ebdSchin 55da2e3ebdSchin #ifndef FILE 56da2e3ebdSchin #ifndef _SFIO_H 57da2e3ebdSchin struct _sfio_s; 58da2e3ebdSchin #endif 59da2e3ebdSchin #define FILE struct _sfio_s 60da2e3ebdSchin #ifndef __FILE_typedef 61da2e3ebdSchin #define __FILE_typedef 1 62da2e3ebdSchin #endif 63da2e3ebdSchin #ifndef _FILEDEFED 64da2e3ebdSchin #define _FILEDEFED 1 65da2e3ebdSchin #endif 66da2e3ebdSchin #endif 67da2e3ebdSchin 68da2e3ebdSchin /* 69da2e3ebdSchin * exit() support -- this matches shell exit codes 70da2e3ebdSchin */ 71da2e3ebdSchin 72da2e3ebdSchin #define EXIT_BITS 8 /* # exit status bits */ 73da2e3ebdSchin 74da2e3ebdSchin #define EXIT_USAGE 2 /* usage exit code */ 75da2e3ebdSchin #define EXIT_QUIT ((1<<(EXIT_BITS))-1) /* parent should quit */ 76da2e3ebdSchin #define EXIT_NOTFOUND ((1<<(EXIT_BITS-1))-1) /* command not found */ 77da2e3ebdSchin #define EXIT_NOEXEC ((1<<(EXIT_BITS-1))-2) /* other exec error */ 78da2e3ebdSchin 79da2e3ebdSchin #define EXIT_CODE(x) ((x)&((1<<EXIT_BITS)-1)) 80da2e3ebdSchin #define EXIT_CORE(x) (EXIT_CODE(x)|(1<<EXIT_BITS)|(1<<(EXIT_BITS-1))) 81da2e3ebdSchin #define EXIT_TERM(x) (EXIT_CODE(x)|(1<<EXIT_BITS)) 82da2e3ebdSchin 83da2e3ebdSchin /* 84da2e3ebdSchin * NOTE: for compatibility the following work for EXIT_BITS={7,8} 85da2e3ebdSchin */ 86da2e3ebdSchin 87da2e3ebdSchin #define EXIT_STATUS(x) (((x)&((1<<(EXIT_BITS-2))-1))?(x):EXIT_CODE((x)>>EXIT_BITS)) 88da2e3ebdSchin 89da2e3ebdSchin #define EXITED_CORE(x) (((x)&((1<<EXIT_BITS)|(1<<(EXIT_BITS-1))))==((1<<EXIT_BITS)|(1<<(EXIT_BITS-1)))||((x)&((1<<(EXIT_BITS-1))|(1<<(EXIT_BITS-2))))==((1<<(EXIT_BITS-1))|(1<<(EXIT_BITS-2)))) 90da2e3ebdSchin #define EXITED_TERM(x) ((x)&((1<<EXIT_BITS)|(1<<(EXIT_BITS-1)))) 91da2e3ebdSchin 92da2e3ebdSchin /* 93da2e3ebdSchin * astconflist() flags 94da2e3ebdSchin */ 95da2e3ebdSchin 96da2e3ebdSchin #define ASTCONF_parse 0x0001 97da2e3ebdSchin #define ASTCONF_write 0x0002 98da2e3ebdSchin #define ASTCONF_read 0x0004 99da2e3ebdSchin #define ASTCONF_lower 0x0008 100da2e3ebdSchin #define ASTCONF_base 0x0010 101da2e3ebdSchin #define ASTCONF_defined 0x0020 102da2e3ebdSchin #define ASTCONF_quote 0x0040 103da2e3ebdSchin #define ASTCONF_table 0x0080 104da2e3ebdSchin #define ASTCONF_matchcall 0x0100 105da2e3ebdSchin #define ASTCONF_matchname 0x0200 106da2e3ebdSchin #define ASTCONF_matchstandard 0x0400 107da2e3ebdSchin #define ASTCONF_error 0x0800 108da2e3ebdSchin #define ASTCONF_system 0x1000 109da2e3ebdSchin #define ASTCONF_AST 0x2000 110da2e3ebdSchin 111da2e3ebdSchin /* 112da2e3ebdSchin * pathcanon() flags 113da2e3ebdSchin */ 114da2e3ebdSchin 115da2e3ebdSchin #define PATH_PHYSICAL 01 116da2e3ebdSchin #define PATH_DOTDOT 02 117da2e3ebdSchin #define PATH_EXISTS 04 118da2e3ebdSchin #define PATH_VERIFIED(n) (((n)&01777)<<5) 119da2e3ebdSchin 120da2e3ebdSchin /* 121da2e3ebdSchin * pathaccess() flags 122da2e3ebdSchin */ 123da2e3ebdSchin 124da2e3ebdSchin #define PATH_READ 004 125da2e3ebdSchin #define PATH_WRITE 002 126da2e3ebdSchin #define PATH_EXECUTE 001 127da2e3ebdSchin #define PATH_REGULAR 010 128da2e3ebdSchin #define PATH_ABSOLUTE 020 129da2e3ebdSchin 130da2e3ebdSchin /* 131da2e3ebdSchin * touch() flags 132da2e3ebdSchin */ 133da2e3ebdSchin 134da2e3ebdSchin #define PATH_TOUCH_CREATE 01 135da2e3ebdSchin #define PATH_TOUCH_VERBATIM 02 136da2e3ebdSchin 137da2e3ebdSchin /* 138da2e3ebdSchin * pathcheck() info 139da2e3ebdSchin */ 140da2e3ebdSchin 141da2e3ebdSchin typedef struct 142da2e3ebdSchin { 143da2e3ebdSchin unsigned long date; 144da2e3ebdSchin char* feature; 145da2e3ebdSchin char* host; 146da2e3ebdSchin char* user; 147da2e3ebdSchin } Pathcheck_t; 148da2e3ebdSchin 149da2e3ebdSchin /* 150da2e3ebdSchin * strgrpmatch() flags 151da2e3ebdSchin */ 152da2e3ebdSchin 153da2e3ebdSchin #define STR_MAXIMAL 01 /* maximal match */ 154da2e3ebdSchin #define STR_LEFT 02 /* implicit left anchor */ 155da2e3ebdSchin #define STR_RIGHT 04 /* implicit right anchor */ 156da2e3ebdSchin #define STR_ICASE 010 /* ignore case */ 157da2e3ebdSchin #define STR_GROUP 020 /* (|&) inside [@|&](...) only */ 158da2e3ebdSchin 159da2e3ebdSchin /* 160da2e3ebdSchin * fmtquote() flags 161da2e3ebdSchin */ 162da2e3ebdSchin 163da2e3ebdSchin #define FMT_ALWAYS 0x01 /* always quote */ 164da2e3ebdSchin #define FMT_ESCAPED 0x02 /* already escaped */ 165da2e3ebdSchin #define FMT_SHELL 0x04 /* escape $ ` too */ 166da2e3ebdSchin #define FMT_WIDE 0x08 /* don't escape 8 bit chars */ 167da2e3ebdSchin #define FMT_PARAM 0x10 /* disable FMT_SHELL ${$( quote */ 168da2e3ebdSchin 169da2e3ebdSchin /* 170da2e3ebdSchin * multibyte macros 171da2e3ebdSchin */ 172da2e3ebdSchin 173da2e3ebdSchin #define mbmax() (ast.mb_cur_max) 174da2e3ebdSchin #define mberr() (ast.tmp_int<0) 175da2e3ebdSchin 176da2e3ebdSchin #define mbcoll() (ast.mb_xfrm!=0) 177da2e3ebdSchin #define mbwide() (mbmax()>1) 178da2e3ebdSchin 17934f9b3eeSRoland Mainz #define mbchar(p) (mbwide()?((ast.tmp_int=(*ast.mb_towc)(&ast.tmp_wchar,(char*)(p),mbmax()))>0?((p+=ast.tmp_int),ast.tmp_wchar):(p+=ast.mb_sync+1,ast.tmp_int)):(*(unsigned char*)(p++))) 180da2e3ebdSchin #define mbinit() (mbwide()?(*ast.mb_towc)((wchar_t*)0,(char*)0,mbmax()):0) 181da2e3ebdSchin #define mbsize(p) (mbwide()?(*ast.mb_len)((char*)(p),mbmax()):((p),1)) 182da2e3ebdSchin #define mbconv(s,w) (ast.mb_conv?(*ast.mb_conv)(s,w):((*(s)=(w)),1)) 183da2e3ebdSchin #define mbwidth(w) (ast.mb_width&&((ast.tmp_int=(*ast.mb_width)(w))>=0||(w)>UCHAR_MAX)?ast.tmp_int:1) 184da2e3ebdSchin #define mbxfrm(t,f,n) (mbcoll()?(*ast.mb_xfrm)((char*)(t),(char*)(f),n):0) 185da2e3ebdSchin 186da2e3ebdSchin /* 187da2e3ebdSchin * common macros 188da2e3ebdSchin */ 189da2e3ebdSchin 190da2e3ebdSchin #define elementsof(x) (sizeof(x)/sizeof(x[0])) 191da2e3ebdSchin #define integralof(x) (((char*)(x))-((char*)0)) 192da2e3ebdSchin #define newof(p,t,n,x) ((p)?(t*)realloc((char*)(p),sizeof(t)*(n)+(x)):(t*)calloc(1,sizeof(t)*(n)+(x))) 193da2e3ebdSchin #define oldof(p,t,n,x) ((p)?(t*)realloc((char*)(p),sizeof(t)*(n)+(x)):(t*)malloc(sizeof(t)*(n)+(x))) 194da2e3ebdSchin #define pointerof(x) ((void*)((char*)0+(x))) 195da2e3ebdSchin #define roundof(x,y) (((x)+(y)-1)&~((y)-1)) 196da2e3ebdSchin #define ssizeof(x) ((int)sizeof(x)) 197da2e3ebdSchin 198da2e3ebdSchin #define streq(a,b) (*(a)==*(b)&&!strcmp(a,b)) 199da2e3ebdSchin #define strneq(a,b,n) (*(a)==*(b)&&!strncmp(a,b,n)) 200da2e3ebdSchin #define strsignal(s) fmtsignal(s) 201da2e3ebdSchin 202da2e3ebdSchin #if defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus) 203da2e3ebdSchin #define NiL 0 204da2e3ebdSchin #define NoP(x) (void)(x) 205da2e3ebdSchin #else 206da2e3ebdSchin #define NiL ((char*)0) 207da2e3ebdSchin #define NoP(x) (&x,1) 208da2e3ebdSchin #endif 209da2e3ebdSchin 210da2e3ebdSchin #if !defined(NoF) 211da2e3ebdSchin #define NoF(x) void _DATA_ ## x () {} 212da2e3ebdSchin #if !defined(_DATA_) 213da2e3ebdSchin #define _DATA_ 214da2e3ebdSchin #endif 215da2e3ebdSchin #endif 216da2e3ebdSchin 217da2e3ebdSchin #if !defined(NoN) 218da2e3ebdSchin #define NoN(x) void _STUB_ ## x () {} 219da2e3ebdSchin #if !defined(_STUB_) 220da2e3ebdSchin #define _STUB_ 221da2e3ebdSchin #endif 222da2e3ebdSchin #endif 223da2e3ebdSchin 224da2e3ebdSchin #define NOT_USED(x) NoP(x) 225da2e3ebdSchin 226da2e3ebdSchin typedef int (*Error_f)(void*, void*, int, ...); 227da2e3ebdSchin 228da2e3ebdSchin typedef int (*Ast_confdisc_f)(const char*, const char*, const char*); 229da2e3ebdSchin typedef int (*Strcmp_context_f)(const char*, const char*, void*); 230da2e3ebdSchin typedef int (*Strcmp_f)(const char*, const char*); 231da2e3ebdSchin 232da2e3ebdSchin #if _BLD_ast && defined(__EXPORT__) 233da2e3ebdSchin #define extern __EXPORT__ 234da2e3ebdSchin #endif 235da2e3ebdSchin 236da2e3ebdSchin extern char* astgetconf(const char*, const char*, const char*, int, Error_f); 237da2e3ebdSchin extern char* astconf(const char*, const char*, const char*); 238da2e3ebdSchin extern Ast_confdisc_f astconfdisc(Ast_confdisc_f); 239da2e3ebdSchin extern void astconflist(Sfio_t*, const char*, int, const char*); 240da2e3ebdSchin extern off_t astcopy(int, int, off_t); 241da2e3ebdSchin extern int astlicense(char*, int, char*, char*, int, int, int); 242da2e3ebdSchin extern int astquery(int, const char*, ...); 243da2e3ebdSchin extern void astwinsize(int, int*, int*); 244da2e3ebdSchin 245da2e3ebdSchin extern ssize_t base64encode(const void*, size_t, void**, void*, size_t, void**); 246da2e3ebdSchin extern ssize_t base64decode(const void*, size_t, void**, void*, size_t, void**); 247da2e3ebdSchin extern int chresc(const char*, char**); 248da2e3ebdSchin extern int chrtoi(const char*); 249da2e3ebdSchin extern int eaccess(const char*, int); 250da2e3ebdSchin extern char* fmtbase(long, int, int); 251da2e3ebdSchin extern char* fmtbasell(intmax_t, int, int); 252da2e3ebdSchin #define fmtbase(a,b,c) fmtbasell((intmax_t)(a),b,c) /* until 2003-09-01 */ 253da2e3ebdSchin extern char* fmtbuf(size_t); 254da2e3ebdSchin extern char* fmtclock(Sfulong_t); 255da2e3ebdSchin extern char* fmtelapsed(unsigned long, int); 256da2e3ebdSchin extern char* fmterror(int); 257da2e3ebdSchin extern char* fmtesc(const char*); 258da2e3ebdSchin extern char* fmtesq(const char*, const char*); 259da2e3ebdSchin extern char* fmtident(const char*); 260da2e3ebdSchin extern char* fmtip4(uint32_t, int); 261da2e3ebdSchin extern char* fmtfmt(const char*); 262da2e3ebdSchin extern char* fmtgid(int); 263da2e3ebdSchin extern char* fmtmatch(const char*); 264da2e3ebdSchin extern char* fmtmode(int, int); 265da2e3ebdSchin extern char* fmtnesq(const char*, const char*, size_t); 266da2e3ebdSchin extern char* fmtnum(unsigned long, int); 267da2e3ebdSchin extern char* fmtperm(int); 268da2e3ebdSchin extern char* fmtquote(const char*, const char*, const char*, size_t, int); 269da2e3ebdSchin extern char* fmtre(const char*); 270da2e3ebdSchin extern char* fmtscale(Sfulong_t, int); 271da2e3ebdSchin extern char* fmtsignal(int); 272da2e3ebdSchin extern char* fmttime(const char*, time_t); 273da2e3ebdSchin extern char* fmtuid(int); 274da2e3ebdSchin extern char* fmtversion(unsigned long); 275da2e3ebdSchin extern void* memdup(const void*, size_t); 276da2e3ebdSchin extern void memfatal(void); 277da2e3ebdSchin extern unsigned int memhash(const void*, int); 278da2e3ebdSchin extern unsigned long memsum(const void*, int, unsigned long); 279da2e3ebdSchin extern char* pathaccess(char*, const char*, const char*, const char*, int); 280da2e3ebdSchin extern char* pathbin(void); 281da2e3ebdSchin extern char* pathcanon(char*, int); 282da2e3ebdSchin extern char* pathcat(char*, const char*, int, const char*, const char*); 283da2e3ebdSchin extern int pathcd(const char*, const char*); 284da2e3ebdSchin extern int pathcheck(const char*, const char*, Pathcheck_t*); 285da2e3ebdSchin extern int pathexists(char*, int); 286da2e3ebdSchin extern char* pathfind(const char*, const char*, const char*, char*, size_t); 287da2e3ebdSchin extern int pathgetlink(const char*, char*, int); 288da2e3ebdSchin extern int pathinclude(const char*); 289da2e3ebdSchin extern char* pathkey(char*, char*, const char*, const char*, const char*); 290da2e3ebdSchin extern size_t pathnative(const char*, char*, size_t); 291da2e3ebdSchin extern char* pathpath(char*, const char*, const char*, int); 292da2e3ebdSchin extern size_t pathposix(const char*, char*, size_t); 293da2e3ebdSchin extern char* pathprobe(char*, char*, const char*, const char*, const char*, int); 29434f9b3eeSRoland Mainz extern size_t pathprog(const char*, char*, size_t); 295da2e3ebdSchin extern char* pathrepl(char*, const char*, const char*); 296da2e3ebdSchin extern int pathsetlink(const char*, const char*); 297da2e3ebdSchin extern char* pathshell(void); 298da2e3ebdSchin extern char* pathtemp(char*, size_t, const char*, const char*, int*); 299da2e3ebdSchin extern char* pathtmp(char*, const char*, const char*, int*); 300da2e3ebdSchin extern char* setenviron(const char*); 301da2e3ebdSchin extern int stracmp(const char*, const char*); 302da2e3ebdSchin extern char* strcopy(char*, const char*); 303da2e3ebdSchin extern unsigned long strelapsed(const char*, char**, int); 304da2e3ebdSchin extern int stresc(char*); 305da2e3ebdSchin extern long streval(const char*, char**, long(*)(const char*, char**)); 306da2e3ebdSchin extern long strexpr(const char*, char**, long(*)(const char*, char**, void*), void*); 307da2e3ebdSchin extern int strgid(const char*); 308da2e3ebdSchin extern int strgrpmatch(const char*, const char*, int*, int, int); 309da2e3ebdSchin extern unsigned int strhash(const char*); 310da2e3ebdSchin extern void* strlook(const void*, size_t, const char*); 311da2e3ebdSchin extern int strmatch(const char*, const char*); 312da2e3ebdSchin extern int strmode(const char*); 313da2e3ebdSchin extern int strnacmp(const char*, const char*, size_t); 314da2e3ebdSchin extern char* strncopy(char*, const char*, size_t); 31534f9b3eeSRoland Mainz extern int strnpcmp(const char*, const char*, size_t); 316da2e3ebdSchin extern double strntod(const char*, size_t, char**); 317da2e3ebdSchin extern _ast_fltmax_t strntold(const char*, size_t, char**); 318da2e3ebdSchin extern long strntol(const char*, size_t, char**, int); 319da2e3ebdSchin extern intmax_t strntoll(const char*, size_t, char**, int); 3207c2fbfb3SApril Chin extern long strnton(const char*, size_t, char**, char*, int); 321da2e3ebdSchin extern unsigned long strntoul(const char*, size_t, char**, int); 3227c2fbfb3SApril Chin extern intmax_t strntonll(const char*, size_t, char**, char*, int); 323da2e3ebdSchin extern uintmax_t strntoull(const char*, size_t, char**, int); 32434f9b3eeSRoland Mainz extern int strnvcmp(const char*, const char*, size_t); 325da2e3ebdSchin extern int stropt(const char*, const void*, int, int(*)(void*, const void*, int, const char*), void*); 32634f9b3eeSRoland Mainz extern int strpcmp(const char*, const char*); 327da2e3ebdSchin extern int strperm(const char*, char**, int); 328da2e3ebdSchin extern void* strpsearch(const void*, size_t, size_t, const char*, char**); 329da2e3ebdSchin extern void* strsearch(const void*, size_t, size_t, Strcmp_f, const char*, void*); 330da2e3ebdSchin extern void strsort(char**, int, int(*)(const char*, const char*)); 331da2e3ebdSchin extern char* strsubmatch(const char*, const char*, int); 332da2e3ebdSchin extern unsigned long strsum(const char*, unsigned long); 333da2e3ebdSchin extern char* strtape(const char*, char**); 334da2e3ebdSchin extern int strtoip4(const char*, char**, uint32_t*, unsigned char*); 335da2e3ebdSchin extern long strton(const char*, char**, char*, int); 336da2e3ebdSchin extern intmax_t strtonll(const char*, char**, char*, int); 337da2e3ebdSchin extern int struid(const char*); 338da2e3ebdSchin extern int struniq(char**, int); 33934f9b3eeSRoland Mainz extern int strvcmp(const char*, const char*); 340da2e3ebdSchin 341da2e3ebdSchin #undef extern 342da2e3ebdSchin 343da2e3ebdSchin /* 344da2e3ebdSchin * C library global data symbols not prototyped by <unistd.h> 345da2e3ebdSchin */ 346da2e3ebdSchin 347da2e3ebdSchin #if !defined(environ) && defined(__DYNAMIC__) 348da2e3ebdSchin #define environ __DYNAMIC__(environ) 349da2e3ebdSchin #else 350da2e3ebdSchin extern char** environ; 351da2e3ebdSchin #endif 352da2e3ebdSchin 353da2e3ebdSchin /* 354da2e3ebdSchin * really handy malloc()/free() (__FILE__,__LINE__,__FUNCTION__) tracing 355da2e3ebdSchin * make with VMDEBUG==1 or debug=1 or CCFLAGS=$(CC.DEBUG) 356da2e3ebdSchin * VMDEBUG==0 disables 357da2e3ebdSchin * at runtime export VMDEBUG or VMTRACE per vmalloc.3 358da2e3ebdSchin * to list originating call locations 359da2e3ebdSchin */ 360da2e3ebdSchin 361da2e3ebdSchin #if !_std_malloc && !defined(VMFL) && !defined(_VMHDR_H) && \ 362da2e3ebdSchin (!defined(VMDEBUG) || VMDEBUG) && (VMDEBUG || _BLD_DEBUG) 363da2e3ebdSchin 364da2e3ebdSchin #define VMFL 1 365da2e3ebdSchin #include <vmalloc.h> 366da2e3ebdSchin 367da2e3ebdSchin #endif 368da2e3ebdSchin 369da2e3ebdSchin #endif 370