1 /* $OpenBSD: stack.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */
4 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
28 static __inline bool stack_empty(const struct stack *);
29 static void stack_grow(struct stack *);
30 static struct array *array_new(void);
31 static __inline void array_free(struct array *);
32 static struct array *array_dup(const struct array *);
33 static __inline void array_grow(struct array *, size_t);
34 static __inline void array_assign(struct array *, size_t, const struct value *);
35 static __inline struct value *array_retrieve(const struct array *, size_t);
38 stack_init(struct stack *stack)
47 stack_empty(const struct stack *stack)
49 bool empty = stack->sp == -1;
56 /* Clear number or string, but leave value itself */
58 stack_free_value(struct value *v)
65 free_number(v->u.num);
71 if (v->array != NULL) {
77 /* Copy number or string content into already allocated target */
79 stack_dup_value(const struct value *a, struct value *copy)
88 copy->u.num = dup_number(a->u.num);
91 copy->u.string = strdup(a->u.string);
92 if (copy->u.string == NULL)
97 copy->array = a->array == NULL ? NULL : array_dup(a->array);
103 stack_size(const struct stack *stack)
106 return (stack->sp + 1);
110 stack_dup(struct stack *stack)
115 value = stack_tos(stack);
117 warnx("stack empty");
120 stack_push(stack, stack_dup_value(value, ©));
124 stack_swap(struct stack *stack)
129 warnx("stack empty");
132 copy = stack->stack[stack->sp];
133 stack->stack[stack->sp] = stack->stack[stack->sp-1];
134 stack->stack[stack->sp-1] = copy;
138 stack_grow(struct stack *stack)
142 if (++stack->sp == stack->size) {
143 new_size = stack->size * 2 + 1;
144 stack->stack = brealloc(stack->stack,
145 new_size * sizeof(*stack->stack));
146 stack->size = new_size;
151 stack_pushnumber(struct stack *stack, struct number *b)
155 stack->stack[stack->sp].type = BCODE_NUMBER;
156 stack->stack[stack->sp].u.num = b;
157 stack->stack[stack->sp].array = NULL;
161 stack_pushstring(struct stack *stack, char *string)
165 stack->stack[stack->sp].type = BCODE_STRING;
166 stack->stack[stack->sp].u.string = string;
167 stack->stack[stack->sp].array = NULL;
171 stack_push(struct stack *stack, struct value *v)
177 stack->stack[stack->sp].type = BCODE_NONE;
180 stack_pushnumber(stack, v->u.num);
183 stack_pushstring(stack, v->u.string);
186 stack->stack[stack->sp].array = v->array == NULL ?
187 NULL : array_dup(v->array);
191 stack_tos(const struct stack *stack)
196 return &stack->stack[stack->sp];
200 stack_set_tos(struct stack *stack, struct value *v)
204 stack_push(stack, v);
206 stack_free_value(&stack->stack[stack->sp]);
207 stack->stack[stack->sp] = *v;
208 stack->stack[stack->sp].array = v->array == NULL ?
209 NULL : array_dup(v->array);
214 stack_pop(struct stack *stack)
217 if (stack_empty(stack))
219 return &stack->stack[stack->sp--];
223 stack_popnumber(struct stack *stack)
226 if (stack_empty(stack))
228 if (stack->stack[stack->sp].array != NULL) {
229 array_free(stack->stack[stack->sp].array);
230 stack->stack[stack->sp].array = NULL;
232 if (stack->stack[stack->sp].type != BCODE_NUMBER) {
233 warnx("not a number"); /* XXX remove */
236 return stack->stack[stack->sp--].u.num;
240 stack_popstring(struct stack *stack)
243 if (stack_empty(stack))
245 if (stack->stack[stack->sp].array != NULL) {
246 array_free(stack->stack[stack->sp].array);
247 stack->stack[stack->sp].array = NULL;
249 if (stack->stack[stack->sp].type != BCODE_STRING) {
250 warnx("not a string"); /* XXX remove */
253 return stack->stack[stack->sp--].u.string;
257 stack_clear(struct stack *stack)
260 while (stack->sp >= 0) {
261 stack_free_value(&stack->stack[stack->sp--]);
268 stack_print(FILE *f, const struct stack *stack, const char *prefix, u_int base)
272 for (i = stack->sp; i >= 0; i--) {
273 print_value(f, &stack->stack[i], prefix, base);
279 static struct array *
284 a = bmalloc(sizeof(*a));
291 array_free(struct array *a)
297 for (i = 0; i < a->size; i++)
298 stack_free_value(&a->data[i]);
303 static struct array *
304 array_dup(const struct array *a)
312 array_grow(n, a->size);
313 for (i = 0; i < a->size; i++)
314 stack_dup_value(&a->data[i], &n->data[i]);
319 array_grow(struct array *array, size_t newsize)
323 array->data = brealloc(array->data, newsize * sizeof(*array->data));
324 for (i = array->size; i < newsize; i++) {
325 array->data[i].type = BCODE_NONE;
326 array->data[i].array = NULL;
328 array->size = newsize;
332 array_assign(struct array *array, size_t i, const struct value *v)
335 if (i >= array->size)
336 array_grow(array, i + 1);
337 stack_free_value(&array->data[i]);
341 static __inline struct value *
342 array_retrieve(const struct array *array, size_t i)
345 if (i >= array->size)
347 return &array->data[i];
351 frame_assign(struct stack *stack, size_t i, const struct value *v)
356 if (stack->sp == -1) {
359 stack_push(stack, &n);
362 a = stack->stack[stack->sp].array;
364 a = stack->stack[stack->sp].array = array_new();
365 array_assign(a, i, v);
369 frame_retrieve(const struct stack *stack, size_t i)
375 a = stack->stack[stack->sp].array;
377 a = stack->stack[stack->sp].array = array_new();
378 return array_retrieve(a, i);