1 /* $Id: zebrash.c,v 1.33 2005-03-09 12:14:42 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 zebrash.c - command-line interface to zebra API
31 #include <unistd.h> /* for isatty */
33 #if HAVE_READLINE_READLINE_H
34 #include <readline/readline.h>
36 #if HAVE_READLINE_HISTORY_H
37 #include <readline/history.h>
40 #include <idzebra/api.h>
42 #include <yaz/proto.h>
43 #include <yaz/sortspec.h>
44 #include <yaz/options.h>
45 #include <yaz/wrbuf.h>
47 #define MAX_NO_ARGS 32
48 #define MAX_OUT_BUFF 4096
49 #define MAX_ARG_LEN 1024
50 #define PROMPT "ZebraSh>"
51 #define DEFAULTCONFIG "./zebra.cfg"
52 #define DEFAULTDATABASE "Default"
53 #define DEFAULTRESULTSET "MyResultSet"
56 /**************************************
57 * Global variables (yuck!)
60 ZebraService zs=0; /* our global handle to zebra */
61 ZebraHandle zh=0; /* the current session */
62 /* time being, only one session works */
63 int nextrecno=1; /* record number to show next */
64 static char *default_config = DEFAULTCONFIG;
65 static int log_level=0;
68 /**************************************
73 static int split_args( char *line, char** args )
74 { /* splits line into individual null-terminated strings,
75 * returns pointers to them in args */
76 /* FIXME - do we need to handle quoted args ?? */
80 args[0]=0; /* by default */
81 while (*p==' ' || *p=='\t' || *p=='\n')
85 while (*p==' ' || *p=='\t' || *p=='\n')
87 if (*p=='#') /* skip comments */
91 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
101 static char *defarg( char *arg, char *def )
109 static int defargint( char *arg, int def )
112 char *a=defarg(arg,0);
118 static char *restargs( char *args[], int n)
119 { /* Returns the rest of the arguments, starting at the nth, */
120 /* to the end of the command line. Assumes args[0] contains */
121 /* the original line, minus the command itself */
122 int skiplen= args[n]-args[1];
123 if (skiplen > strlen(args[0]))
125 return args[0]+skiplen;
128 int onecommand( char *line, WRBUF outbuff, const char *prevout);
130 /**************************************
131 * Simple support commands
134 int cmd_echo( char *args[], WRBUF outbuff)
136 wrbuf_printf(outbuff,"%s\n",restargs(args,1));
140 int cmd_quit( char *args[], WRBUF outbuff)
144 onecommand("zebra_close",outbuff,"");
149 onecommand("zebra_stop",outbuff,"");
152 wrbuf_puts(outbuff, "bye");
153 return -99; /* special stop signal */
156 /**************************************
157 * Tests for starting and stopping zebra, etc
160 static int cmd_help( char *args[], WRBUF outbuff);
162 static int cmd_zebra_start( char *args[], WRBUF outbuff)
165 if (!conf || !*conf) {
166 wrbuf_puts(outbuff,"no config file specified, using ");
167 wrbuf_puts(outbuff, default_config);
168 wrbuf_puts(outbuff, "\n");
171 zs=zebra_start(conf);
173 wrbuf_puts(outbuff, "zebra_start failed" );
179 static int cmd_zebra_stop( char *args[], WRBUF outbuff)
182 wrbuf_puts(outbuff,"zebra seems not to have been started, "
183 "stopping anyway\n");
189 static int cmd_zebra_open( char *args[], WRBUF outbuff)
192 wrbuf_puts(outbuff,"zebra seems not to have been started, "
198 static int cmd_zebra_close( char *args[], WRBUF outbuff)
201 wrbuf_puts(outbuff,"Seems like you have not called zebra_open,"
207 static int cmd_quickstart( char *args[], WRBUF outbuff)
212 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
214 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
215 sprintf(tmp, "yaz_log_level 0x%x", YLOG_DEFAULT_LEVEL | log_level );
217 rc=onecommand(tmp,outbuff,"");
218 yaz_log(log_level,"quickstart");
221 rc=onecommand("zebra_start",outbuff,"");
224 rc=onecommand("zebra_open",outbuff,"");
226 rc=onecommand("select_database Default",outbuff,"");
230 /**************************************
234 static int cmd_yaz_log_file( char *args[], WRBUF outbuff)
236 char *fn = defarg(args[1],0);
237 wrbuf_printf(outbuff, "sending yaz-log to %s\n",fn);
238 yaz_log_init_file(fn);
242 static int cmd_yaz_log_level( char *args[], WRBUF outbuff)
244 int lev = defargint(args[1],YLOG_DEFAULT_LEVEL);
245 wrbuf_printf(outbuff, "setting yaz-log to level %d (ox%x)\n",lev,lev);
246 yaz_log_init_level(lev);
250 static int cmd_yaz_log_prefix( char *args[], WRBUF outbuff)
252 char *pref = defarg(args[1],"ZebraSh");
253 wrbuf_printf(outbuff, "setting yaz-log prefix to %s\n",pref);
254 yaz_log_init_prefix(pref);
258 static int cmd_logf( char *args[], WRBUF outbuff)
260 int lev = defargint(args[1],0);
265 lev=YLOG_LOG; /* this is in the default set!*/
266 yaz_log( lev, restargs(args,i));
273 static int cmd_err ( char *args[], WRBUF outbuff)
275 wrbuf_printf(outbuff, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
277 zebra_errString (zh),
281 static int cmd_errcode ( char *args[], WRBUF outbuff)
283 wrbuf_printf(outbuff, "errCode: %d \n",
287 static int cmd_errstr ( char *args[], WRBUF outbuff)
289 wrbuf_printf(outbuff, "errStr: %s\n",
290 zebra_errString (zh));
293 static int cmd_erradd ( char *args[], WRBUF outbuff)
295 wrbuf_printf(outbuff, "errAdd: %s \n",
300 /**************************************
304 static int cmd_init ( char *args[], WRBUF outbuff)
310 static int cmd_select_database ( char *args[], WRBUF outbuff)
312 char *db=defarg(args[1],DEFAULTDATABASE);
313 wrbuf_printf(outbuff,"Selecting database '%s'\n",db);
314 return zebra_select_database(zh, db);
317 static int cmd_create_database( char *args[], WRBUF outbuff)
319 char *db=defarg(args[1],DEFAULTDATABASE);
320 wrbuf_printf(outbuff,"Creating database '%s'\n",db);
322 return zebra_create_database(zh, db);
325 static int cmd_drop_database( char *args[], WRBUF outbuff)
330 wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
331 return zebra_drop_database(zh, db);
334 static int cmd_begin_trans( char *args[], WRBUF outbuff)
337 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
339 return zebra_begin_trans(zh,rw);
342 static int cmd_end_trans( char *args[], WRBUF outbuff)
344 return zebra_end_trans(zh);
346 /*************************************
347 * Inserting and deleting
350 static int cmd_record_insert( char *args[], WRBUF outbuff)
354 char *rec=restargs(args,1);
356 rc = zebra_insert_record(zh,
366 wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
372 static int cmd_exchange_record( char *args[], WRBUF outbuff)
375 char *action = args[2];
377 char *rec=restargs(args,3);
378 if (!(id && action && args[4] ))
380 wrbuf_puts(outbuff,"Missing arguments!\n");
381 onecommand("help exchange_record", outbuff, "");
384 rc=zebra_admin_exchange_record(zh, rec, strlen(rec),
385 id, strlen(id), atoi(action));
389 /**********************************
390 * Searching and retrieving
393 static int cmd_search_pqf(char *args[], WRBUF outbuff)
397 char *qry = restargs(args,2);
399 rc = zebra_search_PQF(zh, qry, set, &hits);
401 wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
405 static int cmd_find( char *args[], WRBUF outbuff)
407 char *setname=DEFAULTRESULTSET;
410 WRBUF qry = wrbuf_alloc();
411 if (0==strstr(args[0],"@attr"))
412 wrbuf_puts(qry, "@attr 1=/ ");
413 wrbuf_puts(qry,restargs(args,1));
415 onecommand("quickstart", outbuff, "");
416 wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
417 rc = zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
420 wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
427 static int cmd_show( char *args[], WRBUF outbuff)
429 int start=defargint(args[1], nextrecno);
430 int nrecs=defargint(args[2],1);
431 char *setname=defarg(args[3],DEFAULTRESULTSET);
433 ZebraRetrievalRecord *recs;
435 Z_RecordComposition *pcomp=0;
439 odr=odr_createmem(ODR_ENCODE);
440 recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
441 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
442 format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
443 for (i=0;i<nrecs;i++)
444 recs[i].position=start+i;
446 rc = zebra_records_retrieve (zh, odr, setname,
447 pcomp, format, nrecs,recs);
450 for (i=0;i<nrecs;i++)
452 printf("Err %d: %d\n",i,recs[i].errCode);
455 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
456 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
457 wrbuf_puts(outbuff, "\n");
459 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
461 nextrecno=start+nrecs;
467 static int cmd_sort( char *args[], WRBUF outbuff)
472 Z_SortKeySpecList *spec=0;
473 const char * inpsets[]={ DEFAULTRESULTSET, 0};
474 /* FIXME - allow the user to specify result sets in/out */
476 odr=odr_createmem(ODR_ENCODE);
477 spec=yaz_sort_spec (odr, restargs(args,1));
481 rc=zebra_sort(zh, odr,
487 wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
494 * int bend_sort (void *handle, bend_sort_rr *rr)
496 * ZebraHandle zh = (ZebraHandle) handle;
498 * zebra_sort (zh, rr->stream,
499 * rr->num_input_setnames, (const char **)
500 * rr->input_setnames,
501 * rr->output_setname,
504 * zebra_result (zh, &rr->errcode,
511 /**************************************)
512 * Command table, parser, and help
520 int (*testfunc)(char *args[], WRBUF outbuff);
524 struct cmdstruct cmds[] = {
526 * if text is 0, does not list the command
527 * if cmd is "", adds the args (and newline) in command listing
529 { "", "Starting and stopping:", "", 0 },
532 "starts the zebra service. You need to call this first\n"
533 "if no configfile is given, assumes " DEFAULTCONFIG,
536 "stops the zebra service",
539 "starts a zebra session. Once you have called zebra_start\n"
540 "you can call zebra_open to start working",
543 "closes a zebra session",
545 { "quickstart", "[configfile]",
546 "Does a zebra_start, zebra_open, and sets up the log",
549 { "", "Log file:","", 0},
552 "Directs the log to filename (or stderr)",
556 "Sets the logging level (or returns to default)",
560 "Sets the log prefix",
564 "writes an entry in the log",
567 { "", "Error handling:","", 0},
569 "Displays zebra's error status (code, str, add)",
572 "Displays zebra's error code",
575 "Displays zebra's error string",
578 "Displays zebra's additional error message",
581 { "", "Admin:","", 0},
583 "Initializes the zebra database, destroying all data in it",
585 { "select_database", "basename",
586 "Selects a database",
587 cmd_select_database},
588 { "create_database", "basename",
590 cmd_create_database},
591 { "drop_database", "basename",
594 { "begin_trans", "[rw]",
595 "Begins a transaction. rw=1 means write, otherwise read-only",
598 "Ends a transaction",
601 { "","Updating:","",0},
602 { "record_insert","record",
603 "inserts an sgml record into Default",
605 { "exchange_record","database record-id action record",
606 "inserts (1), updates (2), or deletes (3) a record \n"
607 "record-id must be a unique identifier for the record",
608 cmd_exchange_record},
610 { "","Searching and retrieving:","",0},
611 { "search_pqf","setname query",
620 { "show","[start] [numrecs] [resultset]",
623 { "s","[start] [numrecs] [resultset]",
627 "sorts a result set. (example spec: 1=4 >)",
630 { "", "Misc:","", 0},
640 { "help", "[command]",
641 "Gives help on command, or lists them all",
643 { "", "help [command] gives more info on command", "",0 },
645 {0,0,0,0} /* end marker */
649 char *line, /* input line */
650 WRBUF outbuff, /* output goes here */
651 const char *prevout) /* prev output, for 'expect' */
654 char *args[MAX_NO_ARGS];
656 char argbuf[MAX_ARG_LEN];
657 yaz_log(log_level,"%s",line);
658 strncpy(argbuf,line, MAX_ARG_LEN-1);
659 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
660 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
661 nargs=split_args(argbuf, args);
664 for (i = 0; i <= n; i++)
666 const char *cp = args[i];
667 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
671 return -90; /* no command on line, too bad */
673 if (0==strcmp(args[0],"expect"))
676 if (nargs>1) /* args[0] is not yet set, can't use restargs */
677 rest= line + (args[1]-argbuf); /* rest of the line */
679 return -1; /* need something to expect */
680 if (0==strstr(prevout,rest))
682 printf( "Failed expectation, '%s' not found\n", rest);
687 for (i=0;cmds[i].cmd;i++)
688 if (0==strcmp(cmds[i].cmd, args[0]))
691 args[0]= line + (args[1]-argbuf); /* rest of the line */
694 return ((cmds[i].testfunc)(args,outbuff));
696 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
697 yaz_log(log_level,"Unknown command");
701 static int cmd_help( char *args[], WRBUF outbuff)
706 { /* help for a single command */
707 for (i=0;cmds[i].cmd;i++)
708 if (0==strcmp(cmds[i].cmd, args[1]))
710 wrbuf_printf(outbuff,"%s %s\n%s\n",
711 cmds[i].cmd, cmds[i].args,
712 cmds[i].explanation);
715 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
718 { /* list all commands */
720 for (i=0;cmds[i].cmd;i++)
723 { /* ordinary command */
726 wrbuf_puts(outbuff,"\n ");
729 linelen += strlen(cmds[i].cmd) + 2;
730 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
733 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
737 wrbuf_puts(outbuff,"\n");
742 /* If Zebra reports an error after an operation,
743 * append it to the outbuff and log it */
744 static void Zerrors ( WRBUF outbuff)
749 ec=zebra_errCode (zh);
752 yaz_log(log_level, " Zebra error %d: %s, (%s)",
753 ec, zebra_errString (zh),
755 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
756 ec, zebra_errString (zh),
758 zebra_clearError(zh);
762 /**************************************
769 WRBUF outbuff=wrbuf_alloc();
770 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
771 wrbuf_puts(outbuff,"Zebrash at your service");
775 char buf[MAX_ARG_LEN];
777 #if HAVE_READLINE_READLINE_H
779 line_in=readline(PROMPT);
782 #if HAVE_READLINE_HISTORY_H
784 add_history(line_in);
788 /* line_in != NULL if readine is present and input is a tty */
794 if(strlen(line_in) > MAX_ARG_LEN-1) {
795 fprintf(stderr,"Input line too long\n");
803 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
807 /* get rid of \n in line */
808 if ((nl_cp = strchr(buf, '\n')))
810 strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
811 wrbuf_rewind(outbuff);
812 rc=onecommand(buf, outbuff, prevout);
815 wrbuf_puts(outbuff, " OK\n");
816 yaz_log(log_level, "OK");
820 wrbuf_printf(outbuff, " command returned %d\n",rc);
823 printf("%s\n", wrbuf_buf(outbuff));
825 wrbuf_free(outbuff,1);
832 printf ("zebrash [-c config]\n");
835 /**************************************
839 int main (int argc, char ** argv)
843 while ((ret = options ("c:h", argv, argc, &arg)) != -2)
848 default_config = arg;
852 /* FIXME - handle -v */
854 fprintf(stderr, "bad option %s\n", arg);
858 log_level=yaz_log_module_level("zebrash");