+/*
+ * Check if the alternate character-set capabilities are consistent.
+ */
+static void
+check_acs(TERMTYPE *tp)
+{
+ if (VALID_STRING(acs_chars)) {
+ const char *boxes = "lmkjtuvwqxn";
+ char mapped[256];
+ char missing[256];
+ const char *p;
+ char *q;
+
+ memset(mapped, 0, sizeof(mapped));
+ for (p = acs_chars; *p != '\0'; p += 2) {
+ if (p[1] == '\0') {
+ _nc_warning("acsc has odd number of characters");
+ break;
+ }
+ mapped[UChar(p[0])] = p[1];
+ }
+
+ if (mapped[UChar('I')] && !mapped[UChar('i')]) {
+ _nc_warning("acsc refers to 'I', which is probably an error");
+ }
+
+ for (p = boxes, q = missing; *p != '\0'; ++p) {
+ if (!mapped[UChar(p[0])]) {
+ *q++ = p[0];
+ }
+ }
+ *q = '\0';
+
+ assert(strlen(missing) <= strlen(boxes));
+ if (*missing != '\0' && strcmp(missing, boxes)) {
+ _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
+ }
+ }
+}
+
+/*
+ * Check if the color capabilities are consistent
+ */
+static void
+check_colors(TERMTYPE *tp)
+{
+ if ((max_colors > 0) != (max_pairs > 0)
+ || ((max_colors > max_pairs) && (initialize_pair == 0)))
+ _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
+ max_colors, max_pairs);
+
+ PAIRED(set_foreground, set_background);
+ PAIRED(set_a_foreground, set_a_background);
+ PAIRED(set_color_pair, initialize_pair);
+
+ if (VALID_STRING(set_foreground)
+ && VALID_STRING(set_a_foreground)
+ && !_nc_capcmp(set_foreground, set_a_foreground))
+ _nc_warning("expected setf/setaf to be different");
+
+ if (VALID_STRING(set_background)
+ && VALID_STRING(set_a_background)
+ && !_nc_capcmp(set_background, set_a_background))
+ _nc_warning("expected setb/setab to be different");
+
+ /* see: has_colors() */
+ if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
+ && (((set_foreground != NULL)
+ && (set_background != NULL))
+ || ((set_a_foreground != NULL)
+ && (set_a_background != NULL))
+ || set_color_pair)) {
+ if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
+ _nc_warning("expected either op/oc string for resetting colors");
+ }
+}
+
+static char
+keypad_final(const char *string)
+{
+ char result = '\0';
+
+ if (VALID_STRING(string)
+ && *string++ == '\033'
+ && *string++ == 'O'
+ && strlen(string) == 1) {
+ result = *string;
+ }
+
+ return result;
+}
+
+static long
+keypad_index(const char *string)
+{
+ char *test;
+ const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
+ int ch;
+ long result = -1;
+
+ if ((ch = keypad_final(string)) != '\0') {
+ test = strchr(list, ch);
+ if (test != 0)
+ result = (long) (test - list);
+ }
+ return result;
+}
+
+/*
+ * list[] is down, up, left, right
+ * "left" may be ^H rather than \E[D
+ * "down" may be ^J rather than \E[B
+ * But up/right are generally consistently escape sequences for ANSI terminals.
+ */
+static void
+check_ansi_cursor(char *list[4])
+{
+ int j, k;
+ int want;
+ size_t prefix = 0;
+ size_t suffix;
+ bool skip[4];
+ bool repeated = FALSE;
+
+ for (j = 0; j < 4; ++j) {
+ skip[j] = FALSE;
+ for (k = 0; k < j; ++k) {
+ if (j != k
+ && !strcmp(list[j], list[k])) {
+ char *value = _nc_tic_expand(list[k], TRUE, 0);
+ _nc_warning("repeated cursor control %s\n", value);
+ repeated = TRUE;
+ }
+ }
+ }
+ if (!repeated) {
+ char *up = list[1];
+
+ if (UChar(up[0]) == '\033') {
+ if (up[1] == '[') {
+ prefix = 2;
+ } else {
+ prefix = 1;
+ }
+ } else if (UChar(up[0]) == UChar('\233')) {
+ prefix = 1;
+ }
+ if (prefix) {
+ suffix = prefix;
+ while (up[suffix] && isdigit(UChar(up[suffix])))
+ ++suffix;
+ }
+ if (prefix && up[suffix] == 'A') {
+ skip[1] = TRUE;
+ if (!strcmp(list[0], "\n"))
+ skip[0] = TRUE;
+ if (!strcmp(list[2], "\b"))
+ skip[2] = TRUE;
+
+ for (j = 0; j < 4; ++j) {
+ if (skip[j] || strlen(list[j]) == 1)
+ continue;
+ if (memcmp(list[j], up, prefix)) {
+ char *value = _nc_tic_expand(list[j], TRUE, 0);
+ _nc_warning("inconsistent prefix for %s\n", value);
+ continue;
+ }
+ if (strlen(list[j]) < suffix) {
+ char *value = _nc_tic_expand(list[j], TRUE, 0);
+ _nc_warning("inconsistent length for %s, expected %d\n",
+ value, (int) suffix + 1);
+ continue;
+ }
+ want = "BADC"[j];
+ if (list[j][suffix] != want) {
+ char *value = _nc_tic_expand(list[j], TRUE, 0);
+ _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
+ value, want, list[j][suffix]);
+ }
+ }
+ }
+ }
+}
+
+#define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
+
+static void
+check_cursor(TERMTYPE *tp)
+{
+ int count;
+ char *list[4];
+
+ /* if we have a parameterized form, then the non-parameterized is easy */
+ ANDMISSING(parm_down_cursor, cursor_down);
+ ANDMISSING(parm_up_cursor, cursor_up);
+ ANDMISSING(parm_left_cursor, cursor_left);
+ ANDMISSING(parm_right_cursor, cursor_right);
+
+ /* Given any of a set of cursor movement, the whole set should be present.
+ * Technically this is not true (we could use cursor_address to fill in
+ * unsupported controls), but it is likely.
+ */
+ count = 0;
+ if (PRESENT(parm_down_cursor)) {
+ list[count++] = parm_down_cursor;
+ }
+ if (PRESENT(parm_up_cursor)) {
+ list[count++] = parm_up_cursor;
+ }
+ if (PRESENT(parm_left_cursor)) {
+ list[count++] = parm_left_cursor;
+ }
+ if (PRESENT(parm_right_cursor)) {
+ list[count++] = parm_right_cursor;
+ }
+ if (count == 4) {
+ check_ansi_cursor(list);
+ } else if (count != 0) {
+ EXPECTED(parm_down_cursor);
+ EXPECTED(parm_up_cursor);
+ EXPECTED(parm_left_cursor);
+ EXPECTED(parm_right_cursor);
+ }
+
+ count = 0;
+ if (PRESENT(cursor_down)) {
+ list[count++] = cursor_down;
+ }
+ if (PRESENT(cursor_up)) {
+ list[count++] = cursor_up;
+ }
+ if (PRESENT(cursor_left)) {
+ list[count++] = cursor_left;
+ }
+ if (PRESENT(cursor_right)) {
+ list[count++] = cursor_right;
+ }
+ if (count == 4) {
+ check_ansi_cursor(list);
+ } else if (count != 0) {
+ count = 0;
+ if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
+ ++count;
+ if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
+ ++count;
+ if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
+ ++count;
+ if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
+ ++count;
+ if (count) {
+ EXPECTED(cursor_down);
+ EXPECTED(cursor_up);
+ EXPECTED(cursor_left);
+ EXPECTED(cursor_right);
+ }
+ }
+}
+
+#define MAX_KP 5
+/*
+ * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
+ * is mapped inconsistently.
+ */
+static void
+check_keypad(TERMTYPE *tp)
+{
+ char show[80];
+
+ if (VALID_STRING(key_a1) &&
+ VALID_STRING(key_a3) &&
+ VALID_STRING(key_b2) &&
+ VALID_STRING(key_c1) &&
+ VALID_STRING(key_c3)) {
+ char final[MAX_KP + 1];
+ long list[MAX_KP];
+ int increase = 0;
+ int j, k, kk;
+ long last;
+ long test;
+
+ final[0] = keypad_final(key_a1);
+ final[1] = keypad_final(key_a3);
+ final[2] = keypad_final(key_b2);
+ final[3] = keypad_final(key_c1);
+ final[4] = keypad_final(key_c3);
+ final[5] = '\0';
+
+ /* special case: legacy coding using 1,2,3,0,. on the bottom */
+ assert(strlen(final) <= MAX_KP);
+ if (!strcmp(final, "qsrpn"))
+ return;
+
+ list[0] = keypad_index(key_a1);
+ list[1] = keypad_index(key_a3);
+ list[2] = keypad_index(key_b2);
+ list[3] = keypad_index(key_c1);
+ list[4] = keypad_index(key_c3);
+
+ /* check that they're all vt100 keys */
+ for (j = 0; j < MAX_KP; ++j) {
+ if (list[j] < 0) {
+ return;
+ }
+ }
+
+ /* check if they're all in increasing order */
+ for (j = 1; j < MAX_KP; ++j) {
+ if (list[j] > list[j - 1]) {
+ ++increase;
+ }
+ }
+ if (increase != (MAX_KP - 1)) {
+ show[0] = '\0';
+
+ for (j = 0, last = -1; j < MAX_KP; ++j) {
+ for (k = 0, kk = -1, test = 100; k < 5; ++k) {
+ if (list[k] > last &&
+ list[k] < test) {
+ test = list[k];
+ kk = k;
+ }
+ }
+ last = test;
+ assert(strlen(show) < (MAX_KP * 4));
+ switch (kk) {
+ case 0:
+ strcat(show, " ka1");
+ break;
+ case 1:
+ strcat(show, " ka3");
+ break;
+ case 2:
+ strcat(show, " kb2");
+ break;
+ case 3:
+ strcat(show, " kc1");
+ break;
+ case 4:
+ strcat(show, " kc3");
+ break;
+ }
+ }
+
+ _nc_warning("vt100 keypad order inconsistent: %s", show);
+ }
+
+ } else if (VALID_STRING(key_a1) ||
+ VALID_STRING(key_a3) ||
+ VALID_STRING(key_b2) ||
+ VALID_STRING(key_c1) ||
+ VALID_STRING(key_c3)) {
+ show[0] = '\0';
+ if (keypad_index(key_a1) >= 0)
+ strcat(show, " ka1");
+ if (keypad_index(key_a3) >= 0)
+ strcat(show, " ka3");
+ if (keypad_index(key_b2) >= 0)
+ strcat(show, " kb2");
+ if (keypad_index(key_c1) >= 0)
+ strcat(show, " kc1");
+ if (keypad_index(key_c3) >= 0)
+ strcat(show, " kc3");
+ if (*show != '\0')
+ _nc_warning("vt100 keypad map incomplete:%s", show);
+ }
+}
+
+static void
+check_printer(TERMTYPE *tp)
+{
+ PAIRED(enter_doublewide_mode, exit_doublewide_mode);
+ PAIRED(enter_italics_mode, exit_italics_mode);
+ PAIRED(enter_leftward_mode, exit_leftward_mode);
+ PAIRED(enter_micro_mode, exit_micro_mode);
+ PAIRED(enter_shadow_mode, exit_shadow_mode);
+ PAIRED(enter_subscript_mode, exit_subscript_mode);
+ PAIRED(enter_superscript_mode, exit_superscript_mode);
+ PAIRED(enter_upward_mode, exit_upward_mode);
+
+ ANDMISSING(start_char_set_def, stop_char_set_def);
+
+ /* if we have a parameterized form, then the non-parameterized is easy */
+ ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
+ ANDMISSING(set_left_margin_parm, set_left_margin);
+ ANDMISSING(set_right_margin_parm, set_right_margin);
+ ANDMISSING(set_top_margin_parm, set_top_margin);
+
+ ANDMISSING(parm_down_micro, micro_down);
+ ANDMISSING(parm_left_micro, micro_left);
+ ANDMISSING(parm_right_micro, micro_right);
+ ANDMISSING(parm_up_micro, micro_up);
+}
+
+/*
+ * Returns the expected number of parameters for the given capability.
+ */
+static int
+expected_params(const char *name)
+{
+ /* *INDENT-OFF* */
+ static const struct {
+ const char *name;
+ int count;
+ } table[] = {
+ { "S0", 1 }, /* 'screen' extension */
+ { "birep", 2 },
+ { "chr", 1 },
+ { "colornm", 1 },
+ { "cpi", 1 },
+ { "csnm", 1 },
+ { "csr", 2 },
+ { "cub", 1 },
+ { "cud", 1 },
+ { "cuf", 1 },
+ { "cup", 2 },
+ { "cuu", 1 },
+ { "cvr", 1 },
+ { "cwin", 5 },
+ { "dch", 1 },
+ { "defc", 3 },
+ { "dial", 1 },
+ { "dispc", 1 },
+ { "dl", 1 },
+ { "ech", 1 },
+ { "getm", 1 },
+ { "hpa", 1 },
+ { "ich", 1 },
+ { "il", 1 },
+ { "indn", 1 },
+ { "initc", 4 },
+ { "initp", 7 },
+ { "lpi", 1 },
+ { "mc5p", 1 },
+ { "mrcup", 2 },
+ { "mvpa", 1 },
+ { "pfkey", 2 },
+ { "pfloc", 2 },
+ { "pfx", 2 },
+ { "pfxl", 3 },
+ { "pln", 2 },
+ { "qdial", 1 },
+ { "rcsd", 1 },
+ { "rep", 2 },
+ { "rin", 1 },
+ { "sclk", 3 },
+ { "scp", 1 },
+ { "scs", 1 },
+ { "scsd", 2 },
+ { "setab", 1 },
+ { "setaf", 1 },
+ { "setb", 1 },
+ { "setcolor", 1 },
+ { "setf", 1 },
+ { "sgr", 9 },
+ { "sgr1", 6 },
+ { "slength", 1 },
+ { "slines", 1 },
+ { "smgbp", 1 }, /* 2 if smgtp is not given */
+ { "smglp", 1 },
+ { "smglr", 2 },
+ { "smgrp", 1 },
+ { "smgtb", 2 },
+ { "smgtp", 1 },
+ { "tsl", 1 },
+ { "u6", -1 },
+ { "vpa", 1 },
+ { "wind", 4 },
+ { "wingo", 1 },
+ };
+ /* *INDENT-ON* */
+
+ unsigned n;
+ int result = 0; /* function-keys, etc., use none */
+
+ for (n = 0; n < SIZEOF(table); n++) {
+ if (!strcmp(name, table[n].name)) {
+ result = table[n].count;
+ break;
+ }
+ }
+
+ return result;
+}
+
+/*
+ * Make a quick sanity check for the parameters which are used in the given
+ * strings. If there are no "%p" tokens, then there should be no other "%"
+ * markers.
+ */
+static void
+check_params(TERMTYPE *tp, const char *name, char *value)
+{
+ int expected = expected_params(name);
+ int actual = 0;
+ int n;
+ bool params[10];
+ char *s = value;
+
+#ifdef set_top_margin_parm
+ if (!strcmp(name, "smgbp")
+ && set_top_margin_parm == 0)
+ expected = 2;
+#endif
+
+ for (n = 0; n < 10; n++)
+ params[n] = FALSE;
+
+ while (*s != 0) {
+ if (*s == '%') {
+ if (*++s == '\0') {
+ _nc_warning("expected character after %% in %s", name);
+ break;
+ } else if (*s == 'p') {
+ if (*++s == '\0' || !isdigit((int) *s)) {
+ _nc_warning("expected digit after %%p in %s", name);
+ return;
+ } else {
+ n = (*s - '0');
+ if (n > actual)
+ actual = n;
+ params[n] = TRUE;
+ }
+ }
+ }
+ s++;
+ }
+
+ if (params[0]) {
+ _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
+ }
+ if (value == set_attributes || expected < 0) {
+ ;
+ } else if (expected != actual) {
+ _nc_warning("%s uses %d parameters, expected %d", name,
+ actual, expected);
+ for (n = 1; n < actual; n++) {
+ if (!params[n])
+ _nc_warning("%s omits parameter %d", name, n);
+ }
+ }
+}
+
+static char *
+skip_delay(char *s)
+{
+ while (*s == '/' || isdigit(UChar(*s)))
+ ++s;
+ return s;
+}
+
+/*
+ * Skip a delay altogether, e.g., when comparing a simple string to sgr,
+ * the latter may have a worst-case delay on the end.
+ */
+static char *
+ignore_delays(char *s)
+{
+ int delaying = 0;
+
+ do {
+ switch (*s) {
+ case '$':
+ if (delaying == 0)
+ delaying = 1;
+ break;
+ case '<':
+ if (delaying == 1)
+ delaying = 2;
+ break;
+ case '\0':
+ delaying = 0;
+ break;
+ default:
+ if (delaying) {
+ s = skip_delay(s);
+ if (*s == '>')
+ ++s;
+ delaying = 0;
+ }
+ break;
+ }
+ if (delaying)
+ ++s;
+ } while (delaying);
+ return s;
+}
+