xref: /titanic_41/usr/src/lib/libast/common/include/ast.h (revision f52e84dfce89ba2ea0ebb6a191b4466da3012efd)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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.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 char*		pathrepl(char*, const char*, const char*);
295 extern int		pathsetlink(const char*, const char*);
296 extern char*		pathshell(void);
297 extern char*		pathtemp(char*, size_t, const char*, const char*, int*);
298 extern char*		pathtmp(char*, const char*, const char*, int*);
299 extern char*		setenviron(const char*);
300 extern int		stracmp(const char*, const char*);
301 extern char*		strcopy(char*, const char*);
302 extern unsigned long	strelapsed(const char*, char**, int);
303 extern int		stresc(char*);
304 extern long		streval(const char*, char**, long(*)(const char*, char**));
305 extern long		strexpr(const char*, char**, long(*)(const char*, char**, void*), void*);
306 extern int		strgid(const char*);
307 extern int		strgrpmatch(const char*, const char*, int*, int, int);
308 extern unsigned int	strhash(const char*);
309 extern void*		strlook(const void*, size_t, const char*);
310 extern int		strmatch(const char*, const char*);
311 extern int		strmode(const char*);
312 extern int		strnacmp(const char*, const char*, size_t);
313 extern char*		strncopy(char*, const char*, size_t);
314 extern double		strntod(const char*, size_t, char**);
315 extern _ast_fltmax_t	strntold(const char*, size_t, char**);
316 extern long		strntol(const char*, size_t, char**, int);
317 extern intmax_t		strntoll(const char*, size_t, char**, int);
318 extern unsigned long	strntoul(const char*, size_t, char**, int);
319 extern uintmax_t	strntoull(const char*, size_t, char**, int);
320 extern int		stropt(const char*, const void*, int, int(*)(void*, const void*, int, const char*), void*);
321 extern int		strperm(const char*, char**, int);
322 extern void*		strpsearch(const void*, size_t, size_t, const char*, char**);
323 extern void*		strsearch(const void*, size_t, size_t, Strcmp_f, const char*, void*);
324 extern void		strsort(char**, int, int(*)(const char*, const char*));
325 extern char*		strsubmatch(const char*, const char*, int);
326 extern unsigned long	strsum(const char*, unsigned long);
327 extern char*		strtape(const char*, char**);
328 extern int		strtoip4(const char*, char**, uint32_t*, unsigned char*);
329 extern long		strton(const char*, char**, char*, int);
330 extern intmax_t		strtonll(const char*, char**, char*, int);
331 extern int		struid(const char*);
332 extern int		struniq(char**, int);
333 
334 #undef			extern
335 
336 /*
337  * C library global data symbols not prototyped by <unistd.h>
338  */
339 
340 #if !defined(environ) && defined(__DYNAMIC__)
341 #define	environ		__DYNAMIC__(environ)
342 #else
343 extern char**		environ;
344 #endif
345 
346 /*
347  * really handy malloc()/free() (__FILE__,__LINE__,__FUNCTION__) tracing
348  * make with VMDEBUG==1 or debug=1 or CCFLAGS=$(CC.DEBUG)
349  * VMDEBUG==0 disables
350  * at runtime export VMDEBUG or VMTRACE per vmalloc.3
351  * to list originating call locations
352  */
353 
354 #if !_std_malloc && !defined(VMFL) && !defined(_VMHDR_H) && \
355 	(!defined(VMDEBUG) || VMDEBUG) && (VMDEBUG || _BLD_DEBUG)
356 
357 #define VMFL	1
358 #include <vmalloc.h>
359 
360 #endif
361 
362 #endif
363