2008-03-01 20:08:14 +00:00
|
|
|
/*
|
|
|
|
* complete.c - functions for TAB completion
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <complete.h>
|
|
|
|
#include <xfuncs.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <command.h>
|
2011-12-21 08:07:57 +00:00
|
|
|
#include <environment.h>
|
2008-03-01 20:08:14 +00:00
|
|
|
|
2012-04-19 05:02:20 +00:00
|
|
|
static int file_complete(struct string_list *sl, char *instr, int exec)
|
2008-03-01 20:08:14 +00:00
|
|
|
{
|
|
|
|
char *path = strdup(instr);
|
|
|
|
struct stat s;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *d;
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
char *base, *dirn;
|
|
|
|
|
|
|
|
base = basename(instr);
|
|
|
|
dirn = dirname(path);
|
|
|
|
|
|
|
|
dir = opendir(dirn);
|
|
|
|
if (!dir)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
while ((d = readdir(dir))) {
|
|
|
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
2012-04-19 05:02:20 +00:00
|
|
|
if (strncmp(base, d->d_name, strlen(base)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strcpy(tmp, instr);
|
|
|
|
strcat(tmp, d->d_name + strlen(base));
|
|
|
|
if (!stat(tmp, &s) && S_ISDIR(s.st_mode)) {
|
|
|
|
strcat(tmp, "/");
|
|
|
|
} else {
|
|
|
|
if (exec && !S_ISREG(s.st_mode))
|
|
|
|
continue;
|
|
|
|
strcat(tmp, " ");
|
2008-03-01 20:08:14 +00:00
|
|
|
}
|
2012-04-19 05:02:20 +00:00
|
|
|
|
|
|
|
string_list_add_sorted(sl, tmp);
|
2008-03-01 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-21 08:07:57 +00:00
|
|
|
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') {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-06 15:14:38 +00:00
|
|
|
int command_complete(struct string_list *sl, char *instr)
|
2008-03-01 20:08:14 +00:00
|
|
|
{
|
2010-01-04 09:21:11 +00:00
|
|
|
struct command *cmdtp;
|
2011-06-06 09:04:59 +00:00
|
|
|
|
|
|
|
if (!instr)
|
|
|
|
instr = "";
|
2008-03-01 20:08:14 +00:00
|
|
|
|
|
|
|
for_each_command(cmdtp) {
|
2011-06-06 09:04:59 +00:00
|
|
|
if (strncmp(instr, cmdtp->name, strlen(instr)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string_list_add_asprintf(sl, "%s ", cmdtp->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-25 07:47:31 +00:00
|
|
|
EXPORT_SYMBOL(command_complete);
|
2011-06-06 09:04:59 +00:00
|
|
|
|
2011-06-05 12:58:08 +00:00
|
|
|
int device_complete(struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!instr)
|
|
|
|
instr = "";
|
|
|
|
|
|
|
|
len = strlen(instr);
|
|
|
|
|
|
|
|
for_each_device(dev) {
|
|
|
|
if (strncmp(instr, dev_name(dev), len))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string_list_add_asprintf(sl, "%s ", dev_name(dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
return COMPLETE_CONTINUE;
|
|
|
|
}
|
2012-07-25 07:47:31 +00:00
|
|
|
EXPORT_SYMBOL(device_complete);
|
2011-06-05 12:58:08 +00:00
|
|
|
|
2011-06-06 09:04:59 +00:00
|
|
|
static int device_param_complete(char *begin, struct device_d *dev,
|
|
|
|
struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
struct param_d *param;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!instr)
|
|
|
|
instr = "";
|
|
|
|
|
|
|
|
len = strlen(instr);
|
|
|
|
|
|
|
|
list_for_each_entry(param, &dev->parameters, list) {
|
|
|
|
if (strncmp(instr, param->name, len))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string_list_add_asprintf(sl, "%s%s.%s%c",
|
|
|
|
begin ? begin : "", dev_name(dev), param->name,
|
|
|
|
begin ? ' ' : '=');
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
complete: add empty complete support
for cpuinfo, clear, dhcp, false, login, lsmod, meminfo, passwd, pwd, reginfo,
reset, true, usb, version
for mach-imx and mach-mxs: dump_clocks
for u_serial: mycdev
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
2011-06-07 09:32:02 +00:00
|
|
|
int empty_complete(struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
return COMPLETE_END;
|
|
|
|
}
|
2012-07-25 07:47:31 +00:00
|
|
|
EXPORT_SYMBOL(empty_complete);
|
complete: add empty complete support
for cpuinfo, clear, dhcp, false, login, lsmod, meminfo, passwd, pwd, reginfo,
reset, true, usb, version
for mach-imx and mach-mxs: dump_clocks
for u_serial: mycdev
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
2011-06-07 09:32:02 +00:00
|
|
|
|
2012-06-13 17:27:46 +00:00
|
|
|
int command_var_complete(struct string_list *sl, char *instr)
|
2011-06-10 02:36:01 +00:00
|
|
|
{
|
|
|
|
return COMPLETE_CONTINUE;
|
|
|
|
}
|
2012-07-25 07:47:31 +00:00
|
|
|
EXPORT_SYMBOL(command_var_complete);
|
2011-06-10 02:36:01 +00:00
|
|
|
|
2014-05-19 11:55:48 +00:00
|
|
|
int devicetree_alias_complete(struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
struct device_node *aliases;
|
|
|
|
struct property *p;
|
|
|
|
|
|
|
|
aliases = of_find_node_by_path("/aliases");
|
|
|
|
if (!aliases)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
list_for_each_entry(p, &aliases->properties, list) {
|
|
|
|
if (strncmp(instr, p->name, strlen(instr)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string_list_add_asprintf(sl, "%s ", p->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(devicetree_alias_complete);
|
|
|
|
|
|
|
|
int devicetree_nodepath_complete(struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
struct device_node *node, *child;
|
|
|
|
char *dirn, *base;
|
|
|
|
char *path = strdup(instr);
|
|
|
|
|
|
|
|
if (*instr == '/') {
|
|
|
|
dirn = dirname(path);
|
|
|
|
base = basename(instr);
|
|
|
|
node = of_find_node_by_path(dirn);
|
|
|
|
if (!node)
|
|
|
|
goto out;
|
|
|
|
} else if (!*instr) {
|
|
|
|
node = of_get_root_node();
|
|
|
|
if (!node)
|
|
|
|
goto out;
|
|
|
|
base = "";
|
|
|
|
} else {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for_each_child_of_node(node, child) {
|
|
|
|
if (strncmp(base, child->name, strlen(base)))
|
|
|
|
continue;
|
|
|
|
string_list_add_asprintf(sl, "%s/", child->full_name);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(devicetree_nodepath_complete);
|
|
|
|
|
|
|
|
int devicetree_complete(struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
devicetree_nodepath_complete(sl, instr);
|
|
|
|
devicetree_alias_complete(sl, instr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(devicetree_complete);
|
|
|
|
|
|
|
|
int devicetree_file_complete(struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
devicetree_complete(sl, instr);
|
|
|
|
file_complete(sl, instr, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(devicetree_file_complete);
|
|
|
|
|
2011-06-06 09:04:59 +00:00
|
|
|
static int env_param_complete(struct string_list *sl, char *instr, int eval)
|
|
|
|
{
|
|
|
|
struct device_d *dev;
|
|
|
|
struct variable_d *var;
|
2012-10-14 11:39:10 +00:00
|
|
|
struct env_context *c;
|
2011-06-06 09:04:59 +00:00
|
|
|
char *instr_param;
|
|
|
|
int len;
|
|
|
|
char end = '=';
|
|
|
|
char *begin = "";
|
|
|
|
|
|
|
|
if (!instr)
|
|
|
|
instr = "";
|
|
|
|
|
|
|
|
if (eval) {
|
|
|
|
begin = "$";
|
|
|
|
end = ' ';
|
|
|
|
}
|
|
|
|
|
2012-06-24 12:25:18 +00:00
|
|
|
instr_param = strchr(instr, '.');
|
2011-06-06 09:04:59 +00:00
|
|
|
len = strlen(instr);
|
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
c = get_current_context();
|
|
|
|
list_for_each_entry(var, &c->local, list) {
|
2011-06-06 09:04:59 +00:00
|
|
|
if (strncmp(instr, var_name(var), len))
|
|
|
|
continue;
|
|
|
|
string_list_add_asprintf(sl, "%s%s%c",
|
|
|
|
begin, var_name(var), end);
|
|
|
|
}
|
|
|
|
|
2012-10-14 11:39:10 +00:00
|
|
|
c = get_current_context();
|
|
|
|
while (c) {
|
|
|
|
list_for_each_entry(var, &c->global, list) {
|
2011-06-06 09:04:59 +00:00
|
|
|
if (strncmp(instr, var_name(var), len))
|
|
|
|
continue;
|
|
|
|
string_list_add_asprintf(sl, "%s%s%c",
|
|
|
|
begin, var_name(var), end);
|
|
|
|
}
|
2012-10-14 11:39:10 +00:00
|
|
|
c = c->parent;
|
2011-06-06 09:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (instr_param) {
|
|
|
|
len = (instr_param - instr);
|
|
|
|
instr_param++;
|
|
|
|
} else {
|
|
|
|
len = strlen(instr);
|
|
|
|
instr_param = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
for_each_device(dev) {
|
|
|
|
if (!strncmp(instr, dev_name(dev), len)) {
|
|
|
|
if (eval)
|
|
|
|
device_param_complete("$", dev, sl, instr_param);
|
|
|
|
else
|
|
|
|
device_param_complete(NULL, dev, sl, instr_param);
|
2008-03-01 20:08:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tab_pressed = 0;
|
|
|
|
|
|
|
|
void complete_reset(void)
|
|
|
|
{
|
|
|
|
tab_pressed = 0;
|
|
|
|
}
|
|
|
|
|
2011-06-06 15:14:38 +00:00
|
|
|
static char* cmd_complete_lookup(struct string_list *sl, char *instr)
|
|
|
|
{
|
|
|
|
struct command *cmdtp;
|
|
|
|
int len;
|
2012-05-03 08:01:08 +00:00
|
|
|
int ret = COMPLETE_END;
|
2011-06-06 15:14:38 +00:00
|
|
|
char *res = NULL;
|
2014-05-19 11:43:19 +00:00
|
|
|
char *t;
|
2011-06-06 15:14:38 +00:00
|
|
|
|
|
|
|
for_each_command(cmdtp) {
|
|
|
|
len = strlen(cmdtp->name);
|
|
|
|
if (!strncmp(instr, cmdtp->name, len) && instr[len] == ' ') {
|
|
|
|
instr += len + 1;
|
2014-05-19 11:43:19 +00:00
|
|
|
t = strrchr(instr, ' ');
|
|
|
|
if (t)
|
|
|
|
instr = t + 1;
|
|
|
|
|
2011-06-06 15:14:38 +00:00
|
|
|
if (cmdtp->complete) {
|
|
|
|
ret = cmdtp->complete(sl, instr);
|
|
|
|
res = instr;
|
|
|
|
}
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (ret == COMPLETE_CONTINUE && *instr == '$')
|
|
|
|
env_param_complete(sl, instr + 1, 1);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-03-01 20:08:14 +00:00
|
|
|
int complete(char *instr, char **outstr)
|
|
|
|
{
|
2008-03-11 20:40:40 +00:00
|
|
|
struct string_list sl, *entry, *first_entry;
|
2008-03-01 20:08:14 +00:00
|
|
|
int pos;
|
|
|
|
char ch;
|
|
|
|
int changed;
|
|
|
|
static char out[256];
|
|
|
|
int outpos = 0;
|
|
|
|
int reprint = 0;
|
|
|
|
char *t;
|
|
|
|
|
2008-03-11 20:40:40 +00:00
|
|
|
string_list_init(&sl);
|
2008-03-01 20:08:14 +00:00
|
|
|
|
|
|
|
/* advance to the last command */
|
|
|
|
t = strrchr(instr, ';');
|
|
|
|
if (!t)
|
|
|
|
t = instr;
|
|
|
|
else
|
|
|
|
t++;
|
|
|
|
|
|
|
|
while (*t == ' ')
|
|
|
|
t++;
|
|
|
|
|
|
|
|
/* get the completion possibilities */
|
2011-06-06 15:14:38 +00:00
|
|
|
instr = cmd_complete_lookup(&sl, t);
|
|
|
|
if (!instr) {
|
2008-03-01 20:08:14 +00:00
|
|
|
instr = t;
|
2012-04-19 05:02:20 +00:00
|
|
|
if (t && (t[0] == '/' || !strncmp(t, "./", 2))) {
|
|
|
|
file_complete(&sl, t, 1);
|
|
|
|
instr = t;
|
|
|
|
} else if ((t = strrchr(t, ' '))) {
|
2011-06-06 15:14:38 +00:00
|
|
|
t++;
|
2012-04-19 05:02:20 +00:00
|
|
|
file_complete(&sl, t, 0);
|
2011-06-06 15:14:38 +00:00
|
|
|
instr = t;
|
|
|
|
} else {
|
|
|
|
command_complete(&sl, instr);
|
|
|
|
path_command_complete(&sl, instr);
|
|
|
|
env_param_complete(&sl, instr, 0);
|
|
|
|
}
|
|
|
|
if (*instr == '$')
|
|
|
|
env_param_complete(&sl, instr + 1, 1);
|
2011-12-21 08:07:57 +00:00
|
|
|
}
|
2008-03-01 20:08:14 +00:00
|
|
|
|
|
|
|
pos = strlen(instr);
|
|
|
|
|
|
|
|
*outstr = "";
|
2008-03-11 20:40:40 +00:00
|
|
|
if (list_empty(&sl.list))
|
2008-03-01 20:08:14 +00:00
|
|
|
return reprint;
|
|
|
|
|
|
|
|
out[0] = 0;
|
|
|
|
|
2008-03-11 20:40:40 +00:00
|
|
|
first_entry = list_first_entry(&sl.list, struct string_list, list);
|
2008-03-01 20:08:14 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
entry = first_entry;
|
|
|
|
ch = entry->str[pos];
|
|
|
|
if (!ch)
|
|
|
|
break;
|
|
|
|
|
|
|
|
changed = 0;
|
2008-03-11 20:40:40 +00:00
|
|
|
list_for_each_entry(entry, &sl.list, list) {
|
2008-03-01 20:08:14 +00:00
|
|
|
if (!entry->str[pos])
|
|
|
|
break;
|
|
|
|
if (ch != entry->str[pos]) {
|
|
|
|
changed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
break;
|
|
|
|
out[outpos++] = ch;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
2008-03-11 20:40:40 +00:00
|
|
|
if (!list_is_last(&first_entry->list, &sl.list) && !outpos && tab_pressed) {
|
2008-03-01 20:08:14 +00:00
|
|
|
printf("\n");
|
2008-03-11 20:40:40 +00:00
|
|
|
string_list_print_by_column(&sl);
|
2008-03-01 20:08:14 +00:00
|
|
|
reprint = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
out[outpos++] = 0;
|
|
|
|
*outstr = out;
|
|
|
|
|
|
|
|
if (*out == 0)
|
|
|
|
tab_pressed = 1;
|
|
|
|
else
|
|
|
|
tab_pressed = 0;
|
|
|
|
|
2008-03-11 20:40:40 +00:00
|
|
|
string_list_free(&sl);
|
2008-03-01 20:08:14 +00:00
|
|
|
|
|
|
|
return reprint;
|
|
|
|
}
|
|
|
|
|