Check-in [20809b08ce]
Overview
Comment:Added the C file itself
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | pure-c
Files: files | file ages | folders
SHA3-256: 20809b08cefadffe9af9ab291adf56a7049be984e0042a073989e5b5f20620d6
User & Date: rkeene on 2019-12-01 00:17:15
Other Links: branch diff | manifest | tags
Context
2019-12-02
02:44
Implemented completed xvfs-create-c check-in: 0ffa676110 user: rkeene tags: pure-c
2019-12-01
00:17
Added the C file itself check-in: 20809b08ce user: rkeene tags: pure-c
00:16
Started work on xvfs-create-c, a pure C version of the builder with no dependencies check-in: ae8437b96b user: rkeene tags: pure-c
Changes

Added xvfs-create-c.c version [a50f6e796a].


























































































































































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
#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++) {
		arg = argv[idx];

		if (strcmp(arg, "--directory") == 0) {
			option = &options->directory;
		} else if (strcmp(arg, "--name") == 0) {
			option = &options->name;
		} else {
			fprintf(stderr, "Invalid argument %s\n", arg);

			return(0);
		}

		idx++;
		arg = argv[idx];
		*option = arg;
	}

	retval = 1;
	if (!options->directory) {		
		fprintf(stderr, "error: --directory must be specified\n");
		retval = 0;
	}

	if (!options->name) {
		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);
}