Implemented drop database
[idzebra-moved-to-github.git] / index / zebrash.c
index 760e2d5..b4ee5d9 100644 (file)
@@ -1,10 +1,29 @@
-/* zebrash.c - command-line interface to zebra API 
- *  $Id: zebrash.c,v 1.9 2003-06-02 13:33:16 adam Exp $
- *
- * Copyrigth 2003 Index Data Aps
- *
- */
+/* $Id: zebrash.c,v 1.14 2003-06-30 19:37:12 adam 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> 
@@ -84,7 +103,7 @@ static int defargint( char *arg, int def )
     return v;
 }
 
-int onecommand( char *line, char *outbuff);
+int onecommand( char *line, char *outbuff, const char *prevout); 
  
 /**************************************
  * Simple support commands
@@ -155,16 +174,22 @@ static int cmd_zebra_close( char *args[], char *outbuff)
 static int cmd_quickstart( char *args[], char *outbuff)
 {
     char tmp[128];
-    onecommand("yaz_log_file zebrash.log",outbuff);
-    onecommand("yaz_log_prefix ZebraSh", outbuff);
+    int rc=0;
+    if (!rc)
+        rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
+    if (!rc)
+        rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
     sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
-    onecommand(tmp,outbuff);
+    if (!rc)
+        rc=onecommand(tmp,outbuff,"");
     logf(LOG_APP,"quickstart");
-    onecommand("zebra_start",outbuff);
-    onecommand("zebra_open",outbuff);
-    onecommand("select_database Default",outbuff);
-    strcat(outbuff,"ok\n");
-    return 0;
+    if (!rc)
+        rc=onecommand("zebra_start",outbuff,"");
+    if (!rc)
+        rc=onecommand("zebra_open",outbuff,"");
+    if (!rc)
+        rc=onecommand("select_database Default",outbuff,"");
+    return rc;
 }
 
 /**************************************
@@ -270,12 +295,102 @@ static int cmd_init ( char *args[], char *outbuff)
 static int cmd_select_database ( char *args[], char *outbuff)
 {
     char *db=args[1];
-    if (!db)
-       db="Default";
+    if (!db) {
+        db="Default";
+       strcat(outbuff,"Selecting database 'Default'\n");
+    }
     return zebra_select_database(zh, db);
 }
  
-/**************************************
+static int cmd_create_database( char *args[], char *outbuff)
+{
+    char *db=args[1];
+    if (!db)
+        db="Default";
+    strcat(outbuff,"Creating database ");
+    strcat(outbuff,db);
+    strcat(outbuff,"\n");
+       
+    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;
+}
+
+
+/**************************************)
  * Command table, parser, and help 
  */
 
@@ -352,7 +467,27 @@ struct cmdstruct cmds[] = {
     { "select_database",  "basename",
       "Selects a database",
       cmd_select_database},    
-  
+    { "create_database", "basename",
+      "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},
     { "", "Misc:","", 0}, 
     { "echo", "string", 
       "ouputs the string", 
@@ -368,7 +503,10 @@ struct cmdstruct cmds[] = {
     {0,0,0,0} /* end marker */
 };
  
-int onecommand( char *line, char *outbuff)
+int onecommand( 
+               char *line,     /* input line */
+               char *outbuff,  /* output goes here */
+               const char *prevout) /* prev output, for 'expect' */
 {
     int i;
     char *args[MAX_NO_ARGS];
@@ -387,7 +525,23 @@ int onecommand( char *line, char *outbuff)
     }
 #endif
     if (0==n)
-       return -1; /* no command on line, too bad */
+       return -90; /* no command on line, too bad */
+
+    if (0==strcmp(args[0],"expect")) 
+    {
+       char *rest;
+        if (n>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", rest);
+            exit(9); 
+       }
+       return 0;
+    }
     for (i=0;cmds[i].cmd;i++)
        if (0==strcmp(cmds[i].cmd, args[0])) 
        {
@@ -401,7 +555,7 @@ int onecommand( char *line, char *outbuff)
     strcat(outbuff,args[0] );
     strcat(outbuff,"'. Try help");
     logf(LOG_APP,"Unknown command");
-    return -2; 
+    return -90; 
 }
  
 static int cmd_help( char *args[], char *outbuff)
@@ -469,12 +623,13 @@ static void Zerrors ( char *outbuff)
     ec=zebra_errCode (zh);
     if (ec)
     {
-       sprintf(tmp, "Zebra error %d: %s, (%s)",
+       sprintf(tmp, "   Zebra error %d: %s, (%s)",
                ec, zebra_errString (zh),
                zebra_errAdd (zh) );
        strcat(outbuff, tmp);
        strcat(outbuff, "\n");
        logf(LOG_APP, tmp);
+       zebra_clearError(zh);
     }
 }
   
@@ -486,10 +641,11 @@ void shell()
 {
     int rc=0;
     char tmp[MAX_ARG_LEN];
+    char outbuff[MAX_OUT_BUFF]="";
+    char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
     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);
@@ -511,16 +667,18 @@ void shell()
        if (!fgets (buf, MAX_ARG_LEN-1, stdin))
            break; 
 #endif 
+       
+       strncpy(prevout, outbuff, MAX_OUT_BUFF);
        outbuff[0]='\0';
-       rc=onecommand(buf, outbuff);
+       rc=onecommand(buf, outbuff, prevout);
        if (rc==0)
        {
-           strcat(outbuff, "OK\n");
+           strcat(outbuff, "   OK\n");
            logf(LOG_APP, "OK");
        }
-       else if (rc > 0)
+       else if (rc>-90)
        {
-           sprintf(tmp, "command returned %d\n",rc);
+           sprintf(tmp, "   command returned %d\n",rc);
            strcat(outbuff,tmp);
        } 
        Zerrors(outbuff);