Esempio n. 1
0
re_str_t 
nstring(const u_char *str, ...){
	u_char	buf[RE_STR_BUFF_SIZE], *p, *last, *value;
	va_list		args;
	last = buf + RE_STR_BUFF_SIZE;
	p = buf;
	while(*str && buf < last){
			*p++ = *str++;
	}
	va_start(args, str);
	while((value = va_arg(args, u_char *)) && *value != '\0'){
		while(*value && buf < last){
			*p++ = *value++;
		}
	}
	va_end(args);
	*p = '\0';
	return (re_str_t)re_string(buf);
}
Esempio n. 2
0
int main(int argc, char *argv[], char *env[])
{

    // initialize runtime
    re_main(argc, argv, env);

    // strings
    obj_t mystr = re_string("this is now a php string");
    re_var_dump(mystr);

    printf("is mystr a string? %s\n", (re_is_string(mystr) ? "yes" : "no"));

    // numbers
    obj_t myfloat = re_float(1.2345);
    re_var_dump(myfloat);

    printf("is myfloat a number? %s\n", (re_is_number(myfloat) ? "yes" : "no"));
    printf("is myfloat a float? %s\n", (re_is_float(myfloat) ? "yes" : "no"));
    printf("is myfloat an int? %s\n", (re_is_int(myfloat) ? "yes" : "no"));

    obj_t myint = re_int(-2312);
    re_var_dump(myint);

    printf("is myint a number? %s\n", (re_is_number(myint) ? "yes" : "no"));
    printf("is myint a float? %s\n", (re_is_float(myint) ? "yes" : "no"));
    printf("is myint an int? %s\n", (re_is_int(myint) ? "yes" : "no"));

    // bool
    obj_t mybool = PHP_TRUE;
    re_var_dump(mybool);

    mybool = PHP_FALSE;
    re_var_dump(mybool);

    // php hash
    obj_t myhash = re_make_php_hash();

    re_php_hash_insert_cstr(myhash, "key1", "val1");
    re_php_hash_insert_cstr(myhash, "key2", "val2");

    obj_t myval = re_string("some data");
    re_php_hash_insert(myhash, re_int(12), myval);

    obj_t nhash = re_make_php_hash();
    re_php_hash_insert_cstr(nhash, "nested", "data");
    re_php_hash_insert(myhash, re_string("my nested hash"), nhash);

    re_var_dump(myhash);

    printf("is myhash a hash? %s\n", (re_is_php_hash(myhash) ? "yes" : "no"));

    // funcalls
    obj_t retval = re_funcall("strlen", re_list_1(mystr));
    re_var_dump(retval);

    retval = re_funcall("sizeof", re_list_1(myhash));
    re_var_dump(retval);

    // dump the output of a call to strpos with 2 arguments: mystr and "now"
    re_var_dump(re_funcall("strpos", re_list_2(mystr, re_string("now"))));

}
Esempio n. 3
0
void
child_deliver_cmd_hook(pid_t pid, struct account *a, unused struct msg *msg,
    struct child_deliver_data *data, int *result)
{
	struct mail_ctx			*mctx = data->mctx;
	struct mail			*m = data->mail;
	struct match_command_data	*cmddata = data->cmddata;
	int				 flags, status, found = 0;
	char				*s, *cause, *lbuf, *out, *err, tag[24];
	size_t				 llen;
	struct cmd		 	*cmd = NULL;
	struct rmlist			 rml;
	u_int				 i;

	/* If this is the parent, do nothing. */
	if (pid != 0) {
		xfree(mctx);
		return;
	}

	/* Sort out the command. */
	s = replacepath(
	    &cmddata->cmd, m->tags, m, &m->rml, find_tag(m->tags, "home"));
        if (s == NULL || *s == '\0') {
		log_warnx("%s: empty command", a->name);
		goto error;
        }

	log_debug2("%s: %s: started (ret=%d re=%s)", a->name, s, cmddata->ret,
	    cmddata->re.str == NULL ? "none" : cmddata->re.str);
	flags = CMD_ONCE;
	if (cmddata->pipe)
		flags |= CMD_IN;
	if (cmddata->re.str != NULL)
		flags |= CMD_OUT;
	cmd = cmd_start(s, flags, m->data, m->size, &cause);
	if (cmd == NULL) {
		log_warnx("%s: %s: %s", a->name, s, cause);
		goto error;
	}

	llen = IO_LINESIZE;
	lbuf = xmalloc(llen);

	for (;;) {
		/* Stop early if looking for regexp only. */
		if (found && cmddata->ret == -1) {
			log_debug3("%s: %s: found. stopping early", a->name, s);
			status = 1;
			break;
		}

		status = cmd_poll(
		    cmd, &out, &err, &lbuf, &llen, conf.timeout, &cause);
		if (status == -1) {
			log_warnx("%s: %s: %s", a->name, s, cause);
			goto error;
		}
       		if (status != 0)
			break;
		if (err != NULL)
			log_warnx("%s: %s: %s", a->name, s, err);
		if (out == NULL)
			continue;
		log_debug3("%s: %s: out: %s", a->name, s, out);
		if (found)
			continue;

		found = re_string(&cmddata->re, out, &rml, &cause);
		if (found == -1) {
			log_warnx("%s: %s", a->name, cause);
			goto error;
		}
		if (found != 1)
			continue;
		/* Save the matches. */
		if (!rml.valid)
			continue;
		for (i = 0; i < NPMATCH; i++) {
			if (!rml.list[i].valid)
				break;
			xsnprintf(tag, sizeof tag, "command%u", i);
			add_tag(&m->tags, tag, "%.*s", (int) (rml.list[i].eo -
			    rml.list[i].so), out + rml.list[i].so);
		}
	}
	status--;

	log_debug2("%s: %s: returned %d, found %d", a->name, s, status, found);

	cmd_free(cmd);
	xfree(s);
	xfree(lbuf);

	status = cmddata->ret == status;
	if (cmddata->ret != -1 && cmddata->re.str != NULL)
		*result = (found && status) ? MATCH_TRUE : MATCH_FALSE;
	else if (cmddata->ret != -1 && cmddata->re.str == NULL)
		*result = status ? MATCH_TRUE : MATCH_FALSE;
	else if (cmddata->ret == -1 && cmddata->re.str != NULL)
		*result = found ? MATCH_TRUE : MATCH_FALSE;
	else
		*result = MATCH_ERROR;
	return;

error:
	if (cause != NULL)
		xfree(cause);
	if (cmd != NULL)
		cmd_free(cmd);
	if (s != NULL)
		xfree(s);
	if (lbuf != NULL)
		xfree(lbuf);
	*result = MATCH_ERROR;
}