author | Myhailo Danylenko <isbear@ukrpost.net> |
Fri, 15 Jan 2010 15:44:00 +0200 | |
changeset 4 | faaed3ae3fa8 |
parent 2 | 6a47079725da |
child 8 | 2033d1c5d226 |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* yaubil.c -- Yet Another Useless Built-In Language |
|
3 |
* |
|
4 |
* Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
||
22 |
#include <glib.h> |
|
23 |
#include <gmodule.h> |
|
24 |
#include <string.h> |
|
25 |
||
2
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
26 |
#include <mcabber/commands.h> |
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
27 |
#include <mcabber/compl.h> |
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
28 |
#include <mcabber/logprint.h> |
6a47079725da
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
29 |
#include <mcabber/settings.h> |
0 | 30 |
|
31 |
static gboolean ifresult = TRUE; |
|
32 |
||
33 |
#define MSGPREFIX "yaubil: " |
|
34 |
||
35 |
#define TYPE_UNDEF ( 0 ) |
|
36 |
#define TYPE_STR ( 1 ) |
|
37 |
#define TYPE_INT ( 2 ) |
|
38 |
||
39 |
#define STATE_LVALUE ( 1 ) |
|
40 |
#define STATE_OP ( 2 ) |
|
41 |
#define STATE_RVALUE ( 3 ) |
|
42 |
||
43 |
typedef struct { |
|
44 |
int type; |
|
45 |
int int_value; |
|
46 |
char *str_value; |
|
47 |
} value_t; |
|
48 |
||
49 |
typedef struct { |
|
50 |
char op; |
|
51 |
gboolean (*handler) (value_t *l, value_t *r); |
|
52 |
} op_t; |
|
53 |
||
54 |
static int check_value_type (const char *value) |
|
55 |
{ |
|
56 |
if (value) { |
|
57 |
const char *e; |
|
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
58 |
gboolean integer = (*value == '-') ? TRUE : (g_ascii_isdigit (*value) ? TRUE : FALSE); |
0 | 59 |
|
60 |
if (integer) { |
|
61 |
for (e = value + 1; *e; ++e) { |
|
62 |
if (g_ascii_isdigit (*e)) |
|
63 |
integer = TRUE; |
|
64 |
else { |
|
65 |
integer = FALSE; |
|
66 |
break; |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
if (integer) |
|
72 |
return TYPE_INT; |
|
73 |
else |
|
74 |
return TYPE_STR; |
|
75 |
} else |
|
76 |
return TYPE_UNDEF; |
|
77 |
} |
|
78 |
||
79 |
static gboolean op_concat (value_t *l, value_t *r) |
|
80 |
{ |
|
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
81 |
GString *res = g_string_new (NULL); |
0 | 82 |
|
83 |
if (l->type == TYPE_INT) |
|
84 |
g_string_append_printf (res, "%d", l->int_value); |
|
85 |
else if (l->type == TYPE_STR) |
|
86 |
g_string_append (res, l->str_value); |
|
87 |
||
88 |
if (r->type == TYPE_INT) |
|
89 |
g_string_append_printf (res, "%d", r->int_value); |
|
90 |
else if (r->type == TYPE_STR) |
|
91 |
g_string_append (res, r->str_value); |
|
92 |
||
93 |
l->type = TYPE_STR; |
|
94 |
g_free (l->str_value); |
|
95 |
l->str_value = g_string_free (res, FALSE); |
|
96 |
||
97 |
return TRUE; |
|
98 |
} |
|
99 |
||
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
100 |
static gboolean op_head (value_t *l, value_t *r) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
101 |
{ |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
102 |
if (l->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
103 |
l->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
104 |
l->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
105 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
106 |
if (r->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
107 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
108 |
r->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
109 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
110 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
111 |
if (l -> type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
112 |
l -> type = TYPE_STR; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
113 |
if (!l -> str_value) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
114 |
l -> str_value = g_strdup_printf ("%d", l -> int_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
115 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
116 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
117 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
118 |
r->int_value = atoi (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
119 |
g_free (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
120 |
r->str_value = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
121 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
122 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
123 |
if (l->type == TYPE_STR && r->type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
124 |
gchar *res = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
125 |
int len = l -> str_value ? strlen (l -> str_value) : 0; // XXX can str_value be NULL? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
126 |
int off = r -> int_value; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
127 |
if (off < 0 && len + off > 0) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
128 |
res = g_strndup (l -> str_value, len + off); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
129 |
else if (off > 0 && len > off) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
130 |
res = g_strndup (l -> str_value, off); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
131 |
else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
132 |
res = g_strdup (""); // XXX leave NULL here? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
133 |
g_free (l->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
134 |
l->str_value = res; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
135 |
} else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
136 |
return FALSE; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
137 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
138 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
139 |
static gboolean op_tail (value_t *l, value_t *r) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
140 |
{ |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
141 |
if (l->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
142 |
l->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
143 |
l->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
144 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
145 |
if (r->type == TYPE_UNDEF) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
146 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
147 |
r->int_value = 0; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
148 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
149 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
150 |
if (l -> type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
151 |
l -> type = TYPE_STR; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
152 |
if (!l -> str_value) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
153 |
l -> str_value = g_strdup_printf ("%d", l -> int_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
154 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
155 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
156 |
r->type = TYPE_INT; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
157 |
r->int_value = atoi (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
158 |
g_free (r->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
159 |
r->str_value = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
160 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
161 |
|
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
162 |
if (l->type == TYPE_STR && r->type == TYPE_INT) { |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
163 |
gchar *res = NULL; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
164 |
int len = l -> str_value ? strlen (l -> str_value) : 0; // XXX can str_value be NULL? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
165 |
int off = r -> int_value; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
166 |
if (off < 0 && len + off > 0) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
167 |
res = g_strdup (l -> str_value + (-off)); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
168 |
else if (off > 0 && len > off) |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
169 |
res = g_strdup (l -> str_value + (len - off)); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
170 |
else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
171 |
res = g_strdup (""); // XXX leave NULL here? |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
172 |
g_free (l->str_value); |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
173 |
l->str_value = res; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
174 |
} else |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
175 |
return FALSE; |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
176 |
} |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
177 |
|
0 | 178 |
static gboolean op_plus (value_t *l, value_t *r) |
179 |
{ |
|
180 |
if (l->type == TYPE_UNDEF) { |
|
181 |
l->type = TYPE_INT; |
|
182 |
l->int_value = 0; |
|
183 |
} |
|
184 |
if (r->type == TYPE_UNDEF) { |
|
185 |
r->type = TYPE_INT; |
|
186 |
r->int_value = 0; |
|
187 |
} |
|
188 |
||
189 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
190 |
l->type = TYPE_INT; |
|
191 |
l->int_value = atoi (l->str_value); |
|
192 |
// not free value here |
|
193 |
} |
|
194 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
195 |
r->type = TYPE_INT; |
|
196 |
r->int_value = atoi (r->str_value); |
|
197 |
// not free value here |
|
198 |
} |
|
199 |
||
200 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
201 |
// integer |
|
202 |
l->int_value += r->int_value; |
|
203 |
g_free (l->str_value); |
|
204 |
l->str_value = NULL; |
|
205 |
g_free (r->str_value); |
|
206 |
r->str_value = NULL; |
|
207 |
} else { |
|
208 |
// convert both to strings |
|
209 |
if (l->type == TYPE_INT) { |
|
210 |
l->type = TYPE_STR; |
|
211 |
if (!l->str_value) |
|
212 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
213 |
} |
|
214 |
if (r->type == TYPE_INT) { |
|
215 |
r->type = TYPE_STR; |
|
216 |
if (!r->str_value) |
|
217 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
218 |
} |
|
219 |
||
220 |
{ |
|
221 |
char *tmp = l->str_value; |
|
222 |
l->str_value = g_strdup_printf ("%s%s", tmp, r->str_value); |
|
223 |
g_free (tmp); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
return TRUE; |
|
228 |
} |
|
229 |
||
230 |
static gboolean op_minus (value_t *l, value_t *r) |
|
231 |
{ |
|
232 |
if (l->type == TYPE_UNDEF) { |
|
233 |
l->type = TYPE_INT; |
|
234 |
l->int_value = 0; |
|
235 |
} |
|
236 |
if (r->type == TYPE_UNDEF) { |
|
237 |
r->type = TYPE_INT; |
|
238 |
r->int_value = 0; |
|
239 |
} |
|
240 |
||
241 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
242 |
l->type = TYPE_INT; |
|
243 |
l->int_value = atoi (l->str_value); |
|
244 |
g_free (l->str_value); |
|
245 |
l->str_value = NULL; |
|
246 |
} |
|
247 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
248 |
r->type = TYPE_INT; |
|
249 |
r->int_value = atoi (r->str_value); |
|
250 |
g_free (r->str_value); |
|
251 |
r->str_value = NULL; |
|
252 |
} |
|
253 |
||
254 |
if (l->type == TYPE_INT && r->type == TYPE_INT) |
|
255 |
l->int_value -= r->int_value; |
|
256 |
else |
|
257 |
return FALSE; |
|
258 |
||
259 |
return TRUE; |
|
260 |
} |
|
261 |
||
262 |
static gboolean op_multiply (value_t *l, value_t *r) |
|
263 |
{ |
|
264 |
if (l->type == TYPE_UNDEF) { |
|
265 |
l->type = TYPE_INT; |
|
266 |
l->int_value = 0; |
|
267 |
} |
|
268 |
if (r->type == TYPE_UNDEF) { |
|
269 |
r->type = TYPE_INT; |
|
270 |
r->int_value = 0; |
|
271 |
} |
|
272 |
||
273 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
274 |
l->type = TYPE_INT; |
|
275 |
l->int_value = atoi (l->str_value); |
|
276 |
// not free value here |
|
277 |
} |
|
278 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
279 |
r->type = TYPE_INT; |
|
280 |
r->int_value = atoi (r->str_value); |
|
281 |
g_free (r->str_value); |
|
282 |
r->str_value = NULL; |
|
283 |
} |
|
284 |
||
285 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
286 |
l->int_value *= r->int_value; |
|
287 |
g_free (l->str_value); |
|
288 |
l->str_value = NULL; |
|
289 |
} else if (r->type == TYPE_INT) { |
|
290 |
GString *res = g_string_new (NULL); |
|
291 |
int i; |
|
292 |
for (i = r->int_value; i; --i) |
|
293 |
g_string_append (res, l->str_value); |
|
294 |
g_free (l->str_value); |
|
295 |
l->str_value = g_string_free (res, FALSE); |
|
296 |
} else |
|
297 |
return FALSE; |
|
298 |
||
299 |
return TRUE; |
|
300 |
} |
|
301 |
||
302 |
static gboolean op_divide (value_t *l, value_t *r) |
|
303 |
{ |
|
304 |
if (l->type == TYPE_UNDEF) { |
|
305 |
l->type = TYPE_INT; |
|
306 |
l->int_value = 0; |
|
307 |
} |
|
308 |
if (r->type == TYPE_UNDEF) { |
|
309 |
r->type = TYPE_INT; |
|
310 |
r->int_value = 0; |
|
311 |
} |
|
312 |
||
313 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
314 |
l->type = TYPE_INT; |
|
315 |
l->int_value = atoi (l->str_value); |
|
316 |
g_free (l->str_value); |
|
317 |
l->str_value = NULL; |
|
318 |
} |
|
319 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
320 |
r->type = TYPE_INT; |
|
321 |
r->int_value = atoi (r->str_value); |
|
322 |
g_free (r->str_value); |
|
323 |
r->str_value = NULL; |
|
324 |
} |
|
325 |
||
326 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
327 |
if (r->int_value == 0) { |
|
328 |
scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "/: Error: division by zero."); |
|
329 |
return FALSE; |
|
330 |
} |
|
331 |
l->int_value /= r->int_value; |
|
332 |
} else |
|
333 |
return FALSE; |
|
334 |
||
335 |
return TRUE; |
|
336 |
} |
|
337 |
||
338 |
static gboolean op_remain (value_t *l, value_t *r) |
|
339 |
{ |
|
340 |
if (l->type == TYPE_UNDEF) { |
|
341 |
l->type = TYPE_INT; |
|
342 |
l->int_value = 0; |
|
343 |
} |
|
344 |
if (r->type == TYPE_UNDEF) { |
|
345 |
r->type = TYPE_INT; |
|
346 |
r->int_value = 0; |
|
347 |
} |
|
348 |
||
349 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
350 |
l->type = TYPE_INT; |
|
351 |
l->int_value = atoi (l->str_value); |
|
352 |
g_free (l->str_value); |
|
353 |
l->str_value = NULL; |
|
354 |
} |
|
355 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
356 |
r->type = TYPE_INT; |
|
357 |
r->int_value = atoi (r->str_value); |
|
358 |
g_free (r->str_value); |
|
359 |
r->str_value = NULL; |
|
360 |
} |
|
361 |
||
362 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
363 |
if (r->int_value == 0) { |
|
364 |
scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "%%: Error: division by zero."); |
|
365 |
return FALSE; |
|
366 |
} |
|
367 |
l->int_value %= r->int_value; |
|
368 |
} else |
|
369 |
return FALSE; |
|
370 |
||
371 |
return TRUE; |
|
372 |
} |
|
373 |
||
374 |
static gboolean op_equal (value_t *l, value_t *r) |
|
375 |
{ |
|
376 |
if (l->type == TYPE_UNDEF) { |
|
377 |
l->type = TYPE_INT; |
|
378 |
l->int_value = 0; |
|
379 |
} |
|
380 |
if (r->type == TYPE_UNDEF) { |
|
381 |
r->type = TYPE_INT; |
|
382 |
r->int_value = 0; |
|
383 |
} |
|
384 |
||
385 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
386 |
l->type = TYPE_INT; |
|
387 |
l->int_value = atoi (l->str_value); |
|
388 |
// not free value here |
|
389 |
} |
|
390 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
391 |
r->type = TYPE_INT; |
|
392 |
r->int_value = atoi (r->str_value); |
|
393 |
// not free value here |
|
394 |
} |
|
395 |
||
396 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
397 |
// integer |
|
398 |
l->int_value = (l->int_value == r->int_value) ? 1 : 0; |
|
399 |
g_free (l->str_value); |
|
400 |
l->str_value = NULL; |
|
401 |
g_free (r->str_value); |
|
402 |
r->str_value = NULL; |
|
403 |
} else { |
|
404 |
// convert both to strings |
|
405 |
if (l->type == TYPE_INT) { |
|
406 |
l->type = TYPE_STR; |
|
407 |
if (!l->str_value) |
|
408 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
409 |
} |
|
410 |
if (r->type == TYPE_INT) { |
|
411 |
r->type = TYPE_STR; |
|
412 |
if (!r->str_value) |
|
413 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
414 |
} |
|
415 |
||
416 |
{ |
|
417 |
char *tmp = l->str_value; |
|
418 |
l->type = TYPE_INT; |
|
419 |
l->int_value = (g_strcmp0 (l->str_value, r->str_value) == 0) ? 1 : 0; |
|
420 |
g_free (tmp); |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
return TRUE; |
|
425 |
} |
|
426 |
||
427 |
static gboolean op_lt (value_t *l, value_t *r) |
|
428 |
{ |
|
429 |
if (l->type == TYPE_UNDEF) { |
|
430 |
l->type = TYPE_INT; |
|
431 |
l->int_value = 0; |
|
432 |
} |
|
433 |
if (r->type == TYPE_UNDEF) { |
|
434 |
r->type = TYPE_INT; |
|
435 |
r->int_value = 0; |
|
436 |
} |
|
437 |
||
438 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
439 |
l->type = TYPE_INT; |
|
440 |
l->int_value = atoi (l->str_value); |
|
441 |
// not free value here |
|
442 |
} |
|
443 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
444 |
r->type = TYPE_INT; |
|
445 |
r->int_value = atoi (r->str_value); |
|
446 |
// not free value here |
|
447 |
} |
|
448 |
||
449 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
450 |
// integer |
|
451 |
l->int_value = (l->int_value < r->int_value) ? 1 : 0; |
|
452 |
g_free (l->str_value); |
|
453 |
l->str_value = NULL; |
|
454 |
g_free (r->str_value); |
|
455 |
r->str_value = NULL; |
|
456 |
} else { |
|
457 |
// convert both to strings |
|
458 |
if (l->type == TYPE_INT) { |
|
459 |
l->type = TYPE_STR; |
|
460 |
if (!l->str_value) |
|
461 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
462 |
} |
|
463 |
if (r->type == TYPE_INT) { |
|
464 |
r->type = TYPE_STR; |
|
465 |
if (!r->str_value) |
|
466 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
467 |
} |
|
468 |
||
469 |
{ |
|
470 |
char *tmp = l->str_value; |
|
471 |
l->type = TYPE_INT; |
|
472 |
l->int_value = (g_strcmp0 (l->str_value, r->str_value) < 0) ? 1 : 0; |
|
473 |
g_free (tmp); |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
return TRUE; |
|
478 |
} |
|
479 |
||
480 |
static gboolean op_gt (value_t *l, value_t *r) |
|
481 |
{ |
|
482 |
if (l->type == TYPE_UNDEF) { |
|
483 |
l->type = TYPE_INT; |
|
484 |
l->int_value = 0; |
|
485 |
} |
|
486 |
if (r->type == TYPE_UNDEF) { |
|
487 |
r->type = TYPE_INT; |
|
488 |
r->int_value = 0; |
|
489 |
} |
|
490 |
||
491 |
if (l->type == TYPE_STR && check_value_type (l->str_value) == TYPE_INT) { |
|
492 |
l->type = TYPE_INT; |
|
493 |
l->int_value = atoi (l->str_value); |
|
494 |
// not free value here |
|
495 |
} |
|
496 |
if (r->type == TYPE_STR && check_value_type (r->str_value) == TYPE_INT) { |
|
497 |
r->type = TYPE_INT; |
|
498 |
r->int_value = atoi (r->str_value); |
|
499 |
// not free value here |
|
500 |
} |
|
501 |
||
502 |
if (l->type == TYPE_INT && r->type == TYPE_INT) { |
|
503 |
// integer |
|
504 |
l->int_value = (l->int_value > r->int_value) ? 1 : 0; |
|
505 |
g_free (l->str_value); |
|
506 |
l->str_value = NULL; |
|
507 |
g_free (r->str_value); |
|
508 |
r->str_value = NULL; |
|
509 |
} else { |
|
510 |
// convert both to strings |
|
511 |
if (l->type == TYPE_INT) { |
|
512 |
l->type = TYPE_STR; |
|
513 |
if (!l->str_value) |
|
514 |
l->str_value = g_strdup_printf ("%d", l->int_value); |
|
515 |
} |
|
516 |
if (r->type == TYPE_INT) { |
|
517 |
r->type = TYPE_STR; |
|
518 |
if (!r->str_value) |
|
519 |
r->str_value = g_strdup_printf ("%d", r->int_value); |
|
520 |
} |
|
521 |
||
522 |
{ |
|
523 |
char *tmp = l->str_value; |
|
524 |
l->type = TYPE_INT; |
|
525 |
l->int_value = (g_strcmp0 (l->str_value, r->str_value) > 0) ? 1 : 0; |
|
526 |
g_free (tmp); |
|
527 |
} |
|
528 |
} |
|
529 |
||
530 |
return TRUE; |
|
531 |
} |
|
532 |
||
533 |
static op_t operators[] = { |
|
534 |
{ '.', op_concat }, |
|
4
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
535 |
{ ':', op_head }, |
faaed3ae3fa8
String head and tail operators
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
536 |
{ '^', op_tail }, |
0 | 537 |
{ '+', op_plus }, |
538 |
{ '-', op_minus }, |
|
539 |
{ '*', op_multiply }, |
|
540 |
{ '/', op_divide }, |
|
541 |
{ '%', op_remain }, |
|
542 |
{ '=', op_equal }, |
|
543 |
{ '<', op_lt }, |
|
544 |
{ '>', op_gt }, |
|
545 |
{ 0, NULL }, |
|
546 |
}; |
|
547 |
||
548 |
static void destroy_value (value_t *value) |
|
549 |
{ |
|
550 |
if (value->str_value) |
|
551 |
g_free (value->str_value); |
|
552 |
g_free (value); |
|
553 |
} |
|
554 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
555 |
static value_t *process_expression (const char *str, gsize *len) |
0 | 556 |
{ |
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
557 |
const char *strend = str + *len; |
0 | 558 |
const char *p; |
559 |
op_t *op = NULL; |
|
560 |
int state = STATE_LVALUE; |
|
561 |
value_t val = { |
|
562 |
.type = TYPE_UNDEF, |
|
563 |
.int_value = 0, |
|
564 |
.str_value = NULL, |
|
565 |
}; |
|
566 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
567 |
for (p = str; *p && p < strend && *p != ')'; ++p) { |
0 | 568 |
switch (state) { |
569 |
case STATE_LVALUE: |
|
570 |
case STATE_RVALUE: |
|
571 |
||
572 |
if (*p == ' ') |
|
573 |
break; |
|
574 |
||
575 |
{ |
|
576 |
value_t rval = { |
|
577 |
.type = TYPE_UNDEF, |
|
578 |
.int_value = 0, |
|
579 |
.str_value = NULL, |
|
580 |
}; |
|
581 |
||
582 |
if (g_ascii_isdigit (*p) || *p == '-') { // integer // XXX: no unary operators for now... |
|
583 |
||
584 |
const char *e; |
|
585 |
||
586 |
rval.type = TYPE_INT; |
|
587 |
||
588 |
for (e = p + 1; g_ascii_isdigit (*e) && e < strend; ++e); // TODO: does atoi handle 0x etc? then also allow this? |
|
589 |
||
590 |
{ |
|
591 |
char *v = g_strndup (p, e - p); |
|
592 |
rval.int_value = atoi (v); |
|
593 |
g_free (v); |
|
594 |
} |
|
595 |
||
596 |
p = e - 1; |
|
597 |
||
598 |
} else if (*p == '"') { // string |
|
599 |
||
600 |
const char *e; |
|
601 |
||
602 |
rval.type = TYPE_STR; |
|
603 |
||
604 |
{ |
|
605 |
gboolean finished = FALSE; |
|
606 |
gboolean escape = FALSE; |
|
607 |
GString *v = g_string_new (NULL); |
|
608 |
||
609 |
for (e = p + 1; *e && e < strend; ++e) { |
|
610 |
switch (*e) { |
|
611 |
case '\\': |
|
612 |
if (!escape) |
|
613 |
escape = TRUE; |
|
614 |
else |
|
615 |
escape = FALSE; |
|
616 |
break; |
|
617 |
case '"': |
|
618 |
if (!escape) { |
|
619 |
finished = TRUE; |
|
620 |
break; |
|
621 |
} else |
|
622 |
escape = FALSE; |
|
623 |
break; |
|
624 |
default: |
|
625 |
escape = FALSE; |
|
626 |
break; |
|
627 |
} |
|
628 |
||
629 |
if (finished) |
|
630 |
break; |
|
631 |
||
632 |
if (!escape) |
|
633 |
g_string_append_c (v, *e); |
|
634 |
}; |
|
635 |
||
636 |
if (!finished) { |
|
637 |
scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: Unmatched quote."); |
|
638 |
g_string_free (v, TRUE); |
|
639 |
g_free (val.str_value); |
|
640 |
return NULL; |
|
641 |
} |
|
642 |
||
643 |
rval.str_value = g_string_free (v, FALSE); |
|
644 |
} |
|
645 |
||
646 |
p = e; |
|
647 |
||
648 |
} else if (g_ascii_isalpha (*p)) { // variable (MUST start from alpha) |
|
649 |
||
650 |
const char *e; |
|
651 |
||
652 |
for (e = p + 1; (g_ascii_isalnum (*e) || *e == '-' || *e == '_') && e < strend; ++e); |
|
653 |
||
654 |
char *name = g_strndup (p, e - p); |
|
655 |
const char *value = settings_opt_get (name); |
|
656 |
g_free (name); |
|
657 |
||
658 |
rval.type = check_value_type (value); |
|
659 |
if (rval.type == TYPE_INT) |
|
660 |
rval.int_value = atoi (value); |
|
661 |
else if (rval.type == TYPE_STR) |
|
662 |
rval.str_value = g_strdup (value); |
|
663 |
||
664 |
p = e - 1; |
|
665 |
||
666 |
} else if (*p == '(') { |
|
667 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
668 |
gsize len = strend - p - 1; |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
669 |
value_t *n = process_expression (p + 1, &len); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
670 |
|
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
671 |
if (!n) { |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
672 |
scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: Error in subexpression."); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
673 |
g_free (val.str_value); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
674 |
return NULL; |
0 | 675 |
} |
676 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
677 |
rval.type = n->type; |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
678 |
rval.int_value = n->int_value; |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
679 |
rval.str_value = g_strdup (n->str_value); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
680 |
destroy_value (n); |
0 | 681 |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
682 |
p += len + 1; |
0 | 683 |
|
684 |
} else { |
|
685 |
scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: unrecognized symbols."); |
|
686 |
g_free (val.str_value); |
|
687 |
return NULL; |
|
688 |
} |
|
689 |
||
690 |
if (state == STATE_RVALUE) { |
|
691 |
if (op->handler) { |
|
692 |
if (!op->handler (&val, &rval)) { |
|
693 |
scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: operand argument types mismatch."); |
|
694 |
g_free (val.str_value); |
|
695 |
g_free (rval.str_value); |
|
696 |
return NULL; |
|
697 |
} |
|
698 |
} |
|
699 |
g_free (rval.str_value); |
|
700 |
} else { |
|
701 |
val.type = rval.type; |
|
702 |
val.int_value = rval.int_value; |
|
703 |
val.str_value = rval.str_value; |
|
704 |
} |
|
705 |
||
706 |
state = STATE_OP; |
|
707 |
} |
|
708 |
||
709 |
break; |
|
710 |
||
711 |
case STATE_OP: |
|
712 |
||
713 |
if (*p == ' ') |
|
714 |
break; |
|
715 |
||
716 |
{ |
|
717 |
op_t *operator; |
|
718 |
gboolean found = FALSE; |
|
719 |
||
720 |
for (operator = operators; operator->op; ++operator) { |
|
721 |
if (operator->op == *p) { |
|
722 |
op = operator; |
|
723 |
found = TRUE; |
|
724 |
break; |
|
725 |
} |
|
726 |
} |
|
727 |
||
728 |
if (!found) { |
|
729 |
scr_LogPrint (LPRINT_NORMAL, MSGPREFIX "Error: unknown operator."); |
|
730 |
g_free (val.str_value); |
|
731 |
} |
|
732 |
} |
|
733 |
||
734 |
state = STATE_RVALUE; |
|
735 |
||
736 |
break; |
|
737 |
default: |
|
738 |
break; |
|
739 |
} |
|
740 |
} |
|
741 |
||
742 |
{ // return value |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
743 |
*len = p - str; |
0 | 744 |
value_t *rval = g_new (value_t, 1); |
745 |
||
746 |
rval->type = val.type; |
|
747 |
rval->int_value = val.int_value; |
|
748 |
rval->str_value = val.str_value; |
|
749 |
||
750 |
return rval; |
|
751 |
} |
|
752 |
} |
|
753 |
||
754 |
static void do_eval (char *arg) |
|
755 |
{ |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
756 |
gsize len = strlen (arg); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
757 |
value_t *val = process_expression (arg, &len); |
0 | 758 |
|
759 |
if (!val) { |
|
760 |
scr_LogPrint (LPRINT_NORMAL, "eval: Evaluation error."); |
|
761 |
return; |
|
762 |
} |
|
763 |
||
764 |
if (val->type == TYPE_STR) |
|
765 |
process_command (val->str_value, TRUE); |
|
766 |
else |
|
767 |
scr_LogPrint (LPRINT_NORMAL, "eval: Expression does not result in string."); |
|
768 |
||
769 |
destroy_value (val); |
|
770 |
} |
|
771 |
||
772 |
static void do_let (char *arg) |
|
773 |
{ |
|
774 |
char *varname; |
|
775 |
value_t *value; |
|
776 |
int namelen; |
|
777 |
char *val = strchr (arg, '='); |
|
778 |
||
779 |
if (!val) { |
|
780 |
scr_LogPrint (LPRINT_NORMAL, "let: Syntax error: no equal sign in line."); |
|
781 |
return; |
|
782 |
} |
|
783 |
||
784 |
{ |
|
785 |
char *p = val; |
|
786 |
||
787 |
for (p = val - 1; p >= arg && *p == ' '; --p); |
|
788 |
if (p < arg) { |
|
789 |
scr_LogPrint (LPRINT_NORMAL, "let: Syntax error: no destination variable name specified."); |
|
790 |
return; |
|
791 |
} |
|
792 |
||
793 |
namelen = p + 1 - arg; |
|
794 |
} |
|
795 |
||
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
796 |
{ // evaluate expression |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
797 |
gsize len = strlen (val + 1); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
798 |
|
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
799 |
value = process_expression (val + 1, &len); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
800 |
} |
0 | 801 |
|
802 |
if (!value) { |
|
803 |
scr_LogPrint (LPRINT_NORMAL, "let: Evaluation error."); |
|
804 |
return; |
|
805 |
} |
|
806 |
||
807 |
if (value->type == TYPE_INT) { |
|
808 |
value->type = TYPE_STR; |
|
809 |
value->str_value = g_strdup_printf ("%d", value->int_value); |
|
810 |
} |
|
811 |
||
812 |
{ // assign value |
|
813 |
char *varname = g_strndup (arg, namelen); |
|
814 |
||
815 |
if (value->str_value) |
|
816 |
settings_set (SETTINGS_TYPE_OPTION, varname, value->str_value); |
|
817 |
else |
|
818 |
settings_del (SETTINGS_TYPE_OPTION, varname); |
|
819 |
||
820 |
g_free (varname); |
|
821 |
} |
|
822 |
||
823 |
destroy_value (value); |
|
824 |
} |
|
825 |
||
826 |
static void do_if (char *arg) |
|
827 |
{ |
|
1
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
828 |
gsize len = strlen (arg); |
320e4393785a
Parentheses now have no restrictions in strings
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
829 |
value_t *val = process_expression (arg, &len); |
0 | 830 |
|
831 |
if (!val) { |
|
832 |
scr_LogPrint (LPRINT_NORMAL, "if: Evaluation error."); |
|
833 |
return; |
|
834 |
} |
|
835 |
||
836 |
if (val->type == TYPE_UNDEF) { |
|
837 |
val->type = TYPE_INT; |
|
838 |
val->int_value = 0; |
|
839 |
} |
|
840 |
if (val->type == TYPE_STR && check_value_type (val->str_value) == TYPE_INT) { |
|
841 |
val->type = TYPE_INT; |
|
842 |
val->int_value = atoi (val->str_value); |
|
843 |
g_free (val->str_value); |
|
844 |
val->str_value = NULL; |
|
845 |
} |
|
846 |
||
847 |
if (val->type == TYPE_INT) |
|
848 |
ifresult = val->int_value ? TRUE : FALSE; |
|
849 |
else if (val->str_value) |
|
850 |
ifresult = TRUE; |
|
851 |
else |
|
852 |
ifresult = FALSE; |
|
853 |
||
854 |
destroy_value (val); |
|
855 |
} |
|
856 |
||
857 |
static void do_then (char *arg) |
|
858 |
{ |
|
859 |
if (ifresult) |
|
860 |
process_command (arg, TRUE); |
|
861 |
} |
|
862 |
||
863 |
static void do_else (char *arg) |
|
864 |
{ |
|
865 |
if (!ifresult) |
|
866 |
process_command (arg, TRUE); |
|
867 |
} |
|
868 |
||
869 |
static void do_multi (char *arg) |
|
870 |
{ |
|
871 |
char *end; |
|
872 |
char *start = arg; |
|
873 |
||
874 |
for (end = strchr (start, ';'); end; end = strchr (start, ';')) { |
|
875 |
||
876 |
// execute command |
|
877 |
char *command = g_strndup (start, end - start); |
|
878 |
process_command (command, TRUE); |
|
879 |
g_free (command); |
|
880 |
||
881 |
// skip leading spaces |
|
882 |
for (start = end + 1; *start == ' '; ++start); |
|
883 |
} |
|
884 |
||
885 |
if (*start) |
|
886 |
process_command (start, TRUE); |
|
887 |
} |
|
888 |
||
889 |
const gchar *g_module_check_init (GModule *module) |
|
890 |
{ |
|
891 |
cmd_add ("multi", "", COMPL_CMD, COMPL_CMD, do_multi, NULL); |
|
892 |
cmd_add ("if", "", 0, 0, do_if, NULL); |
|
893 |
cmd_add ("then", "", COMPL_CMD, COMPL_CMD, do_then, NULL); |
|
894 |
cmd_add ("else", "", COMPL_CMD, COMPL_CMD, do_else, NULL); |
|
895 |
cmd_add ("eval", "", 0, 0, do_eval, NULL); |
|
896 |
cmd_add ("let", "", 0, 0, do_let, NULL); |
|
897 |
||
898 |
return NULL; |
|
899 |
} |
|
900 |
||
901 |
void g_module_unload (GModule *module) |
|
902 |
{ |
|
903 |
cmd_del ("multi"); |
|
904 |
cmd_del ("if"); |
|
905 |
cmd_del ("then"); |
|
906 |
cmd_del ("else"); |
|
907 |
cmd_del ("eval"); |
|
908 |
cmd_del ("let"); |
|
909 |
} |
|
910 |
||
911 |
/* The End */ |