| Petr Machata | 865303f | 2012-01-06 18:40:38 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * This file is part of ltrace. | 
 | 3 |  * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc. | 
 | 4 |  * | 
 | 5 |  * This program is free software; you can redistribute it and/or | 
 | 6 |  * modify it under the terms of the GNU General Public License as | 
 | 7 |  * published by the Free Software Foundation; either version 2 of the | 
 | 8 |  * License, or (at your option) any later version. | 
 | 9 |  * | 
 | 10 |  * This program is distributed in the hope that it will be useful, but | 
 | 11 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 12 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
 | 13 |  * General Public License for more details. | 
 | 14 |  * | 
 | 15 |  * You should have received a copy of the GNU General Public License | 
 | 16 |  * along with this program; if not, write to the Free Software | 
 | 17 |  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | 
 | 18 |  * 02110-1301 USA | 
 | 19 |  */ | 
 | 20 |  | 
 | 21 | #include <assert.h> | 
 | 22 | #include <stdlib.h> | 
 | 23 |  | 
 | 24 | #include "param.h" | 
 | 25 | #include "type.h" | 
 | 26 | #include "value.h" | 
 | 27 | #include "expr.h" | 
 | 28 |  | 
 | 29 | void | 
 | 30 | param_init_type(struct param *param, struct arg_type_info *type, int own) | 
 | 31 | { | 
 | 32 | 	param->flavor = PARAM_FLAVOR_TYPE; | 
 | 33 | 	param->u.type.type = type; | 
 | 34 | 	param->u.type.own_type = own; | 
 | 35 | } | 
 | 36 |  | 
 | 37 | void | 
 | 38 | param_init_stop(struct param *param) | 
 | 39 | { | 
 | 40 | 	param->flavor = PARAM_FLAVOR_STOP; | 
 | 41 | } | 
 | 42 |  | 
 | 43 | void | 
 | 44 | param_init_pack(struct param *param, | 
 | 45 | 		struct expr_node *args, size_t nargs, int own_args, | 
 | 46 | 		struct param_enum *(*init)(struct value *cb_args, | 
 | 47 | 					   size_t nargs, | 
 | 48 | 					   struct value_dict *arguments), | 
 | 49 | 		int (*next)(struct param_enum *context, | 
 | 50 | 			    struct arg_type_info *infop, | 
 | 51 | 			    int *insert_stop), | 
 | 52 | 		enum param_status (*stop)(struct param_enum *ctx, | 
 | 53 | 					  struct value *value), | 
 | 54 | 		void (*done)(struct param_enum *)) | 
 | 55 | { | 
 | 56 | 	param->flavor = PARAM_FLAVOR_PACK; | 
 | 57 | 	param->u.pack.args = args; | 
 | 58 | 	param->u.pack.nargs = nargs; | 
 | 59 | 	param->u.pack.own_args = own_args; | 
 | 60 | 	param->u.pack.init = init; | 
 | 61 | 	param->u.pack.next = next; | 
 | 62 | 	param->u.pack.stop = stop; | 
 | 63 | 	param->u.pack.done = done; | 
 | 64 | } | 
 | 65 |  | 
 | 66 | struct param_enum * | 
 | 67 | param_pack_init(struct param *param, struct value_dict *fargs) | 
 | 68 | { | 
 | 69 | 	struct value cb_args[param->u.pack.nargs]; | 
 | 70 | 	size_t i; | 
 | 71 |  | 
 | 72 | 	/* For evaluation of argument expressions, we pass in this as | 
 | 73 | 	 * a "current" value.  */ | 
 | 74 | 	struct arg_type_info *void_type = type_get_simple(ARGTYPE_VOID); | 
 | 75 | 	struct value void_val; | 
 | 76 | 	value_init_detached(&void_val, NULL, void_type, 0); | 
 | 77 |  | 
 | 78 | 	struct param_enum *ret = NULL; | 
 | 79 | 	for (i = 0; i < param->u.pack.nargs; ++i) { | 
 | 80 | 		if (expr_eval(¶m->u.pack.args[i], &void_val, | 
 | 81 | 			      fargs, &cb_args[i]) < 0) | 
 | 82 | 			goto release; | 
 | 83 | 	} | 
 | 84 |  | 
 | 85 | 	ret = param->u.pack.init(cb_args, param->u.pack.nargs, fargs); | 
 | 86 |  | 
 | 87 | release: | 
 | 88 | 	while (i-- > 0) | 
 | 89 | 		value_destroy(&cb_args[i]); | 
 | 90 | 	return ret; | 
 | 91 | } | 
 | 92 |  | 
 | 93 | int | 
 | 94 | param_pack_next(struct param *param, struct param_enum *context, | 
 | 95 | 		struct arg_type_info *infop, int *insert_stop) | 
 | 96 | { | 
 | 97 | 	return param->u.pack.next(context, infop, insert_stop); | 
 | 98 | } | 
 | 99 |  | 
 | 100 | enum param_status | 
 | 101 | param_pack_stop(struct param *param, | 
 | 102 | 		struct param_enum *context, struct value *value) | 
 | 103 | { | 
 | 104 | 	return param->u.pack.stop(context, value); | 
 | 105 | } | 
 | 106 |  | 
 | 107 | void | 
 | 108 | param_pack_done(struct param *param, struct param_enum *context) | 
 | 109 | { | 
 | 110 | 	return param->u.pack.done(context); | 
 | 111 | } | 
 | 112 |  | 
 | 113 | void | 
 | 114 | param_destroy(struct param *param) | 
 | 115 | { | 
 | 116 | 	if (param == NULL) | 
 | 117 | 		return; | 
 | 118 |  | 
 | 119 | 	switch (param->flavor) { | 
 | 120 | 	case PARAM_FLAVOR_TYPE: | 
 | 121 | 		if (param->u.type.own_type) { | 
 | 122 | 			type_destroy(param->u.type.type); | 
 | 123 | 			free(param->u.type.type); | 
 | 124 | 		} | 
 | 125 | 		return; | 
 | 126 |  | 
 | 127 | 	case PARAM_FLAVOR_PACK: | 
 | 128 | 		if (param->u.pack.own_args) { | 
 | 129 | 			size_t i; | 
 | 130 | 			for (i = 0; i < param->u.pack.nargs; ++i) | 
 | 131 | 				expr_destroy(¶m->u.pack.args[i]); | 
 | 132 | 			free(param->u.pack.args); | 
 | 133 | 		} | 
 | 134 | 	case PARAM_FLAVOR_STOP: | 
 | 135 | 		return; | 
 | 136 | 	} | 
 | 137 |  | 
 | 138 | 	assert(!"Unknown value of param flavor!"); | 
 | 139 | 	abort(); | 
 | 140 | } |