Diff

Differences From Artifact [a50f6e796a]:

To Artifact [c64f7b2f94]:




1

2
3

4
5
6
7
8








9
10
11
12
13
14
15









































































































































































































































































































































































































16
17
18
19
20
21
22


#include <string.h>

#include <stdio.h>
#include <ctype.h>


struct options {
	char *name;
	char *directory;
};









enum xvfs_minirivet_mode {
	XVFS_MINIRIVET_MODE_COPY,
	XVFS_MINIRIVET_MODE_TCL,
	XVFS_MINIRIVET_MODE_TCL_PRINT
};










































































































































































































































































































































































































static int parse_options(int argc, char **argv, struct options *options) {
	char *arg;
	char **option;
	int idx;
	int retval;

	for (idx = 0; idx < argc; idx++) {
>
>

>


>





>
>
>
>
>
>
>
>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>

struct options {
	char *name;
	char *directory;
};

struct xvfs_state {
	char **children;
	unsigned long child_count;
	unsigned long child_len;
	int bucket_count;
	int max_index;
};

enum xvfs_minirivet_mode {
	XVFS_MINIRIVET_MODE_COPY,
	XVFS_MINIRIVET_MODE_TCL,
	XVFS_MINIRIVET_MODE_TCL_PRINT
};

/*
 * adler32() function from zlib 1.1.4 and under the same license
 */
static unsigned long adler32(unsigned long adler, const unsigned char *buf, unsigned int len) {
	const int len_max = 5552;
	const unsigned long base = 65521;
	unsigned long s1 = adler & 0xffff;
	unsigned long s2 = (adler >> 16) & 0xffff;
	int k, i;

	if (buf == NULL) {
		return(1UL);
	}

	while (len > 0) {
		k = len < len_max ? len : len_max;
		len -= k;

		while (k >= 16) {
			for (i = 0; i < 16; i++) {
				s2 += buf[i];
			}
			s1 = buf[15];

			buf += 16;
			k   -= 16;
		}

		if (k != 0) {
			do {
				s1 += *buf++;
				s2 += s1;
			} while (--k);
		}

		s1 %= base;
		s2 %= base;
	}

	return((s2 << 16) | s1);
}

/*
 * Handle XVFS Rivet template file substitution
 */
static void parse_xvfs_minirivet_file(FILE *outfp, const char * const external_file_name, const char * const internal_file_name) {
	FILE *fp;
	unsigned long file_size;
	unsigned char buf[10];
	size_t item_count;
	int idx;

	fp = fopen(external_file_name, "rb");
	if (!fp) {
		return;
	}

	fprintf(outfp, "\t{\n");
	fprintf(outfp, "\t\t.name = \"%s\",\n", internal_file_name);
	fprintf(outfp, "\t\t.type = XVFS_FILE_TYPE_REG,\n");
	fprintf(outfp, "\t\t.data.fileContents = (const unsigned char *) \"");

	file_size = 0;
	while (1) {
		item_count = fread(&buf, 1, sizeof(buf), fp);
		if (item_count <= 0) {
			break;
		}

		if (file_size != 0) {
			fprintf(outfp, "\n\t\t\t\"");
		}

		for (idx = 0; idx < item_count; idx++) {
			fprintf(outfp, "\\x%02x", (int) buf[idx]);
		}
		fprintf(outfp, "\"");

		file_size += item_count;
	}

	fclose(fp);

	fprintf(outfp, ",\n");
	fprintf(outfp, "\t\t.size = %lu\n", file_size);
	fprintf(outfp, "\t},\n");
}

static void parse_xvfs_minirivet_directory(FILE *outfp, struct xvfs_state *xvfs_state, const char * const directory, const char * const prefix) {
	const unsigned int max_path_len = 8192, max_children = 65536;
	unsigned long child_idx, child_count;
	DIR *dp;
	struct dirent *file_info;
	struct stat file_stat;
	char *full_path_buf;
	char *rel_path_buf;
	char **children;
	int stat_ret;
	int snprintf_ret;

	dp = opendir(directory);
	if (!dp) {
		return;
	}

	full_path_buf = malloc(max_path_len);
	rel_path_buf = malloc(max_path_len);
	children = malloc(sizeof(*children) * max_children);

	child_idx = 0;
	while (1) {
		file_info = readdir(dp);
		if (!file_info) {
			break;
		}

		if (strcmp(file_info->d_name, ".") == 0) {
			continue;
		}

		if (strcmp(file_info->d_name, "..") == 0) {
			continue;
		}

		snprintf_ret = snprintf(full_path_buf, max_path_len, "%s/%s", directory, file_info->d_name);
		if (snprintf_ret >= max_path_len) {
			continue;
		}

		snprintf_ret = snprintf(rel_path_buf, max_path_len, "%s%s%s",
			prefix,
			strcmp(prefix, "") == 0 ? "" : "/",
			file_info->d_name
		);
		if (snprintf_ret >= max_path_len) {
			continue;
		}

		stat_ret = stat(full_path_buf, &file_stat);
		if (stat_ret != 0) {
			continue;
		}

		children[child_idx] = strdup(file_info->d_name);
		child_idx++;

		if (S_ISDIR(file_stat.st_mode)) {
			parse_xvfs_minirivet_directory(outfp, xvfs_state, full_path_buf, rel_path_buf);
		} else {
			parse_xvfs_minirivet_file(outfp, full_path_buf, rel_path_buf);

			xvfs_state->children[xvfs_state->child_count] = strdup(rel_path_buf);
			xvfs_state->child_count++;
		}
	}
	free(full_path_buf);
	free(rel_path_buf);

	child_count = child_idx;

	fprintf(outfp, "\t{\n");
	fprintf(outfp, "\t\t.name = \"%s\",\n", prefix);
	fprintf(outfp, "\t\t.type = XVFS_FILE_TYPE_DIR,\n");
	fprintf(outfp, "\t\t.size = %lu,\n", child_count);
	fprintf(outfp, "\t\t.data.dirChildren  = (const char *[]) {");
	for (child_idx = 0; child_idx < child_count; child_idx++) {
		if (child_idx != 0) {
			fprintf(outfp, ", ");
		}

		fprintf(outfp, "\"%s\"", children[child_idx]);

		free(children[child_idx]);
	}
	fprintf(outfp, "}\n");

	free(children);

	fprintf(outfp, "\t},\n");

	xvfs_state->children[xvfs_state->child_count] = strdup(prefix);
	xvfs_state->child_count++;

	closedir(dp);

	return;
}

static void parse_xvfs_minirivet_hashtable_header(FILE *outfp, struct xvfs_state *xvfs_state) {
	const int max_bucket_count = 30;
	int bucket_count;
	int idx1, idx2;
	int check_hash;
	int first_entry;

	if (xvfs_state->child_count > max_bucket_count) {
		bucket_count = max_bucket_count;
	} else {
		bucket_count = xvfs_state->child_count;
	}
	xvfs_state->bucket_count = bucket_count;
	xvfs_state->max_index = xvfs_state->child_count;

	fprintf(outfp, "\tlong pathIndex_idx;\n");
	fprintf(outfp, "\tint pathIndex_hash;\n");

	/*
	 * XXX:TODO: Make this not O(n^2)
	 */
	for (idx1 = 0; idx1 < bucket_count; idx1++) {
		fprintf(outfp, "\tstatic const long pathIndex_hashTable_%i[] = {\n", idx1);
		fprintf(outfp, "\t\t");
		first_entry = 1;

		for (idx2 = 0; idx2 < xvfs_state->child_count; idx2++) {
			check_hash = adler32(0, xvfs_state->children[idx2], strlen(xvfs_state->children[idx2])) % bucket_count;
			if (check_hash != idx1) {
				continue;
			}

			if (!first_entry) {
				fprintf(outfp, ", ");
			}
			first_entry = 0;

			if (check_hash == idx1) {
				fprintf(outfp, "%i", idx2);
			}
		}

		if (!first_entry) {
			fprintf(outfp, ", ");
		}
		fprintf(outfp, "XVFS_NAME_LOOKUP_ERROR");

		fprintf(outfp, "\n");

		fprintf(outfp, "\t};\n");
	}

	fprintf(outfp, "\tstatic const long * const pathIndex_hashTable[%i] = {\n", bucket_count);
	for (idx1 = 0; idx1 < bucket_count; idx1++) {
		fprintf(outfp, "\t\tpathIndex_hashTable_%i,\n", idx1);
	}
	fprintf(outfp, "\t};\n");
	return;
}

static void parse_xvfs_minirivet_hashtable_body(FILE *outfp, struct xvfs_state *xvfs_state) {
	fprintf(outfp, "\tpathIndex_hash = Tcl_ZlibAdler32(0, (unsigned char *) path, pathLen) %% %i;\n", xvfs_state->bucket_count);
	fprintf(outfp, "\tfor (pathIndex_idx = 0; pathIndex_idx < %i; pathIndex_idx++) {\n", xvfs_state->max_index);
	fprintf(outfp, "\t\tpathIndex = pathIndex_hashTable[pathIndex_hash][pathIndex_idx];\n");
	fprintf(outfp, "\t\tif (pathIndex == XVFS_NAME_LOOKUP_ERROR) {\n");
	fprintf(outfp, "\t\t\tbreak;\n");
	fprintf(outfp, "\t\t}\n");
	fprintf(outfp, "\n");
	fprintf(outfp, "\t\tif (strcmp(path, xvfs_example_data[pathIndex].name) == 0) {\n");
	fprintf(outfp, "\t\t\treturn(pathIndex);\n");
	fprintf(outfp, "\t\t}\n");
	fprintf(outfp, "\t}\n");
	return;
}

static void parse_xvfs_minirivet_handle_tcl_print(FILE *outfp, const struct options * const options, struct xvfs_state *xvfs_state, char *command) {
	char *buffer_p, *buffer_e;

	buffer_p = command;
	while (*buffer_p && isspace(*buffer_p)) {
		buffer_p++;
	}

	buffer_e = buffer_p + strlen(buffer_p) - 1;
	while (buffer_e >= buffer_p && isspace(*buffer_e)) {
		*buffer_e = '\0';
		buffer_e--;
	}

	if (strcmp(buffer_p, "$::xvfs::fsName") == 0) {
		fprintf(outfp, "%s", options->name);
	} else if (strcmp(buffer_p, "$::xvfs::fileInfoStruct") == 0) {
		fprintf(outfp, "static const struct xvfs_file_data xvfs_");
		fprintf(outfp, "%s", options->name);
		fprintf(outfp, "_data[] = {\n");
		parse_xvfs_minirivet_directory(outfp, xvfs_state, options->directory, "");
		fprintf(outfp, "};\n");
	} else if (strcmp(buffer_p, "[zlib adler32 $::xvfs::fsName 0]") == 0) {
		fprintf(outfp, "%lu", adler32(0, (unsigned char *) options->name, strlen(options->name)));
	} else if (strcmp(buffer_p, "$hashTableHeader") == 0) {
		parse_xvfs_minirivet_hashtable_header(outfp, xvfs_state);
	} else if (strcmp(buffer_p, "[dict get $hashTable body]") == 0) {
		parse_xvfs_minirivet_hashtable_body(outfp, xvfs_state);
	} else {
		fprintf(outfp, "@INVALID@%s@INVALID@", buffer_p);
	}

	return;
}

static int parse_xvfs_minirivet(FILE *outfp, const char * const file, const struct options * const options) {
	struct xvfs_state xvfs_state;
	FILE *fp;
	int ch, ch_buf;
	char tcl_buffer[8192], *tcl_buffer_p;
	enum xvfs_minirivet_mode mode;

	fp = fopen(file, "r");
	if (!fp) {
		return(0);
	}

	xvfs_state.child_count = 0;
	xvfs_state.child_len   = 65536;
	xvfs_state.children    = malloc(sizeof(*xvfs_state.children) * xvfs_state.child_len);

#define parse_xvfs_minirivet_getbyte(var) var = fgetc(fp); if (var == EOF) { break; }

	mode = XVFS_MINIRIVET_MODE_COPY;
	while (1) {
		parse_xvfs_minirivet_getbyte(ch);

		switch (mode) {
			case XVFS_MINIRIVET_MODE_COPY:
				if (ch == '<') {
					parse_xvfs_minirivet_getbyte(ch_buf);
					if (ch_buf != '?') {
						fputc('<', outfp);
						ch = ch_buf;

						break;
					}

					tcl_buffer_p = tcl_buffer;
					parse_xvfs_minirivet_getbyte(ch_buf);
					if (ch_buf == '=') {
						mode = XVFS_MINIRIVET_MODE_TCL_PRINT;
					} else {
						mode = XVFS_MINIRIVET_MODE_TCL;
						*tcl_buffer_p = ch_buf;
						tcl_buffer_p++;
					}
					*tcl_buffer_p = '\0';
					continue;
				}
				break;
			case XVFS_MINIRIVET_MODE_TCL:
			case XVFS_MINIRIVET_MODE_TCL_PRINT:
				if (ch == '?') {
					parse_xvfs_minirivet_getbyte(ch_buf);
					if (ch_buf != '>') {
						*tcl_buffer_p = ch;
						tcl_buffer_p++;

						ch = ch_buf;
						
						break;
					}

					*tcl_buffer_p = '\0';

					if (mode == XVFS_MINIRIVET_MODE_TCL_PRINT) {
						parse_xvfs_minirivet_handle_tcl_print(outfp, options, &xvfs_state, tcl_buffer);
					}

					mode = XVFS_MINIRIVET_MODE_COPY;
					continue;
				}
				break;
		}

		switch (mode) {
			case XVFS_MINIRIVET_MODE_COPY:
				fputc(ch, outfp);
				break;
			case XVFS_MINIRIVET_MODE_TCL:
			case XVFS_MINIRIVET_MODE_TCL_PRINT:
				*tcl_buffer_p = ch;
				tcl_buffer_p++;
				break;
		}
	}

#undef parse_xvfs_minirivet_getbyte

	return(1);
}

static int xvfs_create(FILE *outfp, const struct options * const options) {
	return(parse_xvfs_minirivet(outfp, "lib/xvfs/xvfs.c.rvt", options));
}

/*
 * Parse command line options
 */
static int parse_options(int argc, char **argv, struct options *options) {
	char *arg;
	char **option;
	int idx;
	int retval;

	for (idx = 0; idx < argc; idx++) {
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
		fprintf(stderr, "error: --name must be specified\n");
		retval = 0;
	}

	return(retval);
}

static void parse_xvfs_minirivet_directory(FILE *outfp, const char * const directory) {
	fprintf(outfp, "\t...\n");
}

static int parse_xvfs_minirivet(FILE *outfp, const char * const file, const char * const name, const char * const directory) {
	FILE *fp;
	int ch, ch_buf[3];
	char tcl_buffer[8192], *tcl_buffer_p, *tcl_buffer_e;
	enum xvfs_minirivet_mode mode;

	fp = fopen(file, "r");
	if (!fp) {
		return(0);
	}

#define parse_xvfs_minirivet_getbyte(var) var = fgetc(fp); if (var == EOF) { break; }

	mode = XVFS_MINIRIVET_MODE_COPY;
	while (1) {
		parse_xvfs_minirivet_getbyte(ch);

		switch (mode) {
			case XVFS_MINIRIVET_MODE_COPY:
				if (ch == '<') {
					parse_xvfs_minirivet_getbyte(ch_buf[0]);
					if (ch_buf[0] != '?') {
						fputc('<', outfp);
						ch = ch_buf[0];

						break;
					}

					tcl_buffer_p = tcl_buffer;
					parse_xvfs_minirivet_getbyte(ch_buf[0]);
					if (ch_buf[0] == '=') {
						mode = XVFS_MINIRIVET_MODE_TCL_PRINT;
					} else {
						mode = XVFS_MINIRIVET_MODE_TCL;
						*tcl_buffer_p = ch_buf[0];
						tcl_buffer_p++;
					}
					*tcl_buffer_p = '\0';
					continue;
				}
				break;
			case XVFS_MINIRIVET_MODE_TCL:
			case XVFS_MINIRIVET_MODE_TCL_PRINT:
				if (ch == '?') {
					parse_xvfs_minirivet_getbyte(ch_buf[0]);
					if (ch_buf[0] != '>') {
						*tcl_buffer_p = ch;
						tcl_buffer_p++;

						ch = ch_buf[0];
						
						break;
					}

					*tcl_buffer_p = '\0';

					if (mode == XVFS_MINIRIVET_MODE_TCL_PRINT) {
						tcl_buffer_p = tcl_buffer;
						while (*tcl_buffer_p && isspace(*tcl_buffer_p)) {
							tcl_buffer_p++;
						}
						tcl_buffer_e = tcl_buffer_p + strlen(tcl_buffer_p) - 1;
						while (tcl_buffer_e >= tcl_buffer_p && isspace(*tcl_buffer_e)) {
							*tcl_buffer_e = '\0';
							tcl_buffer_e--;
						}

						if (strcmp(tcl_buffer_p, "$::xvfs::fsName") == 0) {
							fprintf(outfp, name);
						} else if (strcmp(tcl_buffer_p, "$::xvfs::fileInfoStruct") == 0) {
							fprintf(outfp, "static const struct xvfs_file_data xvfs_");
							fprintf(outfp, name);
							fprintf(outfp, "_data[] = {\n");
							parse_xvfs_minirivet_directory(outfp, directory);
							fprintf(outfp, "};\n");
						} else if (strcmp(tcl_buffer_p, "[zlib adler32 $::xvfs::fsName]") == 0) {
							fprintf(outfp, "0");
						} else {
							fprintf(outfp, "@INVALID@%s@INVALID@", tcl_buffer_p);
						}
					}

					mode = XVFS_MINIRIVET_MODE_COPY;
					continue;
				}
				break;
		}

		switch (mode) {
			case XVFS_MINIRIVET_MODE_COPY:
				fputc(ch, outfp);
				break;
			case XVFS_MINIRIVET_MODE_TCL:
			case XVFS_MINIRIVET_MODE_TCL_PRINT:
				*tcl_buffer_p = ch;
				tcl_buffer_p++;
				break;
		}
	}

#undef parse_xvfs_minirivet_getbyte

	return(1);
}

static int xvfs_create(FILE *outfp, const char * const name, const char * const directory) {
	return(parse_xvfs_minirivet(outfp, "lib/xvfs/xvfs.c.rvt", name, directory));
}

int main(int argc, char **argv) {
	struct options options = {0};
	int parse_options_ret, xvfs_create_ret;

	argc--;
	argv++;

	parse_options_ret = parse_options(argc, argv, &options);
	if (!parse_options_ret) {
		return(1);
	}

	xvfs_create_ret = xvfs_create(stdout, options.name, options.directory);
	if (!xvfs_create_ret) {
		return(1);
	}

	return(0);
}







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<












|






452
453
454
455
456
457
458

















































































































459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
		fprintf(stderr, "error: --name must be specified\n");
		retval = 0;
	}

	return(retval);
}


















































































































int main(int argc, char **argv) {
	struct options options = {0};
	int parse_options_ret, xvfs_create_ret;

	argc--;
	argv++;

	parse_options_ret = parse_options(argc, argv, &options);
	if (!parse_options_ret) {
		return(1);
	}

	xvfs_create_ret = xvfs_create(stdout, &options);
	if (!xvfs_create_ret) {
		return(1);
	}

	return(0);
}