msh_cmd.c 7.0 KB
Newer Older
B
Bernard Xiong 已提交
1 2 3
/*
 *  internal commands for RT-Thread module shell
 *
B
Bernard Xiong 已提交
4
 * COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
B
Bernard Xiong 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *  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
B
Bernard Xiong 已提交
28
 * 2015-08-28     Bernard      Add mkfs command.
B
Bernard Xiong 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 */

#include <rtthread.h>
#include <finsh.h>

#include "msh.h"

#ifdef FINSH_USING_MSH
#ifdef RT_USING_DFS
#include <dfs_posix.h>

#ifdef DFS_USING_WORKDIR
extern char working_directory[];
#endif

int cmd_ls(int argc, char** argv)
{
    extern void ls(const char *pathname);

    if (argc == 1)
    {
#ifdef DFS_USING_WORKDIR
        ls(working_directory);
#else
        ls("/");
#endif
    }
    else 
    {
        ls(argv[1]);
    }

    return 0;
}
63
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.);
B
Bernard Xiong 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

int cmd_cp(int argc, char** argv)
{
    void copy(const char *src, const char *dst);

    if (argc != 3)
    {
        rt_kprintf("Usage: cp SOURCE DEST\n");
        rt_kprintf("Copy SOURCE to DEST.\n");
    }
    else
    {
        copy(argv[1], argv[2]);
    }

    return 0;
}
81
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cp, __cmd_cp, Copy SOURCE to DEST.);
B
Bernard Xiong 已提交
82 83 84 85 86 87 88 89 90 91

int cmd_mv(int argc, char** argv)
{
    if (argc != 3)
    {
        rt_kprintf("Usage: mv SOURCE DEST\n");
        rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
    }
    else
    {
B
bernard 已提交
92 93 94
		int fd;
		char *dest = RT_NULL;

B
Bernard Xiong 已提交
95
        rt_kprintf("%s => %s\n", argv[1], argv[2]);
B
bernard 已提交
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

		fd = open(argv[2], O_DIRECTORY, 0);
		if (fd >= 0)
		{
			char *src;
			
			close(fd);

			/* it's a directory */			
			dest = (char*)rt_malloc(DFS_PATH_MAX);
			if (dest == RT_NULL)
			{
				rt_kprintf("out of memory\n");
				return -RT_ENOMEM;
			}

			src = argv[1] + rt_strlen(argv[1]);
			while (src != argv[1]) 
			{
				if (*src == '/') break;
				src --;
			}

			rt_snprintf(dest, DFS_PATH_MAX - 1, "%s/%s", argv[2], src);
		}
		else
		{
			fd = open(argv[2], O_RDONLY, 0);
			if (fd >= 0)
			{
				close(fd);
				
				unlink(argv[2]);
			}

			dest = argv[2];
		}

		rename(argv[1], dest);
		if (dest != RT_NULL && dest != argv[2]) rt_free(dest);
B
Bernard Xiong 已提交
136 137 138 139
    }

    return 0;
}
140
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.);
B
Bernard Xiong 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

int cmd_cat(int argc, char** argv)
{
    int index;
    extern void cat(const char* filename);

    if (argc == 1)
    {
        rt_kprintf("Usage: cat [FILE]...\n");
        rt_kprintf("Concatenate FILE(s)\n");
		return 0;
    }

    for (index = 1; index < argc; index ++)
    {
        cat(argv[index]);
    }

    return 0;
}
161
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
B
Bernard Xiong 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

int cmd_rm(int argc, char** argv)
{
    int index;

    if (argc == 1)
    {
        rt_kprintf("Usage: rm FILE...\n");
        rt_kprintf("Remove (unlink) the FILE(s).\n");
		return 0;
    }

    for (index = 1; index < argc; index ++)
    {
        unlink(argv[index]);
    }

    return 0;
}
181
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove (unlink) the FILE(s).);
B
Bernard Xiong 已提交
182

183
#ifdef DFS_USING_WORKDIR
B
Bernard Xiong 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
int cmd_cd(int argc, char** argv)
{
    if (argc == 1)
    {
        rt_kprintf("%s\n", working_directory);
    }
    else if (argc == 2)
    {
        chdir(argv[1]);
    }

    return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.);

