First approach to implement a command line parser.
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Fri, 8 Nov 2013 10:12:48 +0000 (11:12 +0100)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 9 Dec 2013 06:44:28 +0000 (07:44 +0100)
Add framework for parsing command line options.
Whole commands structure is a tree. We search from
root to leaves and print suitable help message if
an error occurred.

Change-Id: Ic3d3114b3d2380021da10410fafb50dcc8fe613a
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
24 files changed:
source/CMakeLists.txt [new file with mode: 0644]
source/base/CMakeLists.txt [new file with mode: 0644]
source/base/include/command.h [new file with mode: 0644]
source/base/include/executable_command.h [new file with mode: 0644]
source/base/include/parser.h [new file with mode: 0644]
source/base/src/command.c [new file with mode: 0644]
source/base/src/executable_command.c [new file with mode: 0644]
source/base/src/parser.c [new file with mode: 0644]
source/config/CMakeLists.txt [new file with mode: 0644]
source/config/include/configuration.h [new file with mode: 0644]
source/config/src/configuration.c [new file with mode: 0644]
source/function/CMakeLists.txt [new file with mode: 0644]
source/function/include/function.h [new file with mode: 0644]
source/function/src/function.c [new file with mode: 0644]
source/gadget/CMakeLists.txt [new file with mode: 0644]
source/gadget/include/gadget.h [new file with mode: 0644]
source/gadget/src/gadget.c [new file with mode: 0644]
source/main.c [new file with mode: 0644]
source/settings/CMakeLists.txt [new file with mode: 0644]
source/settings/include/settings.h [new file with mode: 0644]
source/settings/src/settings.c [new file with mode: 0644]
source/udc/CMakeLists.txt [new file with mode: 0644]
source/udc/include/udc.h [new file with mode: 0644]
source/udc/src/udc.c [new file with mode: 0644]

diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt
new file mode 100644 (file)
index 0000000..28e0a29
--- /dev/null
@@ -0,0 +1,28 @@
+cmake_minimum_required(VERSION 2.8)
+
+#project name
+project (gadget-tool)
+
+INCLUDE_DIRECTORIES( ${gadget-tool_SOURCE_DIR}/base/include )
+
+SET(CMAKE_C_FLAGS -g)
+
+add_subdirectory(config)
+add_subdirectory(function)
+add_subdirectory(gadget)
+add_subdirectory(settings)
+add_subdirectory(udc)
+add_subdirectory(base)
+
+SET( MAIN_SRC main.c)
+
+add_executable( gt ${MAIN_SRC})
+
+target_link_libraries( gt
+       base
+       udc
+       config
+       function
+       gadget
+       settings
+       )
\ No newline at end of file
diff --git a/source/base/CMakeLists.txt b/source/base/CMakeLists.txt
new file mode 100644 (file)
index 0000000..0ba8624
--- /dev/null
@@ -0,0 +1,15 @@
+
+INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include
+                       ${gadget-tool_SOURCE_DIR}/udc/include
+                       ${gadget-tool_SOURCE_DIR}/config/include
+                       ${gadget-tool_SOURCE_DIR}/function/include
+                       ${gadget-tool_SOURCE_DIR}/gadget/include
+                       ${gadget-tool_SOURCE_DIR}/settings/include )
+
+SET( BASE_SRC
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/command.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/executable_command.c
+       )
+
+add_library(base STATIC ${BASE_SRC} )
\ No newline at end of file
diff --git a/source/base/include/command.h b/source/base/include/command.h
new file mode 100644 (file)
index 0000000..04f52be
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file command.h
+ * @brief Definition of Command type used for parsing command line options
+ */
+
+#ifndef __GADGET_TOOL_COMMAND_H__
+#define __GADGET_TOOL_COMMAND_H__
+
+#include "executable_command.h"
+
+struct command;
+
+typedef void (*ParseFunc)(const struct command *cmd, int, const char **,
+               ExecutableCommand *, void *);
+typedef const struct command *(*GetChildrenFunc)(const struct command *cmd);
+
+/**
+ * @brief Representation of a single Command.
+ */
+typedef struct command
+{
+       /**
+        * @brief String of this command.
+        */
+       const char *name;
+       /**
+        * @brief Indicates if subcommand should be parsed from the same string
+        * or from the next one
+        */
+       enum
+       {
+               NEXT = 1,
+               AGAIN = 0
+       } distance;
+       /**
+        * @brief Parses this command.
+        * @details If this param is NULL the whole structure is invalid
+        */
+       ParseFunc parse;
+       /**
+        * @brief Gets the children of this command if any
+        */
+       GetChildrenFunc getChildren;
+       /**
+        * @brief Function used to show help if parsing fails.
+        */
+       ExecutableFunc printHelp;
+} Command;
+
+/**
+ * @brief Looks for a suitable child and recursively parses it.
+ * @details Used convention says that when string is NULL it matches
+ * all other strings
+ *
+ * @param[in] cmd which children should be considered
+ * @param[in] argc number of commands left till end of input
+ * @param[in] argv place to start parse user input
+ * @param[out] exec structure to be filled in with suitable command
+ * @param[in] data additional data
+ */
+void command_parse(const Command *cmd, int argc, const char **argv,
+               ExecutableCommand *exec, void *data);
+
+#endif //__GADGET_TOOL_COMMAND_H__
diff --git a/source/base/include/executable_command.h b/source/base/include/executable_command.h
new file mode 100644 (file)
index 0000000..5c3c3c1
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file executable_command.h
+ * @brief Definition of ExecutableCommand type used for executing actions.
+ */
+
+#ifndef __GADGET_TOOL_EXECUTABLE_COMMAND_H__
+#define __GADGET_TOOL_EXECUTABLE_COMMAND_H__
+
+typedef int (*ExecutableFunc)(void *);
+typedef void (*CleanupFunc)(void *);
+
+typedef struct
+{
+       ExecutableFunc exec;
+       void *data;
+       CleanupFunc destructor;
+} ExecutableCommand;
+
+/**
+ * @brief Fills to_set with given values.
+ * @param[out] to_set structure to be filled in
+ * @param[in] exec function to be set for execution
+ * @param[in] data to be passed to function
+ * @param[in] destructor to clean up data
+ * @note If to_set is NULL function does nothing
+ */
+void executable_command_set(ExecutableCommand *to_set, ExecutableFunc exec,
+               void *data, CleanupFunc destructor);
+
+/**
+ * @brief Cleans the stored data with destructor and sets to_clean fields
+ * to NULL.
+ *
+ * @param[in, out] to_clean structure to be cleaned
+ * @note If to_set is NULL function does nothing
+ */
+void executable_command_clean(ExecutableCommand *to_clean);
+
+/**
+ * @brief Executes the command.
+ * @param[in] cmd to be executed
+ * @return Value returned from executed function or -1 if function not set.
+ */
+int executable_command_exec(ExecutableCommand *cmd);
+
+#endif //__GADGET_TOOL_EXECUTABLE_COMMAND_H__
diff --git a/source/base/include/parser.h b/source/base/include/parser.h
new file mode 100644 (file)
index 0000000..4573880
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file parser.h
+ * @brief Declaration of methods related to parsing command line option.
+ */
+
+#ifndef __GADGET_TOOL_PARSER_H__
+#define __GADGET_TOOL_PARSER_H__
+
+#include "executable_command.h"
+
+/**
+ * @brief Parse the user input and determine a command to run.
+ * @param[in] argc number of arguments provided by user
+ * @param[in] argv user input
+ * @param[out] exec which will be set with suitable values for parsed command
+ * execution
+ */
+void gt_parse_commands(int argc, const char **argv, ExecutableCommand *exec);
+
+#endif //__GADGET_TOOL_PARSER_H__
diff --git a/source/base/src/command.c b/source/base/src/command.c
new file mode 100644 (file)
index 0000000..4d0162c
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file command.c
+ * @brief Declaration of methods related to Command data type.
+ */
+
+#include <string.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+#include "command.h"
+
+
+
+static inline bool name_matches(const char *candidate, const char *pattern)
+{
+       // If string is NULL it suits to all strings
+       return (candidate && (strcmp(candidate, pattern) == 0))
+                       || (!candidate);
+}
+
+void command_parse(const Command *cmd, int argc, const char **argv,
+               ExecutableCommand *exec, void *data)
+{
+       const Command *next = cmd->getChildren(cmd);
+       bool found = false;
+
+       // If there is nothing more to parse it's error
+       if (argc == 0)
+       {
+               executable_command_set(exec, cmd->printHelp, data, NULL);
+               return;
+       }
+
+       while (next->parse)
+       {
+               if (name_matches(next->name, *argv))
+               {
+                       found = true;
+                       break;
+               }
+               ++next;
+       }
+
+       if (found)
+       {
+               // We've found a suitable child so let's go deeper
+               next->parse(next, argc - next->distance, argv + next->distance, exec,
+                               data);
+       }
+       else
+       {
+               // We haven't found any suitable child so let's print help
+               executable_command_set(exec, cmd->printHelp, data, NULL);
+       }
+}
diff --git a/source/base/src/executable_command.c b/source/base/src/executable_command.c
new file mode 100644 (file)
index 0000000..f79222f
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file executable_command.c
+ * @brief Declaration of methods related to ExecutableCommand data type.
+ */
+
+#include <stdlib.h>
+
+#include "executable_command.h"
+
+void executable_command_set(ExecutableCommand *to_set, ExecutableFunc exec,
+               void *data, CleanupFunc destructor)
+{
+       if (to_set)
+       {
+               to_set->exec = exec;
+               to_set->data = data;
+               to_set->destructor = destructor;
+       }
+}
+
+void executable_command_clean(ExecutableCommand *to_clean)
+{
+       if (to_clean)
+       {
+               if (to_clean->destructor)
+               {
+                       to_clean->destructor(to_clean->data);
+               }
+               to_clean->exec = NULL;
+               to_clean->data = NULL;
+               to_clean->destructor = NULL;
+       }
+}
+
+int executable_command_exec(ExecutableCommand *cmd)
+{
+       return (cmd && cmd->exec) ? cmd->exec(cmd->data) : -1;
+}
diff --git a/source/base/src/parser.c b/source/base/src/parser.c
new file mode 100644 (file)
index 0000000..6b1cd99
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file parser.c
+ * @brief Implementation of functions used for parsing command line options.
+ */
+
+#include "parser.h"
+#include "command.h"
+#include "udc.h"
+#include "gadget.h"
+#include "configuration.h"
+#include "function.h"
+#include "settings.h"
+
+#include <stdio.h>
+
+int gt_global_help(void *data)
+{
+       printf("Global help function\n");
+       return -1;
+}
+
+static inline const Command *gt_get_command_root_children(const Command *cmd)
+{
+       static Command commands[] =
+       {
+       { "udc", NEXT, udc_parse, NULL, udc_help_func },
+       { "settings", NEXT, command_parse, gt_settings_get_children, gt_settings_help },
+       { "config", NEXT, command_parse, gt_config_get_children, gt_config_help },
+       { "func", NEXT, command_parse, gt_func_get_children, gt_func_help },
+       { NULL, AGAIN, command_parse, get_gadget_children, gt_global_help },
+       { NULL, AGAIN, NULL, NULL, NULL } };
+
+       return commands;
+}
+
+static inline const Command *gt_get_command_root(const Command *cmd)
+{
+       static Command tool_names[] =
+       {
+       { NULL, NEXT, command_parse, gt_get_command_root_children, gt_global_help },
+       { NULL, AGAIN, NULL, NULL, NULL } };
+       return tool_names;
+}
+
+void gt_parse_commands(int argc, const char **argv, ExecutableCommand *exec)
+{
+       static Command command_pre_root =
+       { NULL, AGAIN, command_parse, gt_get_command_root, gt_global_help };
+
+       command_pre_root.parse(&command_pre_root, argc, argv, exec, NULL);
+}
diff --git a/source/config/CMakeLists.txt b/source/config/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ca55338
--- /dev/null
@@ -0,0 +1,8 @@
+
+INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
+
+SET( CONFIG_SRC
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/configuration.c
+       )
+
+add_library(config STATIC ${CONFIG_SRC} )
\ No newline at end of file
diff --git a/source/config/include/configuration.h b/source/config/include/configuration.h
new file mode 100644 (file)
index 0000000..1497fff
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __GADGET_TOOL_CONFIGURATION_CONFIGURATION_H__
+#define __GADGET_TOOL_CONFIGURATION_CONFIGURATION_H__
+
+#include "command.h"
+
+/**
+ * @brief Gets the next possible commands after config
+ * @param[in] cmd actual command (should be config)
+ * @return Pointer to table with all children of cmd
+ * where the last element is invalid structure filled
+ * with NULLs.
+ */
+const Command *gt_config_get_children(const Command *cmd);
+
+/**
+ * @brief Help function which should be used if invalid
+ * syntax for config was entered.
+ *
+ * @param[in] data additional data
+ * @return -1 because invalid syntax has been provided
+ */
+int gt_config_help(void *data);
+
+#endif //__GADGET_TOOL_CONFIGURATION_CONFIGURATION_H__
diff --git a/source/config/src/configuration.c b/source/config/src/configuration.c
new file mode 100644 (file)
index 0000000..631f877
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "configuration.h"
+
+int gt_config_help(void *data)
+{
+       printf("Configuration help function\n");
+       return -1;
+}
+
+const Command *gt_config_get_children(const Command *cmd)
+{
+       static Command commands[] = {
+                       {NULL, AGAIN, NULL, NULL, NULL}
+       };
+       return commands;
+}
diff --git a/source/function/CMakeLists.txt b/source/function/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ac0fefb
--- /dev/null
@@ -0,0 +1,8 @@
+
+INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
+
+SET( FUNCTION_SRC
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/function.c
+       )
+
+add_library(function STATIC ${FUNCTION_SRC} )
\ No newline at end of file
diff --git a/source/function/include/function.h b/source/function/include/function.h
new file mode 100644 (file)
index 0000000..273051e
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __GADGET_TOOL_FUNCTION_FUNCTION_H__
+#define __GADGET_TOOL_FUNCTION_FUNCTION_H__
+
+#include "command.h"
+
+/**
+ * @brief Gets the next possible commands after func
+ * @param[in] cmd actual command (should be func)
+ * @return Pointer to table with all children of cmd
+ * where the last element is invalid structure filled
+ * with NULLs.
+ */
+const Command *gt_func_get_children(const Command *cmd);
+
+/**
+ * @brief Help function which should be used if invalid
+ * syntax for function was entered.
+ *
+ * @param[in] data additional data
+ * @return -1 because invalid syntax has been provided
+ */
+int gt_func_help(void *data);
+
+#endif //__GADGET_TOOL_FUNCTION_FUNCTION_H__
diff --git a/source/function/src/function.c b/source/function/src/function.c
new file mode 100644 (file)
index 0000000..9181f70
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "function.h"
+
+int gt_func_help(void *data)
+{
+       printf("Function help function\n");
+       return -1;
+}
+
+const Command *gt_func_get_children(const Command *cmd)
+{
+       static Command commands[] = {
+                       {NULL, AGAIN, NULL, NULL, NULL}
+       };
+       return commands;
+}
diff --git a/source/gadget/CMakeLists.txt b/source/gadget/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7f2eec5
--- /dev/null
@@ -0,0 +1,8 @@
+
+INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
+
+SET( GADGET_SRC
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/gadget.c
+       )
+
+add_library(gadget STATIC ${GADGET_SRC} )
\ No newline at end of file
diff --git a/source/gadget/include/gadget.h b/source/gadget/include/gadget.h
new file mode 100644 (file)
index 0000000..90683dd
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __GADGET_TOOL_GADGET_GADGET_H__
+#define __GADGET_TOOL_GADGET_GADGET_H__
+
+#include "command.h"
+
+/**
+ * @brief Gets the commands possible for gadget
+ * @param[in] cmd actual command
+ * @return Pointer to table with all children of cmd
+ * where the last element is invalid structure filled
+ * with NULLs.
+ */
+const Command *get_gadget_children(const Command *cmd);
+
+/**
+ * @brief Help function which should be used if invalid
+ * syntax for gadget was entered.
+ *
+ * @param[in] data additional data
+ * @return -1 because invalid syntax has been provided
+ */
+int gt_gadget_help(void *data);
+
+#endif //__GADGET_TOOL_GADGET_GADGET_H__
diff --git a/source/gadget/src/gadget.c b/source/gadget/src/gadget.c
new file mode 100644 (file)
index 0000000..e2257d9
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "gadget.h"
+
+const Command *get_gadget_children(const Command *cmd)
+{
+       static Command commands[] = {
+                       /*{"create", parse_gadget_create, NULL, gadget_create_help_func},
+                       {"rm", parse_gadget_rm, NULL, gadget_rm_help_func},
+                       {"get", parse_gadget_get, NULL, gadget_get_help_func},
+                       {"set", parse_gadget_set, NULL, gadget_set_help_func},
+                       {"enable", parse_gadget_enable, NULL, gadget_enable_help_func},
+                       {"disable", parse_gadget_disable, NULL, gadget_disable_help_func},
+                       {"gadget", parse_gadget_gadget, NULL, gadget_gadget_help_func},
+                       {"template", parse_gadget_template, NULL, gadget_template_help_func},
+                       {"load", parse_gadget_load, NULL, gadget_load_help_func},
+                       {"save", parse_gadget_save, NULL, gadget_load_help_func}*/
+                       {NULL, AGAIN, NULL, NULL, NULL}
+       };
+       return commands;
+}
+
+int gt_gadget_help(void *data)
+{
+       printf("Gadget help function\n");
+       return -1;
+}
diff --git a/source/main.c b/source/main.c
new file mode 100644 (file)
index 0000000..f791142
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file main.c
+ * @brief Main file of gadget tool.
+ * @details Gadget tool is a command line tool for gadget creation and
+ * maintenance. Firstly gt needs to parse command line arguments to determine
+ * what needs to be done. Then gt just executes a function related to action
+ * specified by user.
+ */
+
+#include <stdio.h>
+
+#include "parser.h"
+#include "executable_command.h"
+
+int main(int argc, const char **av)
+{
+       const char **argv = av;
+       int ret;
+       ExecutableCommand cmd;
+
+       gt_parse_commands(argc, argv, &cmd);
+
+       ret = executable_command_exec(&cmd);
+       executable_command_clean(&cmd);
+       return ret;
+}
diff --git a/source/settings/CMakeLists.txt b/source/settings/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7dcfa96
--- /dev/null
@@ -0,0 +1,8 @@
+
+INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
+
+SET( SETTINGS_SRC
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/settings.c
+       )
+
+add_library(settings STATIC ${SETTINGS_SRC} )
\ No newline at end of file
diff --git a/source/settings/include/settings.h b/source/settings/include/settings.h
new file mode 100644 (file)
index 0000000..e3feff5
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __GADGET_TOOL_SETTINGS_SETTINGS_H__
+#define __GADGET_TOOL_SETTINGS_SETTINGS_H__
+
+#include "command.h"
+
+/**
+ * @brief Gets the next possible commands after settings
+ * @param[in] cmd actual command (should be settings)
+ * @return Pointer to table with all children of cmd
+ * where the last element is invalid structure filled
+ * with NULLs.
+ */
+const Command *gt_settings_get_children(const Command *cmd);
+
+/**
+ * @brief Help function which should be used if invalid
+ * syntax for settings was entered.
+ *
+ * @param[in] data additional data
+ * @return -1 because invalid syntax has been provided
+ */
+int gt_settings_help(void *data);
+
+#endif //__GADGET_TOOL_SETTINGS_SETTINGS_H__
diff --git a/source/settings/src/settings.c b/source/settings/src/settings.c
new file mode 100644 (file)
index 0000000..9f7d200
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "settings.h"
+
+int gt_settings_help(void *data)
+{
+       printf("Settings help function\n");
+       return -1;
+}
+
+const Command *gt_settings_get_children(const Command *cmd)
+{
+       static Command commands[] = {
+               //      {"get", NEXT, settings_parse_get, get_gt_root_children, global_help_func},
+               //      {"set", NEXT, settings_parse_set, get_gt_root_children, global_help_func},
+               //      {"append", NEXT, settings_parse_append, get_gt_root_children, global_help_func},
+               //      {"detach", NEXT, settings_parse_detach, get_gt_root_children, global_help_func},
+                       {NULL, AGAIN, NULL, NULL, NULL}
+       };
+       return commands;
+}
diff --git a/source/udc/CMakeLists.txt b/source/udc/CMakeLists.txt
new file mode 100644 (file)
index 0000000..041b15f
--- /dev/null
@@ -0,0 +1,8 @@
+
+INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
+
+SET( UDC_SRC
+       ${CMAKE_CURRENT_SOURCE_DIR}/src/udc.c
+       )
+
+add_library(udc STATIC ${UDC_SRC} )
diff --git a/source/udc/include/udc.h b/source/udc/include/udc.h
new file mode 100644 (file)
index 0000000..26e9a5f
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __GADGET_TOOL_UDC_UDC_H__
+#define __GADGET_TOOL_UDC_UDC_H__
+
+#include "command.h"
+
+/**
+ * @brief Help function which should be used if invalid
+ * syntax for udc was entered.
+ *
+ * @param[in] data additional data
+ * @return -1 because invalid syntax has been provided
+ */
+int udc_help_func(void *data);
+
+/**
+ * @brief Gets the next possible commands after udc
+ * @param[in] cmd actual command (should be udc)
+ * @return Pointer to table with all children of cmd
+ * where the last element is invalid structure filled
+ * with NULLs.
+ */
+void udc_parse(const Command *cmd, int argc, const char **argv,
+               ExecutableCommand *exec, void * data);
+
+#endif //__GADGET_TOOL_UDC_UDC_PARSE_H__
diff --git a/source/udc/src/udc.c b/source/udc/src/udc.c
new file mode 100644 (file)
index 0000000..c60481d
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+
+#include "udc.h"
+
+static int udc_func(void *data)
+{
+       printf("gt udc called successfully. Not implemented yet.\n");
+       return 0;
+}
+
+int udc_help_func(void *data)
+{
+       printf("UDC help func. Not implemented yet.\n");
+       return -1;
+}
+
+void udc_parse(const Command *cmd, int argc, const char **argv,
+               ExecutableCommand *exec, void * data)
+{
+       if(argc == 0) {
+               // udc should be run run without args
+               executable_command_set(exec, udc_func, data, NULL);
+       } else {
+               // Wrong syntax for udc command, let's print help
+               executable_command_set(exec, cmd->printHelp, data, NULL);
+       }
+}