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