int cmd_pwd(int argc, char** argv)
{
	rt_kprintf("%s\n", working_directory);
	return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.);
205
#endif
B
Bernard Xiong 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

int cmd_mkdir(int argc, char** argv)
{
	if (argc == 1)
	{
		rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
		rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
	}
	else
	{
		mkdir(argv[1], 0);
	}

	return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.);

B
Bernard Xiong 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
int cmd_mkfs(int argc, char** argv)
{
    int result = 0;
    char* type="elm"; /* use the default file system type as 'fatfs' */

    if (argc == 2)
    {
        result = dfs_mkfs(type, argv[1]);
    }
    else if (argc == 4)
    {
        if (strcmp(argv[1], "-t") == 0)
        {
            type = argv[2];
            result = dfs_mkfs(type, argv[1]);
        }
    }
    else
    {
        rt_kprintf("Usage: mkfs [-t type] device\n");
        return 0;
    }
B
Bernard Xiong 已提交
245 246 247 248 249
    
    if (result != RT_EOK)
    {
        rt_kprintf("mkfs failed, result=%d\n", result);
    }
B
Bernard Xiong 已提交
250 251 252 253 254

    return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkfs, __cmd_mkfs, format disk with file system);

B
Bernard Xiong 已提交
255
#endif
B
Bernard Xiong 已提交
256

257 258 259
#ifdef RT_USING_LWIP
int cmd_ifconfig(int argc, char** argv)
{
wuyangyong's avatar
wuyangyong 已提交
260 261
    extern void list_if(void);
    extern void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr);
262

wuyangyong's avatar
wuyangyong 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    if(argc == 1)
    {
        list_if();
    }
    else if(argc == 5)
    {
        rt_kprintf("config : %s\n", argv[1]);
        rt_kprintf("IP addr: %s\n", argv[2]);
        rt_kprintf("Gateway: %s\n", argv[3]);
        rt_kprintf("netmask: %s\n", argv[4]);
        set_if(argv[1], argv[2], argv[3], argv[4]);
    }
    else
    {
        rt_kprintf("bad parameter! e.g: ifconfig e0 192.168.1.30 192.168.1.1 255.255.255.0\n");
    }

    return 0;
282 283 284
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ifconfig, __cmd_ifconfig, list the information of network interfaces);

285
#ifdef RT_LWIP_TCP
wuyangyong's avatar
wuyangyong 已提交
286 287 288 289 290 291 292 293
int cmd_netstat(int argc, char** argv)
{
 	extern void list_tcps(void);

	list_tcps();
	return 0;
}
FINSH_FUNCTION_EXPORT_ALIAS(cmd_netstat, __cmd_netstat, list the information of TCP/IP);
294
#endif
wuyangyong's avatar
wuyangyong 已提交
295
#endif /* RT_USING_LWIP */
296

B
Bernard Xiong 已提交
297 298 299
int cmd_ps(int argc, char** argv)
{
    extern long list_thread(void);
B
Bernard Xiong 已提交
300 301 302 303 304 305 306 307
    extern int list_module(void);
    
#ifdef RT_USING_MODULE
    if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
        list_module();
    else
#endif
        list_thread();
B
Bernard Xiong 已提交
308 309
    return 0;
}
310
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system.);
B
Bernard Xiong 已提交
311 312 313 314 315

int cmd_time(int argc, char** argv)
{
    return 0;
}
316
FINSH_FUNCTION_EXPORT_ALIAS(cmd_time, __cmd_time, Execute command with time.);
B
Bernard Xiong 已提交
317

318
#ifdef RT_USING_HEAP
B
Bernard Xiong 已提交
319 320 321
int cmd_free(int argc, char** argv)
{
    extern void list_mem(void);
B
Bernard Xiong 已提交
322
    extern void list_memheap(void);
B
Bernard Xiong 已提交
323

324 325 326
#ifdef RT_USING_MEMHEAP_AS_HEAP
    list_memheap();
#else
B
Bernard Xiong 已提交
327
    list_mem();
328
#endif
B
Bernard Xiong 已提交
329 330
    return 0;
}
331
FINSH_FUNCTION_EXPORT_ALIAS(cmd_free, __cmd_free, Show the memory usage in the system.);
332
#endif
B
Bernard Xiong 已提交
333

B
Bernard Xiong 已提交
334
#endif
335