| 1 | /** |
| 2 | This software and ancillary information (herein called "SOFTWARE") |
| 3 | called Supermon is made available under the terms described |
| 4 | here. The SOFTWARE has been approved for release with associated |
| 5 | LA-CC Number LA-CC 99-51. |
| 6 | |
| 7 | Unless otherwise indicated, this SOFTWARE has been authored by an |
| 8 | employee or employees of the University of California, operator of the |
| 9 | Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with |
| 10 | the U.S. Department of Energy. The U.S. Government has rights to use, |
| 11 | reproduce, and distribute this SOFTWARE, and to allow others to do so. |
| 12 | The public may copy, distribute, prepare derivative works and publicly |
| 13 | display this SOFTWARE without charge, provided that this Notice and |
| 14 | any statement of authorship are reproduced on all copies. Neither the |
| 15 | Government nor the University makes any warranty, express or implied, |
| 16 | or assumes any liability or responsibility for the use of this |
| 17 | SOFTWARE. |
| 18 | |
| 19 | If SOFTWARE is modified to produce derivative works, such modified |
| 20 | SOFTWARE should be clearly marked, so as not to confuse it with the |
| 21 | version available from LANL. |
| 22 | **/ |
| 23 | /** NOTICE: This software is licensed under the GNU Public License, which |
| 24 | is included as LICENSE_GPL in this source distribution. **/ |
| 25 | /** NOTE: This library is part of the supermon project, hence the name |
| 26 | supermon above. **/ |
| 27 | /*** |
| 28 | * Matt's smaller s-expression parsing library |
| 29 | * |
| 30 | * Written by Matt Sottile ([email protected]), January 2002. |
| 31 | ***/ |
| 32 | |
| 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include "sexp.h" |
| 39 | #include <assert.h> |
| 40 | |
| 41 | static sexp_iowrap_t *_internal_iow = NULL; |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | */ |
| 46 | int |
| 47 | cleanup_sexp_io() |
| 48 | { |
| 49 | if (_internal_iow != NULL) |
| 50 | destroy_iowrap(_internal_iow); |
| 51 | _internal_iow = NULL; |
| 52 | |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * |
| 58 | */ |
| 59 | int |
| 60 | read_sexp (char *buf, int size, int f) |
| 61 | { |
| 62 | sexp_t *sx; |
| 63 | |
| 64 | if (_internal_iow == NULL) |
| 65 | _internal_iow = init_iowrap(f); |
| 66 | if (_internal_iow->fd != f) { |
| 67 | destroy_iowrap(_internal_iow); |
| 68 | if (f > -1) _internal_iow = init_iowrap(f); |
| 69 | else return 0; |
| 70 | } |
| 71 | |
| 72 | sx = read_one_sexp(_internal_iow); |
| 73 | |
| 74 | if (sx == NULL) return 0; |
| 75 | |
| 76 | print_sexp(buf,size,sx); |
| 77 | |
| 78 | destroy_sexp(sx); |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * |
| 85 | */ |
| 86 | int |
| 87 | fread_sexp (char *buf, int size, FILE *fp) |
| 88 | { |
| 89 | return read_sexp(buf,size,fileno(fp)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * |
| 94 | */ |
| 95 | sexp_iowrap_t *init_iowrap(int fd) { |
| 96 | sexp_iowrap_t *iow; |
| 97 | |
| 98 | iow = (sexp_iowrap_t *)malloc(sizeof(sexp_iowrap_t)); |
| 99 | |
| 100 | iow->cc = NULL; |
| 101 | iow->fd = fd; |
| 102 | iow->cnt = 0; |
| 103 | |
| 104 | return iow; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * |
| 109 | */ |
| 110 | void destroy_iowrap(sexp_iowrap_t *iow) { |
| 111 | if (iow == NULL) return; /* idiot */ |
| 112 | |
| 113 | destroy_continuation(iow->cc); |
| 114 | free(iow); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * |
| 119 | */ |
| 120 | sexp_t *read_one_sexp(sexp_iowrap_t *iow) { |
| 121 | sexp_t *sx = NULL; |
| 122 | |
| 123 | if (iow->cnt == 0) { |
| 124 | iow->cnt = read(iow->fd,iow->buf,BUFSIZ); |
| 125 | if (iow->cnt == 0) return NULL; |
| 126 | } |
| 127 | |
| 128 | iow->cc = cparse_sexp(iow->buf,iow->cnt,iow->cc); |
| 129 | |
| 130 | while (iow->cc->last_sexp == NULL) { |
| 131 | if (iow->cc->error != 0) { |
| 132 | fprintf(stderr,"ERROR\n"); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | iow->cnt = read(iow->fd,iow->buf,BUFSIZ); |
| 137 | |
| 138 | if (iow->cnt == 0) |
| 139 | return NULL; |
| 140 | |
| 141 | iow->cc = cparse_sexp(iow->buf,iow->cnt,iow->cc); |
| 142 | } |
| 143 | |
| 144 | sx = iow->cc->last_sexp; |
| 145 | iow->cc->last_sexp = NULL; |
| 146 | |
| 147 | return sx; |
| 148 | } |