ia64/xen-unstable

changeset 1641:9bee11b2df02

bitkeeper revision 1.1038.1.1 (40e2cf14GDX_OUWz8w_ZyzvGOSY2gQ)

Deleted unused files.
Fix VmError typo.
author mjw@wray-m-3.hpl.hp.com
date Wed Jun 30 14:32:52 2004 +0000 (2004-06-30)
parents 24b6048583ea
children 3c4b53f2e2a1
files .rootkeys tools/libxutil/enum.c tools/libxutil/enum.h tools/libxutil/hash_table.c tools/libxutil/hash_table.h tools/libxutil/lexis.c tools/libxutil/lexis.h tools/libxutil/lzi_stream.c tools/libxutil/lzi_stream.h tools/libxutil/lzo_stream.c tools/libxutil/lzo_stream.h tools/libxutil/marshal.c tools/libxutil/marshal.h tools/libxutil/socket_stream.c tools/libxutil/socket_stream.h tools/libxutil/sxpr.c tools/libxutil/sxpr.h tools/libxutil/sxpr_parser.c tools/libxutil/sxpr_parser.h tools/libxutil/xdr.c tools/libxutil/xdr.h tools/python/xen/xend/XendDomainInfo.py
line diff
     1.1 --- a/.rootkeys	Wed Jun 30 11:07:39 2004 +0000
     1.2 +++ b/.rootkeys	Wed Jun 30 14:32:52 2004 +0000
     1.3 @@ -188,41 +188,21 @@ 40e1b09dMYB4ItGCqcMIzirdMd9I-w tools/lib
     1.4  40e033325Sjqs-_4TuzeUEprP_gYFg tools/libxutil/allocate.c
     1.5  40e03332KYz7o1bn2MG_KPbBlyoIMA tools/libxutil/allocate.h
     1.6  40e03332IyRttYoXKoJla5qCC514SQ tools/libxutil/debug.h
     1.7 -40e03332qV5tJ-GJZjo-LBCeGuEjJA tools/libxutil/enum.c
     1.8 -40e03332wwMVxfobgA1PSMTSAGLiCw tools/libxutil/enum.h
     1.9  40e03332p5Dc_owJQRuN72ymJZddFQ tools/libxutil/file_stream.c
    1.10  40e03332jWfB2viAhLSkq1WK0r_iDQ tools/libxutil/file_stream.h
    1.11  40e03332rUjNMGg11n2rN6V4DCrvOg tools/libxutil/gzip_stream.c
    1.12  40e033321O5Qg22haLoq5lpmk4tooQ tools/libxutil/gzip_stream.h
    1.13 -40e03332QrTR96tc6yS2rMBpd2mq1A tools/libxutil/hash_table.c
    1.14 -40e033325KoIb0d_uy8s7b5DUR9fPQ tools/libxutil/hash_table.h
    1.15  40e03332ihnBGzHykVwZnFmkAppb4g tools/libxutil/iostream.c
    1.16  40e03332UGwbLR4wsw4ft14p0Yw5pg tools/libxutil/iostream.h
    1.17  40e0333245DLDzJemeSVBLuutHtzEQ tools/libxutil/kernel_stream.c
    1.18  40e03332aK0GkgpDdc-PVTkWKTeOBg tools/libxutil/kernel_stream.h
    1.19 -40e03332HJ0cDcZDKDUUT-tEiBWOZw tools/libxutil/lexis.c
    1.20 -40e03332tnH9Ggzxbfi3xY9Vh2hUlg tools/libxutil/lexis.h
    1.21 -40e03332aYIW0BNBh6wXuKKn_P7Yyg tools/libxutil/lzi_stream.c
    1.22 -40e0333233voTffE4cJSMGJARfiSSQ tools/libxutil/lzi_stream.h
    1.23 -40e03332FXuMoUnfsAKSgV8X4rFbYQ tools/libxutil/lzo_stream.c
    1.24 -40e03332InJaiLfpDcIXBy2fI0RFGQ tools/libxutil/lzo_stream.h
    1.25 -40e03332a5SCuRsejHStTuWzMQNv8Q tools/libxutil/marshal.c
    1.26 -40e03332TwKyJrZQiiQfNq4vc2hpgw tools/libxutil/marshal.h
    1.27 -40e033328ccHlJuTR1FswYL_EC6LFA tools/libxutil/socket_stream.c
    1.28 -40e03332P0KVQGkmahj47aafo1X0nA tools/libxutil/socket_stream.h
    1.29  40e03332KT_tnnoAMbPVAZBB7kSOAQ tools/libxutil/string_stream.c
    1.30  40e03332-VtK6_OZa1vMHXFil8uq6w tools/libxutil/string_stream.h
    1.31 -40e03332dDtczi6YX7_mMxhYjJeAdQ tools/libxutil/sxpr.c
    1.32 -40e03332QPuyNKDOTIYVvkwK5qO-vg tools/libxutil/sxpr.h
    1.33 -40e03332Pi0_osJ3XPBi38ADPqdl4A tools/libxutil/sxpr_parser.c
    1.34 -40e033324v5QFMvWEXXzv38uUT9kHg tools/libxutil/sxpr_parser.h
    1.35  40e03332gKUInsqtxQOV4mPiMqf_dg tools/libxutil/sys_ctype.h
    1.36  40e03332Rkvq6nn_UNjzAAK_Tk9v1g tools/libxutil/sys_net.c
    1.37  40e03332lQHvQHw4Rh7VsT1_sui29A tools/libxutil/sys_net.h
    1.38  40e033321smklZd7bDSdWvQCeIshtg tools/libxutil/sys_string.c
    1.39  40e03332h5V611rRWURRLqb1Ekatxg tools/libxutil/sys_string.h
    1.40 -40e03332u4q5kgF0N7RfqB4s0pZVew tools/libxutil/xdr.c
    1.41 -40e03332hY16nfRXF4gGd5S1aUJUBw tools/libxutil/xdr.h
    1.42  3f776bd2Xd-dUcPKlPN2vG89VGtfvQ tools/misc/Makefile
    1.43  40ab2cfawIw8tsYo0dQKtp83h4qfTQ tools/misc/fakei386xen
    1.44  3f6dc136ZKOjd8PIqLbFBl_v-rnkGg tools/misc/miniterm/Makefile
     2.1 --- a/tools/libxutil/enum.c	Wed Jun 30 11:07:39 2004 +0000
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,61 +0,0 @@
     2.4 -/*
     2.5 - * Copyright (C) 2002, 2004 Mike Wray <mike.wray@hp.com>
     2.6 - *
     2.7 - * This library is free software; you can redistribute it and/or modify
     2.8 - * it under the terms of the GNU Lesser General Public License as
     2.9 - * published by the Free Software Foundation; either version 2.1 of the
    2.10 - * License, or  (at your option) any later version. This library is 
    2.11 - * distributed in the  hope that it will be useful, but WITHOUT ANY
    2.12 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.13 - * FITNESS FOR A PARTICULAR PURPOSE.
    2.14 - * See the GNU Lesser General Public License for more details.
    2.15 - *
    2.16 - * You should have received a copy of the GNU Lesser General Public License
    2.17 - * along with this library; if not, write to the Free Software Foundation,
    2.18 - * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    2.19 - */
    2.20 -
    2.21 -#ifdef __KERNEL__
    2.22 -#include <linux/errno.h>
    2.23 -#else
    2.24 -#include <errno.h>
    2.25 -#endif
    2.26 -
    2.27 -#include "sys_string.h"
    2.28 -#include "enum.h"
    2.29 -
    2.30 -/** Map an enum name to its value using a table.
    2.31 - *
    2.32 - * @param name enum name
    2.33 - * @param defs enum definitions
    2.34 - * @return enum value or -1 if not known
    2.35 - */
    2.36 -int enum_name_to_val(char *name, EnumDef *defs){
    2.37 -    int val = -1;
    2.38 -    for(; defs->name; defs++){
    2.39 -	if(!strcmp(defs->name, name)){
    2.40 -	    val = defs->val;
    2.41 -	    break;
    2.42 -	}
    2.43 -    }
    2.44 -    return val;
    2.45 -}
    2.46 -
    2.47 -/** Map an enum value to its name using a table.
    2.48 - *
    2.49 - * @param val enum value
    2.50 - * @param defs enum definitions
    2.51 - * @param defs_n number of definitions
    2.52 - * @return enum name or NULL if not known
    2.53 - */
    2.54 -char *enum_val_to_name(int val, EnumDef *defs){
    2.55 -    char *name = NULL;
    2.56 -    for(; defs->name; defs++){
    2.57 -	if(val == defs->val){
    2.58 -	    name = defs->name;
    2.59 -	    break;
    2.60 -	}
    2.61 -    }
    2.62 -    return name;
    2.63 -}
    2.64 -
     3.1 --- a/tools/libxutil/enum.h	Wed Jun 30 11:07:39 2004 +0000
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,30 +0,0 @@
     3.4 -/*
     3.5 - * Copyright (C) 2002, 2004 Mike Wray <mike.wray@hp.com>
     3.6 - *
     3.7 - * This library is free software; you can redistribute it and/or modify
     3.8 - * it under the terms of the GNU Lesser General Public License as
     3.9 - * published by the Free Software Foundation; either version 2.1 of the
    3.10 - * License, or  (at your option) any later version. This library is 
    3.11 - * distributed in the  hope that it will be useful, but WITHOUT ANY
    3.12 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.13 - * FITNESS FOR A PARTICULAR PURPOSE.
    3.14 - * See the GNU Lesser General Public License for more details.
    3.15 - *
    3.16 - * You should have received a copy of the GNU Lesser General Public License
    3.17 - * along with this library; if not, write to the Free Software Foundation,
    3.18 - * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    3.19 - */
    3.20 -
    3.21 -#ifndef _XEN_LIB_ENUM_H_
    3.22 -#define _XEN_LIB_ENUM_H_
    3.23 -
    3.24 -/** Mapping of an enum value to a name. */
    3.25 -typedef struct EnumDef {
    3.26 -    int val;
    3.27 -    char *name;
    3.28 -} EnumDef;
    3.29 -
    3.30 -extern int enum_name_to_val(char *name, EnumDef *defs);
    3.31 -extern char *enum_val_to_name(int val, EnumDef *defs);
    3.32 -
    3.33 -#endif /* _XEN_LIB_ENUM_H_ */
     4.1 --- a/tools/libxutil/hash_table.c	Wed Jun 30 11:07:39 2004 +0000
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,640 +0,0 @@
     4.4 -/*
     4.5 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
     4.6 - *
     4.7 - * This library is free software; you can redistribute it and/or modify
     4.8 - * it under the terms of the GNU Lesser General Public License as published by
     4.9 - * the Free Software Foundation; either version 2.1 of the License, or
    4.10 - * (at your option) any later version.
    4.11 - *
    4.12 - * This library is distributed in the hope that it will be useful,
    4.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 - * GNU Lesser General Public License for more details.
    4.16 - *
    4.17 - * You should have received a copy of the GNU Lesser General Public License
    4.18 - * along with this library; if not, write to the Free Software
    4.19 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    4.20 - */
    4.21 -
    4.22 -#ifdef __KERNEL__
    4.23 -#  include <linux/config.h>
    4.24 -#  include <linux/module.h>
    4.25 -#  include <linux/kernel.h>
    4.26 -#  include <linux/errno.h>
    4.27 -#else
    4.28 -#  include <errno.h>
    4.29 -#  include <stddef.h>
    4.30 -#endif
    4.31 -
    4.32 -//#include <limits.h>
    4.33 -
    4.34 -#include "allocate.h"
    4.35 -#include "hash_table.h"
    4.36 -
    4.37 -/** @file
    4.38 - * Base support for hashtables.
    4.39 - *
    4.40 - * Hash codes are reduced modulo the number of buckets to index tables,
    4.41 - * so there is no need for hash functions to limit the range of hashcodes.
    4.42 - * In fact it is assumed that hashcodes do not change when the number of
    4.43 - * buckets in the table changes.
    4.44 - */
    4.45 -
    4.46 -/*==========================================================================*/
    4.47 -/** Number of bits in half a word. */
    4.48 -//#if __WORDSIZE == 64
    4.49 -//#define HALF_WORD_BITS 32
    4.50 -//#else
    4.51 -#define HALF_WORD_BITS 16
    4.52 -//#endif
    4.53 -
    4.54 -/** Mask for lo half of a word. On 32-bit this is 
    4.55 - * (1<<16) - 1 = 65535 = 0xffff
    4.56 - * It's 4294967295 = 0xffffffff on 64-bit.
    4.57 - */
    4.58 -#define LO_HALF_MASK ((1 << HALF_WORD_BITS) - 1)
    4.59 -
    4.60 -/** Get the lo half of a word. */
    4.61 -#define LO_HALF(x) ((x) & LO_HALF_MASK)
    4.62 -
    4.63 -/** Get the hi half of a word. */
    4.64 -#define HI_HALF(x) ((x) >> HALF_WORD_BITS)
    4.65 -
    4.66 -/** Do a full hash on both inputs, using DES-style non-linear scrambling.
    4.67 - * Both inputs are replaced with the results of the hash.
    4.68 - *
    4.69 - * @param pleft input/output word
    4.70 - * @param pright input/output word
    4.71 - */
    4.72 -void pseudo_des(unsigned long *pleft, unsigned long *pright){
    4.73 -    // Bit-rich mixing constant.
    4.74 -    static const unsigned long a_mixer[] = {
    4.75 -        0xbaa96887L, 0x1e17d32cL, 0x03bcdc3cL, 0x0f33d1b2L, };
    4.76 -
    4.77 -    // Bit-rich mixing constant.
    4.78 -    static const unsigned long b_mixer[] = {
    4.79 -        0x4b0f3b58L, 0xe874f0c3L, 0x6955c5a6L, 0x55a7ca46L, };
    4.80 -
    4.81 -    // Number of iterations - must be 2 or 4.
    4.82 -    static const int ncycle = 4;
    4.83 -    //static const int ncycle = 2;
    4.84 -
    4.85 -    unsigned long left = *pleft, right = *pright;
    4.86 -    unsigned long v, v_hi, v_lo;
    4.87 -    int i;
    4.88 -
    4.89 -    for(i=0; i<ncycle; i++){
    4.90 -        // Flip some bits in right to get v.
    4.91 -        v = right;
    4.92 -        v ^= a_mixer[i];
    4.93 -        // Get lo and hi halves of v.
    4.94 -        v_lo = LO_HALF(v);
    4.95 -        v_hi = HI_HALF(v);
    4.96 -        // Non-linear mix of the halves of v.
    4.97 -        v = ((v_lo * v_lo) + ~(v_hi * v_hi));
    4.98 -        // Swap the halves of v.
    4.99 -        v = (HI_HALF(v) | (LO_HALF(v) << HALF_WORD_BITS));
   4.100 -        // Flip some bits.
   4.101 -        v ^= b_mixer[i];
   4.102 -        // More non-linear mixing.
   4.103 -        v += (v_lo * v_hi);
   4.104 -        v ^= left;
   4.105 -        left = right;
   4.106 -        right = v;
   4.107 -    }
   4.108 -    *pleft = left;
   4.109 -    *pright = right;
   4.110 -}
   4.111 -
   4.112 -/** Hash a string.
   4.113 - *
   4.114 - * @param s input to hash
   4.115 - * @return hashcode
   4.116 - */
   4.117 -Hashcode hash_string(char *s){
   4.118 -    Hashcode h = 0;
   4.119 -    if(s){
   4.120 -        for( ; *s; s++){
   4.121 -            h = hash_2ul(h, *s);
   4.122 -        }
   4.123 -    }
   4.124 -    return h;
   4.125 -}
   4.126 -
   4.127 -/** Get the bucket for a hashcode in a hash table.
   4.128 - *
   4.129 - * @param table to get bucket from
   4.130 - * @param hashcode to get bucket for
   4.131 - * @return bucket
   4.132 - */
   4.133 -inline HTBucket * get_bucket(HashTable *table, Hashcode hashcode){
   4.134 -    return table->buckets + (hashcode % table->buckets_n);
   4.135 -}
   4.136 -
   4.137 -/** Initialize a hash table.
   4.138 - * Can be safely called more than once.
   4.139 - *
   4.140 - * @param table to initialize
   4.141 - */
   4.142 -void HashTable_init(HashTable *table){
   4.143 -    int i;
   4.144 -
   4.145 -    if(!table->init_done){
   4.146 -        table->init_done = 1;
   4.147 -        table->next_id = 0;
   4.148 -        for(i=0; i<table->buckets_n; i++){
   4.149 -            HTBucket *bucket = get_bucket(table, i);
   4.150 -            bucket->head = 0;
   4.151 -            bucket->count = 0;
   4.152 -        }
   4.153 -        table->entry_count = 0;
   4.154 -    }
   4.155 -}
   4.156 -
   4.157 -/** Allocate a new hashtable.
   4.158 - * If the number of buckets is not positive the default is used.
   4.159 - * The number of buckets should usually be prime.
   4.160 - *
   4.161 - * @param buckets_n number of buckets
   4.162 - * @return new hashtable or null
   4.163 - */
   4.164 -HashTable *HashTable_new(int buckets_n){
   4.165 -    HashTable *z = ALLOCATE(HashTable);
   4.166 -    if(!z) goto exit;
   4.167 -    if(buckets_n <= 0){
   4.168 -        buckets_n = HT_BUCKETS_N;
   4.169 -    }
   4.170 -    z->buckets = (HTBucket*)allocate(buckets_n * sizeof(HTBucket));
   4.171 -    if(!z->buckets){
   4.172 -        deallocate(z);
   4.173 -        z = 0;
   4.174 -        goto exit;
   4.175 -    }
   4.176 -    z->buckets_n = buckets_n;
   4.177 -    HashTable_init(z);
   4.178 -  exit:
   4.179 -    return z;
   4.180 -}
   4.181 -
   4.182 -/** Free a hashtable.
   4.183 - * Any entries are removed and freed.
   4.184 - *
   4.185 - * @param h hashtable (ignored if null)
   4.186 - */
   4.187 -void HashTable_free(HashTable *h){
   4.188 -    if(h){
   4.189 -        HashTable_clear(h);
   4.190 -        deallocate(h->buckets);
   4.191 -        deallocate(h);
   4.192 -    }
   4.193 -}
   4.194 -
   4.195 -/** Push an entry on the list in the bucket for a given hashcode.
   4.196 - *
   4.197 - * @param table to add entry to
   4.198 - * @param hashcode for the entry
   4.199 - * @param entry to add
   4.200 - */
   4.201 -static inline void push_on_bucket(HashTable *table, Hashcode hashcode,
   4.202 -				  HTEntry *entry){
   4.203 -    HTBucket *bucket;
   4.204 -    HTEntry *old_head;
   4.205 -
   4.206 -    bucket = get_bucket(table, hashcode);
   4.207 -    old_head = bucket->head;
   4.208 -    bucket->count++;
   4.209 -    bucket->head = entry;
   4.210 -    entry->next = old_head;
   4.211 -}
   4.212 -
   4.213 -/** Change the number of buckets in a hashtable.
   4.214 - * No-op if the number of buckets is not positive.
   4.215 - * Existing entries are reallocated to buckets based on their hashcodes.
   4.216 - * The table is unmodified if the number of buckets cannot be changed.
   4.217 - *
   4.218 - * @param table hashtable
   4.219 - * @param buckets_n new number of buckets
   4.220 - * @return 0 on success, error code otherwise
   4.221 - */
   4.222 -int HashTable_set_buckets_n(HashTable *table, int buckets_n){
   4.223 -    int err = 0;
   4.224 -    HTBucket *old_buckets = table->buckets;
   4.225 -    int old_buckets_n = table->buckets_n;
   4.226 -    int i;
   4.227 -
   4.228 -    if(buckets_n <= 0){
   4.229 -        err = -EINVAL;
   4.230 -        goto exit;
   4.231 -    }
   4.232 -    table->buckets = (HTBucket*)allocate(buckets_n * sizeof(HTBucket));
   4.233 -    if(!table->buckets){
   4.234 -        err = -ENOMEM;
   4.235 -        table->buckets = old_buckets;
   4.236 -        goto exit;
   4.237 -    }
   4.238 -    table->buckets_n = buckets_n;
   4.239 -    for(i=0; i<old_buckets_n; i++){
   4.240 -        HTBucket *bucket = old_buckets + i;
   4.241 -        HTEntry *entry, *next;
   4.242 -        for(entry = bucket->head; entry; entry = next){
   4.243 -            next = entry->next;
   4.244 -            push_on_bucket(table, entry->hashcode, entry);
   4.245 -        }
   4.246 -    }
   4.247 -    deallocate(old_buckets);
   4.248 -  exit:
   4.249 -    return err;
   4.250 -}
   4.251 -
   4.252 -/** Adjust the number of buckets so the table is neither too full nor too empty.
   4.253 - * The table is unmodified if adjusting fails.
   4.254 - *
   4.255 - * @param table hash table
   4.256 - * @param buckets_min minimum number of buckets (use default if 0 or negative)
   4.257 - * @return 0 on success, error code otherwise
   4.258 - */
   4.259 -int HashTable_adjust(HashTable *table, int buckets_min){
   4.260 -    int buckets_n = 0;
   4.261 -    int err = 0;
   4.262 -    if(buckets_min <= 0) buckets_min = HT_BUCKETS_N;
   4.263 -    if(table->entry_count >= table->buckets_n){
   4.264 -        // The table is dense - expand it.
   4.265 -        buckets_n = 2 * table->buckets_n;
   4.266 -    } else if((table->buckets_n > buckets_min) &&
   4.267 -              (4 * table->entry_count < table->buckets_n)){
   4.268 -        // The table is more than minimum size and sparse - shrink it.
   4.269 -        buckets_n = 2 * table->entry_count;
   4.270 -        if(buckets_n < buckets_min) buckets_n = buckets_min;
   4.271 -    }
   4.272 -    if(buckets_n){
   4.273 -        err = HashTable_set_buckets_n(table, buckets_n);
   4.274 -    }
   4.275 -    return err;
   4.276 -}
   4.277 -
   4.278 -/** Allocate a new entry for a given value.
   4.279 - *
   4.280 - * @param value to put in the entry
   4.281 - * @return entry, or 0 on failure
   4.282 - */
   4.283 -HTEntry * HTEntry_new(Hashcode hashcode, void *key, void *value){
   4.284 -    HTEntry *z = ALLOCATE(HTEntry);
   4.285 -    if(z){
   4.286 -        z->hashcode = hashcode;
   4.287 -        z->key = key;
   4.288 -        z->value = value;
   4.289 -    }
   4.290 -    return z;
   4.291 -}
   4.292 -
   4.293 -/** Free an entry.
   4.294 - *
   4.295 - * @param z entry to free
   4.296 - */
   4.297 -inline void HTEntry_free(HTEntry *z){
   4.298 -    if(z){
   4.299 -        deallocate(z);
   4.300 -    }
   4.301 -}
   4.302 -
   4.303 -/** Free an entry in a hashtable.
   4.304 - * The table's entry_free_fn is used is defined, otherwise 
   4.305 - * the HTEntry itself is freed.
   4.306 - *
   4.307 - * @param table hashtable
   4.308 - * @param entry to free
   4.309 - */
   4.310 -inline void HashTable_free_entry(HashTable *table, HTEntry *entry){
   4.311 -    if(!entry)return;
   4.312 -    if(table && table->entry_free_fn){
   4.313 -        table->entry_free_fn(table, entry);
   4.314 -    } else {
   4.315 -        HTEntry_free(entry);
   4.316 -    }
   4.317 -}
   4.318 -
   4.319 -/** Get the first entry satisfying a test from the bucket for the
   4.320 - * given hashcode.
   4.321 - *
   4.322 - * @param table to look in
   4.323 - * @param hashcode indicates the bucket
   4.324 - * @param test_fn test to apply to elements
   4.325 - * @param arg first argument to calls to test_fn
   4.326 - * @return entry found, or 0
   4.327 - */
   4.328 -inline HTEntry * HashTable_find_entry(HashTable *table, Hashcode hashcode,
   4.329 -				      TableTestFn *test_fn, TableArg arg){
   4.330 -    HTBucket *bucket;
   4.331 -    HTEntry *entry = 0;
   4.332 -    HTEntry *next;
   4.333 -
   4.334 -    bucket = get_bucket(table, hashcode);
   4.335 -    for(entry = bucket->head; entry; entry = next){
   4.336 -        next = entry->next;
   4.337 -        if(test_fn(arg, table, entry)){
   4.338 -            break;
   4.339 -        }
   4.340 -    }
   4.341 -    return entry;
   4.342 -}
   4.343 -
   4.344 -/** Test hashtable keys for equality.
   4.345 - * Uses the table's key_equal_fn if defined, otherwise pointer equality.
   4.346 - *
   4.347 - * @param key1 key to compare
   4.348 - * @param key2 key to compare
   4.349 - * @return 1 if equal, 0 otherwise
   4.350 - */
   4.351 -inline int HashTable_key_equal(HashTable *table, void *key1, void *key2){
   4.352 -    return (table->key_equal_fn ? table->key_equal_fn(key1, key2) : key1==key2);
   4.353 -}
   4.354 -
   4.355 -/** Compute the hashcode of a hashtable key.
   4.356 - * The table's key_hash_fn is used if defined, otherwise the address of
   4.357 - * the key is hashed.
   4.358 - *
   4.359 - * @param table hashtable
   4.360 - * @param key to hash
   4.361 - * @return hashcode
   4.362 - */
   4.363 -inline Hashcode HashTable_key_hash(HashTable *table, void *key){
   4.364 -    return (table->key_hash_fn ? table->key_hash_fn(key) : hash_ul((unsigned long)key));
   4.365 -}
   4.366 -
   4.367 -/** Test if an entry has a given key.
   4.368 - *
   4.369 - * @param arg containing key to test for
   4.370 - * @param table the entry is in
   4.371 - * @param entry to test
   4.372 - * @return 1 if the entry has the key, 0 otherwise
   4.373 - */
   4.374 -static inline int has_key(TableArg arg, HashTable *table, HTEntry *entry){
   4.375 -    return HashTable_key_equal(table, arg.ptr, entry->key);
   4.376 -}
   4.377 -
   4.378 -/** Get an entry with a given key.
   4.379 - *
   4.380 - * @param table to search
   4.381 - * @param key to look for
   4.382 - * @return entry if found, null otherwise
   4.383 - */
   4.384 -#if 0
   4.385 -inline HTEntry * HashTable_get_entry(HashTable *table, void *key){
   4.386 -    TableArg arg = { ptr: key };
   4.387 -    return HashTable_find_entry(table, HashTable_key_hash(table, key), has_key, arg);
   4.388 -}
   4.389 -#else
   4.390 -inline HTEntry * HashTable_get_entry(HashTable *table, void *key){
   4.391 -    Hashcode hashcode;
   4.392 -    HTBucket *bucket;
   4.393 -    HTEntry *entry = 0;
   4.394 -    HTEntry *next;
   4.395 -
   4.396 -    hashcode = HashTable_key_hash(table, key);
   4.397 -    bucket = get_bucket(table, hashcode);
   4.398 -    for(entry = bucket->head; entry; entry = next){
   4.399 -        next = entry->next;
   4.400 -        if(HashTable_key_equal(table, key, entry->key)){
   4.401 -            break;
   4.402 -        }
   4.403 -    }
   4.404 -    return entry;
   4.405 -}
   4.406 -#endif
   4.407 -
   4.408 -/** Get the value of an entry with a given key.
   4.409 - *
   4.410 - * @param table to search
   4.411 - * @param key to look for
   4.412 - * @return value if an entry was found, null otherwise
   4.413 - */
   4.414 -inline void * HashTable_get(HashTable *table, void *key){
   4.415 -    HTEntry *entry = HashTable_get_entry(table, key);
   4.416 -    return (entry ? entry->value : 0);
   4.417 -}
   4.418 -
   4.419 -/** Print the buckets in a table.
   4.420 - *
   4.421 - * @param table to print
   4.422 - */
   4.423 -void show_buckets(HashTable *table, IOStream *io){
   4.424 -    int i,j ;
   4.425 -    IOStream_print(io, "entry_count=%d buckets_n=%d\n", table->entry_count, table->buckets_n);
   4.426 -    for(i=0; i<table->buckets_n; i++){
   4.427 -        if(0 || table->buckets[i].count>0){
   4.428 -            IOStream_print(io, "bucket %3d %3d %10p ", i,
   4.429 -                        table->buckets[i].count,
   4.430 -                        table->buckets[i].head);
   4.431 -            for(j = table->buckets[i].count; j>0; j--){
   4.432 -                IOStream_print(io, "+");
   4.433 -            }
   4.434 -            IOStream_print(io, "\n");
   4.435 -        }
   4.436 -    }
   4.437 -    HashTable_print(table, io); 
   4.438 -}
   4.439 -    
   4.440 -/** Print an entry in a table.
   4.441 - *
   4.442 - * @param entry to print
   4.443 - * @param arg a pointer to an IOStream to print to
   4.444 - * @return 0
   4.445 - */
   4.446 -static int print_entry(TableArg arg, HashTable *table, HTEntry *entry){
   4.447 -    IOStream *io = (IOStream*)arg.ptr;
   4.448 -    IOStream_print(io, " b=%4lx h=%08lx i=%08lx |-> e=%8p k=%8p v=%8p\n",
   4.449 -                entry->hashcode % table->buckets_n,
   4.450 -                entry->hashcode,
   4.451 -                entry->index,
   4.452 -                entry, entry->key, entry->value);
   4.453 -    return 0;
   4.454 -}
   4.455 -
   4.456 -/** Print a hash table.
   4.457 - *
   4.458 - * @param table to print
   4.459 - */
   4.460 -void HashTable_print(HashTable *table, IOStream *io){
   4.461 -    IOStream_print(io, "{\n");
   4.462 -    HashTable_map(table, print_entry, (TableArg){ ptr: io });
   4.463 -    IOStream_print(io, "}\n");
   4.464 -}
   4.465 -/*==========================================================================*/
   4.466 -
   4.467 -/** Get the next entry id to use for a table.
   4.468 - *
   4.469 - * @param table hash table
   4.470 - * @return non-zero entry id
   4.471 - */
   4.472 -static inline unsigned long get_next_id(HashTable *table){
   4.473 -    unsigned long id;
   4.474 -
   4.475 -    if(table->next_id == 0){
   4.476 -        table->next_id = 1;
   4.477 -    }
   4.478 -    id = table->next_id++;
   4.479 -    return id;
   4.480 -}
   4.481 -
   4.482 -/** Add an entry to the bucket for the
   4.483 - * given hashcode.
   4.484 - *
   4.485 - * @param table to insert in
   4.486 - * @param hashcode indicates the bucket
   4.487 - * @param key to add an entry for
   4.488 - * @param value to add an entry for
   4.489 - * @return entry on success, 0 on failure
   4.490 - */
   4.491 -inline HTEntry * HashTable_add_entry(HashTable *table, Hashcode hashcode, void *key, void *value){
   4.492 -    HTEntry *entry = HTEntry_new(hashcode, key, value);
   4.493 -    if(entry){
   4.494 -        entry->index = get_next_id(table);
   4.495 -        push_on_bucket(table, hashcode, entry);
   4.496 -        table->entry_count++;
   4.497 -    }
   4.498 -    return entry;
   4.499 -}
   4.500 -
   4.501 -/** Move the front entry for a bucket to the correct point in the bucket order as
   4.502 - * defined by the order function. If this is called every time a new entry is added
   4.503 - * the bucket will be maintained in sorted order.
   4.504 - *
   4.505 - * @param table to modify
   4.506 - * @param hashcode indicates the bucket
   4.507 - * @param order entry comparison function
   4.508 - * @return 0 if an entry was moved, 1 if not
   4.509 - */
   4.510 -int HashTable_order_bucket(HashTable *table, Hashcode hashcode, TableOrderFn *order){
   4.511 -    HTEntry *new_entry = NULL, *prev = NULL, *entry = NULL;
   4.512 -    HTBucket *bucket;
   4.513 -    int err = 1;
   4.514 -
   4.515 -    bucket = get_bucket(table, hashcode);
   4.516 -    new_entry = bucket->head;
   4.517 -    if(!new_entry || !new_entry->next) goto exit;
   4.518 -    for(entry = new_entry->next; entry; prev = entry, entry = entry->next){
   4.519 -        if(order(new_entry, entry) <= 0) break;
   4.520 -    }
   4.521 -    if(prev){
   4.522 -        err = 0;
   4.523 -        bucket->head = new_entry->next; 
   4.524 -        new_entry->next = entry;
   4.525 -        prev->next = new_entry;
   4.526 -    }
   4.527 -  exit:
   4.528 -    return err;
   4.529 -}
   4.530 -
   4.531 -/** Add an entry to a hashtable.
   4.532 - * The entry is added to the bucket for its key's hashcode.
   4.533 - *
   4.534 - * @param table to insert in
   4.535 - * @param key to add an entry for
   4.536 - * @param value to add an entry for
   4.537 - * @return entry on success, 0 on failure
   4.538 - */
   4.539 -inline HTEntry * HashTable_add(HashTable *table, void *key, void *value){
   4.540 -    return HashTable_add_entry(table, HashTable_key_hash(table, key), key, value);
   4.541 -}
   4.542 -
   4.543 -
   4.544 -/** Remove entries satisfying a test from the bucket for the
   4.545 - * given hashcode. 
   4.546 - *
   4.547 - * @param table to remove from
   4.548 - * @param hashcode indicates the bucket
   4.549 - * @param test_fn test to apply to elements
   4.550 - * @param arg first argument to calls to test_fn
   4.551 - * @return number of entries removed
   4.552 - */
   4.553 -inline int HashTable_remove_entry(HashTable *table, Hashcode hashcode,
   4.554 -				  TableTestFn *test_fn, TableArg arg){
   4.555 -    HTBucket *bucket;
   4.556 -    HTEntry *entry, *prev = 0, *next;
   4.557 -    int removed_count = 0;
   4.558 -
   4.559 -    bucket = get_bucket(table, hashcode);
   4.560 -    for(entry = bucket->head; entry; entry = next){
   4.561 -        next = entry->next;
   4.562 -        if(test_fn(arg, table, entry)){
   4.563 -            if(prev){
   4.564 -                prev->next = next;
   4.565 -            } else {
   4.566 -                bucket->head = next;
   4.567 -            }
   4.568 -            bucket->count--;
   4.569 -            table->entry_count--;
   4.570 -            removed_count++;
   4.571 -            HashTable_free_entry(table, entry);
   4.572 -            entry = 0;
   4.573 -        }
   4.574 -        prev = entry;
   4.575 -    }
   4.576 -    return removed_count;
   4.577 -}
   4.578 -
   4.579 -/** Remove entries with a given key. 
   4.580 - *
   4.581 - * @param table to remove from
   4.582 - * @param key of entries to remove
   4.583 - * @return number of entries removed
   4.584 - */
   4.585 -inline int HashTable_remove(HashTable *table, void *key){
   4.586 -#if 1
   4.587 -    Hashcode hashcode;
   4.588 -    HTBucket *bucket;
   4.589 -    HTEntry *entry, *prev = 0, *next;
   4.590 -    int removed_count = 0;
   4.591 -
   4.592 -    hashcode = HashTable_key_hash(table, key);
   4.593 -    bucket = get_bucket(table, hashcode);
   4.594 -    for(entry = bucket->head; entry; entry = next){
   4.595 -        next = entry->next;
   4.596 -        if(HashTable_key_equal(table, key, entry->key)){
   4.597 -            if(prev){
   4.598 -                prev->next = next;
   4.599 -            } else {
   4.600 -                bucket->head = next;
   4.601 -            }
   4.602 -            bucket->count--;
   4.603 -            table->entry_count--;
   4.604 -            removed_count++;
   4.605 -            HashTable_free_entry(table, entry);
   4.606 -            entry = 0;
   4.607 -        }
   4.608 -        prev = entry;
   4.609 -    }
   4.610 -    return removed_count;
   4.611 -#else
   4.612 -    return HashTable_remove_entry(table, HashTable_key_hash(table, key),
   4.613 -				  has_key, (TableArg){ ptr: key});
   4.614 -#endif
   4.615 -}
   4.616 -
   4.617 -/** Remove (and free) all the entries in a bucket.
   4.618 - *
   4.619 - * @param bucket to clear
   4.620 - */
   4.621 -static inline void bucket_clear(HashTable *table, HTBucket *bucket){
   4.622 -    HTEntry *entry, *next;
   4.623 -
   4.624 -    for(entry = bucket->head; entry; entry = next){
   4.625 -        next = entry->next;
   4.626 -        HashTable_free_entry(table, entry);
   4.627 -    }
   4.628 -    bucket->head = 0;
   4.629 -    table->entry_count -= bucket->count;
   4.630 -    bucket->count = 0;
   4.631 -}
   4.632 -
   4.633 -/** Remove (and free) all the entries in a table.
   4.634 - *
   4.635 - * @param table to clear
   4.636 - */
   4.637 -void HashTable_clear(HashTable *table){
   4.638 -    int i, n = table->buckets_n;
   4.639 -
   4.640 -    for(i=0; i<n; i++){
   4.641 -        bucket_clear(table, table->buckets + i);
   4.642 -    }
   4.643 -}
     5.1 --- a/tools/libxutil/hash_table.h	Wed Jun 30 11:07:39 2004 +0000
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,295 +0,0 @@
     5.4 -/* $Id: hash_table.h,v 1.1 2004/03/30 16:21:26 mjw Exp $ */
     5.5 -/*
     5.6 - * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
     5.7 - *
     5.8 - * This library is free software; you can redistribute it and/or modify
     5.9 - * it under the terms of the GNU Lesser General Public License as published by
    5.10 - * the Free Software Foundation; either version 2.1 of the License, or
    5.11 - * (at your option) any later version.
    5.12 - *
    5.13 - * This library is distributed in the hope that it will be useful,
    5.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.16 - * GNU Lesser General Public License for more details.
    5.17 - *
    5.18 - * You should have received a copy of the GNU Lesser General Public License
    5.19 - * along with this library; if not, write to the Free Software
    5.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    5.21 - */
    5.22 -
    5.23 -#ifndef _XEN_LIB_HASH_TABLE_H_
    5.24 -#define _XEN_LIB_HASH_TABLE_H_
    5.25 -
    5.26 -#include "iostream.h"
    5.27 -
    5.28 -typedef unsigned long Hashcode;
    5.29 -
    5.30 -/** Type used to pass parameters to table functions. */
    5.31 -typedef union TableArg {
    5.32 -    unsigned long ul;
    5.33 -    void *ptr;
    5.34 -} TableArg;
    5.35 -
    5.36 -/** An entry in a bucket list. */
    5.37 -typedef struct HTEntry {
    5.38 -    /** Hashcode of the entry's key. */
    5.39 -    Hashcode hashcode;
    5.40 -    /** Identifier for this entry in the table. */
    5.41 -    int index;
    5.42 -    /** The key for this entry. */
    5.43 -    void *key;
    5.44 -    /** The value in this entry. */
    5.45 -    void *value;
    5.46 -    /** The next entry in the list. */
    5.47 -    struct HTEntry *next;
    5.48 -} HTEntry;
    5.49 -
    5.50 -/** A bucket in a rule table. */
    5.51 -typedef struct HTBucket {
    5.52 -    /** Number of entries in the bucket. */
    5.53 -    int count;
    5.54 -    /** First entry in the bucket (may be null). */
    5.55 -    HTEntry *head;
    5.56 -} HTBucket;
    5.57 -
    5.58 -/** Default number of buckets in a hash table.
    5.59 - * You want enough buckets so the lists in the buckets will typically be short.
    5.60 - * It's a good idea if this is prime, since that will help to spread hashcodes
    5.61 - * around the table.
    5.62 - */
    5.63 -//#define HT_BUCKETS_N 1
    5.64 -//#define HT_BUCKETS_N 3
    5.65 -//#define HT_BUCKETS_N 7
    5.66 -//#define HT_BUCKETS_N 17
    5.67 -//#define HT_BUCKETS_N 97
    5.68 -//#define HT_BUCKETS_N 211
    5.69 -//#define HT_BUCKETS_N 401
    5.70 -#define HT_BUCKETS_N 1021
    5.71 -
    5.72 -typedef struct HashTable HashTable;
    5.73 -
    5.74 -/** Type for a function used to select table entries. */
    5.75 -typedef int TableTestFn(TableArg arg, HashTable *table, HTEntry *entry);
    5.76 -
    5.77 -/** Type for a function to map over table entries. */
    5.78 -typedef int TableMapFn(TableArg arg, HashTable *table, HTEntry *entry);
    5.79 -
    5.80 -/** Type for a function to free table entries. */
    5.81 -typedef void TableFreeFn(HashTable *table, HTEntry *entry);
    5.82 -
    5.83 -/** Type for a function to hash table keys. */
    5.84 -typedef Hashcode TableHashFn(void *key);
    5.85 -
    5.86 -/** Type for a function to test table keys for equality. */
    5.87 -typedef int TableEqualFn(void *key1, void *key2);
    5.88 -
    5.89 -/** Type for a function to order table entries. */
    5.90 -typedef int TableOrderFn(HTEntry *e1, HTEntry *e2);
    5.91 -
    5.92 -/** General hash table.
    5.93 - * A hash table with a list in each bucket.
    5.94 - * Functions can be supplied for freeing entries, hashing keys, and comparing keys.
    5.95 - * These all default to 0, when default behaviour treating keys as integers is used.
    5.96 - */
    5.97 -struct HashTable {
    5.98 -    /** Flag indicating whether the table has been initialised. */
    5.99 -    int init_done;
   5.100 -    /** Next value for the id field in inserted rules. */
   5.101 -    unsigned long next_id;
   5.102 -    /** Number of buckets in the bucket array. */
   5.103 -    int buckets_n;
   5.104 -    /** Array of buckets, each with its own list. */
   5.105 -    HTBucket *buckets;
   5.106 -    /** Number of entries in the table. */
   5.107 -    int entry_count;
   5.108 -    /** Function to free keys and values in entries. */
   5.109 -    TableFreeFn *entry_free_fn;
   5.110 -    /** Function to hash keys. */
   5.111 -    TableHashFn *key_hash_fn;
   5.112 -    /** Function to compare keys for equality. */
   5.113 -    TableEqualFn *key_equal_fn;
   5.114 -    /** Place for the user of the table to hang extra data. */
   5.115 -    void *user_data;
   5.116 -};
   5.117 -
   5.118 -extern HashTable *HashTable_new(int bucket_n);
   5.119 -extern void HashTable_free(HashTable *table);
   5.120 -extern HTEntry * HTEntry_new(Hashcode hashcode, void *key, void *value);
   5.121 -extern void HTEntry_free(HTEntry *entry);
   5.122 -extern int HashTable_set_bucket_n(HashTable *table, int bucket_n);
   5.123 -extern void HashTable_clear(HashTable *table);
   5.124 -extern HTEntry * HashTable_add_entry(HashTable *table, Hashcode hashcode, void *key, void *value);
   5.125 -extern HTEntry * HashTable_get_entry(HashTable *table, void *key);
   5.126 -extern HTEntry * HashTable_add(HashTable *table, void *key, void *value);
   5.127 -extern void * HashTable_get(HashTable *table, void *key);
   5.128 -extern int HashTable_remove(HashTable *table, void *key);
   5.129 -extern HTEntry * HashTable_find_entry(HashTable *table, Hashcode hashcode,
   5.130 -                                      TableTestFn *test_fn, TableArg arg);
   5.131 -extern int HashTable_remove_entry(HashTable *table, Hashcode hashcode,
   5.132 -                                   TableTestFn *test_fn, TableArg arg);
   5.133 -//extern int HashTable_map(HashTable *table, TableMapFn *map_fn, TableArg arg);
   5.134 -extern void HashTable_print(HashTable *table, IOStream *out);
   5.135 -extern int HashTable_set_buckets_n(HashTable *table, int buckets_n);
   5.136 -extern int HashTable_adjust(HashTable *table, int buckets_min);
   5.137 -extern void pseudo_des(unsigned long *pleft, unsigned long *pright);
   5.138 -extern Hashcode hash_string(char *s);
   5.139 -
   5.140 -extern int HashTable_order_bucket(HashTable *table, Hashcode hashcode, TableOrderFn *order);
   5.141 -
   5.142 -/** Control whether to use hashing based on DES or simple
   5.143 - * hashing. DES hashing is `more random' but much more expensive.
   5.144 - */
   5.145 -#define HASH_PSEUDO_DES 0
   5.146 -
   5.147 -/** Hash a long using a quick and dirty linear congruential random number generator.
   5.148 - *  See `Numerical Recipes in C', Chapter 7, "An Even Quicker Generator".
   5.149 - *
   5.150 - * @param a value to hash
   5.151 - * @return hashed input
   5.152 - */
   5.153 -static inline unsigned long lcrng_hash(unsigned long a){
   5.154 -    return (1664525L * a + 1013904223L);
   5.155 -}
   5.156 -
   5.157 -/** Hash an unsigned long.
   5.158 - *
   5.159 - * @param a input to hash
   5.160 - * @return hashcode
   5.161 - */
   5.162 -static inline Hashcode hash_ul(unsigned long a){
   5.163 -#if HASH_PSEUDO_DES
   5.164 -    unsigned long left = a;
   5.165 -    unsigned long right = 0L;
   5.166 -    pseudo_des(&left, &right);
   5.167 -    return right;
   5.168 -#else
   5.169 -    a = lcrng_hash(a);
   5.170 -    a = lcrng_hash(a);
   5.171 -    return a;
   5.172 -#endif
   5.173 -}
   5.174 -
   5.175 -/** Hash two unsigned longs together.
   5.176 - *
   5.177 - * @param a input to hash
   5.178 - * @param b input to hash
   5.179 - * @return hashcode
   5.180 - */
   5.181 -static inline Hashcode hash_2ul(unsigned long a, unsigned long b){
   5.182 -#if HASH_PSEUDO_DES
   5.183 -    unsigned long left = a;
   5.184 -    unsigned long right = b;
   5.185 -    pseudo_des(&left, &right);
   5.186 -    return right;
   5.187 -#else
   5.188 -    a = lcrng_hash(a);
   5.189 -    a ^= b;
   5.190 -    a = lcrng_hash(a);
   5.191 -    return a;
   5.192 -#endif
   5.193 -}
   5.194 -
   5.195 -/** Hash a hashcode and an unsigned long together.
   5.196 - *
   5.197 - * @param a input hashcode
   5.198 - * @param b input to hash
   5.199 - * @return hashcode
   5.200 - */
   5.201 -static inline Hashcode hash_hul(Hashcode a, unsigned long b){
   5.202 -#if HASH_PSEUDO_DES
   5.203 -    unsigned long left = a;
   5.204 -    unsigned long right = b;
   5.205 -    pseudo_des(&left, &right);
   5.206 -    return right;
   5.207 -#else
   5.208 -    a ^= b;
   5.209 -    a = lcrng_hash(a);
   5.210 -    return a;
   5.211 -#endif
   5.212 -}
   5.213 -
   5.214 -/** Macro to declare variables for HashTable_for_each() to use.
   5.215 - *
   5.216 - * @param entry variable that is set to entries in the table
   5.217 - */
   5.218 -#define HashTable_for_decl(entry) \
   5.219 -  HashTable *_var_table; \
   5.220 -  HTBucket *_var_bucket; \
   5.221 -  HTBucket *_var_end; \
   5.222 -  HTEntry *_var_next; \
   5.223 -  HTEntry *entry
   5.224 -
   5.225 -/** Macro to iterate over the entries in a hashtable.
   5.226 - * Must be in a scope where HashTable_for_decl() has been used to declare
   5.227 - * variables for it to use.
   5.228 - * The variable 'entry' is iterated over entries in the table.
   5.229 - * The code produced is syntactically a loop, so it must be followed by
   5.230 - * a loop body, typically some statements in braces:
   5.231 - * HashTable_for_each(entry, table){ ...loop body... }
   5.232 - *
   5.233 - * HashTable_for_each() and HashTable_for_decl() cannot be used for nested
   5.234 - * loops as variables will clash.
   5.235 - *
   5.236 - * @note The simplest way to code a direct loop over the entries in a hashtable
   5.237 - * is to use a loop over the buckets, with a nested loop over the entries
   5.238 - * in a bucket. Using this approach in a macro means the macro contains
   5.239 - * an opening brace, and calls to it must be followed by 2 braces!
   5.240 - * To avoid this the code has been restructured so that it is a for loop.
   5.241 - * So that statements could be used in the test expression of the for loop,
   5.242 - * we have used the gcc statement expression extension ({ ... }).
   5.243 - *
   5.244 - * @param entry variable to iterate over the entries
   5.245 - * @param table to iterate over (non-null)
   5.246 - */
   5.247 -#define HashTable_for_each(entry, table) \
   5.248 -  _var_table = table; \
   5.249 -  _var_bucket = _var_table->buckets; \
   5.250 -  _var_end = _var_bucket + _var_table->buckets_n; \
   5.251 -  for(entry=0, _var_next=0; \
   5.252 -      ({ if(_var_next){ \
   5.253 -             entry = _var_next; \
   5.254 -             _var_next = entry->next; \
   5.255 -          } else { \
   5.256 -             while(_var_bucket < _var_end){ \
   5.257 -                 entry = _var_bucket->head; \
   5.258 -                 _var_bucket++; \
   5.259 -                 if(entry){ \
   5.260 -                      _var_next = entry->next; \
   5.261 -                      break; \
   5.262 -                 } \
   5.263 -             } \
   5.264 -          }; \
   5.265 -         entry; }); \
   5.266 -      entry = _var_next )
   5.267 -
   5.268 -/** Map a function over the entries in a table.
   5.269 - * Mapping stops when the function returns a non-zero value.
   5.270 - * Uses the gcc statement expression extension ({ ... }).
   5.271 - *
   5.272 - * @param table to map over
   5.273 - * @param fn function to apply to entries
   5.274 - * @param arg first argument to call the function with
   5.275 - * @return 0 if fn always returned 0, first non-zero value otherwise
   5.276 - */
   5.277 -#define HashTable_map(table, fn, arg) \
   5.278 -  ({ HashTable_for_decl(_var_entry); \
   5.279 -    TableArg _var_arg = arg; \
   5.280 -    int _var_value = 0; \
   5.281 -    HashTable_for_each(_var_entry, table){ \
   5.282 -        if((_var_value = fn(_var_arg, _var_table, _var_entry))) break; \
   5.283 -    } \
   5.284 -    _var_value; })
   5.285 -
   5.286 -/** Cast x to the type for a key or value in a hash table.
   5.287 - * This avoids compiler warnings when using short integers
   5.288 - * as keys or values (especially on 64-bit platforms).
   5.289 - */
   5.290 -#define HKEY(x) ((void*)(unsigned long)(x))
   5.291 -
   5.292 -/** Cast x from the type for a key or value in a hash table.
   5.293 - * to an unsigned long. This avoids compiler warnings when using
   5.294 - * short integers as keys or values (especially on 64-bit platforms).
   5.295 - */
   5.296 -#define HVAL(x) ((unsigned long)(x))
   5.297 -
   5.298 -#endif /* !_XEN_LIB_HASH_TABLE_H_ */
     6.1 --- a/tools/libxutil/lexis.c	Wed Jun 30 11:07:39 2004 +0000
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,93 +0,0 @@
     6.4 -/*
     6.5 - *
     6.6 - * This library is free software; you can redistribute it and/or modify
     6.7 - * it under the terms of the GNU Lesser General Public License as
     6.8 - * published by the Free Software Foundation; either version 2.1 of the
     6.9 - * License, or  (at your option) any later version. This library is 
    6.10 - * distributed in the  hope that it will be useful, but WITHOUT ANY
    6.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or
    6.12 - * FITNESS FOR A PARTICULAR PURPOSE.
    6.13 - * See the GNU Lesser General Public License for more details.
    6.14 - *
    6.15 - * You should have received a copy of the GNU Lesser General Public License
    6.16 - * along with this library; if not, write to the Free Software Foundation,
    6.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    6.18 - */
    6.19 -
    6.20 -/** @file
    6.21 - * Lexical analysis.
    6.22 - */
    6.23 -
    6.24 -#include "sys_string.h"
    6.25 -#include "lexis.h"
    6.26 -#include <errno.h>
    6.27 -
    6.28 -/** Check if a value lies in a (closed) range.
    6.29 - *
    6.30 - * @param x value to test
    6.31 - * @param lo low end of the range
    6.32 - * @param hi high end of the range
    6.33 - * @return 1 if x is in the interval [lo, hi], 0 otherwise
    6.34 - */
    6.35 -inline static int in_range(int x, int lo, int hi){
    6.36 -    return (lo <= x) && (x <= hi);
    6.37 -}
    6.38 -
    6.39 -/** Determine if a string is an (unsigned) decimal number.
    6.40 - * 
    6.41 - * @param s pointer to characters to test
    6.42 - * @param n length of string
    6.43 - * @return 1 if s is a decimal number, 0 otherwise.
    6.44 - */
    6.45 -int is_decimal_number(const char *s, int n){
    6.46 -    int i;
    6.47 -    if(n <= 0)return 0;
    6.48 -    for(i = 0; i < n; i++){
    6.49 -        if(!in_decimal_digit_class(s[i])) return 0;
    6.50 -    }
    6.51 -    return 1;
    6.52 -}
    6.53 -
    6.54 -/** Determine if a string is a hex number.
    6.55 - * Hex numbers are 0, or start with 0x or 0X followed
    6.56 - * by a non-zero number of hex digits (0-9,a-f,A-F).
    6.57 - * 
    6.58 - * @param s pointer to characters to test
    6.59 - * @param n length of string
    6.60 - * @return 1 if s is a hex number, 0 otherwise.
    6.61 - */
    6.62 -int is_hex_number(const char *s, int n){
    6.63 -    int i;
    6.64 -    if(n <= 0) return 0;
    6.65 -    if(n == 1){
    6.66 -        return s[0]=='0';
    6.67 -    }
    6.68 -    if(n <= 3) return 0;
    6.69 -    if(s[0] != '0' || (s[1] != 'x' && s[1] != 'X')) return 0;
    6.70 -    for(i = 2; i < n; i++){
    6.71 -        if(!in_hex_digit_class(s[i])) return 0;
    6.72 -    }
    6.73 -    return 1;
    6.74 -}
    6.75 -
    6.76 -/** Test if a string matches a keyword.
    6.77 - * The comparison is case-insensitive.
    6.78 - * The comparison fails if either argument is null.
    6.79 - *
    6.80 - * @param s string
    6.81 - * @param k keyword
    6.82 - * @return 1 if they match, 0 otherwise
    6.83 - */
    6.84 -int is_keyword(const char *s, const char *k){
    6.85 -  return s && k && !strcasecmp(s, k);
    6.86 -}
    6.87 -
    6.88 -/** Test if a string matches a character.
    6.89 - *
    6.90 - * @param s string
    6.91 - * @param c character (non-null)
    6.92 - * @return 1 if s contains exactly c, 0 otherwise
    6.93 - */
    6.94 -int is_keychar(const char *s, char c){
    6.95 -  return c && (s[0] == c) && !s[1];
    6.96 -}
     7.1 --- a/tools/libxutil/lexis.h	Wed Jun 30 11:07:39 2004 +0000
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,122 +0,0 @@
     7.4 -/*
     7.5 - *
     7.6 - * This library is free software; you can redistribute it and/or modify
     7.7 - * it under the terms of the GNU Lesser General Public License as
     7.8 - * published by the Free Software Foundation; either version 2.1 of the
     7.9 - * License, or  (at your option) any later version. This library is 
    7.10 - * distributed in the  hope that it will be useful, but WITHOUT ANY
    7.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or
    7.12 - * FITNESS FOR A PARTICULAR PURPOSE.
    7.13 - * See the GNU Lesser General Public License for more details.
    7.14 - *
    7.15 - * You should have received a copy of the GNU Lesser General Public License
    7.16 - * along with this library; if not, write to the Free Software Foundation,
    7.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
    7.18 - */
    7.19 -
    7.20 -#ifndef _SP_LEXIS_H_
    7.21 -#define _SP_LEXIS_H_
    7.22 -
    7.23 -#include "sys_string.h"
    7.24 -#include "sys_ctype.h"
    7.25 -
    7.26 -/** @file
    7.27 - * Lexical analysis.
    7.28 - */
    7.29 -
    7.30 -/** Class of characters treated as space. */
    7.31 -#define space_class ((char []){ '\n', '\r', '\t', ' ', '\f' , 0 })
    7.32 -
    7.33 -/** Class of separator characters. */
    7.34 -#define sep_class "{}()<>[]@!;"
    7.35 -
    7.36 -#define comment_class "#"
    7.37 -
    7.38 -/** Determine if a character is in a given class.
    7.39 - * 
    7.40 - * @param c character to test
    7.41 - * @param s null-terminated string of characters in the class
    7.42 - * @return 1 if c is in the class, 0 otherwise.
    7.43 - */
    7.44 -static inline int in_class(int c, const char *s){
    7.45 -  return s && (strchr(s, c) != 0);
    7.46 -}
    7.47 -
    7.48 -/** Determine if a character is in the space class.
    7.49 - * 
    7.50 - * @param c character to test
    7.51 - * @return 1 if c is in the class, 0 otherwise.
    7.52 - */
    7.53 -static inline int in_space_class(int c){
    7.54 -    return in_class(c, space_class);
    7.55 -}
    7.56 -
    7.57 -static inline int in_comment_class(int c){
    7.58 -    return in_class(c, comment_class);
    7.59 -}
    7.60 -
    7.61 -/** Determine if a character is in the separator class.
    7.62 - * Separator characters terminate tokens, and do not need space
    7.63 - * to separate them.
    7.64 - * 
    7.65 - * @param c character to test
    7.66 - * @return 1 if c is in the class, 0 otherwise.
    7.67 - */
    7.68 -static inline int in_sep_class(int c){
    7.69 -    return in_class(c, sep_class);
    7.70 -}
    7.71 -
    7.72 -/** Determine if a character is in the alpha class.
    7.73 - * 
    7.74 - * @param c character to test
    7.75 - * @return 1 if c is in the class, 0 otherwise.
    7.76 - */
    7.77 -static inline int in_alpha_class(int c){
    7.78 -    return isalpha(c);
    7.79 -}
    7.80 -
    7.81 -/** Determine if a character is in the octal digit class.
    7.82 - * 
    7.83 - * @param c character to test
    7.84 - * @return 1 if c is in the class, 0 otherwise.
    7.85 - */
    7.86 -static inline int in_octal_digit_class(int c){
    7.87 -    return '0' <= c && c <= '7';
    7.88 -}
    7.89 -
    7.90 -/** Determine if a character is in the decimal digit class.
    7.91 - * 
    7.92 - * @param c character to test
    7.93 - * @return 1 if c is in the class, 0 otherwise.
    7.94 - */
    7.95 -static inline int in_decimal_digit_class(int c){
    7.96 -    return isdigit(c);
    7.97 -}
    7.98 -
    7.99 -/** Determine if a character is in the hex digit class.
   7.100 - * 
   7.101 - * @param c character to test
   7.102 - * @return 1 if c is in the class, 0 otherwise.
   7.103 - */
   7.104 -static inline int in_hex_digit_class(int c){
   7.105 -    return isdigit(c) || in_class(c, "abcdefABCDEF");
   7.106 -}
   7.107 -
   7.108 -
   7.109 -static inline int in_string_quote_class(int c){
   7.110 -    return in_class(c, "'\"");
   7.111 -}
   7.112 -
   7.113 -static inline int in_printable_class(int c){
   7.114 -    return ('A' <= c && c <= 'Z')
   7.115 -        || ('a' <= c && c <= 'z')
   7.116 -        || ('0' <= c && c <= '9')
   7.117 -        || in_class(c, "!$%&*+,-./:;<=>?@^_`{|}~");
   7.118 -}
   7.119 -
   7.120 -extern int is_decimal_number(const char *s, int n);
   7.121 -extern int is_hex_number(const char *s, int n);
   7.122 -extern int is_keyword(const char *s, const char *k);
   7.123 -extern int is_keychar(const char *s, char c);
   7.124 -
   7.125 -#endif /* !_SP_LEXIS_H_ */
     8.1 --- a/tools/libxutil/lzi_stream.c	Wed Jun 30 11:07:39 2004 +0000
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,590 +0,0 @@
     8.4 -/* $Id: lzi_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $ */
     8.5 -#define __FILE_ID_INFO "$Id: lzi_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $"
     8.6 -#include <what.h>
     8.7 -static char __rcsid[] __attribute__((unused)) = WHAT_ID __FILE_ID_INFO;
     8.8 -/*
     8.9 - * Copyright (C) 2003 Hewlett-Packard Company.
    8.10 - *
    8.11 - * This library is free software; you can redistribute it and/or modify
    8.12 - * it under the terms of the GNU Lesser General Public License as published by
    8.13 - * the Free Software Foundation; either version 2.1 of the License, or
    8.14 - * (at your option) any later version.
    8.15 - *
    8.16 - * This library is distributed in the hope that it will be useful,
    8.17 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.18 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    8.19 - * GNU Lesser General Public License for more details.
    8.20 - *
    8.21 - * You should have received a copy of the GNU Lesser General Public License
    8.22 - * along with this library; if not, write to the Free Software
    8.23 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    8.24 - */
    8.25 -
    8.26 -/** @file
    8.27 - * An IOStream implementation using LZI to provide compression and decompression.
    8.28 - * This is designed to provide compression without output latency.
    8.29 - * Flushing an LZI stream flushes all pending data to the underlying stream.
    8.30 - * This is essential for stream-based (e.g. networked) applications.
    8.31 - *
    8.32 - * A compressed data stream is a sequence of blocks.
    8.33 - * Each block is the block size followed by the compressed data.
    8.34 - * The last block has size zero.
    8.35 - * Sizes are 4-byte unsigned in network order.
    8.36 - *
    8.37 - * This format allows compressed data to be read from a stream without reading
    8.38 - * past the logical end of compressed data.
    8.39 - *
    8.40 - * @author Mike Wray <mike.wray@hpl.hp.com>
    8.41 - */
    8.42 -#ifndef __KERNEL__
    8.43 -
    8.44 -#include <stdio.h>
    8.45 -#include <stdlib.h>
    8.46 -#include <errno.h>
    8.47 -#include <string.h>
    8.48 -
    8.49 -#include "zlib.h"
    8.50 -
    8.51 -#include "allocate.h"
    8.52 -#include "lzi_stream.h"
    8.53 -#include "file_stream.h"
    8.54 -#include "marshal.h"
    8.55 -
    8.56 -#define dprintf(fmt, args...) fprintf(stdout, "[DEBUG] LZI>%s" fmt, __FUNCTION__, ##args)
    8.57 -#define wprintf(fmt, args...) fprintf(stderr, "[WARN]  LZI>%s" fmt, __FUNCTION__, ##args)
    8.58 -#define iprintf(fmt, args...) fprintf(stdout, "[INFO]  LZI>%s" fmt, __FUNCTION__, ##args)
    8.59 -#define eprintf(fmt, args...) fprintf(stderr, "[ERROR] LZI>%s" fmt, __FUNCTION__, ##args)
    8.60 -
    8.61 -static int lzi_read(IOStream *s, void *buf, size_t size, size_t count);
    8.62 -static int lzi_write(IOStream *s, const void *buf, size_t size, size_t count);
    8.63 -static int lzi_print(IOStream *s, const char *msg, va_list args);
    8.64 -static int lzi_getc(IOStream *s);
    8.65 -static int lzi_error(IOStream *s);
    8.66 -static int lzi_close(IOStream *s);
    8.67 -static void lzi_free(IOStream *s);
    8.68 -static int lzi_flush(IOStream *s);
    8.69 -
    8.70 -enum {
    8.71 -    LZI_WRITE = 1,
    8.72 -    LZI_READ = 2,
    8.73 -};
    8.74 -
    8.75 -/** Methods used by a gzFile* IOStream. */
    8.76 -static const IOMethods lzi_methods = {
    8.77 -    read: lzi_read,
    8.78 -    write: lzi_write,
    8.79 -    print: lzi_print,
    8.80 -    getc:  lzi_getc,
    8.81 -    error: lzi_error,
    8.82 -    close: lzi_close,
    8.83 -    free:  lzi_free,
    8.84 -    flush: lzi_flush,
    8.85 -};
    8.86 -
    8.87 -#define BUFFER_SIZE (512 * 1024)
    8.88 -
    8.89 -typedef struct LZIState {
    8.90 -    z_stream zstream;
    8.91 -    void *inbuf;
    8.92 -    uint32_t inbuf_size;
    8.93 -    void *outbuf;
    8.94 -    uint32_t outbuf_size;
    8.95 -    /** Underlying stream for I/O. */
    8.96 -    IOStream *io;
    8.97 -    /** Flags. */
    8.98 -    int flags;
    8.99 -    /** Error indicator. */
   8.100 -    int error;
   8.101 -    int eof;
   8.102 -    int plain_bytes;
   8.103 -    int comp_bytes;
   8.104 -    int zstream_initialized;
   8.105 -    int flushed;
   8.106 -} LZIState;
   8.107 -
   8.108 -static inline int LZIState_writeable(LZIState *s){
   8.109 -    return (s->flags & LZI_WRITE) != 0;
   8.110 -}
   8.111 -
   8.112 -static inline int LZIState_readable(LZIState *s){
   8.113 -    return (s->flags & LZI_READ) != 0;
   8.114 -}
   8.115 -
   8.116 -void LZIState_free(LZIState *z){
   8.117 -    if(!z) return;
   8.118 -    if(z->zstream_initialized){
   8.119 -        if(LZIState_writeable(z)){
   8.120 -            deflateEnd(&z->zstream);
   8.121 -        } else if(LZIState_readable(z)){
   8.122 -            inflateEnd(&z->zstream);
   8.123 -        }
   8.124 -    }
   8.125 -    deallocate(z->inbuf);
   8.126 -    deallocate(z->outbuf);
   8.127 -    deallocate(z);
   8.128 -}
   8.129 -
   8.130 -static int mode_flags(const char *mode, int *flags){
   8.131 -    int err = 0;
   8.132 -    int r=0, w=0;
   8.133 -    if(!mode){
   8.134 -        err = -EINVAL;
   8.135 -        goto exit;
   8.136 -    }
   8.137 -    for(; *mode; mode++){
   8.138 -        if(*mode == 'w') w = 1;
   8.139 -        if(*mode == 'r') r = 1;
   8.140 -    }
   8.141 -    if(r + w != 1){
   8.142 -        err = -EINVAL;
   8.143 -        goto exit;
   8.144 -    }
   8.145 -    if(r) *flags |= LZI_READ;
   8.146 -    if(w) *flags |= LZI_WRITE;
   8.147 -  exit:
   8.148 -    return err;
   8.149 -}
   8.150 -
   8.151 -/** Get the stream state.
   8.152 - * 
   8.153 - * @param s lzi stream
   8.154 - * @return stream state.
   8.155 - */
   8.156 -static inline LZIState * lzi_state(IOStream *io){
   8.157 -    return io->data;
   8.158 -}
   8.159 -
   8.160 -IOStream *lzi_stream_io(IOStream *io){
   8.161 -    LZIState *s = lzi_state(io);
   8.162 -    return s->io;
   8.163 -}
   8.164 -
   8.165 -static inline void set_error(LZIState *s, int err){
   8.166 -    if(err < 0 && !s->error){
   8.167 -        s->error = err;
   8.168 -    }
   8.169 -}
   8.170 -
   8.171 -static int zerror(LZIState *s, int err){
   8.172 -    if(err){
   8.173 -        //dprintf("> err=%d\n", err);
   8.174 -        if(err < 0) set_error(s, -EIO);
   8.175 -    }
   8.176 -    return s->error;
   8.177 -}
   8.178 -
   8.179 -int lzi_stream_plain_bytes(IOStream *io){
   8.180 -    LZIState *s = lzi_state(io);
   8.181 -    return s->plain_bytes;
   8.182 -}
   8.183 -
   8.184 -int lzi_stream_comp_bytes(IOStream *io){
   8.185 -    LZIState *s = lzi_state(io);
   8.186 -    return s->comp_bytes;
   8.187 -}
   8.188 -
   8.189 -float lzi_stream_ratio(IOStream *io){
   8.190 -    LZIState *s = lzi_state(io);
   8.191 -    float ratio = 0.0;
   8.192 -    if(s->comp_bytes){
   8.193 -        ratio = ((float) s->comp_bytes)/((float) s->plain_bytes);
   8.194 -    }
   8.195 -    return ratio;
   8.196 -}
   8.197 -
   8.198 -static int alloc(void **p, int n){
   8.199 -    *p = allocate(n);
   8.200 -    return (p ? 0 : -ENOMEM);
   8.201 -}
   8.202 -
   8.203 -LZIState * LZIState_new(IOStream *io, int flags){
   8.204 -    int err = -ENOMEM;
   8.205 -    int zlevel = Z_BEST_SPEED; // Level 1 compression - fastest.
   8.206 -    int zstrategy = Z_DEFAULT_STRATEGY;
   8.207 -    int zwindow = MAX_WBITS;
   8.208 -    int zmemory = 8;
   8.209 -    LZIState *z = ALLOCATE(LZIState);
   8.210 -
   8.211 -    //dprintf(">\n");
   8.212 -    if(!z) goto exit;
   8.213 -    z->io = io;
   8.214 -    z->flags = flags;
   8.215 -
   8.216 -    if(LZIState_writeable(z)){
   8.217 -        z->outbuf_size = BUFFER_SIZE;
   8.218 -        /* windowBits is passed < 0 to suppress zlib header */
   8.219 -        err = deflateInit2(&z->zstream, zlevel, Z_DEFLATED, -zwindow, zmemory, zstrategy);
   8.220 -        if (err != Z_OK) goto exit;
   8.221 -        z->zstream_initialized = 1;
   8.222 -        err = alloc(&z->outbuf, z->outbuf_size);
   8.223 -        if(err) goto exit;
   8.224 -        z->zstream.next_out = z->outbuf;
   8.225 -        z->zstream.avail_out = z->outbuf_size;
   8.226 -    } else {
   8.227 -        z->inbuf_size = BUFFER_SIZE;
   8.228 -        err = alloc(&z->inbuf, z->inbuf_size);
   8.229 -        if(err) goto exit;
   8.230 -        ///z->zstream.next_in  = z->inbuf;
   8.231 -
   8.232 -        /* windowBits is passed < 0 to tell that there is no zlib header.
   8.233 -         * Note that in this case inflate *requires* an extra "dummy" byte
   8.234 -         * after the compressed stream in order to complete decompression and
   8.235 -         * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
   8.236 -         * present after the compressed stream.
   8.237 -         */
   8.238 -        err = inflateInit2(&z->zstream, -zwindow);
   8.239 -        if(err != Z_OK) goto exit;
   8.240 -        z->zstream_initialized = 1;
   8.241 -    }
   8.242 -        
   8.243 -  exit:
   8.244 -    if(err){
   8.245 -        LZIState_free(z);
   8.246 -        z = NULL;
   8.247 -    }
   8.248 -    //dprintf("< z=%p\n", z);
   8.249 -    return z;
   8.250 -}
   8.251 -
   8.252 -int read_block(LZIState *s){
   8.253 -    int err = 0, k = 0;
   8.254 -    //dprintf(">\n");
   8.255 -    if(s->eof) goto exit;
   8.256 -    err = unmarshal_uint32(s->io, &k);
   8.257 -    if(err) goto exit;
   8.258 -    if(k > s->inbuf_size){
   8.259 -        err = -EINVAL;
   8.260 -        goto exit;
   8.261 -    }
   8.262 -    if(k){
   8.263 -        err = unmarshal_bytes(s->io, s->inbuf, k);
   8.264 -        if(err) goto exit;
   8.265 -    } else {
   8.266 -        s->eof = 1;
   8.267 -    }        
   8.268 -    s->zstream.avail_in = k;
   8.269 -    s->zstream.next_in = s->inbuf;
   8.270 -    s->comp_bytes += 4;
   8.271 -    s->comp_bytes += k;
   8.272 -  exit:
   8.273 -    //dprintf("< err=%d\n", err);
   8.274 -    return err;
   8.275 -}
   8.276 -
   8.277 -int write_block(LZIState *s){
   8.278 -    int err = 0;
   8.279 -    int k = ((char*)s->zstream.next_out) - ((char*)s->outbuf);
   8.280 -    int k2 = s->outbuf_size - s->zstream.avail_out;
   8.281 -    //dprintf("> k=%d k2=%d\n", k, k2);
   8.282 -    if(!k) goto exit;
   8.283 -    err = marshal_uint32(s->io, k);
   8.284 -    if(err) goto exit;
   8.285 -    err = marshal_bytes(s->io, s->outbuf, k);
   8.286 -    if(err) goto exit;
   8.287 -    s->zstream.next_out = s->outbuf;
   8.288 -    s->zstream.avail_out = s->outbuf_size;
   8.289 -    s->comp_bytes += 4;
   8.290 -    s->comp_bytes += k;
   8.291 -  exit:
   8.292 -    //dprintf("< err=%d\n", err);
   8.293 -    return err;
   8.294 -}
   8.295 -
   8.296 -int write_terminator(LZIState *s){
   8.297 -    int err = 0;
   8.298 -    char c = 0;
   8.299 -    err = marshal_uint32(s->io, 1);
   8.300 -    if(err) goto exit;
   8.301 -    err = marshal_bytes(s->io, &c, 1);
   8.302 -    if(err) goto exit;
   8.303 -    err = marshal_uint32(s->io, 0);
   8.304 -    if(err) goto exit;
   8.305 -    s->comp_bytes += 9;
   8.306 -  exit:
   8.307 -    return err;
   8.308 -}
   8.309 -
   8.310 -/** Write to the underlying stream using fwrite();
   8.311 - *
   8.312 - * @param io destination
   8.313 - * @param buf data
   8.314 - * @param size size of data elements
   8.315 - * @param count number of data elements to write
   8.316 - * @return number of data elements written
   8.317 - */
   8.318 -static int lzi_write(IOStream *io, const void *buf, size_t size, size_t count){
   8.319 -    int err = 0;
   8.320 -    int n = size * count;
   8.321 -    LZIState *s = lzi_state(io);
   8.322 -
   8.323 -    //dprintf("> buf=%p size=%d count=%d n=%d\n", buf, size, count, n);
   8.324 -    if(!LZIState_writeable(s)){
   8.325 -        err = -EINVAL;
   8.326 -        goto exit;
   8.327 -    }
   8.328 -    s->flushed = 0;
   8.329 -    s->zstream.next_in = (void*)buf;
   8.330 -    s->zstream.avail_in = n;
   8.331 -    while(s->zstream.avail_in){
   8.332 -        if(s->zstream.avail_out == 0){
   8.333 -            err = write_block(s);
   8.334 -            if(err) goto exit;
   8.335 -        }
   8.336 -        //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out);
   8.337 -        //dprintf("> 1 deflate next_in=%p next_out=%p\n", s->zstream.next_in, s->zstream.next_out);
   8.338 -        err = zerror(s, deflate(&s->zstream, Z_NO_FLUSH));
   8.339 -        //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out);
   8.340 -        //dprintf("> 2 deflate next_in=%p next_out=%p\n", s->zstream.next_in, s->zstream.next_out);
   8.341 -        if(err) goto exit;
   8.342 -    }
   8.343 -    err = n;
   8.344 -    s->plain_bytes += n;
   8.345 -    if(size != 1) err /= size;
   8.346 -  exit:
   8.347 -    //dprintf("< err=%d\n", err);
   8.348 -    return err;
   8.349 -}
   8.350 -
   8.351 -
   8.352 -/** Read from the underlying stream.
   8.353 - *
   8.354 - * @param io input
   8.355 - * @param buf where to put input
   8.356 - * @param size size of data elements
   8.357 - * @param count number of data elements to read
   8.358 - * @return number of data elements read
   8.359 - */
   8.360 -static int lzi_read(IOStream *io, void *buf, size_t size, size_t count){
   8.361 -    int err, zerr;
   8.362 -    int n = size * count;
   8.363 -    LZIState *s = lzi_state(io);
   8.364 -
   8.365 -    //dprintf("> size=%d count=%d n=%d\n", size, count, n);
   8.366 -    if(!LZIState_readable(s)){
   8.367 -        err = -EINVAL;
   8.368 -        goto exit;
   8.369 -    }
   8.370 -    s->zstream.next_out = buf;
   8.371 -    s->zstream.avail_out = n;
   8.372 -    while(s->zstream.avail_out){
   8.373 -        if(s->zstream.avail_in == 0){
   8.374 -            err = read_block(s);
   8.375 -        }
   8.376 -        //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out);
   8.377 -        zerr = inflate(&s->zstream, Z_NO_FLUSH);
   8.378 -        //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out);
   8.379 -        if(zerr == Z_STREAM_END) break;
   8.380 -        //dprintf("> zerr=%d\n", zerr);
   8.381 -        err = zerror(s, zerr);
   8.382 -        if(err) goto exit;
   8.383 -    }
   8.384 -    err = n - s->zstream.avail_out;
   8.385 -    s->plain_bytes += err;
   8.386 -    if(size != 1) err /= size;
   8.387 -  exit:
   8.388 -    set_error(s, err);
   8.389 -    //dprintf("< err=%d\n", err);
   8.390 -    return err;
   8.391 -}
   8.392 -
   8.393 -/** Print to the underlying stream.
   8.394 - * Returns 0 if the formatted output is too big for the internal buffer.
   8.395 - *
   8.396 - * @param io lzi stream
   8.397 - * @param msg format to use
   8.398 - * @param args arguments
   8.399 - * @return result of the print
   8.400 - */
   8.401 -static int lzi_print(IOStream *io, const char *msg, va_list args){
   8.402 -    char buf[1024];
   8.403 -    int buf_n = sizeof(buf);
   8.404 -    int n;
   8.405 -    LZIState *s = lzi_state(io);
   8.406 -    if(!LZIState_writeable(s)){
   8.407 -        n = -EINVAL;
   8.408 -        goto exit;
   8.409 -    }
   8.410 -    n = vsnprintf(buf, buf_n, (char*)msg, args);
   8.411 -    if(n < 0) goto exit;
   8.412 -    if(n > buf_n){
   8.413 -        n = 0;
   8.414 -    } else {
   8.415 -        n = lzi_write(io, buf, 1, n);
   8.416 -    }
   8.417 -  exit:
   8.418 -    return n;
   8.419 -}
   8.420 -
   8.421 -/** Read a character from the underlying stream
   8.422 - *
   8.423 - * @param io lzi stream
   8.424 - * @return character read, IOSTREAM_EOF on end of file (or error)
   8.425 - */
   8.426 -static int lzi_getc(IOStream *io){
   8.427 -    int err;
   8.428 -    char c;
   8.429 -    err = lzi_read(io, &c, 1, 1);
   8.430 -    if(err < 1) c = EOF;
   8.431 -    err = (c==EOF ? IOSTREAM_EOF : c);
   8.432 -    return err;
   8.433 -}
   8.434 -
   8.435 -static int flush_output(LZIState *s, int mode){
   8.436 -    int err = 0, zerr;
   8.437 -    int done = 0;
   8.438 -    int avail_out_old;
   8.439 -    int count = 10;
   8.440 -
   8.441 -    //dprintf("> avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out);
   8.442 -    if(s->flushed == 1 + mode) goto exit;
   8.443 -    //s->zstream.avail_in = 0; /* should be zero already anyway */
   8.444 -    for(;;){
   8.445 -        // Write any available output.
   8.446 -        if(done || s->zstream.avail_out == 0){
   8.447 -            err = write_block(s);
   8.448 -            if(err) goto exit;
   8.449 -            if(done) break;
   8.450 -        }
   8.451 -        //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out);
   8.452 -        avail_out_old = s->zstream.avail_out;
   8.453 -        zerr = deflate(&s->zstream, mode);
   8.454 -        err = zerror(s, zerr);
   8.455 -        //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out);
   8.456 -        //dprintf("> deflate=%d\n", err);
   8.457 -        //done = (s->zstream.avail_out != 0);
   8.458 -        //done = (s->zstream.avail_in == 0) && (s->zstream.avail_out == avail_out_old);
   8.459 -        if(0 && mode == Z_FINISH){
   8.460 -            done = (zerr ==  Z_STREAM_END);
   8.461 -        } else {
   8.462 -            done = (s->zstream.avail_in == 0)
   8.463 -                //&& (s->zstream.avail_out == avail_out_old)
   8.464 -                && (s->zstream.avail_out != 0);
   8.465 -        }
   8.466 -    }
   8.467 -    s->flushed = 1 + mode;
   8.468 -  exit:
   8.469 -    //dprintf("< err=%d\n", err);
   8.470 -    return err;
   8.471 -}
   8.472 -
   8.473 -/** Flush any pending input to the underlying stream.
   8.474 - *
   8.475 - * @param s lzi stream
   8.476 - * @return 0 on success, error code otherwise
   8.477 - */
   8.478 -static int lzi_flush(IOStream *io){
   8.479 -    int err = 0;
   8.480 -    LZIState *s = lzi_state(io);
   8.481 -    //dprintf(">\n");
   8.482 -    if(!LZIState_writeable(s)){
   8.483 -        err = -EINVAL;
   8.484 -        goto exit;
   8.485 -    }
   8.486 -    err = flush_output(s, Z_SYNC_FLUSH);
   8.487 -    if(err) goto exit;
   8.488 -    err = IOStream_flush(s->io);
   8.489 -  exit:
   8.490 -    set_error(s, err);
   8.491 -    //dprintf("< err=%d\n", err);
   8.492 -    return (err < 0 ? err : 0);
   8.493 -}
   8.494 -
   8.495 -/** Check if a stream has an error.
   8.496 - *
   8.497 - * @param s lzi stream
   8.498 - * @return code if has an error, 0 otherwise
   8.499 - */
   8.500 -static int lzi_error(IOStream *s){
   8.501 -    int err = 0;
   8.502 -    LZIState *state = lzi_state(s);
   8.503 -    err = state->error;
   8.504 -    if(err) goto exit;
   8.505 -    err = IOStream_error(state->io);
   8.506 -  exit:
   8.507 -    return err;
   8.508 -}
   8.509 -
   8.510 -/** Close an lzi stream.
   8.511 - *
   8.512 - * @param s lzi stream to close
   8.513 - * @return result of the close
   8.514 - */
   8.515 -static int lzi_close(IOStream *io){
   8.516 -    int err = 0;
   8.517 -    LZIState *s = lzi_state(io);
   8.518 -    if(LZIState_writeable(s)){
   8.519 -        err = flush_output(s, Z_FINISH);
   8.520 -        if(err) goto exit;
   8.521 -        err = write_terminator(s);
   8.522 -        if(err) goto exit;
   8.523 -        err = IOStream_flush(s->io);
   8.524 -    }   
   8.525 -  exit:
   8.526 -    err = IOStream_close(s->io);
   8.527 -    set_error(s, err);
   8.528 -    return err;
   8.529 -}
   8.530 -
   8.531 -/** Free an lzi stream.
   8.532 - *
   8.533 - * @param s lzi stream
   8.534 - */
   8.535 -static void lzi_free(IOStream *s){
   8.536 -    LZIState *state = lzi_state(s);
   8.537 -    IOStream_free(state->io);
   8.538 -    LZIState_free(state);
   8.539 -    s->data = NULL;
   8.540 -}
   8.541 -
   8.542 -/** Create an lzi stream for an IOStream.
   8.543 - *
   8.544 - * @param io stream to wrap
   8.545 - * @return new IOStream using f for i/o
   8.546 - */
   8.547 -IOStream *lzi_stream_new(IOStream *io, const char *mode){
   8.548 -    int err = -ENOMEM;
   8.549 -    int flags = 0;
   8.550 -    IOStream *zio = NULL;
   8.551 -    LZIState *state = NULL;
   8.552 -
   8.553 -    zio = ALLOCATE(IOStream);
   8.554 -    if(!zio) goto exit;
   8.555 -    err = mode_flags(mode, &flags);
   8.556 -    if(err) goto exit;
   8.557 -    state = LZIState_new(io, flags);
   8.558 -    if(!state) goto exit;
   8.559 -    err = 0;
   8.560 -    zio->data = state;
   8.561 -    zio->methods = &lzi_methods;
   8.562 -  exit:
   8.563 -    if(err){
   8.564 -        if(state) LZIState_free(state);
   8.565 -        if(zio) deallocate(zio);
   8.566 -        zio = NULL;
   8.567 -    }
   8.568 -    return zio;
   8.569 -}
   8.570 -
   8.571 -/** IOStream version of fdopen().
   8.572 - *
   8.573 - * @param fd file descriptor
   8.574 - * @param flags giving the mode to open in (as for fdopen())
   8.575 - * @return new stream for the open file, or NULL if failed
   8.576 - */
   8.577 -IOStream *lzi_stream_fdopen(int fd, const char *mode){
   8.578 -    int err = -ENOMEM;
   8.579 -    IOStream *io = NULL, *zio = NULL;
   8.580 -    io = file_stream_fdopen(fd, mode);
   8.581 -    if(!io) goto exit;
   8.582 -    zio = lzi_stream_new(io, mode);
   8.583 -    if(!io) goto exit;
   8.584 -    err = 0;
   8.585 -  exit:
   8.586 -    if(err){
   8.587 -        IOStream_free(io);
   8.588 -        IOStream_free(zio);
   8.589 -        zio = NULL;
   8.590 -    }
   8.591 -    return zio;
   8.592 -}
   8.593 -#endif
     9.1 --- a/tools/libxutil/lzi_stream.h	Wed Jun 30 11:07:39 2004 +0000
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,36 +0,0 @@
     9.4 -#/* $Id: lzi_stream.h,v 1.3 2003/09/30 15:22:53 mjw Exp $ */
     9.5 -/*
     9.6 - * Copyright (C) 2003 Hewlett-Packard Company.
     9.7 - *
     9.8 - * This library is free software; you can redistribute it and/or modify
     9.9 - * it under the terms of the GNU Lesser General Public License as published by
    9.10 - * the Free Software Foundation; either version 2.1 of the License, or
    9.11 - * (at your option) any later version.
    9.12 - *
    9.13 - * This library is distributed in the hope that it will be useful,
    9.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    9.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    9.16 - * GNU Lesser General Public License for more details.
    9.17 - *
    9.18 - * You should have received a copy of the GNU Lesser General Public License
    9.19 - * along with this library; if not, write to the Free Software
    9.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    9.21 - */
    9.22 -
    9.23 -#ifndef _SP_LZI_STREAM_H_
    9.24 -#define _SP_LZI_STREAM_H_
    9.25 -
    9.26 -#ifndef __KERNEL__
    9.27 -#include "iostream.h"
    9.28 -
    9.29 -extern IOStream *lzi_stream_new(IOStream *io, const char *mode);
    9.30 -extern IOStream *lzi_stream_fopen(const char *file, const char *mode);
    9.31 -extern IOStream *lzi_stream_fdopen(int fd, const char *mode);
    9.32 -extern IOStream *lzi_stream_io(IOStream *zio);
    9.33 -
    9.34 -extern int lzi_stream_plain_bytes(IOStream *io);
    9.35 -extern int lzi_stream_comp_bytes(IOStream *io);
    9.36 -extern float lzi_stream_ratio(IOStream *io);
    9.37 -
    9.38 -#endif
    9.39 -#endif /* !_SP_FILE_STREAM_H_ */
    10.1 --- a/tools/libxutil/lzo_stream.c	Wed Jun 30 11:07:39 2004 +0000
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,596 +0,0 @@
    10.4 -/* $Id: lzo_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $ */
    10.5 -#define __FILE_ID_INFO "$Id: lzo_stream.c,v 1.4 2003/09/30 15:22:53 mjw Exp $"
    10.6 -#include <what.h>
    10.7 -static char __rcsid[] __attribute__((unused)) = WHAT_ID __FILE_ID_INFO;
    10.8 -/*
    10.9 - * Copyright (C) 2003 Hewlett-Packard Company.
   10.10 - *
   10.11 - * This library is free software; you can redistribute it and/or modify
   10.12 - * it under the terms of the GNU Lesser General Public License as published by
   10.13 - * the Free Software Foundation; either version 2.1 of the License, or
   10.14 - * (at your option) any later version.
   10.15 - *
   10.16 - * This library is distributed in the hope that it will be useful,
   10.17 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10.18 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10.19 - * GNU Lesser General Public License for more details.
   10.20 - *
   10.21 - * You should have received a copy of the GNU Lesser General Public License
   10.22 - * along with this library; if not, write to the Free Software
   10.23 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   10.24 - */
   10.25 -
   10.26 -/** @file
   10.27 - * An IOStream implementation using LZO to provide compression and decompression.
   10.28 - * This is designed to provide reasonable compression without output latency.
   10.29 - * Flushing an LZO stream flushes all pending data to the underlying stream.
   10.30 - * This is essential for stream-based (e.g. networked) applications.
   10.31 - *
   10.32 - * A compressed data stream is a sequence of blocks.
   10.33 - * Each block except the last is the plain data size followed by the compressed data size
   10.34 - * and the compressed data. The last block has plain data size zero and omits the rest.
   10.35 - * Sizes are 4-byte unsigned in network order. If the compressed size is smaller than
   10.36 - * the plain size the block data is compressed, otherwise it is plain (uncompressed).
   10.37 - *
   10.38 - * This format allows compressed data to be read from a stream without reading
   10.39 - * past the logical end of compressed data.
   10.40 - *
   10.41 - * @author Mike Wray <mike.wray@hpl.hp.com>
   10.42 - */
   10.43 -#ifndef __KERNEL__
   10.44 -
   10.45 -#include <stdio.h>
   10.46 -#include <stdlib.h>
   10.47 -#include <errno.h>
   10.48 -#include <string.h>
   10.49 -
   10.50 -#include "lzo1x.h"
   10.51 -
   10.52 -#include "allocate.h"
   10.53 -#include "lzo_stream.h"
   10.54 -#include "file_stream.h"
   10.55 -#include "marshal.h"
   10.56 -
   10.57 -#define dprintf(fmt, args...) fprintf(stdout, "[DEBUG] LZO>%s" fmt, __FUNCTION__, ##args)
   10.58 -#define wprintf(fmt, args...) fprintf(stderr, "[WARN]  LZO>%s" fmt, __FUNCTION__, ##args)
   10.59 -#define iprintf(fmt, args...) fprintf(stdout, "[INFO]  LZO>%s" fmt, __FUNCTION__, ##args)
   10.60 -#define eprintf(fmt, args...) fprintf(stderr, "[ERROR] LZO>%s" fmt, __FUNCTION__, ##args)
   10.61 -
   10.62 -static int lzo_read(IOStream *s, void *buf, size_t size, size_t count);
   10.63 -static int lzo_write(IOStream *s, const void *buf, size_t size, size_t count);
   10.64 -static int lzo_print(IOStream *s, const char *msg, va_list args);
   10.65 -static int lzo_getc(IOStream *s);
   10.66 -static int lzo_error(IOStream *s);
   10.67 -static int lzo_close(IOStream *s);
   10.68 -static void lzo_free(IOStream *s);
   10.69 -static int lzo_flush(IOStream *s);
   10.70 -
   10.71 -enum {
   10.72 -    LZO_WRITE = 1,
   10.73 -    LZO_READ = 2,
   10.74 -};
   10.75 -
   10.76 -/** Methods used by a gzFile* IOStream. */
   10.77 -static const IOMethods lzo_methods = {
   10.78 -    read: lzo_read,
   10.79 -    write: lzo_write,
   10.80 -    print: lzo_print,
   10.81 -    getc:  lzo_getc,
   10.82 -    error: lzo_error,
   10.83 -    close: lzo_close,
   10.84 -    free:  lzo_free,
   10.85 -    flush: lzo_flush,
   10.86 -};
   10.87 -
   10.88 -//#define PLAIN_SIZE (64 * 1024)
   10.89 -//#define PLAIN_SIZE (128 * 1024)
   10.90 -#define PLAIN_SIZE (512 * 1024)
   10.91 -
   10.92 -//#define NOCOMPRESS
   10.93 -
   10.94 -typedef struct LZOState {
   10.95 -    /** Flags. */
   10.96 -    int flags;
   10.97 -    /** Error indicator. */
   10.98 -    int error;
   10.99 -    /** Underlying stream for I/O. */
  10.100 -    IOStream *io;
  10.101 -    /** Working memory (only needed for compression, not decompression). */
  10.102 -    lzo_byte *memory;
  10.103 -    /** Buffer for plain (uncompressed) data. */
  10.104 -    lzo_byte *plain;
  10.105 -    /** Size of the plain buffer. */
  10.106 -    lzo_uint plain_size;
  10.107 -    /** Pointer into the plain buffer. */
  10.108 -    lzo_byte *plain_ptr;
  10.109 -    /** Number of bytes of plain data available. */
  10.110 -    lzo_uint plain_n;
  10.111 -    /** Buffer for compressed data. */
  10.112 -    lzo_byte *comp;
  10.113 -    /** Size of the compressed buffer. */
  10.114 -    lzo_uint comp_size;
  10.115 -
  10.116 -    int plain_bytes;
  10.117 -    int comp_bytes;
  10.118 -} LZOState;
  10.119 -
  10.120 -void LZOState_free(LZOState *z){
  10.121 -    if(!z) return;
  10.122 -    deallocate(z->memory);
  10.123 -    deallocate(z->plain);
  10.124 -    deallocate(z->comp);
  10.125 -    deallocate(z);
  10.126 -}
  10.127 -
  10.128 -/** Maximum size of compressed data for the given plain data size.
  10.129 - *
  10.130 - * @param plain_size size of plain data
  10.131 - * @return maximum size of compressed data
  10.132 - */
  10.133 -static int comp_size(int plain_size){
  10.134 -    return plain_size + (plain_size / 64) + 16 + 3;
  10.135 -}
  10.136 -
  10.137 -static int mode_flags(const char *mode, int *flags){
  10.138 -    int err = 0;
  10.139 -    int r=0, w=0;
  10.140 -    if(!mode){
  10.141 -        err = -EINVAL;
  10.142 -        goto exit;
  10.143 -    }
  10.144 -    for(; *mode; mode++){
  10.145 -        if(*mode == 'w') w = 1;
  10.146 -        if(*mode == 'r') r = 1;
  10.147 -    }
  10.148 -    if(r + w != 1){
  10.149 -        err = -EINVAL;
  10.150 -        goto exit;
  10.151 -    }
  10.152 -    if(r) *flags |= LZO_READ;
  10.153 -    if(w) *flags |= LZO_WRITE;
  10.154 -  exit:
  10.155 -    return err;
  10.156 -}
  10.157 -
  10.158 -/** Get the stream state.
  10.159 - * 
  10.160 - * @param s lzo stream
  10.161 - * @return stream state.
  10.162 - */
  10.163 -static inline LZOState * lzo_state(IOStream *s){
  10.164 -    return s->data;
  10.165 -}
  10.166 -
  10.167 -IOStream *lzo_stream_io(IOStream *s){
  10.168 -    LZOState *state = lzo_state(s);
  10.169 -    return state->io;
  10.170 -}
  10.171 -
  10.172 -static inline void set_error(LZOState *state, int err){
  10.173 -    if(err < 0 && !state->error){
  10.174 -        state->error = err;
  10.175 -    }
  10.176 -}
  10.177 -
  10.178 -int lzo_stream_plain_bytes(IOStream *s){
  10.179 -    LZOState *state = lzo_state(s);
  10.180 -    return state->plain_bytes;
  10.181 -}
  10.182 -
  10.183 -int lzo_stream_comp_bytes(IOStream *s){
  10.184 -    LZOState *state = lzo_state(s);
  10.185 -    return state->comp_bytes;
  10.186 -}
  10.187 -
  10.188 -float lzo_stream_ratio(IOStream *s){
  10.189 -    LZOState *state = lzo_state(s);
  10.190 -    float ratio = 0.0;
  10.191 -    if(state->comp_bytes){
  10.192 -        ratio = ((float) state->comp_bytes)/((float) state->plain_bytes);
  10.193 -    }
  10.194 -    return ratio;
  10.195 -}
  10.196 -
  10.197 -static inline int LZOState_writeable(LZOState *state){
  10.198 -    return (state->flags & LZO_WRITE) != 0;
  10.199 -}
  10.200 -
  10.201 -static inline int LZOState_readable(LZOState *state){
  10.202 -    return (state->flags & LZO_READ) != 0;
  10.203 -}
  10.204 -
  10.205 -LZOState * LZOState_new(IOStream *io, int flags){
  10.206 -    int err = -ENOMEM;
  10.207 -    LZOState *z = ALLOCATE(LZOState);
  10.208 -    //dprintf(">\n");
  10.209 -    if(!z) goto exit;
  10.210 -    z->io = io;
  10.211 -    z->flags = flags;
  10.212 -    if(LZOState_writeable(z)){
  10.213 -        z->memory = allocate(LZO1X_1_MEM_COMPRESS);
  10.214 -        if(!z->memory) goto exit;
  10.215 -    }
  10.216 -    z->plain_size = PLAIN_SIZE;
  10.217 -    z->plain = allocate(z->plain_size);
  10.218 -    if(!z->plain) goto exit;
  10.219 -    z->plain_ptr = z->plain;
  10.220 -    z->comp_size = comp_size(z->plain_size);
  10.221 -    z->comp = allocate(z->comp_size);
  10.222 -    if(!z->comp) goto exit;
  10.223 -    err = 0;
  10.224 -  exit:
  10.225 -    if(err){
  10.226 -        LZOState_free(z);
  10.227 -        z = NULL;
  10.228 -    }
  10.229 -    //dprintf("< z=%p\n", z);
  10.230 -    return z;
  10.231 -}
  10.232 -
  10.233 -static int lzo_compress(LZOState *state){
  10.234 -    int err = 0;
  10.235 -    int k, comp_n;
  10.236 -    //dprintf(">\n");
  10.237 -    //dprintf(">plain=%p plain_n=%d comp=%p memory=%p\n", state->plain, state->plain_n, state->comp, state->memory);
  10.238 -    // Compress the plain buffer.
  10.239 -    err = lzo1x_1_compress(state->plain, state->plain_n,
  10.240 -                           state->comp, &comp_n,
  10.241 -                           state->memory);
  10.242 -    //dprintf("> err=%d plain_n=%d comp_n=%d\n", err, state->plain_n, comp_n);
  10.243 -    // Write plain size, compressed size.
  10.244 -    err = marshal_uint32(state->io, state->plain_n);
  10.245 -    if(err) goto exit;
  10.246 -    err = marshal_uint32(state->io, comp_n);
  10.247 -    if(err) goto exit;
  10.248 -    //dprintf("> write data...\n");
  10.249 -    // Write the smaller of the compressed and plain data.
  10.250 -    if(state->plain_n < comp_n){
  10.251 -        k = state->plain_n;
  10.252 -        err = marshal_bytes(state->io, state->plain, state->plain_n);
  10.253 -    } else {
  10.254 -        k = comp_n;
  10.255 -        err = marshal_bytes(state->io, state->comp, comp_n);
  10.256 -    }
  10.257 -    if(err) goto exit;
  10.258 -    // Total output bytes.
  10.259 -    k+= 8;
  10.260 -    //dprintf("> wrote %d bytes\n", k);
  10.261 -    state->plain_bytes += state->plain_n;
  10.262 -    state->comp_bytes += k;
  10.263 -    //dprintf("> plain=%d, comp=%d, ratio=%3.2f\n",
  10.264 -    //        state->plain_bytes, state->comp_bytes,
  10.265 -    //        ((float)state->comp_bytes)/((float)state->plain_bytes));
  10.266 -    // Reset the plain buffer.
  10.267 -    state->plain_ptr = state->plain;
  10.268 -    state->plain_n = 0;
  10.269 -    err = k;
  10.270 -  exit:
  10.271 -    //dprintf("< err=%d\n", err);
  10.272 -    return err;
  10.273 -}
  10.274 -
  10.275 -static int lzo_decompress(LZOState *state){
  10.276 -    int plain_n, comp_n;
  10.277 -    int err, k;
  10.278 -    //dprintf(">\n");
  10.279 -    err = unmarshal_uint32(state->io, &plain_n);
  10.280 -    //dprintf("> err=%d plain_n=%d\n", err, plain_n);
  10.281 -    if(err) goto exit;
  10.282 -    state->comp_bytes += 4;
  10.283 -    if(plain_n == 0) goto exit;
  10.284 -    err = unmarshal_uint32(state->io, &comp_n);
  10.285 -    //dprintf("> err=%d comp_n=%d\n", err, comp_n);
  10.286 -    if(err) goto exit;
  10.287 -    state->comp_bytes += 4;
  10.288 -    if(plain_n > state->plain_size){
  10.289 -        err = -EINVAL;
  10.290 -        goto exit;
  10.291 -    }
  10.292 -    if(comp_n > plain_n){
  10.293 -        //dprintf("> reading plain data %d...\n", plain_n);
  10.294 -        k = plain_n;
  10.295 -        err = unmarshal_bytes(state->io, state->plain, plain_n);
  10.296 -        state->plain_n = plain_n;
  10.297 -    } else {
  10.298 -        //dprintf("> reading comp data %d...\n", comp_n);
  10.299 -        k = comp_n;
  10.300 -        err = unmarshal_bytes(state->io, state->comp, comp_n);
  10.301 -        //dprintf("> decompress comp_n=%d\n", comp_n);
  10.302 -        err = lzo1x_decompress(state->comp, comp_n,
  10.303 -                               state->plain, &state->plain_n,
  10.304 -                               state->memory);
  10.305 -        //dprintf("> err=%d plain=%d state->plain_n=%d\n", err, plain_n, state->plain_n);
  10.306 -        if(err != LZO_E_OK || state->plain_n != plain_n){
  10.307 -            // Bad. Corrupted input.
  10.308 -            err = -EINVAL;
  10.309 -            eprintf("> Corrupted!\n");
  10.310 -            goto exit;
  10.311 -        }
  10.312 -    }
  10.313 -    state->comp_bytes += k;
  10.314 -    state->plain_bytes += state->plain_n;
  10.315 -    state->plain_ptr = state->plain;
  10.316 -    err = k;
  10.317 -  exit:
  10.318 -    //dprintf("< err=%d\n", err);
  10.319 -    return err;
  10.320 -}
  10.321 -
  10.322 -/** Write to the underlying stream using fwrite();
  10.323 - *
  10.324 - * @param stream destination
  10.325 - * @param buf data
  10.326 - * @param size size of data elements
  10.327 - * @param count number of data elements to write
  10.328 - * @return number of data elements written
  10.329 - */
  10.330 -static int lzo_write(IOStream *s, const void *buf, size_t size, size_t count){
  10.331 -    int err = 0;
  10.332 -    int n = size * count; // Total number of bytes to write.
  10.333 -    int chunk;            // Size of chunk to write.
  10.334 -    int remaining;        // Number of bytes remaining to write.
  10.335 -    int space;            // Amount of space left in plain buffer.
  10.336 -    LZOState *state = lzo_state(s);
  10.337 -#ifdef NOCOMPRESS
  10.338 -    //dprintf("> buf=%p size=%d count=%d\n", buf, size, count);
  10.339 -    err = IOStream_write(state->io, buf, size, count);
  10.340 -    //dprintf("< err=%d\n", err);
  10.341 -#else
  10.342 -    //dprintf("> buf=%p size=%d count=%d n=%d\n", buf, size, count, n);
  10.343 -    remaining = n;
  10.344 -    space = state->plain_size - state->plain_n;
  10.345 -    //dprintf("> plain=%p plain_ptr=%p plain_n=%d space=%d\n",
  10.346 -    //        state->plain, state->plain_ptr, state->plain_n, space);
  10.347 -    while(remaining){
  10.348 -        chunk = remaining;
  10.349 -        if(chunk > space) chunk = space;
  10.350 -        //dprintf("> memcpy %p %p %d\n", state->plain_ptr, buf, chunk);
  10.351 -        memcpy(state->plain_ptr, buf, chunk);
  10.352 -        remaining -= chunk;
  10.353 -        space -= chunk;
  10.354 -        state->plain_ptr += chunk;
  10.355 -        state->plain_n += chunk;
  10.356 -        if(space == 0){
  10.357 -            // Input buffer is full. Compress and write it.
  10.358 -            err = lzo_compress(state);
  10.359 -            if(err < 0) goto exit;
  10.360 -            space = state->plain_size - state->plain_n;
  10.361 -        }
  10.362 -    }
  10.363 -    err = (size > 1 ? n / size : n);
  10.364 -  exit:
  10.365 -    set_error(state, err);
  10.366 -#endif
  10.367 -    return err;
  10.368 -}
  10.369 -
  10.370 -
  10.371 -/** Read from the underlying stream.
  10.372 - *
  10.373 - * @param stream input
  10.374 - * @param buf where to put input
  10.375 - * @param size size of data elements
  10.376 - * @param count number of data elements to read
  10.377 - * @return number of data elements read
  10.378 - */
  10.379 -static int lzo_read(IOStream *s, void *buf, size_t size, size_t count){
  10.380 -    int err = 0;
  10.381 -    int k = 0;                     // Number of (plain) bytes read.
  10.382 -    int remaining = size * count;  // Number of bytes remaining to read.
  10.383 -    int chunk;                     // Size of chunk to read.
  10.384 -    LZOState *state = lzo_state(s);
  10.385 -#ifdef NOCOMPRESS
  10.386 -    //dprintf("> buf=%p size=%d count=%d\n", buf, size, count);
  10.387 -    err = IOStream_read(state->io, buf, size, count);
  10.388 -    //dprintf("< err=%d\n", err);
  10.389 -#else
  10.390 -    if(!(state->flags & LZO_READ)){
  10.391 -        err = -EINVAL;
  10.392 -        goto exit;
  10.393 -    }
  10.394 -    while(remaining){
  10.395 -        if(state->plain_n == 0){
  10.396 -            // No more plain input, decompress some more.
  10.397 -            err = lzo_decompress(state);
  10.398 -            if(err < 0) goto exit;
  10.399 -            // Stop reading if there is no more input.
  10.400 -            if(err == 0 || state->plain_n == 0) break;
  10.401 -        }
  10.402 -        chunk = remaining;
  10.403 -        if(chunk > state->plain_n) chunk = state->plain_n;
  10.404 -        memcpy(buf, state->plain_ptr, chunk);
  10.405 -        k += chunk;
  10.406 -        buf += chunk;
  10.407 -        state->plain_ptr += chunk;
  10.408 -        state->plain_n -= chunk;
  10.409 -        remaining -= chunk;
  10.410 -    }
  10.411 -    err = k;
  10.412 -  exit:
  10.413 -    set_error(state, err);
  10.414 -#endif
  10.415 -    return err;
  10.416 -}
  10.417 -
  10.418 -/** Print to the underlying stream.
  10.419 - * Returns 0 if the formatted output is too big for the internal buffer.
  10.420 - *
  10.421 - * @param s lzo stream
  10.422 - * @param msg format to use
  10.423 - * @param args arguments
  10.424 - * @return result of the print
  10.425 - */
  10.426 -static int lzo_print(IOStream *s, const char *msg, va_list args){
  10.427 -    char buf[1024];
  10.428 -    int buf_n = sizeof(buf);
  10.429 -    int n;
  10.430 -    LZOState *state = lzo_state(s);
  10.431 -    if(!LZOState_writeable(state)){
  10.432 -        n = -EINVAL;
  10.433 -        goto exit;
  10.434 -    }
  10.435 -    n = vsnprintf(buf, buf_n, (char*)msg, args);
  10.436 -    if(n < 0) goto exit;
  10.437 -    if(n > buf_n){
  10.438 -        n = 0;
  10.439 -    } else {
  10.440 -        n = lzo_write(s, buf, 1, n);
  10.441 -    }
  10.442 -  exit:
  10.443 -    return n;
  10.444 -}
  10.445 -
  10.446 -/** Read a character from the underlying stream
  10.447 - *
  10.448 - * @param s lzo stream
  10.449 - * @return character read, IOSTREAM_EOF on end of file (or error)
  10.450 - */
  10.451 -static int lzo_getc(IOStream *s){
  10.452 -    int err;
  10.453 -    char c;
  10.454 -    err = lzo_read(s, &c, 1, 1);
  10.455 -    if(err < 1) c = EOF;
  10.456 -    err = (c==EOF ? IOSTREAM_EOF : c);
  10.457 -    return err;
  10.458 -}
  10.459 -
  10.460 -/** Flush any pending input to the underlying stream.
  10.461 - *
  10.462 - * @param s lzo stream
  10.463 - * @return 0 on success, error code otherwise
  10.464 - */
  10.465 -static int lzo_flush(IOStream *s){
  10.466 -    int err = 0;
  10.467 -    LZOState *state = lzo_state(s);
  10.468 -    //dprintf(">\n");
  10.469 -#ifdef NOCOMPRESS
  10.470 -    err = IOStream_flush(state->io);
  10.471 -#else    
  10.472 -    if(!LZOState_writeable(state)){
  10.473 -        err = -EINVAL;
  10.474 -        goto exit;
  10.475 -    }
  10.476 -    if(state->plain_n){
  10.477 -        err = lzo_compress(state);
  10.478 -        if(err < 0) goto exit;
  10.479 -    }
  10.480 -    err = IOStream_flush(state->io);
  10.481 -  exit:
  10.482 -    set_error(state, err);
  10.483 -#endif
  10.484 -    //dprintf("< err=%d\n", err);
  10.485 -    return (err < 0 ? err : 0);
  10.486 -}
  10.487 -
  10.488 -/** Check if a stream has an error.
  10.489 - *
  10.490 - * @param s lzo stream
  10.491 - * @return code if has an error, 0 otherwise
  10.492 - */
  10.493 -static int lzo_error(IOStream *s){
  10.494 -    int err = 0;
  10.495 -    LZOState *state = lzo_state(s);
  10.496 -    err = state->error;
  10.497 -    if(err) goto exit;
  10.498 -    err = IOStream_error(state->io);
  10.499 -  exit:
  10.500 -    return err;
  10.501 -}
  10.502 -
  10.503 -int lzo_stream_finish(IOStream *s){
  10.504 -    int err = 0;
  10.505 -    LZOState *state = lzo_state(s);
  10.506 -    if(!LZOState_writeable(state)){
  10.507 -        err = -EINVAL;
  10.508 -        goto exit;
  10.509 -    }
  10.510 -    err = lzo_flush(s);
  10.511 -    if(err < 0) goto exit;
  10.512 -    err = marshal_int32(state->io, 0);
  10.513 -  exit:
  10.514 -    return err;
  10.515 -}        
  10.516 -
  10.517 -/** Close an lzo stream.
  10.518 - *
  10.519 - * @param s lzo stream to close
  10.520 - * @return result of the close
  10.521 - */
  10.522 -static int lzo_close(IOStream *s){
  10.523 -    int err = 0;
  10.524 -    LZOState *state = lzo_state(s);
  10.525 -#ifdef NOCOMPRESS
  10.526 -    err = IOStream_close(state->io);
  10.527 -#else    
  10.528 -    if(LZOState_writeable(state)){
  10.529 -        err = lzo_stream_finish(s);
  10.530 -    }        
  10.531 -    err = IOStream_close(state->io);
  10.532 -    set_error(state, err);
  10.533 -#endif
  10.534 -    return err;
  10.535 -}
  10.536 -
  10.537 -/** Free an lzo stream.
  10.538 - *
  10.539 - * @param s lzo stream
  10.540 - */
  10.541 -static void lzo_free(IOStream *s){
  10.542 -    LZOState *state = lzo_state(s);
  10.543 -    IOStream_free(state->io);
  10.544 -    LZOState_free(state);
  10.545 -    s->data = NULL;
  10.546 -}
  10.547 -
  10.548 -/** Create an lzo stream for an IOStream.
  10.549 - *
  10.550 - * @param io stream to wrap
  10.551 - * @return new IOStream using f for i/o
  10.552 - */
  10.553 -IOStream *lzo_stream_new(IOStream *io, const char *mode){
  10.554 -    int err = -ENOMEM;
  10.555 -    int flags = 0;
  10.556 -    IOStream *zio = NULL;
  10.557 -    LZOState *state = NULL;
  10.558 -
  10.559 -    zio = ALLOCATE(IOStream);
  10.560 -    if(!zio) goto exit;
  10.561 -    err = mode_flags(mode, &flags);
  10.562 -    if(err) goto exit;
  10.563 -    state = LZOState_new(io, flags);
  10.564 -    if(!state) goto exit;
  10.565 -    err = 0;
  10.566 -    zio->data = state;
  10.567 -    zio->methods = &lzo_methods;
  10.568 -  exit:
  10.569 -    if(err){
  10.570 -        if(state) LZOState_free(state);
  10.571 -        if(zio) deallocate(zio);
  10.572 -        zio = NULL;
  10.573 -    }
  10.574 -    return zio;
  10.575 -}
  10.576 -
  10.577 -/** IOStream version of fdopen().
  10.578 - *
  10.579 - * @param fd file descriptor
  10.580 - * @param flags giving the mode to open in (as for fdopen())
  10.581 - * @return new stream for the open file, or NULL if failed
  10.582 - */
  10.583 -IOStream *lzo_stream_fdopen(int fd, const char *mode){
  10.584 -    int err = -ENOMEM;
  10.585 -    IOStream *io = NULL, *zio = NULL;
  10.586 -    io = file_stream_fdopen(fd, mode);
  10.587 -    if(!io) goto exit;
  10.588 -    zio = lzo_stream_new(io, mode);
  10.589 -    if(!io) goto exit;
  10.590 -    err = 0;
  10.591 -  exit:
  10.592 -    if(err){
  10.593 -        IOStream_free(io);
  10.594 -        IOStream_free(zio);
  10.595 -        zio = NULL;
  10.596 -    }
  10.597 -    return zio;
  10.598 -}
  10.599 -#endif
    11.1 --- a/tools/libxutil/lzo_stream.h	Wed Jun 30 11:07:39 2004 +0000
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,36 +0,0 @@
    11.4 -#/* $Id: lzo_stream.h,v 1.3 2003/09/30 15:22:53 mjw Exp $ */
    11.5 -/*
    11.6 - * Copyright (C) 2003 Hewlett-Packard Company.
    11.7 - *
    11.8 - * This library is free software; you can redistribute it and/or modify
    11.9 - * it under the terms of the GNU Lesser General Public License as published by
   11.10 - * the Free Software Foundation; either version 2.1 of the License, or
   11.11 - * (at your option) any later version.
   11.12 - *
   11.13 - * This library is distributed in the hope that it will be useful,
   11.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11.16 - * GNU Lesser General Public License for more details.
   11.17 - *
   11.18 - * You should have received a copy of the GNU Lesser General Public License
   11.19 - * along with this library; if not, write to the Free Software
   11.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   11.21 - */
   11.22 -
   11.23 -#ifndef _SP_LZO_STREAM_H_
   11.24 -#define _SP_LZO_STREAM_H_
   11.25 -
   11.26 -#ifndef __KERNEL__
   11.27 -#include "iostream.h"
   11.28 -
   11.29 -extern IOStream *lzo_stream_new(IOStream *io, const char *mode);
   11.30 -extern IOStream *lzo_stream_fopen(const char *file, const char *mode);
   11.31 -extern IOStream *lzo_stream_fdopen(int fd, const char *mode);
   11.32 -extern IOStream *lzo_stream_io(IOStream *zio);
   11.33 -
   11.34 -extern int lzo_stream_plain_bytes(IOStream *io);
   11.35 -extern int lzo_stream_comp_bytes(IOStream *io);
   11.36 -extern float lzo_stream_ratio(IOStream *io);
   11.37 -
   11.38 -#endif
   11.39 -#endif /* !_SP_FILE_STREAM_H_ */
    12.1 --- a/tools/libxutil/marshal.c	Wed Jun 30 11:07:39 2004 +0000
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,207 +0,0 @@
    12.4 -#include <errno.h>
    12.5 -#include "sys_net.h"
    12.6 -#include "allocate.h"
    12.7 -#include "marshal.h"
    12.8 -
    12.9 -#define dprintf(fmt, args...) IOStream_print(iostdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args)
   12.10 -#define wprintf(fmt, args...) IOStream_print(iostderr, "[WARN]  %s" fmt, __FUNCTION__, ##args)
   12.11 -#define iprintf(fmt, args...) IOStream_print(iostdout, "[INFO]  %s" fmt, __FUNCTION__, ##args)
   12.12 -#define eprintf(fmt, args...) IOStream_print(iostderr, "[ERROR] %s" fmt, __FUNCTION__, ##args)
   12.13 -
   12.14 -
   12.15 -#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof((ary)[0]))
   12.16 -
   12.17 -/* Messages are coded as msgid followed by message fields.
   12.18 - * Initial message on any channel is hello - so can check version
   12.19 - * compatibility.
   12.20 - *
   12.21 - * char* -> uint16_t:n <n bytes>
   12.22 - * ints/uints go as suitable number of bytes (e.g. uint16_t is 2 bytes).
   12.23 - * optional fields go as '1' <val> or '0' (the 0/1 is 1 byte).
   12.24 - * lists go as ('1' <elt>)* '0'
   12.25 - */
   12.26 -
   12.27 -int marshal_flush(IOStream *io){
   12.28 -    int err  = 0;
   12.29 -    err = IOStream_flush(io);
   12.30 -    return err;
   12.31 -}
   12.32 -
   12.33 -int marshal_bytes(IOStream *io, void *s, uint32_t s_n){
   12.34 -    int err = 0;
   12.35 -    int n;
   12.36 -    n = IOStream_write(io, s, s_n);
   12.37 -    if(n < 0){
   12.38 -        err = n;
   12.39 -    } else if (n < s_n){
   12.40 -        wprintf("> Wanted %d, got %d\n", s_n, n);
   12.41 -        err = -EIO;
   12.42 -    }
   12.43 -    return err;
   12.44 -}
   12.45 -
   12.46 -int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n){
   12.47 -    int err = 0;
   12.48 -    int n;
   12.49 -    //dprintf("> s_n=%d\n", s_n);
   12.50 -    n = IOStream_read(io, s, s_n);
   12.51 -    //dprintf("> n=%d\n", n);
   12.52 -    if(n < 0){
   12.53 -        err = n;
   12.54 -    } else if(n < s_n){
   12.55 -        wprintf("> Wanted %d, got %d\n", s_n, n);
   12.56 -        err = -EIO;
   12.57 -    }
   12.58 -    //dprintf("< err=%d\n", err);
   12.59 -    return err;
   12.60 -}
   12.61 -
   12.62 -int marshal_uint8(IOStream *io, uint8_t x){
   12.63 -    return marshal_bytes(io, &x, sizeof(x));
   12.64 -}
   12.65 -
   12.66 -int unmarshal_uint8(IOStream *io, uint8_t *x){
   12.67 -    return unmarshal_bytes(io, x, sizeof(*x));
   12.68 -}
   12.69 -
   12.70 -int marshal_uint16(IOStream *io, uint16_t x){
   12.71 -    x = htons(x);
   12.72 -    return marshal_bytes(io, &x, sizeof(x));
   12.73 -}
   12.74 -
   12.75 -int unmarshal_uint16(IOStream *io, uint16_t *x){
   12.76 -    int err = 0;
   12.77 -    err = unmarshal_bytes(io, x, sizeof(*x));
   12.78 -    *x = ntohs(*x);
   12.79 -    return err;
   12.80 -}
   12.81 -
   12.82 -int marshal_int32(IOStream *io, int32_t x){
   12.83 -    int err = 0;
   12.84 -    //dprintf("> x=%d\n", x);
   12.85 -    x = htonl(x);
   12.86 -    err = marshal_bytes(io, &x, sizeof(x));
   12.87 -    //dprintf("< err=%d\n", err);
   12.88 -    return err;
   12.89 -}
   12.90 -
   12.91 -int unmarshal_int32(IOStream *io, int32_t *x){
   12.92 -    int err = 0;
   12.93 -    //dprintf(">\n");
   12.94 -    err = unmarshal_bytes(io, x, sizeof(*x));
   12.95 -    *x = ntohl(*x);
   12.96 -    //dprintf("< err=%d x=%d\n", err, *x);
   12.97 -    return err;
   12.98 -}
   12.99 -
  12.100 -int marshal_uint32(IOStream *io, uint32_t x){
  12.101 -    int err = 0;
  12.102 -    //dprintf("> x=%u\n", x);
  12.103 -    x = htonl(x);
  12.104 -    err = marshal_bytes(io, &x, sizeof(x));
  12.105 -    //dprintf("< err=%d\n", err);
  12.106 -    return err;
  12.107 -}
  12.108 -
  12.109 -int unmarshal_uint32(IOStream *io, uint32_t *x){
  12.110 -    int err = 0;
  12.111 -    //dprintf(">\n");
  12.112 -    err = unmarshal_bytes(io, x, sizeof(*x));
  12.113 -    *x = ntohl(*x);
  12.114 -    //dprintf("< err=%d x=%u\n", err, *x);
  12.115 -    return err;
  12.116 -}
  12.117 -
  12.118 -int marshal_uint64(IOStream *io, uint64_t x){
  12.119 -    int err;
  12.120 -    err = marshal_uint32(io, (uint32_t) ((x >> 32) & 0xffffffff));
  12.121 -    if(err) goto exit;
  12.122 -    err = marshal_uint32(io, (uint32_t) ( x        & 0xffffffff));
  12.123 -  exit:
  12.124 -    return err;
  12.125 -}
  12.126 -
  12.127 -int unmarshal_uint64(IOStream *io, uint64_t *x){
  12.128 -    int err = 0;
  12.129 -    uint32_t hi, lo;
  12.130 -    err = unmarshal_uint32(io, &hi);
  12.131 -    if(err) goto exit;
  12.132 -    err = unmarshal_uint32(io, &lo);
  12.133 -    *x = (((uint64_t) hi) << 32) | lo;
  12.134 -  exit:
  12.135 -    return err;
  12.136 -}
  12.137 -
  12.138 -int marshal_net16(IOStream *io, net16_t x){
  12.139 -    return marshal_bytes(io, &x, sizeof(x));
  12.140 -}
  12.141 -
  12.142 -int unmarshal_net16(IOStream *io, net16_t *x){
  12.143 -    int err = 0;
  12.144 -    err = unmarshal_bytes(io, x, sizeof(*x));
  12.145 -    return err;
  12.146 -}
  12.147 -
  12.148 -int marshal_net32(IOStream *io, net32_t x){
  12.149 -    return marshal_bytes(io, &x, sizeof(x));
  12.150 -}
  12.151 -
  12.152 -int unmarshal_net32(IOStream *io, net32_t *x){
  12.153 -    int err = 0;
  12.154 -    err = unmarshal_bytes(io, x, sizeof(*x));
  12.155 -    return err;
  12.156 -}
  12.157 -
  12.158 -int marshal_string(IOStream *io, char *s, uint32_t s_n){
  12.159 -    int err;
  12.160 -    //dprintf("> s=%s\n", s);
  12.161 -    err = marshal_uint32(io, s_n);
  12.162 -    if(err) goto exit;
  12.163 -    err = marshal_bytes(io, s, s_n);
  12.164 -  exit:
  12.165 -    //dprintf("< err=%d\n", err);
  12.166 -    return err;
  12.167 -}
  12.168 -
  12.169 -int unmarshal_string(IOStream *io, char *s, uint32_t s_n){
  12.170 -    int err = 0, val_n = 0;
  12.171 -    //dprintf(">\n");
  12.172 -    err = unmarshal_uint32(io, &val_n);
  12.173 -    if(err) goto exit;
  12.174 -    if(val_n >= s_n){
  12.175 -        err = -EINVAL;
  12.176 -        goto exit;
  12.177 -    }
  12.178 -    err = unmarshal_bytes(io, s, val_n);
  12.179 -    if(err) goto exit;
  12.180 -    s[val_n] = '\0';
  12.181 -  exit:
  12.182 -    //dprintf("< err=%d s=%s\n", err, s);
  12.183 -    return err;
  12.184 -}
  12.185 -
  12.186 -int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n){
  12.187 -    int err = 0, val_n = 0;
  12.188 -    char *val = NULL;
  12.189 -    //dprintf(">\n");
  12.190 -    err = unmarshal_uint32(io, &val_n);
  12.191 -    if(err) goto exit;
  12.192 -    val = allocate(val_n + 1);
  12.193 -    if(!val){
  12.194 -        err = -ENOMEM;
  12.195 -        goto exit;
  12.196 -    }
  12.197 -    err = unmarshal_bytes(io, val, val_n);
  12.198 -    if(err) goto exit;
  12.199 -    val[val_n] = '\0';
  12.200 -  exit:
  12.201 -    if(err){
  12.202 -        if(val) deallocate(val);
  12.203 -        val = NULL;
  12.204 -        val_n = 0;
  12.205 -    }
  12.206 -    *s = val;
  12.207 -    if(s_n) *s_n = val_n;
  12.208 -    //dprintf("< err=%d s=%s\n", err, *s);
  12.209 -    return err;
  12.210 -}
    13.1 --- a/tools/libxutil/marshal.h	Wed Jun 30 11:07:39 2004 +0000
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,43 +0,0 @@
    13.4 -/* $Id: marshal.h,v 1.1 2003/10/17 15:48:43 mjw Exp $ */
    13.5 -#ifndef _SP_MARSHAL_H_
    13.6 -#define _SP_MARSHAL_H_
    13.7 -
    13.8 -#include "iostream.h"
    13.9 -
   13.10 -/** A 16-bit uint in network order, e.g. a port number. */
   13.11 -typedef uint16_t net16_t;
   13.12 -
   13.13 -/** A 32-bit uint in network order, e.g. an IP address. */
   13.14 -typedef uint32_t net32_t;
   13.15 -
   13.16 -extern int marshal_flush(IOStream *io);
   13.17 -
   13.18 -extern int marshal_bytes(IOStream *io, void *s, uint32_t s_n);
   13.19 -extern int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n);
   13.20 -
   13.21 -extern int marshal_uint8(IOStream *io, uint8_t x);
   13.22 -extern int unmarshal_uint8(IOStream *io, uint8_t *x);
   13.23 -
   13.24 -extern int marshal_uint16(IOStream *io, uint16_t x);
   13.25 -extern int unmarshal_uint16(IOStream *io, uint16_t *x);
   13.26 -
   13.27 -extern int marshal_uint32(IOStream *io, uint32_t x);
   13.28 -extern int unmarshal_uint32(IOStream *io, uint32_t *x);
   13.29 -
   13.30 -extern int marshal_int32(IOStream *io, int32_t x);
   13.31 -extern int unmarshal_int32(IOStream *io, int32_t *x);
   13.32 -
   13.33 -extern int marshal_uint64(IOStream *io, uint64_t x);
   13.34 -extern int unmarshal_uint64(IOStream *io, uint64_t *x);
   13.35 -
   13.36 -extern int marshal_net16(IOStream *io, net16_t x);
   13.37 -extern int unmarshal_net16(IOStream *io, net16_t *x);
   13.38 -
   13.39 -extern int marshal_net32(IOStream *io, net32_t x);
   13.40 -extern int unmarshal_net32(IOStream *io, net32_t *x);
   13.41 -
   13.42 -extern int marshal_string(IOStream *io, char *s, uint32_t s_n);
   13.43 -extern int unmarshal_string(IOStream *io, char *s, uint32_t s_n);
   13.44 -extern int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n);
   13.45 -
   13.46 -#endif /* ! _SP_MARSHAL_H_ */
    14.1 --- a/tools/libxutil/socket_stream.c	Wed Jun 30 11:07:39 2004 +0000
    14.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.3 @@ -1,259 +0,0 @@
    14.4 -/* $Id: socket_stream.c,v 1.9 2004/03/05 14:45:34 mjw Exp $ */
    14.5 -/*
    14.6 - * Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
    14.7 - *
    14.8 - * This library is free software; you can redistribute it and/or modify
    14.9 - * it under the terms of the GNU Lesser General Public License as published by
   14.10 - * the Free Software Foundation; either version 2.1 of the License, or
   14.11 - * (at your option) any later version.
   14.12 - *
   14.13 - * This library is distributed in the hope that it will be useful,
   14.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14.16 - * GNU Lesser General Public License for more details.
   14.17 - *
   14.18 - * You should have received a copy of the GNU Lesser General Public License
   14.19 - * along with this library; if not, write to the Free Software
   14.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   14.21 - */
   14.22 -
   14.23 -/** @file
   14.24 - * An IOStream implementation using sockets.
   14.25 - */
   14.26 -
   14.27 -#include <stdio.h>
   14.28 -#include <stdlib.h>
   14.29 -#include <string.h>
   14.30 -#include <unistd.h>
   14.31 -#include <errno.h>
   14.32 -#include "allocate.h"
   14.33 -#include "socket_stream.h"
   14.34 -
   14.35 -#define MODULE_NAME "sock"
   14.36 -#define DEBUG 0
   14.37 -//#undef DEBUG
   14.38 -#include "debug.h"
   14.39 -
   14.40 -static int socket_read(IOStream *s, void *buf, size_t n);
   14.41 -static int socket_write(IOStream *s, const void *buf, size_t n);
   14.42 -static int socket_error(IOStream *s);
   14.43 -static int socket_close(IOStream *s);
   14.44 -static void socket_free(IOStream *s);
   14.45 -static int socket_flush(IOStream *s);
   14.46 -
   14.47 -/** Methods used by a socket IOStream. */
   14.48 -static const IOMethods socket_methods = {
   14.49 -    read:  socket_read,
   14.50 -    write: socket_write,
   14.51 -    error: socket_error,
   14.52 -    close: socket_close,
   14.53 -    free:  socket_free,
   14.54 -    flush: socket_flush,
   14.55 -};
   14.56 -
   14.57 -/** Get the socket data.
   14.58 - * 
   14.59 - * @param io socket stream
   14.60 - * @return data
   14.61 - */
   14.62 -static inline SocketData * socket_data(IOStream *io){
   14.63 -    return (SocketData *)io->data;
   14.64 -}
   14.65 -
   14.66 -/** Test if a stream is a socket stream.
   14.67 - *
   14.68 - * @param io stream
   14.69 - * @return 0 if a socket stream, -EINVAL if not
   14.70 - */
   14.71 -int socket_stream_check(IOStream *io){
   14.72 -    return (io && io->methods == &socket_methods ? 0 : -EINVAL);
   14.73 -}
   14.74 -
   14.75 -/** Get the data for a socket stream.
   14.76 - *
   14.77 - * @param io stream
   14.78 - * @param data return value for the data
   14.79 - * @return 0 if a socket stream, -EINVAL if not
   14.80 - */
   14.81 -int socket_stream_data(IOStream *io, SocketData **data){
   14.82 -    int err = socket_stream_check(io);
   14.83 -    if(err){
   14.84 -        *data = NULL;
   14.85 -    } else {
   14.86 -        *data = socket_data(io);
   14.87 -    }
   14.88 -    return err;
   14.89 -}
   14.90 -
   14.91 -/** Set the destination address for a socket stream.
   14.92 - *
   14.93 - * @param io stream
   14.94 - * @param addr address
   14.95 - * @return 0 if a socket stream, -EINVAL if not
   14.96 - */
   14.97 -int socket_stream_set_addr(IOStream *io, struct sockaddr_in *addr){
   14.98 -    int err = 0;
   14.99 -    SocketData *data = NULL;
  14.100 -    err = socket_stream_data(io, &data);
  14.101 -    if(!err){
  14.102 -        data->daddr = *addr;
  14.103 -    }
  14.104 -    return err;
  14.105 -}
  14.106 -
  14.107 -/** Set the send flags for a socket stream.
  14.108 - *
  14.109 - * @param io stream
  14.110 - * @param flags flags
  14.111 - * @return 0 if a socket stream, -EINVAL if not
  14.112 - */
  14.113 -int socket_stream_set_flags(IOStream *io, int flags){
  14.114 -    int err = 0;
  14.115 -    SocketData *data = NULL;
  14.116 -    err = socket_stream_data(io, &data);
  14.117 -    if(!err){
  14.118 -        data->flags = flags;
  14.119 -    }
  14.120 -    return err;
  14.121 -}
  14.122 -
  14.123 -/** Write to the underlying socket using sendto.
  14.124 - *
  14.125 - * @param stream input
  14.126 - * @param buf where to put input
  14.127 - * @param n number of bytes to write
  14.128 - * @return number of bytes written
  14.129 - */
  14.130 -static int socket_write(IOStream *s, const void *buf, size_t n){
  14.131 -    SocketData *data = socket_data(s);
  14.132 -    struct sockaddr *daddr = (struct sockaddr *)&data->daddr;
  14.133 -    socklen_t daddr_n = sizeof(data->daddr);
  14.134 -    int k;
  14.135 -    dprintf("> sock=%d addr=%s:%d n=%d\n",
  14.136 -            data->fd, inet_ntoa(data->daddr.sin_addr), ntohs(data->daddr.sin_port), n);
  14.137 -    if(0){
  14.138 -        struct sockaddr_in self = {};
  14.139 -        socklen_t self_n;
  14.140 -        getsockname(data->fd, (struct sockaddr *)&self, &self_n);
  14.141 -        dprintf("> sockname sock=%d %s:%d\n",
  14.142 -                data->fd, inet_ntoa(self.sin_addr), ntohs(self.sin_port));
  14.143 -    }
  14.144 -    k = sendto(data->fd, buf, n, data->flags, daddr, daddr_n);
  14.145 -    dprintf("> sendto=%d\n", k);
  14.146 -    return k;
  14.147 -}
  14.148 -
  14.149 -/** Read from the underlying stream using recv();
  14.150 - *
  14.151 - * @param stream input
  14.152 - * @param buf where to put input
  14.153 - * @param n number of bytes to read
  14.154 - * @return number of bytes read
  14.155 - */
  14.156 -static int socket_read(IOStream *s, void *buf, size_t n){
  14.157 -    SocketData *data = socket_data(s);
  14.158 -    int k;
  14.159 -    struct sockaddr *saddr = (struct sockaddr *)&data->saddr;
  14.160 -    socklen_t saddr_n = sizeof(data->saddr);
  14.161 -    k = recvfrom(data->fd, buf, n, data->flags, saddr, &saddr_n);
  14.162 -    return k;
  14.163 -}
  14.164 -
  14.165 -/** Print to the underlying socket.
  14.166 - *
  14.167 - * @param s socket stream
  14.168 - * @param msg format to use
  14.169 - * @param args arguments
  14.170 - * @return result of the print
  14.171 - */
  14.172 -static int socket_print(IOStream *s, const char *msg, va_list args){
  14.173 -    SocketData *data = socket_data(s);
  14.174 -    int n;
  14.175 -    n = vsnprintf(data->buf, data->buf_n - 1, msg, args);
  14.176 -    if(0 < n && n < data->buf_n){
  14.177 -        socket_write(s, data->buf, n);
  14.178 -    }
  14.179 -    return n;
  14.180 -}
  14.181 -
  14.182 -/** Read a character from the underlying socket
  14.183 - *
  14.184 - * @param s socket stream
  14.185 - * @return character read, IOSTREAM_EOF on end of socket (or error)
  14.186 - */
  14.187 -static int socket_getc(IOStream *s){
  14.188 -    char b;
  14.189 -    int n, c;
  14.190 -    n = socket_read(s, &b, 1);
  14.191 -    c = (n <= 0 ? IOSTREAM_EOF : b);
  14.192 -    return c;
  14.193 -}
  14.194 -
  14.195 -/** Flush the socket (no-op).
  14.196 - *
  14.197 - * @param s socket stream
  14.198 - * @return 0 on success, error code otherwise
  14.199 - */
  14.200 -static int socket_flush(IOStream *s){
  14.201 -    return 0;
  14.202 -}
  14.203 -
  14.204 -/** Check if a socket stream has an error (no-op).
  14.205 - *
  14.206 - * @param s socket stream
  14.207 - * @return 1 if has an error, 0 otherwise
  14.208 - */
  14.209 -static int socket_error(IOStream *s){
  14.210 -    // Read SOL_SOCKET/SO_ERROR ?
  14.211 -    return 0;
  14.212 -}
  14.213 -
  14.214 -/** Close a socket stream.
  14.215 - *
  14.216 - * @param s socket stream to close
  14.217 - * @return result of the close
  14.218 - */
  14.219 -static int socket_close(IOStream *s){
  14.220 -    SocketData *data = socket_data(s);
  14.221 -    return close(data->fd);
  14.222 -}
  14.223 -
  14.224 -/** Free a socket stream.
  14.225 - *
  14.226 - * @param s socket stream
  14.227 - */
  14.228 -static void socket_free(IOStream *s){
  14.229 -    SocketData *data = socket_data(s);
  14.230 -    deallocate(data);
  14.231 -}
  14.232 -
  14.233 -/** Create an IOStream for a socket.
  14.234 - *
  14.235 - * @param fd socket to wtap
  14.236 - * @return new IOStream using fd for i/o
  14.237 - */
  14.238 -IOStream *socket_stream_new(int fd){
  14.239 -    int err = -ENOMEM;
  14.240 -    IOStream *io = NULL;
  14.241 -    SocketData *data = NULL;
  14.242 -
  14.243 -    io = ALLOCATE(IOStream);
  14.244 -    if(!io) goto exit;
  14.245 -    io->methods = &socket_methods;
  14.246 -    data = ALLOCATE(SocketData);
  14.247 -    if(!data) goto exit;
  14.248 -    io->data = data;
  14.249 -    data->fd = fd;
  14.250 -    data->buf_n = sizeof(data->buf);
  14.251 -    err = 0;
  14.252 -  exit:
  14.253 -    if(err){
  14.254 -        if(io){
  14.255 -            if(data) deallocate(data);
  14.256 -            deallocate(io);
  14.257 -            io = NULL;
  14.258 -        }
  14.259 -    }
  14.260 -    return io;
  14.261 -}
  14.262 -
    15.1 --- a/tools/libxutil/socket_stream.h	Wed Jun 30 11:07:39 2004 +0000
    15.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.3 @@ -1,54 +0,0 @@
    15.4 -/* $Id: socket_stream.h,v 1.2 2004/03/04 17:38:13 mjw Exp $ */
    15.5 -/*
    15.6 - * Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
    15.7 - *
    15.8 - * This library is free software; you can redistribute it and/or modify
    15.9 - * it under the terms of the GNU Lesser General Public License as published by
   15.10 - * the Free Software Foundation; either version 2.1 of the License, or
   15.11 - * (at your option) any later version.
   15.12 - *
   15.13 - * This library is distributed in the hope that it will be useful,
   15.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15.16 - * GNU Lesser General Public License for more details.
   15.17 - *
   15.18 - * You should have received a copy of the GNU Lesser General Public License
   15.19 - * along with this library; if not, write to the Free Software
   15.20 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   15.21 - */
   15.22 -
   15.23 -#ifndef _XEN_LIB_SOCKET_STREAM_H_
   15.24 -#define _XEN_LIB_SOCKET_STREAM_H_
   15.25 -
   15.26 -#ifndef __KERNEL__
   15.27 -#include "iostream.h"
   15.28 -#include <stdio.h>
   15.29 -
   15.30 -#include <sys/socket.h>
   15.31 -#include <netinet/in.h>
   15.32 -#include <arpa/inet.h>
   15.33 -
   15.34 -/** Data associated with a socket stream. */
   15.35 -typedef struct SocketData {
   15.36 -    /** The socket file descriptor. */
   15.37 -    int fd;
   15.38 -    /** Source address from last read (recvfrom). */
   15.39 -    struct sockaddr_in saddr;
   15.40 -    /** Destination address for writes (sendto). */
   15.41 -    struct sockaddr_in daddr;
   15.42 -    /** Write flags (sendto). */
   15.43 -    int flags;
   15.44 -    /** Buffer size. */
   15.45 -    int buf_n;
   15.46 -    /** Buffer for formatted printing. */
   15.47 -    char buf[1024];
   15.48 -} SocketData;
   15.49 -
   15.50 -extern IOStream *socket_stream_new(int fd);
   15.51 -extern int socket_stream_data(IOStream *io, SocketData **data);
   15.52 -extern int socket_stream_check(IOStream *io);
   15.53 -extern int socket_stream_set_addr(IOStream *io, struct sockaddr_in *addr);
   15.54 -extern int socket_stream_set_flags(IOStream *io, int flags);
   15.55 -
   15.56 -#endif
   15.57 -#endif /* !_XEN_LIB_SOCKET_STREAM_H_ */
    16.1 --- a/tools/libxutil/sxpr.c	Wed Jun 30 11:07:39 2004 +0000
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,935 +0,0 @@
    16.4 -/*
    16.5 - *
    16.6 - * This library is free software; you can redistribute it and/or modify
    16.7 - * it under the terms of the GNU Lesser General Public License as
    16.8 - * published by the Free Software Foundation; either version 2.1 of the
    16.9 - * License, or  (at your option) any later version. This library is 
   16.10 - * distributed in the  hope that it will be useful, but WITHOUT ANY
   16.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or
   16.12 - * FITNESS FOR A PARTICULAR PURPOSE.
   16.13 - * See the GNU Lesser General Public License for more details.
   16.14 - *
   16.15 - * You should have received a copy of the GNU Lesser General Public License
   16.16 - * along with this library; if not, write to the Free Software Foundation,
   16.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   16.18 - */
   16.19 -
   16.20 -#include <stdarg.h>
   16.21 -#include "sys_string.h"
   16.22 -#include "lexis.h"
   16.23 -#include "sys_net.h"
   16.24 -#include "hash_table.h"
   16.25 -#include "sxpr.h"
   16.26 -
   16.27 -#include <errno.h>
   16.28 -#undef free
   16.29 -
   16.30 -/** @file
   16.31 - * General representation of sxprs.
   16.32 - * Includes print, equal, and free functions for the sxpr types.
   16.33 - *
   16.34 - * Zero memory containing an Sxpr will have the value ONONE - this is intentional.
   16.35 - * When a function returning an sxpr cannot allocate memory we return ONOMEM.
   16.36 - *
   16.37 - */
   16.38 -
   16.39 -static int atom_print(IOStream *io, Sxpr obj, unsigned flags);
   16.40 -static int atom_equal(Sxpr x, Sxpr y);
   16.41 -static void atom_free(Sxpr obj);
   16.42 -
   16.43 -static int string_print(IOStream *io, Sxpr obj, unsigned flags);
   16.44 -static int string_equal(Sxpr x, Sxpr y);
   16.45 -static void string_free(Sxpr obj);
   16.46 -
   16.47 -static int cons_print(IOStream *io, Sxpr obj, unsigned flags);
   16.48 -static int cons_equal(Sxpr x, Sxpr y);
   16.49 -static void cons_free(Sxpr obj);
   16.50 -
   16.51 -static int null_print(IOStream *io, Sxpr obj, unsigned flags);
   16.52 -static int none_print(IOStream *io, Sxpr obj, unsigned flags);
   16.53 -static int int_print(IOStream *io, Sxpr obj, unsigned flags);
   16.54 -static int bool_print(IOStream *io, Sxpr obj, unsigned flags);
   16.55 -
   16.56 -/** Type definitions. */
   16.57 -static SxprType types[1024] = {
   16.58 -    [T_NONE]     { type:    T_NONE,     name: "none",       print: none_print      },
   16.59 -    [T_NULL]     { type:    T_NULL,     name: "null",       print: null_print      },
   16.60 -    [T_UINT]     { type:    T_UINT,     name: "int",        print: int_print,      },
   16.61 -    [T_BOOL]     { type:    T_BOOL,     name: "bool",       print: bool_print,     },
   16.62 -    [T_ATOM]     { type:    T_ATOM,     name: "atom",       print: atom_print,
   16.63 -		   pointer: TRUE,
   16.64 -		   free:    atom_free,
   16.65 -		   equal:   atom_equal,
   16.66 -		 },
   16.67 -    [T_STRING]   { type:    T_STRING,   name: "string",     print: string_print,
   16.68 -		   pointer: TRUE,
   16.69 -		   free:    string_free,
   16.70 -		   equal:   string_equal,
   16.71 -		 },
   16.72 -    [T_CONS]     { type:    T_CONS,     name: "cons",       print: cons_print,
   16.73 -		   pointer: TRUE,
   16.74 -		   free:    cons_free,
   16.75 -		   equal:   cons_equal,
   16.76 -		 },
   16.77 -};
   16.78 -
   16.79 -/** Number of entries in the types array. */
   16.80 -static int type_sup = sizeof(types)/sizeof(types[0]);
   16.81 -
   16.82 -/** Get the type definition for a given type code.
   16.83 - *
   16.84 - * @param ty type code
   16.85 - * @return type definition or null
   16.86 - */
   16.87 -SxprType *get_sxpr_type(int ty){
   16.88 -    if(0 <= ty && ty < type_sup){
   16.89 -        return types+ty;
   16.90 -    }
   16.91 -    return NULL;
   16.92 -}
   16.93 -
   16.94 -/** The default print function.
   16.95 - *
   16.96 - * @param io stream to print to
   16.97 - * @param x sxpr to print
   16.98 - * @param flags print flags
   16.99 - * @return number of bytes written on success
  16.100 - */
  16.101 -int default_print(IOStream *io, Sxpr x, unsigned flags){
  16.102 -    return IOStream_print(io, "#<%u %lu>\n", get_type(x), get_ul(x));
  16.103 -}
  16.104 -
  16.105 -/** The default equal function.
  16.106 - * Uses eq().
  16.107 - *
  16.108 - * @param x sxpr to compare
  16.109 - * @param y sxpr to compare
  16.110 - * @return 1 if equal, 0 otherwise
  16.111 - */
  16.112 -int default_equal(Sxpr x, Sxpr y){
  16.113 -    return eq(x, y);
  16.114 -}
  16.115 -
  16.116 -/** General sxpr print function.
  16.117 - * Prints an sxpr on a stream using the print function for the sxpr type.
  16.118 - * Printing is controlled by flags from the PrintFlags enum.
  16.119 - * If PRINT_TYPE is in the flags the sxpr type is printed before the sxpr
  16.120 - * (for debugging).
  16.121 - *
  16.122 - * @param io stream to print to
  16.123 - * @param x sxpr to print
  16.124 - * @param flags print flags
  16.125 - * @return number of bytes written
  16.126 - */
  16.127 -int objprint(IOStream *io, Sxpr x, unsigned flags){
  16.128 -    SxprType *def = get_sxpr_type(get_type(x));
  16.129 -    ObjPrintFn *print_fn = (def && def->print ? def->print : default_print);
  16.130 -    int k = 0;
  16.131 -    if(!io) return k;
  16.132 -    if(flags & PRINT_TYPE){
  16.133 -	k += IOStream_print(io, "%s:", def->name);
  16.134 -    }
  16.135 -    k += print_fn(io, x, flags);
  16.136 -    return k;
  16.137 -}
  16.138 -
  16.139 -/** General sxpr free function.
  16.140 - * Frees an sxpr using the free function for its type.
  16.141 - * Free functions must recursively free any subsxprs.
  16.142 - * If no function is defined then the default is to
  16.143 - * free sxprs whose type has pointer true.
  16.144 - * Sxprs must not be used after freeing.
  16.145 - *
  16.146 - * @param x sxpr to free
  16.147 - */
  16.148 -void objfree(Sxpr x){
  16.149 -    SxprType *def = get_sxpr_type(get_type(x));
  16.150 -
  16.151 -    if(def){
  16.152 -	if(def->free){
  16.153 -	    def->free(x);
  16.154 -	} else if (def->pointer){
  16.155 -	    hfree(x);
  16.156 -	}
  16.157 -    }
  16.158 -}
  16.159 -
  16.160 -/** General sxpr equality function.
  16.161 - * Compares x and y using the equal function for x.
  16.162 - * Uses default_equal() if x has no equal function.
  16.163 - *
  16.164 - * @param x sxpr to compare
  16.165 - * @param y sxpr to compare
  16.166 - * @return 1 if equal, 0 otherwise
  16.167 - */
  16.168 -int objequal(Sxpr x, Sxpr y){
  16.169 -    SxprType *def = get_sxpr_type(get_type(x));
  16.170 -    ObjEqualFn *equal_fn = (def && def->equal ? def->equal : default_equal);
  16.171 -    return equal_fn(x, y);
  16.172 -}
  16.173 -
  16.174 -/** Search for a key in an alist.
  16.175 - * An alist is a list of conses, where the cars
  16.176 - * of the conses are the keys. Compares keys using equality.
  16.177 - *
  16.178 - * @param k key
  16.179 - * @param l alist to search
  16.180 - * @return first element of l with car k, or ONULL
  16.181 - */
  16.182 -Sxpr assoc(Sxpr k, Sxpr l){
  16.183 -    for( ; CONSP(l) ; l = CDR(l)){
  16.184 -        Sxpr x = CAR(l);
  16.185 -        if(CONSP(x) && objequal(k, CAR(x))){
  16.186 -            return x;   
  16.187 -        }
  16.188 -    }
  16.189 -    return ONULL;
  16.190 -}
  16.191 -
  16.192 -/** Search for a key in an alist.
  16.193 - * An alist is a list of conses, where the cars
  16.194 - * of the conses are the keys. Compares keys using eq.
  16.195 - *
  16.196 - * @param k key
  16.197 - * @param l alist to search
  16.198 - * @return first element of l with car k, or ONULL
  16.199 - */
  16.200 -Sxpr assocq(Sxpr k, Sxpr l){
  16.201 -    for( ; CONSP(l); l = CDR(l)){
  16.202 -        Sxpr x = CAR(l);
  16.203 -        if(CONSP(x) && eq(k, CAR(x))){
  16.204 -            return x;
  16.205 -        }
  16.206 -    }
  16.207 -    return ONULL;
  16.208 -}
  16.209 -
  16.210 -/** Add a new key and value to an alist.
  16.211 - *
  16.212 - * @param k key
  16.213 - * @param l value
  16.214 - * @param l alist
  16.215 - * @return l with the new cell added to the front
  16.216 - */
  16.217 -Sxpr acons(Sxpr k, Sxpr v, Sxpr l){
  16.218 -    Sxpr x, y;
  16.219 -    x = cons_new(k, v);
  16.220 -    if(NOMEMP(x)) return x;
  16.221 -    y = cons_new(x, l);
  16.222 -    if(NOMEMP(y)) cons_free_cells(x);
  16.223 -    return y;
  16.224 -}
  16.225 -
  16.226 -/** Test if a list contains an element.
  16.227 - * Uses sxpr equality.
  16.228 - *
  16.229 - * @param l list
  16.230 - * @param x element to look for
  16.231 - * @return a tail of l with x as car, or ONULL
  16.232 - */
  16.233 -Sxpr cons_member(Sxpr l, Sxpr x){
  16.234 -    for( ; CONSP(l) && !eq(x, CAR(l)); l = CDR(l)){}
  16.235 -    return l;
  16.236 -}
  16.237 -
  16.238 -/** Test if a list contains an element satisfying a test.
  16.239 - * The test function is called with v and an element of the list.
  16.240 - *
  16.241 - * @param l list
  16.242 - * @param test_fn test function to use
  16.243 - * @param v value for first argument to the test
  16.244 - * @return a tail of l with car satisfying the test, or 0
  16.245 - */
  16.246 -Sxpr cons_member_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v){
  16.247 -    for( ; CONSP(l) && !test_fn(v, CAR(l)); l = CDR(l)){ }
  16.248 -    return l;
  16.249 -}
  16.250 -
  16.251 -/** Test if the elements of list 't' are a subset of the elements
  16.252 - * of list 's'. Element order is not significant.
  16.253 - *
  16.254 - * @param s element list to check subset of
  16.255 - * @param t element list to check if is a subset
  16.256 - * @return 1 if is a subset, 0 otherwise
  16.257 - */
  16.258 -int cons_subset(Sxpr s, Sxpr t){
  16.259 -    for( ; CONSP(t); t = CDR(t)){
  16.260 -	if(!CONSP(cons_member(s, CAR(t)))){
  16.261 -	    return 0;
  16.262 -	}
  16.263 -    }
  16.264 -    return 1;
  16.265 -}
  16.266 -
  16.267 -/** Test if two lists have equal sets of elements.
  16.268 - * Element order is not significant.
  16.269 - *
  16.270 - * @param s list to check
  16.271 - * @param t list to check
  16.272 - * @return 1 if equal, 0 otherwise
  16.273 - */
  16.274 -int cons_set_equal(Sxpr s, Sxpr t){
  16.275 -    return cons_subset(s, t) && cons_subset(t, s);
  16.276 -}
  16.277 -
  16.278 -#ifdef USE_GC
  16.279 -/*============================================================================*/
  16.280 -/* The functions inside this ifdef are only safe if GC is used.
  16.281 - * Otherwise they may leak memory.
  16.282 - */
  16.283 -
  16.284 -/** Remove an element from a list (GC only).
  16.285 - * Uses sxpr equality and removes all instances, even
  16.286 - * if there are more than one.
  16.287 - *
  16.288 - * @param l list to remove elements from
  16.289 - * @param x element to remove
  16.290 - * @return modified input list
  16.291 - */
  16.292 -Sxpr cons_remove(Sxpr l, Sxpr x){
  16.293 -    return cons_remove_if(l, eq, x);
  16.294 -}
  16.295 -
  16.296 -/** Remove elements satisfying a test (GC only).
  16.297 - * The test function is called with v and an element of the set.
  16.298 - *
  16.299 - * @param l list to remove elements from
  16.300 - * @param test_fn function to use to decide if an element should be removed
  16.301 - * @return modified input list
  16.302 - */
  16.303 -Sxpr cons_remove_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v){
  16.304 -    Sxpr prev = ONULL, elt, next;
  16.305 -
  16.306 -    for(elt = l; CONSP(elt); elt = next){
  16.307 -        next = CDR(elt);
  16.308 -        if(test_fn(v, CAR(elt))){
  16.309 -            if(NULLP(prev)){
  16.310 -                l = next;
  16.311 -            } else {
  16.312 -                CDR(prev) = next;
  16.313 -            }
  16.314 -        }
  16.315 -    }
  16.316 -    return l;
  16.317 -}
  16.318 -
  16.319 -/** Set the value for a key in an alist (GC only).
  16.320 - * If the key is present, changes the value, otherwise
  16.321 - * adds a new cell.
  16.322 - *
  16.323 - * @param k key
  16.324 - * @param v value
  16.325 - * @param l alist
  16.326 - * @return modified or extended list
  16.327 - */
  16.328 -Sxpr setf(Sxpr k, Sxpr v, Sxpr l){
  16.329 -    Sxpr e = assoc(k, l);
  16.330 -    if(NULLP(e)){
  16.331 -        l = acons(k, v, l);
  16.332 -    } else {
  16.333 -        CAR(CDR(e)) = v;
  16.334 -    }
  16.335 -    return l;
  16.336 -}
  16.337 -/*============================================================================*/
  16.338 -#endif /* USE_GC */
  16.339 -
  16.340 -/** Create a new atom with the given name.
  16.341 - *
  16.342 - * @param name the name
  16.343 - * @return new atom
  16.344 - */
  16.345 -Sxpr atom_new(char *name){
  16.346 -    Sxpr n, obj = ONOMEM;
  16.347 -
  16.348 -    n = string_new(name);
  16.349 -    if(NOMEMP(n)) goto exit;
  16.350 -    obj = HALLOC(ObjAtom, T_ATOM);
  16.351 -    if(NOMEMP(obj)) goto exit;
  16.352 -    OBJ_ATOM(obj)->name = n;
  16.353 -  exit:
  16.354 -    return obj;
  16.355 -}
  16.356 -
  16.357 -/** Free an atom.
  16.358 - *
  16.359 - * @param obj to free
  16.360 - */
  16.361 -void atom_free(Sxpr obj){
  16.362 -    // Interned atoms are shared, so do not free.
  16.363 -    if(OBJ_ATOM(obj)->interned) return;
  16.364 -    objfree(OBJ_ATOM(obj)->name);
  16.365 -    hfree(obj);
  16.366 -}
  16.367 -
  16.368 -/** Print an atom. Prints the atom name.
  16.369 - *
  16.370 - * @param io stream to print to
  16.371 - * @param obj to print
  16.372 - * @param flags print flags
  16.373 - * @return number of bytes printed
  16.374 - */
  16.375 -int atom_print(IOStream *io, Sxpr obj, unsigned flags){
  16.376 -    //return string_print(io, OBJ_ATOM(obj)->name, (flags | PRINT_RAW));
  16.377 -    return string_print(io, OBJ_ATOM(obj)->name, flags);
  16.378 -}
  16.379 -
  16.380 -/** Atom equality.
  16.381 - *
  16.382 - * @param x to compare
  16.383 - * @param y to compare
  16.384 - * @return 1 if equal, 0 otherwise
  16.385 - */
  16.386 -int atom_equal(Sxpr x, Sxpr y){
  16.387 -    int ok;
  16.388 -    ok = eq(x, y);
  16.389 -    if(ok) goto exit;
  16.390 -    ok = ATOMP(y) && string_equal(OBJ_ATOM(x)->name, OBJ_ATOM(y)->name);
  16.391 -    if(ok) goto exit;
  16.392 -    ok = STRINGP(y) && string_equal(OBJ_ATOM(x)->name, y);
  16.393 -  exit:
  16.394 -    return ok;
  16.395 -}
  16.396 -
  16.397 -/** Get the name of an atom.
  16.398 - *
  16.399 - * @param obj atom
  16.400 - * @return name
  16.401 - */
  16.402 -char * atom_name(Sxpr obj){
  16.403 -    return string_string(OBJ_ATOM(obj)->name);
  16.404 -}
  16.405 -
  16.406 -/** Get the C string from a string sxpr.
  16.407 - *
  16.408 - * @param obj string sxpr
  16.409 - * @return string
  16.410 - */
  16.411 -char * string_string(Sxpr obj){
  16.412 -    return OBJ_STRING(obj);
  16.413 -}
  16.414 -
  16.415 -/** Get the length of a string.
  16.416 - *
  16.417 - * @param obj string
  16.418 - * @return length
  16.419 - */
  16.420 -int string_length(Sxpr obj){
  16.421 -    return strlen(OBJ_STRING(obj));
  16.422 -}
  16.423 -
  16.424 -/** Create a new string. The input string is copied,
  16.425 - * and must be null-terminated.
  16.426 - *
  16.427 - * @param s characters to put in the string
  16.428 - * @return new sxpr
  16.429 - */
  16.430 -Sxpr string_new(char *s){
  16.431 -    int n = (s ? strlen(s) : 0);
  16.432 -    Sxpr obj;
  16.433 -    obj = halloc(n+1, T_STRING);
  16.434 -    if(!NOMEMP(obj)){
  16.435 -        char *str = OBJ_STRING(obj);
  16.436 -        strncpy(str, s, n);
  16.437 -        str[n] = '\0';
  16.438 -    }
  16.439 -    return obj;
  16.440 -}
  16.441 -
  16.442 -/** Free a string.
  16.443 - *
  16.444 - * @param obj to free
  16.445 - */
  16.446 -void string_free(Sxpr obj){
  16.447 -    hfree(obj);
  16.448 -}
  16.449 -
  16.450 -/** Determine if a string needs escapes when printed
  16.451 - * using the given flags.
  16.452 - *
  16.453 - * @param str string to check
  16.454 - * @param flags print flags
  16.455 - * @return 1 if needs escapes, 0 otherwise
  16.456 - */
  16.457 -int needs_escapes(char *str, unsigned flags){
  16.458 -    char *c;
  16.459 -    int val = 0;
  16.460 -
  16.461 -    if(str){
  16.462 -	for(c=str; *c; c++){
  16.463 -	    if(in_alpha_class(*c)) continue;
  16.464 -	    if(in_decimal_digit_class(*c)) continue;
  16.465 -	    if(in_class(*c, "/._+:@~-")) continue;
  16.466 -	    val = 1;
  16.467 -	    break;
  16.468 -	}
  16.469 -    }
  16.470 -    //printf("\n> val=%d str=|%s|\n", val, str);
  16.471 -    return val;
  16.472 -}
  16.473 -
  16.474 -/** Print a string to a stream, with escapes if necessary.
  16.475 - *
  16.476 - * @param io stream to print to
  16.477 - * @param str string
  16.478 - * @param flags print flags
  16.479 - * @return number of bytes written
  16.480 - */
  16.481 -int _string_print(IOStream *io, char *str, unsigned flags){
  16.482 -    int k = 0;
  16.483 -    if((flags & PRINT_RAW) || !needs_escapes(str, flags)){
  16.484 -        k += IOStream_print(io, str);
  16.485 -    } else {
  16.486 -	k += IOStream_print(io, "\"");
  16.487 -	if(str){
  16.488 -            char *s;
  16.489 -            for(s = str; *s; s++){
  16.490 -                if(*s < ' ' || *s >= 127 ){
  16.491 -                    switch(*s){
  16.492 -                    case '\a': k += IOStream_print(io, "\\a");  break;
  16.493 -                    case '\b': k += IOStream_print(io, "\\b");  break;
  16.494 -                    case '\f': k += IOStream_print(io, "\\f");  break;
  16.495 -                    case '\n': k += IOStream_print(io, "\\n");  break;
  16.496 -                    case '\r': k += IOStream_print(io, "\\r");  break;
  16.497 -                    case '\t': k += IOStream_print(io, "\\t");  break;
  16.498 -                    case '\v': k += IOStream_print(io, "\\v");  break;
  16.499 -                    default:
  16.500 -                        // Octal escape;
  16.501 -                        k += IOStream_print(io, "\\%o", *s);
  16.502 -                        break;
  16.503 -                    }
  16.504 -                } else if(*s == c_double_quote ||
  16.505 -                          *s == c_single_quote ||
  16.506 -                          *s == c_escape){
  16.507 -                    k += IOStream_print(io, "\\%c", *s);
  16.508 -                } else {
  16.509 -                    k+= IOStream_print(io, "%c", *s);
  16.510 -                }
  16.511 -            }
  16.512 -	}
  16.513 -	k += IOStream_print(io, "\"");
  16.514 -    }
  16.515 -    return k;
  16.516 -}
  16.517 -
  16.518 -/** Print a string to a stream, with escapes if necessary.
  16.519 - *
  16.520 - * @param io stream to print to
  16.521 - * @param obj string
  16.522 - * @param flags print flags
  16.523 - * @return number of bytes written
  16.524 - */
  16.525 -int string_print(IOStream *io, Sxpr obj, unsigned flags){
  16.526 -    return _string_print(io, OBJ_STRING(obj), flags);
  16.527 -}
  16.528 -
  16.529 -/** Compare an sxpr with a string for equality.
  16.530 - *
  16.531 - * @param x string to compare with
  16.532 - * @param y sxpr to compare
  16.533 - * @return 1 if equal, 0 otherwise
  16.534 - */
  16.535 -int string_equal(Sxpr x, Sxpr y){
  16.536 -    int ok = 0;
  16.537 -    ok = eq(x,y);
  16.538 -    if(ok) goto exit;
  16.539 -    ok = has_type(y, T_STRING) && !strcmp(OBJ_STRING(x), OBJ_STRING(y));
  16.540 -    if(ok) goto exit;
  16.541 -    ok = has_type(y, T_ATOM) && !strcmp(OBJ_STRING(x), atom_name(y));
  16.542 -  exit:
  16.543 -    return ok;
  16.544 -}
  16.545 -
  16.546 -/** Create a new cons cell.
  16.547 - * The cell is ONOMEM if either argument is.
  16.548 - *
  16.549 - * @param car sxpr for the car
  16.550 - * @param cdr sxpr for the cdr
  16.551 - * @return new cons
  16.552 - */
  16.553 -Sxpr cons_new(Sxpr car, Sxpr cdr){
  16.554 -    Sxpr obj;
  16.555 -    if(NOMEMP(car) || NOMEMP(cdr)){
  16.556 -        obj = ONOMEM;
  16.557 -    } else {
  16.558 -        obj = HALLOC(ObjCons, T_CONS);
  16.559 -        if(!NOMEMP(obj)){
  16.560 -            ObjCons *z = OBJ_CONS(obj);
  16.561 -            z->car = car;
  16.562 -            z->cdr = cdr;
  16.563 -        }
  16.564 -    }
  16.565 -    return obj;
  16.566 -}
  16.567 -
  16.568 -/** Push a new element onto a list.
  16.569 - *
  16.570 - * @param list list to add to
  16.571 - * @param elt element to add
  16.572 - * @return 0 if successful, error code otherwise
  16.573 - */
  16.574 -int cons_push(Sxpr *list, Sxpr elt){
  16.575 -    Sxpr l;
  16.576 -    l = cons_new(elt, *list);
  16.577 -    if(NOMEMP(l)) return -ENOMEM;
  16.578 -    *list = l;
  16.579 -    return 0;
  16.580 -}
  16.581 -
  16.582 -/** Free a cons. Recursively frees the car and cdr.
  16.583 - *
  16.584 - * @param obj to free
  16.585 - */
  16.586 -void cons_free(Sxpr obj){
  16.587 -    Sxpr next;
  16.588 -    for(; CONSP(obj); obj = next){
  16.589 -	next = CDR(obj);
  16.590 -	objfree(CAR(obj));
  16.591 -	hfree(obj);
  16.592 -    }
  16.593 -    if(!NULLP(obj)){
  16.594 -	objfree(obj);
  16.595 -    }
  16.596 -}
  16.597 -
  16.598 -/** Free a cons and its cdr cells, but not the car sxprs.
  16.599 - * Does nothing if called on something that is not a cons.
  16.600 - *
  16.601 - * @param obj to free
  16.602 - */
  16.603 -void cons_free_cells(Sxpr obj){
  16.604 -    Sxpr next;
  16.605 -    for(; CONSP(obj); obj = next){
  16.606 -	next = CDR(obj);
  16.607 -	hfree(obj);
  16.608 -    }
  16.609 -}
  16.610 -
  16.611 -/** Print a cons.
  16.612 - * Prints the cons in list format if the cdrs are conses.
  16.613 - * uses pair (dot) format if the last cdr is not a cons (or null).
  16.614 - *
  16.615 - * @param io stream to print to
  16.616 - * @param obj to print
  16.617 - * @param flags print flags
  16.618 - * @return number of bytes written
  16.619 - */
  16.620 -int cons_print(IOStream *io, Sxpr obj, unsigned flags){
  16.621 -    int first = 1;
  16.622 -    int k = 0;
  16.623 -    k += IOStream_print(io, "(");
  16.624 -    for( ; CONSP(obj) ; obj = CDR(obj)){
  16.625 -        if(first){ 
  16.626 -            first = 0;
  16.627 -        } else {
  16.628 -            k += IOStream_print(io, " ");
  16.629 -        }
  16.630 -        k += objprint(io, CAR(obj), flags);
  16.631 -    }
  16.632 -    if(!NULLP(obj)){
  16.633 -        k += IOStream_print(io, " . ");
  16.634 -        k += objprint(io, obj, flags);
  16.635 -    }
  16.636 -    k += IOStream_print(io, ")");
  16.637 -    return (IOStream_error(io) ? -1 : k);
  16.638 -}
  16.639 -
  16.640 -/** Compare a cons with another sxpr for equality.
  16.641 - * If y is a cons, compares the cars and cdrs recursively.
  16.642 - *
  16.643 - * @param x cons to compare
  16.644 - * @param y sxpr to compare
  16.645 - * @return 1 if equal, 0 otherwise
  16.646 - */
  16.647 -int cons_equal(Sxpr x, Sxpr y){
  16.648 -    return CONSP(y) &&
  16.649 -        objequal(CAR(x), CAR(y)) &&
  16.650 -        objequal(CDR(x), CDR(y));
  16.651 -}
  16.652 -
  16.653 -/** Return the length of a cons list.
  16.654 - *
  16.655 - * @param obj list
  16.656 - * @return length
  16.657 - */
  16.658 -int cons_length(Sxpr obj){
  16.659 -    int count = 0;
  16.660 -    for( ; CONSP(obj); obj = CDR(obj)){
  16.661 -        count++;
  16.662 -    }
  16.663 -    return count;
  16.664 -}
  16.665 -
  16.666 -/** Destructively reverse a cons list in-place.
  16.667 - * If the argument is not a cons it is returned unchanged.
  16.668 - * 
  16.669 - * @param l to reverse
  16.670 - * @return reversed list
  16.671 - */
  16.672 -Sxpr nrev(Sxpr l){
  16.673 -    if(CONSP(l)){
  16.674 -	// Iterate down the cells in the list making the cdr of
  16.675 -	// each cell point to the previous cell. The last cell 
  16.676 -	// is the head of the reversed list.
  16.677 -	Sxpr prev = ONULL;
  16.678 -	Sxpr cell = l;
  16.679 -	Sxpr next;
  16.680 -
  16.681 -	while(1){
  16.682 -	    next = CDR(cell);
  16.683 -	    CDR(cell) = prev;
  16.684 -	    if(!CONSP(next)) break;
  16.685 -	    prev = cell;
  16.686 -	    cell = next;
  16.687 -	}
  16.688 -	l = cell;
  16.689 -    }
  16.690 -    return l;
  16.691 -}
  16.692 -
  16.693 -/** Print the null sxpr.	
  16.694 - *
  16.695 - * @param io stream to print to
  16.696 - * @param obj to print
  16.697 - * @param flags print flags
  16.698 - * @return number of bytes written
  16.699 - */
  16.700 -static int null_print(IOStream *io, Sxpr obj, unsigned flags){
  16.701 -    return IOStream_print(io, "()");
  16.702 -}
  16.703 -
  16.704 -/** Print the `unspecified' sxpr none.
  16.705 - *
  16.706 - * @param io stream to print to
  16.707 - * @param obj to print
  16.708 - * @param flags print flags
  16.709 - * @return number of bytes written
  16.710 - */
  16.711 -static int none_print(IOStream *io, Sxpr obj, unsigned flags){
  16.712 -    return IOStream_print(io, "<none>");
  16.713 -}
  16.714 -
  16.715 -/** Print an integer.
  16.716 - *
  16.717 - * @param io stream to print to
  16.718 - * @param obj to print
  16.719 - * @param flags print flags
  16.720 - * @return number of bytes written
  16.721 - */
  16.722 -static int int_print(IOStream *io, Sxpr obj, unsigned flags){
  16.723 -    return IOStream_print(io, "%d", OBJ_INT(obj));
  16.724 -}
  16.725 -
  16.726 -/** Print a boolean.
  16.727 - *
  16.728 - * @param io stream to print to
  16.729 - * @param obj to print
  16.730 - * @param flags print flags
  16.731 - * @return number of bytes written
  16.732 - */
  16.733 -static int bool_print(IOStream *io, Sxpr obj, unsigned flags){
  16.734 -    return IOStream_print(io, (OBJ_UINT(obj) ? k_true : k_false));
  16.735 -}
  16.736 -
  16.737 -int sxprp(Sxpr obj, Sxpr name){
  16.738 -    return CONSP(obj) && objequal(CAR(obj), name);
  16.739 -}
  16.740 -
  16.741 -/** Get the name of an element.
  16.742 - * 
  16.743 - * @param obj element
  16.744 - * @return name
  16.745 - */
  16.746 -Sxpr sxpr_name(Sxpr obj){
  16.747 -    Sxpr val = ONONE;
  16.748 -    if(CONSP(obj)){
  16.749 -        val = CAR(obj);
  16.750 -    } else if(STRINGP(obj) || ATOMP(obj)){
  16.751 -        val = obj;
  16.752 -    }
  16.753 -    return val;
  16.754 -}
  16.755 -
  16.756 -int sxpr_is(Sxpr obj, char *s){
  16.757 -    if(ATOMP(obj)) return !strcmp(atom_name(obj), s);
  16.758 -    if(STRINGP(obj)) return !strcmp(string_string(obj), s);
  16.759 -    return 0;
  16.760 -}
  16.761 -
  16.762 -int sxpr_elementp(Sxpr obj, Sxpr name){
  16.763 -    return CONSP(obj) && objequal(CAR(obj), name);
  16.764 -}
  16.765 -
  16.766 -/** Get the attributes of an sxpr.
  16.767 - * 
  16.768 - * @param obj sxpr
  16.769 - * @return attributes
  16.770 - */
  16.771 -Sxpr sxpr_attributes(Sxpr obj){
  16.772 -    Sxpr val = ONULL;
  16.773 -    if(CONSP(obj)){
  16.774 -        obj = CDR(obj);
  16.775 -        if(CONSP(obj)){
  16.776 -            obj = CAR(obj);
  16.777 -            if(sxprp(obj, intern("@"))){
  16.778 -                val = CDR(obj);
  16.779 -            }
  16.780 -        }
  16.781 -    }
  16.782 -    return val;
  16.783 -}
  16.784 -
  16.785 -Sxpr sxpr_attribute(Sxpr obj, Sxpr key, Sxpr def){
  16.786 -    Sxpr val = ONONE;
  16.787 -    val = assoc(sxpr_attributes(obj), key);
  16.788 -    if(CONSP(val) && CONSP(CDR(val))){
  16.789 -        val = CADR(def);
  16.790 -    } else {
  16.791 -        val = def;
  16.792 -    }
  16.793 -    return val;
  16.794 -}
  16.795 -
  16.796 -/** Get the children of an sxpr.
  16.797 - * 
  16.798 - * @param obj sxpr
  16.799 - * @return children
  16.800 - */
  16.801 -Sxpr sxpr_children(Sxpr obj){
  16.802 -    Sxpr val = ONULL;
  16.803 -    if(CONSP(obj)){
  16.804 -        val = CDR(obj);
  16.805 -        if(CONSP(val) && sxprp(CAR(val), intern("@"))){
  16.806 -            val = CDR(val);
  16.807 -        }
  16.808 -    }
  16.809 -    return val;
  16.810 -}
  16.811 -
  16.812 -Sxpr sxpr_child(Sxpr obj, Sxpr name, Sxpr def){
  16.813 -    Sxpr val = ONONE;
  16.814 -    Sxpr l;
  16.815 -    for(l = sxpr_children(obj); CONSP(l); l = CDR(l)){
  16.816 -        if(sxprp(CAR(l), name)){
  16.817 -            val = CAR(l);
  16.818 -            break;
  16.819 -        }
  16.820 -    }
  16.821 -    if(NONEP(val)) val = def;
  16.822 -    return val;
  16.823 -}
  16.824 -
  16.825 -Sxpr sxpr_child0(Sxpr obj, Sxpr def){
  16.826 -    Sxpr val = ONONE;
  16.827 -    Sxpr l = sxpr_children(obj);
  16.828 -    if(CONSP(l)){
  16.829 -        val = CAR(l);
  16.830 -    } else {
  16.831 -        val = def;
  16.832 -    }
  16.833 -    return val;
  16.834 -}
  16.835 -
  16.836 -Sxpr sxpr_child_value(Sxpr obj, Sxpr name, Sxpr def){
  16.837 -    Sxpr val = ONONE;
  16.838 -    val = sxpr_child(obj, name, ONONE);
  16.839 -    if(NONEP(val)){
  16.840 -        val = def;
  16.841 -    } else {
  16.842 -        val = sxpr_child0(val, def);
  16.843 -    }
  16.844 -    return val;
  16.845 -}
  16.846 -
  16.847 -/** Table of interned symbols. Indexed by symbol name. */
  16.848 -static HashTable *symbols = NULL;
  16.849 -
  16.850 -/** Hash function for entries in the symbol table.
  16.851 - *
  16.852 - * @param key to hash
  16.853 - * @return hashcode
  16.854 - */
  16.855 -static Hashcode sym_hash_fn(void *key){
  16.856 -    return hash_string((char*)key);
  16.857 -}
  16.858 -
  16.859 -/** Key equality function for the symbol table.
  16.860 - *
  16.861 - * @param x to compare
  16.862 - * @param y to compare
  16.863 - * @return 1 if equal, 0 otherwise
  16.864 - */
  16.865 -static int sym_equal_fn(void *x, void *y){
  16.866 -    return !strcmp((char*)x, (char*)y);
  16.867 -}
  16.868 -
  16.869 -/** Entry free function for the symbol table.
  16.870 - *
  16.871 - * @param table the entry is in
  16.872 - * @param entry being freed
  16.873 - */
  16.874 -static void sym_free_fn(HashTable *table, HTEntry *entry){
  16.875 -    if(entry){
  16.876 -	objfree(((ObjAtom*)entry->value)->name);
  16.877 -	HTEntry_free(entry);
  16.878 -    }
  16.879 -}
  16.880 -	
  16.881 -/** Initialize the symbol table.
  16.882 - *
  16.883 - * @return 0 on sucess, error code otherwise
  16.884 - */
  16.885 -static int init_symbols(void){
  16.886 -    symbols = HashTable_new(100);
  16.887 -    if(symbols){
  16.888 -        symbols->key_hash_fn = sym_hash_fn;
  16.889 -        symbols->key_equal_fn = sym_equal_fn;
  16.890 -	symbols->entry_free_fn = sym_free_fn;
  16.891 -        return 0;
  16.892 -    }
  16.893 -    return -1;
  16.894 -}
  16.895 -
  16.896 -/** Cleanup the symbol table. Frees the table and all its symbols.
  16.897 - */
  16.898 -void cleanup_symbols(void){
  16.899 -    HashTable_free(symbols);
  16.900 -    symbols = NULL;
  16.901 -}
  16.902 -
  16.903 -/** Get the interned symbol with the given name.
  16.904 - * No new symbol is created.
  16.905 - *
  16.906 - * @return symbol or null
  16.907 - */
  16.908 -Sxpr get_symbol(char *sym){
  16.909 -    HTEntry *entry;
  16.910 -    if(!symbols){
  16.911 -	if(init_symbols()) return ONOMEM;
  16.912 -	return ONULL;
  16.913 -    }
  16.914 -    entry = HashTable_get_entry(symbols, sym);
  16.915 -    if(entry){
  16.916 -        return OBJP(T_ATOM, entry->value);
  16.917 -    } else {
  16.918 -        return ONULL;
  16.919 -    }
  16.920 -}
  16.921 -
  16.922 -/** Get the interned symbol with the given name.
  16.923 - * Creates a new symbol if necessary.
  16.924 - *
  16.925 - * @return symbol
  16.926 - */
  16.927 -Sxpr intern(char *sym){
  16.928 -    Sxpr symbol = get_symbol(sym);
  16.929 -    if(NULLP(symbol)){
  16.930 -	if(!symbols) return ONOMEM;
  16.931 -        symbol = atom_new(sym);
  16.932 -        if(!NOMEMP(symbol)){
  16.933 -	    OBJ_ATOM(symbol)->interned = TRUE;
  16.934 -            HashTable_add(symbols, atom_name(symbol), get_ptr(symbol));
  16.935 -        }
  16.936 -    }
  16.937 -    return symbol;
  16.938 -}
    17.1 --- a/tools/libxutil/sxpr.h	Wed Jun 30 11:07:39 2004 +0000
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,413 +0,0 @@
    17.4 -/*
    17.5 - *
    17.6 - * This library is free software; you can redistribute it and/or modify
    17.7 - * it under the terms of the GNU Lesser General Public License as
    17.8 - * published by the Free Software Foundation; either version 2.1 of the
    17.9 - * License, or  (at your option) any later version. This library is 
   17.10 - * distributed in the  hope that it will be useful, but WITHOUT ANY
   17.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or
   17.12 - * FITNESS FOR A PARTICULAR PURPOSE.
   17.13 - * See the GNU Lesser General Public License for more details.
   17.14 - *
   17.15 - * You should have received a copy of the GNU Lesser General Public License
   17.16 - * along with this library; if not, write to the Free Software Foundation,
   17.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   17.18 - */
   17.19 -#ifndef _XEN_LIB_SXPR_H_
   17.20 -#define _XEN_LIB_SXPR_H_
   17.21 -
   17.22 -#include <stdint.h>
   17.23 -
   17.24 -#include "hash_table.h"
   17.25 -#include "iostream.h"
   17.26 -#include "allocate.h"
   17.27 -
   17.28 -/** @file
   17.29 - * Definitions for rules and sxprs.
   17.30 - */
   17.31 -
   17.32 -#ifndef NULL
   17.33 -#define NULL 0
   17.34 -#endif
   17.35 -
   17.36 -#ifndef TRUE
   17.37 -#define TRUE 1
   17.38 -#endif
   17.39 -
   17.40 -#ifndef FALSE
   17.41 -#define FALSE 0
   17.42 -#endif
   17.43 -
   17.44 -/** Sxpr type. */
   17.45 -typedef int16_t TypeCode;
   17.46 -
   17.47 -/** A typed sxpr handle.*/
   17.48 -typedef struct Sxpr {
   17.49 -    /** Sxpr type. */
   17.50 -    TypeCode type;
   17.51 -    union {
   17.52 -	/** Sxpr value. */
   17.53 -        unsigned long ul;
   17.54 -	/** Pointer. */
   17.55 -        void *ptr;
   17.56 -    } v;
   17.57 -} Sxpr;
   17.58 -
   17.59 -/** Sxpr type to indicate out of memory. */
   17.60 -#define T_NOMEM      ((TypeCode)-1)
   17.61 -/** The 'unspecified' sxpr. */
   17.62 -#define T_NONE       ((TypeCode)0)
   17.63 -/** The empty list. */
   17.64 -#define T_NULL       ((TypeCode)1)
   17.65 -/** Unsigned integer. */
   17.66 -#define T_UINT       ((TypeCode)2)
   17.67 -/** A string. */
   17.68 -#define T_STRING     ((TypeCode)3)
   17.69 -/** An atom. */
   17.70 -#define T_ATOM       ((TypeCode)4)
   17.71 -/** A boolean. */
   17.72 -#define T_BOOL       ((TypeCode)5)
   17.73 -
   17.74 -/** A cons (pair or list). */
   17.75 -#define T_CONS       ((TypeCode)10)
   17.76 -
   17.77 -/** An error. */
   17.78 -#define T_ERR        ((TypeCode)40)
   17.79 -
   17.80 -/** An atom. */
   17.81 -typedef struct ObjAtom {
   17.82 -    Sxpr name;
   17.83 -    Hashcode hashcode;
   17.84 -    int interned;
   17.85 -} ObjAtom;
   17.86 -
   17.87 -/** A cons (pair). */
   17.88 -typedef struct ObjCons {
   17.89 -    Sxpr car;
   17.90 -    Sxpr cdr;
   17.91 -} ObjCons;
   17.92 -
   17.93 -/** A vector. */
   17.94 -typedef struct ObjVector {
   17.95 -    int n;
   17.96 -    Sxpr data[0];
   17.97 -} ObjVector;
   17.98 -
   17.99 -/** Flags for sxpr printing. */
  17.100 -enum PrintFlags {
  17.101 -    PRINT_RAW           = 0x001,
  17.102 -    PRINT_TYPE          = 0x002,
  17.103 -    PRINT_PRETTY        = 0x004,
  17.104 -    PRINT_NUM           = 0x008,
  17.105 -};
  17.106 -
  17.107 -/** An integer sxpr.
  17.108 - *
  17.109 - * @param ty type
  17.110 - * @param val integer value
  17.111 - */
  17.112 -#define OBJI(ty, val) (Sxpr){ type: (ty), v: { ul: (val) }}
  17.113 -
  17.114 -/** A pointer sxpr.
  17.115 - * If the pointer is non-null, returns an sxpr containing it.
  17.116 - * If the pointer is null, returns ONOMEM.
  17.117 - *
  17.118 - * @param ty type
  17.119 - * @param val pointer
  17.120 - */
  17.121 -#define OBJP(ty, val) ((val) ? (Sxpr){ type: (ty), v: { ptr: (val) }} : ONOMEM)
  17.122 -
  17.123 -/** Make an integer sxpr containing a pointer.
  17.124 - *
  17.125 - * @param val pointer
  17.126 - */
  17.127 -#define PTR(val) OBJP(T_UINT, (void*)(val))
  17.128 -
  17.129 -/** Make an integer sxpr.
  17.130 - * @param x value
  17.131 - */
  17.132 -#define OINT(x)       OBJI(T_UINT,  x)
  17.133 -
  17.134 -/** Make an error sxpr.
  17.135 - *
  17.136 - * @param x value
  17.137 - */
  17.138 -#define OERR(x)       OBJI(T_ERR,   x)
  17.139 -
  17.140 -/** Out of memory constant. */
  17.141 -#define ONOMEM        OBJI(T_NOMEM, 0)
  17.142 -
  17.143 -/** The `unspecified' constant. */
  17.144 -#define ONONE         OBJI(T_NONE,  0)
  17.145 -
  17.146 -/** Empty list constant. */
  17.147 -#define ONULL         OBJI(T_NULL,  0)
  17.148 -
  17.149 -/** False constant. */
  17.150 -#define OFALSE        OBJI(T_BOOL,  0)
  17.151 -
  17.152 -/** True constant. */
  17.153 -#define OTRUE         OBJI(T_BOOL,  1)
  17.154 -
  17.155 -/* Recognizers for the various sxpr types.  */
  17.156 -#define ATOMP(obj)        has_type(obj, T_ATOM)
  17.157 -#define BOOLP(obj)        has_type(obj, T_BOOL)
  17.158 -#define CONSP(obj)        has_type(obj, T_CONS)
  17.159 -#define ERRP(obj)         has_type(obj, T_ERR)
  17.160 -#define INTP(obj)         has_type(obj, T_UINT)
  17.161 -#define NOMEMP(obj)       has_type(obj, T_NOMEM)
  17.162 -#define NONEP(obj)        has_type(obj, T_NONE)
  17.163 -#define NULLP(obj)        has_type(obj, T_NULL)
  17.164 -#define STRINGP(obj)      has_type(obj, T_STRING)
  17.165 -
  17.166 -#define TRUEP(obj)    get_ul(obj)
  17.167 -
  17.168 -/** Convert an sxpr to an unsigned integer. */
  17.169 -#define OBJ_UINT(x)   get_ul(x)
  17.170 -/** Convert an sxpr to an integer. */
  17.171 -#define OBJ_INT(x)    (int)get_ul(x)
  17.172 -
  17.173 -/* Conversions of sxprs to their values.
  17.174 - * No checking is done.
  17.175 - */
  17.176 -#define OBJ_STRING(x)  ((char*)get_ptr(x))
  17.177 -#define OBJ_CONS(x)    ((ObjCons*)get_ptr(x))
  17.178 -#define OBJ_ATOM(x)    ((ObjAtom*)get_ptr(x))
  17.179 -#define OBJ_SET(x)     ((ObjSet*)get_ptr(x))
  17.180 -#define CAR(x)         (OBJ_CONS(x)->car)
  17.181 -#define CDR(x)         (OBJ_CONS(x)->cdr)
  17.182 -
  17.183 -#define CAAR(x)        (CAR(CAR(x)))
  17.184 -#define CADR(x)        (CAR(CDR(x)))
  17.185 -#define CDAR(x)        (CDR(CAR(x)))
  17.186 -#define CDDR(x)        (CDR(CDR(x)))
  17.187 -
  17.188 -/** Get the integer value from an sxpr.
  17.189 - *
  17.190 - * @param obj sxpr
  17.191 - * @return value
  17.192 - */
  17.193 -static inline unsigned long get_ul(Sxpr obj){
  17.194 -    return obj.v.ul;
  17.195 -}
  17.196 -
  17.197 -/** Get the pointer value from an sxpr.
  17.198 - *
  17.199 - * @param obj sxpr
  17.200 - * @return value
  17.201 - */
  17.202 -static inline void * get_ptr(Sxpr obj){
  17.203 -    return obj.v.ptr;
  17.204 -}
  17.205 -
  17.206 -/** Create an sxpr containing a pointer.
  17.207 - *
  17.208 - * @param type typecode
  17.209 - * @param val pointer
  17.210 - * @return sxpr
  17.211 - */
  17.212 -static inline Sxpr obj_ptr(TypeCode type, void *val){
  17.213 -    return (Sxpr){ type: type, v: { ptr: val } };
  17.214 -}
  17.215 -
  17.216 -/** Create an sxpr containing an integer.
  17.217 - *
  17.218 - * @param type typecode
  17.219 - * @param val integer
  17.220 - * @return sxpr
  17.221 - */
  17.222 -static inline Sxpr obj_ul(TypeCode type, unsigned long val){
  17.223 -    return (Sxpr){ type: type, v: { ul: val } };
  17.224 -}
  17.225 -
  17.226 -/** Get the type of an sxpr.
  17.227 - *
  17.228 - * @param obj sxpr
  17.229 - * @return type
  17.230 - */
  17.231 -static inline TypeCode get_type(Sxpr obj){
  17.232 -    return obj.type;
  17.233 -}
  17.234 -
  17.235 -/** Check the type of an sxpr.
  17.236 - *
  17.237 - * @param obj sxpr
  17.238 - * @param type to check
  17.239 - * @return 1 if has the type, 0 otherwise
  17.240 - */
  17.241 -static inline int has_type(Sxpr obj, TypeCode type){
  17.242 -    return get_type(obj) == type;
  17.243 -}
  17.244 -
  17.245 -/** Compare sxprs for literal equality of type and value.
  17.246 - *
  17.247 - * @param x sxpr to compare
  17.248 - * @param y sxpr to compare
  17.249 - * @return 1 if equal, 0 otherwise
  17.250 - */
  17.251 -static inline int eq(Sxpr x, Sxpr y){
  17.252 -    return ((get_type(x) == get_type(y)) && (get_ul(x) == get_ul(y)));
  17.253 -}
  17.254 -
  17.255 -/** Checked version of CAR
  17.256 - *
  17.257 - * @param x sxpr
  17.258 - * @return CAR if a cons, x otherwise
  17.259 - */
  17.260 -static inline Sxpr car(Sxpr x){
  17.261 -    return (CONSP(x) ? CAR(x) : x);
  17.262 -}
  17.263 -
  17.264 -/** Checked version of CDR.
  17.265 - *
  17.266 - * @param x sxpr
  17.267 - * @return CDR if a cons, null otherwise
  17.268 - */
  17.269 -static inline Sxpr cdr(Sxpr x){
  17.270 -    return (CONSP(x) ? CDR(x) : ONULL);
  17.271 -}
  17.272 -
  17.273 -/** Allocate some memory and return an sxpr containing it.
  17.274 - * Returns ONOMEM if allocation failed.
  17.275 - *
  17.276 - * @param n number of bytes to allocate
  17.277 - * @param ty typecode
  17.278 - * @return sxpr
  17.279 - */
  17.280 -static inline Sxpr halloc(size_t n,  TypeCode ty){
  17.281 -    return OBJP(ty, allocate(n));
  17.282 -}
  17.283 -
  17.284 -/** Allocate an sxpr containing a pointer to the given type.
  17.285 - *
  17.286 - * @param ty type (uses sizeof to determine how many bytes to allocate)
  17.287 - * @param code typecode
  17.288 - * @return sxpr, ONOMEM if allocation failed
  17.289 - */
  17.290 -#define HALLOC(ty, code) halloc(sizeof(ty), code)
  17.291 -
  17.292 -typedef int ObjPrintFn(IOStream *io, Sxpr obj, unsigned flags);
  17.293 -typedef int ObjEqualFn(Sxpr obj, Sxpr other);
  17.294 -typedef void ObjFreeFn(Sxpr obj);
  17.295 -
  17.296 -/** An sxpr type definition. */
  17.297 -typedef struct SxprType {
  17.298 -    TypeCode type;
  17.299 -    char *name;
  17.300 -    int pointer;
  17.301 -    ObjPrintFn *print;
  17.302 -    ObjEqualFn *equal;
  17.303 -    ObjFreeFn *free;
  17.304 -} SxprType;
  17.305 -
  17.306 -
  17.307 -extern SxprType *get_sxpr_type(int ty);
  17.308 -
  17.309 -/** Free the pointer in an sxpr.
  17.310 - *
  17.311 - * @param x sxpr containing a pointer
  17.312 - */
  17.313 -static inline void hfree(Sxpr x){
  17.314 -    deallocate(get_ptr(x));
  17.315 -}
  17.316 -
  17.317 -extern int objprint(IOStream *io, Sxpr x, unsigned flags);
  17.318 -extern int objequal(Sxpr x, Sxpr y);
  17.319 -extern void objfree(Sxpr x);
  17.320 -
  17.321 -extern void cons_free_cells(Sxpr obj);
  17.322 -extern Sxpr intern(char *s);
  17.323 -
  17.324 -extern Sxpr assoc(Sxpr k, Sxpr l);
  17.325 -extern Sxpr assocq(Sxpr k, Sxpr l);
  17.326 -extern Sxpr acons(Sxpr k, Sxpr v, Sxpr l);
  17.327 -extern Sxpr nrev(Sxpr l);
  17.328 -extern Sxpr cons_member(Sxpr l, Sxpr x);
  17.329 -extern Sxpr cons_member_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
  17.330 -extern int cons_subset(Sxpr s, Sxpr t);
  17.331 -extern int cons_set_equal(Sxpr s, Sxpr t);
  17.332 -
  17.333 -#ifdef USE_GC
  17.334 -extern Sxpr cons_remove(Sxpr l, Sxpr x);
  17.335 -extern Sxpr cons_remove_if(Sxpr l, ObjEqualFn *test_fn, Sxpr v);
  17.336 -#endif
  17.337 -
  17.338 -extern Sxpr atom_new(char *name);
  17.339 -extern char * atom_name(Sxpr obj);
  17.340 -
  17.341 -extern Sxpr string_new(char *s);
  17.342 -extern char * string_string(Sxpr obj);
  17.343 -extern int string_length(Sxpr obj);
  17.344 -
  17.345 -extern Sxpr cons_new(Sxpr car, Sxpr cdr);
  17.346 -extern int cons_push(Sxpr *list, Sxpr elt);
  17.347 -extern int cons_length(Sxpr obj);
  17.348 -
  17.349 -Sxpr sxpr_name(Sxpr obj);
  17.350 -int sxpr_is(Sxpr obj, char *s);
  17.351 -int sxpr_elementp(Sxpr obj, Sxpr name);
  17.352 -Sxpr sxpr_attributes(Sxpr obj);
  17.353 -Sxpr sxpr_attribute(Sxpr obj, Sxpr key, Sxpr def);
  17.354 -Sxpr sxpr_children(Sxpr obj);
  17.355 -Sxpr sxpr_child(Sxpr obj, Sxpr name, Sxpr def);
  17.356 -Sxpr sxpr_child0(Sxpr obj, Sxpr def);
  17.357 -Sxpr sxpr_child_value(Sxpr obj, Sxpr name, Sxpr def);
  17.358 -
  17.359 -/** Create a new atom.
  17.360 - *
  17.361 - * @param s atom name
  17.362 - * @return new atom
  17.363 - */
  17.364 -static inline Sxpr mkatom(char *s){
  17.365 -    return atom_new(s);
  17.366 -}
  17.367 -
  17.368 -/** Create a new string sxpr.
  17.369 - *
  17.370 - * @param s string bytes (copied)
  17.371 - * @return new string
  17.372 - */
  17.373 -static inline Sxpr mkstring(char *s){
  17.374 -    return string_new(s);
  17.375 -}
  17.376 -
  17.377 -/** Create an integer sxpr.
  17.378 - *
  17.379 - * @param i value
  17.380 - * @return sxpr
  17.381 - */
  17.382 -static inline Sxpr mkint(int i){
  17.383 -    return OBJI(T_UINT, i);
  17.384 -}
  17.385 -
  17.386 -/** Create a boolean sxpr.
  17.387 - *
  17.388 - * @param b value
  17.389 - * @return sxpr
  17.390 - */
  17.391 -static inline Sxpr mkbool(int b){
  17.392 -    return OBJI(T_BOOL, (b ? 1 : 0));
  17.393 -}
  17.394 -
  17.395 -/* Constants used in parsing and printing. */
  17.396 -#define k_list_open    "("
  17.397 -#define c_list_open    '('
  17.398 -#define k_list_close   ")"
  17.399 -#define c_list_close   ')'
  17.400 -#define k_true         "true"
  17.401 -#define k_false        "false"
  17.402 -
  17.403 -#define c_var          '$'
  17.404 -#define c_escape       '\\'
  17.405 -#define c_single_quote '\''
  17.406 -#define c_double_quote '"'
  17.407 -#define c_string_open  c_double_quote
  17.408 -#define c_string_close c_double_quote
  17.409 -#define c_data_open    '['
  17.410 -#define c_data_close   ']'
  17.411 -#define c_binary       '*'
  17.412 -#define c_eval         '!'
  17.413 -#define c_concat_open  '{'
  17.414 -#define c_concat_close '}'
  17.415 -
  17.416 -#endif /* ! _XEN_LIB_SXPR_H_ */
    18.1 --- a/tools/libxutil/sxpr_parser.c	Wed Jun 30 11:07:39 2004 +0000
    18.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3 @@ -1,897 +0,0 @@
    18.4 -
    18.5 -#ifdef __KERNEL__
    18.6 -#  include <linux/config.h>
    18.7 -#  include <linux/module.h>
    18.8 -#  include <linux/kernel.h>
    18.9 -#  include <linux/string.h>
   18.10 -#  include <linux/errno.h>
   18.11 -#else
   18.12 -#  include <stdlib.h>
   18.13 -#  include <errno.h>
   18.14 -#endif
   18.15 -
   18.16 -#include "iostream.h"
   18.17 -#include "lexis.h"
   18.18 -#include "sxpr_parser.h"
   18.19 -#include "sys_string.h"
   18.20 -
   18.21 -/** @file
   18.22 - * Sxpr parsing.
   18.23 - *
   18.24 - * So that the parser does not leak memory, all sxprs constructed by
   18.25 - * the parser must be freed on error.  On successful parse the sxpr
   18.26 - * returned becomes the responsibility of the caller.
   18.27 - *
   18.28 - * @author Mike Wray <mike.wray@hpl.hp.com>
   18.29 - */
   18.30 -
   18.31 -#define dprintf(fmt, args...) IOStream_print(iostdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args)
   18.32 -#define printf(fmt, args...)   IOStream_print(iostdout, fmt, ##args)
   18.33 -
   18.34 -static void reset(Parser *z);
   18.35 -static int inputchar(Parser *p, char c);
   18.36 -static int savechar(Parser *p, char c);
   18.37 -extern void parse_error(Parser *in);
   18.38 -extern void parse_error_id(Parser *in, ParseErrorId id);
   18.39 -
   18.40 -static int begin_start(Parser *p, char c);
   18.41 -static int state_start(Parser *p, char c);
   18.42 -static int end_start(Parser *p);
   18.43 -
   18.44 -static int begin_comment(Parser *p, char c);
   18.45 -static int state_comment(Parser *p, char c);
   18.46 -static int end_comment(Parser *p);
   18.47 -
   18.48 -static int begin_string(Parser *p, char c);
   18.49 -static int state_string(Parser *p, char c);
   18.50 -static int end_string(Parser *p);
   18.51 -static int state_escape(Parser *p, char c);
   18.52 -static int state_octal(Parser *p, char c);
   18.53 -static int state_hex(Parser *p, char c);
   18.54 -
   18.55 -static int begin_atom(Parser *p, char c);
   18.56 -static int state_atom(Parser *p, char c);
   18.57 -static int end_atom(Parser *p);
   18.58 -
   18.59 -static int state_list(Parser *p, char c);
   18.60 -static int begin_list(Parser *p, char c);
   18.61 -static int end_list(Parser *p);
   18.62 -
   18.63 -/** Print a parse error.
   18.64 - *
   18.65 - * @param in parser
   18.66 - * @param msg format followed by printf arguments
   18.67 - */
   18.68 -void eprintf(Parser *in, char *msg, ...){
   18.69 -    va_list args;
   18.70 -    if(in->error_out){
   18.71 -        va_start(args, msg);
   18.72 -        IOStream_vprint(in->error_out, msg, args);
   18.73 -        va_end(args);
   18.74 -    }
   18.75 -}
   18.76 -
   18.77 -/** Print a parse warning.
   18.78 - *
   18.79 - * @param in parser
   18.80 - * @param msg format followed by printf arguments
   18.81 - */
   18.82 -void wprintf(Parser *in, char *msg, ...){
   18.83 -    va_list args;
   18.84 -    if(in->error_out){
   18.85 -        va_start(args, msg);
   18.86 -        IOStream_vprint(in->error_out, msg, args);
   18.87 -        va_end(args);
   18.88 -    }
   18.89 -}
   18.90 -
   18.91 -/*============================================================================*/
   18.92 -
   18.93 -/** Record defining the message for a parse error. */
   18.94 -typedef struct {
   18.95 -  ParseErrorId id;
   18.96 -  char *message;
   18.97 -} ParseError;
   18.98 -
   18.99 -/** Format for printing parse error messages. */
  18.100 -#define PARSE_ERR_FMT "parse error> line %3d, column %2d: %s"
  18.101 -
  18.102 -/** Message catalog for the parse error codes. */
  18.103 -static ParseError catalog[] = {
  18.104 -  { PARSE_ERR_UNSPECIFIED,            "unspecified error" },
  18.105 -  { PARSE_ERR_NOMEM,                  "out of memory" },
  18.106 -  { PARSE_ERR_UNEXPECTED_EOF,         "unexpected end of input" },
  18.107 -  { PARSE_ERR_TOKEN_TOO_LONG,         "token too long" },
  18.108 -  { PARSE_ERR_INVALID_SYNTAX,         "syntax error" },
  18.109 -  { PARSE_ERR_INVALID_ESCAPE,         "invalid escape" },
  18.110 -  { 0, NULL }
  18.111 -};
  18.112 -
  18.113 -/** Number of entries in the message catalog. */
  18.114 -const static int catalog_n = sizeof(catalog)/sizeof(ParseError);
  18.115 -
  18.116 -void ParserState_free(ParserState *z){
  18.117 -    if(!z) return;
  18.118 -    objfree(z->val);
  18.119 -    deallocate(z);
  18.120 -}
  18.121 -
  18.122 -int ParserState_new(ParserStateFn *fn, ParserState *parent, ParserState **val){
  18.123 -    int err = 0;
  18.124 -    ParserState *z;
  18.125 -    z = ALLOCATE(ParserState);
  18.126 -    if(z){
  18.127 -        z->fn = fn;
  18.128 -        z->parent = parent;
  18.129 -        z->val = ONULL;
  18.130 -    } else {
  18.131 -        err = -ENOMEM;
  18.132 -    }
  18.133 -    if(!err) *val = z;
  18.134 -    return err;
  18.135 -}
  18.136 -
  18.137 -/** Free a parser.
  18.138 - * No-op if the parser is null.
  18.139 - *
  18.140 - * @param z parser 
  18.141 - */
  18.142 -void Parser_free(Parser *z){
  18.143 -    if(!z) return;
  18.144 -    objfree(z->val);
  18.145 -    z->val = ONONE;
  18.146 -    deallocate(z);
  18.147 -}
  18.148 -
  18.149 -/** Create a new parser. The error stream defaults to null.
  18.150 - */
  18.151 -Parser * Parser_new(void){
  18.152 -    Parser *z = ALLOCATE(Parser);
  18.153 -    int err = -ENOMEM;
  18.154 -  
  18.155 -    if(!z) goto exit;
  18.156 -    err = 0;
  18.157 -    reset(z);
  18.158 -  exit:
  18.159 -    if(err){
  18.160 -        Parser_free(z);
  18.161 -        z = NULL;
  18.162 -    }
  18.163 -    return z;
  18.164 -}
  18.165 -
  18.166 -/** Get the next character.
  18.167 - * Records the character read in the parser,
  18.168 - * and sets the line and character counts.
  18.169 - *
  18.170 - * @param p parser
  18.171 - * @return error flag: 0 on success, non-zero on error
  18.172 - */
  18.173 -static int inputchar(Parser *p, char c){
  18.174 -    int err = 0;
  18.175 -    if(c=='\n'){
  18.176 -        p->line_no++;
  18.177 -        p->char_no = 0;
  18.178 -    } else {
  18.179 -        p->char_no++;
  18.180 -    }
  18.181 -    return err;
  18.182 -}
  18.183 -
  18.184 -static int savechar(Parser *p, char c){
  18.185 -    int err = 0;
  18.186 -    if(p->buf_i >= p->buf_n){
  18.187 -        err = -ENOMEM;
  18.188 -        goto exit;
  18.189 -    }
  18.190 -    p->buf[p->buf_i] = c;
  18.191 -    p->buf_i++;
  18.192 -  exit:
  18.193 -    return err;
  18.194 -}
  18.195 -
  18.196 -int Parser_input_char(Parser *p, char c){
  18.197 -    int err = 0;
  18.198 -    if(at_eof(p)){
  18.199 -        //skip;
  18.200 -    } else {
  18.201 -        inputchar(p, c);
  18.202 -    }
  18.203 -    if(!p->state){
  18.204 -        err = begin_start(p, c);
  18.205 -        if(err) goto exit;
  18.206 -    }
  18.207 -    err = p->state->fn(p, c);
  18.208 -  exit:
  18.209 -    return err;
  18.210 -}
  18.211 -
  18.212 -int Parser_input_eof(Parser *p){
  18.213 -    int err = 0;
  18.214 -    p->eof = 1;
  18.215 -    err = Parser_input_char(p, IOSTREAM_EOF);
  18.216 -    return err;
  18.217 -}
  18.218 -
  18.219 -int Parser_input(Parser *p, char *buf, int buf_n){
  18.220 -    int err = 0;
  18.221 -    int i = 0;
  18.222 -    if(buf_n <= 0){
  18.223 -        err = Parser_input_eof(p);
  18.224 -        goto exit;
  18.225 -    }
  18.226 -    for(i = 0; i<buf_n; i++){
  18.227 -        err = Parser_input_char(p, buf[i]);
  18.228 -        if(err) goto exit;
  18.229 -    }
  18.230 -  exit:
  18.231 -    err = (err < 0 ? err : buf_n);
  18.232 -    return err;
  18.233 -}
  18.234 -
  18.235 -int Parser_push(Parser *p, ParserStateFn *fn){
  18.236 -    int err = 0;
  18.237 -    err = ParserState_new(fn, p->state, &p->state);
  18.238 -    return err;
  18.239 -}
  18.240 -        
  18.241 -int Parser_pop(Parser *p){
  18.242 -    int err = 0;
  18.243 -    ParserState *s = p->state;
  18.244 -    p->state = s->parent;
  18.245 -    ParserState_free(s);
  18.246 -    return err;
  18.247 -}
  18.248 -
  18.249 -int Parser_return(Parser *p){
  18.250 -    int err = 0;
  18.251 -    Sxpr val = ONONE;
  18.252 -    if(!p->state){
  18.253 -        err = -EINVAL;
  18.254 -        goto exit;
  18.255 -    }
  18.256 -    val = p->state->val;
  18.257 -    p->state->val = ONONE;
  18.258 -    err = Parser_pop(p);
  18.259 -    if(err) goto exit;
  18.260 -    if(p->state){
  18.261 -        err = cons_push(&p->state->val, val);
  18.262 -    } else {
  18.263 -        val = nrev(val);
  18.264 -        p->val = val;
  18.265 -    }
  18.266 -  exit:
  18.267 -    if(err){
  18.268 -        objfree(val);
  18.269 -    }
  18.270 -    return err;
  18.271 -}
  18.272 -
  18.273 -/** Determine if a character is a separator.
  18.274 - *
  18.275 - * @param p parser
  18.276 - * @param c character to test
  18.277 - * @return 1 if a separator, 0 otherwise
  18.278 - */
  18.279 -static int is_separator(Parser *p, char c){
  18.280 -    return in_sep_class(c);
  18.281 -}
  18.282 -
  18.283 -/** Return the current token.
  18.284 - * The return value points at the internal buffer, so
  18.285 - * it must not be modified (or freed). Use copy_token() if you need a copy.
  18.286 - *
  18.287 - * @param p parser
  18.288 - * @return token
  18.289 - */
  18.290 -char *peek_token(Parser *p){
  18.291 -    return p->buf;
  18.292 -}
  18.293 -
  18.294 -/** Return a copy of the current token.
  18.295 - * The returned value should be freed when finished with.
  18.296 - *
  18.297 - * @param p parser
  18.298 - * @return copy of token
  18.299 - */
  18.300 -char *copy_token(Parser *p){
  18.301 -    return strdup(peek_token(p));
  18.302 -}
  18.303 -
  18.304 -static int do_intern(Parser *p){
  18.305 -    int err = 0;
  18.306 -    Sxpr obj = intern(peek_token(p));
  18.307 -    if(NOMEMP(obj)){
  18.308 -        err = -ENOMEM;
  18.309 -    } else {
  18.310 -        p->state->val = obj;
  18.311 -    }
  18.312 -    return err;
  18.313 -}
  18.314 -
  18.315 -static int do_string(Parser *p){
  18.316 -    int err = 0;
  18.317 -    Sxpr obj;
  18.318 -    obj = string_new(peek_token(p));
  18.319 -    if(NOMEMP(obj)){
  18.320 -        err = -ENOMEM;
  18.321 -    } else {
  18.322 -        p->state->val = obj;
  18.323 -    }
  18.324 -    return err;
  18.325 -}
  18.326 -
  18.327 -void newtoken(Parser *p){
  18.328 -    memset(p->buf, 0, p->buf_n);
  18.329 -    p->buf_i = 0;
  18.330 -    p->tok_begin_line = p->line_no;
  18.331 -    p->tok_begin_char = p->char_no;
  18.332 -}
  18.333 -
  18.334 -int get_escape(char c, char *d){
  18.335 -    int err = 0;
  18.336 -    switch(c){
  18.337 -    case 'a':            *d = '\a'; break;
  18.338 -    case 'b':            *d = '\b'; break;
  18.339 -    case 'f':            *d = '\f'; break;
  18.340 -    case 'n':            *d = '\n'; break;
  18.341 -    case 'r':            *d = '\r'; break;
  18.342 -    case 't':            *d = '\t'; break;
  18.343 -    case 'v':            *d = '\v'; break;
  18.344 -    case c_escape:       *d = c_escape; break;
  18.345 -    case c_single_quote: *d = c_single_quote; break;
  18.346 -    case c_double_quote: *d = c_double_quote; break;
  18.347 -    default:
  18.348 -        err = -EINVAL;
  18.349 -    }
  18.350 -    return err;
  18.351 -}
  18.352 -
  18.353 -
  18.354 -int begin_start(Parser *p, char c){
  18.355 -    return Parser_push(p, state_start);
  18.356 -}
  18.357 -
  18.358 -int state_start(Parser *p, char c){
  18.359 -    int err = 0;
  18.360 -    if(at_eof(p)){
  18.361 -        err = end_start(p);
  18.362 -    } else if(in_space_class(c)){
  18.363 -        //skip
  18.364 -    } else if(in_comment_class(c)){
  18.365 -        begin_comment(p, c);
  18.366 -    } else if(c == c_list_open){
  18.367 -        begin_list(p, c);
  18.368 -    } else if(c == c_list_close){
  18.369 -        parse_error(p);
  18.370 -        err = -EINVAL;
  18.371 -    } else if(in_string_quote_class(c)){
  18.372 -        begin_string(p, c);
  18.373 -    } else if(in_printable_class(c)){
  18.374 -        begin_atom(p, c);
  18.375 -    } else if(c == 0x04){
  18.376 -        //ctrl-D, EOT: end-of-text.
  18.377 -        Parser_input_eof(p);
  18.378 -    } else {
  18.379 -        parse_error(p);
  18.380 -        err = -EINVAL;
  18.381 -    }
  18.382 -    return err;
  18.383 -}
  18.384 -
  18.385 -int end_start(Parser *p){
  18.386 -    int err = 0;
  18.387 -    err = Parser_return(p);
  18.388 -    return err;
  18.389 -}
  18.390 -
  18.391 -int begin_comment(Parser *p, char c){
  18.392 -    int err = 0;
  18.393 -    err = Parser_push(p, state_comment);
  18.394 -    if(err) goto exit;
  18.395 -    err = inputchar(p, c);
  18.396 -  exit:
  18.397 -    return err;
  18.398 -}
  18.399 -
  18.400 -int state_comment(Parser *p, char c){
  18.401 -    int err = 0;
  18.402 -    if(c == '\n' || at_eof(p)){
  18.403 -        err = end_comment(p);
  18.404 -    } else {
  18.405 -        err = inputchar(p, c);
  18.406 -    }
  18.407 -    return err;
  18.408 -}
  18.409 -
  18.410 -int end_comment(Parser *p){
  18.411 -    return Parser_pop(p);
  18.412 -}
  18.413 -
  18.414 -int begin_string(Parser *p, char c){
  18.415 -    int err = 0;
  18.416 -    err = Parser_push(p, state_string);
  18.417 -    if(err) goto exit;
  18.418 -    newtoken(p);
  18.419 -    p->state->delim = c;
  18.420 -  exit:
  18.421 -    return err;
  18.422 -}
  18.423 -
  18.424 -int state_string(Parser *p, char c){
  18.425 -    int err = 0;
  18.426 -    if(at_eof(p)){
  18.427 -        parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF);
  18.428 -        err = -EINVAL;
  18.429 -    } else if(c == p->state->delim){
  18.430 -        err = end_string(p);
  18.431 -    } else if(c == '\\'){
  18.432 -        err = Parser_push(p, state_escape);
  18.433 -    } else {
  18.434 -        err = savechar(p, c);
  18.435 -    }
  18.436 -    return err;
  18.437 -}
  18.438 -
  18.439 -int end_string(Parser *p){
  18.440 -    int err = 0;
  18.441 -    err = do_string(p);
  18.442 -    if(err) goto exit;
  18.443 -    err = Parser_return(p);
  18.444 -  exit:
  18.445 -    return err;
  18.446 -}
  18.447 -
  18.448 -int state_escape(Parser *p, char c){
  18.449 -    int err = 0;
  18.450 -    char d;
  18.451 -    if(at_eof(p)){
  18.452 -        parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF);
  18.453 -        err = -EINVAL;
  18.454 -        goto exit;
  18.455 -    }
  18.456 -    if(get_escape(c, &d) == 0){
  18.457 -        err = savechar(p, d);
  18.458 -        if(err) goto exit;
  18.459 -        err = Parser_pop(p);
  18.460 -    } else if(c == 'x'){
  18.461 -        p->state->fn = state_hex;
  18.462 -        p->state->ival = 0;
  18.463 -        p->state->count = 0;
  18.464 -    } else {
  18.465 -        p->state->fn = state_octal;
  18.466 -        p->state->ival = 0;
  18.467 -        p->state->count = 0;
  18.468 -        err = Parser_input_char(p, c);
  18.469 -    }
  18.470 -  exit:
  18.471 -    return err;
  18.472 -}
  18.473 -
  18.474 -int octaldone(Parser *p){
  18.475 -    int err = 0;
  18.476 -    char d = (char)(p->state->ival & 0xff);
  18.477 -    err = Parser_pop(p);
  18.478 -    if(err) goto exit;
  18.479 -    err = Parser_input_char(p, d);
  18.480 -  exit:
  18.481 -    return err;
  18.482 -}
  18.483 -
  18.484 -int octaldigit(Parser *p, char c){
  18.485 -    int err = 0;
  18.486 -    p->state->ival *= 8;
  18.487 -    p->state->ival += c - '0'; 
  18.488 -    p->state->count++;
  18.489 -    if(err) goto exit;
  18.490 -    if(p->state->ival < 0 || p->state->ival > 0xff){
  18.491 -        parse_error(p);
  18.492 -        err = -EINVAL;
  18.493 -        goto exit;
  18.494 -    }
  18.495 -    if(p->state->count == 3){
  18.496 -        err = octaldone(p);
  18.497 -    }
  18.498 -  exit:
  18.499 -    return err;
  18.500 -}
  18.501 -
  18.502 -int state_octal(Parser *p, char c){
  18.503 -    int err = 0;
  18.504 -    if(at_eof(p)){
  18.505 -        parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF);
  18.506 -        err = -EINVAL;
  18.507 -        goto exit;
  18.508 -    } else if('0' <= c && c <= '7'){
  18.509 -        err = octaldigit(p, c);
  18.510 -    } else {
  18.511 -        err = octaldone(p);
  18.512 -        if(err) goto exit;
  18.513 -        Parser_input_char(p, c);
  18.514 -    }
  18.515 -  exit:
  18.516 -    return err;
  18.517 -}
  18.518 -
  18.519 -int hexdone(Parser *p){
  18.520 -    int err = 0;
  18.521 -    char d = (char)(p->state->ival & 0xff);
  18.522 -    err = Parser_pop(p);
  18.523 -    if(err) goto exit;
  18.524 -    err = Parser_input_char(p, d);
  18.525 -  exit:
  18.526 -    return err;
  18.527 -}
  18.528 -    
  18.529 -int hexdigit(Parser *p, char c, char d){
  18.530 -    int err = 0;
  18.531 -    p->state->ival *= 16;
  18.532 -    p->state->ival += c - d; 
  18.533 -    p->state->count++;
  18.534 -    if(err) goto exit;
  18.535 -    if(p->state->ival < 0 || p->state->ival > 0xff){
  18.536 -        parse_error(p);
  18.537 -        err = -EINVAL;
  18.538 -        goto exit;
  18.539 -    }
  18.540 -    if(p->state->count == 2){
  18.541 -        err = hexdone(p);
  18.542 -    }
  18.543 -  exit:
  18.544 -    return err;
  18.545 -}
  18.546 -    
  18.547 -int state_hex(Parser *p, char c){
  18.548 -    int err = 0;
  18.549 -    if(at_eof(p)){
  18.550 -        parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF);
  18.551 -        err = -EINVAL;
  18.552 -        goto exit;
  18.553 -    } else if('0' <= c && c <= '9'){
  18.554 -        err = hexdigit(p, c, '0');
  18.555 -    } else if('A' <= c && c <= 'F'){
  18.556 -        err = hexdigit(p, c, 'A');
  18.557 -    } else if('a' <= c && c <= 'f'){
  18.558 -        err = hexdigit(p, c, 'a');
  18.559 -    } else if(p->state->count){
  18.560 -        err =hexdone(p);
  18.561 -        if(err) goto exit;
  18.562 -        Parser_input_char(p, c);
  18.563 -    }
  18.564 -  exit:
  18.565 -    return err;
  18.566 -}
  18.567 -
  18.568 -int begin_atom(Parser *p, char c){
  18.569 -    int err = 0;
  18.570 -    err = Parser_push(p, state_atom);
  18.571 -    if(err) goto exit;
  18.572 -    newtoken(p);
  18.573 -    err = savechar(p, c);
  18.574 -  exit:
  18.575 -    return err;
  18.576 -}
  18.577 -
  18.578 -int state_atom(Parser *p, char c){
  18.579 -    int err = 0;
  18.580 -    if(at_eof(p)){
  18.581 -        err = end_atom(p);
  18.582 -    } else if(is_separator(p, c) ||
  18.583 -              in_space_class(c) ||
  18.584 -              in_comment_class(c)){
  18.585 -        err = end_atom(p);
  18.586 -        if(err) goto exit;
  18.587 -        err = Parser_input_char(p, c);
  18.588 -    } else {
  18.589 -        err = savechar(p, c);
  18.590 -    }
  18.591 -  exit:
  18.592 -    return err;
  18.593 -}
  18.594 -
  18.595 -int end_atom(Parser *p){
  18.596 -    int err = 0;
  18.597 -    err = do_intern(p);
  18.598 -    if(err) goto exit;
  18.599 -    err = Parser_return(p);
  18.600 -  exit:
  18.601 -    return err;
  18.602 -}
  18.603 -
  18.604 -int state_list(Parser *p, char c){
  18.605 -    int err = 0;
  18.606 -    if(at_eof(p)){
  18.607 -        parse_error_id(p, PARSE_ERR_UNEXPECTED_EOF);
  18.608 -        err = -EINVAL;
  18.609 -    } else if(c == c_list_close){
  18.610 -        p->state->val = nrev(p->state->val);
  18.611 -        err = end_list(p);
  18.612 -    } else {
  18.613 -        err = state_start(p, c);
  18.614 -    }
  18.615 -    return err;
  18.616 -    
  18.617 -}
  18.618 -
  18.619 -int begin_list(Parser *p, char c){
  18.620 -    return Parser_push(p, state_list);
  18.621 -}
  18.622 -
  18.623 -int end_list(Parser *p){
  18.624 -    return Parser_return(p);
  18.625 -}
  18.626 -
  18.627 -/** Reset the fields of a parser to initial values.
  18.628 - *
  18.629 - * @param z parser
  18.630 - */
  18.631 -static void reset(Parser *z){
  18.632 -  IOStream *error_out = z->error_out;
  18.633 -  int flags = z->flags;
  18.634 -  zero(z, sizeof(Parser));
  18.635 -  z->buf_n = sizeof(z->buf) - 1;
  18.636 -  z->buf_i = 0;
  18.637 -  z->line_no = 1;
  18.638 -  z->char_no = 0;
  18.639 -  z->error_out = error_out;
  18.640 -  z->flags = flags;
  18.641 -}
  18.642 -
  18.643 -/** Set the parser error stream.
  18.644 - * Parse errors are reported on the the error stream if it is non-null.
  18.645 - * 
  18.646 - * @param z parser
  18.647 - * @param error_out error stream
  18.648 - */
  18.649 -void set_error_stream(Parser *z, IOStream *error_out){
  18.650 -  if(z){
  18.651 -    z->error_out = error_out;
  18.652 -  }
  18.653 -}
  18.654 -
  18.655 -/** Get the parser error message for an error code.
  18.656 - *
  18.657 - * @param id error code
  18.658 - * @return error message (empty string if the code is unknown)
  18.659 - */
  18.660 -static char *get_message(ParseErrorId id){
  18.661 -  int i;
  18.662 -  for(i=0; i<catalog_n; i++){
  18.663 -    if(id == catalog[i].id){
  18.664 -      return catalog[i].message;
  18.665 -    }
  18.666 -  }
  18.667 -  return "";
  18.668 -}
  18.669 -
  18.670 -/** Get the line number.
  18.671 - *
  18.672 - * @param in parser
  18.673 - */
  18.674 -int get_line(Parser *in){
  18.675 -  return in->line_no;
  18.676 -}
  18.677 -
  18.678 -/** Get the column number.
  18.679 - *
  18.680 - * @param in parser
  18.681 - */
  18.682 -int get_column(Parser *in){
  18.683 -  return in->char_no;
  18.684 -}
  18.685 -
  18.686 -/** Get the line number the current token started on.
  18.687 - *
  18.688 - * @param in parser
  18.689 - */
  18.690 -int get_tok_line(Parser *in){
  18.691 -  return in->tok_begin_line;
  18.692 -}
  18.693 -
  18.694 -/** Get the column number the current token started on.
  18.695 - *
  18.696 - * @param in parser
  18.697 - */
  18.698 -int get_tok_column(Parser *in){
  18.699 -  return in->tok_begin_char;
  18.700 -}
  18.701 -
  18.702 -/** Report a parse error.
  18.703 - * Does nothing if the error stream is null or there is no error.
  18.704 - *
  18.705 - * @param in parser
  18.706 - */
  18.707 -static void report_error(Parser *in){
  18.708 -  if(in->error_out && in->err){
  18.709 -    char *msg = get_message(in->err);
  18.710 -    char *tok = peek_token(in);
  18.711 -    IOStream_print(in->error_out, PARSE_ERR_FMT,
  18.712 -		   get_tok_line(in), get_tok_column(in), msg);
  18.713 -    if(tok && tok[0]){
  18.714 -        IOStream_print(in->error_out, " '%s'", tok);
  18.715 -    }
  18.716 -    IOStream_print(in->error_out, "\n");
  18.717 -  }
  18.718 -}
  18.719 -
  18.720 -/** Get the error message for the current parse error code.
  18.721 - * Does nothing if there is no error.
  18.722 - *
  18.723 - * @param in parser
  18.724 - * @param buf where to place the message
  18.725 - * @param n maximum number of characters to place in buf
  18.726 - * @return current error code (zero for no error)
  18.727 - */
  18.728 -int parse_error_message(Parser *in, char *buf, int n){
  18.729 -    if(in->err){
  18.730 -        char *msg = get_message(in->err);
  18.731 -        snprintf(buf, n, PARSE_ERR_FMT, get_tok_line(in), get_tok_column(in), msg);
  18.732 -    }
  18.733 -    return in->err;
  18.734 -}
  18.735 -
  18.736 -/** Flag an unspecified parse error. All subsequent reads will fail.
  18.737 - *
  18.738 - * @param in parser
  18.739 - */
  18.740 -void parse_error(Parser *in){
  18.741 -    parse_error_id(in, PARSE_ERR_INVALID_SYNTAX);
  18.742 -}
  18.743 -
  18.744 -/** Flag a parse error. All subsequent reads will fail.
  18.745 - * Does not change the parser error code if it is already set.
  18.746 - *
  18.747 - * @param in parser
  18.748 - * @param id error code
  18.749 - */
  18.750 -void parse_error_id(Parser *in, ParseErrorId id){
  18.751 -    if(!in->err){
  18.752 -        in->err = id;
  18.753 -        report_error(in);
  18.754 -    }
  18.755 -}
  18.756 -
  18.757 -/** Test if the parser's error flag is set.
  18.758 - *
  18.759 - * @param in parser
  18.760 - * @return 1 if set, 0 otherwise
  18.761 - */
  18.762 -int has_error(Parser *in){
  18.763 -    return (in->err > 0);
  18.764 -}
  18.765 -
  18.766 -/** Test if the parser is at end of input.
  18.767 - *
  18.768 - * @param in parser
  18.769 - * @return 1 if at EOF, 0 otherwise
  18.770 - */
  18.771 -int at_eof(Parser *p){
  18.772 -    return p->eof;
  18.773 -}
  18.774 -
  18.775 -#ifdef SXPR_PARSER_MAIN
  18.776 -/* Stuff for standalone testing. */
  18.777 -
  18.778 -#include "file_stream.h"
  18.779 -#include "string_stream.h"
  18.780 -
  18.781 -int stringof(Sxpr exp, char **s){
  18.782 -    int err = 0;
  18.783 -    if(ATOMP(exp)){
  18.784 -        *s = atom_name(exp);
  18.785 -    } else if(STRINGP(exp)){
  18.786 -        *s = string_string(exp);
  18.787 -    } else {
  18.788 -        err = -EINVAL;
  18.789 -        *s = NULL;
  18.790 -    }
  18.791 -    return err;
  18.792 -}
  18.793 -
  18.794 -int child_string(Sxpr exp, Sxpr key, char **s){
  18.795 -    int err = 0;
  18.796 -    Sxpr val = sxpr_child_value(exp, key, ONONE);
  18.797 -    err = stringof(val, s);
  18.798 -    return err;
  18.799 -}
  18.800 -
  18.801 -int intof(Sxpr exp, int *v){
  18.802 -    int err = 0;
  18.803 -    char *s;
  18.804 -    unsigned long l;
  18.805 -    if(INTP(exp)){
  18.806 -        *v = OBJ_INT(exp);
  18.807 -    } else {
  18.808 -        err = stringof(exp, &s);
  18.809 -        if(err) goto exit;
  18.810 -        err = convert_atoul(s, &l);
  18.811 -        *v = (int)l;
  18.812 -    }
  18.813 - exit:
  18.814 -    return err;
  18.815 -}
  18.816 -
  18.817 -int child_int(Sxpr exp, Sxpr key, int *v){
  18.818 -    int err = 0;
  18.819 -    Sxpr val = sxpr_child_value(exp, key, ONONE);
  18.820 -    err = intof(val, v);
  18.821 -    return err;
  18.822 -}
  18.823 -
  18.824 -int eval_vnet(Sxpr exp){
  18.825 -    int err = 0;
  18.826 -    Sxpr oid = intern("id");
  18.827 -    int id;
  18.828 -    err = child_int(exp, oid, &id);
  18.829 -    if(err) goto exit;
  18.830 -    dprintf("> vnet id=%d\n", id);
  18.831 - exit:
  18.832 -    dprintf("< err=%d\n", err);
  18.833 -    return err;
  18.834 -}
  18.835 -
  18.836 -int eval_connect(Sxpr exp){
  18.837 -    int err = 0;
  18.838 -    Sxpr ovif = intern("vif");
  18.839 -    Sxpr ovnet = intern("vnet");
  18.840 -    char *vif;
  18.841 -    int vnet;
  18.842 -
  18.843 -    err = child_string(exp, ovif, &vif);
  18.844 -    if(err) goto exit;
  18.845 -    err = child_int(exp, ovnet, &vnet);
  18.846 -    if(err) goto exit;
  18.847 -    dprintf("> connect vif=%s vnet=%d\n", vif, vnet);
  18.848 - exit:
  18.849 -    dprintf("< err=%d\n", err);
  18.850 -    return err;
  18.851 -}
  18.852 -
  18.853 -int eval(Sxpr exp){
  18.854 -    int err = 0;
  18.855 -    Sxpr oconnect = intern("connect");
  18.856 -    Sxpr ovnet = intern("vnet");
  18.857 -    
  18.858 -    if(sxpr_elementp(exp, ovnet)){
  18.859 -        err = eval_vnet(exp);
  18.860 -    } else if(sxpr_elementp(exp, oconnect)){
  18.861 -        err = eval_connect(exp);
  18.862 -    } else {
  18.863 -        err = -EINVAL;
  18.864 -    }
  18.865 -    return err;
  18.866 -}
  18.867 -
  18.868 -/** Main program for testing.
  18.869 - * Parses input and prints it.
  18.870 - *
  18.871 - * @param argc number of arguments
  18.872 - * @param argv arguments
  18.873 - * @return error code
  18.874 - */
  18.875 -int main(int argc, char *argv[]){
  18.876 -    Parser *pin;
  18.877 -    int err = 0;
  18.878 -    char buf[1024];
  18.879 -    int k;
  18.880 -    Sxpr obj, l, x;
  18.881 -
  18.882 -    pin = Parser_new();
  18.883 -    set_error_stream(pin, iostdout);
  18.884 -    dprintf("> parse...\n");
  18.885 -    while(1){
  18.886 -        k = fread(buf, 1, 1024, stdin);
  18.887 -        err = Parser_input(pin, buf, k);
  18.888 -        dprintf("> Parser_input=%d\n", err);
  18.889 -        if(k <= 0) break;
  18.890 -    }
  18.891 -    obj = pin->val;
  18.892 -    for(l = obj ; CONSP(l); l = CDR(l)){
  18.893 -        x = CAR(l);
  18.894 -        objprint(iostdout, x, 0); printf("\n");
  18.895 -        eval(x);
  18.896 -    }
  18.897 -    dprintf("> err=%d\n", err);
  18.898 -    return 0;
  18.899 -}
  18.900 -#endif
    19.1 --- a/tools/libxutil/sxpr_parser.h	Wed Jun 30 11:07:39 2004 +0000
    19.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.3 @@ -1,125 +0,0 @@
    19.4 -/*
    19.5 - *
    19.6 - * This library is free software; you can redistribute it and/or modify
    19.7 - * it under the terms of the GNU Lesser General Public License as
    19.8 - * published by the Free Software Foundation; either version 2.1 of the
    19.9 - * License, or  (at your option) any later version. This library is 
   19.10 - * distributed in the  hope that it will be useful, but WITHOUT ANY
   19.11 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or
   19.12 - * FITNESS FOR A PARTICULAR PURPOSE.
   19.13 - * See the GNU Lesser General Public License for more details.
   19.14 - *
   19.15 - * You should have received a copy of the GNU Lesser General Public License
   19.16 - * along with this library; if not, write to the Free Software Foundation,
   19.17 - * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   19.18 - */
   19.19 -
   19.20 -#ifndef _XEN_LIB_SXPR_PARSER_H_
   19.21 -#define _XEN_LIB_SXPR_PARSER_H_
   19.22 -
   19.23 -#include "sxpr.h"
   19.24 -#include "iostream.h"
   19.25 -
   19.26 -/** @file
   19.27 - * Sxpr parsing definitions.
   19.28 - */
   19.29 -
   19.30 -/** Size of a parser input buffer.
   19.31 - * Tokens read must fit into this size (including trailing null).
   19.32 - */
   19.33 -#define PARSER_BUF_SIZE 1024
   19.34 -
   19.35 -struct Parser;
   19.36 -typedef int ParserStateFn(struct Parser *, char c);
   19.37 -
   19.38 -typedef struct ParserState {
   19.39 -    struct ParserState *parent;
   19.40 -    Sxpr val;
   19.41 -    int ival;
   19.42 -    int count;
   19.43 -    char delim;
   19.44 -    ParserStateFn *fn;
   19.45 -} ParserState;
   19.46 -
   19.47 -/** Structure representing an input source for the parser.
   19.48 - * Can read from any IOStream implementation.
   19.49 - */
   19.50 -typedef struct Parser {
   19.51 -    Sxpr val;
   19.52 -    /** Error reporting stream (null for no reports). */
   19.53 -    IOStream *error_out;
   19.54 -    int eof;
   19.55 -    /** Error flag. Non-zero if there has been a read error. */
   19.56 -    int err;
   19.57 -    /** Line number on input (from 1). */
   19.58 -    int line_no;
   19.59 -    /** Column number of input (reset on new line). */
   19.60 -    int char_no;
   19.61 -    /** Lookahead character. */
   19.62 -    char c;
   19.63 -    /** Buffer for reading tokens. */
   19.64 -    char buf[PARSER_BUF_SIZE];
   19.65 -    /** Size of token buffer. */
   19.66 -    int buf_n;
   19.67 -    int buf_i;
   19.68 -    /** Line the last token started on. */
   19.69 -    int tok_begin_line;
   19.70 -    /** Character number the last token started on. */
   19.71 -    int tok_begin_char;
   19.72 -    /** Parsing flags. */
   19.73 -    int flags;
   19.74 -    ParserState *state;
   19.75 -} Parser;
   19.76 -
   19.77 -/** Parser error codes. */
   19.78 -typedef enum {
   19.79 -    PARSE_ERR_NONE=0,
   19.80 -    PARSE_ERR_UNSPECIFIED,
   19.81 -    PARSE_ERR_NOMEM,
   19.82 -    PARSE_ERR_UNEXPECTED_EOF,
   19.83 -    PARSE_ERR_TOKEN_TOO_LONG,
   19.84 -    PARSE_ERR_INVALID_SYNTAX,
   19.85 -    PARSE_ERR_INVALID_ESCAPE,
   19.86 -} ParseErrorId;
   19.87 -
   19.88 -
   19.89 -/** Parser flags. */
   19.90 -//enum {
   19.91 -//};
   19.92 -
   19.93 -/** Raise some parser flags.
   19.94 - *
   19.95 - * @param in parser
   19.96 - * @param flags flags mask
   19.97 - */
   19.98 -inline static void parser_flags_raise(Parser *in, int flags){
   19.99 -    in->flags |= flags;
  19.100 -}
  19.101 -
  19.102 -/** Lower some parser flags.
  19.103 - *
  19.104 - * @param in parser
  19.105 - * @param flags flags mask
  19.106 - */
  19.107 -inline static void parser_flags_lower(Parser *in, int flags){
  19.108 -    in->flags &= ~flags;
  19.109 -}
  19.110 -
  19.111 -/** Clear all parser flags.
  19.112 - *
  19.113 - * @param in parser
  19.114 - */
  19.115 -inline static void parser_flags_clear(Parser *in){
  19.116 -    in->flags = 0;
  19.117 -}
  19.118 -
  19.119 -extern void Parser_free(Parser *z);
  19.120 -extern Parser * Parser_new(void);
  19.121 -extern int Parser_input(Parser *p, char *buf, int buf_n);
  19.122 -extern int Parser_input_eof(Parser *p);
  19.123 -
  19.124 -extern int parse_error_message(Parser *in, char *buf, int n);
  19.125 -extern int has_error(Parser *in);
  19.126 -extern int at_eof(Parser *in);
  19.127 -
  19.128 -#endif /* ! _XEN_LIB_SXPR_PARSER_H_ */
    20.1 --- a/tools/libxutil/xdr.c	Wed Jun 30 11:07:39 2004 +0000
    20.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.3 @@ -1,246 +0,0 @@
    20.4 -/* $Id: xdr.c,v 1.3 2003/09/29 13:40:00 mjw Exp $ */
    20.5 -#include "xdr.h"
    20.6 -#include <errno.h>
    20.7 -/** @file
    20.8 - * XDR packer/unpacker for elements.
    20.9 - *
   20.10 - * string -> [T_STRING] [len:u32] <len bytes>
   20.11 - * atom   -> [T_ATOM]   [len:u32] <len bytes>
   20.12 - * uint   -> [T_UINT]   [value]
   20.13 - * cons   -> [T_CONS]   <car> <cdr>
   20.14 - * null   -> [T_NULL]
   20.15 - * none   -> [T_NONE]
   20.16 - * bool   -> [T_BOOL]   { 0:u8 | 1:u8 }
   20.17 - *
   20.18 - * types packed as u16.
   20.19 - *
   20.20 - * So (a b c) -> [T_CONS] a [T_CONS] b [T_CONS] c [T_NULL]
   20.21 - *    ()      -> [T_NULL]
   20.22 - */
   20.23 -
   20.24 -int pack_bool(IOStream *io, int x){
   20.25 -    int err=0;
   20.26 -    err = IOStream_print(io, "%c", 0xff & x);
   20.27 -    if(err > 0) err = 0;
   20.28 -    return err;
   20.29 -}
   20.30 -
   20.31 -int unpack_bool(IOStream *io, int *x){
   20.32 -    int err = 0;
   20.33 -    int c;
   20.34 -    c = IOStream_getc(io);
   20.35 -    *x = (c < 0 ? 0 : c);
   20.36 -    err = IOStream_error(io);
   20.37 -    if(c < 0 && !err) err = -EIO;
   20.38 -    return err;
   20.39 -}
   20.40 -
   20.41 -int pack_ushort(IOStream *io, unsigned short x){
   20.42 -    int err=0;
   20.43 -    err = IOStream_print(io, "%c%c",
   20.44 -                         0xff & (x >>  8),
   20.45 -                         0xff & (x      ));
   20.46 -    if(err > 0) err = 0;
   20.47 -    return err;
   20.48 -}
   20.49 -
   20.50 -int unpack_ushort(IOStream *io, unsigned short *x){
   20.51 -    int err = 0;
   20.52 -    int i, c = 0;
   20.53 -    *x = 0;
   20.54 -    for(i = 0; i< 2; i++){
   20.55 -        c = IOStream_getc(io);
   20.56 -        if(c < 0) break;
   20.57 -        *x <<= 8;
   20.58 -        *x |= (0xff & c);
   20.59 -    }
   20.60 -    err = IOStream_error(io);
   20.61 -    if(c < 0 && !err) err = -EIO;
   20.62 -    return err;
   20.63 -}
   20.64 -
   20.65 -int pack_uint(IOStream *io, unsigned int x){
   20.66 -    int err=0;
   20.67 -    err = IOStream_print(io, "%c%c%c%c",
   20.68 -                         0xff & (x >> 24),
   20.69 -                         0xff & (x >> 16),
   20.70 -                         0xff & (x >>  8),
   20.71 -                         0xff & (x      ));
   20.72 -    if(err > 0) err = 0;
   20.73 -    return err;
   20.74 -}
   20.75 -
   20.76 -int unpack_uint(IOStream *io, unsigned int *x){
   20.77 -    int err = 0;
   20.78 -    int i, c = 0;
   20.79 -    *x = 0;
   20.80 -    for(i = 0; i< 4; i++){
   20.81 -        c = IOStream_getc(io);
   20.82 -        if(c < 0) break;
   20.83 -        *x <<= 8;
   20.84 -        *x |= (0xff & c);
   20.85 -    }
   20.86 -    err = IOStream_error(io);
   20.87 -    if(c < 0 && !err) err = -EIO;
   20.88 -    return err;
   20.89 -}
   20.90 -
   20.91 -int pack_string(IOStream *io, Sxpr x){
   20.92 -    int err = 0;
   20.93 -    int n = string_length(x);
   20.94 -    char *s = string_string(x);
   20.95 -    int i;
   20.96 -    err = pack_uint(io, n);
   20.97 -    if(err) goto exit;
   20.98 -    for(i = 0; i < n; i++){
   20.99 -        err = IOStream_print(io, "%c", s[i]);
  20.100 -        if(err < 0) break;
  20.101 -    }
  20.102 -    if(err > 0) err = 0;
  20.103 -  exit:
  20.104 -    return err;
  20.105 -}
  20.106 -
  20.107 -int unpack_string(IOStream *io, Sxpr *x){
  20.108 -    int err;
  20.109 -    unsigned int n;
  20.110 -    int i, c = 0;
  20.111 -    char *s;
  20.112 -    Sxpr val = ONONE;
  20.113 -    
  20.114 -    err = unpack_uint(io, &n);
  20.115 -    if(err) goto exit;
  20.116 -    val = halloc(n+1, T_STRING);
  20.117 -    if(NOMEMP(val)){
  20.118 -        err = -ENOMEM;
  20.119 -        goto exit;
  20.120 -    }
  20.121 -    s = string_string(val);
  20.122 -    for(i=0; i<n; i++){
  20.123 -        c = IOStream_getc(io);
  20.124 -        if(c < 0) break;
  20.125 -        s[i] = (char)c;
  20.126 -    }
  20.127 -    s[n] = '\0';
  20.128 -  exit:
  20.129 -    err = IOStream_error(io);
  20.130 -    if(c < 0 && !err) err = -EIO;
  20.131 -    if(err){
  20.132 -        objfree(val);
  20.133 -        val = ONONE;
  20.134 -    }
  20.135 -    *x = val;
  20.136 -    return err;
  20.137 -}
  20.138 -
  20.139 -int pack_cons(IOStream *io, Sxpr x){
  20.140 -    int err = 0;
  20.141 -    err = pack_sxpr(io, CAR(x));
  20.142 -    if(err) goto exit;
  20.143 -    err = pack_sxpr(io, CDR(x));
  20.144 -  exit:
  20.145 -    return err;
  20.146 -}
  20.147 -
  20.148 -int unpack_cons(IOStream *io, Sxpr *x){
  20.149 -    int err = 0;
  20.150 -    Sxpr u = ONONE, v = ONONE, val = ONONE;
  20.151 -    err = unpack_sxpr(io, &u);
  20.152 -    if(err) goto exit;
  20.153 -    err = unpack_sxpr(io, &v);
  20.154 -    if(err) goto exit;
  20.155 -    val = cons_new(u, v);
  20.156 -    if(NOMEMP(val)){
  20.157 -        err = -ENOMEM;
  20.158 -    }
  20.159 -  exit:
  20.160 -    if(err){
  20.161 -        objfree(u);
  20.162 -        objfree(v);
  20.163 -        val = ONONE;
  20.164 -    }        
  20.165 -    *x = val;
  20.166 -    return err;
  20.167 -}
  20.168 -
  20.169 -int pack_sxpr(IOStream *io, Sxpr x){
  20.170 -    int err = 0;
  20.171 -    unsigned short type = get_type(x);
  20.172 -    err = pack_ushort(io, type);
  20.173 -    if(err) goto exit;
  20.174 -    switch(type){
  20.175 -    case T_NULL:
  20.176 -        break;
  20.177 -    case T_NONE:
  20.178 -        break;
  20.179 -        break;
  20.180 -    case T_BOOL:
  20.181 -        err = pack_bool(io, get_ul(x));
  20.182 -        break;
  20.183 -    case T_CONS:
  20.184 -        err = pack_cons(io, x);
  20.185 -        break;
  20.186 -    case T_ATOM:
  20.187 -        err = pack_string(io, OBJ_ATOM(x)->name);
  20.188 -        break;
  20.189 -    case T_STRING:
  20.190 -        err = pack_string(io, x);
  20.191 -        break;
  20.192 -    case T_UINT:
  20.193 -        err = pack_uint(io, get_ul(x));
  20.194 -        break;
  20.195 -    default:
  20.196 -        err = -EINVAL;
  20.197 -        IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type);
  20.198 -        break;
  20.199 -    }
  20.200 -  exit:
  20.201 -    return err;
  20.202 -}
  20.203 -
  20.204 -int unpack_sxpr(IOStream *io, Sxpr *x){
  20.205 -    int err = 0;
  20.206 -    unsigned short type;
  20.207 -    unsigned int u;
  20.208 -    Sxpr val = ONONE, y;
  20.209 -
  20.210 -    err = unpack_ushort(io, &type);
  20.211 -    if(err) goto exit;
  20.212 -    switch(type){
  20.213 -    case T_NULL:
  20.214 -        val = ONULL;
  20.215 -        break;
  20.216 -    case T_NONE:
  20.217 -        val = ONONE;
  20.218 -        break;
  20.219 -    case T_CONS:
  20.220 -        err = unpack_cons(io, &val);
  20.221 -        break;
  20.222 -    case T_BOOL:
  20.223 -        err = unpack_bool(io, &u);
  20.224 -        if(err) goto exit;
  20.225 -        val = (u ? OTRUE : OFALSE);
  20.226 -        break;
  20.227 -    case T_ATOM:
  20.228 -        err = unpack_string(io, &y);
  20.229 -        if(err) goto exit;
  20.230 -        val = intern(string_string(y));
  20.231 -        objfree(y);
  20.232 -        break;
  20.233 -    case T_STRING:
  20.234 -        err = unpack_string(io, &val);
  20.235 -        break;
  20.236 -    case T_UINT:
  20.237 -        err = unpack_uint(io, &u);
  20.238 -        if(err) goto exit;
  20.239 -        val = OBJI(type, u);
  20.240 -        break;
  20.241 -    default:
  20.242 -        err = -EINVAL;
  20.243 -        IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type);
  20.244 -        break;
  20.245 -    }
  20.246 -  exit:
  20.247 -    *x = (err ? ONONE : val);
  20.248 -    return err;
  20.249 -}
    21.1 --- a/tools/libxutil/xdr.h	Wed Jun 30 11:07:39 2004 +0000
    21.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.3 @@ -1,14 +0,0 @@
    21.4 -/* $Id: xdr.h,v 1.2 2003/09/29 13:40:00 mjw Exp $ */
    21.5 -#ifndef _SP_XDR_H_
    21.6 -#define _SP_XDR_H_
    21.7 -#include "iostream.h"
    21.8 -#include "sxpr.h"
    21.9 -int pack_uint(IOStream *out, unsigned int x);
   21.10 -int unpack_uint(IOStream *in, unsigned int *x);
   21.11 -int pack_string(IOStream *out, Sxpr x);
   21.12 -int unpack_string(IOStream *in, Sxpr *x);
   21.13 -int pack_cons(IOStream *out, Sxpr x);
   21.14 -int unpack_cons(IOStream *in, Sxpr *x);
   21.15 -int pack_sxpr(IOStream *out, Sxpr x);
   21.16 -int unpack_sxpr(IOStream *in, Sxpr *x);
   21.17 -#endif /* _SP_XDR_H_ */
    22.1 --- a/tools/python/xen/xend/XendDomainInfo.py	Wed Jun 30 11:07:39 2004 +0000
    22.2 +++ b/tools/python/xen/xend/XendDomainInfo.py	Wed Jun 30 14:32:52 2004 +0000
    22.3 @@ -283,7 +283,7 @@ def vm_restore(src, progress=0):
    22.4      d = restorefn(state_file=src, progress=progress)
    22.5      dom = int(d['dom'])
    22.6      if dom < 0:
    22.7 -        raise VMError('restore failed')
    22.8 +        raise VmError('restore failed')
    22.9      vmconfig = sxp.from_string(d['vmconfig'])
   22.10      vm.config = sxp.child_value(vmconfig, 'config')
   22.11      deferred = vm.dom_configure(dom)
   22.12 @@ -611,7 +611,7 @@ class XendDomainInfo:
   22.13              if not os.path.isfile(kernel):
   22.14                  raise VmError('Kernel image does not exist: %s' % kernel)
   22.15              if ramdisk and not os.path.isfile(ramdisk):
   22.16 -                raise VMError('Kernel ramdisk does not exist: %s' % ramdisk)
   22.17 +                raise VmError('Kernel ramdisk does not exist: %s' % ramdisk)
   22.18          print 'create-domain> init_domain...'
   22.19          self.init_domain()
   22.20          print 'create_domain>', 'dom=', self.dom
   22.21 @@ -691,7 +691,7 @@ class XendDomainInfo:
   22.22          """
   22.23          d = dom_get(dom)
   22.24          if not d:
   22.25 -            raise VMError("Domain not found: %d" % dom)
   22.26 +            raise VmError("Domain not found: %d" % dom)
   22.27          try:
   22.28              self.setdom(dom)
   22.29              self.name = d['name']
   22.30 @@ -807,10 +807,10 @@ def vm_dev_vbd(vm, val, index):
   22.31      vdev = index
   22.32      uname = sxp.child_value(val, 'uname')
   22.33      if not uname:
   22.34 -        raise VMError('vbd: Missing uname')
   22.35 +        raise VmError('vbd: Missing uname')
   22.36      dev = sxp.child_value(val, 'dev')
   22.37      if not dev:
   22.38 -        raise VMError('vbd: Missing dev')
   22.39 +        raise VmError('vbd: Missing dev')
   22.40      mode = sxp.child_value(val, 'mode', 'r')
   22.41      defer = make_disk(vm.dom, uname, dev, mode, vm.recreate)
   22.42      def fn(vbd):
   22.43 @@ -833,24 +833,24 @@ def parse_pci(val):
   22.44  def vm_dev_pci(vm, val, index):
   22.45      bus = sxp.child_value(val, 'bus')
   22.46      if not bus:
   22.47 -        raise VMError('pci: Missing bus')
   22.48 +        raise VmError('pci: Missing bus')
   22.49      dev = sxp.child_value(val, 'dev')
   22.50      if not dev:
   22.51 -        raise VMError('pci: Missing dev')
   22.52 +        raise VmError('pci: Missing dev')
   22.53      func = sxp.child_value(val, 'func')
   22.54      if not func:
   22.55 -        raise VMError('pci: Missing func')
   22.56 +        raise VmError('pci: Missing func')
   22.57      try:
   22.58          bus = parse_pci(bus)
   22.59          dev = parse_pci(dev)
   22.60          func = parse_pci(func)
   22.61      except:
   22.62 -        raise VMError('pci: invalid parameter')
   22.63 +        raise VmError('pci: invalid parameter')
   22.64      rc = xc.physdev_pci_access_modify(dom=vm.dom, bus=bus, dev=dev,
   22.65                                        func=func, enable=1)
   22.66      if rc < 0:
   22.67          #todo non-fatal
   22.68 -        raise VMError('pci: Failed to configure device: bus=%s dev=%s func=%s' %
   22.69 +        raise VmError('pci: Failed to configure device: bus=%s dev=%s func=%s' %
   22.70                        (bus, dev, func))
   22.71      return rc
   22.72