Annotation For xvfs-create-c.c

Lines of xvfs-create-c.c from check-in 20809b08ce that are changed by the sequence of edits moving toward check-in 0ffa676110:

                         1: #include <string.h>
                         2: #include <stdio.h>
                         3: #include <ctype.h>
                         4: 
                         5: struct options {
                         6: 	char *name;
                         7: 	char *directory;
                         8: };
                         9: 
                        10: enum xvfs_minirivet_mode {
                        11: 	XVFS_MINIRIVET_MODE_COPY,
                        12: 	XVFS_MINIRIVET_MODE_TCL,
                        13: 	XVFS_MINIRIVET_MODE_TCL_PRINT
                        14: };
                        15: 
                        16: static int parse_options(int argc, char **argv, struct options *options) {
                        17: 	char *arg;
                        18: 	char **option;
                        19: 	int idx;
                        20: 	int retval;
                        21: 
                        22: 	for (idx = 0; idx < argc; idx++) {
                        23: 		arg = argv[idx];
                        24: 
                        25: 		if (strcmp(arg, "--directory") == 0) {
                        26: 			option = &options->directory;
                        27: 		} else if (strcmp(arg, "--name") == 0) {
                        28: 			option = &options->name;
                        29: 		} else {
                        30: 			fprintf(stderr, "Invalid argument %s\n", arg);
                        31: 
                        32: 			return(0);
                        33: 		}
                        34: 
                        35: 		idx++;
                        36: 		arg = argv[idx];
                        37: 		*option = arg;
                        38: 	}
                        39: 
                        40: 	retval = 1;
                        41: 	if (!options->directory) {		
                        42: 		fprintf(stderr, "error: --directory must be specified\n");
                        43: 		retval = 0;
                        44: 	}
                        45: 
                        46: 	if (!options->name) {
                        47: 		fprintf(stderr, "error: --name must be specified\n");
                        48: 		retval = 0;
                        49: 	}
                        50: 
                        51: 	return(retval);
                        52: }
                        53: 
