started show command - doesn't work yet
[idzebra-moved-to-github.git] / index / zebrash.c
index 79575d5..ed8aabf 100644 (file)
@@ -1,10 +1,29 @@
-/* zebrash.c - command-line interface to zebra API 
- *  $Id: zebrash.c,v 1.10 2003-06-20 14:21:23 heikki Exp $
- *
- * Copyrigth 2003 Index Data Aps
- *
- */
+/* $Id: zebrash.c,v 1.16 2003-07-03 16:16:22 heikki Exp $
+   Copyright (C) 2002,2003
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra 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, or (at your option) any later
+version.
+
+Zebra 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 Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+/* 
+   zebrash.c - command-line interface to zebra API
+*/
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h> 
 
 #include "zebraapi.h"
 #include <yaz/log.h>
+#include <yaz/proto.h>
 
 #define MAX_NO_ARGS 32
 #define MAX_OUT_BUFF 4096
 #define MAX_ARG_LEN 1024
 #define PROMPT "ZebraSh>"
 #define DEFAULTCONFIG "./zebra.cfg"
+#define DEFAULTRESULTSET "MyResultSet"
 
 /**************************************
  * Global variables (yuck!)
@@ -33,6 +54,7 @@
 ZebraService zs=0;  /* our global handle to zebra */
 ZebraHandle  zh=0;  /* the current session */
 /* time being, only one session works */
+int nextrecno=0;  /* record number to show next */
 
 /**************************************
  * Help functions
@@ -164,10 +186,12 @@ static int cmd_quickstart( char *args[], char *outbuff)
     if (!rc)
         rc=onecommand(tmp,outbuff,"");
     logf(LOG_APP,"quickstart");
-    if (!rc)
-        rc=onecommand("zebra_start",outbuff,"");
-    if (!rc)
-        rc=onecommand("zebra_open",outbuff,"");
+    if (!zs)
+        if (!rc)
+            rc=onecommand("zebra_start",outbuff,"");
+    if (!zh)
+        if (!rc)
+            rc=onecommand("zebra_open",outbuff,"");
     if (!rc)
         rc=onecommand("select_database Default",outbuff,"");
     return rc;
@@ -286,7 +310,6 @@ static int cmd_select_database ( char *args[], char *outbuff)
 static int cmd_create_database( char *args[], char *outbuff)
 {
     char *db=args[1];
-       int rc;
     if (!db)
         db="Default";
     strcat(outbuff,"Creating database ");
@@ -295,7 +318,168 @@ static int cmd_create_database( char *args[], char *outbuff)
        
     return zebra_create_database(zh, db);
 }
-/**************************************
+
+static int cmd_drop_database( char *args[], char *outbuff)
+{
+    char *db=args[1];
+    if (!db)
+        db="Default";
+    strcat(outbuff,"Dropping database ");
+    strcat(outbuff,db);
+    strcat(outbuff,"\n");
+       
+    return zebra_drop_database(zh, db);
+}
+
+static int cmd_begin_trans( char *args[], char *outbuff)
+{
+    int rw=0;
+    if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
+        rw=1;
+    return zebra_begin_trans(zh,rw);
+}
+static int cmd_end_trans( char *args[], char *outbuff)
+{
+    return zebra_end_trans(zh);
+}
+/*************************************
+ * Inserting and deleting
+ */
+
+static int cmd_record_insert( char *args[], char *outbuff)
+{
+    int sysno=0;
+    char buf[MAX_ARG_LEN];
+    int i;
+    int rc;
+    
+    i=1;
+    buf[0]='\0';
+    while (args[i])
+    {
+       strcat(buf, args[i++]);
+       strcat(buf, " ");
+    }
+    rc=zebra_record_insert(zh,buf, strlen(buf), &sysno);
+    if (0==rc)
+    {
+        sprintf(buf,"ok sysno=%d\n",sysno);
+       strcat(outbuff,buf);
+    }
+    return rc;
+}
+
+
+static int cmd_exchange_record( char *args[], char *outbuff)
+{
+    char *base=args[1];
+    char *id = args[2];
+    char *action = args[3];
+    int i=4;
+    int rc;
+    char buf[MAX_ARG_LEN];
+    if (!(base && id && action && args[4] ))
+    {
+       strcat(outbuff,"Missing arguments!\n");
+       onecommand("help exchange_record", outbuff, "");
+       return -90;
+    }
+    while (args[i])
+    {
+       strcat(buf, args[i++]);
+       strcat(buf, " ");
+    }
+    rc=zebra_admin_exchange_record(zh, base, buf, strlen(buf),
+        id, strlen(id), atoi(action));
+    return rc;
+}
+
+/**********************************
+ * Searching and retrieving
+ */
+
+static int cmd_search_pqf( char *args[], char *outbuff)
+{
+    int hits=0;
+    char *set=args[1];
+    char qry[MAX_ARG_LEN]="";
+    int i=2;
+    int rc;
+    while (args[i])
+    {
+           strcat(qry, args[i++]);
+           strcat(qry, " ");
+    }
+    rc=zebra_search_PQF(zh, qry, set, &hits);
+    if (0==rc)
+    {
+        sprintf(qry,"%d hits found\n",hits);
+        strcat(outbuff,qry);
+    }
+    return rc;
+}
+
+static int cmd_find( char *args[], char *outbuff)
+{
+    char *setname=DEFAULTRESULTSET;
+    char qry[MAX_ARG_LEN]="";
+    int i=1;
+    int rc;
+    int hits=0;
+    if (0==strstr(args[0],"@attr"))
+        strcat(qry, "@attr 1=/ ");
+    while (args[i])
+    {
+           strcat(qry, args[i++]);
+           strcat(qry, " ");
+    }
+    if (!zh)
+           onecommand("quickstart", outbuff, "");
+    strcat(outbuff, "find ");
+    strcat(outbuff, qry);
+    strcat(outbuff, "\n");
+    rc=zebra_search_PQF(zh, qry, setname, &hits);
+    if (0==rc)
+    {
+        sprintf(qry,"%d hits found\n",hits);
+        strcat(outbuff,qry);
+        nextrecno=0;
+    }
+    return rc;
+}
+
+static int cmd_show( char *args[], char *outbuff)
+{
+    int start=defargint(args[1], nextrecno);
+    int nrecs=defargint(args[2],1);
+    char *setname=defarg(args[3],DEFAULTRESULTSET);
+    int rc=0;
+    ODR odr;
+    Z_RecordComposition *pcomp=0;
+
+    oid_value format;
+    ZebraRetrievalRecord recs;
+    odr=odr_createmem(ODR_ENCODE);
+    rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
+    printf("rc1=%d\n",rc);
+    format=oid_getvalbyname ("xml"); /*FIXME*/
+
+    rc = zebra_records_retrieve (zh, odr, setname,
+            pcomp, format, nrecs, &recs);
+                    
+                    /*
+                    ODR stream,
+                    const char *setname, 
+                    Z_RecordComposition *comp,
+                    oid_value input_format,
+                    int num_recs, 
+                    ZebraRetrievalRecord *recs);
+                    */
+
+    nextrecno=start+1;
+    return rc;
+}
+/**************************************)
  * Command table, parser, and help 
  */
 
