1 /* $Id: zebrash.c,v 1.26 2004-06-15 07:42:45 adam Exp $
2 Copyright (C) 2002,2003,2004
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>
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;
66 /**************************************
71 static int split_args( char *line, char** args )
72 { /* splits line into individual null-terminated strings,
73 * returns pointers to them in args */
74 /* FIXME - do we need to handle quoted args ?? */
78 args[0]=0; /* by default */
79 while (*p==' ' || *p=='\t' || *p=='\n')
83 while (*p==' ' || *p=='\t' || *p=='\n')
85 if (*p=='#') /* skip comments */
89 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
99 static char *defarg( char *arg, char *def )
107 static int defargint( char *arg, int def )
110 char *a=defarg(arg,0);
116 static char *restargs( char *args[], int n)
117 { /* Returns the rest of the arguments, starting at the nth, */
118 /* to the end of the command line. Assumes args[0] contains */
119 /* the original line, minus the command itself */
120 int skiplen= args[n]-args[1];
121 if (skiplen > strlen(args[0]))
123 return args[0]+skiplen;
126 int onecommand( char *line, WRBUF outbuff, const char *prevout);
128 /**************************************
129 * Simple support commands
132 int cmd_echo( char *args[], WRBUF outbuff)
134 wrbuf_printf(outbuff,"%s\n",restargs(args,1));
138 int cmd_quit( char *args[], WRBUF outbuff)
142 onecommand("zebra_close",outbuff,"");
147 onecommand("zebra_stop",outbuff,"");
150 wrbuf_puts(outbuff, "bye");
151 return -99; /* special stop signal */
154 /**************************************
155 * Tests for starting and stopping zebra, etc
158 static int cmd_help( char *args[], WRBUF outbuff);
160 static int cmd_zebra_start( char *args[], WRBUF outbuff)
163 if (!conf || !*conf) {
164 wrbuf_puts(outbuff,"no config file specified, using ");
165 wrbuf_puts(outbuff, default_config);
166 wrbuf_puts(outbuff, "\n");
169 zs=zebra_start(conf, 0, 0);
171 wrbuf_puts(outbuff, "zebra_start failed" );
177 static int cmd_zebra_stop( char *args[], WRBUF outbuff)
180 wrbuf_puts(outbuff,"zebra seems not to have been started, "
181 "stopping anyway\n");
187 static int cmd_zebra_open( char *args[], WRBUF outbuff)
190 wrbuf_puts(outbuff,"zebra seems not to have been started, "
196 static int cmd_zebra_close( char *args[], WRBUF outbuff)
199 wrbuf_puts(outbuff,"Seems like you have not called zebra_open,"
205 static int cmd_quickstart( char *args[], WRBUF outbuff)
210 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
212 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
213 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
215 rc=onecommand(tmp,outbuff,"");
216 logf(LOG_APP,"quickstart");
219 rc=onecommand("zebra_start",outbuff,"");
222 rc=onecommand("zebra_open",outbuff,"");
224 rc=onecommand("select_database Default",outbuff,"");
228 /**************************************
232 static int cmd_yaz_log_file( char *args[], WRBUF outbuff)
234 char *fn = defarg(args[1],0);
235 wrbuf_printf(outbuff, "sending yaz-log to %s\n",fn);
236 yaz_log_init_file(fn);
240 static int cmd_yaz_log_level( char *args[], WRBUF outbuff)
242 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
243 wrbuf_printf(outbuff, "setting yaz-log to level %d (ox%x)\n",lev,lev);
244 yaz_log_init_level(lev);
248 static int cmd_yaz_log_prefix( char *args[], WRBUF outbuff)
250 char *pref = defarg(args[1],"ZebraSh");
251 wrbuf_printf(outbuff, "setting yaz-log prefix to %s\n",pref);
252 yaz_log_init_prefix(pref);
256 static int cmd_logf( char *args[], WRBUF outbuff)
258 int lev = defargint(args[1],0);
263 lev=LOG_LOG; /* this is in the default set!*/
264 logf( lev, restargs(args,i));
271 static int cmd_err ( char *args[], WRBUF outbuff)
273 wrbuf_printf(outbuff, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
275 zebra_errString (zh),
279 static int cmd_errcode ( char *args[], WRBUF outbuff)
281 wrbuf_printf(outbuff, "errCode: %d \n",
285 static int cmd_errstr ( char *args[], WRBUF outbuff)
287 wrbuf_printf(outbuff, "errStr: %s\n",
288 zebra_errString (zh));
291 static int cmd_erradd ( char *args[], WRBUF outbuff)
293 wrbuf_printf(outbuff, "errAdd: %s \n",
298 /**************************************
302 static int cmd_init ( char *args[], WRBUF outbuff)
308 static int cmd_select_database ( char *args[], WRBUF outbuff)
310 char *db=defarg(args[1],DEFAULTDATABASE);
311 wrbuf_printf(outbuff,"Selecting database '%s'\n",db);
312 return zebra_select_database(zh, db);
315 static int cmd_create_database( char *args[], WRBUF outbuff)
317 char *db=defarg(args[1],DEFAULTDATABASE);
318 wrbuf_printf(outbuff,"Creating database '%s'\n",db);
320 return zebra_create_database(zh, db);
323 static int cmd_drop_database( char *args[], WRBUF outbuff)
328 wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
329 return zebra_drop_database(zh, db);
332 static int cmd_begin_trans( char *args[], WRBUF outbuff)
335 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
337 return zebra_begin_trans(zh,rw);
340 static int cmd_end_trans( char *args[], WRBUF outbuff)
342 return zebra_end_trans(zh);
344 /*************************************
345 * Inserting and deleting
348 static int cmd_record_insert( char *args[], WRBUF outbuff)
352 char *rec=restargs(args,1);
354 rc = zebra_insert_record(zh,
363 wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
369 static int cmd_exchange_record( char *args[], WRBUF outbuff)
372 char *action = args[2];
374 char *rec=restargs(args,3);
375 if (!(id && action && args[4] ))
377 wrbuf_puts(outbuff,"Missing arguments!\n");
378 onecommand("help exchange_record", outbuff, "");
381 rc=zebra_admin_exchange_record(zh, rec, strlen(rec),
382 id, strlen(id), atoi(action));
386 /**********************************
387 * Searching and retrieving
390 static int cmd_search_pqf( char *args[], WRBUF outbuff)
394 char *qry=restargs(args,2);
396 rc=zebra_search_PQF(zh, qry, set, &hits);
398 wrbuf_printf(outbuff,"%d hits found\n",hits);
402 static int cmd_find( char *args[], WRBUF outbuff)
404 char *setname=DEFAULTRESULTSET;
407 WRBUF qry=wrbuf_alloc();
408 if (0==strstr(args[0],"@attr"))
409 wrbuf_puts(qry, "@attr 1=/ ");
410 wrbuf_puts(qry,restargs(args,1));
412 onecommand("quickstart", outbuff, "");
413 wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
414 rc=zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
417 wrbuf_printf(outbuff,"%d hits found\n",hits);
424 static int cmd_show( char *args[], WRBUF outbuff)
426 int start=defargint(args[1], nextrecno);
427 int nrecs=defargint(args[2],1);
428 char *setname=defarg(args[3],DEFAULTRESULTSET);
430 ZebraRetrievalRecord *recs;
432 Z_RecordComposition *pcomp=0;
436 odr=odr_createmem(ODR_ENCODE);
437 recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
438 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
439 format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
440 for (i=0;i<nrecs;i++)
441 recs[i].position=start+i;
443 rc = zebra_records_retrieve (zh, odr, setname,
444 pcomp, format, nrecs,recs);
447 for (i=0;i<nrecs;i++)
449 printf("Err %d: %d\n",i,recs[i].errCode);
452 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
453 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
454 wrbuf_puts(outbuff, "\n");
456 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
458 nextrecno=start+nrecs;
464 static int cmd_sort( char *args[], WRBUF outbuff)
469 Z_SortKeySpecList *spec=0;
470 const char * inpsets[]={ DEFAULTRESULTSET, 0};
471 /* FIXME - allow the user to specify result sets in/out */
473 odr=odr_createmem(ODR_ENCODE);
474 spec=yaz_sort_spec (odr, restargs(args,1));
478 rc=zebra_sort(zh, odr,
484 wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
491 * int bend_sort (void *handle, bend_sort_rr *rr)
493 * ZebraHandle zh = (ZebraHandle) handle;
495 * zebra_sort (zh, rr->stream,
496 * rr->num_input_setnames, (const char **)
497 * rr->input_setnames,
498 * rr->output_setname,
501 * zebra_result (zh, &rr->errcode,
508 /**************************************)
509 * Command table, parser, and help
517 int (*testfunc)(char *args[], WRBUF outbuff);
521 struct cmdstruct cmds[] = {
523 * if text is 0, does not list the command
524 * if cmd is "", adds the args (and newline) in command listing
526 { "", "Starting and stopping:", "", 0 },
529 "starts the zebra service. You need to call this first\n"
530 "if no configfile is given, assumes " DEFAULTCONFIG,
533 "stops the zebra service",
536 "starts a zebra session. Once you have called zebra_start\n"
537 "you can call zebra_open to start working",
540 "closes a zebra session",
542 { "quickstart", "[configfile]",
543 "Does a zebra_start, zebra_open, and sets up the log",
546 { "", "Log file:","", 0},
549 "Directs the log to filename (or stderr)",
553 "Sets the logging level (or returns to default)",
557 "Sets the log prefix",
561 "writes an entry in the log",
564 { "", "Error handling:","", 0},
566 "Displays zebra's error status (code, str, add)",
569 "Displays zebra's error code",
572 "Displays zebra's error string",
575 "Displays zebra's additional error message",
578 { "", "Admin:","", 0},
580 "Initializes the zebra database, destroying all data in it",
582 { "select_database", "basename",
583 "Selects a database",
584 cmd_select_database},
585 { "create_database", "basename",
587 cmd_create_database},
588 { "drop_database", "basename",
591 { "begin_trans", "[rw]",
592 "Begins a transaction. rw=1 means write, otherwise read-only",
595 "Ends a transaction",
598 { "","Updating:","",0},
599 { "record_insert","record",
600 "inserts an sgml record into Default",
602 { "exchange_record","database record-id action record",
603 "inserts (1), updates (2), or deletes (3) a record \n"
604 "record-id must be a unique identifier for the record",
605 cmd_exchange_record},
607 { "","Searching and retrieving:","",0},
608 { "search_pqf","setname query",
617 { "show","[start] [numrecs] [resultset]",
620 { "s","[start] [numrecs] [resultset]",
624 "sorts a result set. (example spec: 1=4 >)",
627 { "", "Misc:","", 0},
637 { "help", "[command]",
638 "Gives help on command, or lists them all",
640 { "", "help [command] gives more info on command", "",0 },
642 {0,0,0,0} /* end marker */
646 char *line, /* input line */
647 WRBUF outbuff, /* output goes here */
648 const char *prevout) /* prev output, for 'expect' */
651 char *args[MAX_NO_ARGS];
653 char argbuf[MAX_ARG_LEN];
654 logf(LOG_APP,"%s",line);
655 strncpy(argbuf,line, MAX_ARG_LEN-1);
656 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
657 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
658 nargs=split_args(argbuf, args);
661 for (i = 0; i <= n; i++)
663 const char *cp = args[i];
664 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
668 return -90; /* no command on line, too bad */
670 if (0==strcmp(args[0],"expect"))
673 if (nargs>1) /* args[0] is not yet set, can't use restargs */
674 rest= line + (args[1]-argbuf); /* rest of the line */
676 return -1; /* need something to expect */
677 if (0==strstr(prevout,rest))
679 printf( "Failed expectation, '%s' not found\n", rest);
684 for (i=0;cmds[i].cmd;i++)
685 if (0==strcmp(cmds[i].cmd, args[0]))
688 args[0]= line + (args[1]-argbuf); /* rest of the line */
691 return ((cmds[i].testfunc)(args,outbuff));
693 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
694 logf(LOG_APP,"Unknown command");
698 static int cmd_help( char *args[], WRBUF outbuff)
703 { /* help for a single command */
704 for (i=0;cmds[i].cmd;i++)
705 if (0==strcmp(cmds[i].cmd, args[1]))
707 wrbuf_printf(outbuff,"%s %s\n%s\n",
708 cmds[i].cmd, cmds[i].args,
709 cmds[i].explanation);
712 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
715 { /* list all commands */
717 for (i=0;cmds[i].cmd;i++)
720 { /* ordinary command */
723 wrbuf_puts(outbuff,"\n ");
726 linelen += strlen(cmds[i].cmd) + 2;
727 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
730 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
734 wrbuf_puts(outbuff,"\n");
739 /* If Zebra reports an error after an operation,
740 * append it to the outbuff and log it */
741 static void Zerrors ( WRBUF outbuff)
746 ec=zebra_errCode (zh);
749 logf(LOG_APP, " Zebra error %d: %s, (%s)",
750 ec, zebra_errString (zh),
752 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
753 ec, zebra_errString (zh),
755 zebra_clearError(zh);
759 /**************************************
766 WRBUF outbuff=wrbuf_alloc();
767 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
768 wrbuf_puts(outbuff,"Zebrash at your service");
772 char buf[MAX_ARG_LEN];
774 #if HAVE_READLINE_READLINE_H
776 line_in=readline(PROMPT);
779 #if HAVE_READLINE_HISTORY_H
781 add_history(line_in);
785 /* line_in != NULL if readine is present and input is a tty */
791 if(strlen(line_in) > MAX_ARG_LEN-1) {
792 fprintf(stderr,"Input line too long\n");
800 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
804 /* get rid of \n in line */
805 if ((nl_cp = strchr(buf, '\n')))
807 strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
808 wrbuf_rewind(outbuff);
809 rc=onecommand(buf, outbuff, prevout);
812 wrbuf_puts(outbuff, " OK\n");
817 wrbuf_printf(outbuff, " command returned %d\n",rc);
820 printf("%s\n", wrbuf_buf(outbuff));
822 wrbuf_free(outbuff,1);
829 printf ("zebrash [-c config]\n");
832 /**************************************
836 int main (int argc, char ** argv)
840 while ((ret = options ("c:h", argv, argc, &arg)) != -2)
845 default_config = arg;
850 fprintf(stderr, "bad option %s\n", arg);