jim-nvp is moving from jimtcl to openocd
authorSteve Bennett <steveb@workware.net.au>
Thu, 29 Sep 2011 06:12:41 +0000 (16:12 +1000)
committerØyvind Harboe <oyvind.harboe@zylin.com>
Fri, 30 Sep 2011 07:38:22 +0000 (09:38 +0200)
The jim-nvp code is specific to openocd, so it belongs in openocd,
not in the core jimtcl.

Signed-off-by: Steve Bennett <steveb@workware.net.au>
Makefile.am
common.mk
configure.ac
src/helper/Makefile.am
src/helper/jim-nvp.c [new file with mode: 0644]
src/helper/jim-nvp.h [new file with mode: 0644]

index 7bc25af26db0c0d479c1945a98671ad0635d11a9..0d20233b4054f9a464b5aba18ca29f7ea5cc6416 100644 (file)
@@ -3,7 +3,7 @@
 AUTOMAKE_OPTIONS = gnu 1.6
 
 # make sure we pass the correct jimtcl flags to distcheck
-DISTCHECK_CONFIGURE_FLAGS = --with-jim-ext=nvp --disable-lineedit --disable-install-jim
+DISTCHECK_CONFIGURE_FLAGS = --disable-install-jim
 
 nobase_dist_pkgdata_DATA = \
        contrib/libdcc/dcc_stdio.c \
index 20b96bd4a1fb3e2ce4fd06eab69cb75470d13649..1cb0743ecdcb60c5fa275763f6199c1fabc3c5dc 100644 (file)
--- a/common.mk
+++ b/common.mk
@@ -2,6 +2,7 @@
 # common flags used in openocd build
 AM_CPPFLAGS = -I$(top_srcdir)/src \
                          -I$(top_builddir)/src \
+                         -I$(top_srcdir)/src/helper \
                          -DPKGDATADIR=\"$(pkgdatadir)\" \
                          -DPKGLIBDIR=\"$(pkglibdir)\"
 
index ec51bd7e275f76a9014b315e3c3b780679d2543d..ef847ec39afefe8a77fc01667cba5b3eea857799 100644 (file)
@@ -776,7 +776,7 @@ fi
 
 if test "$use_internal_jimtcl" = yes; then
   if test -f "$srcdir/jimtcl/configure.ac"; then
-    AX_CONFIG_SUBDIR_OPTION([jimtcl], [--with-jim-ext=nvp --disable-lineedit --disable-install-jim])
+    AX_CONFIG_SUBDIR_OPTION([jimtcl], [--disable-install-jim])
   else
     AC_MSG_ERROR([jimtcl not found, run git submodule init and git submodule update.])
   fi
index 25585a4896468309717118949694c1ac6a06bbbe..c9479b43edb18aba62a6f098f952e75d63059262 100644 (file)
@@ -20,7 +20,8 @@ libhelper_la_SOURCES = \
        time_support.c \
        replacements.c \
        fileio.c \
-       util.c
+       util.c \
+       jim-nvp.c
 
 if IOUTIL
 libhelper_la_SOURCES += ioutil.c
@@ -46,7 +47,8 @@ noinst_HEADERS = \
        replacements.h \
        fileio.h \
        system.h \
-       bin2char.c
+       bin2char.c \
+       jim-nvp.h
 
 EXTRA_DIST = startup.tcl
 
