int linenoiseColumns(void) { struct current current; enableRawMode(¤t); getWindowSize(¤t); disableRawMode(¤t); return current.cols; }
int linenoiseLines(void) { struct current current; enableRawMode (¤t); getWindowSize (¤t); disableRawMode (¤t); return current.rows; }
static int linenoiseRaw(char* buf, size_t buflen, const char* prompt) { int count = -1; if (buflen != 0) { if (enableRawMode() == -1) return -1; count = linenoisePrompt(buf, buflen, prompt); disableRawMode(); printf("\n"); } return count; }
int main(int argc, char **argv) { // takdit <ファイル名> という引数のみ実行 if (argc != 2) { fprintf(stderr, "Usage: takdit <filename>\n"); exit(1); } initEditor(); // エディタの初期化 editorSelectSyntaxHighlight(argv[1]); // シンタックスハイライトの適用 editorOpen(argv[1]); // ファイルを開く enableRawMode(STDIN_FILENO); // Rawモードの有効化 editorSetStatusMessage( "HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find"); while (1) { editorRefreshScreen(); // 変更の反映 editorProcessKeypress(STDIN_FILENO); // キー入力待ち } return 0; }
char *linenoise(const char *prompt) { shouldTerminate = 0; if(requestedInteractiveTermination()){ return NULL; } int count; struct current current; char buf[LINENOISE_MAX_LINE]; if (enableRawMode(¤t) == -1) { printf("%s", prompt); fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) { return NULL; } count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { current.buf = buf; current.bufmax = sizeof(buf); current.len = 0; current.chars = 0; current.pos = 0; current.prompt = prompt; count = linenoisePrompt(¤t); disableRawMode(¤t); printf("\n"); if (count == -1) { return NULL; } } return my_strdup(buf); }
char *linenoise(const char *prompt) { int count; struct current current; char buf[LINENOISE_MAX_LINE]; if (enableRawMode(¤t) == -1) { printf("%s", prompt); fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) { return NULL; } count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { current.buf = buf; current.bufmax = sizeof(buf); current.len = 0; current.chars = 0; current.pos = 0; current.prompt = prompt; current.capture = NULL; count = linenoiseEdit(¤t); disableRawMode(¤t); printf("\n"); free(current.capture); if (count == -1) { return NULL; } } return strdup(buf); }
static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) { int fd = STDIN_FILENO; int count; if (buflen == 0) { errno = EINVAL; return -1; } if (!isatty(STDIN_FILENO)) { if (fgets(buf, buflen, stdin) == NULL) return -1; count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { if (enableRawMode(fd) == -1) return -1; count = linenoisePrompt(fd, buf, buflen, prompt); disableRawMode(fd); printf("\n"); } return count; }
/* This function calls the line editing function linenoiseEdit() using * the STDIN file descriptor set in raw mode. */ static int linenoiseRaw(char *buf, size_t buflen, char *prompt) { int count; if (buflen == 0) { errno = EINVAL; return -1; } if (!isatty(STDIN_FILENO)) { /* Not a tty: read from file / pipe. */ if (fgets(buf, buflen, stdin) == NULL) return -1; count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { /* Interactive editing. */ if (enableRawMode(STDIN_FILENO) == -1) return -1; count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt); disableRawMode(STDIN_FILENO); printf("\n"); } return count; }
/* This special mode is used by linenoise in order to print scan codes * on screen for debugging / development purposes. It is implemented * by the linenoise_example program using the --keycodes option. */ void linenoisePrintKeyCodes(void) { char quit[4]; printf("Linenoise key codes debugging mode.\n" "Press keys to see scan codes. Type 'quit' at any time to exit.\n"); if (enableRawMode(STDIN_FILENO) == -1) return; memset(quit,' ',4); while(1) { char c; int nread; nread = read(STDIN_FILENO,&c,1); if (nread <= 0) continue; memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */ quit[sizeof(quit)-1] = c; /* Insert current char on the right. */ if (memcmp(quit,"quit",sizeof(quit)) == 0) break; printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c, (int)c); printf("\r"); /* Go left edge manually, we are in raw mode. */ fflush(stdout); } disableRawMode(STDIN_FILENO); }
void do_console(int fd) { fd_set rfds; struct timeval tv; int rc; if (0 != enableRawMode()) { fprintf(stderr, "Not a TTY?\n"); return; } while(1) { uint8_t c; tv.tv_sec = 1; tv.tv_usec = 0; int maxfd = 1; FD_ZERO(&rfds); FD_SET(0, &rfds); FD_SET(fd, &rfds); maxfd = fd+1; rc = select(maxfd, &rfds, NULL, NULL, &tv); if (rc == -1) { fprintf(stderr, "select failed\n"); return; } else if (rc) { if (FD_ISSET(0, &rfds)) { if(read(0, &c, 1) > 0) { if (c == 0x03 || c == 0x04) // ctrl-d, ctrl-c { return; } if (1 != serialWrite(fd, &c, 1)) { fprintf(stderr, "write error\n"); return; } } } else if (FD_ISSET(fd, &rfds)) { if(serialRead(fd, &c, 1) > 0) { if (1 != write(1, &c, 1)) { fprintf(stderr, "write error\n"); return; } } } } else { // timeout } } }