1 /* $Id: zebrash.c,v 1.37 2006-05-10 08:13:23 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
35 #if HAVE_READLINE_READLINE_H
36 #include <readline/readline.h>
38 #if HAVE_READLINE_HISTORY_H
39 #include <readline/history.h>
42 #include <idzebra/api.h>
44 #include <yaz/proto.h>
45 #include <yaz/sortspec.h>
46 #include <yaz/options.h>
47 #include <yaz/wrbuf.h>
49 #define MAX_NO_ARGS 32
50 #define MAX_OUT_BUFF 4096
51 #define MAX_ARG_LEN 1024
52 #define PROMPT "ZebraSh>"
53 #define DEFAULTCONFIG "./zebra.cfg"
54 #define DEFAULTDATABASE "Default"
55 #define DEFAULTRESULTSET "MyResultSet"
58 /**************************************
59 * Global variables (yuck!)
62 ZebraService zs=0; /* our global handle to zebra */
63 ZebraHandle zh=0; /* the current session */
64 /* time being, only one session works */
65 int nextrecno=1; /* record number to show next */
66 static char *default_config = DEFAULTCONFIG;
67 static int log_level=0;
70 /**************************************
75 static int split_args( char *line, char** args )
76 { /* splits line into individual null-terminated strings,
77 * returns pointers to them in args */
78 /* FIXME - do we need to handle quoted args ?? */
82 args[0]=0; /* by default */
83 while (*p==' ' || *p=='\t' || *p=='\n')
87 while (*p==' ' || *p=='\t' || *p=='\n')
89 if (*p=='#') /* skip comments */
93 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
103 static char *defarg( char *arg, char *def )
111 static int defargint( char *arg, int def )
114 char *a=defarg(arg,0);
120 static char *restargs( char *args[], int n)
121 { /* Returns the rest of the arguments, starting at the nth, */
122 /* to the end of the command line. Assumes args[0] contains */
123 /* the original line, minus the command itself */
124 int skiplen= args[n]-args[1];
125 if (skiplen > strlen(args[0]))
127 return args[0]+skiplen;
130 int onecommand( char *line, WRBUF outbuff, const char *prevout);
132 /**************************************
133 * Simple support commands
136 int cmd_echo( char *args[], WRBUF outbuff)
138 wrbuf_printf(outbuff,"%s\n",restargs(args,1));
142 int cmd_quit( char *args[], WRBUF outbuff)
146 onecommand("zebra_close",outbuff,"");
151 onecommand("zebra_stop",outbuff,"");
154 wrbuf_puts(outbuff, "bye");
155 return -99; /* special stop signal */
158 /**************************************
159 * Tests for starting and stopping zebra, etc
162 static int cmd_help( char *args[], WRBUF outbuff);
164 static int cmd_zebra_start( char *args[], WRBUF outbuff)
167 if (!conf || !*conf) {
168 wrbuf_puts(outbuff,"no config file specified, using ");
169 wrbuf_puts(outbuff, default_config);
170 wrbuf_puts(outbuff, "\n");
173 zs=zebra_start(conf);
175 wrbuf_puts(outbuff, "zebra_start failed" );
181 static int cmd_zebra_stop( char *args[], WRBUF outbuff)
184 wrbuf_puts(outbuff,"zebra seems not to have been started, "
185 "stopping anyway\n");
191 static int cmd_zebra_open( char *args[], WRBUF outbuff)
194 wrbuf_puts(outbuff,"zebra seems not to have been started, "
196 zh = zebra_open(zs, 0);
200 static int cmd_zebra_close( char *args[], WRBUF outbuff)
203 wrbuf_puts(outbuff,"Seems like you have not called zebra_open,"
209 static int cmd_quickstart( char *args[], WRBUF outbuff)
214 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
216 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
217 sprintf(tmp, "yaz_log_level 0x%x", YLOG_DEFAULT_LEVEL | log_level );
219 rc=onecommand(tmp,outbuff,"");
220 yaz_log(log_level,"quickstart");
223 rc=onecommand("zebra_start",outbuff,"");
226 rc=onecommand("zebra_open",outbuff,"");
228 rc=onecommand("select_database Default",outbuff,"");
232 /**************************************
236 static int cmd_yaz_log_file( char *args[], WRBUF outbuff)
238 char *fn = defarg(args[1],0);
239 wrbuf_printf(outbuff, "sending yaz-log to %s\n",fn);
240 yaz_log_init_file(fn);
244 static int cmd_yaz_log_level( char *args[], WRBUF outbuff)
246 int lev = defargint(args[1],YLOG_DEFAULT_LEVEL);
247 wrbuf_printf(outbuff, "setting yaz-log to level %d (ox%x)\n",lev,lev);
248 yaz_log_init_level(lev);
252 static int cmd_yaz_log_prefix( char *args[], WRBUF outbuff)
254 char *pref = defarg(args[1],"ZebraSh");
255 wrbuf_printf(outbuff, "setting yaz-log prefix to %s\n",pref);
256 yaz_log_init_prefix(pref);
260 static int cmd_logf( char *args[], WRBUF outbuff)
262 int lev = defargint(args[1],0);
267 lev=YLOG_LOG; /* this is in the default set!*/
268 yaz_log( lev, restargs(args,i));
275 static int cmd_err ( char *args[], WRBUF outbuff)
277 wrbuf_printf(outbuff, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
279 zebra_errString (zh),
283 static int cmd_errcode ( char *args[], WRBUF outbuff)
285 wrbuf_printf(outbuff, "errCode: %d \n",
289 static int cmd_errstr ( char *args[], WRBUF outbuff)
291 wrbuf_printf(outbuff, "errStr: %s\n",
292 zebra_errString (zh));
295 static int cmd_erradd ( char *args[], WRBUF outbuff)
297 wrbuf_printf(outbuff, "errAdd: %s \n",
302 /**************************************
306 static int cmd_init ( char *args[], WRBUF outbuff)
312 static int cmd_select_database ( char *args[], WRBUF outbuff)
314 char *db=defarg(args[1],DEFAULTDATABASE);
315 wrbuf_printf(outbuff,"Selecting database '%s'\n",db);
316 return zebra_select_database(zh, db);
319 static int cmd_create_database( char *args[], WRBUF outbuff)
321 char *db=defarg(args[1],DEFAULTDATABASE);
322 wrbuf_printf(outbuff,"Creating database '%s'\n",db);
324 return zebra_create_database(zh, db);
327 static int cmd_drop_database( char *args[], WRBUF outbuff)
332 wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
333 return zebra_drop_database(zh, db);
336 static int cmd_begin_trans( char *args[], WRBUF outbuff)
339 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
341 return zebra_begin_trans(zh,rw);
344 static int cmd_end_trans( char *args[], WRBUF outbuff)
346 return zebra_end_trans(zh);
348 /*************************************
349 * Inserting and deleting
352 static int cmd_record_insert( char *args[], WRBUF outbuff)
356 char *rec=restargs(args,1);
358 rc = zebra_insert_record(zh,
368 wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
374 static int cmd_exchange_record( char *args[], WRBUF outbuff)
377 char *action = args[2];
379 char *rec=restargs(args,3);
380 if (!(id && action && args[4] ))
382 wrbuf_puts(outbuff,"Missing arguments!\n");
383 onecommand("help exchange_record", outbuff, "");
386 rc=zebra_admin_exchange_record(zh, rec, strlen(rec),
387 id, strlen(id), atoi(action));
391 /**********************************
392 * Searching and retrieving
395 static int cmd_search_pqf(char *args[], WRBUF outbuff)
399 char *qry = restargs(args,2);
401 rc = zebra_search_PQF(zh, qry, set, &hits);
403 wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
407 static int cmd_find( char *args[], WRBUF outbuff)
409 char *setname=DEFAULTRESULTSET;
412 WRBUF qry = wrbuf_alloc();
413 if (0==strstr(args[0],"@attr"))
414 wrbuf_puts(qry, "@attr 1=/ ");
415 wrbuf_puts(qry,restargs(args,1));
417 onecommand("quickstart", outbuff, "");
418 wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
419 rc = zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
422 wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
429 static int cmd_show( char *args[], WRBUF outbuff)
431 int start=defargint(args[1], nextrecno);
432 int nrecs=defargint(args[2],1);
433 char *setname=defarg(args[3],DEFAULTRESULTSET);
435 ZebraRetrievalRecord *recs;
437 Z_RecordComposition *pcomp=0;
441 odr=odr_createmem(ODR_ENCODE);
442 recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
443 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
444 format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
445 for (i=0;i<nrecs;i++)
446 recs[i].position=start+i;
448 rc = zebra_records_retrieve (zh, odr, setname,
449 pcomp, format, nrecs,recs);
452 for (i=0;i<nrecs;i++)
454 printf("Err %d: %d\n",i,recs[i].errCode);
457 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
458 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
459 wrbuf_puts(outbuff, "\n");
461 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
463 nextrecno=start+nrecs;
469 static int cmd_sort( char *args[], WRBUF outbuff)
474 Z_SortKeySpecList *spec=0;
475 const char * inpsets[]={ DEFAULTRESULTSET, 0};
476 /* FIXME - allow the user to specify result sets in/out */
478 odr=odr_createmem(ODR_ENCODE);
479 spec=yaz_sort_spec (odr, restargs(args,1));
483 rc=zebra_sort(zh, odr,
489 wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
496 * int bend_sort (void *handle, bend_sort_rr *rr)
498 * ZebraHandle zh = (ZebraHandle) handle;
500 * zebra_sort (zh, rr->stream,
501 * rr->num_input_setnames, (const char **)
502 * rr->input_setnames,
503 * rr->output_setname,
506 * zebra_result (zh, &rr->errcode,
513 /**************************************)
514 * Command table, parser, and help
522 int (*testfunc)(char *args[], WRBUF outbuff);
526 struct cmdstruct cmds[] = {
528 * if text is 0, does not list the command
529 * if cmd is "", adds the args (and newline) in command listing
531 { "", "Starting and stopping:", "", 0 },
534 "starts the zebra service. You need to call this first\n"
535 "if no configfile is given, assumes " DEFAULTCONFIG,
538 "stops the zebra service",
541 "starts a zebra session. Once you have called zebra_start\n"
542 "you can call zebra_open to start working",
545 "closes a zebra session",
547 { "quickstart", "[configfile]",
548 "Does a zebra_start, zebra_open, and sets up the log",
551 { "", "Log file:","", 0},
554 "Directs the log to filename (or stderr)",
558 "Sets the logging level (or returns to default)",
562 "Sets the log prefix",
566 "writes an entry in the log",
569 { "", "Error handling:","", 0},
571 "Displays zebra's error status (code, str, add)",
574 "Displays zebra's error code",
577 "Displays zebra's error string",
580 "Displays zebra's additional error message",
583 { "", "Admin:","", 0},
585 "Initializes the zebra database, destroying all data in it",
587 { "select_database", "basename",
588 "Selects a database",
589 cmd_select_database},
590 { "create_database", "basename",
592 cmd_create_database},
593 { "drop_database", "basename",
596 { "begin_trans", "[rw]",
597 "Begins a transaction. rw=1 means write, otherwise read-only",
600 "Ends a transaction",
603 { "","Updating:","",0},
604 { "record_insert","record",
605 "inserts an sgml record into Default",
607 { "exchange_record","database record-id action record",
608 "inserts (1), updates (2), or deletes (3) a record \n"
609 "record-id must be a unique identifier for the record",
610 cmd_exchange_record},
612 { "","Searching and retrieving:","",0},
613 { "search_pqf","setname query",
622 { "show","[start] [numrecs] [resultset]",
625 { "s","[start] [numrecs] [resultset]",
629 "sorts a result set. (example spec: 1=4 >)",
632 { "", "Misc:","", 0},
642 { "help", "[command]",
643 "Gives help on command, or lists them all",
645 { "", "help [command] gives more info on command", "",0 },
647 {0,0,0,0} /* end marker */
651 char *line, /* input line */
652 WRBUF outbuff, /* output goes here */
653 const char *prevout) /* prev output, for 'expect' */
656 char *args[MAX_NO_ARGS];
658 char argbuf[MAX_ARG_LEN];
659 yaz_log(log_level,"%s",line);
660 strncpy(argbuf,line, MAX_ARG_LEN-1);
661 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
662 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
663 nargs=split_args(argbuf, args);
666 for (i = 0; i <= n; i++)
668 const char *cp = args[i];
669 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
673 return -90; /* no command on line, too bad */
675 if (0==strcmp(args[0],"expect"))
678 if (nargs>1) /* args[0] is not yet set, can't use restargs */
679 rest= line + (args[1]-argbuf); /* rest of the line */
681 return -1; /* need something to expect */
682 if (0==strstr(prevout,rest))
684 printf( "Failed expectation, '%s' not found\n", rest);
689 for (i=0;cmds[i].cmd;i++)
690 if (0==strcmp(cmds[i].cmd, args[0]))
693 args[0]= line + (args[1]-argbuf); /* rest of the line */
696 return ((cmds[i].testfunc)(args,outbuff));
698 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
699 yaz_log(log_level,"Unknown command");
703 static int cmd_help( char *args[], WRBUF outbuff)
708 { /* help for a single command */
709 for (i=0;cmds[i].cmd;i++)
710 if (0==strcmp(cmds[i].cmd, args[1]))
712 wrbuf_printf(outbuff,"%s %s\n%s\n",
713 cmds[i].cmd, cmds[i].args,
714 cmds[i].explanation);
717 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
720 { /* list all commands */
722 for (i=0;cmds[i].cmd;i++)
725 { /* ordinary command */
728 wrbuf_puts(outbuff,"\n ");
731 linelen += strlen(cmds[i].cmd) + 2;
732 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
735 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
739 wrbuf_puts(outbuff,"\n");
744 /* If Zebra reports an error after an operation,
745 * append it to the outbuff and log it */
746 static void Zerrors (WRBUF outbuff)
751 ec=zebra_errCode (zh);
754 yaz_log(log_level, " Zebra error %d: %s, (%s)",
755 ec, zebra_errString (zh),
757 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
758 ec, zebra_errString (zh),
763 /**************************************
770 WRBUF outbuff=wrbuf_alloc();
771 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
772 wrbuf_puts(outbuff,"Zebrash at your service");
776 char buf[MAX_ARG_LEN];
778 #if HAVE_READLINE_READLINE_H
780 line_in=readline(PROMPT);
783 #if HAVE_READLINE_HISTORY_H
785 add_history(line_in);
789 /* line_in != NULL if readine is present and input is a tty */
795 if(strlen(line_in) > MAX_ARG_LEN-1) {
796 fprintf(stderr,"Input line too long\n");
804 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
808 /* get rid of \n in line */
809 if ((nl_cp = strchr(buf, '\n')))
811 strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
812 wrbuf_rewind(outbuff);
813 rc=onecommand(buf, outbuff, prevout);
816 wrbuf_puts(outbuff, " OK\n");
817 yaz_log(log_level, "OK");
821 wrbuf_printf(outbuff, " command returned %d\n",rc);
824 printf("%s\n", wrbuf_buf(outbuff));
826 wrbuf_free(outbuff,1);
833 printf ("zebrash [-c config]\n");
836 /**************************************
840 int main (int argc, char ** argv)
844 while ((ret = options ("c:h", argv, argc, &arg)) != -2)
849 default_config = arg;
853 /* FIXME - handle -v */
855 fprintf(stderr, "bad option %s\n", arg);
859 log_level=yaz_log_module_level("zebrash");
867 * indent-tabs-mode: nil
869 * vim: shiftwidth=4 tabstop=8 expandtab