Started on the zebra shell - api-level debugging tool
authorHeikki Levanto <heikki@indexdata.dk>
Thu, 6 Feb 2003 16:58:56 +0000 (16:58 +0000)
committerHeikki Levanto <heikki@indexdata.dk>
Thu, 6 Feb 2003 16:58:56 +0000 (16:58 +0000)
configure.in
index/Makefile.am
index/zebrash.c [new file with mode: 0644]

index 3da6705..1acf321 100644 (file)
@@ -1,11 +1,12 @@
 dnl Zebra, Index Data Aps, 1995-2002
-dnl $Id: configure.in,v 1.62 2003-02-04 12:06:46 pop Exp $
+dnl $Id: configure.in,v 1.63 2003-02-06 16:58:56 heikki Exp $
 dnl
 AC_INIT(include/zebraver.h)
 AM_INIT_AUTOMAKE(idzebra,1.3.4)
 dnl ------ Substitutions
 AC_SUBST(TCL_INCLUDE)
 AC_SUBST(TCL_LIB)
+AC_SUBST(READLINE_LIBS)
 dnl ------ Perl substitutions
 AC_SUBST(PERL_BINARY)
 AC_SUBST(PERL_XS_INIT)
@@ -128,6 +129,39 @@ dnl
 dnl ------ mkstemp
 AC_CHECK_FUNCS(mkstemp)
 dnl
+dnl ------ GNU Readline
+READLINE_SHARED_LIBADD=""
+AC_CHECK_LIB(ncurses, tgetent, [READLINE_SHARED_LIBADD="-lncurses"],
+        AC_CHECK_LIB(termcap, tgetent, [READLINE_SHARED_LIBADD="-ltermcap"])
+)
+READLINE_LIBS=""
+AC_CHECK_LIB(readline, readline, [READLINE_LIBS="$READLINE_LIBS -lreadline $READLINE_SHARED_LIBADD"],,$READLINE_SHARED_LIBADD)
+AC_CHECK_LIB(history, add_history, [READLINE_LIBS="$READLINE_LIBS -lhistory"])
+if test "$ac_cv_lib_readline_readline" = "yes"; then
+        AC_CHECK_HEADERS(readline/readline.h readline/history.h)
+        xLIBS=$LIBS
+        LIBS="$LIBS $READLINE_LIBS"
+        AC_TRY_LINK([
+        #include <stdio.h>
+        #include <readline/readline.h>
+        ],[
+        static void f()
+        {
+                rl_attempted_completion_over = 0;
+        }
+        ],AC_DEFINE(HAVE_READLINE_COMPLETION_OVER))
+        AC_TRY_LINK([
+        #include <stdio.h>
+        #include <readline/readline.h>
+        ],[
+        static void f()
+        {
+                rl_completion_matches (0, 0);
+        }
+        ],AC_DEFINE(HAVE_READLINE_RL_COMPLETION_MATCHES))
+        LIBS=$xLIBS
+fi
+dnl
 dnl ------ iconv
 AC_ARG_WITH(iconv, [  --with-iconv[=DIR]      iconv library in DIR])
 if test "$with_iconv" != "no"; then
index 1f3f2b8..57d46b3 100644 (file)
@@ -1,4 +1,4 @@
-## $Id: Makefile.am,v 1.18 2003-02-04 12:06:47 pop Exp $
+## $Id: Makefile.am,v 1.19 2003-02-06 16:58:57 heikki Exp $
 
 noinst_PROGRAMS = apitest kdump
 
@@ -17,15 +17,18 @@ libzebra_a_LIBADD = \
  ../recctrl/librecctrl.a ../data1/libdata1.a ../bfile/libbfile.a \
  ../dfa/libdfa.a ../util/libutil.a
 
-bin_PROGRAMS = zebraidx zebrasrv
+bin_PROGRAMS = zebraidx zebrasrv zebrash
 
 zebraidx_SOURCES = main.c
 zebrasrv_SOURCES = zserver.c
+zebrash_SOURCES = zebrash.c
 apitest_SOURCES = apitest.c
 kdump_SOURCES=kdump.c kcompare.c
 
 AM_CPPFLAGS = -I$(srcdir)/../include $(YAZINC) $(TCL_INCLUDE) -DDEFAULT_PROFILE_PATH=\"$(pkgdatadir)/tab\"
 
+zebrash_LDADD= libzebra.a $(YAZLIB) $(READLINE_LIBS)
+
 LDADD = libzebra.a $(YAZLIB) $(TCL_LIB)
 
 libzebra.a: $(libzebra_a_OBJECTS) $(libzebra_a_DEPENDENCIES)
