Annotation For xvfs-create-c.c

Origin for each line in xvfs-create-c.c from check-in 2d9592486f:

0ffa676110 2019-12-02        rkeene: #include <sys/stat.h>
0ffa676110 2019-12-02        rkeene: #include <stdlib.h>
20809b08ce 2019-12-01        rkeene: #include <string.h>
0ffa676110 2019-12-02        rkeene: #include <dirent.h>
20809b08ce 2019-12-01        rkeene: #include <stdio.h>
20809b08ce 2019-12-01        rkeene: #include <ctype.h>
0ffa676110 2019-12-02        rkeene: #include <fcntl.h>
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: struct options {
20809b08ce 2019-12-01        rkeene: 	char *name;
20809b08ce 2019-12-01        rkeene: 	char *directory;
0ffa676110 2019-12-02        rkeene: };
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: struct xvfs_state {
0ffa676110 2019-12-02        rkeene: 	char **children;
0ffa676110 2019-12-02        rkeene: 	unsigned long child_count;
0ffa676110 2019-12-02        rkeene: 	unsigned long child_len;
0ffa676110 2019-12-02        rkeene: 	int bucket_count;
0ffa676110 2019-12-02        rkeene: 	int max_index;
20809b08ce 2019-12-01        rkeene: };
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: enum xvfs_minirivet_mode {
20809b08ce 2019-12-01        rkeene: 	XVFS_MINIRIVET_MODE_COPY,
20809b08ce 2019-12-01        rkeene: 	XVFS_MINIRIVET_MODE_TCL,
20809b08ce 2019-12-01        rkeene: 	XVFS_MINIRIVET_MODE_TCL_PRINT
20809b08ce 2019-12-01        rkeene: };
20809b08ce 2019-12-01        rkeene: 
0ffa676110 2019-12-02        rkeene: /*
0ffa676110 2019-12-02        rkeene:  * adler32() function from zlib 1.1.4 and under the same license
0ffa676110 2019-12-02        rkeene:  */
0ffa676110 2019-12-02        rkeene: static unsigned long adler32(unsigned long adler, const unsigned char *buf, unsigned int len) {
0ffa676110 2019-12-02        rkeene: 	const int len_max = 5552;
0ffa676110 2019-12-02        rkeene: 	const unsigned long base = 65521;
0ffa676110 2019-12-02        rkeene: 	unsigned long s1 = adler & 0xffff;
0ffa676110 2019-12-02        rkeene: 	unsigned long s2 = (adler >> 16) & 0xffff;
0ffa676110 2019-12-02        rkeene: 	int k, i;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	if (buf == NULL) {
0ffa676110 2019-12-02        rkeene: 		return(1UL);
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	while (len > 0) {
0ffa676110 2019-12-02        rkeene: 		k = len < len_max ? len : len_max;
0ffa676110 2019-12-02        rkeene: 		len -= k;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		while (k >= 16) {
0ffa676110 2019-12-02        rkeene: 			for (i = 0; i < 16; i++) {
0ffa676110 2019-12-02        rkeene: 				s2 += buf[i];
0ffa676110 2019-12-02        rkeene: 			}
0ffa676110 2019-12-02        rkeene: 			s1 = buf[15];
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 			buf += 16;
0ffa676110 2019-12-02        rkeene: 			k   -= 16;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		if (k != 0) {
0ffa676110 2019-12-02        rkeene: 			do {
0ffa676110 2019-12-02        rkeene: 				s1 += *buf++;
0ffa676110 2019-12-02        rkeene: 				s2 += s1;
0ffa676110 2019-12-02        rkeene: 			} while (--k);
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		s1 %= base;
0ffa676110 2019-12-02        rkeene: 		s2 %= base;
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	return((s2 << 16) | s1);
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: /*
0ffa676110 2019-12-02        rkeene:  * Handle XVFS Rivet template file substitution
0ffa676110 2019-12-02        rkeene:  */
0ffa676110 2019-12-02        rkeene: static void parse_xvfs_minirivet_file(FILE *outfp, const char * const external_file_name, const char * const internal_file_name) {
0ffa676110 2019-12-02        rkeene: 	FILE *fp;
0ffa676110 2019-12-02        rkeene: 	unsigned long file_size;
0ffa676110 2019-12-02        rkeene: 	unsigned char buf[10];
0ffa676110 2019-12-02        rkeene: 	size_t item_count;
0ffa676110 2019-12-02        rkeene: 	int idx;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fp = fopen(external_file_name, "rb");
0ffa676110 2019-12-02        rkeene: 	if (!fp) {
0ffa676110 2019-12-02        rkeene: 		return;
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t{\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.name = \"%s\",\n", internal_file_name);
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.type = XVFS_FILE_TYPE_REG,\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.data.fileContents = (const unsigned char *) \"");
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	file_size = 0;
0ffa676110 2019-12-02        rkeene: 	while (1) {
0ffa676110 2019-12-02        rkeene: 		item_count = fread(&buf, 1, sizeof(buf), fp);
0ffa676110 2019-12-02        rkeene: 		if (item_count <= 0) {
0ffa676110 2019-12-02        rkeene: 			break;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		if (file_size != 0) {
0ffa676110 2019-12-02        rkeene: 			fprintf(outfp, "\n\t\t\t\"");
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		for (idx = 0; idx < item_count; idx++) {
0ffa676110 2019-12-02        rkeene: 			fprintf(outfp, "\\x%02x", (int) buf[idx]);
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "\"");
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		file_size += item_count;
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fclose(fp);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, ",\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.size = %lu\n", file_size);
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t},\n");
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: static void parse_xvfs_minirivet_directory(FILE *outfp, struct xvfs_state *xvfs_state, const char * const directory, const char * const prefix) {
0ffa676110 2019-12-02        rkeene: 	const unsigned int max_path_len = 8192, max_children = 65536;
0ffa676110 2019-12-02        rkeene: 	unsigned long child_idx, child_count;
0ffa676110 2019-12-02        rkeene: 	DIR *dp;
0ffa676110 2019-12-02        rkeene: 	struct dirent *file_info;
0ffa676110 2019-12-02        rkeene: 	struct stat file_stat;
0ffa676110 2019-12-02        rkeene: 	char *full_path_buf;
0ffa676110 2019-12-02        rkeene: 	char *rel_path_buf;
0ffa676110 2019-12-02        rkeene: 	char **children;
0ffa676110 2019-12-02        rkeene: 	int stat_ret;
0ffa676110 2019-12-02        rkeene: 	int snprintf_ret;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	dp = opendir(directory);
0ffa676110 2019-12-02        rkeene: 	if (!dp) {
0ffa676110 2019-12-02        rkeene: 		return;
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	full_path_buf = malloc(max_path_len);
0ffa676110 2019-12-02        rkeene: 	rel_path_buf = malloc(max_path_len);
0ffa676110 2019-12-02        rkeene: 	children = malloc(sizeof(*children) * max_children);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	child_idx = 0;
0ffa676110 2019-12-02        rkeene: 	while (1) {
0ffa676110 2019-12-02        rkeene: 		file_info = readdir(dp);
0ffa676110 2019-12-02        rkeene: 		if (!file_info) {
0ffa676110 2019-12-02        rkeene: 			break;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		if (strcmp(file_info->d_name, ".") == 0) {
0ffa676110 2019-12-02        rkeene: 			continue;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		if (strcmp(file_info->d_name, "..") == 0) {
0ffa676110 2019-12-02        rkeene: 			continue;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		snprintf_ret = snprintf(full_path_buf, max_path_len, "%s/%s", directory, file_info->d_name);
0ffa676110 2019-12-02        rkeene: 		if (snprintf_ret >= max_path_len) {
0ffa676110 2019-12-02        rkeene: 			continue;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		snprintf_ret = snprintf(rel_path_buf, max_path_len, "%s%s%s",
0ffa676110 2019-12-02        rkeene: 			prefix,
0ffa676110 2019-12-02        rkeene: 			strcmp(prefix, "") == 0 ? "" : "/",
0ffa676110 2019-12-02        rkeene: 			file_info->d_name
0ffa676110 2019-12-02        rkeene: 		);
0ffa676110 2019-12-02        rkeene: 		if (snprintf_ret >= max_path_len) {
0ffa676110 2019-12-02        rkeene: 			continue;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		stat_ret = stat(full_path_buf, &file_stat);
0ffa676110 2019-12-02        rkeene: 		if (stat_ret != 0) {
0ffa676110 2019-12-02        rkeene: 			continue;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		children[child_idx] = strdup(file_info->d_name);
0ffa676110 2019-12-02        rkeene: 		child_idx++;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		if (S_ISDIR(file_stat.st_mode)) {
0ffa676110 2019-12-02        rkeene: 			parse_xvfs_minirivet_directory(outfp, xvfs_state, full_path_buf, rel_path_buf);
0ffa676110 2019-12-02        rkeene: 		} else {
0ffa676110 2019-12-02        rkeene: 			parse_xvfs_minirivet_file(outfp, full_path_buf, rel_path_buf);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 			xvfs_state->children[xvfs_state->child_count] = strdup(rel_path_buf);
0ffa676110 2019-12-02        rkeene: 			xvfs_state->child_count++;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 	free(full_path_buf);
0ffa676110 2019-12-02        rkeene: 	free(rel_path_buf);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	child_count = child_idx;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t{\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.name = \"%s\",\n", prefix);
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.type = XVFS_FILE_TYPE_DIR,\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.size = %lu,\n", child_count);
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t.data.dirChildren  = (const char *[]) {");
0ffa676110 2019-12-02        rkeene: 	for (child_idx = 0; child_idx < child_count; child_idx++) {
0ffa676110 2019-12-02        rkeene: 		if (child_idx != 0) {
0ffa676110 2019-12-02        rkeene: 			fprintf(outfp, ", ");
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "\"%s\"", children[child_idx]);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		free(children[child_idx]);
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "}\n");
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	free(children);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t},\n");
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	xvfs_state->children[xvfs_state->child_count] = strdup(prefix);
0ffa676110 2019-12-02        rkeene: 	xvfs_state->child_count++;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	closedir(dp);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	return;
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: static void parse_xvfs_minirivet_hashtable_header(FILE *outfp, struct xvfs_state *xvfs_state) {
0ffa676110 2019-12-02        rkeene: 	const int max_bucket_count = 30;
0ffa676110 2019-12-02        rkeene: 	int bucket_count;
0ffa676110 2019-12-02        rkeene: 	int idx1, idx2;
0ffa676110 2019-12-02        rkeene: 	int check_hash;
0ffa676110 2019-12-02        rkeene: 	int first_entry;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	if (xvfs_state->child_count > max_bucket_count) {
0ffa676110 2019-12-02        rkeene: 		bucket_count = max_bucket_count;
0ffa676110 2019-12-02        rkeene: 	} else {
0ffa676110 2019-12-02        rkeene: 		bucket_count = xvfs_state->child_count;
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 	xvfs_state->bucket_count = bucket_count;
0ffa676110 2019-12-02        rkeene: 	xvfs_state->max_index = xvfs_state->child_count;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\tlong pathIndex_idx;\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\tint pathIndex_hash;\n");
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	/*
0ffa676110 2019-12-02        rkeene: 	 * XXX:TODO: Make this not O(n^2)
0ffa676110 2019-12-02        rkeene: 	 */
0ffa676110 2019-12-02        rkeene: 	for (idx1 = 0; idx1 < bucket_count; idx1++) {
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "\tstatic const long pathIndex_hashTable_%i[] = {\n", idx1);
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "\t\t");
0ffa676110 2019-12-02        rkeene: 		first_entry = 1;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		for (idx2 = 0; idx2 < xvfs_state->child_count; idx2++) {
1d38520446 2019-12-02        rkeene: 			check_hash = adler32(0, (unsigned char *) xvfs_state->children[idx2], strlen(xvfs_state->children[idx2])) % bucket_count;
0ffa676110 2019-12-02        rkeene: 			if (check_hash != idx1) {
0ffa676110 2019-12-02        rkeene: 				continue;
0ffa676110 2019-12-02        rkeene: 			}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 			if (!first_entry) {
0ffa676110 2019-12-02        rkeene: 				fprintf(outfp, ", ");
0ffa676110 2019-12-02        rkeene: 			}
0ffa676110 2019-12-02        rkeene: 			first_entry = 0;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 			if (check_hash == idx1) {
0ffa676110 2019-12-02        rkeene: 				fprintf(outfp, "%i", idx2);
0ffa676110 2019-12-02        rkeene: 			}
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		if (!first_entry) {
0ffa676110 2019-12-02        rkeene: 			fprintf(outfp, ", ");
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "XVFS_NAME_LOOKUP_ERROR");
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "\n");
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "\t};\n");
0ffa676110 2019-12-02        rkeene: 	}
c69f6f1b5a 2019-12-02        rkeene: 
c69f6f1b5a 2019-12-02        rkeene: 	for (idx2 = 0; idx2 < xvfs_state->child_count; idx2++) {
c69f6f1b5a 2019-12-02        rkeene: 		free(xvfs_state->children[idx2]);
c69f6f1b5a 2019-12-02        rkeene: 	}
c69f6f1b5a 2019-12-02        rkeene: 	free(xvfs_state->children);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\tstatic const long * const pathIndex_hashTable[%i] = {\n", bucket_count);
0ffa676110 2019-12-02        rkeene: 	for (idx1 = 0; idx1 < bucket_count; idx1++) {
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "\t\tpathIndex_hashTable_%i,\n", idx1);
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t};\n");
0ffa676110 2019-12-02        rkeene: 	return;
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: static void parse_xvfs_minirivet_hashtable_body(FILE *outfp, struct xvfs_state *xvfs_state) {
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\tpathIndex_hash = Tcl_ZlibAdler32(0, (unsigned char *) path, pathLen) %% %i;\n", xvfs_state->bucket_count);
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\tfor (pathIndex_idx = 0; pathIndex_idx < %i; pathIndex_idx++) {\n", xvfs_state->max_index);
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\tpathIndex = pathIndex_hashTable[pathIndex_hash][pathIndex_idx];\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\tif (pathIndex == XVFS_NAME_LOOKUP_ERROR) {\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t\tbreak;\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t}\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\tif (strcmp(path, xvfs_example_data[pathIndex].name) == 0) {\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t\treturn(pathIndex);\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t\t}\n");
0ffa676110 2019-12-02        rkeene: 	fprintf(outfp, "\t}\n");
0ffa676110 2019-12-02        rkeene: 	return;
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: static void parse_xvfs_minirivet_handle_tcl_print(FILE *outfp, const struct options * const options, struct xvfs_state *xvfs_state, char *command) {
0ffa676110 2019-12-02        rkeene: 	char *buffer_p, *buffer_e;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	buffer_p = command;
0ffa676110 2019-12-02        rkeene: 	while (*buffer_p && isspace(*buffer_p)) {
0ffa676110 2019-12-02        rkeene: 		buffer_p++;
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	buffer_e = buffer_p + strlen(buffer_p) - 1;
0ffa676110 2019-12-02        rkeene: 	while (buffer_e >= buffer_p && isspace(*buffer_e)) {
0ffa676110 2019-12-02        rkeene: 		*buffer_e = '\0';
0ffa676110 2019-12-02        rkeene: 		buffer_e--;
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	if (strcmp(buffer_p, "$::xvfs::fsName") == 0) {
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "%s", options->name);
0ffa676110 2019-12-02        rkeene: 	} else if (strcmp(buffer_p, "$::xvfs::fileInfoStruct") == 0) {
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "static const struct xvfs_file_data xvfs_");
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "%s", options->name);
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "_data[] = {\n");
0ffa676110 2019-12-02        rkeene: 		parse_xvfs_minirivet_directory(outfp, xvfs_state, options->directory, "");
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "};\n");
0ffa676110 2019-12-02        rkeene: 	} else if (strcmp(buffer_p, "[zlib adler32 $::xvfs::fsName 0]") == 0) {
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "%lu", adler32(0, (unsigned char *) options->name, strlen(options->name)));
0ffa676110 2019-12-02        rkeene: 	} else if (strcmp(buffer_p, "$hashTableHeader") == 0) {
0ffa676110 2019-12-02        rkeene: 		parse_xvfs_minirivet_hashtable_header(outfp, xvfs_state);
0ffa676110 2019-12-02        rkeene: 	} else if (strcmp(buffer_p, "[dict get $hashTable body]") == 0) {
0ffa676110 2019-12-02        rkeene: 		parse_xvfs_minirivet_hashtable_body(outfp, xvfs_state);
0ffa676110 2019-12-02        rkeene: 	} else {
0ffa676110 2019-12-02        rkeene: 		fprintf(outfp, "@INVALID@%s@INVALID@", buffer_p);
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	return;
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: static int parse_xvfs_minirivet(FILE *outfp, const char * const template, const struct options * const options) {
0ffa676110 2019-12-02        rkeene: 	struct xvfs_state xvfs_state;
0ffa676110 2019-12-02        rkeene: 	int ch, ch_buf;
2d9592486f 2019-12-02        rkeene: 	int template_idx = 0;
0ffa676110 2019-12-02        rkeene: 	char tcl_buffer[8192], *tcl_buffer_p;
0ffa676110 2019-12-02        rkeene: 	enum xvfs_minirivet_mode mode;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	xvfs_state.child_count = 0;
0ffa676110 2019-12-02        rkeene: 	xvfs_state.child_len   = 65536;
0ffa676110 2019-12-02        rkeene: 	xvfs_state.children    = malloc(sizeof(*xvfs_state.children) * xvfs_state.child_len);
0ffa676110 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: #define parse_xvfs_minirivet_getbyte(var) var = template[template_idx]; template_idx++; if (var == 0) { break; }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	mode = XVFS_MINIRIVET_MODE_COPY;
9f07de5ec5 2019-12-02        rkeene: 	tcl_buffer_p = NULL;
0ffa676110 2019-12-02        rkeene: 	while (1) {
0ffa676110 2019-12-02        rkeene: 		parse_xvfs_minirivet_getbyte(ch);
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		switch (mode) {
0ffa676110 2019-12-02        rkeene: 			case XVFS_MINIRIVET_MODE_COPY:
0ffa676110 2019-12-02        rkeene: 				if (ch == '<') {
0ffa676110 2019-12-02        rkeene: 					parse_xvfs_minirivet_getbyte(ch_buf);
0ffa676110 2019-12-02        rkeene: 					if (ch_buf != '?') {
0ffa676110 2019-12-02        rkeene: 						fputc('<', outfp);
0ffa676110 2019-12-02        rkeene: 						ch = ch_buf;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 						break;
0ffa676110 2019-12-02        rkeene: 					}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 					tcl_buffer_p = tcl_buffer;
0ffa676110 2019-12-02        rkeene: 					parse_xvfs_minirivet_getbyte(ch_buf);
0ffa676110 2019-12-02        rkeene: 					if (ch_buf == '=') {
0ffa676110 2019-12-02        rkeene: 						mode = XVFS_MINIRIVET_MODE_TCL_PRINT;
0ffa676110 2019-12-02        rkeene: 					} else {
0ffa676110 2019-12-02        rkeene: 						mode = XVFS_MINIRIVET_MODE_TCL;
0ffa676110 2019-12-02        rkeene: 						*tcl_buffer_p = ch_buf;
0ffa676110 2019-12-02        rkeene: 						tcl_buffer_p++;
0ffa676110 2019-12-02        rkeene: 					}
0ffa676110 2019-12-02        rkeene: 					*tcl_buffer_p = '\0';
0ffa676110 2019-12-02        rkeene: 					continue;
0ffa676110 2019-12-02        rkeene: 				}
0ffa676110 2019-12-02        rkeene: 				break;
0ffa676110 2019-12-02        rkeene: 			case XVFS_MINIRIVET_MODE_TCL:
0ffa676110 2019-12-02        rkeene: 			case XVFS_MINIRIVET_MODE_TCL_PRINT:
0ffa676110 2019-12-02        rkeene: 				if (ch == '?') {
0ffa676110 2019-12-02        rkeene: 					parse_xvfs_minirivet_getbyte(ch_buf);
0ffa676110 2019-12-02        rkeene: 					if (ch_buf != '>') {
0ffa676110 2019-12-02        rkeene: 						*tcl_buffer_p = ch;
0ffa676110 2019-12-02        rkeene: 						tcl_buffer_p++;
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 						ch = ch_buf;
0ffa676110 2019-12-02        rkeene: 						
0ffa676110 2019-12-02        rkeene: 						break;
0ffa676110 2019-12-02        rkeene: 					}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 					*tcl_buffer_p = '\0';
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 					if (mode == XVFS_MINIRIVET_MODE_TCL_PRINT) {
0ffa676110 2019-12-02        rkeene: 						parse_xvfs_minirivet_handle_tcl_print(outfp, options, &xvfs_state, tcl_buffer);
0ffa676110 2019-12-02        rkeene: 					}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 					mode = XVFS_MINIRIVET_MODE_COPY;
0ffa676110 2019-12-02        rkeene: 					continue;
0ffa676110 2019-12-02        rkeene: 				}
0ffa676110 2019-12-02        rkeene: 				break;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 		switch (mode) {
0ffa676110 2019-12-02        rkeene: 			case XVFS_MINIRIVET_MODE_COPY:
0ffa676110 2019-12-02        rkeene: 				fputc(ch, outfp);
0ffa676110 2019-12-02        rkeene: 				break;
0ffa676110 2019-12-02        rkeene: 			case XVFS_MINIRIVET_MODE_TCL:
0ffa676110 2019-12-02        rkeene: 			case XVFS_MINIRIVET_MODE_TCL_PRINT:
0ffa676110 2019-12-02        rkeene: 				*tcl_buffer_p = ch;
0ffa676110 2019-12-02        rkeene: 				tcl_buffer_p++;
0ffa676110 2019-12-02        rkeene: 				break;
0ffa676110 2019-12-02        rkeene: 		}
0ffa676110 2019-12-02        rkeene: 	}
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: #undef parse_xvfs_minirivet_getbyte
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: 	return(1);
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: static int xvfs_create(FILE *outfp, const struct options * const options) {
2d9592486f 2019-12-02        rkeene: 	const int template_len = 65536;
2d9592486f 2019-12-02        rkeene: 	const char * const template_file = "lib/xvfs/xvfs.c.rvt";
2d9592486f 2019-12-02        rkeene: 	FILE *fp;
2d9592486f 2019-12-02        rkeene: 	char *template, *template_p;
2d9592486f 2019-12-02        rkeene: 	int template_remain;
2d9592486f 2019-12-02        rkeene: 	size_t fread_ret;
2d9592486f 2019-12-02        rkeene: 	int retval;
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 	fp = fopen(template_file, "r");
2d9592486f 2019-12-02        rkeene: 	if (!fp) {
2d9592486f 2019-12-02        rkeene: 		return(0);
2d9592486f 2019-12-02        rkeene: 	}
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 	template = malloc(template_len);
2d9592486f 2019-12-02        rkeene: 	template_remain = template_len;
2d9592486f 2019-12-02        rkeene: 	if (!template) {
2d9592486f 2019-12-02        rkeene: 		fclose(fp);
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 		return(0);
2d9592486f 2019-12-02        rkeene: 	}
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 	template_p = template;
2d9592486f 2019-12-02        rkeene: 	while (1) {
2d9592486f 2019-12-02        rkeene: 		fread_ret = fread(template_p, 1, template_remain, fp);
2d9592486f 2019-12-02        rkeene: 		if (fread_ret <= 0) {
2d9592486f 2019-12-02        rkeene: 			break;
2d9592486f 2019-12-02        rkeene: 		}
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 		template_p += fread_ret;
2d9592486f 2019-12-02        rkeene: 		template_remain -= fread_ret;
2d9592486f 2019-12-02        rkeene: 	}
2d9592486f 2019-12-02        rkeene: 	if (template_remain > 0) {
2d9592486f 2019-12-02        rkeene: 		*template_p = '\0';
2d9592486f 2019-12-02        rkeene: 	}
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 	fclose(fp);
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 	retval = parse_xvfs_minirivet(outfp, template, options);
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 	free(template);
2d9592486f 2019-12-02        rkeene: 
2d9592486f 2019-12-02        rkeene: 	return(retval);
0ffa676110 2019-12-02        rkeene: }
0ffa676110 2019-12-02        rkeene: 
0ffa676110 2019-12-02        rkeene: /*
0ffa676110 2019-12-02        rkeene:  * Parse command line options
0ffa676110 2019-12-02        rkeene:  */
20809b08ce 2019-12-01        rkeene: static int parse_options(int argc, char **argv, struct options *options) {
20809b08ce 2019-12-01        rkeene: 	char *arg;
20809b08ce 2019-12-01        rkeene: 	char **option;
20809b08ce 2019-12-01        rkeene: 	int idx;
20809b08ce 2019-12-01        rkeene: 	int retval;
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 	for (idx = 0; idx < argc; idx++) {
20809b08ce 2019-12-01        rkeene: 		arg = argv[idx];
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 		if (strcmp(arg, "--directory") == 0) {
20809b08ce 2019-12-01        rkeene: 			option = &options->directory;
20809b08ce 2019-12-01        rkeene: 		} else if (strcmp(arg, "--name") == 0) {
20809b08ce 2019-12-01        rkeene: 			option = &options->name;
20809b08ce 2019-12-01        rkeene: 		} else {
20809b08ce 2019-12-01        rkeene: 			fprintf(stderr, "Invalid argument %s\n", arg);
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 			return(0);
20809b08ce 2019-12-01        rkeene: 		}
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 		idx++;
20809b08ce 2019-12-01        rkeene: 		arg = argv[idx];
20809b08ce 2019-12-01        rkeene: 		*option = arg;
20809b08ce 2019-12-01        rkeene: 	}
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 	retval = 1;
20809b08ce 2019-12-01        rkeene: 	if (!options->directory) {		
20809b08ce 2019-12-01        rkeene: 		fprintf(stderr, "error: --directory must be specified\n");
20809b08ce 2019-12-01        rkeene: 		retval = 0;
20809b08ce 2019-12-01        rkeene: 	}
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 	if (!options->name) {
20809b08ce 2019-12-01        rkeene: 		fprintf(stderr, "error: --name must be specified\n");
20809b08ce 2019-12-01        rkeene: 		retval = 0;
20809b08ce 2019-12-01        rkeene: 	}
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 	return(retval);
20809b08ce 2019-12-01        rkeene: }
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: int main(int argc, char **argv) {
20809b08ce 2019-12-01        rkeene: 	struct options options = {0};
20809b08ce 2019-12-01        rkeene: 	int parse_options_ret, xvfs_create_ret;
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 	argc--;
20809b08ce 2019-12-01        rkeene: 	argv++;
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 	parse_options_ret = parse_options(argc, argv, &options);
20809b08ce 2019-12-01        rkeene: 	if (!parse_options_ret) {
20809b08ce 2019-12-01        rkeene: 		return(1);
20809b08ce 2019-12-01        rkeene: 	}
20809b08ce 2019-12-01        rkeene: 
0ffa676110 2019-12-02        rkeene: 	xvfs_create_ret = xvfs_create(stdout, &options);
20809b08ce 2019-12-01        rkeene: 	if (!xvfs_create_ret) {
20809b08ce 2019-12-01        rkeene: 		return(1);
20809b08ce 2019-12-01        rkeene: 	}
20809b08ce 2019-12-01        rkeene: 
20809b08ce 2019-12-01        rkeene: 	return(0);
20809b08ce 2019-12-01        rkeene: }