1 /* $Id: zebrash.c,v 1.16 2003-07-03 16:16: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>
43 #define MAX_NO_ARGS 32
44 #define MAX_OUT_BUFF 4096
45 #define MAX_ARG_LEN 1024
46 #define PROMPT "ZebraSh>"
47 #define DEFAULTCONFIG "./zebra.cfg"
48 #define DEFAULTRESULTSET "MyResultSet"
50 /**************************************
51 * Global variables (yuck!)
54 ZebraService zs=0; /* our global handle to zebra */
55 ZebraHandle zh=0; /* the current session */
56 /* time being, only one session works */
57 int nextrecno=0; /* record number to show next */
59 /**************************************
64 static int split_args( char *line, char** args )
65 { /* splits line into individual null-terminated strings,
66 * returns pointers to them in args */
67 /* FIXME - do we need to handle quoted args ?? */
71 args[0]=0; /* by default */
72 while (*p==' ' || *p=='\t' || *p=='\n')
76 while (*p==' ' || *p=='\t' || *p=='\n')
78 if (*p=='#') /* skip comments */
82 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
92 static char *defarg( char *arg, char *def )
100 static int defargint( char *arg, int def )
103 char *a=defarg(arg,0);
109 int onecommand( char *line, char *outbuff, const char *prevout);
111 /**************************************
112 * Simple support commands
115 int cmd_echo( char *args[], char *outbuff)
117 strcpy(outbuff, args[0]);
121 int cmd_quit( char *args[], char *outbuff)
123 strcpy(outbuff, "bye");
124 return -99; /* special stop signal */
127 /**************************************
128 * Tests for starting and stopping zebra, etc
131 static int cmd_help( char *args[], char *outbuff);
133 static int cmd_zebra_start( char *args[], char *outbuff)
136 if (!conf || !*conf) {
137 strcat(outbuff,"no config file specified, using "
138 DEFAULTCONFIG "\n" );
141 zs=zebra_start(conf);
143 strcpy(outbuff, "zebra_start failed" );
149 static int cmd_zebra_stop( char *args[], char *outbuff)
152 strcat(outbuff,"zebra seems not to have been started, "
153 "stopping anyway\n");
159 static int cmd_zebra_open( char *args[], char *outbuff)
162 strcat(outbuff,"zebra seems not to have been started, "
168 static int cmd_zebra_close( char *args[], char *outbuff)
171 strcat(outbuff,"Seems like you have not called zebra_open,"
177 static int cmd_quickstart( char *args[], char *outbuff)
182 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
184 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
185 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
187 rc=onecommand(tmp,outbuff,"");
188 logf(LOG_APP,"quickstart");
191 rc=onecommand("zebra_start",outbuff,"");
194 rc=onecommand("zebra_open",outbuff,"");
196 rc=onecommand("select_database Default",outbuff,"");
200 /**************************************
204 static int cmd_yaz_log_file( char *args[], char *outbuff)
206 char *fn = defarg(args[1],0);
208 sprintf(tmp, "sending yaz-log to %s\n",fn);
209 strcat(outbuff, tmp);
210 yaz_log_init_file(fn);
214 static int cmd_yaz_log_level( char *args[], char *outbuff)
216 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
218 sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
219 strcat(outbuff, tmp);
220 yaz_log_init_level(lev);
224 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
226 char *pref = defarg(args[1],"ZebraSh");
228 sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
229 strcat(outbuff, tmp);
230 yaz_log_init_prefix(pref);
234 static int cmd_logf( char *args[], char *outbuff)
236 int lev = defargint(args[1],0);
237 char tmp[MAX_OUT_BUFF]="";
242 lev=LOG_LOG; /* this is in the default set!*/
245 strcat(tmp, args[i++]);
255 static int cmd_err ( char *args[], char *outbuff)
257 char tmp[MAX_OUT_BUFF];
258 sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
260 zebra_errString (zh),
262 strcat(outbuff, tmp);
265 static int cmd_errcode ( char *args[], char *outbuff)
267 char tmp[MAX_OUT_BUFF];
268 sprintf(tmp, "errCode: %d \n",
270 strcat(outbuff, tmp);
273 static int cmd_errstr ( char *args[], char *outbuff)
275 char tmp[MAX_OUT_BUFF];
276 sprintf(tmp, "errStr: %s\n",
277 zebra_errString (zh));
278 strcat(outbuff, tmp);
281 static int cmd_erradd ( char *args[], char *outbuff)
283 char tmp[MAX_OUT_BUFF];
284 sprintf(tmp, "errAdd: %s \n",
286 strcat(outbuff, tmp);
290 /**************************************
294 static int cmd_init ( char *args[], char *outbuff)
300 static int cmd_select_database ( char *args[], char *outbuff)
305 strcat(outbuff,"Selecting database 'Default'\n");
307 return zebra_select_database(zh, db);
310 static int cmd_create_database( char *args[], char *outbuff)
315 strcat(outbuff,"Creating database ");
317 strcat(outbuff,"\n");
319 return zebra_create_database(zh, db);
322 static int cmd_drop_database( char *args[], char *outbuff)
327 strcat(outbuff,"Dropping database ");
329 strcat(outbuff,"\n");
331 return zebra_drop_database(zh, db);
334 static int cmd_begin_trans( char *args[], char *outbuff)
337 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
339 return zebra_begin_trans(zh,rw);
341 static int cmd_end_trans( char *args[], char *outbuff)
343 return zebra_end_trans(zh);
345 /*************************************
346 * Inserting and deleting
349 static int cmd_record_insert( char *args[], char *outbuff)
352 char buf[MAX_ARG_LEN];
360 strcat(buf, args[i++]);
363 rc=zebra_record_insert(zh,buf, strlen(buf), &sysno);
366 sprintf(buf,"ok sysno=%d\n",sysno);
373 static int cmd_exchange_record( char *args[], char *outbuff)
377 char *action = args[3];
380 char buf[MAX_ARG_LEN];
381 if (!(base && id && action && args[4] ))
383 strcat(outbuff,"Missing arguments!\n");
384 onecommand("help exchange_record", outbuff, "");
389 strcat(buf, args[i++]);
392 rc=zebra_admin_exchange_record(zh, base, buf, strlen(buf),
393 id, strlen(id), atoi(action));
397 /**********************************
398 * Searching and retrieving
401 static int cmd_search_pqf( char *args[], char *outbuff)
405 char qry[MAX_ARG_LEN]="";
410 strcat(qry, args[i++]);
413 rc=zebra_search_PQF(zh, qry, set, &hits);
416 sprintf(qry,"%d hits found\n",hits);
422 static int cmd_find( char *args[], char *outbuff)
424 char *setname=DEFAULTRESULTSET;
425 char qry[MAX_ARG_LEN]="";
429 if (0==strstr(args[0],"@attr"))
430 strcat(qry, "@attr 1=/ ");
433 strcat(qry, args[i++]);
437 onecommand("quickstart", outbuff, "");
438 strcat(outbuff, "find ");
439 strcat(outbuff, qry);
440 strcat(outbuff, "\n");
441 rc=zebra_search_PQF(zh, qry, setname, &hits);
444 sprintf(qry,"%d hits found\n",hits);
451 static int cmd_show( char *args[], char *outbuff)
453 int start=defargint(args[1], nextrecno);
454 int nrecs=defargint(args[2],1);
455 char *setname=defarg(args[3],DEFAULTRESULTSET);
458 Z_RecordComposition *pcomp=0;
461 ZebraRetrievalRecord recs;
462 odr=odr_createmem(ODR_ENCODE);
463 rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
464 printf("rc1=%d\n",rc);
465 format=oid_getvalbyname ("xml"); /*FIXME*/
467 rc = zebra_records_retrieve (zh, odr, setname,
468 pcomp, format, nrecs, &recs);
473 Z_RecordComposition *comp,
474 oid_value input_format,
476 ZebraRetrievalRecord *recs);
482 /**************************************)
483 * Command table, parser, and help
491 int (*testfunc)(char *args[], char *outbuff);
495 struct cmdstruct cmds[] = {
497 * if text is 0, does not list the command
498 * if cmd is "", adds the args (and newline) in command listing
500 { "", "Starting and stopping:", "", 0 },
503 "starts the zebra service. You need to call this first\n"
504 "if no configfile is given, assumes " DEFAULTCONFIG,
507 "stops the zebra service",
510 "starts a zebra session. Once you have called zebra_start\n"
511 "you can call zebra_open to start working",
514 "closes a zebra session",
516 { "quickstart", "[configfile]",
517 "Does a zebra_start, zebra_open, and sets up the log",
520 { "", "Log file:","", 0},
523 "Directs the log to filename (or stderr)",
527 "Sets the logging level (or returns to default)",
531 "Sets the log prefix",
535 "writes an entry in the log",
538 { "", "Error handling:","", 0},
540 "Displays zebra's error status (code, str, add)",
543 "Displays zebra's error code",
546 "Displays zebra's error string",
549 "Displays zebra's additional error message",
552 { "", "Admin:","", 0},
554 "Initializes the zebra database, destroying all data in it",
556 { "select_database", "basename",
557 "Selects a database",
558 cmd_select_database},
559 { "create_database", "basename",
561 cmd_create_database},
562 { "drop_database", "basename",
565 { "begin_trans", "[rw]",
566 "Begins a transaction. rw=1 means write, otherwise read-only",
569 "Ends a transaction",
572 { "","Updating:","",0},
573 { "record_insert","record",
574 "inserts an sgml record into Default",
576 { "exchange_record","database record-id action record",
577 "inserts (1), updates (2), or deletes (3) a record \n"
578 "record-id must be a unique identifier for the record",
579 cmd_exchange_record},
580 { "","Searching and retrieving:","",0},
581 { "search_pqf","setname query",
590 { "show","[start] [numrecs] [resultset]",
593 { "s","[start] [numrecs] [resultset]",
596 { "", "Misc:","", 0},
606 { "help", "[command]",
607 "Gives help on command, or lists them all",
609 { "", "help [command] gives more info on command", "",0 },
611 {0,0,0,0} /* end marker */
615 char *line, /* input line */
616 char *outbuff, /* output goes here */
617 const char *prevout) /* prev output, for 'expect' */
620 char *args[MAX_NO_ARGS];
622 char argbuf[MAX_ARG_LEN];
623 logf(LOG_APP,"%s",line);
624 strncpy(argbuf,line, MAX_ARG_LEN-1);
625 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
626 memset(args,'\0',MAX_NO_ARGS*sizeof(char *));
627 nargs=split_args(argbuf, args);
630 for (i = 0; i <= n; i++)
632 const char *cp = args[i];
633 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
637 return -90; /* no command on line, too bad */
639 if (0==strcmp(args[0],"expect"))
643 rest= line + (args[1]-argbuf); /* rest of the line */
645 return -1; /* need something to expect */
646 printf("expecting '%s'\n",rest); /*!*/
647 if (0==strstr(prevout,rest))
649 printf( "Failed expectation, '%s' not found\n", rest);
654 for (i=0;cmds[i].cmd;i++)
655 if (0==strcmp(cmds[i].cmd, args[0]))
658 args[0]= line + (args[1]-argbuf); /* rest of the line */
661 return ((cmds[i].testfunc)(args,outbuff));
663 strcat(outbuff, "Unknown command '");
664 strcat(outbuff,args[0] );
665 strcat(outbuff,"'. Try help");
666 logf(LOG_APP,"Unknown command");
670 static int cmd_help( char *args[], char *outbuff)
673 char tmp[MAX_ARG_LEN];
675 { /* help for a single command */
676 for (i=0;cmds[i].cmd;i++)
677 if (0==strcmp(cmds[i].cmd, args[1]))
679 strcat(outbuff,cmds[i].cmd);
681 strcat(outbuff,cmds[i].args);
682 strcat(outbuff,"\n");
683 strcat(outbuff,cmds[i].explanation);
684 strcat(outbuff,"\n");
687 strcat(outbuff, "Unknown command ");
688 strcat(outbuff, args[1] );
691 { /* list all commands */
693 for (i=0;cmds[i].cmd;i++)
694 if (cmds[i].explanation)
696 /* sprintf(tmp, "%s %s %s\n",
697 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
699 strcat(tmp, cmds[i].cmd);
703 strcat(outbuff, tmp);
704 strcat(outbuff,"\n");
708 strcat(outbuff, cmds[i].args);
709 strcat(outbuff,"\n");
715 strcat(outbuff,"\n");
724 /* If Zebra reports an error after an operation,
725 * append it to the outbuff and log it */
726 static void Zerrors ( char *outbuff)
729 char tmp[MAX_OUT_BUFF];
732 ec=zebra_errCode (zh);
735 sprintf(tmp, " Zebra error %d: %s, (%s)",
736 ec, zebra_errString (zh),
738 strcat(outbuff, tmp);
739 strcat(outbuff, "\n");
741 zebra_clearError(zh);
745 /**************************************
752 char tmp[MAX_ARG_LEN];
753 char outbuff[MAX_OUT_BUFF]="";
754 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
757 char buf[MAX_ARG_LEN];
758 #if HAVE_READLINE_READLINE_H
760 line_in=readline(PROMPT);
763 #if HAVE_READLINE_HISTORY_H
765 add_history(line_in);
767 if(strlen(line_in) > MAX_ARG_LEN-1) {
768 fprintf(stderr,"Input line too long\n");
776 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
780 strncpy(prevout, outbuff, MAX_OUT_BUFF);
782 rc=onecommand(buf, outbuff, prevout);
785 strcat(outbuff, " OK\n");
790 sprintf(tmp, " command returned %d\n",rc);
794 printf("%s\n", outbuff);
799 /**************************************
803 int main (int argc, char ** args)