diff --git a/index/zebrash.c b/index/zebrash.c
new file mode 100644 (file)
index 0000000..09afede
--- /dev/null
@@ -0,0 +1,241 @@
+/* zebrash.c - command-line interface to zebra API 
+ *  $ID$
+ *
+ * Copyrigth 2003 Index Data Aps
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#if HAVE_READLINE_READLINE_H
+#include <readline/readline.h> 
+#endif
+#if HAVE_READLINE_HISTORY_H
+#include <readline/history.h>
+#endif
+
+#include "zebraapi.h"
+
+#define MAX_NO_ARGS 32
+#define MAX_OUT_BUFF 4096
+#define MAX_ARG_LEN 1024
+#define PROMPT "ZebraSh>"
+#define DEFAULTCONFIG "./zebra.cfg"
+
+/**************************************
+ * Global variables (yuck!)
+ */
+
+ZebraService zs=0;  /* our global handle to zebra */
+ZebraHandle  zh=0;  /* the current session */
+                  /* time being, only one session works */
+
+/**************************************
+ * Help functions
+ */
+
+static int split_args( char *line, char** argv )
+{ /* splits line into individual null-terminated strings, 
+   * returns pointers to them in argv */
+  char *p=line;
+  int i=0;
+  argv[0]=0; /* by default */
+  while (*p==' ' || *p=='\t' || *p=='\n')
+    p++;
+  while (*p)
+  {
+    while (*p==' ' || *p=='\t' || *p=='\n')
+      p++;
+    argv[i++]=p;
+    argv[i]=0;
+    while (*p && *p!=' ' && *p!='\t' && *p!='\n')
+      p++;
+    *p++='\0';
+  }
+  return i;
+}
+
+ /**************************************
+ * Individual commands
+ */
+
+ int cmd_echo( char *args[], char *outbuff)
+ {
+   strcpy(outbuff, args[0]);
+   return 0;
+ }
+ int cmd_quit( char *args[], char *outbuff)
+ {
+   strcpy(outbuff, "bye");
+   return -99; /* special stop signal */
+ }
+ int cmd_help( char *args[], char *outbuff); 
+ int cmd_zebra_start( char *args[], char *outbuff)
+ {
+   char *conf=args[1];
+   if (!conf || !*conf) {
+     strcat(outbuff,"no config file specified, using "
+                     DEFAULTCONFIG "\n" );
+     conf=DEFAULTCONFIG;
+   }
+   zs=zebra_start(conf);
+   if (!zs) {
+     strcpy(outbuff, "zebra_open failed" );
+     return 2;
+   }
+   return 0; /* ok */
+ }
+ int cmd_zebra_stop( char *args[], char *outbuff)
+ {
+   if (!zs)
+     strcat(outbuff,"zebra seems not to have been started, "
+                    "stopping anyway");
+   zebra_stop(zs);
+   zs=0;
+   return 0; /* ok */
+ }
+
+int cmd_zebra_open( char *args[], char *outbuff)
+{
+  if (!zs)
+    strcat(outbuff,"zebra seems not to have been started, "
+                   "trying anyway");
+  zh=zebra_open(zs);
+  return 0; /* ok */
+}
+
+int cmd_zebra_close( char *args[], char *outbuff)
+{
+  if (!zh)
+    strcat(outbuff,"Seems like you have not called zebra_open,"
+                   "trying anyway");
+  zebra_close(zh);
+  return 0; /* ok */
+}
+/**************************************
+ * Command table, parser, and help 
+ */
+
+ struct cmdstruct
+ {
+   char * cmd;
+   char * args;
+   char * explanation;
+   int (*testfunc)(char *args[], char *outbuff);
+ } ;
+
+ struct cmdstruct cmds[] = {
+   { "zebra_start", "[configfile]", 
+        "starts the zebra service", cmd_zebra_start },
+   { "zebra_stop", "", 
+        "stops the zebra service", cmd_zebra_stop },
+   { "zebra_open", "", 
+        "starts a zebra session", cmd_zebra_open },
+   { "zebra_close", "", 
+        "closes a zebra session", cmd_zebra_close },
+   { "echo", "string", "ouputs the string", cmd_echo },
+   { "quit", "", "exits the program", cmd_quit },
+   { "help", "", 0, cmd_help },
+   
+   {0,0,0,0} /* end marker */
+ };
+ int onecommand( char *line, char *outbuff)
+ {
+   int i;
+   char *argv[MAX_NO_ARGS];
+   int n;
+   char argbuf[MAX_ARG_LEN];
+   strncpy(argbuf,line, MAX_ARG_LEN-1);
+   argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
+   n=split_args(argbuf, argv);
+   if (0==n)
+     return 0; /* no command on line, too bad */
+   for (i=0;cmds[i].cmd;i++)
+     if (0==strcmp(cmds[i].cmd, argv[0])) 
+     {
+       if (n>1)
+         argv[0]= line + (argv[1]-argbuf); /* rest of the line */
+       else
+         argv[0]=""; 
+       return ((cmds[i].testfunc)(argv,outbuff));
+     }
+   sprintf (outbuff, "Unknown command '%s'. Try help",argv[0] );
+   return -1; 
+ }
+ int cmd_help( char *args[], char *outbuff)
+ { 
+   int i;
+   char tmp[MAX_ARG_LEN];
+   for (i=0;cmds[i].cmd;i++)
+     if (cmds[i].explanation)
+     {
+       sprintf(tmp, "%s %s %s\n",
+         cmds[i].cmd, cmds[i].args, cmds[i].explanation);
+       strcat(outbuff,tmp);
+     }
+    return 0;
+ }
+/************************************** 
+ * The shell
+ */
+void shell()
+{
+  int rc=0;
+  while (rc!=-99)
+  {
+    char buf[MAX_ARG_LEN];
+    char outbuff[MAX_OUT_BUFF];
+#if HAVE_READLINE_READLINE_H
+    char* line_in;
+         line_in=readline(PROMPT);
+         if (!line_in)
+           break;
+#if HAVE_READLINE_HISTORY_H
+         if (*line_in)
+           add_history(line_in);
+#endif
+         if(strlen(line_in) > MAX_ARG_LEN-1) {
+           fprintf(stderr,"Input line too long\n");
+           break;
+         };
+         strcpy(buf,line_in);
+         free (line_in);
+#else    
+         printf (PROMPT); 
+         fflush (stdout);
+         if (!fgets (buf, MAX_ARG_LEN-1, stdin))
+           break;
+#endif 
+    outbuff[0]='\0';
+    rc=onecommand(buf, outbuff);
+       printf("%s\n", outbuff);
+  }
+
+ }
+/**************************************
+ * Main 
+ */
+int main (int argc, char ** argv)
+{
+  shell();
+  return 0;
+} /* main */