| 2659 | } |
| 2660 | |
| 2661 | boolean |
| 2662 | bind_key(uchar key, const char *command, boolean user) |
| 2663 | { |
| 2664 | struct ext_func_tab *extcmd; |
| 2665 | long len; |
| 2666 | char *buf, *p = NULL, *lastp = NULL; |
| 2667 | |
| 2668 | /* special case: "nothing" is reserved for unbinding */ |
| 2669 | if (!strcmpi(command, "nothing")) { |
| 2670 | cmdbind_remove(key); |
| 2671 | return TRUE; |
| 2672 | } |
| 2673 | |
| 2674 | /* copy command to buf for modification */ |
| 2675 | len = strlen(command) + 1; |
| 2676 | buf = (char *)alloc(len); |
| 2677 | (void) strncpy(buf, command, len); |
| 2678 | |
| 2679 | /* does buf have a parameter in parenthesis? */ |
| 2680 | if ((p = strchr(buf, '(')) != 0 |
| 2681 | && (lastp = strrchr(buf, ')')) != 0 |
| 2682 | && (lastp > p)) { |
| 2683 | *p = '\0'; |
| 2684 | *lastp = '\0'; |
| 2685 | /* p points to the parameter */ |
| 2686 | p++; |
| 2687 | } |
| 2688 | |
| 2689 | for (extcmd = extcmdlist; extcmd->ef_txt; extcmd++) { |
| 2690 | if (strcmpi(buf, extcmd->ef_txt)) |
| 2691 | continue; |
| 2692 | if ((extcmd->flags & INTERNALCMD) != 0) |
| 2693 | continue; |
| 2694 | cmdbind_add(key, extcmd, user); |
| 2695 | |
| 2696 | if ((extcmd->flags & CMD_PARAM) != 0) { |
| 2697 | if (!p) { |
| 2698 | config_error_add("'%s' requires a parameter", buf); |
| 2699 | } else { |
| 2700 | struct Cmd_bind *bind = cmdbind_get(key); |
| 2701 | int maxlen = min(30, strlen(p)) + 1; |
| 2702 | |
| 2703 | if (maxlen <= 1) { |
| 2704 | config_error_add("Required parameter cannot be empty"); |
| 2705 | } else { |
| 2706 | bind->param = (char *) alloc(maxlen); |
| 2707 | (void) strncpy(bind->param, p, maxlen); |
| 2708 | bind->param[maxlen-1] = '\0'; |
| 2709 | } |
| 2710 | } |
| 2711 | } else if (p && strlen(p) > 0) |
| 2712 | config_error_add("'%s' does not take a parameter", buf); |
| 2713 | |
| 2714 | #if 0 /* silently accept key binding for unavailable command (!SHELL,&c) */ |
| 2715 | if ((extcmd->flags & CMD_NOT_AVAILABLE) != 0) { |
| 2716 | char buf[BUFSZ]; |
| 2717 | |
| 2718 | Sprintf(buf, cmdnotavail, extcmd->ef_txt); |
no test coverage detected