+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col != 0) {
+ current->cols = ws.ws_col;
+ return 0;
+ }
+
+ /* Failed to query the window size. Perhaps we are on a serial terminal.
+ * Try to query the width by sending the cursor as far to the right
+ * and reading back the cursor position.
+ * Note that this is only done once per call to linenoise rather than
+ * every time the line is refreshed for efficiency reasons.
+ */
+ if (current->cols == 0) {
+ current->cols = 80;
+
+ /* Move cursor far right and report cursor position, then back to the left */
+ fd_printf(current->fd, "\x1b[999C" "\x1b[6n");
+
+ /* Parse the response: ESC [ rows ; cols R */
+ if (fd_read_char(current->fd, 100) == 0x1b && fd_read_char(current->fd, 100) == '[') {
+ int n = 0;
+ while (1) {
+ int ch = fd_read_char(current->fd, 100);
+ if (ch == ';') {
+ /* Ignore rows */
+ n = 0;
+ }
+ else if (ch == 'R') {
+ /* Got cols */
+ if (n != 0 && n < 1000) {
+ current->cols = n;
+ }
+ break;
+ }
+ else if (ch >= 0 && ch <= '9') {
+ n = n * 10 + ch - '0';
+ }
+ else {
+ break;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * If escape (27) was received, reads subsequent
+ * chars to determine if this is a known special key.
+ *
+ * Returns SPECIAL_NONE if unrecognised, or -1 if EOF.
+ *
+ * If no additional char is received within a short time,
+ * 27 is returned.
+ */
+static int check_special(int fd)
+{
+ int c = fd_read_char(fd, 50);
+ int c2;
+
+ if (c < 0) {
+ return 27;
+ }
+
+ c2 = fd_read_char(fd, 50);
+ if (c2 < 0) {
+ return c2;
+ }
+ if (c == '[' || c == 'O') {
+ /* Potential arrow key */
+ switch (c2) {
+ case 'A':
+ return SPECIAL_UP;
+ case 'B':
+ return SPECIAL_DOWN;
+ case 'C':
+ return SPECIAL_RIGHT;
+ case 'D':
+ return SPECIAL_LEFT;
+ case 'F':
+ return SPECIAL_END;
+ case 'H':
+ return SPECIAL_HOME;
+ }
+ }
+ if (c == '[' && c2 >= '1' && c2 <= '8') {
+ /* extended escape */
+ c = fd_read_char(fd, 50);
+ if (c == '~') {
+ switch (c2) {
+ case '2':
+ return SPECIAL_INSERT;
+ case '3':
+ return SPECIAL_DELETE;
+ case '5':
+ return SPECIAL_PAGE_UP;
+ case '6':
+ return SPECIAL_PAGE_DOWN;
+ case '7':
+ return SPECIAL_HOME;
+ case '8':
+ return SPECIAL_END;
+ }
+ }
+ while (c != -1 && c != '~') {
+ /* .e.g \e[12~ or '\e[11;2~ discard the complete sequence */
+ c = fd_read_char(fd, 50);
+ }
+ }
+
+ return SPECIAL_NONE;
+}
+#elif defined(USE_WINCONSOLE)
+
+static DWORD orig_consolemode = 0;
+
+static int enableRawMode(struct current *current) {
+ DWORD n;
+ INPUT_RECORD irec;
+
+ current->outh = GetStdHandle(STD_OUTPUT_HANDLE);
+ current->inh = GetStdHandle(STD_INPUT_HANDLE);
+
+ if (!PeekConsoleInput(current->inh, &irec, 1, &n)) {
+ return -1;
+ }
+ if (getWindowSize(current) != 0) {
+ return -1;
+ }
+ if (GetConsoleMode(current->inh, &orig_consolemode)) {
+ SetConsoleMode(current->inh, ENABLE_PROCESSED_INPUT);
+ }
+ return 0;
+}
+
+static void disableRawMode(struct current *current)
+{
+ SetConsoleMode(current->inh, orig_consolemode);
+}
+
+static void clearScreen(struct current *current)
+{
+ COORD topleft = { 0, 0 };
+ DWORD n;
+
+ FillConsoleOutputCharacter(current->outh, ' ',
+ current->cols * current->rows, topleft, &n);
+ FillConsoleOutputAttribute(current->outh,
+ FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN,
+ current->cols * current->rows, topleft, &n);
+ SetConsoleCursorPosition(current->outh, topleft);
+}
+
+static void cursorToLeft(struct current *current)
+{
+ COORD pos = { 0, (SHORT)current->y };
+ DWORD n;
+
+ FillConsoleOutputAttribute(current->outh,
+ FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN, current->cols, pos, &n);
+ current->x = 0;
+}
+
+static int outputChars(struct current *current, const char *buf, int len)
+{
+ COORD pos = { (SHORT)current->x, (SHORT)current->y };
+ DWORD n;
+
+ WriteConsoleOutputCharacter(current->outh, buf, len, pos, &n);
+ current->x += len;
+ return 0;
+}
+
+static void outputControlChar(struct current *current, char ch)
+{
+ COORD pos = { (SHORT)current->x, (SHORT)current->y };
+ DWORD n;
+
+ FillConsoleOutputAttribute(current->outh, BACKGROUND_INTENSITY, 2, pos, &n);
+ outputChars(current, "^", 1);
+ outputChars(current, &ch, 1);
+}
+
+static void eraseEol(struct current *current)
+{
+ COORD pos = { (SHORT)current->x, (SHORT)current->y };
+ DWORD n;
+
+ FillConsoleOutputCharacter(current->outh, ' ', current->cols - current->x, pos, &n);
+}
+
+static void setCursorPos(struct current *current, int x)
+{
+ COORD pos = { (SHORT)x, (SHORT)current->y };
+
+ SetConsoleCursorPosition(current->outh, pos);
+ current->x = x;
+}
+
+static int fd_read(struct current *current)
+{
+ while (1) {
+ INPUT_RECORD irec;
+ DWORD n;
+ if (WaitForSingleObject(current->inh, INFINITE) != WAIT_OBJECT_0) {
+ break;
+ }
+ if (!ReadConsoleInput (current->inh, &irec, 1, &n)) {
+ break;
+ }
+ if (irec.EventType == KEY_EVENT && irec.Event.KeyEvent.bKeyDown) {
+ KEY_EVENT_RECORD *k = &irec.Event.KeyEvent;
+ if (k->dwControlKeyState & ENHANCED_KEY) {
+ switch (k->wVirtualKeyCode) {
+ case VK_LEFT:
+ return SPECIAL_LEFT;
+ case VK_RIGHT:
+ return SPECIAL_RIGHT;
+ case VK_UP:
+ return SPECIAL_UP;
+ case VK_DOWN:
+ return SPECIAL_DOWN;
+ case VK_INSERT:
+ return SPECIAL_INSERT;
+ case VK_DELETE:
+ return SPECIAL_DELETE;
+ case VK_HOME:
+ return SPECIAL_HOME;
+ case VK_END:
+ return SPECIAL_END;
+ case VK_PRIOR:
+ return SPECIAL_PAGE_UP;
+ case VK_NEXT:
+ return SPECIAL_PAGE_DOWN;
+ }
+ }
+ /* Note that control characters are already translated in AsciiChar */
+ else {
+#ifdef USE_UTF8
+ return k->uChar.UnicodeChar;
+#else
+ return k->uChar.AsciiChar;
+#endif
+ }
+ }
+ }
+ return -1;
+}
+
+static int countColorControlChars(const char* prompt)
+{
+ /* For windows we assume that there are no embedded ansi color
+ * control sequences.
+ */
+ return 0;
+}
+
+static int getWindowSize(struct current *current)
+{
+ CONSOLE_SCREEN_BUFFER_INFO info;
+ if (!GetConsoleScreenBufferInfo(current->outh, &info)) {
+ return -1;
+ }
+ current->cols = info.dwSize.X;
+ current->rows = info.dwSize.Y;
+ if (current->cols <= 0 || current->rows <= 0) {
+ current->cols = 80;
+ return -1;
+ }
+ current->y = info.dwCursorPosition.Y;
+ current->x = info.dwCursorPosition.X;
+ return 0;
+}
+#endif
+
+static int utf8_getchars(char *buf, int c)
+{
+#ifdef USE_UTF8
+ return utf8_fromunicode(buf, c);
+#else
+ *buf = c;
+ return 1;
+#endif
+}
+
+/**
+ * Returns the unicode character at the given offset,
+ * or -1 if none.
+ */
+static int get_char(struct current *current, int pos)
+{
+ if (pos >= 0 && pos < current->chars) {
+ int c;
+ int i = utf8_index(current->buf, pos);
+ (void)utf8_tounicode(current->buf + i, &c);
+ return c;
+ }
+ return -1;