diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-03-28 19:49:28 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-03-28 19:49:28 +0000 |
commit | 4d94719cf982033183a5d641ef741f5e229d212d (patch) | |
tree | 6e4e30215926ce1f068e9e7738a470fc7dea18a1 /nuttx/examples/nsh | |
parent | 305d77e4688822294cd49112b4d8d73c48619979 (diff) | |
download | px4-nuttx-4d94719cf982033183a5d641ef741f5e229d212d.tar.gz px4-nuttx-4d94719cf982033183a5d641ef741f5e229d212d.tar.bz2 px4-nuttx-4d94719cf982033183a5d641ef741f5e229d212d.zip |
Add wget command to NSH
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1657 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples/nsh')
-rw-r--r-- | nuttx/examples/nsh/README.txt | 19 | ||||
-rw-r--r-- | nuttx/examples/nsh/nsh.h | 5 | ||||
-rw-r--r-- | nuttx/examples/nsh/nsh_main.c | 6 | ||||
-rw-r--r-- | nuttx/examples/nsh/nsh_netcmds.c | 154 |
4 files changed, 178 insertions, 6 deletions
diff --git a/nuttx/examples/nsh/README.txt b/nuttx/examples/nsh/README.txt index 8bdd14eb5..2a8d43f48 100644 --- a/nuttx/examples/nsh/README.txt +++ b/nuttx/examples/nsh/README.txt @@ -303,8 +303,8 @@ o exit o get [-b|-n] [-f <local-path>] -h <ip-address> <remote-path> - Copy the file at <remote-address> from the host whose IP address is - identified by <ip-address>. Other options: + Use TFTP to copy the file at <remote-address> from the host whose IP + address is identified by <ip-address>. Other options: -f <local-path> The file will be saved relative to the current working directory @@ -717,6 +717,16 @@ o usleep <usec> Pause execution (sleep) of <usec> microseconds. +o wget [-o <local-path>] <url> + + Use HTTP to copy the file at <url> to the current directory. + Options: + + -o <local-path> + The file will be saved relative to the current working directory + and with the same name as on the HTTP server unless <local-path> + is provided. + o xd <hex-address> <byte-count> Dump <byte-count> bytes of data from address <hex-address> @@ -780,10 +790,11 @@ Command Dependencies on Configuration Settings umount !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_READABLE unset !CONFIG_DISABLE_ENVIRON usleep !CONFIG_DISABLE_SIGNALS + get CONFIG_NET && CONFIG_NET_TCP && CONFIG_NFILE_DESCRIPTORS > 0 xd --- * NOTES: - 1. Because of hardware padding, the actual required for put and get + 1. Because of hardware padding, the actual buffersize required for put and get operations size may be larger. 2. Special TFTP server start-up optionss will probably be required to permit creation of file for the correct operation of the put command. @@ -808,7 +819,7 @@ also allow it to squeeze into very small memory footprints. CONFIG_EXAMPLES_NSH_DISABLE_PWD, CONFIG_EXAMPLES_NSH_DISABLE_RM, CONFIG_EXAMPLES_NSH_DISABLE_RMDIR, CONFIG_EXAMPLES_NSH_DISABLE_SET, CONFIG_EXAMPLES_NSH_DISABLE_SH, CONFIG_EXAMPLES_NSH_DISABLE_SLEEP, CONFIG_EXAMPLES_NSH_DISABLE_TEST, CONFIG_EXAMPLES_NSH_DISABLE_UMOUNT, CONFIG_EXAMPLES_NSH_DISABLE_UNSET, - CONFIG_EXAMPLES_NSH_DISABLE_USLEEP, CONFIG_EXAMPLES_NSH_DISABLE_XD + CONFIG_EXAMPLES_NSH_DISABLE_USLEEP, CONFIG_EXAMPLES_NSH_DISABLE_WGET, CONFIG_EXAMPLES_NSH_DISABLE_XD NSH-Specific Configuration Settings ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/nuttx/examples/nsh/nsh.h b/nuttx/examples/nsh/nsh.h index 0ef0baaec..4a986a996 100644 --- a/nuttx/examples/nsh/nsh.h +++ b/nuttx/examples/nsh/nsh.h @@ -434,6 +434,11 @@ extern int cmd_lbracket(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); extern int cmd_put(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #endif +#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0 +# ifndef CONFIG_EXAMPLES_NSH_DISABLE_WGET + extern int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); +# endif +#endif #if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) # ifndef CONFIG_EXAMPLES_NSH_DISABLE_PING diff --git a/nuttx/examples/nsh/nsh_main.c b/nuttx/examples/nsh/nsh_main.c index db116b5c0..edcdf131d 100644 --- a/nuttx/examples/nsh/nsh_main.c +++ b/nuttx/examples/nsh/nsh_main.c @@ -317,6 +317,12 @@ static const struct cmdmap_s g_cmdmap[] = # endif #endif +#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0 +# ifndef CONFIG_EXAMPLES_NSH_DISABLE_GET + { "wget", cmd_wget, 2, 3, "[-o <local-path>] <url>" }, +# endif +#endif + #ifndef CONFIG_EXAMPLES_NSH_DISABLE_XD { "xd", cmd_xd, 3, 3, "<hex-address> <byte-count>" }, #endif diff --git a/nuttx/examples/nsh/nsh_netcmds.c b/nuttx/examples/nsh/nsh_netcmds.c index f95e45017..f9e0acc6c 100644 --- a/nuttx/examples/nsh/nsh_netcmds.c +++ b/nuttx/examples/nsh/nsh_netcmds.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/nsh/nsh_netcmds.c * - * Copyright (C) 2007, 2008, 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -41,12 +41,14 @@ #ifdef CONFIG_NET #include <sys/types.h> +#include <sys/stat.h> /* Needed for open */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sched.h> -#include <libgen.h> +#include <fcntl.h> /* Needed for open */ +#include <libgen.h> /* Needed for basename */ #include <errno.h> #include <nuttx/net.h> @@ -70,6 +72,13 @@ # include <net/uip/tftp.h> #endif +#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0 +# ifndef CONFIG_EXAMPLES_NSH_DISABLE_WGET +# include <net/uip/uip-lib.h> +# include <net/uip/webclient.h> +# endif +#endif + #include "nsh.h" /**************************************************************************** @@ -302,9 +311,11 @@ int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv, case ':': fmt = g_fmtargrequired; + goto errout; case '?': default: + fmt = g_fmtarginvalid; goto errout; } } @@ -379,6 +390,20 @@ errout: #endif /**************************************************************************** + * Name: wget_callback + ****************************************************************************/ + +#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0 +#ifndef CONFIG_EXAMPLES_NSH_DISABLE_WGET +static void wget_callback(FAR char **buffer, int offset, int datend, + FAR int *buflen, FAR void *arg) +{ + (void)write((int)arg, &((*buffer)[offset]), datend - offset); +} +#endif +#endif + +/**************************************************************************** * Public Functions ****************************************************************************/ @@ -626,4 +651,129 @@ int cmd_put(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) #endif #endif +/**************************************************************************** + * Name: cmd_wget + ****************************************************************************/ + +#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0 +#ifndef CONFIG_EXAMPLES_NSH_DISABLE_WGET +int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) +{ + char *localfile = NULL; + char *allocfile = NULL; + char *buffer = NULL; + char *fullpath = NULL; + char *url; + char *fmt; + int option; + int fd = -1; + int ret; + + /* Get the wget options */ + + while ((option = getopt(argc, argv, ":o:")) != ERROR) + { + switch (option) + { + case 'o': + localfile = optarg; + break; + + case ':': + fmt = g_fmtargrequired; + goto errout; + + case '?': + default: + fmt = g_fmtarginvalid; + goto errout; + } + } + + /* There should be exactly on parameter left on the command-line */ + + if (optind == argc-1) + { + url = argv[optind]; + } + else if (optind >= argc) + { + fmt = g_fmttoomanyargs; + goto errout; + } + else + { + fmt = g_fmtargrequired; + goto errout; + } + + /* Get the local file name */ + + if (!localfile) + { + allocfile = strdup(url); + localfile = basename(allocfile); + } + + /* Get the full path to the local file */ + + fullpath = nsh_getfullpath(vtbl, localfile); + + /* Open the the local file for writing */ + + fd = open(fullpath, O_WRONLY|O_CREAT|O_TRUNC, 0644); + if (fd < 0) + { + nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO); + ret = ERROR; + goto exit; + } + + /* Allocate an I/O buffer */ + + buffer = malloc(512); + if (!buffer) + { + fmt = g_fmtcmdoutofmemory; + goto errout; + } + + /* And perform the wget */ + + ret = wget(argv[1], buffer, 512, wget_callback, (FAR void *)fd); + if (ret < 0) + { + nsh_output(vtbl, g_fmtcmdfailed, argv[0], "wget", NSH_ERRNO); + goto exit; + } + + /* Free allocated resources */ + +exit: + if (fd >= 0) + { + close(fd); + } + if (allocfile) + { + free(allocfile); + } + if (fullpath) + { + free(fullpath); + } + if (buffer) + { + free(buffer); + } + return ret; + +errout: + nsh_output(vtbl, fmt, argv[0]); + ret = ERROR; + goto exit; +} +#endif +#endif + #endif /* CONFIG_NET */ |