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