int key_string_lookup_string(const char *string) { int key; const u_char *ptr; if (string[0] == '\0') return (KEYC_NONE); if (string[1] == '\0') return (string[0]); ptr = NULL; if ((string[0] == 'C' || string[0] == 'c') && string[1] == '-') ptr = string + 2; else if (string[0] == '^') ptr = string + 1; if (ptr != NULL) { if (ptr[0] == '\0') return (KEYC_NONE); if (ptr[1] == '\0') { if (ptr[0] == 32) return (0); if (ptr[0] == 63) return (KEYC_BSPACE); if (ptr[0] >= 64 && ptr[0] <= 95) return (ptr[0] - 64); if (ptr[0] >= 97 && ptr[0] <= 122) return (ptr[0] - 96); return (KEYC_NONE); } key = key_string_search_table(ptr); if (key != KEYC_NONE) return (key | KEYC_CTRL); return (KEYC_NONE); } if ((string[0] == 'M' || string[0] == 'm') && string[1] == '-') { ptr = string + 2; if (ptr[0] == '\0') return (KEYC_NONE); if (ptr[1] == '\0') { if (ptr[0] < 32 || ptr[0] > 127) return (KEYC_NONE); return (ptr[0] | KEYC_ESCAPE); } key = key_string_lookup_string(ptr); if (key != KEYC_NONE) return (key | KEYC_ESCAPE); return (KEYC_NONE); } return (key_string_search_table(string)); }
/* Lookup a string and convert to a key value. */ int key_string_lookup_string(const char *string) { int key, modifiers; u_short u; int size; /* Is this a hexadecimal value? */ if (string[0] == '0' && string[1] == 'x') { if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4) return (KEYC_NONE); return (u); } /* Check for modifiers. */ modifiers = 0; if (string[0] == '^' && string[1] != '\0') { modifiers |= KEYC_CTRL; string++; } modifiers |= key_string_get_modifiers(&string); if (string[0] == '\0') return (KEYC_NONE); /* Is this a standard ASCII key? */ if (string[1] == '\0') { key = (u_char) string[0]; if (key < 32 || key == 127 || key > 255) return (KEYC_NONE); } else { /* Otherwise look the key up in the table. */ key = key_string_search_table(string); if (key == KEYC_NONE) return (KEYC_NONE); } /* Convert the standard control keys. */ if (key < KEYC_BASE && (modifiers & KEYC_CTRL)) { if (key >= 97 && key <= 122) key -= 96; else if (key >= 64 && key <= 95) key -= 64; else if (key == 32) key = 0; else if (key == 63) key = KEYC_BSPACE; else return (KEYC_NONE); modifiers &= ~KEYC_CTRL; } return (key | modifiers); }
/* Lookup a string and convert to a key value. */ key_code key_string_lookup_string(const char *string) { static const char *other = "!#()+,-.0123456789:;<=>?'\r\t"; key_code key; u_short u; int size; key_code modifiers; struct utf8_data ud; u_int i; enum utf8_state more; wchar_t wc; /* Is this no key? */ if (strcasecmp(string, "None") == 0) return (KEYC_NONE); /* Is this a hexadecimal value? */ if (string[0] == '0' && string[1] == 'x') { if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4) return (KEYC_UNKNOWN); return (u); } /* Check for modifiers. */ modifiers = 0; if (string[0] == '^' && string[1] != '\0') { modifiers |= KEYC_CTRL; string++; } modifiers |= key_string_get_modifiers(&string); if (string[0] == '\0') return (KEYC_UNKNOWN); /* Is this a standard ASCII key? */ if (string[1] == '\0' && (u_char)string[0] <= 127) { key = (u_char)string[0]; if (key < 32 || key == 127) return (KEYC_UNKNOWN); } else { /* Try as a UTF-8 key. */ if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) { if (strlen(string) != ud.size) return (KEYC_UNKNOWN); for (i = 1; i < ud.size; i++) more = utf8_append(&ud, (u_char)string[i]); if (more != UTF8_DONE) return (KEYC_UNKNOWN); if (utf8_combine(&ud, &wc) != UTF8_DONE) return (KEYC_UNKNOWN); return (wc | modifiers); } /* Otherwise look the key up in the table. */ key = key_string_search_table(string); if (key == KEYC_UNKNOWN) return (KEYC_UNKNOWN); } /* Convert the standard control keys. */ if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) { if (key >= 97 && key <= 122) key -= 96; else if (key >= 64 && key <= 95) key -= 64; else if (key == 32) key = 0; else if (key == 63) key = KEYC_BSPACE; else return (KEYC_UNKNOWN); modifiers &= ~KEYC_CTRL; } return (key | modifiers); }
/* Lookup a string and convert to a key value, handling C-/M-/S- prefix. */ int key_string_lookup_string(const char *string) { int key; const char *ptr; if (string[0] == '\0') return (KEYC_NONE); if (string[1] == '\0') return ((u_char) string[0]); ptr = NULL; if ((string[0] == 'C' || string[0] == 'c') && string[1] == '-') ptr = string + 2; else if (string[0] == '^') ptr = string + 1; if (ptr != NULL) { if (ptr[0] == '\0') return (KEYC_NONE); /* * Lookup as a named key. If a function key (>= KEYC_BASE), * return it with the ctrl modifier, otherwise fallthrough with * the key value from the table (eg for C-Space). If not a * named key, check for single character keys and try that. */ key = key_string_search_table(ptr); if (key != KEYC_NONE) { if (key >= KEYC_BASE) return (key | KEYC_CTRL); } else { if (ptr[1] != '\0') return (KEYC_NONE); key = (u_char) ptr[0]; } /* * Figure out if the single character in key is a valid ctrl * key. */ if (key == 32) return (0); if (key == 63) return (KEYC_BSPACE); if (key >= 64 && key <= 95) return (key - 64); if (key >= 97 && key <= 122) return (key - 96); return (KEYC_NONE); } if ((string[0] == 'M' || string[0] == 'm') && string[1] == '-') { ptr = string + 2; if (ptr[0] == '\0') return (KEYC_NONE); key = key_string_lookup_string(ptr); if (key != KEYC_NONE) { if (key >= KEYC_BASE) return (key | KEYC_ESCAPE); } else { if (ptr[1] == '\0') return (KEYC_NONE); key = (u_char) ptr[0]; } if (key >= 32 && key <= 127) return (key | KEYC_ESCAPE); return (KEYC_NONE); } if ((string[0] == 'S' || string[0] == 's') && string[1] == '-') { ptr = string + 2; if (ptr[0] == '\0') return (KEYC_NONE); key = key_string_lookup_string(ptr); if (key != KEYC_NONE) { if (key >= KEYC_BASE) return (key | KEYC_SHIFT); } else { if (ptr[1] == '\0') return (KEYC_NONE); key = (u_char) ptr[0]; } if (key >= 32 && key <= 127) return (key | KEYC_SHIFT); return (KEYC_NONE); } return (key_string_search_table(string)); }