+ }
+
+ /* the ultimate fallback, assume fixed 24x80 size */
+ if (*linep <= 0) {
+ *linep = 24;
+ }
+ if (*colp <= 0) {
+ *colp = 80;
+ }
+}
+
+#if defined(USE_CHECK_SIZE) && defined(user6) && defined(user7)
+static const char *
+skip_csi(const char *value)
+{
+ if (UChar(*value) == CSI_CHR) {
+ ++value;
+ } else if (*value == ESC_CHR && value[1] == L_BLOCK) {
+ value += 2;
+ }
+ return value;
+}
+
+static bool
+is_expected(const char *value, const char *expected)
+{
+ bool result = FALSE;
+ if (VALID_STRING(value)) {
+ const char *skipped = skip_csi(value);
+ if (skipped != value) {
+ if (!strcmp(skipped, expected))
+ result = TRUE;
+ }
+ }
+ return result;
+}
+
+static bool
+get_position(TERMINAL *termp, int fd, int *row, int *col)
+{
+ bool result = FALSE;
+ size_t need = strlen(user7);
+ int have;
+
+ have = (int) write(fd, user7, need);
+
+ if (have == (int) need) {
+ int y, x;
+ char buf[20];
+ char *s;
+ char cc;
+ const char *skipped;
+ int scanned;
+
+ s = memset(buf, '\0', sizeof(buf));
+ do {
+ size_t ask = (sizeof(buf) - 1 - (size_t) (s - buf));
+ int got = (int) read(fd, s, ask);
+ if (got == 0)
+ break;
+ s += got;
+ *s = '\0';
+ } while (strchr(buf, 'R') == NULL && (size_t) (s + 1 - buf) < sizeof(buf));
+ T(("CPR response %s", _nc_visbuf(buf)));
+ skipped = skip_csi(buf);
+ cc = '\0';
+ if (skipped != buf
+ && *skipped != '\0'
+ && (scanned = sscanf(skip_csi(buf), "%d;%d%c", &y, &x, &cc)) == 3
+ && (cc == 'R')) {
+ *row = y;
+ *col = x;
+ result = TRUE;
+ }
+ }
+ T(("get_position %s %d,%d", result ? "OK" : "ERR", *row, *col));
+ return result;
+}
+
+static bool
+set_position(NCURSES_SP_DCLx TERMINAL *termp, int row, int col)
+{
+ bool result;
+ char *actual = TIPARM_2(cursor_address, row, col);
+ T((T_CALLED("set_position %d,%d)"), row, col));
+#if NCURSES_SP_FUNCS
+ result = (NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx "set_position",
+ actual) == OK);
+ NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
+#else
+ /* This does not support padding because without sp-funcs, we have only
+ * the interface using stdio, but we are not guaranteed that Filedes
+ * is the same as fileno(stdout).
+ */
+ result = FALSE;
+ if (actual != NULL) {
+ size_t want = strlen(actual);
+ int have = (int) write(termp->Filedes, actual, want);
+ result = ((int) want == have);
+ }
+#endif
+ returnBool(result);
+}