diff --git a/src/helper/jim-nvp.c b/src/helper/jim-nvp.c
new file mode 100644 (file)
index 0000000..be94f15
--- /dev/null
@@ -0,0 +1,338 @@
+#include <string.h>
+#include <jim-nvp.h>
+
+int Jim_GetNvp(Jim_Interp *interp,
+    Jim_Obj *objPtr, const Jim_Nvp * nvp_table, const Jim_Nvp ** result)
+{
+    Jim_Nvp *n;
+    int e;
+
+    e = Jim_Nvp_name2value_obj(interp, nvp_table, objPtr, &n);
+    if (e == JIM_ERR) {
+        return e;
+    }
+
+    /* Success? found? */
+    if (n->name) {
+        /* remove const */
+        *result = (Jim_Nvp *) n;
+        return JIM_OK;
+    }
+    else {
+        return JIM_ERR;
+    }
+}
+
+Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp * p, const char *name)
+{
+    while (p->name) {
+        if (0 == strcmp(name, p->name)) {
+            break;
+        }
+        p++;
+    }
+    return ((Jim_Nvp *) (p));
+}
+
+Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp * p, const char *name)
+{
+    while (p->name) {
+        if (0 == strcasecmp(name, p->name)) {
+            break;
+        }
+        p++;
+    }
+    return ((Jim_Nvp *) (p));
+}
+
+int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
+{
+    return Jim_Nvp_name2value(interp, p, Jim_String(o), result);
+}
+
+
+int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp * _p, const char *name, Jim_Nvp ** result)
+{
+    const Jim_Nvp *p;
+
+    p = Jim_Nvp_name2value_simple(_p, name);
+
+    /* result */
+    if (result) {
+        *result = (Jim_Nvp *) (p);
+    }
+
+    /* found? */
+    if (p->name) {
+        return JIM_OK;
+    }
+    else {
+        return JIM_ERR;
+    }
+}
+
+int
+Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** puthere)
+{
+    return Jim_Nvp_name2value_nocase(interp, p, Jim_String(o), puthere);
+}
+
+int
+Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp * _p, const char *name,
+    Jim_Nvp ** puthere)
+{
+    const Jim_Nvp *p;
+
+    p = Jim_Nvp_name2value_nocase_simple(_p, name);
+
+    if (puthere) {
+        *puthere = (Jim_Nvp *) (p);
+    }
+    /* found */
+    if (p->name) {
+        return JIM_OK;
+    }
+    else {
+        return JIM_ERR;
+    }
+}
+
+
+int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp * p, Jim_Obj *o, Jim_Nvp ** result)
+{
+    int e;;
+    jim_wide w;
+
+    e = Jim_GetWide(interp, o, &w);
+    if (e != JIM_OK) {
+        return e;
+    }
+
+    return Jim_Nvp_value2name(interp, p, w, result);
+}
+
+Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp * p, int value)
+{
+    while (p->name) {
+        if (value == p->value) {
+            break;
+        }
+        p++;
+    }
+    return ((Jim_Nvp *) (p));
+}
+
+
+int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp * _p, int value, Jim_Nvp ** result)
+{
+    const Jim_Nvp *p;
+
+    p = Jim_Nvp_value2name_simple(_p, value);
+
+    if (result) {
+        *result = (Jim_Nvp *) (p);
+    }
+
+    if (p->name) {
+        return JIM_OK;
+    }
+    else {
+        return JIM_ERR;
+    }
+}
+
+
+int Jim_GetOpt_Setup(Jim_GetOptInfo * p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+    memset(p, 0, sizeof(*p));
+    p->interp = interp;
+    p->argc = argc;
+    p->argv = argv;
+
+    return JIM_OK;
+}
+
+void Jim_GetOpt_Debug(Jim_GetOptInfo * p)
+{
+    int x;
+
+    fprintf(stderr, "---args---\n");
+    for (x = 0; x < p->argc; x++) {
+        fprintf(stderr, "%2d) %s\n", x, Jim_String(p->argv[x]));
+    }
+    fprintf(stderr, "-------\n");
+}
+
+
+int Jim_GetOpt_Obj(Jim_GetOptInfo * goi, Jim_Obj **puthere)
+{
+    Jim_Obj *o;
+
+    o = NULL;                   // failure
+    if (goi->argc) {
+        // success
+        o = goi->argv[0];
+        goi->argc -= 1;
+        goi->argv += 1;
+    }
+    if (puthere) {
+        *puthere = o;
+    }
+    if (o != NULL) {
+        return JIM_OK;
+    }
+    else {
+        return JIM_ERR;
+    }
+}
+
+int Jim_GetOpt_String(Jim_GetOptInfo * goi, char **puthere, int *len)
+{
+    int r;
+    Jim_Obj *o;
+    const char *cp;
+
+
+    r = Jim_GetOpt_Obj(goi, &o);
+    if (r == JIM_OK) {
+        cp = Jim_GetString(o, len);
+        if (puthere) {
+            /* remove const */
+            *puthere = (char *)(cp);
+        }
+    }
+    return r;
+}
+
+int Jim_GetOpt_Double(Jim_GetOptInfo * goi, double *puthere)
+{
+    int r;
+    Jim_Obj *o;
+    double _safe;
+
+    if (puthere == NULL) {
+        puthere = &_safe;
+    }
+
+    r = Jim_GetOpt_Obj(goi, &o);
+    if (r == JIM_OK) {
+        r = Jim_GetDouble(goi->interp, o, puthere);
+        if (r != JIM_OK) {
+            Jim_SetResultFormatted(goi->interp, "not a number: %#s", o);
+        }
+    }
+    return r;
+}
+
+int Jim_GetOpt_Wide(Jim_GetOptInfo * goi, jim_wide * puthere)
+{
+    int r;
+    Jim_Obj *o;
+    jim_wide _safe;
+
+    if (puthere == NULL) {
+        puthere = &_safe;
+    }
+
+    r = Jim_GetOpt_Obj(goi, &o);
+    if (r == JIM_OK) {
+        r = Jim_GetWide(goi->interp, o, puthere);
+    }
+    return r;
+}
+
+int Jim_GetOpt_Nvp(Jim_GetOptInfo * goi, const Jim_Nvp * nvp, Jim_Nvp ** puthere)
+{
+    Jim_Nvp *_safe;
+    Jim_Obj *o;
+    int e;
+
+    if (puthere == NULL) {
+        puthere = &_safe;
+    }
+
+    e = Jim_GetOpt_Obj(goi, &o);
+    if (e == JIM_OK) {
+        e = Jim_Nvp_name2value_obj(goi->interp, nvp, o, puthere);
+    }
+
+    return e;
+}
+
+void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo * goi, const Jim_Nvp * nvptable, int hadprefix)
+{
+    if (hadprefix) {
+        Jim_SetResult_NvpUnknown(goi->interp, goi->argv[-2], goi->argv[-1], nvptable);
+    }
+    else {
+        Jim_SetResult_NvpUnknown(goi->interp, NULL, goi->argv[-1], nvptable);
+    }
+}
+
+
+int Jim_GetOpt_Enum(Jim_GetOptInfo * goi, const char *const *lookup, int *puthere)
+{
+    int _safe;
+    Jim_Obj *o;
+    int e;
+
+    if (puthere == NULL) {
+        puthere = &_safe;
+    }
+    e = Jim_GetOpt_Obj(goi, &o);
+    if (e == JIM_OK) {
+        e = Jim_GetEnum(goi->interp, o, lookup, puthere, "option", JIM_ERRMSG);
+    }
+    return e;
+}
+
+void
+Jim_SetResult_NvpUnknown(Jim_Interp *interp,
+    Jim_Obj *param_name, Jim_Obj *param_value, const Jim_Nvp * nvp)
+{
+    if (param_name) {
+        Jim_SetResultFormatted(interp, "%#s: Unknown: %#s, try one of: ", param_name, param_value);
+    }
+    else {
+        Jim_SetResultFormatted(interp, "Unknown param: %#s, try one of: ", param_value);
+    }
+    while (nvp->name) {
+        const char *a;
+        const char *b;
+
+        if ((nvp + 1)->name) {
+            a = nvp->name;
+            b = ", ";
+        }
+        else {
+            a = "or ";
+            b = nvp->name;
+        }
+        Jim_AppendStrings(interp, Jim_GetResult(interp), a, b, NULL);
+        nvp++;
+    }
+}
+
+const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+    static Jim_Obj *debug_string_obj;
+
+    int x;
+
+    if (debug_string_obj) {
+        Jim_FreeObj(interp, debug_string_obj);
+    }
+
+    debug_string_obj = Jim_NewEmptyStringObj(interp);
+    for (x = 0; x < argc; x++) {
+        Jim_AppendStrings(interp, debug_string_obj, Jim_String(argv[x]), " ", NULL);
+    }
+
+    return Jim_String(debug_string_obj);
+}
+
+int Jim_nvpInit(Jim_Interp *interp)
+{
+    /* This is really a helper library, not an extension, but this is the easy way */
+    return JIM_OK;
+}
diff --git a/src/helper/jim-nvp.h b/src/helper/jim-nvp.h
new file mode 100644 (file)
index 0000000..12ff889
--- /dev/null
@@ -0,0 +1,275 @@
+#ifndef JIM_NVP_H
+#define JIM_NVP_H
+
+#include <jim.h>
+
+/** Name Value Pairs, aka: NVP
+ *   -  Given a string - return the associated int.
+ *   -  Given a number - return the associated string.
+ *   .
+ *
+ * Very useful when the number is not a simple index into an array of
+ * known string, or there may be multiple strings (aliases) that mean then same
+ * thing.
+ *
+ * An NVP Table is terminated with ".name = NULL".
+ *
+ * During the 'name2value' operation, if no matching string is found
+ * the pointer to the terminal element (with p->name == NULL) is returned.
+ *
+ * Example:
+ * \code
+ *      const Jim_Nvp yn[] = {
+ *          { "yes", 1 },
+ *          { "no" , 0 },
+ *          { "yep", 1 },
+ *          { "nope", 0 },
+ *          { NULL, -1 },
+ *      };
+ *
+ *  Jim_Nvp *result
+ *  e = Jim_Nvp_name2value(interp, yn, "y", &result);
+ *         returns &yn[0];
+ *  e = Jim_Nvp_name2value(interp, yn, "n", &result);
+ *         returns &yn[1];
+ *  e = Jim_Nvp_name2value(interp, yn, "Blah", &result);
+ *         returns &yn[4];
+ * \endcode
+ *
+ * During the number2name operation, the first matching value is returned.
+ */
+typedef struct {
+       const char *name;
+       int         value;
+} Jim_Nvp;
+
+
+int Jim_GetNvp (Jim_Interp *interp,
+                                                                       Jim_Obj *objPtr,
+                                                                       const Jim_Nvp *nvp_table,
+                                                                       const Jim_Nvp **result);
+
+/* Name Value Pairs Operations */
+Jim_Nvp *Jim_Nvp_name2value_simple(const Jim_Nvp *nvp_table, const char *name);
+Jim_Nvp *Jim_Nvp_name2value_nocase_simple(const Jim_Nvp *nvp_table, const char *name);
+Jim_Nvp *Jim_Nvp_value2name_simple(const Jim_Nvp *nvp_table, int v);
+
+int Jim_Nvp_name2value(Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result);
+int Jim_Nvp_name2value_nocase(Jim_Interp *interp, const Jim_Nvp *nvp_table, const char *name, Jim_Nvp **result);
+int Jim_Nvp_value2name(Jim_Interp *interp, const Jim_Nvp *nvp_table, int value, Jim_Nvp **result);
+
+int Jim_Nvp_name2value_obj(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result);
+int Jim_Nvp_name2value_obj_nocase(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *name_obj, Jim_Nvp **result);
+int Jim_Nvp_value2name_obj(Jim_Interp *interp, const Jim_Nvp *nvp_table, Jim_Obj *value_obj, Jim_Nvp **result);
+
+/** prints a nice 'unknown' parameter error message to the 'result' */
+void Jim_SetResult_NvpUnknown(Jim_Interp *interp,
+                                                                                                  Jim_Obj *param_name,
+                                                                                                  Jim_Obj *param_value,
+                                                                                                  const Jim_Nvp *nvp_table);
+
+
+/** Debug: convert argc/argv into a printable string for printf() debug
+ *
+ * \param interp - the interpeter
+ * \param argc   - arg count
+ * \param argv   - the objects
+ *
+ * \returns string pointer holding the text.
+ *
+ * Note, next call to this function will free the old (last) string.
+ *
+ * For example might want do this:
+ * \code
+ *     fp = fopen("some.file.log", "a");
+ *     fprintf(fp, "PARAMS are: %s\n", Jim_DebugArgvString(interp, argc, argv));
+ *     fclose(fp);
+ * \endcode
+ */
+const char *Jim_Debug_ArgvString(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
+
+
+/** A TCL -ish GetOpt like code.
+ *
+ * Some TCL objects have various "configuration" values.
+ * For example - in Tcl/Tk the "buttons" have many options.
+ *
+ * Usefull when dealing with command options.
+ * that may come in any order...
+ *
+ * Does not support "-foo = 123" type options.
+ * Only supports tcl type options, like "-foo 123"
+ */
+
+typedef struct jim_getopt {
+       Jim_Interp     *interp;
+       int            argc;
+       Jim_Obj        * const * argv;
+       int            isconfigure; /* non-zero if configure */
+} Jim_GetOptInfo;
+
+/** GetOpt - how to.
+ *
+ * Example (short and incomplete):
+ * \code
+ *   Jim_GetOptInfo goi;
+ *
+ *   Jim_GetOpt_Setup(&goi, interp, argc, argv);
+ *
+ *   while (goi.argc) {
+ *         e = Jim_GetOpt_Nvp(&goi, nvp_options, &n);
+ *         if (e != JIM_OK) {
+ *               Jim_GetOpt_NvpUnknown(&goi, nvp_options, 0);
+ *               return e;
+ *         }
+ *
+ *         switch (n->value) {
+ *         case ALIVE:
+ *             printf("Option ALIVE specified\n");
+ *             break;
+ *         case FIRST:
+ *             if (goi.argc < 1) {
+ *                     .. not enough args error ..
+ *             }
+ *             Jim_GetOpt_String(&goi, &cp, NULL);
+ *             printf("FIRSTNAME: %s\n", cp);
+ *         case AGE:
+ *             Jim_GetOpt_Wide(&goi, &w);
+ *             printf("AGE: %d\n", (int)(w));
+ *             break;
+ *         case POLITICS:
+ *             e = Jim_GetOpt_Nvp(&goi, nvp_politics, &n);
+ *             if (e != JIM_OK) {
+ *                 Jim_GetOpt_NvpUnknown(&goi, nvp_politics, 1);
+ *                 return e;
+ *             }
+ *         }
+ *  }
+ *
+ * \endcode
+ *
+ */
+
+/** Setup GETOPT
+ *
+ * \param goi    - get opt info to be initialized
+ * \param interp - jim interp
+ * \param argc   - argc count.
+ * \param argv   - argv (will be copied)
+ *
+ * \code
+ *     Jim_GetOptInfo  goi;
+ *
+ *     Jim_GetOptSetup(&goi, interp, argc, argv);
+ * \endcode
+ */
+
+int Jim_GetOpt_Setup(Jim_GetOptInfo *goi,
+                                                                                       Jim_Interp *interp,
+                                                                                       int argc,
+                                                                                       Jim_Obj * const *  argv);
+
+
+/** Debug - Dump parameters to stderr
+ * \param goi - current parameters
+ */
+void Jim_GetOpt_Debug(Jim_GetOptInfo *goi);
+
+
+
+/** Remove argv[0] from the list.
+ *
+ * \param goi - get opt info
+ * \param puthere - where param is put
+ *
+ */
+int Jim_GetOpt_Obj(Jim_GetOptInfo *goi, Jim_Obj **puthere);
+
+/** Remove argv[0] as string.
+ *
+ * \param goi     - get opt info
+ * \param puthere - where param is put
+ * \param len     - return its length
+ */
+int Jim_GetOpt_String(Jim_GetOptInfo *goi, char **puthere, int *len);
+
+/** Remove argv[0] as double.
+ *
+ * \param goi     - get opt info
+ * \param puthere - where param is put.
+ *
+ */
+int Jim_GetOpt_Double(Jim_GetOptInfo *goi, double *puthere);
+
+/** Remove argv[0] as wide.
+ *
+ * \param goi     - get opt info
+ * \param puthere - where param is put.
+ */
+int Jim_GetOpt_Wide(Jim_GetOptInfo *goi, jim_wide *puthere);
+
+/** Remove argv[0] as NVP.
+ *
+ * \param goi     - get opt info
+ * \param lookup  - nvp lookup table
+ * \param puthere - where param is put.
+ *
+ */
+int Jim_GetOpt_Nvp(Jim_GetOptInfo *goi, const Jim_Nvp *lookup, Jim_Nvp **puthere);
+
+/** Create an appropriate error message for an NVP.
+ *
+ * \param goi - options info
+ * \param lookup - the NVP table that was used.
+ * \param hadprefix - 0 or 1 if the option had a prefix.
+ *
+ * This function will set the "interp->result" to a human readable
+ * error message listing the available options.
+ *
+ * This function assumes the previous option argv[-1] is the unknown string.
+ *
+ * If this option had some prefix, then pass "hadprefix = 1" else pass "hadprefix = 0"
+ *
+ * Example:
+ * \code
+ *
+ *  while (goi.argc) {
+ *     // Get the next option
+ *     e = Jim_GetOpt_Nvp(&goi, cmd_options, &n);
+ *     if (e != JIM_OK) {
+ *          // option was not recognized
+ *          // pass 'hadprefix = 0' because there is no prefix
+ *          Jim_GetOpt_NvpUnknown(&goi, cmd_options, 0);
+ *          return e;
+ *     }
+ *
+ *     switch (n->value) {
+ *     case OPT_SEX:
+ *          // handle:  --sex male | female | lots | needmore
+ *          e = Jim_GetOpt_Nvp(&goi, &nvp_sex, &n);
+ *          if (e != JIM_OK) {
+ *               Jim_GetOpt_NvpUnknown(&ogi, nvp_sex, 1);
+ *               return e;
+ *          }
+ *          printf("Code: (%d) is %s\n", n->value, n->name);
+ *          break;
+ *     case ...:
+ *          [snip]
+ *     }
+ * }
+ * \endcode
+ *
+ */
+void Jim_GetOpt_NvpUnknown(Jim_GetOptInfo *goi, const Jim_Nvp *lookup, int hadprefix);
+
+
+/** Remove argv[0] as Enum
+ *
+ * \param goi     - get opt info
+ * \param lookup  - lookup table.
+ * \param puthere - where param is put.
+ *
+ */
+int Jim_GetOpt_Enum(Jim_GetOptInfo *goi, const char * const *  lookup, int *puthere);
+
+#endif