1 /* $Id: zebrash.c,v 1.17 2003-07-04 14:00:22 heikki 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)
136 wrbuf_puts(outbuff, "bye");
137 return -99; /* special stop signal */
140 /**************************************
141 * Tests for starting and stopping zebra, etc
144 static int cmd_help( char *args[], WRBUF outbuff);
146 static int cmd_zebra_start( char *args[], WRBUF outbuff)
149 if (!conf || !*conf) {
150 wrbuf_puts(outbuff,"no config file specified, using "
151 DEFAULTCONFIG "\n" );
154 zs=zebra_start(conf);
156 wrbuf_puts(outbuff, "zebra_start failed" );
162 static int cmd_zebra_stop( char *args[], WRBUF outbuff)
165 wrbuf_puts(outbuff,"zebra seems not to have been started, "
166 "stopping anyway\n");
172 static int cmd_zebra_open( char *args[], WRBUF outbuff)
175 wrbuf_puts(outbuff,"zebra seems not to have been started, "
181 static int cmd_zebra_close( char *args[], WRBUF outbuff)
184 wrbuf_puts(outbuff,"Seems like you have not called zebra_open,"
190 static int cmd_quickstart( char *args[], WRBUF outbuff)
195 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
197 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
198 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
200 rc=onecommand(tmp,outbuff,"");
201 logf(LOG_APP,"quickstart");
204 rc=onecommand("zebra_start",outbuff,"");
207 rc=onecommand("zebra_open",outbuff,"");
209 rc=onecommand("select_database Default",outbuff,"");
213 /**************************************
217 static int cmd_yaz_log_file( char *args[], WRBUF outbuff)
219 char *fn = defarg(args[1],0);
220 wrbuf_printf(outbuff, "sending yaz-log to %s\n",fn);
221 yaz_log_init_file(fn);
225 static int cmd_yaz_log_level( char *args[], WRBUF outbuff)
227 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
228 wrbuf_printf(outbuff, "setting yaz-log to level %d (ox%x)\n",lev,lev);
229 yaz_log_init_level(lev);
233 static int cmd_yaz_log_prefix( char *args[], WRBUF outbuff)
235 char *pref = defarg(args[1],"ZebraSh");
236 wrbuf_printf(outbuff, "setting yaz-log prefix to %s\n",pref);
237 yaz_log_init_prefix(pref);
241 static int cmd_logf( char *args[], WRBUF outbuff)
243 int lev = defargint(args[1],0);
248 lev=LOG_LOG; /* this is in the default set!*/
249 logf( lev, restargs(args,i));
256 static int cmd_err ( char *args[], WRBUF outbuff)
258 wrbuf_printf(outbuff, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
260 zebra_errString (zh),
264 static int cmd_errcode ( char *args[], WRBUF outbuff)
266 wrbuf_printf(outbuff, "errCode: %d \n",
270 static int cmd_errstr ( char *args[], WRBUF outbuff)
272 wrbuf_printf(outbuff, "errStr: %s\n",
273 zebra_errString (zh));
276 static int cmd_erradd ( char *args[], WRBUF outbuff)
278 wrbuf_printf(outbuff, "errAdd: %s \n",
283 /**************************************
287 static int cmd_init ( char *args[], WRBUF outbuff)
293 static int cmd_select_database ( char *args[], WRBUF outbuff)
295 char *db=defarg(args[1],DEFAULTDATABASE);
296 wrbuf_puts(outbuff,"Selecting database 'Default'\n");
297 return zebra_select_database(zh, db);
300 static int cmd_create_database( char *args[], WRBUF outbuff)
302 char *db=defarg(args[1],DEFAULTDATABASE);
303 wrbuf_printf(outbuff,"Creating database '%s'\n",db);
305 return zebra_create_database(zh, db);
308 static int cmd_drop_database( char *args[], WRBUF outbuff)
313 wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
314 return zebra_drop_database(zh, db);
317 static int cmd_begin_trans( char *args[], WRBUF outbuff)
320 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
322 return zebra_begin_trans(zh,rw);
325 static int cmd_end_trans( char *args[], WRBUF outbuff)
327 return zebra_end_trans(zh);
329 /*************************************
330 * Inserting and deleting
333 static int cmd_record_insert( char *args[], WRBUF outbuff)
337 char *rec=restargs(args,1);
339 rc=zebra_record_insert(zh,rec, strlen(rec), &sysno);
342 wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
348 static int cmd_exchange_record( char *args[], WRBUF outbuff)
352 char *action = args[3];
354 char *rec=restargs(args,4);
355 if (!(base && id && action && args[4] ))
357 wrbuf_puts(outbuff,"Missing arguments!\n");
358 onecommand("help exchange_record", outbuff, "");
361 rc=zebra_admin_exchange_record(zh, base, rec, strlen(rec),
362 id, strlen(id), atoi(action));
366 /**********************************
367 * Searching and retrieving
370 static int cmd_search_pqf( char *args[], WRBUF outbuff)
374 char *qry=restargs(args,2);
376 rc=zebra_search_PQF(zh, qry, set, &hits);
378 wrbuf_printf(outbuff,"%d hits found\n",hits);
382 static int cmd_find( char *args[], WRBUF outbuff)
384 char *setname=DEFAULTRESULTSET;
387 WRBUF qry=wrbuf_alloc();
388 if (0==strstr(args[0],"@attr"))
389 wrbuf_puts(qry, "@attr 1=/ ");
390 wrbuf_puts(qry,restargs(args,1));
392 onecommand("quickstart", outbuff, "");
393 wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
394 rc=zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
397 wrbuf_printf(outbuff,"%d hits found\n",hits);
404 static int cmd_show( char *args[], WRBUF outbuff)
406 int start=defargint(args[1], nextrecno);
407 int nrecs=defargint(args[2],1);
408 char *setname=defarg(args[3],DEFAULTRESULTSET);
411 Z_RecordComposition *pcomp=0;
415 odr=odr_createmem(ODR_ENCODE);
416 ZebraRetrievalRecord *recs=
417 odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
418 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
419 format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
420 for (i=0;i<nrecs;i++)
421 recs[i].position=start+i;
423 rc = zebra_records_retrieve (zh, odr, setname,
424 pcomp, format, nrecs,recs);
425 printf("rc=%d\n",rc);
428 for (i=0;i<nrecs;i++)
430 printf("Err %d: %d\n",i,recs[i].errCode);
433 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
434 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
435 wrbuf_puts(outbuff, "\n");
437 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
439 nextrecno=start+nrecs+1;
443 /**************************************)
444 * Command table, parser, and help
452 int (*testfunc)(char *args[], WRBUF outbuff);
456 struct cmdstruct cmds[] = {
458 * if text is 0, does not list the command
459 * if cmd is "", adds the args (and newline) in command listing
461 { "", "Starting and stopping:", "", 0 },
464 "starts the zebra service. You need to call this first\n"
465 "if no configfile is given, assumes " DEFAULTCONFIG,
468 "stops the zebra service",
471 "starts a zebra session. Once you have called zebra_start\n"
472 "you can call zebra_open to start working",
475 "closes a zebra session",
477 { "quickstart", "[configfile]",
478 "Does a zebra_start, zebra_open, and sets up the log",
481 { "", "Log file:","", 0},
484 "Directs the log to filename (or stderr)",
488 "Sets the logging level (or returns to default)",
492 "Sets the log prefix",
496 "writes an entry in the log",
499 { "", "Error handling:","", 0},
501 "Displays zebra's error status (code, str, add)",
504 "Displays zebra's error code",
507 "Displays zebra's error string",
510 "Displays zebra's additional error message",
513 { "", "Admin:","", 0},
515 "Initializes the zebra database, destroying all data in it",
517 { "select_database", "basename",
518 "Selects a database",
519 cmd_select_database},
520 { "create_database", "basename",
522 cmd_create_database},
523 { "drop_database", "basename",
526 { "begin_trans", "[rw]",
527 "Begins a transaction. rw=1 means write, otherwise read-only",
530 "Ends a transaction",
533 { "","Updating:","",0},
534 { "record_insert","record",
535 "inserts an sgml record into Default",
537 { "exchange_record","database record-id action record",
538 "inserts (1), updates (2), or deletes (3) a record \n"
539 "record-id must be a unique identifier for the record",
540 cmd_exchange_record},
541 { "","Searching and retrieving:","",0},
542 { "search_pqf","setname query",
551 { "show","[start] [numrecs] [resultset]",
554 { "s","[start] [numrecs] [resultset]",
557 { "", "Misc:","", 0},
567 { "help", "[command]",
568 "Gives help on command, or lists them all",
570 { "", "help [command] gives more info on command", "",0 },
572 {0,0,0,0} /* end marker */
576 char *line, /* input line */
577 WRBUF outbuff, /* output goes here */
578 const char *prevout) /* prev output, for 'expect' */
581 char *args[MAX_NO_ARGS];
583 char argbuf[MAX_ARG_LEN];
584 logf(LOG_APP,"%s",line);
585 strncpy(argbuf,line, MAX_ARG_LEN-1);
586 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
587 /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
588 nargs=split_args(argbuf, args);
591 for (i = 0; i <= n; i++)
593 const char *cp = args[i];
594 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
598 return -90; /* no command on line, too bad */
600 if (0==strcmp(args[0],"expect"))
603 if (nargs>1) /* args[0] is not yet set, can't use restargs */
604 rest= line + (args[1]-argbuf); /* rest of the line */
606 return -1; /* need something to expect */
607 if (0==strstr(prevout,rest))
609 printf( "Failed expectation, '%s' not found\n", rest);
614 for (i=0;cmds[i].cmd;i++)
615 if (0==strcmp(cmds[i].cmd, args[0]))
618 args[0]= line + (args[1]-argbuf); /* rest of the line */
621 return ((cmds[i].testfunc)(args,outbuff));
623 wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
624 logf(LOG_APP,"Unknown command");
628 static int cmd_help( char *args[], WRBUF outbuff)
633 { /* help for a single command */
634 for (i=0;cmds[i].cmd;i++)
635 if (0==strcmp(cmds[i].cmd, args[1]))
637 wrbuf_printf(outbuff,"%s %s\n%s\n",
638 cmds[i].cmd, cmds[i].args,
639 cmds[i].explanation);
642 wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
645 { /* list all commands */
647 for (i=0;cmds[i].cmd;i++)
650 { /* ordinary command */
653 wrbuf_puts(outbuff,"\n ");
656 linelen += strlen(cmds[i].cmd) + 2;
657 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
660 wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
664 wrbuf_puts(outbuff,"\n");
669 /* If Zebra reports an error after an operation,
670 * append it to the outbuff and log it */
671 static void Zerrors ( WRBUF outbuff)
676 ec=zebra_errCode (zh);
679 logf(LOG_APP, " Zebra error %d: %s, (%s)",
680 ec, zebra_errString (zh),
682 wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
683 ec, zebra_errString (zh),
685 zebra_clearError(zh);
689 /**************************************
696 WRBUF outbuff=wrbuf_alloc();
697 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
698 wrbuf_puts(outbuff,"Zebrash at your service");
701 char buf[MAX_ARG_LEN];
702 #if HAVE_READLINE_READLINE_H
704 line_in=readline(PROMPT);
707 #if HAVE_READLINE_HISTORY_H
709 add_history(line_in);
711 if(strlen(line_in) > MAX_ARG_LEN-1) {
712 fprintf(stderr,"Input line too long\n");
720 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
724 strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
725 wrbuf_rewind(outbuff);
726 rc=onecommand(buf, outbuff, prevout);
729 wrbuf_puts(outbuff, " OK\n");
734 wrbuf_printf(outbuff, " command returned %d\n",rc);
737 printf("%s\n", wrbuf_buf(outbuff));
742 /**************************************
746 int main (int argc, char ** args)