+#ifdef NCURSES_VERSION
+ if (x_opt && (my_blob == 0) && y_opt) {
+#if NCURSES_XNAMES
+ TERMTYPE *term = &(cur_term->type);
+ if (term != 0
+ && ((NUM_BOOLEANS(term) != BOOLCOUNT)
+ || (NUM_NUMBERS(term) != NUMCOUNT)
+ || (NUM_STRINGS(term) != STRCOUNT))) {
+ for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
+ dump_xname(ExtBoolname(term, (int) n, boolnames));
+ }
+ for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
+ dump_xname(ExtNumname(term, (int) n, numnames));
+ }
+ for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
+ dump_xname(ExtStrname(term, (int) n, strnames));
+ }
+ }
+#endif
+ }
+#endif
+ }
+}
+
+typedef enum {
+ pDefault = 0
+ ,pComment
+ ,pDescription
+ ,pEscaped
+ ,pNewline
+ ,pName
+ ,pNumber
+ ,pString
+} STATE;
+
+static void
+parse_description(const char *input_name)
+{
+ 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;
+ 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);
+ 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;
+ break;
+ }
+ my_blob[k++] = (char) ch;
+ 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 = 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 '\\':
+ if (my_blob[j + 1] == '\0') {
+ state = pDefault;
+ continue;
+ }
+ break;
+ case '\n':
+ state = pNewline;
+ continue;
+ case ':':
+ state = pDefault;
+ break;
+ }
+ my_blob[k++] = (char) ch;
+ 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;
+ /*
+ * Commented-out capabilities might be accessible (they are in
+ * ncurses).
+ */
+ if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
+ j += 2;
+ }
+ if (my_blob[j + 1] != '\0') {
+ switch (my_blob[j + 2]) {
+ case '#':
+ my_numvalues[count_nums] = &my_blob[j + 3];
+ my_numcodes[count_nums++] = &my_blob[j];
+ my_blob[j + 2] = '\0';
+ state = pNumber;
+ j += 2;
+ break;
+ case '=':
+ my_strvalues[count_strs] = &my_blob[j + 3];
+ my_strcodes[count_strs++] = &my_blob[j];
+ my_blob[j + 2] = '\0';
+ state = pString;
+ j += 2;
+ break;
+ default:
+ if (my_blob[j + 2] == '@') {
+ /*
+ * 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] = "";
+ my_strcodes[count_strs++] = &my_blob[j];
+ } else if (count_nums) {
+ my_numvalues[count_nums] = "";
+ my_numcodes[count_nums++] = &my_blob[j];
+ } else {
+ my_boolcodes[count_bools++] = &my_blob[j];
+ }
+ } else {
+ my_boolcodes[count_bools++] = &my_blob[j];
+ }
+ j++;
+ 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') {
+ state = pDefault;
+ continue;
+ } else {
+ ++j;
+ }
+ break;
+ case '\n':
+ state = pNewline;
+ continue;
+ 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("%5d:%s\n", (int) j, my_boolcodes[j]);
+
+ printf("numbers:%d\n", (int) count_nums);
+ for (j = 0; my_numcodes[j]; ++j)
+ printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
+
+ printf("strings:%d\n", (int) count_strs);
+ for (j = 0; my_strcodes[j]; ++j)
+ printf("%5d:%s(%s)\n", (int) j, 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 = 0;
+ char **result = 0;
+ char *blob = 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;
+ strcpy(unused, list[count]);
+ unused += chunk;
+ }
+ }
+ if (pass == 0) {
+ blob = malloc(length);
+ result = typeCalloc(char *, count + 1);
+ unused = blob;
+ if (blob == 0 || result == 0)
+ failed("copy_code_list failed");
+ }