~jb55/protoverse

ref: e127c848c0029d1f8521c6d1f81a68eb54e9d4a8 protoverse/src/protoverse.c -rw-r--r-- 4.2 KiB
e127c848William Casarin fixes 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "io.h"
#include "parse.h"
#include "describe.h"
#include "serve.h"
#include "client.h"
#include "wasm.h"
#include "resource.h"
#include "entity.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

#define MAX_ENTITIES 1048576
#define streq(a, b) strcmp(a,b) == 0

/*
static void print_all_cells(struct parser *parser)
{
	struct cell *cell;
	int i, j;
	int ncells;

	ncells = cursor_count(parser->cells, sizeof(struct cell));
	printf("ncells %d\n", ncells);
	for (i = 0; i < ncells; i++) {
		cell = get_cell(parser->cells, i);
		print_cell(parser->attributes, cell);
		printf("\n");

		for (j = 0; j < cell->n_children; j++) {
			cell = get_cell(parser->cells, cell->children[j]);
			assert(cell);
			printf("  ");
			print_cell(parser->attributes, cell);
			printf("\n");
		}
	}
}
*/

static int match_selector(struct cursor *attrs, struct cell *cell, const char *query)
{
	const char *str;
	int str_len;
	int query_len = strlen(query);

	if (!cell_attr_str(attrs, cell, &str, &str_len, A_NAME))
		return 0;
	if (query_len == str_len && !memcmp(str, query, str_len))
		return 1;
	if (!cell_attr_str(attrs, cell, &str, &str_len, A_ID))
		return 0;
	if (query_len == str_len && !memcmp(str, query, str_len))
		return 1;

	return 0;
}

static int cell_selector(struct parser *parser, u32 root, const char *query)
{
	int res;
	int i;
	struct cell *cell = get_cell(&parser->cells, root);

	if ((res = match_selector(&parser->attributes, cell, query)))
		return root;

	for (i = 0; i < cell->n_children; i++) {
		//cell = get_cell(&parser->cells, cell->children[i]);
		if ((res = cell_selector(parser, cell->children[i], query)))
			return res;
	}

	return 0;
}

static int print_cell_tree(struct parser *parser, u32 root, int depth)
{
	int i;


	struct cell *cell = get_cell(&parser->cells, root);
	if (!cell) {
		printf("no root cell...\n");
		return 0;
	}

	/*  sanity TODO: configurable max depth */
	if (depth > 255)
		return 0;

	for (i = 0; i < depth; i++) {
		printf("  ");
	}

	print_cell(&parser->attributes, cell);
	printf("\n");

	for (i = 0; i < cell->n_children; i++) {
		print_cell_tree(parser, cell->children[i], depth+1);
	}


	return 1;
}

static void init_protoverse_server(struct protoverse_server *server)
{
	(void)server;
	/*
	init_resource_manager(&server->env.entities, sizeof(struct entity),
			1024, MAX_ENTITIES, "entity");
			*/
}

static void free_protoverse_server(struct protoverse_server *server)
{
	(void)server;
	//destroy_resource_manager(&server->env.entities);
}

static int usage(void)
{
	printf("usage: protoverse <command> [args]\n\n");
	printf("   COMMANDS\n\n");
	printf("       parse file.space\n");
	printf("       serve file.space\n");
	printf("       client\n");
	printf("       run code.wasm\n");

	return 1;
}


extern char **environ;

int main(int argc, const char *argv[])
{
	static u8 buf[4096*8];
	char **env = environ;
	const char *space, *code_file;
	const char *cmd;
	unsigned char *wasm_data;
	struct parser parser;
	struct protoverse_server server;
	int root, found;
	int ok;
	int retval;
	size_t len;

	if (argc < 2)
		return usage();

	cmd = argv[1];

	if (streq(cmd, "parse")) {
		if (argc < 3)
			return usage();
		ok = init_parser(&parser);
		if (!ok) {
			printf("failed to initialize parser\n");
			return 1;
		}
		space = argv[2];
		ok = parse_file(&parser, space, &root, buf, sizeof(buf));
		if (!ok) {
			printf("failed to parse file\n");
			return 1;
		}

		if (argc > 3) {
			if ((found = cell_selector(&parser, root, argv[3])))
				root = found;
		}

		//print_cell_tree(&parser, root, 0);

		describe(&parser, root);
		free_parser(&parser);
	} else if (streq(cmd, "serve")) {
		if (argc != 3)
			return usage();
		space = argv[2];
		printf("serving protoverse on port 1988...\n");

		server.port = 1988;
		server.bind = "127.0.0.1";

		init_protoverse_server(&server);
		protoverse_serve(&server);
		free_protoverse_server(&server);
	} else if (streq(cmd, "client")) {
		protoverse_connect("127.0.0.1", 1988);
	} else if (streq(cmd, "run")) {
		if (argc < 3)
			return usage();
		code_file = argv[2];
		if (!map_file(code_file, &wasm_data, &len)) {
			perror("mmap");
			return 1;
		}
		run_wasm(wasm_data, len, argc - 2, argv + 2, env, &retval);
		munmap(wasm_data, len);
		return retval;
	}

	return 0;
}