~ubuntu-branches/ubuntu/hardy/orbital-eunuchs-sniper/hardy

« back to all changes in this revision

Viewing changes to src/sexpr/sexp_ops.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-05-29 09:32:48 UTC
  • mfrom: (1.1.1 upstream) (2.1.2 gutsy)
  • Revision ID: james.westby@ubuntu.com-20070529093248-laj1bsm2dffohdf9
Tags: 1.30+svn20070601-1
Fix broken "upstream" rule to generate correctly versioned orig.tar.gz
to avoid native package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
26
/** NOTE: This library is part of the supermon project, hence the name
 
27
          supermon above. **/
 
28
#include <assert.h>
 
29
#include <stdlib.h>
 
30
#include <stdio.h>
 
31
#include <string.h>
 
32
#include "sexp_ops.h"
 
33
 
 
34
/**
 
35
 * Given an s-expression, find the atom inside of it with the 
 
36
 * value matchine name, and return a reference to it.  If the atom
 
37
 * doesn't occur inside start, return NULL.
 
38
 */
 
39
sexp_t *
 
40
find_sexp (char *name, sexp_t * start)
 
41
{
 
42
  sexp_t *temp;
 
43
 
 
44
  if (start == NULL)
 
45
    return NULL;
 
46
 
 
47
  if (start->ty == SEXP_LIST)
 
48
    {
 
49
      temp = find_sexp (name, start->list);
 
50
      if (temp == NULL)
 
51
        return find_sexp (name, start->next);
 
52
      else
 
53
        return temp;
 
54
    }
 
55
  else
 
56
    {
 
57
      assert(start->val != NULL);
 
58
      if (strcmp (start->val, name) == 0)
 
59
        return start;
 
60
      else
 
61
        return find_sexp (name, start->next);
 
62
    }
 
63
 
 
64
  return NULL;                  /* shouldn't get here */
 
65
}
 
66
 
 
67
/**
 
68
 * Breadth first search - look at ->next before ->list when seeing list
 
69
 * elements of an expression.
 
70
 */
 
71
sexp_t *bfs_find_sexp(char *str, sexp_t *sx) {
 
72
  sexp_t *t = sx;
 
73
  sexp_t *rt;
 
74
  
 
75
  if (sx == NULL) return NULL;
 
76
 
 
77
  while (t != NULL) {
 
78
    if (t->ty == SEXP_VALUE) {
 
79
      assert(t->val != NULL);
 
80
      if (strcmp(t->val,str) == 0) {
 
81
        return t;
 
82
      }
 
83
    } 
 
84
 
 
85
    t = t->next;
 
86
  }
 
87
 
 
88
  t = sx;
 
89
  while (t != NULL) {
 
90
    if (t->ty == SEXP_LIST) {
 
91
      rt = bfs_find_sexp(str,t->list);
 
92
      if (rt != NULL) return rt;
 
93
    }
 
94
    
 
95
    t = t->next;
 
96
  }
 
97
 
 
98
  return NULL;
 
99
}
 
100
 
 
101
/**
 
102
 * Copy an s-expression.
 
103
 */
 
104
sexp_t *copy_sexp(sexp_t *s) {
 
105
  sexp_t *snew;
 
106
 
 
107
  if (s == NULL) return NULL;
 
108
 
 
109
  /*  snew = (sexp_t *)malloc(sizeof(sexp_t)); */
 
110
  snew = sexp_t_allocate();
 
111
  assert(snew != NULL);
 
112
 
 
113
  snew->ty = s->ty;
 
114
  if (snew->ty == SEXP_VALUE) {
 
115
    snew->aty = s->aty;
 
116
    assert(s->val != NULL);
 
117
    strcpy(snew->val,s->val);
 
118
    snew->list = NULL;
 
119
  } else {
 
120
    snew->list = copy_sexp(s->list);
 
121
  }
 
122
  
 
123
  snew->next = copy_sexp(s->next);
 
124
 
 
125
  return snew;
 
126
}
 
127
 
 
128
/**
 
129
 * Cons: Concatenate two s-expressions together, without references to the
 
130
 * originals.
 
131
 */
 
