BAL and Maple Release 2.2

Signed-off-by: Shad Ansari <developer@Carbon.local>
diff --git a/bal_release/src/lib/libcmdline/Makefile b/bal_release/src/lib/libcmdline/Makefile
new file mode 100644
index 0000000..5c5038d
--- /dev/null
+++ b/bal_release/src/lib/libcmdline/Makefile
@@ -0,0 +1,35 @@
+###############################################################################
+#
+#  <:copyright-BRCM:2016:DUAL/GPL:standard
+#  
+#     Copyright (c) 2016 Broadcom
+#     All Rights Reserved
+#  
+#  Unless you and Broadcom execute a separate written software license
+#  agreement governing use of this software, this software is licensed
+#  to you under the terms of the GNU General Public License version 2
+#  (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
+#  with the following added to such license:
+#  
+#     As a special exception, the copyright holders of this software give
+#     you permission to link this software with independent modules, and
+#     to copy and distribute the resulting executable under terms of your
+#     choice, provided that you also meet, for each linked independent
+#     module, the terms and conditions of the license of that module.
+#     An independent module is a module which is not derived from this
+#     software.  The special exception does not apply to any modifications
+#     of the software.
+#  
+#  Not withstanding the above, under no circumstances may you combine
+#  this software in any way with any other Broadcom software provided
+#  under a license other than the GPL, without Broadcom's express prior
+#  written consent.
+#  
+#  :>
+#
+###############################################################################
+MOD_NAME = cmdline
+MOD_TYPE = lib
+
+srcs = cmdline.c
+
diff --git a/bal_release/src/lib/libcmdline/cmdline.c b/bal_release/src/lib/libcmdline/cmdline.c
new file mode 100644
index 0000000..d20b10a
--- /dev/null
+++ b/bal_release/src/lib/libcmdline/cmdline.c
@@ -0,0 +1,135 @@
+/******************************************************************************
+ *
+ *  <:copyright-BRCM:2016:DUAL/GPL:standard
+ *
+ *     Copyright (c) 2016 Broadcom
+ *     All Rights Reserved
+ *
+ *  Unless you and Broadcom execute a separate written software license
+ *  agreement governing use of this software, this software is licensed
+ *  to you under the terms of the GNU General Public License version 2
+ *  (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
+ *  with the following added to such license:
+ *
+ *     As a special exception, the copyright holders of this software give
+ *     you permission to link this software with independent modules, and
+ *     to copy and distribute the resulting executable under terms of your
+ *     choice, provided that you also meet, for each linked independent
+ *     module, the terms and conditions of the license of that module.
+ *     An independent module is a module which is not derived from this
+ *     software.  The special exception does not apply to any modifications
+ *     of the software.
+ *
+ *  Not withstanding the above, under no circumstances may you combine
+ *  this software in any way with any other Broadcom software provided
+ *  under a license other than the GPL, without Broadcom's express prior
+ *  written consent.
+ *
+ *  :>
+ *
+ *****************************************************************************/
+/*
+ * cmdline.c - Command line argument helper
+ */
+
+#include <cmdline.h>
+
+/* Print usage information */
+bcmos_errno cl_print_usage(const char *app, const char *arg,
+    const cl_argument supported_args[], int num_supported_args,
+    cl_argument_usage_flags flags)
+{
+    const char *owner = "";
+    int i;
+
+    if (arg)
+        bcmos_printf("*** Error in parameter %s\n", arg);
+    if (app)
+    {
+        const char *p, *fname;
+        p = strrchr(app, '/');
+        fname = p ? p + 1 : app;
+        bcmos_printf("Usage: %s arguments\n", fname);
+    }
+    for (i = 0; i < num_supported_args; i++)
+    {
+        const cl_argument *opt = &supported_args[i];
+        if ((flags & CL_ARGUMENT_USAGE_FLAG_OWNER) != 0 &&
+            opt->owner && strcmp(opt->owner, owner))
+        {
+            owner = opt->owner;
+            bcmos_printf("\n%s options:\n", owner);
+        }
+        if (opt->short_name)
+            bcmos_printf("%-3s%c", opt->short_name, opt->long_name ? ',' : ' ');
+        else
+            bcmos_printf("    ");
+        bcmos_printf("%-20s %20s  %s\n",
+            opt->long_name ? opt->long_name : "",
+            opt->extra_arg ? opt->extra_arg : "",
+            opt->description ? opt->description : "");
+    }
+    return BCM_ERR_PARM;
+}
+
+/* Get parameter by name */
+const cl_argument *cl_parm_get(const char *name, const cl_argument supported_args[], int num_supported_args)
+{
+    const cl_argument *opt = NULL;
+    int i;
+
+    /* Make sure that all mandatory parameters are set */
+    for (i = 0; i < num_supported_args; i++)
+    {
+        opt = &supported_args[i];
+        if ((opt->short_name && !strcmp(opt->short_name, name)) ||
+            (opt->long_name && !strcmp(opt->long_name, name)))
+            break;
+    }
+    return (i < num_supported_args) ? opt : NULL;
+}
+
+/* Validate command line parameters */
+bcmos_errno cl_validate(int argc, char *argv[],
+    const cl_argument supported_args[], int num_supported_args)
+{
+    bcmos_bool parm_set[num_supported_args];
+    bcmos_errno rc = BCM_ERR_OK;
+    int i;
+
+    memset(parm_set, 0, sizeof(parm_set));
+    for (i = 1; i < argc; i++)
+    {
+        const cl_argument *opt = cl_parm_get(argv[i], supported_args, num_supported_args);
+        if (opt == NULL)
+        {
+            bcmos_printf("*** Invalid parameter: %s\n", argv[i]);
+            rc = BCM_ERR_PARM;
+            break;
+        }
+        if (opt->extra_arg)
+        {
+            ++i;
+            if (i >= argc)
+            {
+                bcmos_printf("*** Argument is missing after %s\n", argv[i-1]);
+                rc = BCM_ERR_PARM;
+                break;
+            }
+        }
+        parm_set[opt - supported_args] = BCMOS_TRUE;
+    }
+
+    /* Make sure that all mandatory parameters are set */
+    for (i = 0; i < num_supported_args; i++)
+    {
+        const cl_argument *opt = &supported_args[i];
+        if ((opt->flags & CL_ARGUMENT_FLAG_MANDATORY) && !parm_set[i])
+        {
+            bcmos_printf("*** Mandatory parameter %s is missing\n", opt->long_name ? opt->long_name : opt->short_name);
+            rc = BCM_ERR_PARM;
+            break;
+        }
+    }
+    return rc;
+}
diff --git a/bal_release/src/lib/libcmdline/cmdline.h b/bal_release/src/lib/libcmdline/cmdline.h
new file mode 100644
index 0000000..cad20b0
--- /dev/null
+++ b/bal_release/src/lib/libcmdline/cmdline.h
@@ -0,0 +1,101 @@
+/******************************************************************************
+ *
+ *  <:copyright-BRCM:2016:DUAL/GPL:standard
+ *
+ *     Copyright (c) 2016 Broadcom
+ *     All Rights Reserved
+ *
+ *  Unless you and Broadcom execute a separate written software license
+ *  agreement governing use of this software, this software is licensed
+ *  to you under the terms of the GNU General Public License version 2
+ *  (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
+ *  with the following added to such license:
+ *
+ *     As a special exception, the copyright holders of this software give
+ *     you permission to link this software with independent modules, and
+ *     to copy and distribute the resulting executable under terms of your
+ *     choice, provided that you also meet, for each linked independent
+ *     module, the terms and conditions of the license of that module.
+ *     An independent module is a module which is not derived from this
+ *     software.  The special exception does not apply to any modifications
+ *     of the software.
+ *
+ *  Not withstanding the above, under no circumstances may you combine
+ *  this software in any way with any other Broadcom software provided
+ *  under a license other than the GPL, without Broadcom's express prior
+ *  written consent.
+ *
+ *  :>
+ *
+ *****************************************************************************/
+/*
+ * cmdline.h - Command line argument helper
+ */
+
+#ifndef _CMDLINE_H_
+#define _CMDLINE_H_
+
+#include <bcmos_system.h>
+
+/** Argument flags */
+typedef enum
+{
+    CL_ARGUMENT_FLAG_NONE = 0,
+    CL_ARGUMENT_FLAG_MANDATORY = 0x1,
+} cl_argument_flags;
+
+/** Command line argument */
+typedef struct cl_argument
+{
+    const char *long_name;      /**< Command line option: long form */
+    const char *short_name;     /**< Command line option: short form */
+    const char *extra_arg;      /**< Extra arguments */
+    const char *description;    /**< Description */
+    const char *owner;          /**< Owner module */
+    cl_argument_flags flags;    /**< Command line argument flags */
+} cl_argument;
+
+/** Print usage flags */
+typedef enum
+{
+    CL_ARGUMENT_USAGE_FLAG_NONE = 0,
+    CL_ARGUMENT_USAGE_FLAG_OWNER = 0x1, /**< Print argument owners */
+} cl_argument_usage_flags;
+
+/** Print usage
+ * \param[in]   app                     Application name. Can be NULL
+ * \param[in]   arg                     Argument that triggered command line parsing error. Can be NULL
+ * \param[in]   supported_args          Array of supported arguments
+ * \param[in]   num_supported_args      Number of elements in supported_args[] array
+ * \param[in]   flags                   Usage flags
+ * \returns     BCM_ERR_PARM
+ */
+bcmos_errno cl_print_usage(const char *app, const char *arg,
+    const cl_argument supported_args[], int num_supported_args,
+    cl_argument_usage_flags flags);
+
+/** Validate parameters
+ *
+ * - make sure that there are no unknown parameters
+ * - make sure that all mandatory parameters are present
+ *
+ * \param[in]   argc                    Number of command line arguments
+ * \param[in]   argv                    Command line arguments
+ * \param[in]   supported_args          Array of supported arguments
+ * \param[in]   num_supported_args      Number of elements in supported_args[] array
+ * \returns     BCM_ERR_OK or BCM_ERR_PARM
+ */
+bcmos_errno cl_validate(int argc, char *argv[],
+    const cl_argument supported_args[], int num_supported_args);
+
+/** Get CL parameter by name
+ *
+ * \param[in]   name                    Parameter name
+ * \param[in]   supported_args          Supported arguments array
+ * \param[in]   num_supported_args      Supported arguments array size
+ * \returns argument descriptor pointer or NULL if not found
+ */
+const cl_argument *cl_parm_get(const char *name, const cl_argument supported_args[],
+    int num_supported_args);
+
+#endif /* _CMDLINE_H_ */