1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 zebrash.c - command-line interface to zebra API
32 #if HAVE_READLINE_READLINE_H
33 #include <readline/readline.h>
35 #if HAVE_READLINE_HISTORY_H
36 #include <readline/history.h>
39 #include <idzebra/api.h>
41 #include <yaz/proto.h>
42 #include <yaz/sortspec.h>
43 #include <yaz/options.h>
44 #include <yaz/wrbuf.h>
45 #include <yaz/oid_db.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, "
194 zh = zebra_open(zs, 0);
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, "%s", 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_update_record(zh,
366 wrbuf_printf(outbuff,"ok sysno=" ZINT_FORMAT "\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, "");
385 rc = zebra_update_record(zh, action_update, 0 /* record_type */,
393 /**********************************
394 * Searching and retrieving
397 static int cmd_search_pqf(char *args[], WRBUF outbuff)
401 char *qry = restargs(args,2);
403 rc = zebra_search_PQF(zh, qry, set, &hits);
405 wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
409 static int cmd_find( char *args[], WRBUF outbuff)
411 char *setname=DEFAULTRESULTSET;
414 WRBUF qry = wrbuf_alloc();
415 if (0==strstr(args[0],"@attr"))
416 wrbuf_puts(qry, "@attr 1=/ ");
417 wrbuf_puts(qry,restargs(args,1));
419 onecommand("quickstart", outbuff, "");
420 wrbuf_printf(outbuff, "find %s\n",wrbuf_cstr(qry));
421 rc = zebra_search_PQF(zh, wrbuf_cstr(qry), setname, &hits);
424 wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
431 static int cmd_show( char *args[], WRBUF outbuff)
433 int start=defargint(args[1], nextrecno);
434 int nrecs=defargint(args[2],1);
435 char *setname=defarg(args[3],DEFAULTRESULTSET);
437 ZebraRetrievalRecord *recs;
439 Z_RecordComposition *pcomp=0;
442 odr = odr_createmem(ODR_ENCODE);
443 recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
444 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
446 for (i=0;i<nrecs;i++)
447 recs[i].position=start+i;
449 rc = zebra_records_retrieve(zh, odr, setname,
450 pcomp, yaz_oid_recsyn_xml, nrecs,recs);
453 for (i=0;i<nrecs;i++)
455 printf("Err %d: %d\n",i,recs[i].errCode);
458 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
459 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
460 wrbuf_puts(outbuff, "\n");
462 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
464 nextrecno=start+nrecs;
470 static int cmd_sort( char *args[], WRBUF outbuff)
475 Z_SortKeySpecList *spec=0;
476 const char * inpsets[]={ DEFAULTRESULTSET, 0};
477 /* FIXME - allow the user to specify result sets in/out */
479 odr=odr_createmem(ODR_ENCODE);
480 spec=yaz_sort_spec (odr, restargs(args,1));
484 rc=zebra_sort(zh, odr,
490 wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
497 * int bend_sort (void *handle, bend_sort_rr *rr)
499 * ZebraHandle zh = (ZebraHandle) handle;
501 * zebra_sort (zh, rr->stream,
502 * rr->num_input_setnames, (const char **)
503 * rr->input_setnames,
504 * rr->output_setname,
507 * zebra_result (zh, &rr->errcode,
514 /**************************************)
515 * Command table, parser, and help
523 int (*testfunc)(char *args[], WRBUF outbuff);
527 struct cmdstruct cmds[] = {
529 * if text is 0, does not list the command
530 * if cmd is "", adds the args (and newline) in command listing
532 { "", "Starting and stopping:", "", 0 },
535 "starts the zebra service. You need to call this first\n"
536 "if no configfile is given, assumes " DEFAULTCONFIG,
539 "stops the zebra service",
542 "starts a zebra session. Once you have called zebra_start\n"
543 "you can call zebra_open to start working",
546 "closes a zebra session",
548 { "quickstart", "[configfile]",
549 "Does a zebra_start, zebra_open, and sets up the log",
552 { "", "Log file:","", 0},
555 "Directs the log to filename (or stderr)",
559 "Sets the logging level (or returns to default)",
563 "Sets the log prefix",
567 "writes an entry in the log",
570 { "", "Error handling:","", 0},
572 "Displays zebra's error status (code, str, add)",
575 "Displays zebra's error code",
578 "Displays zebra's error string",
581 "Displays zebra's additional error message",
584 { "", "Admin:","", 0},
586 "Initializes the zebra database, destroying all data in it",
588 { "select_database", "basename",
589 "Selects a database",
590 cmd_select_database},
591 { "create_database", "basename",
593 cmd_create_database},
594 { "drop_database", "basename",
597 { "begin_trans", "[rw]",
598 "Begins a transaction. rw=1 means write, otherwise read-only",
601 "Ends a transaction",
604 { "","Updating:","",0},
605 { "record_insert","record",
606 "inserts an sgml record into Default",
608 { "exchange_record","database record-id action record",
609 "inserts (1), updates (2), or deletes (3) a record \n"
610 "record-id must be a unique identifier for the record",
611 cmd_exchange_record},
613 { "","Searching and retrieving:","",0},
614 { "search_pqf","setname query",
623 { "show","[start] [numrecs] [resultset]",
626 { "s","[start] [numrecs] [resultset]",
630 "sorts a result set. (example spec: 1=4 >)",
633 { "", "Misc:","", 0},
643 { "help", "[command]",
644 "Gives help on command, or lists them all",
646 { "", "help [command] gives more info on command", "",0 },
648 {0,0,0,0} /* end marker */
652 char *line, /* input line */
653 WRBUF outbuff, /* output goes here */
654 const char *prevout) /* prev output, for 'expect' */
657 char *args[MAX_NO_ARGS];
659 char argbuf[MAX_ARG_LEN];
660 yaz_log(log_level,"%s",line);
661 strncpy(argbuf,line, MAX_ARG_LEN-1);
662 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
663 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
664 nargs=split_args(argbuf, args);
667 for (i = 0; i <= n; i++)
669 const char *cp = args[i];
670 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
674 return -90; /* no command on line, too bad */
676 if (0==strcmp(args[0],"expect"))
679 if (nargs>1) /* args[0] is not yet set, can't use restargs */
680 rest= line + (args[1]-argbuf); /* rest of the line */
682 return -1; /* need something to expect */
683 if (0==strstr(prevout,rest))
685 printf( "Failed expectation, '%s' not found\n", rest);
690 for (i=0;cmds[i].cmd;i++)
691 if (0==strcmp(cmds[i].cmd, args[0]))
694 args[0]= line + (args[1]-argbuf); /* rest of the line */
697 return ((cmds[i].testfunc)(args,outbuff));
699 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
700 yaz_log(log_level,"Unknown command");
704 static int cmd_help( char *args[], WRBUF outbuff)
709 { /* help for a single command */
710 for (i=0;cmds[i].cmd;i++)
711 if (0==strcmp(cmds[i].cmd, args[1]))
713 wrbuf_printf(outbuff,"%s %s\n%s\n",
714 cmds[i].cmd, cmds[i].args,
715 cmds[i].explanation);
718 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
721 { /* list all commands */
723 for (i=0;cmds[i].cmd;i++)
726 { /* ordinary command */
729 wrbuf_puts(outbuff,"\n ");
732 linelen += strlen(cmds[i].cmd) + 2;
733 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
736 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
740 wrbuf_puts(outbuff,"\n");
745 /* If Zebra reports an error after an operation,
746 * append it to the outbuff and log it */
747 static void Zerrors (WRBUF outbuff)
752 ec=zebra_errCode (zh);
755 yaz_log(log_level, " Zebra error %d: %s, (%s)",
756 ec, zebra_errString (zh),
758 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
759 ec, zebra_errString (zh),
764 /**************************************
771 WRBUF outbuff=wrbuf_alloc();
772 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
773 wrbuf_puts(outbuff,"Zebrash at your service");
777 char buf[MAX_ARG_LEN];
779 #if HAVE_READLINE_READLINE_H
781 line_in=readline(PROMPT);
784 #if HAVE_READLINE_HISTORY_H
786 add_history(line_in);
790 /* line_in != NULL if readine is present and input is a tty */
796 if(strlen(line_in) > MAX_ARG_LEN-1) {
797 fprintf(stderr,"Input line too long\n");
805 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
809 /* get rid of \n in line */
810 if ((nl_cp = strchr(buf, '\n')))
812 strncpy(prevout, wrbuf_cstr(outbuff), MAX_OUT_BUFF);
813 wrbuf_rewind(outbuff);
814 rc=onecommand(buf, outbuff, prevout);
817 wrbuf_puts(outbuff, " OK\n");
818 yaz_log(log_level, "OK");
822 wrbuf_printf(outbuff, " command returned %d\n",rc);
825 printf("%s\n", wrbuf_cstr(outbuff));
827 wrbuf_destroy(outbuff);
831 static void usage(void)
834 printf ("zebrash [-c config]\n");
837 /**************************************
841 int main (int argc, char ** argv)
845 while ((ret = options ("c:h", argv, argc, &arg)) != -2)
850 default_config = arg;
854 /* FIXME - handle -v */
856 fprintf(stderr, "bad option %s\n", arg);
860 log_level=yaz_log_module_level("zebrash");
868 * c-file-style: "Stroustrup"
869 * indent-tabs-mode: nil
871 * vim: shiftwidth=4 tabstop=8 expandtab