+ if (sb.st_size == 0) {
+ failed("input is empty");
+ }
+
+ /*
+ * None of the arrays could be larger than the input-file, and since it
+ * is small, just allocate the maximum for simplicity.
+ */
+ if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
+ (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
+ (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
+ (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
+ (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
+ (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
+ failed("cannot allocate memory for input-file");
+ }
+
+ if ((fp = fopen(input_name, "r")) == 0)
+ failed("cannot open input-file");
+ len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
+ my_blob[sb.st_size] = '\0';
+ fclose(fp);
+
+ /*
+ * First, get rid of comments and escaped newlines, as well as repeated
+ * colons to construct a canonical entry.
+ *
+ * FIXME: actually this should make an additional pass just to strip
+ * comment-lines and escaped newlines. But it is workable for infocmp
+ * output.
+ */
+ state = pNewline;
+ for (j = k = 0; j < len; ++j) {
+ int ch = my_blob[j];
+ if (ch == '\t') {
+ ch = ' ';
+ }
+ switch (state) {
+ case pNewline:
+ if (ch == ' ') {
+ continue;
+ }
+ if (ch == '#') {
+ state = pComment;
+ continue;
+ }
+ state = pDefault;
+ /* FALLTHRU */
+ case pDefault:
+ switch (ch) {
+ case '|':
+ state = pDescription;
+ continue;
+ case '\\':
+ state = pEscaped;
+ continue;
+ case '\n':
+ state = pNewline;
+ continue;
+ case ' ':
+ case ':':
+ break;
+ default:
+ state = pName;