20809b08ce 2019-12-01   54: static void parse_xvfs_minirivet_directory(FILE *outfp, const char * const directory) {
20809b08ce 2019-12-01   55: 	fprintf(outfp, "\t...\n");
20809b08ce 2019-12-01   56: }
20809b08ce 2019-12-01   57: 
20809b08ce 2019-12-01   58: static int parse_xvfs_minirivet(FILE *outfp, const char * const file, const char * const name, const char * const directory) {
20809b08ce 2019-12-01   59: 	FILE *fp;
20809b08ce 2019-12-01   60: 	int ch, ch_buf[3];
20809b08ce 2019-12-01   61: 	char tcl_buffer[8192], *tcl_buffer_p, *tcl_buffer_e;
20809b08ce 2019-12-01   62: 	enum xvfs_minirivet_mode mode;
20809b08ce 2019-12-01   63: 
20809b08ce 2019-12-01   64: 	fp = fopen(file, "r");
20809b08ce 2019-12-01   65: 	if (!fp) {
20809b08ce 2019-12-01   66: 		return(0);
20809b08ce 2019-12-01   67: 	}
20809b08ce 2019-12-01   68: 
20809b08ce 2019-12-01   69: #define parse_xvfs_minirivet_getbyte(var) var = fgetc(fp); if (var == EOF) { break; }
20809b08ce 2019-12-01   70: 
20809b08ce 2019-12-01   71: 	mode = XVFS_MINIRIVET_MODE_COPY;
20809b08ce 2019-12-01   72: 	while (1) {
20809b08ce 2019-12-01   73: 		parse_xvfs_minirivet_getbyte(ch);
20809b08ce 2019-12-01   74: 
20809b08ce 2019-12-01   75: 		switch (mode) {
20809b08ce 2019-12-01   76: 			case XVFS_MINIRIVET_MODE_COPY:
20809b08ce 2019-12-01   77: 				if (ch == '<') {
20809b08ce 2019-12-01   78: 					parse_xvfs_minirivet_getbyte(ch_buf[0]);
20809b08ce 2019-12-01   79: 					if (ch_buf[0] != '?') {
20809b08ce 2019-12-01   80: 						fputc('<', outfp);
20809b08ce 2019-12-01   81: 						ch = ch_buf[0];
20809b08ce 2019-12-01   82: 
20809b08ce 2019-12-01   83: 						break;
20809b08ce 2019-12-01   84: 					}
20809b08ce 2019-12-01   85: 
20809b08ce 2019-12-01   86: 					tcl_buffer_p = tcl_buffer;
20809b08ce 2019-12-01   87: 					parse_xvfs_minirivet_getbyte(ch_buf[0]);
20809b08ce 2019-12-01   88: 					if (ch_buf[0] == '=') {
20809b08ce 2019-12-01   89: 						mode = XVFS_MINIRIVET_MODE_TCL_PRINT;
20809b08ce 2019-12-01   90: 					} else {
20809b08ce 2019-12-01   91: 						mode = XVFS_MINIRIVET_MODE_TCL;
20809b08ce 2019-12-01   92: 						*tcl_buffer_p = ch_buf[0];
20809b08ce 2019-12-01   93: 						tcl_buffer_p++;
20809b08ce 2019-12-01   94: 					}
20809b08ce 2019-12-01   95: 					*tcl_buffer_p = '\0';
20809b08ce 2019-12-01   96: 					continue;
20809b08ce 2019-12-01   97: 				}
20809b08ce 2019-12-01   98: 				break;
20809b08ce 2019-12-01   99: 			case XVFS_MINIRIVET_MODE_TCL:
20809b08ce 2019-12-01  100: 			case XVFS_MINIRIVET_MODE_TCL_PRINT:
20809b08ce 2019-12-01  101: 				if (ch == '?') {
20809b08ce 2019-12-01  102: 					parse_xvfs_minirivet_getbyte(ch_buf[0]);
20809b08ce 2019-12-01  103: 					if (ch_buf[0] != '>') {
20809b08ce 2019-12-01  104: 						*tcl_buffer_p = ch;
20809b08ce 2019-12-01  105: 						tcl_buffer_p++;
20809b08ce 2019-12-01  106: 
20809b08ce 2019-12-01  107: 						ch = ch_buf[0];
20809b08ce 2019-12-01  108: 						
20809b08ce 2019-12-01  109: 						break;
20809b08ce 2019-12-01  110: 					}
20809b08ce 2019-12-01  111: 
20809b08ce 2019-12-01  112: 					*tcl_buffer_p = '\0';
20809b08ce 2019-12-01  113: 
20809b08ce 2019-12-01  114: 					if (mode == XVFS_MINIRIVET_MODE_TCL_PRINT) {
20809b08ce 2019-12-01  115: 						tcl_buffer_p = tcl_buffer;
20809b08ce 2019-12-01  116: 						while (*tcl_buffer_p && isspace(*tcl_buffer_p)) {
20809b08ce 2019-12-01  117: 							tcl_buffer_p++;
20809b08ce 2019-12-01  118: 						}
20809b08ce 2019-12-01  119: 						tcl_buffer_e = tcl_buffer_p + strlen(tcl_buffer_p) - 1;
20809b08ce 2019-12-01  120: 						while (tcl_buffer_e >= tcl_buffer_p && isspace(*tcl_buffer_e)) {
20809b08ce 2019-12-01  121: 							*tcl_buffer_e = '\0';
20809b08ce 2019-12-01  122: 							tcl_buffer_e--;
20809b08ce 2019-12-01  123: 						}
20809b08ce 2019-12-01  124: 
20809b08ce 2019-12-01  125: 						if (strcmp(tcl_buffer_p, "$::xvfs::fsName") == 0) {
20809b08ce 2019-12-01  126: 							fprintf(outfp, name);
20809b08ce 2019-12-01  127: 						} else if (strcmp(tcl_buffer_p, "$::xvfs::fileInfoStruct") == 0) {
20809b08ce 2019-12-01  128: 							fprintf(outfp, "static const struct xvfs_file_data xvfs_");
20809b08ce 2019-12-01  129: 							fprintf(outfp, name);
20809b08ce 2019-12-01  130: 							fprintf(outfp, "_data[] = {\n");
20809b08ce 2019-12-01  131: 							parse_xvfs_minirivet_directory(outfp, directory);
20809b08ce 2019-12-01  132: 							fprintf(outfp, "};\n");
20809b08ce 2019-12-01  133: 						} else if (strcmp(tcl_buffer_p, "[zlib adler32 $::xvfs::fsName]") == 0) {
20809b08ce 2019-12-01  134: 							fprintf(outfp, "0");
20809b08ce 2019-12-01  135: 						} else {
20809b08ce 2019-12-01  136: 							fprintf(outfp, "@INVALID@%s@INVALID@", tcl_buffer_p);
20809b08ce 2019-12-01  137: 						}
20809b08ce 2019-12-01  138: 					}
20809b08ce 2019-12-01  139: 
20809b08ce 2019-12-01  140: 					mode = XVFS_MINIRIVET_MODE_COPY;
20809b08ce 2019-12-01  141: 					continue;
20809b08ce 2019-12-01  142: 				}
20809b08ce 2019-12-01  143: 				break;
20809b08ce 2019-12-01  144: 		}
20809b08ce 2019-12-01  145: 
20809b08ce 2019-12-01  146: 		switch (mode) {
20809b08ce 2019-12-01  147: 			case XVFS_MINIRIVET_MODE_COPY:
20809b08ce 2019-12-01  148: 				fputc(ch, outfp);
20809b08ce 2019-12-01  149: 				break;
20809b08ce 2019-12-01  150: 			case XVFS_MINIRIVET_MODE_TCL:
20809b08ce 2019-12-01  151: 			case XVFS_MINIRIVET_MODE_TCL_PRINT:
20809b08ce 2019-12-01  152: 				*tcl_buffer_p = ch;
20809b08ce 2019-12-01  153: 				tcl_buffer_p++;
20809b08ce 2019-12-01  154: 				break;
20809b08ce 2019-12-01  155: 		}
20809b08ce 2019-12-01  156: 	}
20809b08ce 2019-12-01  157: 
20809b08ce 2019-12-01  158: #undef parse_xvfs_minirivet_getbyte
20809b08ce 2019-12-01  159: 
20809b08ce 2019-12-01  160: 	return(1);
20809b08ce 2019-12-01  161: }
20809b08ce 2019-12-01  162: 
20809b08ce 2019-12-01  163: static int xvfs_create(FILE *outfp, const char * const name, const char * const directory) {
20809b08ce 2019-12-01  164: 	return(parse_xvfs_minirivet(outfp, "lib/xvfs/xvfs.c.rvt", name, directory));
20809b08ce 2019-12-01  165: }
20809b08ce 2019-12-01  166: 
                       167: int main(int argc, char **argv) {
                       168: 	struct options options = {0};
                       169: 	int parse_options_ret, xvfs_create_ret;
                       170: 
                       171: 	argc--;
                       172: 	argv++;
                       173: 
                       174: 	parse_options_ret = parse_options(argc, argv, &options);
                       175: 	if (!parse_options_ret) {
                       176: 		return(1);
                       177: 	}
                       178: 
20809b08ce 2019-12-01  179: 	xvfs_create_ret = xvfs_create(stdout, options.name, options.directory);
                       180: 	if (!xvfs_create_ret) {
                       181: 		return(1);
                       182: 	}
                       183: 
                       184: 	return(0);
                       185: }