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