ia64/xen-unstable

view tools/vnet/libxutil/sxpr.h @ 6766:219d96d545fc

merge?
author cl349@firebug.cl.cam.ac.uk
date Mon Sep 12 20:00:41 2005 +0000 (2005-09-12)
parents dd668f7527cb
children b2f4823b6ff0 b35215021b32 9af349b055e5 3233e7ecfa9f
line source
1 /*
2 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of the
7 * License, or (at your option) any later version. This library is
8 * distributed in the hope that it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.
11 * See the GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17 #ifndef _XUTIL_SXPR_H_
18 #define _XUTIL_SXPR_H_
20 #ifdef __KERNEL__
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #else
24 #include <stdint.h>
25 #endif
27 #include "hash_table.h"
28 #include "iostream.h"
29 #include "allocate.h"
31 /** @file
32 * Definitions for rules and sxprs.
33 */
35 #ifndef NULL
36 #define NULL 0
37 #endif
39 #ifndef TRUE
40 #define TRUE 1
41 #endif
43 #ifndef FALSE
44 #define FALSE 0
45 #endif
47 /** Sxpr type. */
48 typedef int16_t TypeCode;
50 /** A typed sxpr handle.*/
51 typedef struct Sxpr {
52 /** Sxpr type. */
53 TypeCode type;
54 union {
55 /** Sxpr value. */
56 unsigned long ul;
57 /** Pointer. */
58 void *ptr;
59 } v;
60 } Sxpr;
62 /** Get the integer value from an sxpr.
63 *
64 * @param obj sxpr
65 * @return value
66 */
67 static inline unsigned long get_ul(Sxpr obj){
68 return obj.v.ul;
69 }
71 /** Get the pointer value from an sxpr.
72 *
73 * @param obj sxpr
74 * @return value
75 */
76 static inline void * get_ptr(Sxpr obj){
77 return obj.v.ptr;
78 }
80 /** Create an sxpr containing a pointer.
81 *
82 * @param ty typecode
83 * @param val pointer
84 * @return sxpr
85 */
86 static inline Sxpr obj_ptr(TypeCode ty, void *val){
87 return (Sxpr){ .type= ty, .v= { .ptr= val } };
88 }
90 /** Create an sxpr containing an integer.
91 *
92 * @param ty typecode
93 * @param val integer
94 * @return sxpr
95 */
96 static inline Sxpr obj_ul(TypeCode ty, unsigned long val){
97 return (Sxpr){ .type= ty, .v= { .ul= val } };
98 }
100 /** Get the type of an sxpr.
101 *
102 * @param obj sxpr
103 * @return type
104 */
105 static inline TypeCode get_type(Sxpr obj){
106 return obj.type;
107 }
109 /** Check the type of an sxpr.
110 *
111 * @param obj sxpr
112 * @param type to check
113 * @return 1 if has the type, 0 otherwise
114 */
115 static inline int has_type(Sxpr obj, TypeCode type){
116 return get_type(obj) == type;
117 }
119 /** Compare sxprs for literal equality of type and value.
120 *
121 * @param x sxpr to compare
122 * @param y sxpr to compare
123 * @return 1 if equal, 0 otherwise
124 */
125 static inline int eq(Sxpr x, Sxpr y){
126 return ((get_type(x) == get_type(y)) && (get_ul(x) == get_ul(y)));
127 }
129 /** The 'unspecified' sxpr. */
130 #define T_NONE ((TypeCode)0)
131 /** The empty list. */
132 #define T_NULL ((TypeCode)1)
133 /** Unsigned integer. */
134 #define T_UINT ((TypeCode)2)
135 /** A string. */
136 #define T_STRING ((TypeCode)3)
137 /** An atom. */
138 #define T_ATOM ((TypeCode)4)
139 /** A boolean. */
140 #define T_BOOL ((TypeCode)5)
142 /** A cons (pair or list). */
143 #define T_CONS ((TypeCode)10)
145 /** An error. */
146 #define T_ERR ((TypeCode)40)
147 /** Sxpr type to indicate out of memory. */
148 #define T_NOMEM ((TypeCode)41)
150 typedef struct ObjString {
151 int len;
152 char data[];
153 } ObjString;
155 /** An atom. */
156 typedef struct ObjAtom {
157 Sxpr name;
158 Hashcode hashcode;
159 int interned;
160 } ObjAtom;
162 /** A cons (pair). */
163 typedef struct ObjCons {
164 Sxpr car;
165 Sxpr cdr;
166 } ObjCons;
168 /** Flags for sxpr printing. */
169 enum PrintFlags {
170 PRINT_RAW = 0x001,
171 PRINT_TYPE = 0x002,
172 PRINT_PRETTY = 0x004,
173 PRINT_COUNTED = 0x008,
174 PRINT_ADDR = 0x010,
175 };
177 extern int _string_print(IOStream *io, char *str, int n, unsigned flags);
178 extern int _string_print_raw(IOStream *io, char *str, int n);
179 extern int _string_print_counted(IOStream *io, char *str, int n);
180 extern int _string_print_quoted(IOStream *io, char *str, int n);
181 extern int _string_print_string(IOStream *io, char *str, int n);
183 /** An integer sxpr.
184 *
185 * @param ty type
186 * @param val integer value
187 */
188 #define OBJI(ty, val) obj_ul(ty, val)
190 /** Make an integer sxpr.
191 * @param x value
192 */
193 #define OINT(x) OBJI(T_UINT, x)
195 /** Make an error sxpr.
196 *
197 * @param x value
198 */
199 #define OERR(x) OBJI(T_ERR, x)
201 /** Out of memory constant. */
202 #define ONOMEM OBJI(T_NOMEM, 0)
204 /** The `unspecified' constant. */
205 #define ONONE OBJI(T_NONE, 0)
207 /** Empty list constant. */
208 #define ONULL OBJI(T_NULL, 0)
210 /** False constant. */
211 #define OFALSE OBJI(T_BOOL, 0)
213 /** True constant. */
214 #define OTRUE OBJI(T_BOOL, 1)
216 /** A pointer sxpr.
217 * If the pointer is non-null, returns an sxpr containing it.
218 * If the pointer is null, returns ONOMEM.
219 *
220 * @param ty type
221 * @param val pointer
222 */
223 static inline Sxpr OBJP(int ty, void *val){
224 return (val ? obj_ptr(ty, val) : ONOMEM);
225 }
227 /** Make an integer sxpr containing a pointer.
228 *
229 * @param val pointer
230 */
231 static inline Sxpr PTR(void *val){
232 return OBJP(T_UINT, (void*)(val));
233 }
235 /** Allocate some memory and return an sxpr containing it.
236 * Returns ONOMEM if allocation failed.
237 *
238 * @param n number of bytes to allocate
239 * @param ty typecode
240 * @return sxpr
241 */
242 static inline Sxpr halloc(int n, int ty){
243 return OBJP(ty, allocate(n));
244 }
246 /** Allocate an sxpr containing a pointer to the given type.
247 *
248 * @param _ctype type (uses sizeof to determine how many bytes to allocate)
249 * @param _tycode typecode
250 * @return sxpr, ONOMEM if allocation failed
251 */
252 #define HALLOC(_ctype, _tycode) halloc(sizeof(_ctype), _tycode)
254 /* Recognizers for the various sxpr types. */
255 #define ATOMP(obj) has_type(obj, T_ATOM)
256 #define BOOLP(obj) has_type(obj, T_BOOL)
257 #define CONSP(obj) has_type(obj, T_CONS)
258 #define ERRP(obj) has_type(obj, T_ERR)
259 #define INTP(obj) has_type(obj, T_UINT)
260 #define NOMEMP(obj) has_type(obj, T_NOMEM)
261 #define NONEP(obj) has_type(obj, T_NONE)
262 #define NULLP(obj) has_type(obj, T_NULL)
263 #define STRINGP(obj) has_type(obj, T_STRING)
265 #define TRUEP(obj) get_ul(obj)
267 /** Convert an sxpr to an unsigned integer. */
268 #define OBJ_UINT(x) get_ul(x)
269 /** Convert an sxpr to an integer. */
270 #define OBJ_INT(x) (int)get_ul(x)
272 /* Conversions of sxprs to their values.
273 * No checking is done.
274 */
275 #define OBJ_STRING(x) ((ObjString*)get_ptr(x))
276 #define OBJ_CONS(x) ((ObjCons*)get_ptr(x))
277 #define OBJ_ATOM(x) ((ObjAtom*)get_ptr(x))
278 #define OBJ_SET(x) ((ObjSet*)get_ptr(x))
279 #define CAR(x) (OBJ_CONS(x)->car)
280 #define CDR(x) (OBJ_CONS(x)->cdr)
282 #define CAAR(x) (CAR(CAR(x)))
283 #define CADR(x) (CAR(CDR(x)))
284 #define CDAR(x) (CDR(CAR(x)))
285 #define CDDR(x) (CDR(CDR(x)))
287 /** Checked version of CAR
288 *
289 * @param x sxpr
290 * @return CAR if a cons, x otherwise
291 */
292 static inline Sxpr car(Sxpr x){
293 return (CONSP(x) ? CAR(x) : x);
294 }
296 /** Checked version of CDR.
297 *
298 * @param x sxpr
299 * @return CDR if a cons, null otherwise
300 */
301 static inline Sxpr cdr(Sxpr x){
302 return (CONSP(x) ? CDR(x) : ONULL);
303 }
305 typedef int ObjPrintFn(IOStream *io, Sxpr obj, unsigned flags);
306 typedef int ObjEqualFn(Sxpr obj, Sxpr other);
307 typedef void ObjFreeFn(Sxpr obj);
308 typedef Sxpr ObjCopyFn(Sxpr obj);
310 /** An sxpr type definition. */
311 typedef struct SxprType {
312 TypeCode type;
313 char *name;
314 int pointer;
315 ObjPrintFn *print;
316 ObjEqualFn *equal;
317 ObjFreeFn *free;
318 ObjCopyFn *copy;
319 } SxprType;
322 extern int def_sxpr_type(SxprType *tydef);
323 extern SxprType *get_sxpr_type(int ty);
325 /** Free the pointer in an sxpr.
326 *
327 * @param x sxpr containing a pointer
328 */
329 static inline void hfree(Sxpr x){
330 deallocate(get_ptr(x));
331 }
333 extern int objprint(IOStream *io, Sxpr x, unsigned flags);
334 extern int objequal(Sxpr x, Sxpr y);
335 extern void objfree(Sxpr x);
336 extern Sxpr objcopy(Sxpr x);
338 extern void cons_free_cells(Sxpr obj);
339 extern Sxpr intern(char *s);
341 extern Sxpr assoc(Sxpr k, Sxpr l);
342 extern Sxpr assocq(Sxpr k, Sxpr l);
343 extern Sxpr acons(Sxpr k, Sxpr v, Sxpr l);
344 extern Sxpr nrev(Sxpr l);
345 extern Sxpr cons_member(Sxpr l, Sxpr x);
346 extern Sxpr cons_member_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
347 extern int cons_subset(Sxpr s, Sxpr t);
348 extern int cons_set_equal(Sxpr s, Sxpr t);
350 #ifdef USE_GC
351 extern Sxpr cons_remove(Sxpr l, Sxpr x);
352 extern Sxpr cons_remove_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
353 #endif
355 extern Sxpr atom_new(char *name);
356 extern char * atom_name(Sxpr obj);
357 extern int atom_length(Sxpr obj);
359 extern Sxpr string_new(char *s);
360 extern Sxpr string_new_n(char *s, int n);
361 extern char * string_string(Sxpr obj);
362 extern int string_length(Sxpr obj);
364 extern Sxpr cons_new(Sxpr car, Sxpr cdr);
365 extern int cons_push(Sxpr *list, Sxpr elt);
366 extern int cons_length(Sxpr obj);
368 Sxpr sxpr_name(Sxpr obj);
369 int sxpr_is(Sxpr obj, char *s);
370 int sxpr_elementp(Sxpr obj, Sxpr name);
371 Sxpr sxpr_attributes(Sxpr obj);
372 Sxpr sxpr_attribute(Sxpr obj, Sxpr key, Sxpr def);
373 Sxpr sxpr_children(Sxpr obj);
374 Sxpr sxpr_child(Sxpr obj, Sxpr name, Sxpr def);
375 Sxpr sxpr_childN(Sxpr obj, int n, Sxpr def);
376 Sxpr sxpr_child0(Sxpr obj, Sxpr def);
377 Sxpr sxpr_child_value(Sxpr obj, Sxpr name, Sxpr def);
379 /** Create a new atom.
380 *
381 * @param s atom name
382 * @return new atom
383 */
384 static inline Sxpr mkatom(char *s){
385 return atom_new(s);
386 }
388 /** Create a new string sxpr.
389 *
390 * @param s string bytes (copied)
391 * @return new string
392 */
393 static inline Sxpr mkstring(char *s){
394 return string_new(s);
395 }
397 /** Create an integer sxpr.
398 *
399 * @param i value
400 * @return sxpr
401 */
402 static inline Sxpr mkint(int i){
403 return OBJI(T_UINT, i);
404 }
406 /** Create a boolean sxpr.
407 *
408 * @param b value
409 * @return sxpr
410 */
411 static inline Sxpr mkbool(int b){
412 return OBJI(T_BOOL, (b ? 1 : 0));
413 }
415 /* Constants used in parsing and printing. */
416 #define k_list_open "("
417 #define c_list_open '('
418 #define k_list_close ")"
419 #define c_list_close ')'
420 #define k_true "true"
421 #define k_false "false"
423 #define c_escape '\\'
424 #define c_single_quote '\''
425 #define c_double_quote '"'
426 #define c_string_open c_double_quote
427 #define c_string_close c_double_quote
429 #define c_data_open '<'
430 #define c_data_quote '<'
431 #define c_data_count '*'
432 //#define c_data_open '['
433 //#define c_data_close ']'
434 //#define c_binary '*'
436 #define c_var '$'
437 #define c_eval '!'
438 #define c_concat_open '{'
439 #define c_concat_close '}'
441 #endif /* ! _XUTIL_SXPR_H_ */