ia64/xen-unstable

changeset 4904:940a73d4b2a6

bitkeeper revision 1.1402 (4284c140AGr7UEnDR8q3JG62gMR37g)

Merge iostream fixes.
Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Fri May 13 15:01:20 2005 +0000 (2005-05-13)
parents bb2558cbc4f8
children 7f7c68433c4c
files .rootkeys tools/libxutil/file_stream.c tools/libxutil/gzip_stream.c tools/libxutil/iostream.h tools/libxutil/kernel_stream.c tools/libxutil/string_stream.c tools/xfrd/http.h tools/xfrd/xdr.c tools/xfrd/xdr.h
line diff
     1.1 --- a/.rootkeys	Fri May 13 14:37:25 2005 +0000
     1.2 +++ b/.rootkeys	Fri May 13 15:01:20 2005 +0000
     1.3 @@ -1085,15 +1085,12 @@ 40e9808epTR4zWrYjGUnaaynK20Q5A tools/xfr
     1.4  40e9808eysqT4VNDlJFqsZB2rdg4Qw tools/xfrd/connection.c
     1.5  40e9808eyXfJUi4E0C3WSgrEXqQ1sQ tools/xfrd/connection.h
     1.6  40e9808eULGwffNOE4kBrAfZ9YAVMA tools/xfrd/debug.h
     1.7 -411b5139tfKZfWs1LQHmwDR_wjKoxQ tools/xfrd/http.h
     1.8  40e9808ePADCSKL1YgGCt2TbYPnYkw tools/xfrd/lzi_stream.c
     1.9  40e9808eDNAdpF71o5teYb9DTT-PRw tools/xfrd/lzi_stream.h
    1.10  40e9808eQxi0EzTcPJtosrzxEIjA-Q tools/xfrd/marshal.c
    1.11  40e9808etg13xfRm0Lqd8vY-jHOoTg tools/xfrd/marshal.h
    1.12  40e9808eCsmywryb036TdtRMJHDMmQ tools/xfrd/select.c
    1.13  40e9808e99OcM547cKMTfmCVSoWVAw tools/xfrd/select.h
    1.14 -40e9808eF3NVldqRNS5IHM8gbFAvpw tools/xfrd/xdr.c
    1.15 -40e9808ezXzoRHm7pybXU69NtnjimA tools/xfrd/xdr.h
    1.16  40e9808edpUtf4bJ8IbqClPJj_OvbA tools/xfrd/xen_domain.c
    1.17  40e9808eHviFFIwdUKOA234uIeifjA tools/xfrd/xen_domain.h
    1.18  40e9808eIFeV-MDCNyVTNt5NfMPKeQ tools/xfrd/xfrd.c
     2.1 --- a/tools/libxutil/file_stream.c	Fri May 13 14:37:25 2005 +0000
     2.2 +++ b/tools/libxutil/file_stream.c	Fri May 13 15:01:20 2005 +0000
     2.3 @@ -46,18 +46,21 @@ static const IOMethods file_methods = {
     2.4  static IOStream _iostdin = {
     2.5      methods: &file_methods,
     2.6      data: (void*)1,
     2.7 +    nofree: 1,
     2.8  };
     2.9  
    2.10  /** IOStream for stdout. */
    2.11  static IOStream _iostdout = {
    2.12      methods: &file_methods,
    2.13      data: (void*)2,
    2.14 +    nofree: 1,
    2.15  };
    2.16  
    2.17  /** IOStream for stderr. */
    2.18  static IOStream _iostderr = {
    2.19      methods: &file_methods,
    2.20      data: (void*)3,
    2.21 +    nofree: 1,
    2.22  };
    2.23  
    2.24  /** IOStream for stdin. */
    2.25 @@ -152,10 +155,7 @@ static int file_error(IOStream *s){
    2.26   */
    2.27  static int file_close(IOStream *s){
    2.28      int result = 0;
    2.29 -    if (s->data){
    2.30 -        result = fclose(get_file(s));
    2.31 -        s->data = (void*)0;
    2.32 -    }
    2.33 +    result = fclose(get_file(s));
    2.34      return result;
    2.35  }
    2.36  
    2.37 @@ -164,7 +164,7 @@ static int file_close(IOStream *s){
    2.38   * @param s file stream
    2.39   */
    2.40  static void file_free(IOStream *s){
    2.41 -    file_close(s);
    2.42 +    // Nothing extra to do - close did it all.
    2.43  }
    2.44  
    2.45  /** Create an IOStream for a stream.
    2.46 @@ -175,8 +175,8 @@ static void file_free(IOStream *s){
    2.47  IOStream *file_stream_new(FILE *f){
    2.48      IOStream *io = ALLOCATE(IOStream);
    2.49      if(io){
    2.50 -	io->methods = &file_methods;
    2.51 -	io->data = (void*)f;
    2.52 +        io->methods = &file_methods;
    2.53 +        io->data = (void*)f;
    2.54      }
    2.55      return io;
    2.56  }
    2.57 @@ -191,10 +191,10 @@ IOStream *file_stream_fopen(const char *
    2.58      IOStream *io = 0;
    2.59      FILE *fin = fopen(file, flags);
    2.60      if(fin){
    2.61 -	io = file_stream_new(fin);
    2.62 -	if(!io){
    2.63 -	    fclose(fin);
    2.64 -	}
    2.65 +        io = file_stream_new(fin);
    2.66 +        if(!io){
    2.67 +            fclose(fin);
    2.68 +        }
    2.69      }
    2.70      return io;
    2.71  }
    2.72 @@ -211,8 +211,9 @@ IOStream *file_stream_fdopen(int fd, con
    2.73      FILE *fin = fdopen(fd, flags);
    2.74      if(fin){
    2.75          io = file_stream_new(fin);
    2.76 -        if(!io)
    2.77 +        if(!io){
    2.78              fclose(fin);
    2.79 +        }
    2.80      }
    2.81      return io;
    2.82  }
     3.1 --- a/tools/libxutil/gzip_stream.c	Fri May 13 14:37:25 2005 +0000
     3.2 +++ b/tools/libxutil/gzip_stream.c	Fri May 13 15:01:20 2005 +0000
     3.3 @@ -39,7 +39,7 @@ static int gzip_flush(IOStream *s);
     3.4  
     3.5  /** Methods used by a gzFile* IOStream. */
     3.6  static const IOMethods gzip_methods = {
     3.7 -    read: gzip_read,
     3.8 +    read:  gzip_read,
     3.9      write: gzip_write,
    3.10      error: gzip_error,
    3.11      close: gzip_close,
    3.12 @@ -108,10 +108,7 @@ static int gzip_error(IOStream *s){
    3.13   */
    3.14  static int gzip_close(IOStream *s){
    3.15      int result = 0;
    3.16 -    if (s->data){
    3.17 -        result = gzclose(get_gzfile(s));
    3.18 -        s->data = (void*)0;
    3.19 -    }
    3.20 +    result = gzclose(get_gzfile(s));
    3.21      return result;
    3.22  }
    3.23  
    3.24 @@ -120,7 +117,7 @@ static int gzip_close(IOStream *s){
    3.25   * @param s gzip stream
    3.26   */
    3.27  static void gzip_free(IOStream *s){
    3.28 -    gzip_close(s);
    3.29 +    // Nothing to do - close did it all.
    3.30  }
    3.31  
    3.32  /** Create an IOStream for a gzip stream.
    3.33 @@ -131,8 +128,8 @@ static void gzip_free(IOStream *s){
    3.34  IOStream *gzip_stream_new(gzFile *f){
    3.35      IOStream *io = ALLOCATE(IOStream);
    3.36      if(io){
    3.37 -	io->methods = &gzip_methods;
    3.38 -	io->data = (void*)f;
    3.39 +        io->methods = &gzip_methods;
    3.40 +        io->data = (void*)f;
    3.41      }
    3.42      return io;
    3.43  }
     4.1 --- a/tools/libxutil/iostream.h	Fri May 13 14:37:25 2005 +0000
     4.2 +++ b/tools/libxutil/iostream.h	Fri May 13 15:01:20 2005 +0000
     4.3 @@ -33,7 +33,7 @@
     4.4  
     4.5  #include "allocate.h"
     4.6  
     4.7 -/** End of input return value. */
     4.8 +/** End of input return value (for getc). */
     4.9  #define IOSTREAM_EOF -1
    4.10  
    4.11  /** An input/output abstraction.
    4.12 @@ -82,6 +82,8 @@ struct IOStream {
    4.13      int written;
    4.14      /** Number of bytes read. */
    4.15      int read;
    4.16 +    /** Flag indicating whether not to free when closed. */
    4.17 +    int nofree;
    4.18  };
    4.19  
    4.20  
    4.21 @@ -107,7 +109,7 @@ extern int IOStream_vprint(IOStream *io,
    4.22  static inline int IOStream_read(IOStream *stream, void *buf, size_t n){
    4.23      int result;
    4.24      if(stream->closed){
    4.25 -        result = IOSTREAM_EOF;
    4.26 +        result = -EIO;
    4.27          goto exit;
    4.28      }
    4.29      if(!stream->methods || !stream->methods->read){
    4.30 @@ -132,7 +134,7 @@ static inline int IOStream_read(IOStream
    4.31  static inline int IOStream_write(IOStream *stream, const void *buf, size_t n){
    4.32      int result;
    4.33      if(stream->closed){
    4.34 -        result = IOSTREAM_EOF;
    4.35 +        result = -EIO;
    4.36          goto exit;
    4.37      }
    4.38      if(!stream->methods || !stream->methods->write){
    4.39 @@ -150,15 +152,14 @@ static inline int IOStream_write(IOStrea
    4.40  /** Flush the stream.
    4.41   *
    4.42   * @param stream stream
    4.43 - * @return 0 on success, IOSTREAM_EOF otherwise
    4.44 + * @return 0 on success, negative error code otherwise
    4.45   */
    4.46  static inline int IOStream_flush(IOStream *stream){
    4.47      int result = 0;
    4.48      if(stream->closed){
    4.49 -        result = IOSTREAM_EOF;
    4.50 +        result = -EIO;
    4.51      } else if(stream->methods->flush){
    4.52          result = (stream->methods->flush)(stream);
    4.53 -        if(result < 0) result = IOSTREAM_EOF;
    4.54      }
    4.55      return result;
    4.56  }
    4.57 @@ -179,14 +180,25 @@ static inline int IOStream_error(IOStrea
    4.58  /** Close the stream.
    4.59   *
    4.60   * @param stream to close
    4.61 - * @return 1 for error, 0 otherwise
    4.62 + * @return 0 on success, negative error code otherwise
    4.63   */
    4.64  static inline int IOStream_close(IOStream *stream){
    4.65 -    int err = 1;
    4.66 +    int err = 0;
    4.67 +    if(!stream || stream->closed){
    4.68 +        err = -EIO;
    4.69 +        goto exit;
    4.70 +    }
    4.71      if(stream->methods && stream->methods->close){
    4.72          err = (stream->methods->close)(stream);
    4.73          stream->closed = 1;
    4.74      }
    4.75 +    if(stream->nofree) goto exit;
    4.76 +    if(stream->methods && stream->methods->free){
    4.77 +        (stream->methods->free)(stream);
    4.78 +    }
    4.79 +    *stream = (IOStream){};
    4.80 +    deallocate(stream);
    4.81 +  exit:
    4.82      return err;
    4.83  }
    4.84  
    4.85 @@ -199,22 +211,6 @@ static inline int IOStream_is_closed(IOS
    4.86      return stream->closed;
    4.87  }
    4.88  
    4.89 -/** Free the memory used by the stream.
    4.90 - *
    4.91 - * @param stream to free
    4.92 - */
    4.93 -static inline void IOStream_free(IOStream *stream){
    4.94 -    if(!stream->closed && stream->methods && stream->methods->close){
    4.95 -        (stream->methods->close)(stream);
    4.96 -    }
    4.97 -    if(stream->methods && stream->methods->free){
    4.98 -        (stream->methods->free)(stream);
    4.99 -    }
   4.100 -    *stream = (IOStream){};
   4.101 -    deallocate(stream);
   4.102 -}
   4.103 -
   4.104 -
   4.105  /** Print a character to a stream, like fputc().
   4.106   *
   4.107   * @param stream to print to
     5.1 --- a/tools/libxutil/kernel_stream.c	Fri May 13 14:37:25 2005 +0000
     5.2 +++ b/tools/libxutil/kernel_stream.c	Fri May 13 15:01:20 2005 +0000
     5.3 @@ -57,10 +57,10 @@ static void kernel_stream_unlock(IOStrea
     5.4  
     5.5  /** Methods for a kernel stream. Output only. */
     5.6  static const IOMethods kernel_methods = {
     5.7 -  write:  kernel_write,
     5.8 -  free:   kernel_free,
     5.9 -  lock:   kernel_stream_lock,
    5.10 -  unlock: kernel_stream_unlock,
    5.11 +    write:  kernel_write,
    5.12 +    free:   kernel_free,
    5.13 +    lock:   kernel_stream_lock,
    5.14 +    unlock: kernel_stream_unlock,
    5.15  };
    5.16  
    5.17  /** Shared state for kernel streams.
    5.18 @@ -68,15 +68,16 @@ static const IOMethods kernel_methods = 
    5.19   * shared state and avoid allocating it.
    5.20   */
    5.21  static const KernelData kernel_data = {
    5.22 -  lock: SPIN_LOCK_UNLOCKED,
    5.23 -  flags: 0,
    5.24 -  buf_n: BUF_N,
    5.25 +    lock:  SPIN_LOCK_UNLOCKED,
    5.26 +    flags: 0,
    5.27 +    buf_n: BUF_N,
    5.28  };
    5.29  
    5.30  /** Stream for kernel printk. */
    5.31  static IOStream iokernel = {
    5.32      methods: &kernel_methods,
    5.33      data:    &kernel_data,
    5.34 +    nofree:  1,
    5.35  };
    5.36  
    5.37  /** Stream for kernel printk. */
    5.38 @@ -94,7 +95,7 @@ IOStream *iostderr = &iokernel;
    5.39   * @return kernel stream
    5.40   */
    5.41  IOStream get_stream_kernel(void){
    5.42 -  return iokernel;
    5.43 +    return iokernel;
    5.44  }
    5.45  
    5.46  /** Obtain the lock on the stream state.
    5.47 @@ -102,7 +103,7 @@ IOStream get_stream_kernel(void){
    5.48   * @param kdata stream state
    5.49   */
    5.50  static inline void KernelData_lock(KernelData *kdata){
    5.51 -  spin_lock_irqsave(&kdata->lock, kdata->flags);
    5.52 +    spin_lock_irqsave(&kdata->lock, kdata->flags);
    5.53  }
    5.54  
    5.55  /** Release the lock on the stream state.
    5.56 @@ -110,7 +111,7 @@ static inline void KernelData_lock(Kerne
    5.57   * @param kdata stream state
    5.58   */
    5.59  static inline void KernelData_unlock(KernelData *kdata){
    5.60 -  spin_unlock_irqrestore(&kdata->lock, kdata->flags);
    5.61 +    spin_unlock_irqrestore(&kdata->lock, kdata->flags);
    5.62  }
    5.63  
    5.64  /** Get the stream state.
    5.65 @@ -119,7 +120,7 @@ static inline void KernelData_unlock(Ker
    5.66   * @return stream state
    5.67   */
    5.68  static inline KernelData *get_kernel_data(IOStream *s){
    5.69 -  return (KernelData*)s->data;
    5.70 +    return (KernelData*)s->data;
    5.71  }
    5.72  
    5.73  /** Obtain the lock on the stream state.
    5.74 @@ -146,14 +147,14 @@ void kernel_stream_unlock(IOStream *s){
    5.75   * @return result of the print
    5.76   */
    5.77  static int kernel_write(IOStream *stream, const void *buf, size_t n){
    5.78 -  KernelData *kdata = get_kernel_data(stream);
    5.79 -  int k;
    5.80 -  k = kdata->buf_n - 1;
    5.81 -  if(n < k) k = n;
    5.82 -  memcpy(kdata->buf, buf, k);
    5.83 -  kdata->buf[k] = '\0';
    5.84 -  printk(kdata->buf);
    5.85 -  return k;
    5.86 +    KernelData *kdata = get_kernel_data(stream);
    5.87 +    int k;
    5.88 +    k = kdata->buf_n - 1;
    5.89 +    if(n < k) k = n;
    5.90 +    memcpy(kdata->buf, buf, k);
    5.91 +    kdata->buf[k] = '\0';
    5.92 +    printk(kdata->buf);
    5.93 +    return k;
    5.94  }
    5.95  
    5.96  /** Free a kernel stream.
    5.97 @@ -164,11 +165,11 @@ static int kernel_write(IOStream *stream
    5.98   * @param io stream to free
    5.99   */
   5.100  static void kernel_free(IOStream *io){
   5.101 -  KernelData *kdata;
   5.102 -  if(io == &iokernel) return;
   5.103 -  kdata = get_kernel_data(io);
   5.104 -  memset(kdata, 0, sizeof(*kdata));
   5.105 -  deallocate(kdata);
   5.106 +    KernelData *kdata;
   5.107 +    if(io == &iokernel) return;
   5.108 +    kdata = get_kernel_data(io);
   5.109 +    memset(kdata, 0, sizeof(*kdata));
   5.110 +    deallocate(kdata);
   5.111  }
   5.112  #endif /* __KERNEL__ */
   5.113  
     6.1 --- a/tools/libxutil/string_stream.c	Fri May 13 14:37:25 2005 +0000
     6.2 +++ b/tools/libxutil/string_stream.c	Fri May 13 15:01:20 2005 +0000
     6.3 @@ -28,11 +28,13 @@
     6.4  static int string_error(IOStream *io);
     6.5  static int string_close(IOStream *io);
     6.6  static void string_free(IOStream *io);
     6.7 +static int string_write(IOStream *io, const void *msg, size_t n);
     6.8 +static int string_read(IOStream *io, void *buf, size_t n);
     6.9  
    6.10  /** Methods for a string stream. */
    6.11  static IOMethods string_methods = {
    6.12 -    //print: string_print,
    6.13 -    //getc:  string_getc,
    6.14 +    read:  string_read,
    6.15 +    write: string_write,
    6.16      error: string_error,
    6.17      close: string_close,
    6.18      free:  string_free,
    6.19 @@ -47,6 +49,28 @@ static inline StringData *get_string_dat
    6.20      return (StringData*)io->data;
    6.21  }
    6.22  
    6.23 +static int string_write(IOStream *io, const void *msg, size_t n){
    6.24 +    StringData *data = get_string_data(io);
    6.25 +    int k;
    6.26 +
    6.27 +    k = data->end - data->out;
    6.28 +    if(n > k) n = k;
    6.29 +    memcpy(data->out, msg, n);
    6.30 +    data->out += n;
    6.31 +    return n;
    6.32 +}
    6.33 +
    6.34 +static int string_read(IOStream *io, void *buf, size_t n){
    6.35 +    StringData *data = get_string_data(io);
    6.36 +    int k;
    6.37 +
    6.38 +    k = data->end - data->in;
    6.39 +    if(n > k) n = k;
    6.40 +    memcpy(buf, data->in, k);
    6.41 +    data->in += n;
    6.42 +    return n;
    6.43 +}
    6.44 +
    6.45  /** Test if a string stream has an error.
    6.46   *
    6.47   * @param io string stream
    6.48 @@ -70,7 +94,6 @@ static int string_close(IOStream *io){
    6.49  }
    6.50  
    6.51  /** Free a string stream.
    6.52 - * The stream must have been allocated, not statically created.
    6.53   * The stream state is freed, but the underlying string is not.
    6.54   *
    6.55   * @param io string stream
    6.56 @@ -90,6 +113,9 @@ IOMethods *string_stream_get_methods(voi
    6.57  }
    6.58  
    6.59  /** Initialise a string stream, usually from static data.
    6.60 + * If the stream and StringData should be freed when
    6.61 + * the stream is closed, unset io->nofree.
    6.62 + * The string is not freed on close.
    6.63   *
    6.64   * @param io address of IOStream to fill in
    6.65   * @param data address of StringData to fill in
    6.66 @@ -107,10 +133,12 @@ void string_stream_init(IOStream *io, St
    6.67          memzero(io, sizeof(*io));
    6.68          io->methods = &string_methods;
    6.69          io->data = data;
    6.70 +        io->nofree = 1;
    6.71      }
    6.72  }
    6.73  
    6.74  /** Allocate and initialise a string stream.
    6.75 + * The stream is freed on close, but the string is not.
    6.76   *
    6.77   * @param s string to use
    6.78   * @param n length of the string
    6.79 @@ -123,6 +151,7 @@ IOStream *string_stream_new(char *s, int
    6.80      if(data && io){
    6.81          ok = 1;
    6.82          string_stream_init(io, data, s, n);
    6.83 +        io->nofree = 0;
    6.84      }
    6.85      if(!ok){
    6.86          deallocate(data);
     7.1 --- a/tools/xfrd/http.h	Fri May 13 14:37:25 2005 +0000
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,50 +0,0 @@
     7.4 -#ifndef _XFRD_HTTP_H_
     7.5 -#define _XFRD_HTTP_H_
     7.6 -
     7.7 -enum {
     7.8 -    HTTP_OK                              = 200,
     7.9 -    HTTP_CREATED                         = 201,
    7.10 -    HTTP_ACCEPTED                        = 202,
    7.11 -    HTTP_NON_AUTHORITATIVE_INFORMATION   = 203,
    7.12 -    HTTP_NO_CONTENT                      = 204,
    7.13 -    HTTP_RESET_CONTENT                   = 205,
    7.14 -    HTTP_PARTIAL_CONTENT                 = 206,
    7.15 -    HTTP_MULTI_STATUS                    = 207,
    7.16 -
    7.17 -    HTTP_MULTIPLE_CHOICE                 = 300,
    7.18 -    HTTP_MOVED_PERMANENTLY               = 301,
    7.19 -    HTTP_FOUND                           = 302,
    7.20 -    HTTP_SEE_OTHER                       = 303,
    7.21 -    HTTP_NOT_MODIFIED                    = 304,
    7.22 -    HTTP_USE_PROXY                       = 305,
    7.23 -    HTTP_TEMPORARY_REDIRECT              = 307,
    7.24 -
    7.25 -    HTTP_BAD_REQUEST                     = 400,
    7.26 -    HTTP_UNAUTHORIZED                    = 401,
    7.27 -    HTTP_PAYMENT_REQUIRED                = 402,
    7.28 -    HTTP_FORBIDDEN                       = 403,
    7.29 -    HTTP_NOT_FOUND                       = 404,
    7.30 -    HTTP_NOT_ALLOWED                     = 405,
    7.31 -    HTTP_NOT_ACCEPTABLE                  = 406,
    7.32 -    HTTP_PROXY_AUTH_REQUIRED             = 407,
    7.33 -    HTTP_REQUEST_TIMEOUT                 = 408,
    7.34 -    HTTP_CONFLICT                        = 409,
    7.35 -    HTTP_GONE                            = 410,
    7.36 -    HTTP_LENGTH_REQUIRED                 = 411,
    7.37 -    HTTP_PRECONDITION_FAILED             = 412,
    7.38 -    HTTP_REQUEST_ENTITY_TOO_LARGE        = 413,
    7.39 -    HTTP_REQUEST_URI_TOO_LONG            = 414,
    7.40 -    HTTP_UNSUPPORTED_MEDIA_TYPE          = 415,
    7.41 -    HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
    7.42 -    HTTP_EXPECTATION_FAILED              = 417,
    7.43 -
    7.44 -    HTTP_INTERNAL_SERVER_ERROR           = 500,
    7.45 -    HTTP_NOT_IMPLEMENTED                 = 501,
    7.46 -    HTTP_BAD_GATEWAY                     = 502,
    7.47 -    HTTP_SERVICE_UNAVAILABLE             = 503,
    7.48 -    HTTP_GATEWAY_TIMEOUT                 = 504,
    7.49 -    HTTP_VERSION_NOT_SUPPORTED           = 505,
    7.50 -    HTTP_INSUFFICIENT_STORAGE_SPACE      = 507,
    7.51 -    HTTP_NOT_EXTENDED                    = 510,
    7.52 -};
    7.53 -#endif /* ! _XFRD_HTTP_H_ */
     8.1 --- a/tools/xfrd/xdr.c	Fri May 13 14:37:25 2005 +0000
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,316 +0,0 @@
     8.4 -#include <errno.h>
     8.5 -#include "xdr.h"
     8.6 -
     8.7 -#define MODULE_NAME "XDR"
     8.8 -//#define DEBUG 1
     8.9 -#undef DEBUG
    8.10 -#include "debug.h"
    8.11 -
    8.12 -/** @file
    8.13 - * XDR packer/unpacker for elements.
    8.14 - *
    8.15 - * string -> [T_STRING] [len:u16] <len bytes>
    8.16 - * atom   -> [T_ATOM]   [len:u16] <len bytes>
    8.17 - * uint   -> [T_UINT]   [value]
    8.18 - * cons   -> [T_LIST]   {1 elt}* 0
    8.19 - * null   -> [T_NULL]
    8.20 - * none   -> [T_NONE]
    8.21 - * bool   -> [T_BOOL]   { 0:u8 | 1:u8 }
    8.22 - *
    8.23 - * types packed as u16.
    8.24 - *
    8.25 - * So (a b c) -> [T_CONS] a [T_CONS] b [T_CONS] c [T_NULL]
    8.26 - *    ()      -> [T_NULL]
    8.27 - */
    8.28 -
    8.29 -int pack_bool(IOStream *io, int x){
    8.30 -    int err=0;
    8.31 -    //dprintf("> x=%d\n", x);
    8.32 -    err = IOStream_print(io, "%c", 0xff & x);
    8.33 -    if(err > 0) err = 0;
    8.34 -    //dprintf("< err=%d\n", err);
    8.35 -    return err;
    8.36 -}
    8.37 -
    8.38 -int unpack_bool(IOStream *io, int *x){
    8.39 -    int err = 0;
    8.40 -    int c;
    8.41 -    //dprintf(">\n");
    8.42 -    c = IOStream_getc(io);
    8.43 -    *x = (c < 0 ? 0 : c);
    8.44 -    err = IOStream_error(io);
    8.45 -    if(c < 0 && !err) err = -EIO;
    8.46 -    //dprintf("< err=%d x=%d\n", err, *x);
    8.47 -    return err;
    8.48 -}
    8.49 -
    8.50 -int pack_ushort(IOStream *io, unsigned short x){
    8.51 -    int err=0;
    8.52 -    //dprintf("> x=%u\n", x);
    8.53 -    err = IOStream_print(io, "%c%c",
    8.54 -                         0xff & (x >>  8),
    8.55 -                         0xff & (x      ));
    8.56 -    if(err > 0) err = 0;
    8.57 -    //dprintf("< err=%d\n", err);
    8.58 -    return err;
    8.59 -}
    8.60 -
    8.61 -int unpack_ushort(IOStream *io, unsigned short *x){
    8.62 -    int err = 0;
    8.63 -    int i, c = 0;
    8.64 -    //dprintf(">\n");
    8.65 -    *x = 0;
    8.66 -    for(i = 0; i< 2; i++){
    8.67 -        c = IOStream_getc(io);
    8.68 -        if(c < 0) break;
    8.69 -        *x <<= 8;
    8.70 -        *x |= (0xff & c);
    8.71 -    }
    8.72 -    err = IOStream_error(io);
    8.73 -
    8.74 -    if(c < 0 && !err) err = -EIO;
    8.75 -    //dprintf("< err=%d x=%u\n", err, *x);
    8.76 -    return err;
    8.77 -}
    8.78 -
    8.79 -int pack_type(IOStream *io, unsigned short x){
    8.80 -    return pack_ushort(io, x);
    8.81 -}
    8.82 -
    8.83 -int unpack_type(IOStream *io, unsigned short *x){
    8.84 -    return unpack_ushort(io, x);
    8.85 -}
    8.86 -
    8.87 -int pack_uint(IOStream *io, unsigned int x){
    8.88 -    int err=0;
    8.89 -    //dprintf("> x=%u\n", x);
    8.90 -    err = IOStream_print(io, "%c%c%c%c",
    8.91 -                         0xff & (x >> 24),
    8.92 -                         0xff & (x >> 16),
    8.93 -                         0xff & (x >>  8),
    8.94 -                         0xff & (x      ));
    8.95 -    if(err > 0) err = 0;
    8.96 -    //dprintf("< err=%d\n", err);
    8.97 -    return err;
    8.98 -}
    8.99 -
   8.100 -int unpack_uint(IOStream *io, unsigned int *x){
   8.101 -    int err = 0;
   8.102 -    int i, c = 0;
   8.103 -    //dprintf(">\n");
   8.104 -    *x = 0;
   8.105 -    for(i = 0; i< 4; i++){
   8.106 -        c = IOStream_getc(io);
   8.107 -        if(c < 0) break;
   8.108 -        *x <<= 8;
   8.109 -        *x |= (0xff & c);
   8.110 -    }
   8.111 -    err = IOStream_error(io);
   8.112 -    if(c < 0 && !err) err = -EIO;
   8.113 -    //dprintf("< err=%d x=%u\n", err, *x);
   8.114 -    return err;
   8.115 -}
   8.116 -
   8.117 -int pack_string(IOStream *io, Sxpr x){
   8.118 -    int err = 0;
   8.119 -    unsigned short n = 0xffff & string_length(x);
   8.120 -    char *s = string_string(x);
   8.121 -    int i;
   8.122 -    //dprintf("> n=%d s=%s\n", n, s);
   8.123 -    err = pack_ushort(io, n);
   8.124 -    if(err) goto exit;
   8.125 -    for(i = 0; i < n; i++){
   8.126 -        err = IOStream_print(io, "%c", s[i]);
   8.127 -        if(err < 0) break;
   8.128 -    }
   8.129 -    if(err > 0) err = 0;
   8.130 -  exit:
   8.131 -    //dprintf("< err=%d\n", err);
   8.132 -    return err;
   8.133 -}
   8.134 -
   8.135 -int unpack_string(IOStream *io, Sxpr *x){
   8.136 -    int err;
   8.137 -    unsigned short n;
   8.138 -    int i, c = 0;
   8.139 -    char *s;
   8.140 -    Sxpr val = ONONE;
   8.141 -    
   8.142 -    //dprintf(">\n");
   8.143 -    err = unpack_ushort(io, &n);
   8.144 -    if(err) goto exit;
   8.145 -    val = halloc(n+1, T_STRING);
   8.146 -    if(NOMEMP(val)){
   8.147 -        err = -ENOMEM;
   8.148 -        goto exit;
   8.149 -    }
   8.150 -    s = string_string(val);
   8.151 -    for(i=0; i<n; i++){
   8.152 -        c = IOStream_getc(io);
   8.153 -        if(c < 0) break;
   8.154 -        s[i] = (char)c;
   8.155 -    }
   8.156 -    s[n] = '\0';
   8.157 -  exit:
   8.158 -    err = IOStream_error(io);
   8.159 -    if(c < 0 && !err) err = -EIO;
   8.160 -    if(err){
   8.161 -        objfree(val);
   8.162 -        val = ONONE;
   8.163 -    }
   8.164 -    *x = val;
   8.165 -    //IOStream_print(iostdout, "n=%d str=", n); 
   8.166 -    //objprint(iostdout, *x, 0);
   8.167 -    //IOStream_print(iostdout, "\n");
   8.168 -    //dprintf("< err=%d\n", err);
   8.169 -    return err;
   8.170 -}
   8.171 -
   8.172 -int pack_cons(IOStream *io, Sxpr x){
   8.173 -    int err = 0;
   8.174 -    Sxpr l;
   8.175 -    //dprintf(">\n");
   8.176 -    for(l = x; CONSP(l); l = CDR(l)){
   8.177 -        err = pack_bool(io, 1);
   8.178 -        if(err) goto exit;
   8.179 -        err = pack_sxpr(io, CAR(l));
   8.180 -        if(err) goto exit;
   8.181 -    }
   8.182 -    err = pack_bool(io, 0);
   8.183 -  exit:
   8.184 -    //dprintf("< err=%d\n", err);
   8.185 -    return err;
   8.186 -}
   8.187 -
   8.188 -int unpack_cons(IOStream *io, Sxpr *x){
   8.189 -    int err = 0;
   8.190 -    int more = 0;
   8.191 -    Sxpr u = ONONE, v = ONONE, val = ONULL;
   8.192 -
   8.193 -    dprintf(">\n");
   8.194 -    while(1){
   8.195 -        err = unpack_bool(io, &more);
   8.196 -        if(err) goto exit;
   8.197 -        if(!more){
   8.198 -            //IOStream_print(iostdout, "unpack_cons 1 val=");
   8.199 -            ////objprint(iostdout, val, 0);
   8.200 -            IOStream_print(iostdout, "\n");
   8.201 -
   8.202 -            val = nrev(val);
   8.203 -
   8.204 -            //IOStream_print(iostdout, "unpack_cons 2 val=");
   8.205 -            //objprint(iostdout, val, 0);
   8.206 -            //IOStream_print(iostdout, "\n");
   8.207 -
   8.208 -            break;
   8.209 -        }
   8.210 -        err = unpack_sxpr(io, &u);
   8.211 -        if(err) goto exit;
   8.212 -        v = cons_new(u, val);
   8.213 -        if(NOMEMP(v)){
   8.214 -            err = -ENOMEM;
   8.215 -            objfree(u);
   8.216 -            goto exit;
   8.217 -        }
   8.218 -        val = v;
   8.219 -    }
   8.220 -  exit:
   8.221 -    if(err){
   8.222 -        objfree(val);
   8.223 -        val = ONONE;
   8.224 -    }
   8.225 -    *x = val;
   8.226 -    dprintf("< err=%d\n", err);
   8.227 -    return err;
   8.228 -}
   8.229 -    
   8.230 -int pack_sxpr(IOStream *io, Sxpr x){
   8.231 -    int err = 0;
   8.232 -    unsigned short type = get_type(x);
   8.233 -    //dprintf(">\n");
   8.234 -    //objprint(iostdout, x, 0);
   8.235 -    //IOStream_print(iostdout, "\n");
   8.236 -
   8.237 -    err = pack_type(io, type);
   8.238 -    if(err) goto exit;
   8.239 -    switch(type){
   8.240 -    case T_NULL:
   8.241 -        break;
   8.242 -    case T_NONE:
   8.243 -        break;
   8.244 -    case T_BOOL:
   8.245 -        err = pack_bool(io, get_ul(x));
   8.246 -        break;
   8.247 -    case T_CONS:
   8.248 -        err = pack_cons(io, x);
   8.249 -        break;
   8.250 -    case T_ATOM:
   8.251 -        err = pack_string(io, OBJ_ATOM(x)->name);
   8.252 -        break;
   8.253 -    case T_STRING:
   8.254 -        err = pack_string(io, x);
   8.255 -        break;
   8.256 -    case T_UINT:
   8.257 -        err = pack_uint(io, get_ul(x));
   8.258 -        break;
   8.259 -    default:
   8.260 -        err = -EINVAL;
   8.261 -        IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type);
   8.262 -        break;
   8.263 -    }
   8.264 -  exit:
   8.265 -    //dprintf("< err=%d\n", err);
   8.266 -    return err;
   8.267 -}
   8.268 -
   8.269 -int unpack_sxpr(IOStream *io, Sxpr *x){
   8.270 -    int err = 0;
   8.271 -    unsigned short type;
   8.272 -    unsigned int u;
   8.273 -    Sxpr val = ONONE, y;
   8.274 -
   8.275 -    //dprintf(">\n");
   8.276 -    err = unpack_type(io, &type);
   8.277 -    if(err) goto exit;
   8.278 -    switch(type){
   8.279 -    case T_NULL:
   8.280 -        val = ONULL;
   8.281 -        break;
   8.282 -    case T_NONE:
   8.283 -        val = ONONE;
   8.284 -        break;
   8.285 -    case T_CONS:
   8.286 -        err = unpack_cons(io, &val);
   8.287 -        break;
   8.288 -    case T_BOOL:
   8.289 -        err = unpack_bool(io, (int *)&u);
   8.290 -        if(err) goto exit;
   8.291 -        val = (u ? OTRUE : OFALSE);
   8.292 -        break;
   8.293 -    case T_ATOM:
   8.294 -        err = unpack_string(io, &y);
   8.295 -        if(err) goto exit;
   8.296 -        val = intern(string_string(y));
   8.297 -        objfree(y);
   8.298 -        break;
   8.299 -    case T_STRING:
   8.300 -        err = unpack_string(io, &val);
   8.301 -        break;
   8.302 -    case T_UINT:
   8.303 -        err = unpack_uint(io, &u);
   8.304 -        if(err) goto exit;
   8.305 -        val = OBJI(type, u);
   8.306 -        break;
   8.307 -    default:
   8.308 -        err = -EINVAL;
   8.309 -        IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type);
   8.310 -        break;
   8.311 -    }
   8.312 -  exit:
   8.313 -    *x = (err ? ONONE : val);
   8.314 -    //IOStream_print(iostdout, "sxpr="); 
   8.315 -    //objprint(iostdout, *x, 0);
   8.316 -    //IOStream_print(iostdout, "\n");
   8.317 -    //dprintf("< err=%d\n", err);
   8.318 -    return err;
   8.319 -}
     9.1 --- a/tools/xfrd/xdr.h	Fri May 13 14:37:25 2005 +0000
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,30 +0,0 @@
     9.4 -#ifndef _XUTIL_XDR_H_
     9.5 -#define _XUTIL_XDR_H_
     9.6 -#include "iostream.h"
     9.7 -#include "sxpr.h"
     9.8 -
     9.9 -int pack_type(IOStream *io, unsigned short x);
    9.10 -
    9.11 -int unpack_type(IOStream *io, unsigned short *x);
    9.12 -
    9.13 -int pack_bool(IOStream *io, int x);
    9.14 -
    9.15 -int unpack_bool(IOStream *io, int *x);
    9.16 -
    9.17 -int pack_uint(IOStream *out, unsigned int x);
    9.18 -
    9.19 -int unpack_uint(IOStream *in, unsigned int *x);
    9.20 -
    9.21 -int pack_string(IOStream *out, Sxpr x);
    9.22 -
    9.23 -int unpack_string(IOStream *in, Sxpr *x);
    9.24 -
    9.25 -int pack_cons(IOStream *out, Sxpr x);
    9.26 -
    9.27 -int unpack_cons(IOStream *in, Sxpr *x);
    9.28 -
    9.29 -int pack_sxpr(IOStream *out, Sxpr x);
    9.30 -
    9.31 -int unpack_sxpr(IOStream *in, Sxpr *x);
    9.32 -
    9.33 -#endif /* _XUTIL_XDR_H_ */