@@ -373,12 +557,49 @@ struct cmdstruct cmds[] = {
       "Selects a database",
       cmd_select_database},    
     { "create_database", "basename",
-      "Creates a database",
+      "Create database",
       cmd_create_database},
+    { "drop_database", "basename",
+      "Drop database",
+      cmd_drop_database},
+    { "begin_trans", "[rw]",
+      "Begins a transaction. rw=1 means write, otherwise read-only",
+      cmd_begin_trans},
+    { "end_trans","",
+      "Ends a transaction",
+      cmd_end_trans},
+
+    { "","Updating:","",0},
+    { "record_insert","record",
+      "inserts an sgml record into Default",
+      cmd_record_insert},
+    { "exchange_record","database record-id action record",
+      "inserts (1), updates (2), or deletes (3) a record \n"
+      "record-id must be a unique identifier for the record",
+      cmd_exchange_record},
+    { "","Searching and retrieving:","",0},
+    { "search_pqf","setname query",
+      "search ",
+      cmd_search_pqf},
+    { "find","query",
+      "simplified search",
+      cmd_find},
+    { "f","query",
+      "simplified search",
+      cmd_find},
+    { "show","[start] [numrecs] [resultset]",
+      "shows a result",
+      cmd_show},
+    { "s","[start] [numrecs] [resultset]",
+      "shows a result",
+      cmd_show},
     { "", "Misc:","", 0}, 
     { "echo", "string", 
       "ouputs the string", 
       cmd_echo },
+    { "q", "", 
+      "exits the program", 
+      cmd_quit },
     { "quit", "", 
       "exits the program", 
       cmd_quit },
@@ -397,12 +618,13 @@ int onecommand(
 {
     int i;
     char *args[MAX_NO_ARGS];
-    int n;
+    int nargs;
     char argbuf[MAX_ARG_LEN];
     logf(LOG_APP,"%s",line);
     strncpy(argbuf,line, MAX_ARG_LEN-1);
     argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
-    n=split_args(argbuf, args);
+    memset(args,'\0',MAX_NO_ARGS*sizeof(char *));
+    nargs=split_args(argbuf, args);
 
 #if 0
     for (i = 0; i <= n; i++)
@@ -411,20 +633,20 @@ int onecommand(
        printf ("args %d :%s:\n", i, cp ? cp : "<null>");
     }
 #endif
-    if (0==n)
+    if (0==nargs)
        return -90; /* no command on line, too bad */
 
     if (0==strcmp(args[0],"expect")) 
     {
        char *rest;
-        if (n>1)
+        if (nargs>1)
             rest= line + (args[1]-argbuf); /* rest of the line */
         else
             return -1; /* need something to expect */
        printf("expecting '%s'\n",rest); /*!*/
        if (0==strstr(prevout,rest))
        {
-           printf( "Failed expectation, '%s' not found\n");
+           printf( "Failed expectation, '%s' not found\n", rest);
             exit(9); 
        }
        return 0;
@@ -432,7 +654,7 @@ int onecommand(
     for (i=0;cmds[i].cmd;i++)
        if (0==strcmp(cmds[i].cmd, args[0])) 
        {
-           if (n>1)
+           if (nargs>1)
                args[0]= line + (args[1]-argbuf); /* rest of the line */
            else
                args[0]="";