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