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