+typedef enum {
+ pDefault = 0
+ ,pComment
+ ,pDescription
+ ,pEscaped
+ ,pNewline
+ ,pName
+ ,pNumber
+ ,pString
+} STATE;
+
+static void
+parse_description(const char *input_name)
+{
+ static char empty[1];
+
+ FILE *fp;
+ struct stat sb;
+ size_t count_bools = 0;
+ size_t count_nums = 0;
+ size_t count_strs = 0;
+ size_t len;
+ size_t j, k, jl;
+ STATE state;
+
+ if (stat(input_name, &sb) != 0
+ || (sb.st_mode & S_IFMT) != S_IFREG) {
+ failed("input is not a file");
+ }
+
+ 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.
+ */
+ 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 ' ':
+ break;
+ case ',':
+ my_blob[k++] = (char) ch;
+ break;
+ default:
+ if (isalpha(UChar(ch)))
+ state = pName;
+ else
+ fprintf(stderr, "OOPS @%d:%.20s\n", __LINE__, my_blob + j);
+ my_blob[k++] = (char) ch;
+ break;
+ }
+ break;
+ case pComment:
+ if (ch == '\n')
+ state = pNewline;
+ break;
+ case pDescription:
+ switch (ch) {
+ case ',':
+ state = pDefault;
+ break;
+ case '\n':
+ state = pNewline;
+ break;
+ }
+ break;
+ case pEscaped:
+ if (ch != '\n') {
+ my_blob[k++] = (char) ch;
+ state = pDefault;
+ } else {
+ state = pNewline;
+ }
+ break;
+ case pName:
+ switch (ch) {
+ case '\n':
+ state = pNewline;
+ continue;
+ case ' ':
+ case ',':
+ state = pDefault;
+ break;
+ case '#':
+ state = pNumber;
+ break;
+ case '=':
+ state = pString;
+ break;
+ case '|':
+ state = pDescription;
+ continue;
+ }
+ my_blob[k++] = (char) ch;
+ break;
+ case pNumber:
+ switch (ch) {
+ case '\n':
+ state = pNewline;
+ continue;
+ case ',':
+ state = pDefault;
+ break;
+ case ' ':
+ state = pDefault;
+ continue;
+ }
+ my_blob[k++] = (char) ch;
+ break;
+ case pString:
+ switch (ch) {
+ case '\n':
+ state = pNewline;
+ break;
+ case ',':
+ state = pDefault;
+ my_blob[k++] = (char) ch;
+ break;
+ default:
+ my_blob[k++] = (char) ch;
+ break;
+ }
+ break;
+ default:
+ /* not used */
+ break;
+ }
+ }
+ my_blob[k] = '\0';
+
+ /*
+ * Then, parse what's left, making indexes of the names and values.
+ */
+ state = pDefault;
+ for (j = 0; my_blob[j] != '\0'; ++j) {
+ switch (state) {
+ case pDefault:
+ switch (my_blob[j]) {
+ case '\\':
+ state = pEscaped;
+ break;
+ case ',':
+ my_blob[j] = '\0';
+ if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ',')
+ state = pName;
+ break;
+ case ' ':
+ break;
+ default:
+ break;
+ }
+ case pEscaped:
+ break;
+ case pName:
+ state = pDefault;
+ if (isalpha(UChar(my_blob[j]))) {
+ for (jl = 1; isalnum(UChar(my_blob[j + jl])); ++jl) {
+ ;
+ }
+ } else {
+ jl = 0;
+ }
+ if (jl != 0) {
+ switch (my_blob[j + jl]) {
+ case '#':
+ my_numvalues[count_nums] = &my_blob[j + jl + 1];
+ my_numcodes[count_nums++] = &my_blob[j];
+ my_blob[j + jl] = '\0';
+ state = pNumber;
+ j += jl;
+ break;
+ case '=':
+ my_strvalues[count_strs] = &my_blob[j + jl + 1];
+ my_strcodes[count_strs++] = &my_blob[j];
+ my_blob[j + jl] = '\0';
+ state = pString;
+ j += jl;
+ break;
+ default:
+ if (my_blob[j + jl] == '@') {
+ /*
+ * We cannot get the type for a cancelled item
+ * directly, but can infer it assuming the input
+ * came from infocmp, which puts the data in a
+ * known order.
+ */
+ if (count_strs) {
+ my_strvalues[count_strs] = empty;
+ my_strcodes[count_strs++] = &my_blob[j];
+ } else if (count_nums) {
+ my_numvalues[count_nums] = empty;
+ my_numcodes[count_nums++] = &my_blob[j];
+ } else {
+ my_boolcodes[count_bools++] = &my_blob[j];
+ }
+ my_blob[j + jl] = '\0';
+ j += jl + 1;
+ } else {
+ my_boolcodes[count_bools++] = &my_blob[j];
+ my_blob[j + jl] = '\0';
+ j += jl;
+ }
+ state = (isCapName(my_blob[j + 1])
+ ? pName
+ : pDefault);
+ break;
+ }
+ }
+ break;
+ case pNumber:
+ if (!isdigit(UChar(my_blob[j]))) {
+ --j;
+ state = pDefault;
+ }
+ break;
+ case pString:
+ switch (my_blob[j]) {
+ case '\\':
+ if (my_blob[j + 1] != '\0') {
+ ++j;
+ } else {
+ --j;
+ state = pDefault;
+ }
+ break;
+ case ',':
+ --j;
+ state = pDefault;
+ break;
+ }
+ break;
+ case pNewline:
+ case pComment:
+ case pDescription:
+ default:
+ break;
+ }
+ }
+ my_boolcodes[count_bools] = 0;
+ my_numcodes[count_nums] = 0;
+ my_numvalues[count_nums] = 0;
+ my_strcodes[count_strs] = 0;
+ my_strvalues[count_strs] = 0;
+
+#if 0
+ printf("# bools:%d\n", (int) count_bools);
+ for (j = 0; my_boolcodes[j]; ++j)
+ printf("\t%s,\n", my_boolcodes[j]);
+
+ printf("# numbers:%d\n", (int) count_nums);
+ for (j = 0; my_numcodes[j]; ++j)
+ printf("\t%s#%s,\n", my_numcodes[j], my_numvalues[j]);
+
+ printf("# strings:%d\n", (int) count_strs);
+ for (j = 0; my_strcodes[j]; ++j)
+ printf("\t%s=%s,\n", my_strcodes[j], my_strvalues[j]);
+#endif
+}
+
+#if USE_CODE_LISTS
+static char **
+copy_code_list(NCURSES_CONST char *const *list)
+{
+ int pass;
+ size_t count;
+ size_t length = 1;
+ char **result = 0;
+ char *unused = 0;
+
+ for (pass = 0; pass < 2; ++pass) {
+ for (count = 0; list[count] != 0; ++count) {
+ size_t chunk = strlen(list[count]) + 1;
+ if (pass == 0) {
+ length += chunk;
+ } else {
+ result[count] = unused;
+ _nc_STRCPY(unused, list[count], length);
+ unused += chunk;
+ }
+ }
+ if (pass == 0) {
+ char *blob = malloc(length);
+ result = typeCalloc(char *, count + 1);
+ unused = blob;
+ if (blob == 0 || result == 0)
+ failed("copy_code_list failed");
+ }
+ }
+
+ return result;