コード例 #1
0
ファイル: generate.c プロジェクト: tanguband/tang
/*!
 * @brief ダンジョン生成のメインルーチン / Generate a new dungeon level
 * @details Note that "dun_body" adds about 4000 bytes of memory to the stack.
 * @return ダンジョン生成が全て無事に成功したらTRUEを返す。
 */
static bool cave_gen(void)
{
    int i, k, y, x;

    dun_data dun_body;

    /* Global data */
    dun = &dun_body;

    dun->destroyed = FALSE;
    dun->empty_level = FALSE;
    dun->cavern = FALSE;
    dun->laketype = 0;

    /* Fill the arrays of floors and walls in the good proportions */
    set_floor_and_wall(dungeon_type);

    /* Prepare allocation table */
    get_mon_num_prep(get_monster_hook(), NULL);

    /* Randomize the dungeon creation values */
    dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
    dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
    dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
    dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
    dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);

    /* Actual maximum number of rooms on this level */
    dun->row_rooms = cur_hgt / BLOCK_HGT;
    dun->col_rooms = cur_wid / BLOCK_WID;

    /* Initialize the room table */
    for (y = 0; y < dun->row_rooms; y++)
    {
        for (x = 0; x < dun->col_rooms; x++)
        {
            dun->room_map[y][x] = FALSE;
        }
    }

    /* No rooms yet */
    dun->cent_n = 0;

    /* Empty arena levels */
    if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
    {
        dun->empty_level = TRUE;

        if (cheat_room)
#ifdef JP
            msg_print("アリーナレベル");
#else
            msg_print("Arena level.");
#endif
    }

    if (dun->empty_level)
    {
        /* Start with floors */
        for (y = 0; y < cur_hgt; y++)
        {
            for (x = 0; x < cur_wid; x++)
            {
                place_floor_bold(y, x);
            }
        }

        /* Special boundary walls -- Top and bottom */
        for (x = 0; x < cur_wid; x++)
        {
            place_extra_bold(0, x);
            place_extra_bold(cur_hgt - 1, x);
        }

        /* Special boundary walls -- Left and right */
        for (y = 1; y < (cur_hgt - 1); y++)
        {
            place_extra_bold(y, 0);
            place_extra_bold(y, cur_wid - 1);
        }
    }
    else
    {
        /* Start with walls */
        for (y = 0; y < cur_hgt; y++)
        {
            for (x = 0; x < cur_wid; x++)
            {
                place_extra_bold(y, x);
            }
        }
    }


    /* Generate various caverns and lakes */
    gen_caverns_and_lakes();


    /* Build maze */
    if (d_info[dungeon_type].flags1 & DF1_MAZE)
    {
        build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);

        /* Place 3 or 4 down stairs near some walls */
        if (!alloc_stairs(feat_down_stair, rand_range(2, 3), 3)) return FALSE;

        /* Place 1 or 2 up stairs near some walls */
        if (!alloc_stairs(feat_up_stair, 1, 3)) return FALSE;
    }

    /* Build some rooms */
    else
    {
        int tunnel_fail_count = 0;

        /*
         * Build each type of room in turn until we cannot build any more.
         */
        if (!generate_rooms()) return FALSE;


        /* Make a hole in the dungeon roof sometimes at level 1 */
        if (dun_level == 1)
        {
            while (one_in_(DUN_MOS_DEN))
            {
                place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
            }
        }

        /* Destroy the level if necessary */
        if (dun->destroyed) destroy_level();

        /* Hack -- Add some rivers */
        if (one_in_(3) && (randint1(dun_level) > 5))
        {
            int feat1 = 0, feat2 = 0;

            /* Choose water or lava */
            if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
            {
                feat1 = feat_deep_water;
                feat2 = feat_shallow_water;
            }
            else if  (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
            {
                feat1 = feat_deep_lava;
                feat2 = feat_shallow_lava;
            }
            else feat1 = 0;

            if (feat1)
            {
                feature_type *f_ptr = &f_info[feat1];

                /* Only add river if matches lake type or if have no lake at all */
                if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
                        ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
                        !dun->laketype)
                {
                    add_river(feat1, feat2);
                }
            }
        }

        /* Hack -- Scramble the room order */
        for (i = 0; i < dun->cent_n; i++)
        {
            int ty, tx;
            int pick = rand_range(0, i);

            ty = dun->cent[i].y;
            tx = dun->cent[i].x;
            dun->cent[i].y = dun->cent[pick].y;
            dun->cent[i].x = dun->cent[pick].x;
            dun->cent[pick].y = ty;
            dun->cent[pick].x = tx;
        }

        /* Start with no tunnel doors */
        dun->door_n = 0;

        /* Hack -- connect the first room to the last room */
        y = dun->cent[dun->cent_n-1].y;
        x = dun->cent[dun->cent_n-1].x;

        /* Connect all the rooms together */
        for (i = 0; i < dun->cent_n; i++)
        {
            int j;

            /* Reset the arrays */
            dun->tunn_n = 0;
            dun->wall_n = 0;

            /* Connect the room to the previous room */
            if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
            {
                /* make cave-like tunnel */
                (void)build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
            }
            else
            {
                /* make normal tunnel */
                if (!build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x)) tunnel_fail_count++;
            }

            if (tunnel_fail_count >= 2) return FALSE;

            /* Turn the tunnel into corridor */
            for (j = 0; j < dun->tunn_n; j++)
            {
                cave_type *c_ptr;
                feature_type *f_ptr;

                /* Access the grid */
                y = dun->tunn[j].y;
                x = dun->tunn[j].x;

                /* Access the grid */
                c_ptr = &cave[y][x];
                f_ptr = &f_info[c_ptr->feat];

                /* Clear previous contents (if not a lake), add a floor */
                if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
                {
                    /* Clear mimic type */
                    c_ptr->mimic = 0;

                    place_floor_grid(c_ptr);
                }
            }

            /* Apply the piercings that we found */
            for (j = 0; j < dun->wall_n; j++)
            {
                cave_type *c_ptr;

                /* Access the grid */
                y = dun->wall[j].y;
                x = dun->wall[j].x;

                /* Access the grid */
                c_ptr = &cave[y][x];

                /* Clear mimic type */
                c_ptr->mimic = 0;

                /* Clear previous contents, add up floor */
                place_floor_grid(c_ptr);

                /* Occasional doorway */
                if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
                {
                    /* Place a random door */
                    place_random_door(y, x, TRUE);
                }
            }

            /* Remember the "previous" room */
            y = dun->cent[i].y;
            x = dun->cent[i].x;
        }

        /* Place intersection doors */
        for (i = 0; i < dun->door_n; i++)
        {
            /* Extract junction location */
            y = dun->door[i].y;
            x = dun->door[i].x;

            /* Try placing doors */
            try_door(y, x - 1);
            try_door(y, x + 1);
            try_door(y - 1, x);
            try_door(y + 1, x);
        }

        /* Place 3 or 4 down stairs near some walls */
        if (!alloc_stairs(feat_down_stair, rand_range(3, 4), 3)) return FALSE;

        /* Place 1 or 2 up stairs near some walls */
        if (!alloc_stairs(feat_up_stair, rand_range(1, 2), 3)) return FALSE;
    }

    if (!dun->laketype)
    {
        if (d_info[dungeon_type].stream2)
        {
            /* Hack -- Add some quartz streamers */
            for (i = 0; i < DUN_STR_QUA; i++)
            {
                build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
            }
        }

        if (d_info[dungeon_type].stream1)
        {
            /* Hack -- Add some magma streamers */
            for (i = 0; i < DUN_STR_MAG; i++)
            {
                build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
            }
        }
    }

    /* Special boundary walls -- Top and bottom */
    for (x = 0; x < cur_wid; x++)
    {
        set_bound_perm_wall(&cave[0][x]);
        set_bound_perm_wall(&cave[cur_hgt - 1][x]);
    }

    /* Special boundary walls -- Left and right */
    for (y = 1; y < (cur_hgt - 1); y++)
    {
        set_bound_perm_wall(&cave[y][0]);
        set_bound_perm_wall(&cave[y][cur_wid - 1]);
    }

    /* Determine the character location */
    if (!new_player_spot()) return FALSE;

    if (!place_quest_monsters()) return FALSE;

    /* Basic "amount" */
    k = (dun_level / 3);
    if (k > 10) k = 10;
    if (k < 2) k = 2;

    /* Pick a base number of monsters */
    i = d_info[dungeon_type].min_m_alloc_level;

    /* To make small levels a bit more playable */
    if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
    {
        int small_tester = i;

        i = (i * cur_hgt) / MAX_HGT;
        i = (i * cur_wid) / MAX_WID;
        i += 1;

        if (i > small_tester) i = small_tester;
        else if (cheat_hear)
        {
#ifdef JP
            msg_format("モンスター数基本値を %d から %d に減らします", small_tester, i);
#else
            msg_format("Reduced monsters base from %d to %d", small_tester, i);
#endif

        }
    }

    i += randint1(8);

    /* Put some monsters in the dungeon */
    for (i = i + k; i > 0; i--)
    {
        (void)alloc_monster(0, PM_ALLOW_SLEEP);
    }

    /* Place some traps in the dungeon */
    alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));

    /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
    if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));

    /* Mega Hack -- No object at first level of deeper dungeon */
    if (p_ptr->enter_dungeon && dun_level > 1)
    {
        /* No stair scum! */
        object_level = 1;
    }

    /* Put some objects in rooms */
    alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));

    /* Put some objects/gold in the dungeon */
    alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
    alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));

    /* Set back to default */
    object_level = base_level;

    /* Put the Guardian */
    if (!alloc_guardian(TRUE)) return FALSE;

    if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
    {
        /* Lite the cave */
        for (y = 0; y < cur_hgt; y++)
        {
            for (x = 0; x < cur_wid; x++)
            {
                cave[y][x].info |= (CAVE_GLOW);
            }
        }
    }

    return TRUE;
}
コード例 #2
0
ファイル: xml.c プロジェクト: HaxeFoundation/neko
static void do_parse_xml( const char *xml, const char **lp, int *line, value callb, const char *parentname ) {
	STATE state = BEGIN;
	STATE next = BEGIN;
	field aname = (field)0;
	value attribs = NULL;
	value nodename = NULL;
	const char *start = NULL;
	const char *p = *lp;
	char c = *p;
	int nsubs = 0, nbrackets = 0;
	while( c ) {
		switch( state ) {
		case IGNORE_SPACES:
			switch( c ) {
			case '\n':
			case '\r':
			case '\t':
			case ' ':
				break;
			default:
				state = next;
				continue;
			}
			break;
		case BEGIN:
			switch( c ) {
			case '<':
				state = IGNORE_SPACES;
				next = BEGIN_NODE;
				break;
			default:
				start = p;
				state = PCDATA;
				continue;
			}
			break;
		case PCDATA:
			if( c == '<' ) {
				val_ocall1(callb,id_pcdata,copy_string(start,p-start));
				nsubs++;
				state = IGNORE_SPACES;
				next = BEGIN_NODE;
			}
			break;
		case CDATA:
			if( c == ']' && p[1] == ']' && p[2] == '>' ) {
				val_ocall1(callb,id_cdata,copy_string(start,p-start));
				nsubs++;
				p += 2;
				state = BEGIN;
			}
			break;
		case BEGIN_NODE:
			switch( c ) {
			case '!':
				if( p[1] == '[' ) {
					p += 2;
					if( (p[0] != 'C' && p[0] != 'c') ||
						(p[1] != 'D' && p[1] != 'd') ||
						(p[2] != 'A' && p[2] != 'a') ||
						(p[3] != 'T' && p[3] != 't') ||
						(p[4] != 'A' && p[4] != 'a') ||
						(p[5] != '[') )
						ERROR("Expected <![CDATA[");
					p += 5;
					state = CDATA;
					start = p + 1;
					break;
				}
				if( p[1] == 'D' || p[1] == 'd' ) {
					if( (p[2] != 'O' && p[2] != 'o') ||
						(p[3] != 'C' && p[3] != 'c') ||
						(p[4] != 'T' && p[4] != 't') ||
						(p[5] != 'Y' && p[5] != 'y') ||
						(p[6] != 'P' && p[6] != 'p') ||
						(p[7] != 'E' && p[7] != 'e') )
						ERROR("Expected <!DOCTYPE");
					p += 7;
					state = DOCTYPE;
					start = p + 1;
					break;
				}
				if( p[1] != '-' || p[2] != '-' )
					ERROR("Expected <!--");
				p += 2;
				state = COMMENT;
				start = p + 1;
				break;
			case '?':
				state = HEADER;
				start = p;
				break;
			case '/':
				if( parentname == NULL )
					ERROR("Expected node name");
				start = p + 1;
				state = IGNORE_SPACES;
				next = CLOSE;
				break;
			default:
				state = TAG_NAME;
				start = p;
				continue;
			}
			break;
		case TAG_NAME:
			if( !is_valid_char(c) ) {
				if( p == start )
					ERROR("Expected node name");
				nodename = copy_string(start,p-start);
				attribs = alloc_object(NULL);
				state = IGNORE_SPACES;
				next = BODY;
				continue;
			}
			break;
		case BODY:
			switch( c ) {
			case '/':
				state = WAIT_END;
				nsubs++;
				val_ocall2(callb,id_xml,nodename,attribs);
				break;
			case '>':
				state = CHILDREN;
				nsubs++;
				val_ocall2(callb,id_xml,nodename,attribs);
				break;
			default:
				state = ATTRIB_NAME;
				start = p;
				continue;
			}
			break;
		case ATTRIB_NAME:
			if( !is_valid_char(c) ) {
				value tmp;
				if( start == p )
					ERROR("Expected attribute name");
				tmp = copy_string(start,p-start);
				aname = val_id(val_string(tmp));
				if( !val_is_null(val_field(attribs,aname)) )
					ERROR("Duplicate attribute");
				state = IGNORE_SPACES;
				next = EQUALS;
				continue;
			}
			break;
		case EQUALS:
			switch( c ) {
			case '=':
				state = IGNORE_SPACES;
				next = ATTVAL_BEGIN;
				break;
			default:
				ERROR("Expected =");
			}
			break;
		case ATTVAL_BEGIN:
			switch( c ) {
			case '"':
			case '\'':
				state = ATTRIB_VAL;
				start = p;
				break;
			default:
				ERROR("Expected \"");
			}
			break;
		case ATTRIB_VAL:
			if( c == *start ) {
				value aval = copy_string(start+1,p-start-1);
				alloc_field(attribs,aname,aval);
				state = IGNORE_SPACES;
				next = BODY;
			}
			break;
		case CHILDREN:
			*lp = p;
			do_parse_xml(xml,lp,line,callb,val_string(nodename));
			p = *lp;
			start = p;
			state = BEGIN;
			break;
		case WAIT_END:
			switch( c ) {
			case '>':
				val_ocall0(callb,id_done);
				state = BEGIN;
				break;
			default :
				ERROR("Expected >");
			}
			break;
		case WAIT_END_RET:
			switch( c ) {
			case '>':
				if( nsubs == 0 )
					val_ocall1(callb,id_pcdata,alloc_string(""));
				val_ocall0(callb,id_done);
				*lp = p;
				return;
			default :
				ERROR("Expected >");
			}
			break;
		case CLOSE:
			if( !is_valid_char(c) ) {
				if( start == p )
					ERROR("Expected node name");
				{
					value v = copy_string(start,p - start);
					if( strcmpi(parentname,val_string(v)) != 0 ) {
						buffer b = alloc_buffer("Expected </");
						buffer_append(b,parentname);
						buffer_append(b,">");
						ERROR(val_string(buffer_to_string(b)));
					}
				}
				state = IGNORE_SPACES;
				next = WAIT_END_RET;
				continue;
			}
			break;
		case COMMENT:
			if( c == '-' && p[1] == '-' && p[2] == '>' ) {
				val_ocall1(callb,id_comment,copy_string(start,p-start));
				p += 2;
				state = BEGIN;
			}
			break;
		case DOCTYPE:
			if( c == '[' )
				nbrackets++;
			else if( c == ']' )
				nbrackets--;
			else if( c == '>' && nbrackets == 0 ) {
				val_ocall1(callb,id_doctype,copy_string(start,p-start));
				state = BEGIN;
			}
			break;
		case HEADER:
			if( c == '?' && p[1] == '>' ) {
				p++;
				val_ocall1(callb,id_comment,copy_string(start,p-start));
				state = BEGIN;
			}
			break;
		}
		c = *++p;
		if( c == '\n' )
			(*line)++;
	}
	if( state == BEGIN ) {
		start = p;
		state = PCDATA;
	}
	if( parentname == NULL && state == PCDATA ) {
		if( p != start || nsubs == 0 )
			val_ocall1(callb,id_pcdata,copy_string(start,p-start));
		return;
	}
	ERROR("Unexpected end");
}
コード例 #3
0
ファイル: perf.c プロジェクト: 601040605/trinity
static int open_perf_fds(void)
{
	struct objhead *head;
	unsigned int i = 0;
	unsigned int perm_count = 0;
	unsigned int inval_count = 0;

	head = get_objhead(OBJ_GLOBAL, OBJ_FD_PERF);
	head->destroy = &perffd_destructor;

	while (i < MAX_PERF_FDS) {
		struct syscallrecord *rec;
		int fd;

		rec = &shm->children[0]->syscall;
		sanitise_perf_event_open(rec);

		fd = syscall(__NR_perf_event_open, rec->a1, rec->a2, rec->a3, rec->a4, rec->a5);
		if (fd != -1) {
			struct object *obj;

			obj = alloc_object();
			obj->perffd = fd;
			add_object(obj, OBJ_GLOBAL, OBJ_FD_PERF);

			output(2, "fd[%d] = perf\n", fd);
			i++;

			/* any time we succeed, reset the failure counts.
			 * They're only there for the cases where we hit them repeatedly.
			 */
			inval_count = 0;
			perm_count = 0;
		} else {
			switch (errno) {
			case ENOSYS:
				/* If ENOSYS, bail early rather than do MAX_PERF_FDS retries */
				return FALSE;

			case EINVAL:
				/* If we get here we probably generated something invalid and
				 * perf_event_open threw it out. Go around the loop again.
				 * OR its LXCore throwing us in an endless loop.
				 */
				inval_count++;
				break;

			case EACCES:
				perm_count++;
				break;
			}
		}

		if (perm_count > 1000) {
			output(2, "Couldn't open enough perf events, got EPERM too much. Giving up.\n");
			return FALSE;
		}

		if (inval_count > 10000) {
			output(2, "couldn't open enough perf events, got EINVAL too much. Giving up.\n");
			return FALSE;
		}

		if (shm->exit_reason != STILL_RUNNING)
			return FALSE;
	}

	return TRUE;
}
コード例 #4
0
ファイル: tmx_xml.c プロジェクト: NickLawsonDev/Allegro5
/* parse layers and objectgroups */
static int parse_layer(xmlTextReaderPtr reader, tmx_layer **layer_headadr, int map_h, int map_w, enum tmx_layer_type type, const char *filename) {
	tmx_layer *res;
	tmx_object *obj;
	int curr_depth;
	const char *name;
	char *value;
	
	curr_depth = xmlTextReaderDepth(reader);
	
	if (!(res = alloc_layer())) return 0;
	res->type = type;
	while(*layer_headadr) {
		layer_headadr = &((*layer_headadr)->next);
	}
	*layer_headadr = res;
	
	/* parses each attribute */
	if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"name"))) { /* name */
		res->name = value;
	} else {
		tmx_err(E_MISSEL, "xml parser: missing 'name' attribute in the 'layer' element");
		return 0;
	}
	
	if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"visible"))) { /* visible */
		res->visible = (char)atoi(value);
		tmx_free_func(value);
	}
	
	if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"opacity"))) { /* opacity */
		res->opacity = (float)strtod(value, NULL);
		tmx_free_func(value);
	}
	
	if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"color"))) { /* color */
		res->color = get_color_rgb(value);
		tmx_free_func(value);
	}
	
	do {
		if (xmlTextReaderRead(reader) != 1) return 0; /* error_handler has been called */
		
		if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
			name = (char*)xmlTextReaderConstName(reader);
			if (!strcmp(name, "properties")) {
				if (!parse_properties(reader, &(res->properties))) return 0;
			} else if (!strcmp(name, "data")) {
				if (!parse_data(reader, &(res->content.gids), map_h * map_w)) return 0;
			} else if (!strcmp(name, "image")) {
				if (!parse_image(reader, &(res->content.image), 0, filename)) return 0;
			} else if (!strcmp(name, "object")) {
				if (!(obj = alloc_object())) return 0;
				
				obj->next = res->content.head;
				res->content.head = obj;
				
				if (!parse_object(reader, obj)) return 0;
			} else {
				/* Unknow element, skipping it's tree */
				if (xmlTextReaderNext(reader) != 1) return 0;
			}
		}
	} while (xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT ||
	         xmlTextReaderDepth(reader) != curr_depth);
	
	return 1;
}
コード例 #5
0
/* accept a socket (creates a new fd) */
static struct sock *accept_socket( obj_handle_t handle )
{
    struct sock *acceptsock;
    struct sock *sock;
    int	acceptfd;
    struct sockaddr	saddr;

    sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
    if (!sock)
    	return NULL;

    if ( sock->deferred )
    {
        acceptsock = sock->deferred;
        sock->deferred = NULL;
    }
    else
    {

        /* Try to accept(2). We can't be safe that this an already connected socket
         * or that accept() is allowed on it. In those cases we will get -1/errno
         * return.
         */
        unsigned int slen = sizeof(saddr);
        acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
        if (acceptfd==-1)
        {
            sock_set_error();
            release_object( sock );
            return NULL;
        }
        if (!(acceptsock = alloc_object( &sock_ops )))
        {
            close( acceptfd );
            release_object( sock );
            return NULL;
        }

        /* newly created socket gets the same properties of the listening socket */
        fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
        acceptsock->state  = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
        if (sock->state & FD_WINE_NONBLOCKING)
            acceptsock->state |= FD_WINE_NONBLOCKING;
        acceptsock->mask    = sock->mask;
        acceptsock->hmask   = 0;
        acceptsock->pmask   = 0;
        acceptsock->polling = 0;
        acceptsock->type    = sock->type;
        acceptsock->family  = sock->family;
        acceptsock->event   = NULL;
        acceptsock->window  = sock->window;
        acceptsock->message = sock->message;
        acceptsock->wparam  = 0;
        if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
        acceptsock->flags = sock->flags;
        acceptsock->deferred = NULL;
        acceptsock->read_q  = NULL;
        acceptsock->write_q = NULL;
        memset( acceptsock->errors, 0, sizeof(acceptsock->errors) );
        if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
                                                    get_fd_options( sock->fd ) )))
        {
            release_object( acceptsock );
            release_object( sock );
            return NULL;
        }
    }
    clear_error();
    sock->pmask &= ~FD_ACCEPT;
    sock->hmask &= ~FD_ACCEPT;
    sock_reselect( sock );
    release_object( sock );
    return acceptsock;
}
コード例 #6
0
ファイル: builtins.c プロジェクト: pfalcon-mirrors/nekovm
/**
	$new : object? -> object
	<doc>Return a copy of the object or a new object if [null]</doc>
**/
static value builtin_new( value o ) {
	if( !val_is_null(o) && !val_is_object(o) )
		neko_error();
	return alloc_object(o);
}
コード例 #7
0
ファイル: builtins.c プロジェクト: pfalcon-mirrors/nekovm
void neko_init_builtins() {
	neko_builtins = alloc_root(2);
	neko_builtins[0] = alloc_object(NULL);
	neko_builtins[1] = alloc_function(builtin_print,VAR_ARGS,"$print");

	BUILTIN(print,VAR_ARGS);

	BUILTIN(array,VAR_ARGS);
	BUILTIN(amake,1);
	BUILTIN(acopy,1);
	BUILTIN(asize,1);
	BUILTIN(asub,3);
	BUILTIN(ablit,5);
	BUILTIN(aconcat,1);

	BUILTIN(smake,1);
	BUILTIN(ssize,1);
	BUILTIN(scopy,1);
	BUILTIN(ssub,3);
	BUILTIN(sget,2);
	BUILTIN(sset,3);
	BUILTIN(sblit,5);
	BUILTIN(sfind,3);

	BUILTIN(new,1);
	BUILTIN(objget,2);
	BUILTIN(objset,3);
	BUILTIN(objcall,3);
	BUILTIN(objfield,2);
	BUILTIN(objremove,2);
	BUILTIN(objfields,1);
	BUILTIN(hash,1);
	BUILTIN(fasthash,1);
	BUILTIN(field,1);
	BUILTIN(objsetproto,2);
	BUILTIN(objgetproto,1);

	BUILTIN(int,1);
	BUILTIN(float,1);
	BUILTIN(string,1);
	BUILTIN(typeof,1);
	BUILTIN(closure,VAR_ARGS);
	BUILTIN(apply,VAR_ARGS);
	BUILTIN(varargs,1);
	BUILTIN(compare,2);
	BUILTIN(pcompare,2);
	BUILTIN(not,1);
	BUILTIN(throw,1);
	BUILTIN(rethrow,1);
	BUILTIN(nargs,1);
	BUILTIN(call,3);
	BUILTIN(isnan,1);
	BUILTIN(isinfinite,1);
	BUILTIN(istrue,1);

	BUILTIN(getkind,1);
	BUILTIN(iskind,2);

	BUILTIN(hnew,1);
	BUILTIN(hget,3);
	BUILTIN(hmem,3);
	BUILTIN(hset,4);
	BUILTIN(hadd,3);
	BUILTIN(hremove,3);
	BUILTIN(hresize,2);
	BUILTIN(hkey,1);
	BUILTIN(hcount,1);
	BUILTIN(hsize,1);
	BUILTIN(hiter,2);

	BUILTIN(iadd,2);
	BUILTIN(isub,2);
	BUILTIN(imult,2);
	BUILTIN(idiv,2);

	BUILTIN(excstack,0);
	BUILTIN(callstack,0);
	BUILTIN(version,0);
	BUILTIN(setresolver,1);
}