2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
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.
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.
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
30 param_init_type(struct param *param, struct arg_type_info *type, int own)
32 param->flavor = PARAM_FLAVOR_TYPE;
33 param->u.type.type = type;
34 param->u.type.own_type = own;
38 param_init_stop(struct param *param)
40 param->flavor = PARAM_FLAVOR_STOP;
44 param_init_pack(struct param *param, enum param_pack_flavor ppflavor,
45 struct expr_node *args, size_t nargs, int own_args,
46 struct param_enum *(*init)(struct value *cb_args,
48 struct value_dict *arguments),
49 int (*next)(struct param_enum *context,
50 struct arg_type_info *infop,
52 enum param_status (*stop)(struct param_enum *ctx,
54 void (*done)(struct param_enum *))
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.ppflavor = ppflavor;
61 param->u.pack.init = init;
62 param->u.pack.next = next;
63 param->u.pack.stop = stop;
64 param->u.pack.done = done;
68 param_pack_init(struct param *param, struct value_dict *fargs)
70 struct value cb_args[param->u.pack.nargs];
73 /* For evaluation of argument expressions, we pass in this as
74 * a "current" value. */
75 struct arg_type_info *void_type = type_get_simple(ARGTYPE_VOID);
76 struct value void_val;
77 value_init_detached(&void_val, NULL, void_type, 0);
79 struct param_enum *ret = NULL;
80 for (i = 0; i < param->u.pack.nargs; ++i) {
81 if (expr_eval(¶m->u.pack.args[i], &void_val,
82 fargs, &cb_args[i]) < 0)
86 ret = param->u.pack.init(cb_args, param->u.pack.nargs, fargs);
90 value_destroy(&cb_args[i]);
95 param_pack_next(struct param *param, struct param_enum *context,
96 struct arg_type_info *infop, int *insert_stop)
98 return param->u.pack.next(context, infop, insert_stop);
102 param_pack_stop(struct param *param,
103 struct param_enum *context, struct value *value)
105 return param->u.pack.stop(context, value);
109 param_pack_done(struct param *param, struct param_enum *context)
111 return param->u.pack.done(context);
115 param_destroy(struct param *param)
120 switch (param->flavor) {
121 case PARAM_FLAVOR_TYPE:
122 if (param->u.type.own_type) {
123 type_destroy(param->u.type.type);
124 free(param->u.type.type);
128 case PARAM_FLAVOR_PACK:
129 if (param->u.pack.own_args) {
131 for (i = 0; i < param->u.pack.nargs; ++i)
132 expr_destroy(¶m->u.pack.args[i]);
133 free(param->u.pack.args);
137 case PARAM_FLAVOR_STOP:
141 assert(!"Unknown value of param flavor!");