1 /* $Id: zebrash.c,v 1.29 2004-08-25 09:23:36 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>
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;
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);
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,
364 wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
370 static int cmd_exchange_record( char *args[], WRBUF outbuff)
373 char *action = args[2];
375 char *rec=restargs(args,3);
376 if (!(id && action && args[4] ))
378 wrbuf_puts(outbuff,"Missing arguments!\n");
379 onecommand("help exchange_record", outbuff, "");
382 rc=zebra_admin_exchange_record(zh, rec, strlen(rec),
383 id, strlen(id), atoi(action));
387 /**********************************
388 * Searching and retrieving
391 static int cmd_search_pqf( char *args[], WRBUF outbuff)
395 char *qry=restargs(args,2);
397 rc=zebra_search_PQF(zh, qry, set, &hits);
399 wrbuf_printf(outbuff,"%d hits found\n",hits);
403 static int cmd_find( char *args[], WRBUF outbuff)
405 char *setname=DEFAULTRESULTSET;
408 WRBUF qry=wrbuf_alloc();
409 if (0==strstr(args[0],"@attr"))
410 wrbuf_puts(qry, "@attr 1=/ ");
411 wrbuf_puts(qry,restargs(args,1));
413 onecommand("quickstart", outbuff, "");
414 wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
415 rc=zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
418 wrbuf_printf(outbuff,"%d hits found\n",hits);
425 static int cmd_show( char *args[], WRBUF outbuff)
427 int start=defargint(args[1], nextrecno);
428 int nrecs=defargint(args[2],1);
429 char *setname=defarg(args[3],DEFAULTRESULTSET);
431 ZebraRetrievalRecord *recs;
433 Z_RecordComposition *pcomp=0;
437 odr=odr_createmem(ODR_ENCODE);
438 recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
439 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
440 format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
441 for (i=0;i<nrecs;i++)
442 recs[i].position=start+i;
444 rc = zebra_records_retrieve (zh, odr, setname,
445 pcomp, format, nrecs,recs);
448 for (i=0;i<nrecs;i++)
450 printf("Err %d: %d\n",i,recs[i].errCode);
453 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
454 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
455 wrbuf_puts(outbuff, "\n");
457 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
459 nextrecno=start+nrecs;
465 static int cmd_sort( char *args[], WRBUF outbuff)
470 Z_SortKeySpecList *spec=0;
471 const char * inpsets[]={ DEFAULTRESULTSET, 0};
472 /* FIXME - allow the user to specify result sets in/out */
474 odr=odr_createmem(ODR_ENCODE);
475 spec=yaz_sort_spec (odr, restargs(args,1));
479 rc=zebra_sort(zh, odr,
485 wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
492 * int bend_sort (void *handle, bend_sort_rr *rr)
494 * ZebraHandle zh = (ZebraHandle) handle;
496 * zebra_sort (zh, rr->stream,
497 * rr->num_input_setnames, (const char **)
498 * rr->input_setnames,
499 * rr->output_setname,
502 * zebra_result (zh, &rr->errcode,
509 /**************************************)
510 * Command table, parser, and help
518 int (*testfunc)(char *args[], WRBUF outbuff);
522 struct cmdstruct cmds[] = {
524 * if text is 0, does not list the command
525 * if cmd is "", adds the args (and newline) in command listing
527 { "", "Starting and stopping:", "", 0 },
530 "starts the zebra service. You need to call this first\n"
531 "if no configfile is given, assumes " DEFAULTCONFIG,
534 "stops the zebra service",
537 "starts a zebra session. Once you have called zebra_start\n"
538 "you can call zebra_open to start working",
541 "closes a zebra session",
543 { "quickstart", "[configfile]",
544 "Does a zebra_start, zebra_open, and sets up the log",
547 { "", "Log file:","", 0},
550 "Directs the log to filename (or stderr)",
554 "Sets the logging level (or returns to default)",
558 "Sets the log prefix",
562 "writes an entry in the log",
565 { "", "Error handling:","", 0},
567 "Displays zebra's error status (code, str, add)",
570 "Displays zebra's error code",
573 "Displays zebra's error string",
576 "Displays zebra's additional error message",
579 { "", "Admin:","", 0},
581 "Initializes the zebra database, destroying all data in it",
583 { "select_database", "basename",
584 "Selects a database",
585 cmd_select_database},
586 { "create_database", "basename",
588 cmd_create_database},
589 { "drop_database", "basename",
592 { "begin_trans", "[rw]",
593 "Begins a transaction. rw=1 means write, otherwise read-only",
596 "Ends a transaction",
599 { "","Updating:","",0},
600 { "record_insert","record",
601 "inserts an sgml record into Default",
603 { "exchange_record","database record-id action record",
604 "inserts (1), updates (2), or deletes (3) a record \n"
605 "record-id must be a unique identifier for the record",
606 cmd_exchange_record},
608 { "","Searching and retrieving:","",0},
609 { "search_pqf","setname query",
618 { "show","[start] [numrecs] [resultset]",
621 { "s","[start] [numrecs] [resultset]",
625 "sorts a result set. (example spec: 1=4 >)",
628 { "", "Misc:","", 0},
638 { "help", "[command]",
639 "Gives help on command, or lists them all",
641 { "", "help [command] gives more info on command", "",0 },
643 {0,0,0,0} /* end marker */
647 char *line, /* input line */
648 WRBUF outbuff, /* output goes here */
649 const char *prevout) /* prev output, for 'expect' */
652 char *args[MAX_NO_ARGS];
654 char argbuf[MAX_ARG_LEN];
655 logf(LOG_APP,"%s",line);
656 strncpy(argbuf,line, MAX_ARG_LEN-1);
657 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
658 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
659 nargs=split_args(argbuf, args);
662 for (i = 0; i <= n; i++)
664 const char *cp = args[i];
665 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
669 return -90; /* no command on line, too bad */
671 if (0==strcmp(args[0],"expect"))
674 if (nargs>1) /* args[0] is not yet set, can't use restargs */
675 rest= line + (args[1]-argbuf); /* rest of the line */
677 return -1; /* need something to expect */
678 if (0==strstr(prevout,rest))
680 printf( "Failed expectation, '%s' not found\n", rest);
685 for (i=0;cmds[i].cmd;i++)
686 if (0==strcmp(cmds[i].cmd, args[0]))
689 args[0]= line + (args[1]-argbuf); /* rest of the line */
692 return ((cmds[i].testfunc)(args,outbuff));
694 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
695 logf(LOG_APP,"Unknown command");
699 static int cmd_help( char *args[], WRBUF outbuff)
704 { /* help for a single command */
705 for (i=0;cmds[i].cmd;i++)
706 if (0==strcmp(cmds[i].cmd, args[1]))
708 wrbuf_printf(outbuff,"%s %s\n%s\n",
709 cmds[i].cmd, cmds[i].args,
710 cmds[i].explanation);
713 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
716 { /* list all commands */
718 for (i=0;cmds[i].cmd;i++)
721 { /* ordinary command */
724 wrbuf_puts(outbuff,"\n ");
727 linelen += strlen(cmds[i].cmd) + 2;
728 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
731 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
735 wrbuf_puts(outbuff,"\n");
740 /* If Zebra reports an error after an operation,
741 * append it to the outbuff and log it */
742 static void Zerrors ( WRBUF outbuff)
747 ec=zebra_errCode (zh);
750 logf(LOG_APP, " Zebra error %d: %s, (%s)",
751 ec, zebra_errString (zh),
753 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
754 ec, zebra_errString (zh),
756 zebra_clearError(zh);
760 /**************************************
767 WRBUF outbuff=wrbuf_alloc();
768 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
769 wrbuf_puts(outbuff,"Zebrash at your service");
773 char buf[MAX_ARG_LEN];
775 #if HAVE_READLINE_READLINE_H
777 line_in=readline(PROMPT);
780 #if HAVE_READLINE_HISTORY_H
782 add_history(line_in);
786 /* line_in != NULL if readine is present and input is a tty */
792 if(strlen(line_in) > MAX_ARG_LEN-1) {
793 fprintf(stderr,"Input line too long\n");
801 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
805 /* get rid of \n in line */
806 if ((nl_cp = strchr(buf, '\n')))
808 strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
809 wrbuf_rewind(outbuff);
810 rc=onecommand(buf, outbuff, prevout);
813 wrbuf_puts(outbuff, " OK\n");
818 wrbuf_printf(outbuff, " command returned %d\n",rc);
821 printf("%s\n", wrbuf_buf(outbuff));
823 wrbuf_free(outbuff,1);
830 printf ("zebrash [-c config]\n");
833 /**************************************
837 int main (int argc, char ** argv)
841 while ((ret = options ("c:h", argv, argc, &arg)) != -2)
846 default_config = arg;
851 fprintf(stderr, "bad option %s\n", arg);