msh.c 7.7 KB
Newer Older
B
Bernard Xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 *  RT-Thread module shell implementation.
 *
 * COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
 *
 *  This file is part of RT-Thread (http://www.rt-thread.org)
 *  Maintainer: bernard.xiong <bernard.xiong at gmail.com>
 *
 *  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2013-03-30     Bernard      the first verion for FinSH
 */

#include "msh.h"
#include <finsh.h>
#include <shell.h>

#define RT_FINSH_ARG_MAX	10
typedef int (*cmd_function_t)(int argc, char** argv);

#ifdef FINSH_USING_MSH
B
bernard 已提交
38 39 40 41 42 43
#ifdef FINSH_USING_MSH_ONLY
rt_bool_t msh_is_used(void)
{
	return RT_TRUE;
}
#else
B
Bernard Xiong 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#ifdef FINSH_USING_MSH_DEFAULT
static rt_bool_t __msh_state = RT_TRUE;
#else
static rt_bool_t __msh_state = RT_FALSE;
#endif
rt_bool_t msh_is_used(void)
{
	return __msh_state;
}

static int msh_exit(int argc, char** argv)
{
	/* return to finsh shell mode */
	__msh_state = RT_FALSE;

	return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(msh_exit, __cmd_exit, return to RT-Thread shell mode.);

static int msh_enter(void)
{
	/* enter module shell mode */
	__msh_state = RT_TRUE;
	return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(msh_enter, msh, use module shell);
B
bernard 已提交
70
#endif
B
Bernard Xiong 已提交
71 72 73 74 75 76 77 78 79 80 81 82

int msh_help(int argc, char** argv)
{
	rt_kprintf("RT-Thread shell commands:\n");
	{
		struct finsh_syscall *index;

		for (index = _syscall_table_begin;
			index < _syscall_table_end;
			FINSH_NEXT_SYSCALL(index))
		{
			if (strncmp(index->name, "__cmd_", 6) != 0) continue;
83 84 85
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
			rt_kprintf("%-16s - %s\n", &index->name[6], index->desc);
#else
B
Bernard Xiong 已提交
86
			rt_kprintf("%s ", &index->name[6]);
87
#endif
B
Bernard Xiong 已提交
88 89 90 91 92 93
		}
	}
	rt_kprintf("\n");

	return 0;
}
94
FINSH_FUNCTION_EXPORT_ALIAS(msh_help, __cmd_help, RT-Thread shell help.);
B
Bernard Xiong 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

static int msh_split(char* cmd, rt_size_t length, char* argv[RT_FINSH_ARG_MAX])
{
	char *ptr;
	rt_size_t position;
	rt_size_t argc;

	ptr = cmd;
	position = 0; argc = 0;

	while (position < length)
	{
		/* strip bank and tab */
		while ((*ptr == ' ' || *ptr == '\t') && position < length)
		{
			*ptr = '\0';
			ptr ++; position ++;
		}
		if (position >= length) break;

		/* handle string */
		if (*ptr == '"')
		{
			ptr ++; position ++;
			argv[argc] = ptr; argc ++;

			/* skip this string */
			while (*ptr != '"' && position < length)
			{
				if (*ptr == '\\')
				{
					if (*(ptr + 1) == '"')
					{
						ptr ++; position ++;
					}
				}
				ptr ++; position ++;
			}
			if (position >= length) break;

			/* skip '"' */
			*ptr = '\0'; ptr ++; position ++;
		}
		else
		{
			argv[argc] = ptr;
			argc ++;
			while ((*ptr != ' ' && *ptr != '\t') && position < length)
			{
				ptr ++; position ++;
			}
			if (position >= length) break;
		}
	}

	return argc;
}

static cmd_function_t msh_get_cmd(char *cmd)
{
	struct finsh_syscall *index;
	cmd_function_t cmd_func = RT_NULL;

	for (index = _syscall_table_begin;
		index < _syscall_table_end;
		FINSH_NEXT_SYSCALL(index))
	{
		if (strncmp(index->name, "__cmd_", 6) != 0) continue;
		
		if (strcmp(&index->name[6], cmd) == 0)
		{
			cmd_func = (cmd_function_t)index->func;
			break;
		}
	}

	return cmd_func;
}

int msh_exec(char* cmd, rt_size_t length)
{
	int argc;
	char *argv[RT_FINSH_ARG_MAX];

	cmd_function_t cmd_func;

	memset(argv, 0x00, sizeof(argv));
	argc = msh_split(cmd, length, argv);
	if (argc == 0) return -1;

	/* get command in internal commands */
	cmd_func = msh_get_cmd(argv[0]);
	if (cmd_func == RT_NULL) 
	{
		rt_kprintf("%s: command not found\n", argv[0]);
		return -1;
	}

	/* exec this command */
	return cmd_func(argc, argv);
}

static int str_common(const char *str1, const char *str2)
{
	const char *str = str1;

	while ((*str != 0) && (*str2 != 0) && (*str == *str2))
	{
		str ++;
		str2 ++;
	}

	return (str - str1);
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
#ifdef RT_USING_DFS
#include <dfs_posix.h>
void msh_auto_complete_path(char *path)
{
	DIR* dir;
	struct dirent *dirent;
	char *full_path, *ptr, *index;

	full_path = (char*)rt_malloc(256);
	if (full_path == RT_NULL) return; /* out of memory */

	ptr = full_path;
	if (*path != '/') 
	{
		getcwd(full_path, 256);
		if (full_path[rt_strlen(full_path) - 1]  != '/')
			strcat(full_path, "/");
	}
	else *full_path = '\0';

	index = RT_NULL; ptr = path;
	for (;;)
	{
		if (*ptr == '/') index = ptr + 1; if (!*ptr) break; ptr ++;
	}
	if (index == RT_NULL) index = path;

	if (index != RT_NULL)
	{
		char *dest = index;

		/* fill the parent path */
		ptr = full_path; 
		while (*ptr) ptr ++;

		for (index = path; index != dest;)
			*ptr++ = *index++;
		*ptr = '\0';

		dir = opendir(full_path);
		if (dir == RT_NULL) /* open directory failed! */
		{
			rt_free(full_path);
			return;
		}

		/* restore the index position */
		index = dest;
	}

	/* auto complete the file or directory name */
	if (*index == '\0') /* display all of files and directories */
	{
		for (;;)
		{
			dirent = readdir(dir);
			if (dirent == RT_NULL) break;
			
			rt_kprintf("%s\n", dirent->d_name);
		}
	}
	else
	{
		int length, min_length;

		min_length = 0;
		for (;;)
		{
			dirent = readdir(dir);
			if (dirent == RT_NULL) break;

			/* matched the prefix string */
			if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
			{
				if (min_length == 0)
				{
					min_length = rt_strlen(dirent->d_name);
					/* save dirent name */
					strcpy(full_path, dirent->d_name);
				}
				
				length = str_common(dirent->d_name, full_path);
				
				if (length < min_length)
				{
					min_length = length;
				}
			}
		}

		if (min_length)
		{
			if (min_length < rt_strlen(full_path))
			{
				/* list the candidate */
				rewinddir(dir);

				for (;;)
				{
					dirent = readdir(dir);
					if (dirent == RT_NULL) break;

					if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
						rt_kprintf("%s\n", dirent->d_name);
				}
			}
			
			length = index - path;
			memcpy(index, full_path, min_length);
			path[length + min_length] = '\0';
		}
	}

	closedir(dir);
	rt_free(full_path);
}
#endif

B
Bernard Xiong 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
void msh_auto_complete(char *prefix)
{
	int length, min_length;
	const char *name_ptr, *cmd_name;
	struct finsh_syscall *index;

	min_length = 0;
	name_ptr = RT_NULL;

	if (*prefix == '\0') 
	{
		msh_help(0, RT_NULL);
		return;
	}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
#ifdef RT_USING_DFS
	/* check whether a spare in the command */
	{
		char *ptr;

		ptr = prefix + rt_strlen(prefix);
		while (ptr != prefix)
		{
			if (*ptr == ' ')
			{
				msh_auto_complete_path(ptr + 1);
				break;
			}
			
			ptr --;
		}
	}
#endif
	
B
Bernard Xiong 已提交
362 363 364 365 366 367 368 369 370 371
	/* checks in internal command */
	{
		for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
		{
			/* skip finsh shell function */
			if (strncmp(index->name, "__cmd_", 6) != 0) continue;

			cmd_name = (const char*) &index->name[6];
			if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
			{
372
				if (min_length == 0)
B
Bernard Xiong 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
				{
					/* set name_ptr */
					name_ptr = cmd_name;
					/* set initial length */
					min_length = strlen(name_ptr);
				}

				length = str_common(name_ptr, cmd_name);
				if (length < min_length)
					min_length = length;

				rt_kprintf("%s\n", cmd_name);
			}
		}
	}

	/* auto complete string */
	if (name_ptr != NULL)
	{
		rt_strncpy(prefix, name_ptr, min_length);
	}

	return ;
}
#endif
398