void keytostr(char *buf, int buf_size, int key) { int i; char buf1[32]; for (i = 0; i < (int)(sizeof(keycodes)/sizeof(keycodes[0])); i++) { if (keycodes[i] == key) { pstrcpy(buf, buf_size, keystr[i]); return; } } if (key >= KEY_META(0) && key <= KEY_META(0xff)) { keytostr(buf1, sizeof(buf1), key & 0xff); snprintf(buf, buf_size, "M-%s", buf1); } else if (key >= KEY_CTRL('a') && key <= KEY_CTRL('z')) { snprintf(buf, buf_size, "C-%c", key + 'a' - 1); } else if (key >= KEY_F1 && key <= KEY_F20) { snprintf(buf, buf_size, "f%d", key - KEY_F1 + 1); } else if (key > 32 && key < 127 && buf_size >= 2) { buf[0] = key; buf[1] = '\0'; } else { char *q; q = utf8_encode(buf1, key); *q = '\0'; pstrcpy(buf, buf_size, buf1); } }
void keytostr(char *buf, int buf_size, int key) { int i; char buf1[32]; for(i=0;i<sizeof(keycodes)/sizeof(keycodes[0]);i++) { if (keycodes[i] == key) { pstrcpy(buf, buf_size, keystr[i]); return; } } if (key >= KEY_META(' ') && key <= KEY_META(127)) { keytostr(buf1, sizeof(buf1), key & 0xff); snprintf(buf, buf_size, "M-%s", buf1); } else if (key >= 1 && key <= 31) { snprintf(buf, buf_size, "C-%c", key + 'a' - 1); } else if (key >= KEY_F1 && key <= KEY_F12) { snprintf(buf, buf_size, "F%d", key - KEY_F1 + 1); } else { char *q; q = utf8_encode(buf, key); *q = '\0'; } }
static void displaysettings(struct termios *m, int all) { const struct key *kbd = keys; const struct mode *mod = modes; struct winsize winsize; speed_t in, out; tcflag_t *bitsp, mask; const char *linestr; in = cfgetispeed(m); out = cfgetospeed(m); if (!in || in == out) { if (all || out != B38400) printtoken("speed %s baud;", baudtostr(out)); } else { printtoken("ispeed %s baud;", baudtostr(in)); printtoken("ospeed %s baud;", baudtostr(out)); } if (all) { if (ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize)) eprintf("TIOCGWINSZ <stdin>:"); printtoken("rows %u;", winsize.ws_row); printtoken("columns %u;", winsize.ws_col); } printtoken("\n"); if (all || m->c_line != 0) { linestr = linetostr(m->c_line); if (linestr) printtoken("line = %s;", linestr); else printtoken("line = %u;", (unsigned)(m->c_line)); } if (all || (m->c_cc[VMIN] != 1 && !(m->c_lflag & ICANON))) printtoken("min = %u;", (unsigned)(m->c_cc[VMIN])); if (all || (m->c_cc[VTIME] != 0 && !(m->c_lflag & ICANON))) printtoken("time = %u;", (unsigned)(m->c_cc[VTIME])); printtoken("\n"); for (; kbd->op; kbd++) if (all || m->c_cc[kbd->index] != kbd->sanevalue) printtoken("%s = %s;", kbd->op, keytostr(m->c_cc[kbd->index])); printtoken("\n"); for (; mod->op; mod++) { switch (mod->type) { case CTRL: bitsp = &m->c_cflag; break; case IN: bitsp = &m->c_iflag; break; case OUT: bitsp = &m->c_oflag; break; case LOCAL: bitsp = &m->c_lflag; break; default: bitsp = 0; break; } if (!bitsp || (mod->flags & DUP)) continue; mask = mod->clear ? mod->clear : mod->set; if ((*bitsp & mask) == mod->set) { if (all || !isdefault(mod->flags)) printtoken("%s", mod->op); } else if (mod->flags & BOOL) { if (all || isdefault(mod->flags)) printtoken("-%s", mod->op); } } printtoken("\n"); }