132
sexp_t *cons_sexp(sexp_t *r, sexp_t *l) {
 
133
  sexp_t *cr, *cl, *t;
 
134
 
 
135
  cr = copy_sexp(r);
 
136
  if (cr->ty == SEXP_VALUE) {
 
137
    fprintf(stderr,"Cannot cons non-lists.\n");
 
138
    destroy_sexp(cr);
 
139
    return NULL;
 
140
  } else {
 
141
    t = cr->list;
 
142
    while (t != NULL && t->next != NULL) t = t->next;
 
143
  }
 
144
 
 
145
  cl = copy_sexp(l);
 
146
 
 
147
  if (cl->ty == SEXP_LIST) {
 
148
    if (t != NULL && cl != NULL) {
 
149
      t->next = cl->list;
 
150
      /* free(cl); */ /* memory leak fix: SMJ, 4/24/2002 */
 
151
      sexp_t_deallocate(cl);
 
152
    }
 
153
  } else {
 
154
    fprintf(stderr,"Cannot cons non-lists.\n");
 
155
    destroy_sexp(cr);
 
156
    destroy_sexp(cl);
 
157
    return NULL;
 
158
  }
 
159
 
 
160
  return cr;
 
161
}
 
162
 
 
163
/**
 
164
 * car: similar to head, except this is a copy and not just a reference.
 
165
 */
 
166
sexp_t *car_sexp(sexp_t *s) {
 
167
        sexp_t *cr, *ocr;
 
168
 
 
169
        /* really dumb - calling on null */
 
170
        if (s == NULL) {
 
171
                fprintf(stderr,"car called on null sexpr.\n");
 
172
                return NULL;
 
173
        }
 
174
 
 
175
        /* less dumb - calling on an atom */
 
176
        if (s->ty == SEXP_VALUE) {
 
177
                fprintf(stderr,"car called on an atom.\n");
 
178
                return NULL;
 
179
        }
 
180
 
 
181
        /*      ocr = (sexp_t *)malloc(sizeof(sexp_t));*/
 
182
        ocr = sexp_t_allocate();
 
183
        assert(ocr != NULL);
 
184
        ocr->ty = SEXP_LIST;
 
185
        ocr->next = NULL;
 
186
 
 
187
        /* allocate the new sexp_t */
 
188
        /* cr = (sexp_t *)malloc(sizeof(sexp_t)); */
 
189
        cr = sexp_t_allocate();
 
190
        assert(cr != NULL);
 
191
        ocr->list = cr;
 
192
 
 
193
        /* copy the head of the sexpr */
 
194
        if (s->list->ty == SEXP_VALUE) {
 
195
                cr->ty = SEXP_VALUE;
 
196
                assert(s->list->val != NULL);
 
197
                strcpy(cr->val,s->list->val);
 
198
                cr->next = cr->list = NULL;
 
199
        } else {
 
200
                cr->ty = SEXP_LIST;
 
201
                cr->next = NULL;
 
202
                cr->list = copy_sexp(s->list->list);
 
203
        }
 
204
 
 
205
        return ocr;
 
206
}
 
207
 
 
208
/**
 
209
 * cdr: similar to tail, except this is a copy and not just a reference.
 
210
 */
 
211
sexp_t *cdr_sexp(sexp_t *s) {
 
212
        sexp_t *cd;
 
213
 
 
214
        /* really dumb */
 
215
        if (s == NULL) {
 
216
                fprintf(stderr,"cdr called on null.\n");
 
217
                return NULL;
 
218
        }
 
219
 
 
220
        /* less dumb */
 
221
        if (s->ty != SEXP_LIST) {
 
222
                fprintf(stderr,"cdr called on atom.\n");
 
223
                return NULL;
 
224
        }
 
225
 
 
226
        /* cd = (sexp_t *)malloc(sizeof(sexp_t)); */
 
227
        cd = sexp_t_allocate();
 
228
        assert(cd != NULL);
 
229
 
 
230
        cd->ty = SEXP_LIST;
 
231
        cd->next = NULL;
 
232
        cd->list = copy_sexp(s->list->next);
 
233
        return cd;
 
234
}
 
235