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