From: Sascha Hauer <s.hauer@pengutronix.de>
To: Alexander Aring <a.aring@phytec.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 2/2] auto-completion: add auto-completion for path files
Date: Mon, 12 Dec 2011 10:08:14 +0100 [thread overview]
Message-ID: <20111212090814.GO27267@pengutronix.de> (raw)
In-Reply-To: <1323424372-8142-3-git-send-email-a.aring@phytec.de>
On Fri, Dec 09, 2011 at 10:52:48AM +0100, Alexander Aring wrote:
> Add auto-completion for path files.
>
> Signed-off-by: Alexander Aring <a.aring@phytec.de>
> ---
> commands/ls.c | 4 +-
> common/complete.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-
> 2 files changed, 68 insertions(+), 4 deletions(-)
>
> diff --git a/commands/ls.c b/commands/ls.c
> index 278a8bc..070aa90 100644
> --- a/commands/ls.c
> +++ b/commands/ls.c
> @@ -68,7 +68,7 @@ int ls(const char *path, ulong flags)
> if (stat(tmp, &s))
> goto out;
> if (flags & LS_COLUMN)
> - string_list_add(&sl, d->d_name);
> + string_list_add_sorted(&sl, d->d_name);
> else
> ls_one(d->d_name, &s);
> }
> @@ -156,7 +156,7 @@ static int do_ls(struct command *cmdtp, int argc, char *argv[])
>
> if (!(s.st_mode & S_IFDIR)) {
> if (flags & LS_COLUMN)
> - string_list_add(&sl, argv[o]);
> + string_list_add_sorted(&sl, argv[o]);
> else
> ls_one(argv[o], &s);
> }
Ok, but this seems unrelated to the subject of this patch. Please
separate.
> diff --git a/common/complete.c b/common/complete.c
> index 46ba871..0b79f92 100644
> --- a/common/complete.c
> +++ b/common/complete.c
> @@ -27,6 +27,7 @@
> #include <libgen.h>
> #include <command.h>
> #include <stringlist.h>
> +#include <environment.h>
>
> static int file_complete(struct string_list *sl, char *instr)
> {
> @@ -55,7 +56,7 @@ static int file_complete(struct string_list *sl, char *instr)
> strcat(tmp, "/");
> else
> strcat(tmp, " ");
> - string_list_add(sl, tmp);
> + string_list_add_sorted(sl, tmp);
> }
> }
>
> @@ -67,6 +68,67 @@ out:
> return 0;
> }
>
> +static int path_command_complete(struct string_list *sl, char *instr)
> +{
> + struct stat s;
> + DIR *dir;
> + struct dirent *d;
> + char tmp[PATH_MAX];
> + char *path, *p, *n;
> +
> + p = path = strdup(getenv("PATH"));
> +
> + if (!path)
> + return -1;
> +
> + while (p) {
> + n = strchr(p, ':');
> + if (n)
> + *n++ = '\0';
> + if (*p != '\0') {
You can save one indention level by doing
if (*p == '\0' ) {
p = n;
continue;
}
> + dir = opendir(p);
> +
> + /* We need to check all PATH dirs, so if one failed,
> + * try next */
> + if (!dir) {
> + p = n;
> + continue;
> + }
> +
> + while ((d = readdir(dir))) {
> + if (!strcmp(d->d_name, ".") ||
> + !strcmp(d->d_name, ".."))
> + continue;
> +
> + if (!strncmp(instr, d->d_name, strlen(instr))) {
> + strcpy(tmp, d->d_name);
> + if (!stat(tmp, &s) &&
> + S_ISDIR(s.st_mode))
> + continue;
> + else
> + strcat(tmp, " ");
> +
> + /* This function is called
> + * after command_complete,
> + * so we check if a double
> + * entry exist */
> + if (string_list_contains
> + (sl, tmp) == 0) {
> + string_list_add_sorted(sl, tmp);
> + }
> + }
> + }
> +
> + closedir(dir);
> + }
> + p = n;
> + }
> +
> + free(path);
> +
> + return 0;
> +}
> +
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2011-12-12 9:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1323424372-8142-1-git-send-email-a.aring@phytec.de>
[not found] ` <1323424372-8142-2-git-send-email-a.aring@phytec.de>
2011-12-12 8:48 ` [PATCH 1/2] stringlist-functions: add sorted insert Sascha Hauer
[not found] ` <1323424372-8142-3-git-send-email-a.aring@phytec.de>
2011-12-12 9:08 ` Sascha Hauer [this message]
[not found] ` <1323424372-8142-4-git-send-email-a.aring@phytec.de>
2011-12-12 9:38 ` [PATCH 2/5] twlcore: rename twl4030 to twlcore driver Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20111212090814.GO27267@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=a.aring@phytec.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox