source: trunk/packages/xen-3.1/xen-3.1/tools/vnet/libxutil/sxpr.h @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 10.3 KB
Line 
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_
19
20#ifdef __KERNEL__
21#include <linux/config.h>
22#include <linux/types.h>
23#else
24#include <stdint.h>
25#endif
26
27#include "hash_table.h"
28#include "iostream.h"
29#include "allocate.h"
30
31/** @file
32 * Definitions for rules and sxprs.
33 */
34
35#ifndef NULL
36#define NULL 0
37#endif
38
39#ifndef TRUE
40#define TRUE 1
41#endif
42
43#ifndef FALSE
44#define FALSE 0
45#endif
46
47/** Sxpr type. */
48typedef int16_t TypeCode;
49
50/** A typed sxpr handle.*/
51typedef 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;
61
62/** Get the integer value from an sxpr.
63 *
64 * @param obj sxpr
65 * @return value
66 */
67static inline unsigned long get_ul(Sxpr obj){
68    return obj.v.ul;
69}
70
71/** Get the pointer value from an sxpr.
72 *
73 * @param obj sxpr
74 * @return value
75 */
76static inline void * get_ptr(Sxpr obj){
77    return obj.v.ptr;
78}
79
80/** Create an sxpr containing a pointer.
81 *
82 * @param ty typecode
83 * @param val pointer
84 * @return sxpr
85 */
86static inline Sxpr obj_ptr(TypeCode ty, void *val){
87    return (Sxpr){ .type= ty, .v= { .ptr= val } };
88}
89
90/** Create an sxpr containing an integer.
91 *
92 * @param ty typecode
93 * @param val integer
94 * @return sxpr
95 */
96static inline Sxpr obj_ul(TypeCode ty, unsigned long val){
97    return (Sxpr){ .type= ty, .v= { .ul= val } };
98}
99
100/** Get the type of an sxpr.
101 *
102 * @param obj sxpr
103 * @return type
104 */
105static inline TypeCode get_type(Sxpr obj){
106    return obj.type;
107}
108
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 */
115static inline int has_type(Sxpr obj, TypeCode type){
116    return get_type(obj) == type;
117}
118
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 */
125static inline int eq(Sxpr x, Sxpr y){
126    return ((get_type(x) == get_type(y)) && (get_ul(x) == get_ul(y)));
127}
128
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)
141
142/** A cons (pair or list). */
143#define T_CONS       ((TypeCode)10)
144
145/** An error. */
146#define T_ERR        ((TypeCode)40)
147/** Sxpr type to indicate out of memory. */
148#define T_NOMEM      ((TypeCode)41)
149
150typedef struct ObjString {
151    int len;
152    char data[0];
153} ObjString;
154
155/** An atom. */
156typedef struct ObjAtom {
157    Sxpr name;
158    Hashcode hashcode;
159    int interned;
160} ObjAtom;
161
162/** A cons (pair). */
163typedef struct ObjCons {
164    Sxpr car;
165    Sxpr cdr;
166} ObjCons;
167
168/** Flags for sxpr printing. */
169enum PrintFlags {
170    PRINT_RAW           = 0x001,
171    PRINT_TYPE          = 0x002,
172    PRINT_PRETTY        = 0x004,
173    PRINT_COUNTED       = 0x008,
174    PRINT_ADDR          = 0x010,
175};
176
177extern int _string_print(IOStream *io, char *str, int n, unsigned flags);
178extern int _string_print_raw(IOStream *io, char *str, int n);
179extern int _string_print_counted(IOStream *io, char *str, int n);
180extern int _string_print_quoted(IOStream *io, char *str, int n);
181extern int _string_print_string(IOStream *io, char *str, int n);
182
183/** An integer sxpr.
184 *
185 * @param ty type
186 * @param val integer value
187 */
188#define OBJI(ty, val) obj_ul(ty, val)
189
190/** Make an integer sxpr.
191 * @param x value
192 */
193#define OINT(x)       OBJI(T_UINT,  x)
194
195/** Make an error sxpr.
196 *
197 * @param x value
198 */
199#define OERR(x)       OBJI(T_ERR,   x)
200
201/** Out of memory constant. */
202#define ONOMEM        OBJI(T_NOMEM, 0)
203
204/** The `unspecified' constant. */
205#define ONONE         OBJI(T_NONE,  0)
206
207/** Empty list constant. */
208#define ONULL         OBJI(T_NULL,  0)
209
210/** False constant. */
211#define OFALSE        OBJI(T_BOOL,  0)
212
213/** True constant. */
214#define OTRUE         OBJI(T_BOOL,  1)
215
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 */
223static inline Sxpr OBJP(int ty, void *val){
224    return (val ? obj_ptr(ty, val) : ONOMEM);
225}
226
227/** Make an integer sxpr containing a pointer.
228 *
229 * @param val pointer
230 */
231static inline Sxpr PTR(void *val){
232    return OBJP(T_UINT, (void*)(val));
233}
234
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 */
242static inline Sxpr halloc(int n, int ty){
243    return OBJP(ty, allocate(n));
244}
245
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)
253
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)
264
265#define TRUEP(obj)    get_ul(obj)
266
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)
271
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)
281
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)))
286
287/** Checked version of CAR
288 *
289 * @param x sxpr
290 * @return CAR if a cons, x otherwise
291 */
292static inline Sxpr car(Sxpr x){
293    return (CONSP(x) ? CAR(x) : x);
294}
295
296/** Checked version of CDR.
297 *
298 * @param x sxpr
299 * @return CDR if a cons, null otherwise
300 */
301static inline Sxpr cdr(Sxpr x){
302    return (CONSP(x) ? CDR(x) : ONULL);
303}
304
305typedef int ObjPrintFn(IOStream *io, Sxpr obj, unsigned flags);
306typedef int ObjEqualFn(Sxpr obj, Sxpr other);
307typedef void ObjFreeFn(Sxpr obj);
308typedef Sxpr ObjCopyFn(Sxpr obj);
309
310/** An sxpr type definition. */
311typedef 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;
320
321extern int def_sxpr_type(SxprType *tydef);
322extern SxprType *get_sxpr_type(int ty);
323
324/** Free the pointer in an sxpr.
325 *
326 * @param x sxpr containing a pointer
327 */
328static inline void hfree(Sxpr x){
329    deallocate(get_ptr(x));
330}
331
332extern int objprint(IOStream *io, Sxpr x, unsigned flags);
333extern int objequal(Sxpr x, Sxpr y);
334extern void objfree(Sxpr x);
335extern Sxpr objcopy(Sxpr x);
336
337extern void cons_free_cells(Sxpr obj);
338extern Sxpr intern(char *s);
339
340extern Sxpr assoc(Sxpr k, Sxpr l);
341extern Sxpr assocq(Sxpr k, Sxpr l);
342extern Sxpr acons(Sxpr k, Sxpr v, Sxpr l);
343extern Sxpr nrev(Sxpr l);
344extern Sxpr cons_member(Sxpr l, Sxpr x);
345extern Sxpr cons_member_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
346extern int cons_subset(Sxpr s, Sxpr t);
347extern int cons_set_equal(Sxpr s, Sxpr t);
348
349#ifdef USE_GC
350extern Sxpr cons_remove(Sxpr l, Sxpr x);
351extern Sxpr cons_remove_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
352#endif
353
354extern Sxpr atom_new(char *name);
355extern char * atom_name(Sxpr obj);
356extern int atom_length(Sxpr obj);
357
358extern Sxpr string_new(char *s);
359extern Sxpr string_new_n(char *s, int n);
360extern char * string_string(Sxpr obj);
361extern int string_length(Sxpr obj);
362
363extern Sxpr cons_new(Sxpr car, Sxpr cdr);
364extern int cons_push(Sxpr *list, Sxpr elt);
365extern int cons_length(Sxpr obj);
366
367Sxpr sxpr_name(Sxpr obj);
368int sxpr_is(Sxpr obj, char *s);
369int sxpr_elementp(Sxpr obj, Sxpr name);
370Sxpr sxpr_attributes(Sxpr obj);
371Sxpr sxpr_attribute(Sxpr obj, Sxpr key, Sxpr def);
372Sxpr sxpr_children(Sxpr obj);
373Sxpr sxpr_child(Sxpr obj, Sxpr name, Sxpr def);
374Sxpr sxpr_childN(Sxpr obj, int n, Sxpr def);
375Sxpr sxpr_child0(Sxpr obj, Sxpr def);
376Sxpr sxpr_child_value(Sxpr obj, Sxpr name, Sxpr def);
377
378/** Create a new atom.
379 *
380 * @param s atom name
381 * @return new atom
382 */
383static inline Sxpr mkatom(char *s){
384    return atom_new(s);
385}
386
387/** Create a new string sxpr.
388 *
389 * @param s string bytes (copied)
390 * @return new string
391 */
392static inline Sxpr mkstring(char *s){
393    return string_new(s);
394}
395
396/** Create an integer sxpr.
397 *
398 * @param i value
399 * @return sxpr
400 */
401static inline Sxpr mkint(int i){
402    return OBJI(T_UINT, i);
403}
404
405/** Create a boolean sxpr.
406 *
407 * @param b value
408 * @return sxpr
409 */
410static inline Sxpr mkbool(int b){
411    return OBJI(T_BOOL, (b ? 1 : 0));
412}
413
414/* Constants used in parsing and printing. */
415#define k_list_open    "("
416#define c_list_open    '('
417#define k_list_close   ")"
418#define c_list_close   ')'
419#define k_true         "true"
420#define k_false        "false"
421
422#define c_escape       '\\'
423#define c_single_quote '\''
424#define c_double_quote '"'
425#define c_string_open  c_double_quote
426#define c_string_close c_double_quote
427
428#define c_data_open    '<'
429#define c_data_quote   '<'
430#define c_data_count   '*'
431//#define c_data_open    '['
432//#define c_data_close   ']'
433//#define c_binary       '*'
434
435#define c_var          '$'
436#define c_eval         '!'
437#define c_concat_open  '{'
438#define c_concat_close '}'
439
440#endif /* ! _XUTIL_SXPR_H_ */
Note: See TracBrowser for help on using the repository browser.