From 67861ab4bc9411a36319f48f9aaad1a129fcde9b Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 1 Jun 2011 23:09:29 +0000 Subject: Add a simple shell and configuration to verify the FTP client library git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3656 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/Makefile | 2 +- apps/examples/README.txt | 15 ++ apps/examples/ftpc/Makefile | 105 +++++++++++ apps/examples/ftpc/ftpc.h | 125 +++++++++++++ apps/examples/ftpc/ftpc_cmds.c | 300 ++++++++++++++++++++++++++++++ apps/examples/ftpc/ftpc_main.c | 410 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 956 insertions(+), 1 deletion(-) create mode 100644 apps/examples/ftpc/Makefile create mode 100644 apps/examples/ftpc/ftpc.h create mode 100755 apps/examples/ftpc/ftpc_cmds.c create mode 100755 apps/examples/ftpc/ftpc_main.c (limited to 'apps/examples') diff --git a/apps/examples/Makefile b/apps/examples/Makefile index 18a395fb1..d4e96c11a 100644 --- a/apps/examples/Makefile +++ b/apps/examples/Makefile @@ -37,7 +37,7 @@ # Sub-directories -SUBDIRS = dhcpd hello helloxx hidkbd igmp mm mount nettest nsh null nx \ +SUBDIRS = dhcpd ftpc hello helloxx hidkbd igmp mm mount nettest nsh null nx \ nxffs nxflat ostest pashello pipe poll rgmp romfs sendmail serloop \ thttpd udp uip usbserial usbstorage wget wlan diff --git a/apps/examples/README.txt b/apps/examples/README.txt index a8216be39..f14037e72 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -48,6 +48,21 @@ examples/dhcpd CONFIGURED_APPS += uiplib +examples/ftpc +^^^^^^^^^^^^^ + + This is a simple FTP client shell used to exercise the capabilities + of the FTPC library (apps/netutils/ftpc). This example is configured + to that it will only work as a "built-in" program that can be run from + NSH when CONFIG_NSH_BUILTIN_APPS is defined. + + From NSH, the startup command line is then: + + ftpc xx.xx.xx.xx[:pp] + + where xx.xx.xx.xx is the IP address of the FTP server and pp is an + optional port number. + examples/hello ^^^^^^^^^^^^^^ diff --git a/apps/examples/ftpc/Makefile b/apps/examples/ftpc/Makefile new file mode 100644 index 000000000..ff2ec543b --- /dev/null +++ b/apps/examples/ftpc/Makefile @@ -0,0 +1,105 @@ +############################################################################ +# apps/examples/ftpc/Makefile +# +# Copyright (C) 2011 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# FTPC Client Application + +APPNAME = ftpc +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 4096 + +ASRCS = +CSRCS = ftpc_main.c ftpc_cmds.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(WINTOOL),y) + BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}" +else + BIN = "$(APPDIR)/libapps$(LIBEXT)" +endif + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + @( for obj in $(OBJS) ; do \ + $(call ARCHIVE, $(BIN), $${obj}); \ + done ; ) + @touch .built + +# Register application + +.context: + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + @touch $@ + +context: .context + +# Create dependencies + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + @rm -f *.o *~ .*.swp .built + $(call CLEAN) + +distclean: clean + @rm -f .context Make.dep .depend + +-include Make.dep diff --git a/apps/examples/ftpc/ftpc.h b/apps/examples/ftpc/ftpc.h new file mode 100644 index 000000000..42f1d3fa0 --- /dev/null +++ b/apps/examples/ftpc/ftpc.h @@ -0,0 +1,125 @@ +/**************************************************************************** + * apps/examples/ftpc/ftpc.h + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __APPS_EXAMPLES_FTPC_FTPC_H +#define __APPS_EXAMPLES_FTPC_FTPC_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/* Maximum size of one command line */ + +#ifndef CONFIG_FTPC_LINELEN +# define CONFIG_FTPC_LINELEN 80 +#endif + +/* If CONFIG_STDIO_LINEBUFFER is defined, the STDIO buffer will be flushed + * on each new line. Otherwise, STDIO needs to be explicitly flushed to + * see the output in context. + */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && \ + CONFIG_STDIO_BUFFER_SIZE > 0 && !defined(CONFIG_STDIO_LINEBUFFER) +# define FFLUSH() fflush(stdout) +#else +# define FFLUSH() +#endif + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +typedef int (*cmd_t)(SESSION handle, int argc, char **argv); + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +extern const char g_ftpcgreeting[]; +extern const char g_ftpcprompt[]; +extern const char g_fmtcmdfailed[]; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/* Network initialization */ + +extern int ftpc_netinit(void); + +/* Current working directory support */ + +extern char *ftpc_getfullpath(const char *relpath); +extern void ftpc_freefullpath(char *relpath); + +/* FTP command handlers */ + +extern int cmd_login(SESSION handle, int argc, char **argv); +extern int cmd_quit(SESSION handle, int argc, char **argv); +extern int cmd_chdir(SESSION handle, int argc, char **argv); +extern int cmd_rpwd(SESSION handle, int argc, char **argv); +extern int cmd_cdup(SESSION handle, int argc, char **argv); +extern int cmd_mkdir(SESSION handle, int argc, char **argv); + +extern int cmd_rmdir(SESSION handle, int argc, char **argv); +extern int cmd_unlink(SESSION handle, int argc, char **argv); +extern int cmd_chmod(SESSION handle, int argc, char **argv); +extern int cmd_rename(SESSION handle, int argc, char **argv); +extern int cmd_size(SESSION handle, int argc, char **argv); +extern int cmd_time(SESSION handle, int argc, char **argv); +extern int cmd_idle(SESSION handle, int argc, char **argv); +extern int cmd_noop(SESSION handle, int argc, char **argv); +extern int cmd_rhelp(SESSION handle, int argc, char **argv); +extern int cmd_rls(SESSION handle, int argc, char **argv); +extern int cmd_get(SESSION handle, int argc, char **argv); +extern int cmd_put(SESSION handle, int argc, char **argv); + +#endif /* __APPS_EXAMPLES_FTPC_FTPC_H */ diff --git a/apps/examples/ftpc/ftpc_cmds.c b/apps/examples/ftpc/ftpc_cmds.c new file mode 100755 index 000000000..4787da33e --- /dev/null +++ b/apps/examples/ftpc/ftpc_cmds.c @@ -0,0 +1,300 @@ +/**************************************************************************** + * examples/ftpc/ftpc_cmds.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include + +#include "ftpc.h" + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: cmd_login + ****************************************************************************/ + +int cmd_login(SESSION handle, int argc, char **argv) +{ + struct ftpc_login_s login = {0, 0, 0, true}; + + login.uname = argv[1]; + if (argc > 2) + { + login.pwd = argv[2]; + } + + return ftpc_login(handle, &login); +} + +/**************************************************************************** + * Name: cmd_quit + ****************************************************************************/ + +int cmd_quit(SESSION handle, int argc, char **argv) +{ + int ret = ftpc_quit(handle); + if (ret < 0) + { + printf("quit failed: %d\n", errno); + } + printf("Exitting...\n"); + exit(0); + return ERROR; +} + +/**************************************************************************** + * Name: cmd_chdir + ****************************************************************************/ + +int cmd_chdir(SESSION handle, int argc, char **argv) +{ + return ftpc_chdir(handle, argv[1]); +} + +/**************************************************************************** + * Name: cmd_rpwd + ****************************************************************************/ + +int cmd_rpwd(SESSION handle, int argc, char **argv) +{ + FAR char *pwd = ftpc_pwd(handle); + if (pwd) + { + printf("PWD: %s\n", pwd); + free(pwd); + return OK; + } + return ERROR; +} + +/**************************************************************************** + * Name: cmd_cdup + ****************************************************************************/ + +int cmd_cdup(SESSION handle, int argc, char **argv) +{ + return ftpc_cdup(handle); +} + +/**************************************************************************** + * Name: cmd_mkdir + ****************************************************************************/ + +int cmd_mkdir(SESSION handle, int argc, char **argv) +{ + return ftpc_mkdir(handle, argv[1]); +} + +/**************************************************************************** + * Name: cmd_rmdir + ****************************************************************************/ + +int cmd_rmdir(SESSION handle, int argc, char **argv) +{ + return ftpc_rmdir(handle, argv[1]); +} + +/**************************************************************************** + * Name: cmd_unlink + ****************************************************************************/ + +int cmd_unlink(SESSION handle, int argc, char **argv) +{ + return ftpc_unlink(handle, argv[1]); +} + +/**************************************************************************** + * Name: cmd_chmod + ****************************************************************************/ + +int cmd_chmod(SESSION handle, int argc, char **argv) +{ + return ftpc_chmod(handle, argv[1], argv[2]); +} + +/**************************************************************************** + * Name: cmd_rename + ****************************************************************************/ + +int cmd_rename(SESSION handle, int argc, char **argv) +{ + return ftpc_rename(handle, argv[1], argv[2]); +} + +/**************************************************************************** + * Name: cmd_size + ****************************************************************************/ + +int cmd_size(SESSION handle, int argc, char **argv) +{ + uint64_t size = ftpc_filesize(handle, argv[1]); + printf("SIZE: %ull\n", size); + return OK; +} + +/**************************************************************************** + * Name: cmd_time + ****************************************************************************/ + +int cmd_time(SESSION handle, int argc, char **argv) +{ + time_t filetime = ftpc_filetime(handle, argv[1]); + printf("TIME: %ul\n", (long)filetime); + return OK; +} + +/**************************************************************************** + * Name: cmd_idle + ****************************************************************************/ + +int cmd_idle(SESSION handle, int argc, char **argv) +{ + unsigned int idletime = 0; + + if (argc > 1) + { + idletime = atoi(argv[1]); + } + + return ftpc_idle(handle, idletime); +} + +/**************************************************************************** + * Name: cmd_noop + ****************************************************************************/ + +int cmd_noop(SESSION handle, int argc, char **argv) +{ + return ftpc_noop(handle); +} + +/**************************************************************************** + * Name: cmd_rhelp + ****************************************************************************/ + +int cmd_rhelp(SESSION handle, int argc, char **argv) +{ + FAR const char *cmd = NULL; + int ret; + + if (argc > 1) + { + cmd = argv[1]; + } + + ret = ftpc_help(handle, cmd); + if (ret == OK) + { + FAR char *msg = ftpc_response(handle); + puts(msg); + free(msg); + } + + return ret; +} + +/**************************************************************************** + * Name: cmd_rls + ****************************************************************************/ + +int cmd_rls(SESSION handle, int argc, char **argv) +{ + FAR struct ftpc_dirlist_s *dirlist; + int i; + + dirlist = ftpc_listdir(handle, argv[1]); + if (!dirlist) + { + return ERROR; + } + + printf("%s/\n", argv[1]); + for (i = 0; i < dirlist->nnames; i++) + { + printf(" %s\n", dirlist->name[i]); + } + + ftpc_dirfree(dirlist); + return OK; +} + +/**************************************************************************** + * Name: cmd_get + ****************************************************************************/ + +int cmd_get(SESSION handle, int argc, char **argv) +{ + FAR const char *lname = argv[1]; + + if (argc > 2) + { + lname = argv[2]; + } + return ftpc_getfile(handle, argv[1], lname, FTPC_GET_NORMAL, FTPC_XFRMODE_ASCII); +} + +/**************************************************************************** + * Name: cmd_put + ****************************************************************************/ + +int cmd_put(SESSION handle, int argc, char **argv) +{ + FAR const char *rname = argv[1]; + + if (argc > 2) + { + rname = argv[2]; + } + return ftp_putfile(handle, argv[1], rname, FTPC_PUT_NORMAL, FTPC_XFRMODE_ASCII); +} diff --git a/apps/examples/ftpc/ftpc_main.c b/apps/examples/ftpc/ftpc_main.c new file mode 100755 index 000000000..e80b80a0c --- /dev/null +++ b/apps/examples/ftpc/ftpc_main.c @@ -0,0 +1,410 @@ +/**************************************************************************** + * examples/ftpc/ftpc_main.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include +#include + +#include "ftpc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define FTPC_MAX_ARGUMENTS 3 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct cmdmap_s +{ + const char *cmd; /* Name of the command */ + cmd_t handler; /* Function that handles the command */ + uint8_t minargs; /* Minimum number of arguments (including command) */ + uint8_t maxargs; /* Maximum number of arguments (including command) */ + const char *usage; /* Usage instructions for 'help' command */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const char g_delim[] = " \t\n"; + +static int cmd_help(SESSION handle, int argc, char **argv); +static int cmd_unrecognized(SESSION handle, int argc, char **argv); + +static const struct cmdmap_s g_cmdmap[] = +{ + { "cd", cmd_chdir, 2, 2, "" }, + { "chmod", cmd_chmod, 3, 3, " " }, + { "get", cmd_unlink, 2, 3, " []" }, + { "help", cmd_help, 1, 2, "" }, + { "idle", cmd_idle, 1, 2, "[]" }, + { "login", cmd_login, 2, 3, " []" }, + { "ls", cmd_login, 1, 2, "[]" }, + { "quit", cmd_login, 1, 1, "" }, + { "mkdir", cmd_mkdir, 2, 2, "" }, + { "noop", cmd_noop, 1, 1, "" }, + { "put", cmd_unlink, 2, 3, " []" }, + { "pwd", cmd_rpwd, 1, 1, "" }, + { "rename", cmd_rename, 3, 3, " " }, + { "rhelp", cmd_rhelp, 1, 2, "[]" }, + { "rm", cmd_unlink, 2, 2, "" }, + { "rmdir", cmd_rmdir, 2, 2, "" }, + { "size", cmd_size, 2, 2, "" }, + { "time", cmd_time, 2, 2, "" }, + { "up", cmd_cdup, 1, 1, "" }, +}; + +static char g_line[CONFIG_FTPC_LINELEN]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: cmd_help + ****************************************************************************/ + +static int cmd_help(SESSION handle, int argc, char **argv) +{ + const struct cmdmap_s *ptr; + + printf("Local FTPC commands:\n"); + for (ptr = g_cmdmap; ptr->cmd; ptr++) + { + if (ptr->usage) + { + printf(" %s %s\n", ptr->cmd, ptr->usage); + } + else + { + printf(" %s\n", ptr->cmd); + } + } + return OK; +} + +/**************************************************************************** + * Name: cmd_unrecognized + ****************************************************************************/ + +static int cmd_unrecognized(SESSION handle, int argc, char **argv) +{ + printf("Command %s unrecognized\n", argv[0]); + return ERROR; +} + +/**************************************************************************** + * Name: ftpc_argument + ****************************************************************************/ + +char *ftpc_argument(char **saveptr) +{ + char *pbegin = *saveptr; + char *pend = NULL; + const char *term; + + /* Find the beginning of the next token */ + + for (; + *pbegin && strchr(g_delim, *pbegin) != NULL; + pbegin++); + + /* If we are at the end of the string with nothing + * but delimiters found, then return NULL. + */ + + if (!*pbegin) + { + return NULL; + } + + /* Does the token begin with '#' -- comment */ + + else if (*pbegin == '#') + { + /* Return NULL meaning that we are at the end of the line */ + + *saveptr = pbegin; + pbegin = NULL; + } + else + { + /* Otherwise, we are going to have to parse to find the end of + * the token. Does the token begin with '"'? + */ + + if (*pbegin == '"') + { + /* Yes.. then only another '"' can terminate the string */ + + pbegin++; + term = "\""; + } + else + { + /* No, then any of the usual terminators will terminate the argument */ + + term = g_delim; + } + + /* Find the end of the string */ + + for (pend = pbegin + 1; + *pend && strchr(term, *pend) == NULL; + pend++); + + /* pend either points to the end of the string or to + * the first delimiter after the string. + */ + + if (*pend) + { + /* Turn the delimiter into a null terminator */ + + *pend++ = '\0'; + } + + /* Save the pointer where we left off */ + + *saveptr = pend; + + } + + /* Return the beginning of the token. */ + + return pbegin; +} + +/**************************************************************************** + * Name: ftpc_execute + ****************************************************************************/ + +static int ftpc_execute(SESSION handle, int argc, char *argv[]) +{ + const struct cmdmap_s *cmdmap; + const char *cmd; + cmd_t handler = cmd_unrecognized; + int ret; + + /* The form of argv is: + * + * argv[0]: The command name. This is argv[0] when the arguments + * are, finally, received by the command handler + * argv[1]: The beginning of argument (up to FTPC_MAX_ARGUMENTS) + * argv[argc]: NULL terminating pointer + */ + + cmd = argv[0]; + + /* See if the command is one that we understand */ + + for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++) + { + if (strcmp(cmdmap->cmd, cmd) == 0) + { + /* Check if a valid number of arguments was provided. We + * do this simple, imperfect checking here so that it does + * not have to be performed in each command. + */ + + if (argc < cmdmap->minargs) + { + /* Fewer than the minimum number were provided */ + + printf("Too few arguments for '%s'\n", cmd); + return ERROR; + } + else if (argc > cmdmap->maxargs) + { + /* More than the maximum number were provided */ + + printf("Too many arguments for '%s'\n", cmd); + return ERROR; + } + else + { + /* A valid number of arguments were provided (this does + * not mean they are right). + */ + + handler = cmdmap->handler; + break; + } + } + } + + ret = handler(handle, argc, argv); + if (ret < 0) + { + printf("%s failed: %d\n", cmd, errno); + } + return ret; +} + +/**************************************************************************** + * Name: ftpc_parse + ****************************************************************************/ + +int ftpc_parse(SESSION handle, char *cmdline) +{ + FAR char *argv[FTPC_MAX_ARGUMENTS]; + FAR char *saveptr; + FAR char *cmd; + int argc; + int ret; + + /* Initialize parser state */ + + memset(argv, 0, FTPC_MAX_ARGUMENTS*sizeof(FAR char *)); + + /* Parse out the command at the beginning of the line */ + + saveptr = cmdline; + cmd = ftpc_argument(&saveptr); + + /* Check if any command was provided -OR- if command processing is + * currently disabled. + */ + + if (!cmd) + { + /* An empty line is not an error */ + + return OK; + } + + /* Parse all of the arguments following the command name. */ + + argv[0] = cmd; + for (argc = 1; argc < FTPC_MAX_ARGUMENTS; argc++) + { + argv[argc] = ftpc_argument(&saveptr); + if (!argv[argc]) + { + break; + } + } + argv[argc] = NULL; + + /* Check if the maximum number of arguments was exceeded */ + + if (argc > FTPC_MAX_ARGUMENTS) + { + printf("Too many arguments\n"); + ret = -EINVAL; + } + else + { + /* Then execute the command */ + + ret = ftpc_execute(handle, argc, argv); + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int ftpc_main(int argc, char **argv, char **envp) +{ + struct ftpc_connect_s connect = {{0}, 0}; + SESSION handle; + FAR char *ptr; + + if (argc != 2) + { + printf("Usage:\n"); + printf(" %s xx.xx.xx.xx[:pp]\n", argv[0]); + printf("Where\n"); + printf(" xx.xx.xx.xx is the IP address of the FTP server\n"); + printf(" pp is option port to use with the FTP server\n"); + exit(1); + } + + ptr = strchr(argv[1], ':'); + if (ptr) + { + *ptr = '\0'; + connect.port = atoi(ptr+1); + } + connect.addr.s_addr = inet_addr(argv[1]); + + handle = ftpc_connect(&connect); + if (!handle) + { + printf("Failed to connect to the server: %d\n", errno); + exit(1); + } + + /* Present a greeting */ + + printf("FTPC:\n"); + FFLUSH(); + + /* Then enter the command line parsing loop */ + + for (;;) + { + /* Display the prompt string */ + + fputs("FTPC>", stdout); + FFLUSH(); + + /* Get the next line of input */ + + if (fgets(g_line, CONFIG_FTPC_LINELEN, stdin)) + { + /* Parse process the command */ + + (void)ftpc_parse(handle, g_line); + FFLUSH(); + } + } + + return 0; +} \ No newline at end of file -- cgit v1.2.3