Author: bdonlan
Date: 2004-12-31 21:33:30 -0500 (Fri, 31 Dec 2004)
New Revision: 496

Added:
   trunk/clients/havercurs/cmdline.c
Modified:
   trunk/
   trunk/clients/havercurs/Makefile.am
Log:
 [EMAIL PROTECTED]:  bdonlan | 2005-01-01T02:33:21.022703Z
 Add cmdline.c, with the beginnings of a command lookup trie structure



Property changes on: trunk
___________________________________________________________________
Name: svk:merge
   - 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local/trunk:10259
27e50396-46e3-0310-8b22-ae223a1f35ce:/local:212
edfcd8bd-4ce7-0310-a97e-bb1efd40edf3:/local:238
   + 1f59643a-e6e5-0310-bc24-f7d4c744f460:/haver/local/trunk:10261
27e50396-46e3-0310-8b22-ae223a1f35ce:/local:212
edfcd8bd-4ce7-0310-a97e-bb1efd40edf3:/local:238

Modified: trunk/clients/havercurs/Makefile.am
===================================================================
--- trunk/clients/havercurs/Makefile.am 2005-01-01 02:18:11 UTC (rev 495)
+++ trunk/clients/havercurs/Makefile.am 2005-01-01 02:33:30 UTC (rev 496)
@@ -9,4 +9,5 @@
                                        mymalloc.c mymalloc.h \
                                        net.c net.h \
                                        lineio.c lineio.h \
-                                       hashtable.c hashtable.h
+                                       hashtable.c hashtable.h \
+                                       cmdline.c cmdline.h

Added: trunk/clients/havercurs/cmdline.c
===================================================================
--- trunk/clients/havercurs/cmdline.c   2005-01-01 02:18:11 UTC (rev 495)
+++ trunk/clients/havercurs/cmdline.c   2005-01-01 02:33:30 UTC (rev 496)
@@ -0,0 +1,195 @@
+/* vim: set ts=4 sw=4 expandtab si ai sta tw=104:
+ * cmdline.h - Functions for processing and dispatching commands
+ * 
+ * Copyright (C) 2004 Bryan Donlan
+ * 
+ * This module is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This module is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this module; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "cmdline.h"
+#include "mymalloc.h"
+
+/* These are the only characters recognized in a command.
+ * TODO: report illegal characters to higher level code
+ */
+static const char trie_chars[] =
+    "[EMAIL PROTECTED]&*()_+\\|/.,<>";
+
+#define TRIE_CHARS (sizeof trie_chars - 1)
+    
+typedef struct trie {
+    struct trie *ptrs[TRIE_CHARS];
+    cmdline_handler *handler;
+    void *baton;
+} trie;
+
+static trie *root = NULL;
+
+/** trie_trans_char
+ *
+ * Find the index of a character in trie_chars[]
+ *
+ * Arguments:
+ * char c - chatacter to translate
+ *
+ * Return value:
+ * Index of character, or -1 if not found
+ */
+static inline int trie_find_char(char c) {
+    char *p = strchr(trie_chars, c);
+    if (!p)
+        return -1;
+    return p - trie_chars;
+}
+
+/** trie_find_level()
+ *
+ * Locate the node in the trie corresponding to the specified prefix.
+ *
+ * Arguments:
+ * const char *prefix - prefix to locate
+ * int create - nonzero if the node and its parents should be created if
+ *              nonexistent
+ *
+ * Return value:
+ * Pointer to trie node if found
+ * NULL if not found and create is 0
+ * NULL if illegal characters detected
+ */
+static trie *trie_find_level(
+        const char *prefix,
+        int create
+        )
+{
+    trie *cur = root;
+    while (*prefix && cur) {
+        trie *next;
+        int idx = trie_find_char(*prefix);
+        if (idx == -1)
+            return NULL;
+        next = cur->ptrs[idx];
+        if (!next) {
+            int i;
+            if (!create)
+                return NULL;
+            next = mymalloc(sizeof *next);
+            next->handler = NULL;
+            next->baton = NULL;
+            for (i = 0; i < TRIE_CHARS; i++)
+                next->ptrs[i] = NULL;
+            cur->ptrs[idx] = next;
+        }
+        cur = next;
+    }
+    return cur;
+}
+
+/** cmdline_init()
+ *
+ * Initializes the module. Must be called before any other functions in this 
header.
+ */
+void cmdline_init(void);
+
+/** cmdline_free()
+ *
+ * De-initializes the module. cmdline_init() must be called again after 
calling this,
+ * before any other functions from this header are called
+ */
+void cmdline_free(void);
+
+/** cmdline_register()
+ *
+ * Registers a command handler.
+ *
+ * Arguments:
+ * const char *command - name of the command
+ * cmdline_handler *cb - callback to call when command is invoked
+ * void *baton - value to pass to the callback's baton argument
+ *
+ * Return value:
+ * Previous handler or NULL if none
+ */
+cmdline_handler *cmdline_register(
+               const char *command,
+               cmdline_handler *cb,
+               void *baton
+               );
+
+/** cmdline_unregister()
+ *
+ * Unregisters a command handler
+ *
+ * Arguments:
+ * const char *command - name of the command
+ *
+ * Return value:
+ * Previous handler or NULL if none
+ */
+cmdline_handler *cmdline_unregister(const char *command);
+
+/** cmdline_lookup()
+ *
+ * Obtains the handler for a given command
+ *
+ * Arguments:
+ * const char *command - name of the command
+ * void **baton - pointer to location to place the baton value, or NULL
+ *                               to discard
+ * int partial  - 1 if partial matching should be attempted
+ *
+ * Return value:
+ * Handler callback, or NULL if none
+ */
+cmdline_handler *cmdline_lookup(
+               const char *command,
+               void **baton,
+        int partial
+               );
+
+/** cmdline_process()
+ *
+ * Dispatches a command to its handler, if any.
+ *
+ * Arguments:
+ * const char *cmdline - full command line to process
+ * int particl         - 1 if partial matching should be attempted
+ *
+ * Return value:
+ * 2 if command was processed with a partial match
+ * 1 if command was successfully processed
+ * 0 if no handler was found
+ * -1 if multiple partial matches were found; no action will be taken
+ */
+int cmdline_process(const char *cmdline);
+
+/** cmdline_enum()
+ *
+ * Obtain a complete listing of partial matches for a command prefix
+ *
+ * Arguments:
+ * const char *prefix - the prefix to search for
+ * cmdline_enum_callback *cb - callback to call on each match
+ * void *baton - baton to pass to callback
+ *
+ * Return value:
+ * Total number of matches (regardless of whether cb() returns 0)
+ */
+size_t cmdline_enum(
+        const char *prefix,
+        cmdline_enum_callback *cb,
+        void *baton
+        );


Property changes on: trunk/clients/havercurs/cmdline.c
___________________________________________________________________
Name: svn:eol-style
   + native


Reply via email to