1 /* $OpenBSD: stack.c,v 1.13 2014/12/01 13:13:00 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);
75 /* Copy number or string content into already allocated target */
77 stack_dup_value(const struct value *a, struct value *copy)
86 copy->u.num = dup_number(a->u.num);
89 copy->u.string = strdup(a->u.string);
90 if (copy->u.string == NULL)
95 copy->array = a->array == NULL ? NULL : array_dup(a->array);
101 stack_size(const struct stack *stack)
104 return (stack->sp + 1);
108 stack_dup(struct stack *stack)
113 value = stack_tos(stack);
115 warnx("stack empty");
118 stack_push(stack, stack_dup_value(value, ©));
122 stack_swap(struct stack *stack)
127 warnx("stack empty");
130 copy = stack->stack[stack->sp];
131 stack->stack[stack->sp] = stack->stack[stack->sp-1];
132 stack->stack[stack->sp-1] = copy;
136 stack_grow(struct stack *stack)
140 if (++stack->sp == stack->size) {
141 new_size = stack->size * 2 + 1;
142 stack->stack = breallocarray(stack->stack,
143 new_size, sizeof(*stack->stack));
144 stack->size = new_size;
149 stack_pushnumber(struct stack *stack, struct number *b)
153 stack->stack[stack->sp].type = BCODE_NUMBER;
154 stack->stack[stack->sp].u.num = b;
155 stack->stack[stack->sp].array = NULL;
159 stack_pushstring(struct stack *stack, char *string)
163 stack->stack[stack->sp].type = BCODE_STRING;
164 stack->stack[stack->sp].u.string = string;
165 stack->stack[stack->sp].array = NULL;
169 stack_push(struct stack *stack, struct value *v)
175 stack->stack[stack->sp].type = BCODE_NONE;
178 stack_pushnumber(stack, v->u.num);
181 stack_pushstring(stack, v->u.string);
184 stack->stack[stack->sp].array = v->array == NULL ?
185 NULL : array_dup(v->array);
189 stack_tos(const struct stack *stack)
194 return &stack->stack[stack->sp];
198 stack_set_tos(struct stack *stack, struct value *v)
202 stack_push(stack, v);
204 stack_free_value(&stack->stack[stack->sp]);
205 stack->stack[stack->sp] = *v;
206 stack->stack[stack->sp].array = v->array == NULL ?
207 NULL : array_dup(v->array);
212 stack_pop(struct stack *stack)
215 if (stack_empty(stack))
217 return &stack->stack[stack->sp--];
221 stack_popnumber(struct stack *stack)
224 if (stack_empty(stack))
226 array_free(stack->stack[stack->sp].array);
227 stack->stack[stack->sp].array = NULL;
228 if (stack->stack[stack->sp].type != BCODE_NUMBER) {
229 warnx("not a number"); /* XXX remove */
232 return stack->stack[stack->sp--].u.num;
236 stack_popstring(struct stack *stack)
239 if (stack_empty(stack))
241 array_free(stack->stack[stack->sp].array);
242 stack->stack[stack->sp].array = NULL;
243 if (stack->stack[stack->sp].type != BCODE_STRING) {
244 warnx("not a string"); /* XXX remove */
247 return stack->stack[stack->sp--].u.string;
251 stack_clear(struct stack *stack)
254 while (stack->sp >= 0)
255 stack_free_value(&stack->stack[stack->sp--]);
261 stack_print(FILE *f, const struct stack *stack, const char *prefix, u_int base)
265 for (i = stack->sp; i >= 0; i--) {
266 print_value(f, &stack->stack[i], prefix, base);
272 static struct array *
277 a = bmalloc(sizeof(*a));
284 array_free(struct array *a)
290 for (i = 0; i < a->size; i++)
291 stack_free_value(&a->data[i]);
296 static struct array *
297 array_dup(const struct array *a)
305 array_grow(n, a->size);
306 for (i = 0; i < a->size; i++)
307 stack_dup_value(&a->data[i], &n->data[i]);
312 array_grow(struct array *array, size_t newsize)
316 array->data = breallocarray(array->data, newsize, sizeof(*array->data));
317 for (i = array->size; i < newsize; i++) {
318 array->data[i].type = BCODE_NONE;
319 array->data[i].array = NULL;
321 array->size = newsize;
325 array_assign(struct array *array, size_t i, const struct value *v)
328 if (i >= array->size)
329 array_grow(array, i + 1);
330 stack_free_value(&array->data[i]);
334 static __inline struct value *
335 array_retrieve(const struct array *array, size_t i)
338 if (i >= array->size)
340 return &array->data[i];
344 frame_assign(struct stack *stack, size_t i, const struct value *v)
349 if (stack->sp == -1) {
352 stack_push(stack, &n);
355 a = stack->stack[stack->sp].array;
357 a = stack->stack[stack->sp].array = array_new();
358 array_assign(a, i, v);
362 frame_retrieve(const struct stack *stack, size_t i)
368 a = stack->stack[stack->sp].array;
370 a = stack->stack[stack->sp].array = array_new();
371 return array_retrieve(a, i);