1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#include <magic_eval.h>
#include <magic.h>
#include <magic_eval_lib.h>
#define DEBUG MAGIC_DEBUG_SET(0)
#if DEBUG
#define MAGIC_EVAL_PRINTF _magic_printf
#else
#define MAGIC_EVAL_PRINTF magic_null_printf
#endif
PRIVATE int magic_eval_print_style = MAGIC_EVAL_PRINT_STYLE_DEFAULT;
/*===========================================================================*
* magic_eval_get_var_cb *
*===========================================================================*/
PRIVATE struct val* magic_eval_get_var_cb(char* name, struct val* val)
{
_magic_selement_t selement;
int ret;
double dvalue;
void *pvalue;
unsigned long uvalue;
long ivalue;
char vvalue;
struct _magic_dsentry dsentry_buff;
MAGIC_EVAL_PRINTF("magic_eval_get_var_cb: %s requested\n", name);
ret = magic_selement_lookup_by_name(name, &selement, &dsentry_buff);
if(ret < 0) {
return NULL;
}
val->type = T_INT;
switch(selement.type->type_id) {
case MAGIC_TYPE_FLOAT:
dvalue = magic_selement_to_float(&selement);
val->type = T_REAL;
val->rval = dvalue;
break;
case MAGIC_TYPE_POINTER:
pvalue = magic_selement_to_ptr(&selement);
val->ival = (long) pvalue;
break;
case MAGIC_TYPE_INTEGER:
case MAGIC_TYPE_ENUM:
if(MAGIC_TYPE_FLAG(selement.type, MAGIC_TYPE_UNSIGNED)) {
uvalue = magic_selement_to_unsigned(&selement);
val->ival = (long) uvalue;
}
else {
ivalue = magic_selement_to_int(&selement);
val->ival = ivalue;
}
break;
case MAGIC_TYPE_VOID:
vvalue = *((char*) selement.address);
val->ival = (long) vvalue;
break;
default:
return NULL;
break;
}
if(magic_eval_print_style & MAGIC_EVAL_PRINT_VAR_VALUES) {
if(val->type == T_INT) {
_magic_printf("VAR: %s = %ld\n", name, val->ival);
}
else {
_magic_printf("VAR: %s = %g\n", name, val->rval);
}
}
return val;
}
/*===========================================================================*
* magic_eval_get_func_result_cb *
*===========================================================================*/
PRIVATE struct val* magic_eval_get_func_result_cb(char* name, struct val* arg,
struct val* ret)
{
struct _magic_function *function;
magic_eval_func_t magic_eval_func;
long result, iarg;
MAGIC_EVAL_PRINTF("magic_eval_get_func_result_cb: %s requested\n", name);
if(strncmp(MAGIC_EVAL_FUNC_PREFIX, name, strlen(MAGIC_EVAL_FUNC_PREFIX))) {
return NULL;
}
function = magic_function_lookup_by_name(NULL, name);
if(!function) {
return NULL;
}
magic_eval_func = (magic_eval_func_t) (long) function->address;
iarg = arg->type == T_INT ? arg->ival : (long) arg->rval;
result = magic_eval_func(iarg);
ret->type = T_INT;
ret->ival = result;
if(magic_eval_print_style & MAGIC_EVAL_PRINT_FUNC_RESULTS) {
_magic_printf("FUNCTION: %s(%ld) = %ld\n", name, iarg, result);
}
return ret;
}
/*===========================================================================*
* magic_eval_init *
*===========================================================================*/
PUBLIC void magic_eval_init()
{
eval_set_cb_get_var(magic_eval_get_var_cb);
eval_set_cb_get_func_result(magic_eval_get_func_result_cb);
}
/*===========================================================================*
* magic_eval *
*===========================================================================*/
PRIVATE int magic_eval(char *expr, struct val* result)
{
int ret;
MAGIC_EVAL_PRINTF("magic_eval: Evaluating expression %s\n", expr);
ret = evaluate(expr, result, NULL);
switch(ret) {
case ERROR_SYNTAX:
ret = MAGIC_EINVAL;
break;
case ERROR_FUNCNOTFOUND:
case ERROR_VARNOTFOUND:
ret = MAGIC_ENOENT;
break;
case ERROR_NOMEM:
ret = MAGIC_ENOMEM;
break;
case ERROR_DIV0:
ret = MAGIC_EBADMSTATE;
break;
case RESULT_OK:
ret = 0;
break;
default:
ret = MAGIC_EGENERIC;
break;
}
return ret;
}
/*===========================================================================*
* magic_eval_int *
*===========================================================================*/
PUBLIC int magic_eval_int(char *expr, long *result)
{
struct val val_res;
int ret;
ret = magic_eval(expr, &val_res);
if(ret < 0) {
return ret;
}
*result = val_res.type == T_INT ? val_res.ival : (long) val_res.rval;
return 0;
}
/*===========================================================================*
* magic_eval_bool *
*===========================================================================*/
PUBLIC int magic_eval_bool(char *expr, char *result)
{
struct val val_res;
int ret;
ret = magic_eval(expr, &val_res);
if(ret < 0) {
return ret;
}
if(val_res.type != T_INT) {
return MAGIC_EINVAL;
}
*result = val_res.ival == 0 ? 0 : 1;
return 0;
}
/*===========================================================================*
* magic_eval_float *
*===========================================================================*/
PUBLIC int magic_eval_float(char *expr, double *result)
{
struct val val_res;
int ret;
ret = magic_eval(expr, &val_res);
if(ret < 0) {
return ret;
}
*result = val_res.type == T_INT ? (double) val_res.ival : val_res.rval;
return 0;
}
/*===========================================================================*
* magic_eval_get_print_style *
*===========================================================================*/
PUBLIC int magic_eval_get_print_style()
{
return magic_eval_print_style;
}
/*===========================================================================*
* magic_eval_set_print_style *
*===========================================================================*/
PUBLIC void magic_eval_set_print_style(int style)
{
magic_eval_print_style = style;
}
|