12 #elif defined __GNUC__
13 # define alloca __builtin_alloca
15 # define alloca __alloca
16 #elif defined _MSC_VER
18 # define alloca _alloca
24 void *alloca (size_t);
28 #include "embryo_private.h"
30 #define STRGET(ep, str, par) { \
31 Embryo_Cell *___cptr; \
33 if ((___cptr = embryo_data_address_get(ep, par))) { \
35 ___l = embryo_data_string_length_get(ep, ___cptr); \
36 (str) = alloca(___l + 1); \
37 if (str) embryo_data_string_get(ep, ___cptr, str); \
39 #define STRSET(ep, par, str) { \
40 Embryo_Cell *___cptr; \
41 if ((___cptr = embryo_data_address_get(ep, par))) { \
42 embryo_data_string_set(ep, str, ___cptr); \
45 /* exported string api */
48 _embryo_str_atoi(Embryo_Program *ep, Embryo_Cell *params)
53 if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
54 STRGET(ep, s1, params[1]);
56 return (Embryo_Cell)atoi(s1);
60 _embryo_str_fnmatch(Embryo_Program *ep, Embryo_Cell *params)
64 /* params[1] = glob */
66 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
67 STRGET(ep, s1, params[1]);
68 STRGET(ep, s2, params[2]);
69 if ((!s1) || (!s2)) return -1;
70 return (Embryo_Cell)fnmatch(s1, s2, 0);
74 _embryo_str_strcmp(Embryo_Program *ep, Embryo_Cell *params)
78 /* params[1] = str1 */
79 /* params[2] = str2 */
80 if (params[0] != (2 * sizeof(Embryo_Cell))) return -1;
81 STRGET(ep, s1, params[1]);
82 STRGET(ep, s2, params[2]);
83 if ((!s1) || (!s2)) return -1;
84 return (Embryo_Cell)strcmp(s1, s2);
88 _embryo_str_strncmp(Embryo_Program *ep, Embryo_Cell *params)
92 /* params[1] = str1 */
93 /* params[2] = str2 */
95 if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
96 if (params[3] < 0) params[3] = 0;
97 STRGET(ep, s1, params[1]);
98 STRGET(ep, s2, params[2]);
99 if ((!s1) || (!s2)) return -1;
100 return (Embryo_Cell)strncmp(s1, s2, (size_t)params[3]);
104 _embryo_str_strcpy(Embryo_Program *ep, Embryo_Cell *params)
108 /* params[1] = dst */
109 /* params[2] = str */
110 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
111 STRGET(ep, s1, params[2]);
113 STRSET(ep, params[1], s1);
118 _embryo_str_strncpy(Embryo_Program *ep, Embryo_Cell *params)
123 /* params[1] = dst */
124 /* params[2] = str */
126 if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
127 if (params[3] < 0) params[3] = 0;
128 STRGET(ep, s1, params[2]);
131 if (l > params[3]) s1[params[3]] = 0;
132 STRSET(ep, params[1], s1);
137 _embryo_str_strlen(Embryo_Program *ep, Embryo_Cell *params)
141 /* params[1] = str */
142 if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
143 STRGET(ep, s1, params[1]);
145 return (Embryo_Cell)strlen(s1);
149 _embryo_str_strcat(Embryo_Program *ep, Embryo_Cell *params)
153 /* params[1] = dsr */
154 /* params[2] = str */
155 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
156 STRGET(ep, s1, params[1]);
157 STRGET(ep, s2, params[2]);
158 if ((!s1) || (!s2)) return 0;
159 s3 = alloca(strlen(s1) + strlen(s2) + 1);
163 STRSET(ep, params[1], s3);
168 _embryo_str_strncat(Embryo_Program *ep, Embryo_Cell *params)
173 /* params[1] = dst */
174 /* params[2] = str */
176 if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
177 if (params[3] < 0) params[3] = 0;
178 STRGET(ep, s1, params[1]);
179 STRGET(ep, s2, params[2]);
180 if ((!s1) || (!s2)) return 0;
183 s3 = alloca(l1 + l2 + 1);
186 strncat(s3, s2, params[3]);
187 if (l2 >= params[3]) s3[l1 + params[3]] = 0;
188 STRSET(ep, params[1], s3);
193 _embryo_str_strprep(Embryo_Program *ep, Embryo_Cell *params)
197 /* params[1] = dst */
198 /* params[2] = str */
199 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
200 STRGET(ep, s1, params[1]);
201 STRGET(ep, s2, params[2]);
202 if ((!s1) || (!s2)) return 0;
203 s3 = alloca(strlen(s1) + strlen(s2) + 1);
207 STRSET(ep, params[1], s3);
212 _embryo_str_strnprep(Embryo_Program *ep, Embryo_Cell *params)
217 /* params[1] = dst */
218 /* params[2] = str */
220 if (params[0] != (3 * sizeof(Embryo_Cell))) return 0;
221 if (params[3] < 0) params[3] = 0;
222 STRGET(ep, s1, params[1]);
223 STRGET(ep, s2, params[2]);
224 if ((!s1) || (!s2)) return 0;
227 s3 = alloca(l1 + l2 + 1);
229 strncpy(s3, s2, params[3]);
230 if (params[3] <= l2) s3[params[3]] = 0;
232 STRSET(ep, params[1], s3);
237 _embryo_str_strcut(Embryo_Program *ep, Embryo_Cell *params)
242 /* params[1] = dst */
243 /* params[2] = str */
246 if (params[0] != (4 * sizeof(Embryo_Cell))) return 0;
247 if (params[3] < 0) params[3] = 0;
248 if (params[4] < params[3]) params[4] = params[3];
249 STRGET(ep, s1, params[2]);
252 if (params[3] >= l1) params[3] = l1;
253 if (params[4] >= l1) params[4] = l1;
254 if (params[4] == params[3])
256 STRSET(ep, params[1], "");
259 s2 = alloca(params[4] - params[3] + 1);
260 strncpy(s2, s1 + params[3], params[4] - params[3]);
261 s2[params[4] - params[3]] = 0;
262 STRSET(ep, params[1], s2);
267 _embryo_str_snprintf(Embryo_Program *ep, Embryo_Cell *params)
275 /* params[1] = buf */
276 /* params[2] = bufsize */
277 /* params[3] = format_string */
278 /* params[4] = first arg ... */
279 if (params[0] < (Embryo_Cell)(3 * sizeof(Embryo_Cell))) return 0;
280 if (params[2] <= 0) return 0;
281 STRGET(ep, s1, params[3]);
283 s2 = alloca(params[2] + 1);
286 pnum = (params[0] / sizeof(Embryo_Cell)) - 3;
287 for (p = 0, o = 0, i = 0; (s1[i]) && (o < (params[2] - 1)) && (p < (pnum + 1)); i++)
289 if ((!inesc) && (!insub))
291 if (s1[i] == '\\') inesc = 1;
292 else if (s1[i] == '%') insub = 1;
293 if ((!inesc) && (!insub))
322 if ((insub) && (s1[i] == '%')) pnum++;
323 if ((insub) && (p < pnum))
332 cptr = embryo_data_address_get(ep, params[4 + p]);
333 if (cptr) s2[o] = (char)(*cptr);
346 if (s1[i] == 'i') strcpy(fmt, "%i");
347 else if (s1[i] == 'd') strcpy(fmt, "%d");
348 else if (s1[i] == 'x') strcpy(fmt, "%x");
349 else if (s1[i] == 'X') strcpy(fmt, "%08x");
350 cptr = embryo_data_address_get(ep, params[4 + p]);
351 if (cptr) snprintf(tmp, sizeof(tmp), fmt, (int)(*cptr));
353 if ((o + l) > (params[2] - 1))
355 l = params[2] - 1 - o;
369 cptr = embryo_data_address_get(ep, params[4 + p]);
370 if (cptr) snprintf(tmp, sizeof(tmp), "%f", (double)EMBRYO_CELL_TO_FLOAT(*cptr));
372 if ((o + l) > (params[2] - 1))
374 l = params[2] - 1 - o;
388 STRGET(ep, tmp, params[4 + p]);
390 if ((o + l) > (params[2] - 1))
392 l = params[2] - 1 - o;
412 STRSET(ep, params[1], s2);
417 _embryo_str_strstr(Embryo_Program *ep, Embryo_Cell *params)
421 /* params[1] = str */
422 /* params[2] = ndl */
423 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
424 STRGET(ep, s1, params[1]);
425 STRGET(ep, s2, params[2]);
426 if ((!s1) || (!s2)) return -1;
429 return (Embryo_Cell)(p - s1);
433 _embryo_str_strchr(Embryo_Program *ep, Embryo_Cell *params)
437 /* params[1] = str */
439 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
440 STRGET(ep, s1, params[1]);
441 STRGET(ep, s2, params[2]);
442 if ((!s1) || (!s2)) return -1;
443 p = strchr(s1, s2[0]);
445 return (Embryo_Cell)(p - s1);
449 _embryo_str_strrchr(Embryo_Program *ep, Embryo_Cell *params)
453 /* params[1] = str */
455 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
456 STRGET(ep, s1, params[1]);
457 STRGET(ep, s2, params[2]);
458 if ((!s1) || (!s2)) return -1;
459 p = strrchr(s1, s2[0]);
461 return (Embryo_Cell)(p - s1);
464 /* functions used by the rest of embryo */
467 _embryo_str_init(Embryo_Program *ep)
469 embryo_program_native_call_add(ep, "atoi", _embryo_str_atoi);
470 embryo_program_native_call_add(ep, "fnmatch", _embryo_str_fnmatch);
471 embryo_program_native_call_add(ep, "strcmp", _embryo_str_strcmp);
472 embryo_program_native_call_add(ep, "strncmp", _embryo_str_strncmp);
473 embryo_program_native_call_add(ep, "strcpy", _embryo_str_strcpy);
474 embryo_program_native_call_add(ep, "strncpy", _embryo_str_strncpy);
475 embryo_program_native_call_add(ep, "strlen", _embryo_str_strlen);
476 embryo_program_native_call_add(ep, "strcat", _embryo_str_strcat);
477 embryo_program_native_call_add(ep, "strncat", _embryo_str_strncat);
478 embryo_program_native_call_add(ep, "strprep", _embryo_str_strprep);
479 embryo_program_native_call_add(ep, "strnprep", _embryo_str_strnprep);
480 embryo_program_native_call_add(ep, "strcut", _embryo_str_strcut);
481 embryo_program_native_call_add(ep, "snprintf", _embryo_str_snprintf);
482 embryo_program_native_call_add(ep, "strstr", _embryo_str_strstr);
483 embryo_program_native_call_add(ep, "strchr", _embryo_str_strchr);
484 embryo_program_native_call_add(ep, "strrchr", _embryo_str_strrchr);