Ejemplo n.º 1
0
Archivo: async.c Proyecto: Elohim/FGmud
void f_async_db_exec(){
	array_t *info;
	db_t *db;
	info = allocate_empty_array(1);
	info->item[0].type = T_STRING;
	info->item[0].subtype = STRING_MALLOC;
	info->item[0].u.string = string_copy((sp-1)->u.string, "f_db_exec");
	int num_arg = st_num_arg;
	valid_database("exec", info);

	db = find_db_conn((sp-2)->u.number);
	if (!db) {
		error("Attempt to exec on an invalid database handle\n");
	}
	if(!db_mut){
		db_mut = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
		pthread_mutex_init(db_mut, NULL);
	}
	st_num_arg = num_arg;
	function_to_call_t *cb = get_cb();
	process_efun_callback(2, cb, F_ASYNC_DB_EXEC);
	cb->f.fp->hdr.ref++;

	add_db_exec((sp-2)->u.number, cb);
	pop_3_elems();
}
Ejemplo n.º 2
0
void c_parse_command(int  num_arg) {
    svalue_t *arg;
    svalue_t *fp;
    int i;

    /*
     * type checking on first three required parameters to parse_command()
     */
    arg = sp - 2;
    CHECK_TYPES(&arg[0], T_STRING, 1, F_PARSE_COMMAND);
    CHECK_TYPES(&arg[1], T_OBJECT | T_ARRAY, 2, F_PARSE_COMMAND);
    CHECK_TYPES(&arg[2], T_STRING, 3, F_PARSE_COMMAND);

    /*
     * allocate stack frame for rvalues and return value (number of matches);
     * perform some stack manipulation;
     */
    fp = sp;
    CHECK_STACK_OVERFLOW(num_arg + 1);
    sp += num_arg + 1;
    arg = sp;
    *(arg--) = *(fp--);		/* move pattern to top of stack */
    *(arg--) = *(fp--);		/* move source object or array to just below 
				   the pattern */
    *(arg) = *(fp);		/* move source string just below the object */
    fp->type = T_NUMBER;

    /*
     * prep area for rvalues
     */
    for (i = 1; i <= num_arg; i++)
	fp[i].type = T_INVALID;

    /*
     * do it...
     */
    i = parse(arg[0].u.string, &arg[1], arg[2].u.string, &fp[1], num_arg);

    /*
     * remove mandatory parameters
     */
    pop_3_elems();

    /*
     * save return value on stack
     */
    fp->u.number = i;
}
Ejemplo n.º 3
0
void
f_socket_connect (void)
{
    int i, fd, port;
    char addr[ADDR_BUF_SIZE];

    if (!((sp - 1)->type & (T_FUNCTION | T_STRING))) {
	bad_arg(3, F_SOCKET_CONNECT);
    }
    if (!(sp->type & (T_FUNCTION | T_STRING))) {
	bad_arg(4, F_SOCKET_CONNECT);
    }
    fd = (sp - 3)->u.number;
    get_socket_address(fd, addr, &port, 0);

    if (!strcmp(addr, "0.0.0.0") && port == 0) {
	/*
	 * socket descriptor is not bound yet
	 */
	char *s;
	int start = 0;

	addr[0] = '\0';
	if ((s = strchr((sp - 2)->u.string, ' '))) {
	    /*
	     * use specified address and port
	     */
	    i = s - (sp - 2)->u.string;
	    if (i > ADDR_BUF_SIZE - 1) {
		start = i - ADDR_BUF_SIZE - 1;
		i = ADDR_BUF_SIZE - 1;
	    }
	    strncat(addr, (sp - 2)->u.string + start, i);
	    port = atoi(s + 1);
	}
#ifdef DEBUG
    } else {
	fprintf(stderr, "socket_connect: socket already bound to address/port: %s/%d\n",
		addr, port);
	fprintf(stderr, "socket_connect: requested on: %s\n", (sp - 2)->u.string);
#endif
    }

    (sp-3)->u.number = VALID_SOCKET("connect") ?
      socket_connect(fd, (sp - 2)->u.string, sp - 1, sp) : EESECURITY;
    pop_3_elems();
}
Ejemplo n.º 4
0
void
f_socket_acquire (void)
{
    int fd, port;
    char addr[ADDR_BUF_SIZE];

    if (!((sp - 1)->type & (T_FUNCTION | T_STRING))) {
	bad_arg(3, F_SOCKET_ACQUIRE);
    }
    if (!(sp->type & (T_FUNCTION | T_STRING))) {
	bad_arg(4, F_SOCKET_ACQUIRE);
    }
    fd = (sp - 3)->u.number;
    get_socket_address(fd, addr, &port, 0);

    (sp-3)->u.number = VALID_SOCKET("acquire") ?
      socket_acquire((sp - 3)->u.number, (sp - 2),
		     (sp - 1), sp) : EESECURITY;

    pop_3_elems();
}
Ejemplo n.º 5
0
void f_pcre_assoc(void)
{
	svalue_t *arg;
	array_t *vec;

	arg = sp - st_num_arg + 1;

	if ((arg + 2)->type != T_ARRAY)
		error("Bad argument 3 to pcre_assoc()\n");

	vec = pcre_assoc(arg, (arg+1)->u.arr, (arg+2)->u.arr,
			st_num_arg > 3 ? (arg+3) : &const0);

	if (st_num_arg == 4)
		pop_3_elems();
	else
		pop_2_elems();

	free_string_svalue(sp);

	sp->type  = T_ARRAY;
	sp->u.arr = vec;
}
Ejemplo n.º 6
0
void f_pcre_replace(void)
{
	pcre_t *run;
	array_t *replacements;

	unsigned int i;
	char *ret;

	run = CALLOCATE(1, pcre_t, TAG_TEMPORARY, "f_pcre_replace: run");

	run->ovector = NULL;
	run->ovecsize = 0;
	assign_svalue_no_free(&run->pattern, (sp - 1));
	run->subject = (sp - 2)->u.string;
	replacements = sp->u.arr;

	run->s_length = SVALUE_STRLEN(sp - 2);


	if(pcre_magic(run) < 0)
	{
		pcre_free_memory(run);
		error("PCRE compilation failed at offset %d: %s\n", run->erroffset,
				run->error);
	}


	if (run->rc < 0) /* No match. could do handling of matching errors if wanted */
	{
		pcre_free_memory(run);
		pop_2_elems();
		return;
	}


	if (run->rc > (run->ovecsize/3-1))
	{
		pcre_free_memory(run);
		error("Too many substrings.\n");
	}
	if ( (run->rc - 1) != replacements->size )
	{
		int tmp = run->rc-1;
		pcre_free_memory(run);
		error("Number of captured substrings and replacements do not match, "
				"%d vs %d.\n", tmp, replacements->size);
	}



	if (run->rc == 1)
	{
		/* No captured substrings, return subject */
		pcre_free_memory(run);
		pop_2_elems();
		return;
		//push_malloced_string(run->subject);
	}

	ret = pcre_get_replace(run, replacements);


	pop_3_elems();
	push_malloced_string(ret);
	pcre_free_memory(run);
	return;
}