From nobody Mon Mar 28 07:21:41 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id B08CF1A43B97; Mon, 28 Mar 2022 07:21:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KRkdx4THdz4WNn; Mon, 28 Mar 2022 07:21:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648452101; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BGA770f6BFQR/wigUlIFZKpxeZcGWMoFW+JyVaytapg=; b=oD0REGHsGwgyLkqj37QtSPY7JCg8NS7o9iZtHJYokWHbWj3zMT24fJfZNSMiZoY4jxTNFG nyeR5sfomjcuOUIfIKodFNyv/JzZRGcf9sp0McdpZvCZDGMDxl2c0m7zpJEF7gxJlBW8BQ nWLCziXYdAouuLk1sFytSmdtPZMpcbXoDz5Wo+9QPkiz189zp5NEfsIj1z6o/6v/pDip7T gZY3RdDltttN285jTepv6R/OsX+OvOYf+wjE1jCgE9mh6XTO0OLJ+ajU4UsFc9UDu8NDXt GYJ/U4q6LJRp2oJXjiJrSBMZzIANvIEVyiUwQVV/mKLOnWvw18kJgHJDE/9TeQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7A1812534D; Mon, 28 Mar 2022 07:21:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22S7LfYg035842; Mon, 28 Mar 2022 07:21:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22S7Lfik035841; Mon, 28 Mar 2022 07:21:41 GMT (envelope-from git) Date: Mon, 28 Mar 2022 07:21:41 GMT Message-Id: <202203280721.22S7Lfik035841@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Piotr Pawel Stefaniak Subject: git: 68700941c7ad - main - sh: fix autocompletion for commands that share name with a directory List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pstef X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 68700941c7ad58d6fa8eda82f3f370d87670fa6a Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648452101; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BGA770f6BFQR/wigUlIFZKpxeZcGWMoFW+JyVaytapg=; b=CAswMKCeOzlG/zxwqrcUSxkb7PRuK44ZJz/Jb+1rypxc037dj+T3d4C/jk5cDwf8wz9DD1 Qwi4n8tQoceKA3kudnpX4eIZPEa+whLazg641T9W5/fGLzY0rUMO9a3shaBIcTEPPlVJnq OUovaBUcv9c+9PF82ONoIyfjBRgF5IktXCEOAdoqVxd1okersOvVFJSB9cwrxyD4DoGKq0 vsR9w6tWDCjaoBWxaWl37+EwwIGYvBJNnXXQ4nJr/INWLGkL4hvzRWoaVGm70htb2nYFrS 5+AQILTVdTHrTfHHlxaTH2GVAnPs4ijXjvLWll04CU/+cdLG6Tn3r7+KWXkXUg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1648452101; a=rsa-sha256; cv=none; b=n2wspikSG0ArKktn9A2RKnmoPeyNgC/qv66k+846MKwjbyGEpVt7bwiscn6xv0+Ws0/O4u Y9xZLS2TAZc3wsG3R4kr9u+zL1FLAM/5jgrK7jC8neya8U49R3K64MjkPtz8v4XEiJQBrW Yyg0ANKi7LRAqmcfoRlyaHvlEurGXhqQcMkP6BtTdabxypErj6amYP5AzNUr4JpUAgpCgw zqirMLicAJshLGemRpbddSQbADXfxymBVEU5dfgxrTqrUbc7EeusAO9XSL5cdyfjdhWnKu WOBKKvk5GvCHLwXsZ0vZHYQ4CwYsUZv3dNDqSCGoJ0Ai0L2GD+kwrpSZ0Gmu0A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by pstef: URL: https://cgit.FreeBSD.org/src/commit/?id=68700941c7ad58d6fa8eda82f3f370d87670fa6a commit 68700941c7ad58d6fa8eda82f3f370d87670fa6a Author: Piotr Pawel Stefaniak AuthorDate: 2022-03-12 11:08:05 +0000 Commit: Piotr Pawel Stefaniak CommitDate: 2022-03-28 06:45:03 +0000 sh: fix autocompletion for commands that share name with a directory Provide libedit a special function making it always add a space after the autocompleted command. The default one adds a slash if the word is also a name of a directory in the current working directory, but this is wrong for commands. Reviewed by: bapt, jilles Differential Revision: https://reviews.freebsd.org/D34544 --- bin/sh/histedit.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/bin/sh/histedit.c b/bin/sh/histedit.c index 3c113333f4c8..453260a27e54 100644 --- a/bin/sh/histedit.c +++ b/bin/sh/histedit.c @@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -73,12 +74,14 @@ EditLine *el; /* editline cookie */ int displayhist; static int savehist; static FILE *el_in, *el_out; +static bool in_command_completion; static char *fc_replace(const char *, char *, char *); static int not_fcnumber(const char *); static int str_to_event(const char *, int); static int comparator(const void *, const void *, void *); static char **sh_matches(const char *, int, int); +static const char *append_char_function(const char *); static unsigned char sh_complete(EditLine *, int); static const char * @@ -602,8 +605,10 @@ static char size_t i = 0, size = 16, uniq; size_t curpos = end - start, lcstring = -1; + in_command_completion = false; if (start > 0 || memchr("/.~", text[0], 3) != NULL) return (NULL); + in_command_completion = true; if ((free_path = path = strdup(pathval())) == NULL) goto out; if ((matches = malloc(size * sizeof(matches[0]))) == NULL) @@ -696,6 +701,32 @@ out: return (matches); } +/* + * If we don't specify this function as app_func in the call to fn_complete2, + * libedit will use the default one, which adds a " " to plain files and + * a "/" to directories regardless of whether it's a command name or a plain + * path (relative or absolute). We never want to add "/" to commands. + * + * For example, after I did "mkdir rmdir", "rmdi" would be autocompleted to + * "rmdir/" instead of "rmdir ". + */ +static const char * +append_char_function(const char *name) +{ + struct stat stbuf; + char *expname = name[0] == '~' ? fn_tilde_expand(name) : NULL; + const char *rs; + + if (!in_command_completion && + stat(expname ? expname : name, &stbuf) == 0 && + S_ISDIR(stbuf.st_mode)) + rs = "/"; + else + rs = " "; + free(expname); + return (rs); +} + /* * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to * bind a key (tab by default) to execute the function. @@ -704,8 +735,8 @@ unsigned char sh_complete(EditLine *sel, int ch __unused) { return (unsigned char)fn_complete2(sel, NULL, sh_matches, - L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100, - NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH); + L" \t\n\"\\'`@$><=;|&{(", NULL, append_char_function, + (size_t)100, NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH); } #else