1 /* $Id: zebrash.c,v 1.21 2003-09-23 09:53:08 adam Exp $
2 Copyright (C) 2002,2003
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
32 #if HAVE_READLINE_READLINE_H
33 #include <readline/readline.h>
35 #if HAVE_READLINE_HISTORY_H
36 #include <readline/history.h>
41 #include <yaz/proto.h>
42 #include <yaz/wrbuf.h>
44 #define MAX_NO_ARGS 32
45 #define MAX_OUT_BUFF 4096
46 #define MAX_ARG_LEN 1024
47 #define PROMPT "ZebraSh>"
48 #define DEFAULTCONFIG "./zebra.cfg"
49 #define DEFAULTDATABASE "Default"
50 #define DEFAULTRESULTSET "MyResultSet"
53 /**************************************
54 * Global variables (yuck!)
57 ZebraService zs=0; /* our global handle to zebra */
58 ZebraHandle zh=0; /* the current session */
59 /* time being, only one session works */
60 int nextrecno=1; /* record number to show next */
62 /**************************************
67 static int split_args( char *line, char** args )
68 { /* splits line into individual null-terminated strings,
69 * returns pointers to them in args */
70 /* FIXME - do we need to handle quoted args ?? */
74 args[0]=0; /* by default */
75 while (*p==' ' || *p=='\t' || *p=='\n')
79 while (*p==' ' || *p=='\t' || *p=='\n')
81 if (*p=='#') /* skip comments */
85 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
95 static char *defarg( char *arg, char *def )
103 static int defargint( char *arg, int def )
106 char *a=defarg(arg,0);
112 static char *restargs( char *args[], int n)
113 { /* Returns the rest of the arguments, starting at the nth, */
114 /* to the end of the command line. Assumes args[0] contains */
115 /* the original line, minus the command itself */
116 int skiplen= args[n]-args[1];
117 if (skiplen > strlen(args[0]))
119 return args[0]+skiplen;
122 int onecommand( char *line, WRBUF outbuff, const char *prevout);
124 /**************************************
125 * Simple support commands
128 int cmd_echo( char *args[], WRBUF outbuff)
130 wrbuf_printf(outbuff,"%s\n",restargs(args,1));
134 int cmd_quit( char *args[], WRBUF outbuff)
138 onecommand("zebra_close",outbuff,"");
143 onecommand("zebra_stop",outbuff,"");
146 wrbuf_puts(outbuff, "bye");
147 return -99; /* special stop signal */
150 /**************************************
151 * Tests for starting and stopping zebra, etc
154 static int cmd_help( char *args[], WRBUF outbuff);
156 static int cmd_zebra_start( char *args[], WRBUF outbuff)
159 if (!conf || !*conf) {
160 wrbuf_puts(outbuff,"no config file specified, using "
161 DEFAULTCONFIG "\n" );
164 zs=zebra_start(conf);
166 wrbuf_puts(outbuff, "zebra_start failed" );
172 static int cmd_zebra_stop( char *args[], WRBUF outbuff)
175 wrbuf_puts(outbuff,"zebra seems not to have been started, "
176 "stopping anyway\n");
182 static int cmd_zebra_open( char *args[], WRBUF outbuff)
185 wrbuf_puts(outbuff,"zebra seems not to have been started, "
191 static int cmd_zebra_close( char *args[], WRBUF outbuff)
194 wrbuf_puts(outbuff,"Seems like you have not called zebra_open,"
200 static int cmd_quickstart( char *args[], WRBUF outbuff)
205 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
207 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
208 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
210 rc=onecommand(tmp,outbuff,"");
211 logf(LOG_APP,"quickstart");
214 rc=onecommand("zebra_start",outbuff,"");
217 rc=onecommand("zebra_open",outbuff,"");
219 rc=onecommand("select_database Default",outbuff,"");
223 /**************************************
227 static int cmd_yaz_log_file( char *args[], WRBUF outbuff)
229 char *fn = defarg(args[1],0);
230 wrbuf_printf(outbuff, "sending yaz-log to %s\n",fn);
231 yaz_log_init_file(fn);
235 static int cmd_yaz_log_level( char *args[], WRBUF outbuff)
237 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
238 wrbuf_printf(outbuff, "setting yaz-log to level %d (ox%x)\n",lev,lev);
239 yaz_log_init_level(lev);
243 static int cmd_yaz_log_prefix( char *args[], WRBUF outbuff)
245 char *pref = defarg(args[1],"ZebraSh");
246 wrbuf_printf(outbuff, "setting yaz-log prefix to %s\n",pref);
247 yaz_log_init_prefix(pref);
251 static int cmd_logf( char *args[], WRBUF outbuff)
253 int lev = defargint(args[1],0);
258 lev=LOG_LOG; /* this is in the default set!*/
259 logf( lev, restargs(args,i));
266 static int cmd_err ( char *args[], WRBUF outbuff)
268 wrbuf_printf(outbuff, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
270 zebra_errString (zh),
274 static int cmd_errcode ( char *args[], WRBUF outbuff)
276 wrbuf_printf(outbuff, "errCode: %d \n",
280 static int cmd_errstr ( char *args[], WRBUF outbuff)
282 wrbuf_printf(outbuff, "errStr: %s\n",
283 zebra_errString (zh));
286 static int cmd_erradd ( char *args[], WRBUF outbuff)
288 wrbuf_printf(outbuff, "errAdd: %s \n",
293 /**************************************
297 static int cmd_init ( char *args[], WRBUF outbuff)
303 static int cmd_select_database ( char *args[], WRBUF outbuff)
305 char *db=defarg(args[1],DEFAULTDATABASE);
306 wrbuf_puts(outbuff,"Selecting database 'Default'\n");
307 return zebra_select_database(zh, db);
310 static int cmd_create_database( char *args[], WRBUF outbuff)
312 char *db=defarg(args[1],DEFAULTDATABASE);
313 wrbuf_printf(outbuff,"Creating database '%s'\n",db);
315 return zebra_create_database(zh, db);
318 static int cmd_drop_database( char *args[], WRBUF outbuff)
323 wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
324 return zebra_drop_database(zh, db);
327 static int cmd_begin_trans( char *args[], WRBUF outbuff)
330 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
332 return zebra_begin_trans(zh,rw);
335 static int cmd_end_trans( char *args[], WRBUF outbuff)
337 return zebra_end_trans(zh);
339 /*************************************
340 * Inserting and deleting
343 static int cmd_record_insert( char *args[], WRBUF outbuff)
347 char *rec=restargs(args,1);
349 rc=zebra_record_insert(zh,rec, strlen(rec), &sysno);
352 wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
358 static int cmd_exchange_record( char *args[], WRBUF outbuff)
362 char *action = args[3];
364 char *rec=restargs(args,4);
365 if (!(base && id && action && args[4] ))
367 wrbuf_puts(outbuff,"Missing arguments!\n");
368 onecommand("help exchange_record", outbuff, "");
371 rc=zebra_admin_exchange_record(zh, base, rec, strlen(rec),
372 id, strlen(id), atoi(action));
376 /**********************************
377 * Searching and retrieving
380 static int cmd_search_pqf( char *args[], WRBUF outbuff)
384 char *qry=restargs(args,2);
386 rc=zebra_search_PQF(zh, qry, set, &hits);
388 wrbuf_printf(outbuff,"%d hits found\n",hits);
392 static int cmd_find( char *args[], WRBUF outbuff)
394 char *setname=DEFAULTRESULTSET;
397 WRBUF qry=wrbuf_alloc();
398 if (0==strstr(args[0],"@attr"))
399 wrbuf_puts(qry, "@attr 1=/ ");
400 wrbuf_puts(qry,restargs(args,1));
402 onecommand("quickstart", outbuff, "");
403 wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
404 rc=zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
407 wrbuf_printf(outbuff,"%d hits found\n",hits);
414 static int cmd_show( char *args[], WRBUF outbuff)
416 int start=defargint(args[1], nextrecno);
417 int nrecs=defargint(args[2],1);
418 char *setname=defarg(args[3],DEFAULTRESULTSET);
420 ZebraRetrievalRecord *recs;
422 Z_RecordComposition *pcomp=0;
426 odr=odr_createmem(ODR_ENCODE);
427 recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
428 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
429 format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
430 for (i=0;i<nrecs;i++)
431 recs[i].position=start+i;
433 rc = zebra_records_retrieve (zh, odr, setname,
434 pcomp, format, nrecs,recs);
437 for (i=0;i<nrecs;i++)
439 printf("Err %d: %d\n",i,recs[i].errCode);
442 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
443 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
444 wrbuf_puts(outbuff, "\n");
446 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
448 nextrecno=start+nrecs+1;
453 /**************************************)
454 * Command table, parser, and help
462 int (*testfunc)(char *args[], WRBUF outbuff);
466 struct cmdstruct cmds[] = {
468 * if text is 0, does not list the command
469 * if cmd is "", adds the args (and newline) in command listing
471 { "", "Starting and stopping:", "", 0 },
474 "starts the zebra service. You need to call this first\n"
475 "if no configfile is given, assumes " DEFAULTCONFIG,
478 "stops the zebra service",
481 "starts a zebra session. Once you have called zebra_start\n"
482 "you can call zebra_open to start working",
485 "closes a zebra session",
487 { "quickstart", "[configfile]",
488 "Does a zebra_start, zebra_open, and sets up the log",
491 { "", "Log file:","", 0},
494 "Directs the log to filename (or stderr)",
498 "Sets the logging level (or returns to default)",
502 "Sets the log prefix",
506 "writes an entry in the log",
509 { "", "Error handling:","", 0},
511 "Displays zebra's error status (code, str, add)",
514 "Displays zebra's error code",
517 "Displays zebra's error string",
520 "Displays zebra's additional error message",
523 { "", "Admin:","", 0},
525 "Initializes the zebra database, destroying all data in it",
527 { "select_database", "basename",
528 "Selects a database",
529 cmd_select_database},
530 { "create_database", "basename",
532 cmd_create_database},
533 { "drop_database", "basename",
536 { "begin_trans", "[rw]",
537 "Begins a transaction. rw=1 means write, otherwise read-only",
540 "Ends a transaction",
543 { "","Updating:","",0},
544 { "record_insert","record",
545 "inserts an sgml record into Default",
547 { "exchange_record","database record-id action record",
548 "inserts (1), updates (2), or deletes (3) a record \n"
549 "record-id must be a unique identifier for the record",
550 cmd_exchange_record},
551 { "","Searching and retrieving:","",0},
552 { "search_pqf","setname query",
561 { "show","[start] [numrecs] [resultset]",
564 { "s","[start] [numrecs] [resultset]",
567 { "", "Misc:","", 0},
577 { "help", "[command]",
578 "Gives help on command, or lists them all",
580 { "", "help [command] gives more info on command", "",0 },
582 {0,0,0,0} /* end marker */
586 char *line, /* input line */
587 WRBUF outbuff, /* output goes here */
588 const char *prevout) /* prev output, for 'expect' */
591 char *args[MAX_NO_ARGS];
593 char argbuf[MAX_ARG_LEN];
594 logf(LOG_APP,"%s",line);
595 strncpy(argbuf,line, MAX_ARG_LEN-1);
596 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
597 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
598 nargs=split_args(argbuf, args);
601 for (i = 0; i <= n; i++)
603 const char *cp = args[i];
604 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
608 return -90; /* no command on line, too bad */
610 if (0==strcmp(args[0],"expect"))
613 if (nargs>1) /* args[0] is not yet set, can't use restargs */
614 rest= line + (args[1]-argbuf); /* rest of the line */
616 return -1; /* need something to expect */
617 if (0==strstr(prevout,rest))
619 printf( "Failed expectation, '%s' not found\n", rest);
624 for (i=0;cmds[i].cmd;i++)
625 if (0==strcmp(cmds[i].cmd, args[0]))
628 args[0]= line + (args[1]-argbuf); /* rest of the line */
631 return ((cmds[i].testfunc)(args,outbuff));
633 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
634 logf(LOG_APP,"Unknown command");
638 static int cmd_help( char *args[], WRBUF outbuff)
643 { /* help for a single command */
644 for (i=0;cmds[i].cmd;i++)
645 if (0==strcmp(cmds[i].cmd, args[1]))
647 wrbuf_printf(outbuff,"%s %s\n%s\n",
648 cmds[i].cmd, cmds[i].args,
649 cmds[i].explanation);
652 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
655 { /* list all commands */
657 for (i=0;cmds[i].cmd;i++)
660 { /* ordinary command */
663 wrbuf_puts(outbuff,"\n ");
666 linelen += strlen(cmds[i].cmd) + 2;
667 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
670 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
674 wrbuf_puts(outbuff,"\n");
679 /* If Zebra reports an error after an operation,
680 * append it to the outbuff and log it */
681 static void Zerrors ( WRBUF outbuff)
686 ec=zebra_errCode (zh);
689 logf(LOG_APP, " Zebra error %d: %s, (%s)",
690 ec, zebra_errString (zh),
692 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
693 ec, zebra_errString (zh),
695 zebra_clearError(zh);
699 /**************************************
706 WRBUF outbuff=wrbuf_alloc();
707 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
708 wrbuf_puts(outbuff,"Zebrash at your service");
711 char buf[MAX_ARG_LEN];
712 #if HAVE_READLINE_READLINE_H
714 line_in=readline(PROMPT);
717 #if HAVE_READLINE_HISTORY_H
719 add_history(line_in);
721 if(strlen(line_in) > MAX_ARG_LEN-1) {
722 fprintf(stderr,"Input line too long\n");
730 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
734 strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
735 wrbuf_rewind(outbuff);
736 rc=onecommand(buf, outbuff, prevout);
739 wrbuf_puts(outbuff, " OK\n");
744 wrbuf_printf(outbuff, " command returned %d\n",rc);
747 printf("%s\n", wrbuf_buf(outbuff));
749 wrbuf_free(outbuff,1);
753 /**************************************
757 int main (int argc, char ** args)