libuutil.h revision 471:fb6202c3da23
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#ifndef	_LIBUUTIL_H
28#define	_LIBUUTIL_H
29
30#pragma ident	"%Z%%M%	%I%	%E% SMI"
31
32#include <sys/types.h>
33#include <stdarg.h>
34
35#ifdef	__cplusplus
36extern "C" {
37#endif
38
39/*
40 * Standard flags codes.
41 */
42#define	UU_DEFAULT		0
43
44/*
45 * Standard error codes.
46 */
47#define	UU_ERROR_NONE		0	/* no error */
48#define	UU_ERROR_INVALID_ARGUMENT 1	/* invalid argument */
49#define	UU_ERROR_UNKNOWN_FLAG	2	/* passed flag invalid */
50#define	UU_ERROR_NO_MEMORY	3	/* out of memory */
51#define	UU_ERROR_CALLBACK_FAILED 4	/* callback-initiated error */
52#define	UU_ERROR_NOT_SUPPORTED	5	/* operation not supported */
53#define	UU_ERROR_EMPTY		6	/* no value provided */
54#define	UU_ERROR_UNDERFLOW	7	/* value is too small */
55#define	UU_ERROR_OVERFLOW	8	/* value is too value */
56#define	UU_ERROR_INVALID_CHAR	9	/* value contains unexpected char */
57#define	UU_ERROR_INVALID_DIGIT	10	/* value contains digit not in base */
58
59#define	UU_ERROR_SYSTEM		99	/* underlying system error */
60#define	UU_ERROR_UNKNOWN	100	/* error status not known */
61
62/*
63 * Standard program exit codes.
64 */
65#define	UU_EXIT_OK	(*(uu_exit_ok()))
66#define	UU_EXIT_FATAL	(*(uu_exit_fatal()))
67#define	UU_EXIT_USAGE	(*(uu_exit_usage()))
68
69/*
70 * Exit status profiles.
71 */
72#define	UU_PROFILE_DEFAULT	0
73#define	UU_PROFILE_LAUNCHER	1
74
75/*
76 * Error reporting functions.
77 */
78uint32_t uu_error(void);
79const char *uu_strerror(uint32_t);
80
81/*
82 * Program notification functions.
83 */
84extern void uu_alt_exit(int);
85extern const char *uu_setpname(char *);
86extern const char *uu_getpname(void);
87/*PRINTFLIKE1*/
88extern void uu_warn(const char *, ...);
89extern void uu_vwarn(const char *, va_list);
90/*PRINTFLIKE1*/
91extern void uu_die(const char *, ...) __NORETURN;
92extern void uu_vdie(const char *, va_list) __NORETURN;
93/*PRINTFLIKE2*/
94extern void uu_xdie(int, const char *, ...) __NORETURN;
95extern void uu_vxdie(int, const char *, va_list) __NORETURN;
96
97/*
98 * Exit status functions (not to be used directly)
99 */
100extern int *uu_exit_ok(void);
101extern int *uu_exit_fatal(void);
102extern int *uu_exit_usage(void);
103
104/*
105 * string->number conversions
106 */
107extern int uu_strtoint(const char *, void *, size_t, int, int64_t, int64_t);
108extern int uu_strtouint(const char *, void *, size_t, int, uint64_t, uint64_t);
109
110/*
111 * Debug print facility functions.
112 */
113typedef struct uu_dprintf uu_dprintf_t;
114
115typedef enum {
116	UU_DPRINTF_SILENT,
117	UU_DPRINTF_FATAL,
118	UU_DPRINTF_WARNING,
119	UU_DPRINTF_NOTICE,
120	UU_DPRINTF_INFO,
121	UU_DPRINTF_DEBUG
122} uu_dprintf_severity_t;
123
124extern uu_dprintf_t *uu_dprintf_create(const char *, uu_dprintf_severity_t,
125    uint_t);
126/*PRINTFLIKE3*/
127extern void uu_dprintf(uu_dprintf_t *, uu_dprintf_severity_t,
128    const char *, ...);
129extern void uu_dprintf_destroy(uu_dprintf_t *);
130extern const char *uu_dprintf_getname(uu_dprintf_t *);
131
132/*
133 * Identifier test flags and function.
134 */
135#define	UU_NAME_DOMAIN		0x1	/* allow SUNW, or com.sun, prefix */
136#define	UU_NAME_PATH		0x2	/* allow '/'-delimited paths */
137
138int uu_check_name(const char *, uint_t);
139
140/*
141 * File creation functions.
142 */
143extern int uu_open_tmp(const char *dir, uint_t uflags);
144
145/*
146 * Convenience functions.
147 */
148/*PRINTFLIKE1*/
149extern char *uu_msprintf(const char *format, ...);
150extern void *uu_zalloc(size_t);
151extern void uu_free(void *);
152
153/*
154 * Comparison function type definition.
155 *   Developers should be careful in their use of the _private argument. If you
156 *   break interface guarantees, you get undefined behavior.
157 */
158typedef int uu_compare_fn_t(const void *__left, const void *__right,
159    void *__private);
160
161/*
162 * Walk variant flags.
163 *   A data structure need not provide support for all variants and
164 *   combinations.  Refer to the appropriate documentation.
165 */
166#define	UU_WALK_ROBUST		0x00000001	/* walk can survive removes */
167#define	UU_WALK_REVERSE		0x00000002	/* reverse walk order */
168
169#define	UU_WALK_PREORDER	0x00000010	/* walk tree in pre-order */
170#define	UU_WALK_POSTORDER	0x00000020	/* walk tree in post-order */
171
172/*
173 * Walk callback function return codes.
174 */
175#define	UU_WALK_ERROR		-1
176#define	UU_WALK_NEXT		0
177#define	UU_WALK_DONE		1
178
179/*
180 * Walk callback function type definition.
181 */
182typedef int uu_walk_fn_t(void *_elem, void *_private);
183
184/*
185 * lists: opaque structures
186 */
187typedef struct uu_list_pool uu_list_pool_t;
188typedef struct uu_list uu_list_t;
189
190typedef struct uu_list_node {
191	uintptr_t uln_opaque[2];
192} uu_list_node_t;
193
194typedef struct uu_list_walk uu_list_walk_t;
195
196typedef uintptr_t uu_list_index_t;
197
198/*
199 * lists: interface
200 *
201 * basic usage:
202 *	typedef struct foo {
203 *		...
204 *		uu_list_node_t foo_node;
205 *		...
206 *	} foo_t;
207 *
208 *	static int
209 *	foo_compare(void *l_arg, void *r_arg, void *private)
210 *	{
211 *		foo_t *l = l_arg;
212 *		foo_t *r = r_arg;
213 *
214 *		if (... l greater than r ...)
215 *			return (1);
216 *		if (... l less than r ...)
217 *			return (-1);
218 *		return (0);
219 *	}
220 *
221 *	...
222 *		// at initialization time
223 *		foo_pool = uu_list_pool_create("foo_pool",
224 *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
225 *		    debugging? 0 : UU_AVL_POOL_DEBUG);
226 *	...
227 */
228uu_list_pool_t *uu_list_pool_create(const char *, size_t, size_t,
229    uu_compare_fn_t *, uint32_t);
230#define	UU_LIST_POOL_DEBUG	0x00000001
231
232void uu_list_pool_destroy(uu_list_pool_t *);
233
234/*
235 * usage:
236 *
237 *	foo_t *a;
238 *	a = malloc(sizeof(*a));
239 *	uu_list_node_init(a, &a->foo_list, pool);
240 *	...
241 *	uu_list_node_fini(a, &a->foo_list, pool);
242 *	free(a);
243 */
244void uu_list_node_init(void *, uu_list_node_t *, uu_list_pool_t *);
245void uu_list_node_fini(void *, uu_list_node_t *, uu_list_pool_t *);
246
247uu_list_t *uu_list_create(uu_list_pool_t *, void *_parent, uint32_t);
248#define	UU_LIST_DEBUG	0x00000001
249#define	UU_LIST_SORTED	0x00000002	/* list is sorted */
250
251void uu_list_destroy(uu_list_t *);	/* list must be empty */
252
253size_t uu_list_numnodes(uu_list_t *);
254
255void *uu_list_first(uu_list_t *);
256void *uu_list_last(uu_list_t *);
257
258void *uu_list_next(uu_list_t *, void *);
259void *uu_list_prev(uu_list_t *, void *);
260
261int uu_list_walk(uu_list_t *, uu_walk_fn_t *, void *, uint32_t);
262
263uu_list_walk_t *uu_list_walk_start(uu_list_t *, uint32_t);
264void *uu_list_walk_next(uu_list_walk_t *);
265void uu_list_walk_end(uu_list_walk_t *);
266
267void *uu_list_find(uu_list_t *, void *, void *, uu_list_index_t *);
268void uu_list_insert(uu_list_t *, void *, uu_list_index_t);
269
270void *uu_list_nearest_next(uu_list_t *, uu_list_index_t);
271void *uu_list_nearest_prev(uu_list_t *, uu_list_index_t);
272
273void *uu_list_teardown(uu_list_t *, void **);
274
275void uu_list_remove(uu_list_t *, void *);
276
277/*
278 * lists: interfaces for non-sorted lists only
279 */
280int uu_list_insert_before(uu_list_t *, void *_target, void *_elem);
281int uu_list_insert_after(uu_list_t *, void *_target, void *_elem);
282
283/*
284 * avl trees: opaque structures
285 */
286typedef struct uu_avl_pool uu_avl_pool_t;
287typedef struct uu_avl uu_avl_t;
288
289typedef struct uu_avl_node {
290#ifdef _LP64
291	uintptr_t uan_opaque[3];
292#else
293	uintptr_t uan_opaque[4];
294#endif
295} uu_avl_node_t;
296
297typedef struct uu_avl_walk uu_avl_walk_t;
298
299typedef uintptr_t uu_avl_index_t;
300
301/*
302 * avl trees: interface
303 *
304 * basic usage:
305 *	typedef struct foo {
306 *		...
307 *		uu_avl_node_t foo_node;
308 *		...
309 *	} foo_t;
310 *
311 *	static int
312 *	foo_compare(void *l_arg, void *r_arg, void *private)
313 *	{
314 *		foo_t *l = l_arg;
315 *		foo_t *r = r_arg;
316 *
317 *		if (... l greater than r ...)
318 *			return (1);
319 *		if (... l less than r ...)
320 *			return (-1);
321 *		return (0);
322 *	}
323 *
324 *	...
325 *		// at initialization time
326 *		foo_pool = uu_avl_pool_create("foo_pool",
327 *		    sizeof (foo_t), offsetof(foo_t, foo_node), foo_compare,
328 *		    debugging? 0 : UU_AVL_POOL_DEBUG);
329 *	...
330 */
331uu_avl_pool_t *uu_avl_pool_create(const char *, size_t, size_t,
332    uu_compare_fn_t *, uint32_t);
333#define	UU_AVL_POOL_DEBUG	0x00000001
334
335void uu_avl_pool_destroy(uu_avl_pool_t *);
336
337/*
338 * usage:
339 *
340 *	foo_t *a;
341 *	a = malloc(sizeof(*a));
342 *	uu_avl_node_init(a, &a->foo_avl, pool);
343 *	...
344 *	uu_avl_node_fini(a, &a->foo_avl, pool);
345 *	free(a);
346 */
347void uu_avl_node_init(void *, uu_avl_node_t *, uu_avl_pool_t *);
348void uu_avl_node_fini(void *, uu_avl_node_t *, uu_avl_pool_t *);
349
350uu_avl_t *uu_avl_create(uu_avl_pool_t *, void *_parent, uint32_t);
351#define	UU_AVL_DEBUG	0x00000001
352
353void uu_avl_destroy(uu_avl_t *);	/* list must be empty */
354
355size_t uu_avl_numnodes(uu_avl_t *);
356
357void *uu_avl_first(uu_avl_t *);
358void *uu_avl_last(uu_avl_t *);
359
360void *uu_avl_next(uu_avl_t *, void *);
361void *uu_avl_prev(uu_avl_t *, void *);
362
363int uu_avl_walk(uu_avl_t *, uu_walk_fn_t *, void *, uint32_t);
364
365uu_avl_walk_t *uu_avl_walk_start(uu_avl_t *, uint32_t);
366void *uu_avl_walk_next(uu_avl_walk_t *);
367void uu_avl_walk_end(uu_avl_walk_t *);
368
369void *uu_avl_find(uu_avl_t *, void *, void *, uu_avl_index_t *);
370void uu_avl_insert(uu_avl_t *, void *, uu_avl_index_t);
371
372void *uu_avl_nearest_next(uu_avl_t *, uu_avl_index_t);
373void *uu_avl_nearest_prev(uu_avl_t *, uu_avl_index_t);
374
375void *uu_avl_teardown(uu_avl_t *, void **);
376
377void uu_avl_remove(uu_avl_t *, void *);
378
379#ifdef	__cplusplus
380}
381#endif
382
383#endif	/* _LIBUUTIL_H */
384