📄 key.c
字号:
el_free((ptr_t) ptr->val.str); break; default: EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type)); break; } switch (ptr->type = ntype) { case XK_CMD: ptr->val = *val; break; case XK_STR: case XK_EXE: ptr->val.str = strdup(val->str); break; default: EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype)); break; } } else { /* still more chars to go */ if (ptr->next == NULL) ptr->next = node__get(*str); /* setup new node */ (void) node__try(el, ptr->next, str, val, ntype); } return (0);}/* node__delete(): * Delete node that matches str */private intnode__delete(EditLine *el, key_node_t **inptr, const char *str){ key_node_t *ptr; key_node_t *prev_ptr = NULL; ptr = *inptr; if (ptr->ch != *str) { key_node_t *xm; for (xm = ptr; xm->sibling != NULL; xm = xm->sibling) if (xm->sibling->ch == *str) break; if (xm->sibling == NULL) return (0); prev_ptr = xm; ptr = xm->sibling; } if (*++str == '\0') { /* we're there */ if (prev_ptr == NULL) *inptr = ptr->sibling; else prev_ptr->sibling = ptr->sibling; ptr->sibling = NULL; node__put(el, ptr); return (1); } else if (ptr->next != NULL && node__delete(el, &ptr->next, str) == 1) { if (ptr->next != NULL) return (0); if (prev_ptr == NULL) *inptr = ptr->sibling; else prev_ptr->sibling = ptr->sibling; ptr->sibling = NULL; node__put(el, ptr); return (1); } else { return (0); }}/* node__put(): * Puts a tree of nodes onto free list using free(3). */private voidnode__put(EditLine *el, key_node_t *ptr){ if (ptr == NULL) return; if (ptr->next != NULL) { node__put(el, ptr->next); ptr->next = NULL; } node__put(el, ptr->sibling); switch (ptr->type) { case XK_CMD: case XK_NOD: break; case XK_EXE: case XK_STR: if (ptr->val.str != NULL) el_free((ptr_t) ptr->val.str); break; default: EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ptr->type)); break; } el_free((ptr_t) ptr);}/* node__get(): * Returns pointer to an key_node_t for ch. */private key_node_t *node__get(int ch){ key_node_t *ptr; ptr = (key_node_t *) el_malloc((size_t) sizeof(key_node_t)); if (ptr == NULL) return NULL; ptr->ch = ch; ptr->type = XK_NOD; ptr->val.str = NULL; ptr->next = NULL; ptr->sibling = NULL; return (ptr);}/* node_lookup(): * look for the str starting at node ptr. * Print if last node */private intnode_lookup(EditLine *el, const char *str, key_node_t *ptr, int cnt){ int ncnt; if (ptr == NULL) return (-1); /* cannot have null ptr */ if (*str == 0) { /* no more chars in str. node_enum from here. */ (void) node_enum(el, ptr, cnt); return (0); } else { /* If match put this char into el->el_key.buf. Recurse */ if (ptr->ch == *str) { /* match found */ ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch); if (ptr->next != NULL) /* not yet at leaf */ return (node_lookup(el, str + 1, ptr->next, ncnt + 1)); else { /* next node is null so key should be complete */ if (str[1] == 0) { el->el_key.buf[ncnt + 1] = '"'; el->el_key.buf[ncnt + 2] = '\0'; key_kprint(el, el->el_key.buf, &ptr->val, ptr->type); return (0); } else return (-1); /* mismatch -- str still has chars */ } } else { /* no match found try sibling */ if (ptr->sibling) return (node_lookup(el, str, ptr->sibling, cnt)); else return (-1); } }}/* node_enum(): * Traverse the node printing the characters it is bound in buffer */private intnode_enum(EditLine *el, key_node_t *ptr, int cnt){ int ncnt; if (cnt >= KEY_BUFSIZ - 5) { /* buffer too small */ el->el_key.buf[++cnt] = '"'; el->el_key.buf[++cnt] = '\0'; (void) fprintf(el->el_errfile, "Some extended keys too long for internal print buffer"); (void) fprintf(el->el_errfile, " \"%s...\"\n", el->el_key.buf); return (0); } if (ptr == NULL) {#ifdef DEBUG_EDIT (void) fprintf(el->el_errfile, "node_enum: BUG!! Null ptr passed\n!");#endif return (-1); } /* put this char at end of str */ ncnt = key__decode_char(el->el_key.buf, cnt, (unsigned char) ptr->ch); if (ptr->next == NULL) { /* print this key and function */ el->el_key.buf[ncnt + 1] = '"'; el->el_key.buf[ncnt + 2] = '\0'; key_kprint(el, el->el_key.buf, &ptr->val, ptr->type); } else (void) node_enum(el, ptr->next, ncnt + 1); /* go to sibling if there is one */ if (ptr->sibling) (void) node_enum(el, ptr->sibling, cnt); return (0);}/* key_kprint(): * Print the specified key and its associated * function specified by val */protected voidkey_kprint(EditLine *el, const char *key, key_value_t *val, int ntype){ el_bindings_t *fp; char unparsbuf[EL_BUFSIZ]; static const char fmt[] = "%-15s-> %s\n"; if (val != NULL) switch (ntype) { case XK_STR: case XK_EXE: (void) fprintf(el->el_outfile, fmt, key, key__decode_str(val->str, unparsbuf, ntype == XK_STR ? "\"\"" : "[]")); break; case XK_CMD: for (fp = el->el_map.help; fp->name; fp++) if (val->cmd == fp->func) { (void) fprintf(el->el_outfile, fmt, key, fp->name); break; }#ifdef DEBUG_KEY if (fp->name == NULL) (void) fprintf(el->el_outfile, "BUG! Command not found.\n");#endif break; default: EL_ABORT((el->el_errfile, "Bad XK_ type %d\n", ntype)); break; } else (void) fprintf(el->el_outfile, fmt, key, "no input");}/* key__decode_char(): * Put a printable form of char in buf. */private intkey__decode_char(char *buf, int cnt, int ch){ if (ch == 0) { buf[cnt++] = '^'; buf[cnt] = '@'; return (cnt); } if (iscntrl(ch)) { buf[cnt++] = '^'; if (ch == '\177') buf[cnt] = '?'; else buf[cnt] = ch | 0100; } else if (ch == '^') { buf[cnt++] = '\\'; buf[cnt] = '^'; } else if (ch == '\\') { buf[cnt++] = '\\'; buf[cnt] = '\\'; } else if (ch == ' ' || (isprint(ch) && !isspace(ch))) { buf[cnt] = ch; } else { buf[cnt++] = '\\'; buf[cnt++] = (((unsigned int) ch >> 6) & 7) + '0'; buf[cnt++] = (((unsigned int) ch >> 3) & 7) + '0'; buf[cnt] = (ch & 7) + '0'; } return (cnt);}/* key__decode_str(): * Make a printable version of the ey */protected char *key__decode_str(const char *str, char *buf, const char *sep){ char *b; const char *p; b = buf; if (sep[0] != '\0') *b++ = sep[0]; if (*str == 0) { *b++ = '^'; *b++ = '@'; if (sep[0] != '\0' && sep[1] != '\0') *b++ = sep[1]; *b++ = 0; return (buf); } for (p = str; *p != 0; p++) { if (iscntrl((unsigned char) *p)) { *b++ = '^'; if (*p == '\177') *b++ = '?'; else *b++ = *p | 0100; } else if (*p == '^' || *p == '\\') { *b++ = '\\'; *b++ = *p; } else if (*p == ' ' || (isprint((unsigned char) *p) && !isspace((unsigned char) *p))) { *b++ = *p; } else { *b++ = '\\'; *b++ = (((unsigned int) *p >> 6) & 7) + '0'; *b++ = (((unsigned int) *p >> 3) & 7) + '0'; *b++ = (*p & 7) + '0'; } } if (sep[0] != '\0' && sep[1] != '\0') *b++ = sep[1]; *b++ = 0; return (buf); /* should check for overflow */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -