Add wget command to NSH

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1657 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2009-03-28 19:49:28 +00:00
parent 6484f2c6bb
commit 540b7c48c9
13 changed files with 265 additions and 30 deletions
+15 -4
View File
@@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+5
View File
@@ -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
+6
View File
@@ -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
+152 -2
View File
@@ -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;
}
}
@@ -378,6 +389,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 */
+3 -2
View File
@@ -56,7 +56,8 @@
* Name: callback
****************************************************************************/
static void callback(FAR char **buffer, int offset, int datend, FAR int *buflen)
static void callback(FAR char **buffer, int offset, int datend,
FAR int *buflen, FAR void *arg)
{
(void)write(1, &((*buffer)[offset]), datend - offset);
}
@@ -89,7 +90,7 @@ int main(int argc, char **argv, char **envp)
}
printf("WGET: Getting %s\n", argv[1]);
ret = wget(argv[1], buffer, 1024, callback);
ret = wget(argv[1], buffer, 1024, callback, NULL);
if (ret < 0)
{
fprintf(stderr, "WGET: wget failed: %s\n", strerror(errno));
+3 -2
View File
@@ -94,7 +94,8 @@ static char g_iobuffer[512];
* Name: callback
****************************************************************************/
static void callback(FAR char **buffer, int offset, int datend, FAR int *buflen)
static void callback(FAR char **buffer, int offset, int datend,
FAR int *buflen, FAR void *arg)
{
(void)write(1, &((*buffer)[offset]), datend - offset);
}
@@ -155,6 +156,6 @@ int user_start(int argc, char *argv[])
/* Then start the server */
wget(CONFIG_EXAMPLE_WGET_URL, g_iobuffer, 512, callback);
wget(CONFIG_EXAMPLE_WGET_URL, g_iobuffer, 512, callback, NULL);
return 0;
}