1 /* $Id: zebrash.c,v 1.25 2004-01-22 11:27:21 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/wrbuf.h>
46 #define MAX_NO_ARGS 32
47 #define MAX_OUT_BUFF 4096
48 #define MAX_ARG_LEN 1024
49 #define PROMPT "ZebraSh>"
50 #define DEFAULTCONFIG "./zebra.cfg"
51 #define DEFAULTDATABASE "Default"
52 #define DEFAULTRESULTSET "MyResultSet"
55 /**************************************
56 * Global variables (yuck!)
59 ZebraService zs=0; /* our global handle to zebra */
60 ZebraHandle zh=0; /* the current session */
61 /* time being, only one session works */
62 int nextrecno=1; /* record number to show next */
64 /**************************************
69 static int split_args( char *line, char** args )
70 { /* splits line into individual null-terminated strings,
71 * returns pointers to them in args */
72 /* FIXME - do we need to handle quoted args ?? */
76 args[0]=0; /* by default */
77 while (*p==' ' || *p=='\t' || *p=='\n')
81 while (*p==' ' || *p=='\t' || *p=='\n')
83 if (*p=='#') /* skip comments */
87 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
97 static char *defarg( char *arg, char *def )
105 static int defargint( char *arg, int def )
108 char *a=defarg(arg,0);
114 static char *restargs( char *args[], int n)
115 { /* Returns the rest of the arguments, starting at the nth, */
116 /* to the end of the command line. Assumes args[0] contains */
117 /* the original line, minus the command itself */
118 int skiplen= args[n]-args[1];
119 if (skiplen > strlen(args[0]))
121 return args[0]+skiplen;
124 int onecommand( char *line, WRBUF outbuff, const char *prevout);
126 /**************************************
127 * Simple support commands
130 int cmd_echo( char *args[], WRBUF outbuff)
132 wrbuf_printf(outbuff,"%s\n",restargs(args,1));
136 int cmd_quit( char *args[], WRBUF outbuff)
140 onecommand("zebra_close",outbuff,"");
145 onecommand("zebra_stop",outbuff,"");
148 wrbuf_puts(outbuff, "bye");
149 return -99; /* special stop signal */
152 /**************************************
153 * Tests for starting and stopping zebra, etc
156 static int cmd_help( char *args[], WRBUF outbuff);
158 static int cmd_zebra_start( char *args[], WRBUF outbuff)
161 if (!conf || !*conf) {
162 wrbuf_puts(outbuff,"no config file specified, using "
163 DEFAULTCONFIG "\n" );
166 zs=zebra_start(conf, 0, 0);
168 wrbuf_puts(outbuff, "zebra_start failed" );
174 static int cmd_zebra_stop( char *args[], WRBUF outbuff)
177 wrbuf_puts(outbuff,"zebra seems not to have been started, "
178 "stopping anyway\n");
184 static int cmd_zebra_open( char *args[], WRBUF outbuff)
187 wrbuf_puts(outbuff,"zebra seems not to have been started, "
193 static int cmd_zebra_close( char *args[], WRBUF outbuff)
196 wrbuf_puts(outbuff,"Seems like you have not called zebra_open,"
202 static int cmd_quickstart( char *args[], WRBUF outbuff)
207 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
209 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
210 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
212 rc=onecommand(tmp,outbuff,"");
213 logf(LOG_APP,"quickstart");
216 rc=onecommand("zebra_start",outbuff,"");
219 rc=onecommand("zebra_open",outbuff,"");
221 rc=onecommand("select_database Default",outbuff,"");
225 /**************************************
229 static int cmd_yaz_log_file( char *args[], WRBUF outbuff)
231 char *fn = defarg(args[1],0);
232 wrbuf_printf(outbuff, "sending yaz-log to %s\n",fn);
233 yaz_log_init_file(fn);
237 static int cmd_yaz_log_level( char *args[], WRBUF outbuff)
239 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
240 wrbuf_printf(outbuff, "setting yaz-log to level %d (ox%x)\n",lev,lev);
241 yaz_log_init_level(lev);
245 static int cmd_yaz_log_prefix( char *args[], WRBUF outbuff)
247 char *pref = defarg(args[1],"ZebraSh");
248 wrbuf_printf(outbuff, "setting yaz-log prefix to %s\n",pref);
249 yaz_log_init_prefix(pref);
253 static int cmd_logf( char *args[], WRBUF outbuff)
255 int lev = defargint(args[1],0);
260 lev=LOG_LOG; /* this is in the default set!*/
261 logf( lev, restargs(args,i));
268 static int cmd_err ( char *args[], WRBUF outbuff)
270 wrbuf_printf(outbuff, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
272 zebra_errString (zh),
276 static int cmd_errcode ( char *args[], WRBUF outbuff)
278 wrbuf_printf(outbuff, "errCode: %d \n",
282 static int cmd_errstr ( char *args[], WRBUF outbuff)
284 wrbuf_printf(outbuff, "errStr: %s\n",
285 zebra_errString (zh));
288 static int cmd_erradd ( char *args[], WRBUF outbuff)
290 wrbuf_printf(outbuff, "errAdd: %s \n",
295 /**************************************
299 static int cmd_init ( char *args[], WRBUF outbuff)
305 static int cmd_select_database ( char *args[], WRBUF outbuff)
307 char *db=defarg(args[1],DEFAULTDATABASE);
308 wrbuf_printf(outbuff,"Selecting database '%s'\n",db);
309 return zebra_select_database(zh, db);
312 static int cmd_create_database( char *args[], WRBUF outbuff)
314 char *db=defarg(args[1],DEFAULTDATABASE);
315 wrbuf_printf(outbuff,"Creating database '%s'\n",db);
317 return zebra_create_database(zh, db);
320 static int cmd_drop_database( char *args[], WRBUF outbuff)
325 wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
326 return zebra_drop_database(zh, db);
329 static int cmd_begin_trans( char *args[], WRBUF outbuff)
332 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
334 return zebra_begin_trans(zh,rw);
337 static int cmd_end_trans( char *args[], WRBUF outbuff)
339 return zebra_end_trans(zh);
341 /*************************************
342 * Inserting and deleting
345 static int cmd_record_insert( char *args[], WRBUF outbuff)
349 char *rec=restargs(args,1);
351 rc = zebra_insert_record(zh,
360 wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
366 static int cmd_exchange_record( char *args[], WRBUF outbuff)
369 char *action = args[2];
371 char *rec=restargs(args,3);
372 if (!(id && action && args[4] ))
374 wrbuf_puts(outbuff,"Missing arguments!\n");
375 onecommand("help exchange_record", outbuff, "");
378 rc=zebra_admin_exchange_record(zh, rec, strlen(rec),
379 id, strlen(id), atoi(action));
383 /**********************************
384 * Searching and retrieving
387 static int cmd_search_pqf( char *args[], WRBUF outbuff)
391 char *qry=restargs(args,2);
393 rc=zebra_search_PQF(zh, qry, set, &hits);
395 wrbuf_printf(outbuff,"%d hits found\n",hits);
399 static int cmd_find( char *args[], WRBUF outbuff)
401 char *setname=DEFAULTRESULTSET;
404 WRBUF qry=wrbuf_alloc();
405 if (0==strstr(args[0],"@attr"))
406 wrbuf_puts(qry, "@attr 1=/ ");
407 wrbuf_puts(qry,restargs(args,1));
409 onecommand("quickstart", outbuff, "");
410 wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
411 rc=zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
414 wrbuf_printf(outbuff,"%d hits found\n",hits);
421 static int cmd_show( char *args[], WRBUF outbuff)
423 int start=defargint(args[1], nextrecno);
424 int nrecs=defargint(args[2],1);
425 char *setname=defarg(args[3],DEFAULTRESULTSET);
427 ZebraRetrievalRecord *recs;
429 Z_RecordComposition *pcomp=0;
433 odr=odr_createmem(ODR_ENCODE);
434 recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
435 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
436 format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
437 for (i=0;i<nrecs;i++)
438 recs[i].position=start+i;
440 rc = zebra_records_retrieve (zh, odr, setname,
441 pcomp, format, nrecs,recs);
444 for (i=0;i<nrecs;i++)
446 printf("Err %d: %d\n",i,recs[i].errCode);
449 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
450 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
451 wrbuf_puts(outbuff, "\n");
453 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
455 nextrecno=start+nrecs;
461 static int cmd_sort( char *args[], WRBUF outbuff)
466 Z_SortKeySpecList *spec=0;
467 const char * inpsets[]={ DEFAULTRESULTSET, 0};
468 /* FIXME - allow the user to specify result sets in/out */
470 odr=odr_createmem(ODR_ENCODE);
471 spec=yaz_sort_spec (odr, restargs(args,1));
475 rc=zebra_sort(zh, odr,
481 wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
488 * int bend_sort (void *handle, bend_sort_rr *rr)
490 * ZebraHandle zh = (ZebraHandle) handle;
492 * zebra_sort (zh, rr->stream,
493 * rr->num_input_setnames, (const char **)
494 * rr->input_setnames,
495 * rr->output_setname,
498 * zebra_result (zh, &rr->errcode,
505 /**************************************)
506 * Command table, parser, and help
514 int (*testfunc)(char *args[], WRBUF outbuff);
518 struct cmdstruct cmds[] = {
520 * if text is 0, does not list the command
521 * if cmd is "", adds the args (and newline) in command listing
523 { "", "Starting and stopping:", "", 0 },
526 "starts the zebra service. You need to call this first\n"
527 "if no configfile is given, assumes " DEFAULTCONFIG,
530 "stops the zebra service",
533 "starts a zebra session. Once you have called zebra_start\n"
534 "you can call zebra_open to start working",
537 "closes a zebra session",
539 { "quickstart", "[configfile]",
540 "Does a zebra_start, zebra_open, and sets up the log",
543 { "", "Log file:","", 0},
546 "Directs the log to filename (or stderr)",
550 "Sets the logging level (or returns to default)",
554 "Sets the log prefix",
558 "writes an entry in the log",
561 { "", "Error handling:","", 0},
563 "Displays zebra's error status (code, str, add)",
566 "Displays zebra's error code",
569 "Displays zebra's error string",
572 "Displays zebra's additional error message",
575 { "", "Admin:","", 0},
577 "Initializes the zebra database, destroying all data in it",
579 { "select_database", "basename",
580 "Selects a database",
581 cmd_select_database},
582 { "create_database", "basename",
584 cmd_create_database},
585 { "drop_database", "basename",
588 { "begin_trans", "[rw]",
589 "Begins a transaction. rw=1 means write, otherwise read-only",
592 "Ends a transaction",
595 { "","Updating:","",0},
596 { "record_insert","record",
597 "inserts an sgml record into Default",
599 { "exchange_record","database record-id action record",
600 "inserts (1), updates (2), or deletes (3) a record \n"
601 "record-id must be a unique identifier for the record",
602 cmd_exchange_record},
604 { "","Searching and retrieving:","",0},
605 { "search_pqf","setname query",
614 { "show","[start] [numrecs] [resultset]",
617 { "s","[start] [numrecs] [resultset]",
621 "sorts a result set. (example spec: 1=4 >)",
624 { "", "Misc:","", 0},
634 { "help", "[command]",
635 "Gives help on command, or lists them all",
637 { "", "help [command] gives more info on command", "",0 },
639 {0,0,0,0} /* end marker */
643 char *line, /* input line */
644 WRBUF outbuff, /* output goes here */
645 const char *prevout) /* prev output, for 'expect' */
648 char *args[MAX_NO_ARGS];
650 char argbuf[MAX_ARG_LEN];
651 logf(LOG_APP,"%s",line);
652 strncpy(argbuf,line, MAX_ARG_LEN-1);
653 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
654 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
655 nargs=split_args(argbuf, args);
658 for (i = 0; i <= n; i++)
660 const char *cp = args[i];
661 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
665 return -90; /* no command on line, too bad */
667 if (0==strcmp(args[0],"expect"))
670 if (nargs>1) /* args[0] is not yet set, can't use restargs */
671 rest= line + (args[1]-argbuf); /* rest of the line */
673 return -1; /* need something to expect */
674 if (0==strstr(prevout,rest))
676 printf( "Failed expectation, '%s' not found\n", rest);
681 for (i=0;cmds[i].cmd;i++)
682 if (0==strcmp(cmds[i].cmd, args[0]))
685 args[0]= line + (args[1]-argbuf); /* rest of the line */
688 return ((cmds[i].testfunc)(args,outbuff));
690 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
691 logf(LOG_APP,"Unknown command");
695 static int cmd_help( char *args[], WRBUF outbuff)
700 { /* help for a single command */
701 for (i=0;cmds[i].cmd;i++)
702 if (0==strcmp(cmds[i].cmd, args[1]))
704 wrbuf_printf(outbuff,"%s %s\n%s\n",
705 cmds[i].cmd, cmds[i].args,
706 cmds[i].explanation);
709 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
712 { /* list all commands */
714 for (i=0;cmds[i].cmd;i++)
717 { /* ordinary command */
720 wrbuf_puts(outbuff,"\n ");
723 linelen += strlen(cmds[i].cmd) + 2;
724 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
727 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
731 wrbuf_puts(outbuff,"\n");
736 /* If Zebra reports an error after an operation,
737 * append it to the outbuff and log it */
738 static void Zerrors ( WRBUF outbuff)
743 ec=zebra_errCode (zh);
746 logf(LOG_APP, " Zebra error %d: %s, (%s)",
747 ec, zebra_errString (zh),
749 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
750 ec, zebra_errString (zh),
752 zebra_clearError(zh);
756 /**************************************
763 WRBUF outbuff=wrbuf_alloc();
764 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
765 wrbuf_puts(outbuff,"Zebrash at your service");
769 char buf[MAX_ARG_LEN];
771 #if HAVE_READLINE_READLINE_H
773 line_in=readline(PROMPT);
776 #if HAVE_READLINE_HISTORY_H
778 add_history(line_in);
782 /* line_in != NULL if readine is present and input is a tty */
788 if(strlen(line_in) > MAX_ARG_LEN-1) {
789 fprintf(stderr,"Input line too long\n");
797 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
801 /* get rid of \n in line */
802 if ((nl_cp = strchr(buf, '\n')))
804 strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
805 wrbuf_rewind(outbuff);
806 rc=onecommand(buf, outbuff, prevout);
809 wrbuf_puts(outbuff, " OK\n");
814 wrbuf_printf(outbuff, " command returned %d\n",rc);
817 printf("%s\n", wrbuf_buf(outbuff));
819 wrbuf_free(outbuff,1);
823 /**************************************
827 int main (int argc, char ** args)