xref: /titanic_41/usr/src/lib/libast/common/include/ast.h (revision 5203bc321053fb87d7073c7640548fab73634793)
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
57 struct _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 
141 typedef 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 
226 typedef int (*Error_f)(void*, void*, int, ...);
227 
228 typedef int (*Ast_confdisc_f)(const char*, const char*, const char*);
229 typedef int (*Strcmp_context_f)(const char*, const char*, void*);
230 typedef int (*Strcmp_f)(const char*, const char*);
231 
232 #if _BLD_ast && defined(__EXPORT__)
233 #define extern		__EXPORT__
234 #endif
235 
236 extern char*		astgetconf(const char*, const char*, const char*, int, Error_f);
237 extern char*		astconf(const char*, const char*, const char*);
238 extern Ast_confdisc_f	astconfdisc(Ast_confdisc_f);
239 extern void		astconflist(Sfio_t*, const char*, int, const char*);
240 extern off_t		astcopy(int, int, off_t);
241 extern int		astlicense(char*, int, char*, char*, int, int, int);
242 extern int		astquery(int, const char*, ...);
243 extern void		astwinsize(int, int*, int*);
244 
245 extern ssize_t		base64encode(const void*, size_t, void**, void*, size_t, void**);
246 extern ssize_t		base64decode(const void*, size_t, void**, void*, size_t, void**);
247 extern int		chresc(const char*, char**);
248 extern int		chrtoi(const char*);
249 extern int		eaccess(const char*, int);
250 extern char*		fmtbase(long, int, int);
251 extern char*		fmtbasell(intmax_t, int, int);
252 #define fmtbase(a,b,c)	fmtbasell((intmax_t)(a),b,c) /* until 2003-09-01 */
253 extern char*		fmtbuf(size_t);
254 extern char*		fmtclock(Sfulong_t);
255 extern char*		fmtelapsed(unsigned long, int);
256 extern char*		fmterror(int);
257 extern char*		fmtesc(const char*);
258 extern char*		fmtesq(const char*, const char*);
259 extern char*		fmtident(const char*);
260 extern char*		fmtip4(uint32_t, int);
261 extern char*		fmtfmt(const char*);
262 extern char*		fmtgid(int);
263 extern char*		fmtmatch(const char*);
264 extern char*		fmtmode(int, int);
265 extern char*		fmtnesq(const char*, const char*, size_t);
266 extern char*		fmtnum(unsigned long, int);
267 extern char*		fmtperm(int);
268 extern char*		fmtquote(const char*, const char*, const char*, size_t, int);
269 extern char*		fmtre(const char*);
270 extern char*		fmtscale(Sfulong_t, int);
271 extern char*		fmtsignal(int);
272 extern char*		fmttime(const char*, time_t);
273 extern char*		fmtuid(int);
274 extern char*		fmtversion(unsigned long);
275 extern void*		memdup(const void*, size_t);
276 extern void		memfatal(void);
277 extern unsigned int	memhash(const void*, int);
278 extern unsigned long	memsum(const void*, int, unsigned long);
279 extern char*		pathaccess(char*, const char*, const char*, const char*, int);
280 extern char*		pathbin(void);
281 extern char*		pathcanon(char*, int);
282 extern char*		pathcat(char*, const char*, int, const char*, const char*);
283 extern int		pathcd(const char*, const char*);
284 extern int		pathcheck(const char*, const char*, Pathcheck_t*);
285 extern int		pathexists(char*, int);
286 extern char*		pathfind(const char*, const char*, const char*, char*, size_t);
287 extern int		pathgetlink(const char*, char*, int);
288 extern int		pathinclude(const char*);
289 extern char*		pathkey(char*, char*, const char*, const char*, const char*);
290 extern size_t		pathnative(const char*, char*, size_t);
291 extern char*		pathpath(char*, const char*, const char*, int);
292 extern size_t		pathposix(const char*, char*, size_t);
293 extern char*		pathprobe(char*, char*, const char*, const char*, const char*, int);
294 extern size_t		pathprog(const char*, char*, size_t);
295 extern char*		pathrepl(char*, const char*, const char*);
296 extern int		pathsetlink(const char*, const char*);
297 extern char*		pathshell(void);
298 extern char*		pathtemp(char*, size_t, const char*, const char*, int*);
299 extern char*		pathtmp(char*, const char*, const char*, int*);
300 extern char*		setenviron(const char*);
301 extern int		stracmp(const char*, const char*);
302 extern char*		strcopy(char*, const char*);
303 extern unsigned long	strelapsed(const char*, char**, int);
304 extern int		stresc(char*);
305 extern long		streval(const char*, char**, long(*)(const char*, char**));
306 extern long		strexpr(const char*, char**, long(*)(const char*, char**, void*), void*);
307 extern int		strgid(const char*);
308 extern int		strgrpmatch(const char*, const char*, int*, int, int);
309 extern unsigned int	strhash(const char*);
310 extern void*		strlook(const void*, size_t, const char*);
311 extern int		strmatch(const char*, const char*);
312 extern int		strmode(const char*);
313 extern int		strnacmp(const char*, const char*, size_t);
314 extern char*		strncopy(char*, const char*, size_t);
315 extern int		strnpcmp(const char*, const char*, size_t);
316 extern double		strntod(const char*, size_t, char**);
317 extern _ast_fltmax_t	strntold(const char*, size_t, char**);
318 extern long		strntol(const char*, size_t, char**, int);
319 extern intmax_t		strntoll(const char*, size_t, char**, int);
320 extern long		strnton(const char*, size_t, char**, char*, int);
321 extern unsigned long	strntoul(const char*, size_t, char**, int);
322 extern intmax_t		strntonll(const char*, size_t, char**, char*, int);
323 extern uintmax_t	strntoull(const char*, size_t, char**, int);
324 extern int		strnvcmp(const char*, const char*, size_t);
325 extern int		stropt(const char*, const void*, int, int(*)(void*, const void*, int, const char*), void*);
326 extern int		strpcmp(const char*, const char*);
327 extern int		strperm(const char*, char**, int);
328 extern void*		strpsearch(const void*, size_t, size_t, const char*, char**);
329 extern void*		strsearch(const void*, size_t, size_t, Strcmp_f, const char*, void*);
330 extern void		strsort(char**, int, int(*)(const char*, const char*));
331 extern char*		strsubmatch(const char*, const char*, int);
332 extern unsigned long	strsum(const char*, unsigned long);
333 extern char*		strtape(const char*, char**);
334 extern int		strtoip4(const char*, char**, uint32_t*, unsigned char*);
335 extern long		strton(const char*, char**, char*, int);
336 extern intmax_t		strtonll(const char*, char**, char*, int);
337 extern int		struid(const char*);
338 extern int		struniq(char**, int);
339 extern 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
350 extern 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