1 /* $Id: zebrash.c,v 1.15 2003-07-03 14:45:02 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>
42 #define MAX_NO_ARGS 32
43 #define MAX_OUT_BUFF 4096
44 #define MAX_ARG_LEN 1024
45 #define PROMPT "ZebraSh>"
46 #define DEFAULTCONFIG "./zebra.cfg"
48 /**************************************
49 * Global variables (yuck!)
52 ZebraService zs=0; /* our global handle to zebra */
53 ZebraHandle zh=0; /* the current session */
54 /* time being, only one session works */
56 /**************************************
61 static int split_args( char *line, char** args )
62 { /* splits line into individual null-terminated strings,
63 * returns pointers to them in args */
64 /* FIXME - do we need to handle quoted args ?? */
68 args[0]=0; /* by default */
69 while (*p==' ' || *p=='\t' || *p=='\n')
73 while (*p==' ' || *p=='\t' || *p=='\n')
75 if (*p=='#') /* skip comments */
79 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
89 static char *defarg( char *arg, char *def )
97 static int defargint( char *arg, int def )
100 char *a=defarg(arg,0);
106 int onecommand( char *line, char *outbuff, const char *prevout);
108 /**************************************
109 * Simple support commands
112 int cmd_echo( char *args[], char *outbuff)
114 strcpy(outbuff, args[0]);
118 int cmd_quit( char *args[], char *outbuff)
120 strcpy(outbuff, "bye");
121 return -99; /* special stop signal */
124 /**************************************
125 * Tests for starting and stopping zebra, etc
128 static int cmd_help( char *args[], char *outbuff);
130 static int cmd_zebra_start( char *args[], char *outbuff)
133 if (!conf || !*conf) {
134 strcat(outbuff,"no config file specified, using "
135 DEFAULTCONFIG "\n" );
138 zs=zebra_start(conf);
140 strcpy(outbuff, "zebra_start failed" );
146 static int cmd_zebra_stop( char *args[], char *outbuff)
149 strcat(outbuff,"zebra seems not to have been started, "
150 "stopping anyway\n");
156 static int cmd_zebra_open( char *args[], char *outbuff)
159 strcat(outbuff,"zebra seems not to have been started, "
165 static int cmd_zebra_close( char *args[], char *outbuff)
168 strcat(outbuff,"Seems like you have not called zebra_open,"
174 static int cmd_quickstart( char *args[], char *outbuff)
179 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
181 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
182 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
184 rc=onecommand(tmp,outbuff,"");
185 logf(LOG_APP,"quickstart");
188 rc=onecommand("zebra_start",outbuff,"");
191 rc=onecommand("zebra_open",outbuff,"");
193 rc=onecommand("select_database Default",outbuff,"");
197 /**************************************
201 static int cmd_yaz_log_file( char *args[], char *outbuff)
203 char *fn = defarg(args[1],0);
205 sprintf(tmp, "sending yaz-log to %s\n",fn);
206 strcat(outbuff, tmp);
207 yaz_log_init_file(fn);
211 static int cmd_yaz_log_level( char *args[], char *outbuff)
213 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
215 sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
216 strcat(outbuff, tmp);
217 yaz_log_init_level(lev);
221 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
223 char *pref = defarg(args[1],"ZebraSh");
225 sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
226 strcat(outbuff, tmp);
227 yaz_log_init_prefix(pref);
231 static int cmd_logf( char *args[], char *outbuff)
233 int lev = defargint(args[1],0);
234 char tmp[MAX_OUT_BUFF]="";
239 lev=LOG_LOG; /* this is in the default set!*/
242 strcat(tmp, args[i++]);
252 static int cmd_err ( char *args[], char *outbuff)
254 char tmp[MAX_OUT_BUFF];
255 sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
257 zebra_errString (zh),
259 strcat(outbuff, tmp);
262 static int cmd_errcode ( char *args[], char *outbuff)
264 char tmp[MAX_OUT_BUFF];
265 sprintf(tmp, "errCode: %d \n",
267 strcat(outbuff, tmp);
270 static int cmd_errstr ( char *args[], char *outbuff)
272 char tmp[MAX_OUT_BUFF];
273 sprintf(tmp, "errStr: %s\n",
274 zebra_errString (zh));
275 strcat(outbuff, tmp);
278 static int cmd_erradd ( char *args[], char *outbuff)
280 char tmp[MAX_OUT_BUFF];
281 sprintf(tmp, "errAdd: %s \n",
283 strcat(outbuff, tmp);
287 /**************************************
291 static int cmd_init ( char *args[], char *outbuff)
297 static int cmd_select_database ( char *args[], char *outbuff)
302 strcat(outbuff,"Selecting database 'Default'\n");
304 return zebra_select_database(zh, db);
307 static int cmd_create_database( char *args[], char *outbuff)
312 strcat(outbuff,"Creating database ");
314 strcat(outbuff,"\n");
316 return zebra_create_database(zh, db);
319 static int cmd_drop_database( char *args[], char *outbuff)
324 strcat(outbuff,"Dropping database ");
326 strcat(outbuff,"\n");
328 return zebra_drop_database(zh, db);
331 static int cmd_begin_trans( char *args[], char *outbuff)
334 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
336 return zebra_begin_trans(zh,rw);
338 static int cmd_end_trans( char *args[], char *outbuff)
340 return zebra_end_trans(zh);
342 /*************************************
343 * Inserting and deleting
346 static int cmd_record_insert( char *args[], char *outbuff)
349 char buf[MAX_ARG_LEN];
357 strcat(buf, args[i++]);
360 rc=zebra_record_insert(zh,buf, strlen(buf), &sysno);
363 sprintf(buf,"ok sysno=%d\n",sysno);
370 static int cmd_exchange_record( char *args[], char *outbuff)
374 char *action = args[3];
377 char buf[MAX_ARG_LEN];
378 if (!(base && id && action && args[4] ))
380 strcat(outbuff,"Missing arguments!\n");
381 onecommand("help exchange_record", outbuff, "");
386 strcat(buf, args[i++]);
389 rc=zebra_admin_exchange_record(zh, base, buf, strlen(buf),
390 id, strlen(id), atoi(action));
394 /**********************************
395 * Searching and retrieving
398 static int cmd_search_pqf( char *args[], char *outbuff)
402 char qry[MAX_ARG_LEN]="";
407 strcat(qry, args[i++]);
410 rc=zebra_search_PQF(zh, qry, set, &hits);
413 sprintf(qry,"%d hits found\n",hits);
419 static int cmd_find( char *args[], char *outbuff)
421 char *setname="MyResultSet";
422 char qry[MAX_ARG_LEN]="";
426 if (0==strstr(args[0],"@attr"))
427 strcat(qry, "@attr 1=/ ");
430 strcat(qry, args[i++]);
434 onecommand("quickstart", outbuff, "");
435 strcat(outbuff, "find ");
436 strcat(outbuff, qry);
437 strcat(outbuff, "\n");
438 rc=zebra_search_PQF(zh, qry, setname, &hits);
441 sprintf(qry,"%d hits found\n",hits);
447 /**************************************)
448 * Command table, parser, and help
456 int (*testfunc)(char *args[], char *outbuff);
460 struct cmdstruct cmds[] = {
462 * if text is 0, does not list the command
463 * if cmd is "", adds the args (and newline) in command listing
465 { "", "Starting and stopping:", "", 0 },
468 "starts the zebra service. You need to call this first\n"
469 "if no configfile is given, assumes " DEFAULTCONFIG,
472 "stops the zebra service",
475 "starts a zebra session. Once you have called zebra_start\n"
476 "you can call zebra_open to start working",
479 "closes a zebra session",
481 { "quickstart", "[configfile]",
482 "Does a zebra_start, zebra_open, and sets up the log",
485 { "", "Log file:","", 0},
488 "Directs the log to filename (or stderr)",
492 "Sets the logging level (or returns to default)",
496 "Sets the log prefix",
500 "writes an entry in the log",
503 { "", "Error handling:","", 0},
505 "Displays zebra's error status (code, str, add)",
508 "Displays zebra's error code",
511 "Displays zebra's error string",
514 "Displays zebra's additional error message",
517 { "", "Admin:","", 0},
519 "Initializes the zebra database, destroying all data in it",
521 { "select_database", "basename",
522 "Selects a database",
523 cmd_select_database},
524 { "create_database", "basename",
526 cmd_create_database},
527 { "drop_database", "basename",
530 { "begin_trans", "[rw]",
531 "Begins a transaction. rw=1 means write, otherwise read-only",
534 "Ends a transaction",
537 { "","Updating:","",0},
538 { "record_insert","record",
539 "inserts an sgml record into Default",
541 { "exchange_record","database record-id action record",
542 "inserts (1), updates (2), or deletes (3) a record \n"
543 "record-id must be a unique identifier for the record",
544 cmd_exchange_record},
545 { "","Searching and retrieving:","",0},
546 { "search_pqf","setname query",
555 { "", "Misc:","", 0},
565 { "help", "[command]",
566 "Gives help on command, or lists them all",
568 { "", "help [command] gives more info on command", "",0 },
570 {0,0,0,0} /* end marker */
574 char *line, /* input line */
575 char *outbuff, /* output goes here */
576 const char *prevout) /* prev output, for 'expect' */
579 char *args[MAX_NO_ARGS];
581 char argbuf[MAX_ARG_LEN];
582 logf(LOG_APP,"%s",line);
583 strncpy(argbuf,line, MAX_ARG_LEN-1);
584 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
585 n=split_args(argbuf, args);
588 for (i = 0; i <= n; i++)
590 const char *cp = args[i];
591 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
595 return -90; /* no command on line, too bad */
597 if (0==strcmp(args[0],"expect"))
601 rest= line + (args[1]-argbuf); /* rest of the line */
603 return -1; /* need something to expect */
604 printf("expecting '%s'\n",rest); /*!*/
605 if (0==strstr(prevout,rest))
607 printf( "Failed expectation, '%s' not found\n", rest);
612 for (i=0;cmds[i].cmd;i++)
613 if (0==strcmp(cmds[i].cmd, args[0]))
616 args[0]= line + (args[1]-argbuf); /* rest of the line */
619 return ((cmds[i].testfunc)(args,outbuff));
621 strcat(outbuff, "Unknown command '");
622 strcat(outbuff,args[0] );
623 strcat(outbuff,"'. Try help");
624 logf(LOG_APP,"Unknown command");
628 static int cmd_help( char *args[], char *outbuff)
631 char tmp[MAX_ARG_LEN];
633 { /* help for a single command */
634 for (i=0;cmds[i].cmd;i++)
635 if (0==strcmp(cmds[i].cmd, args[1]))
637 strcat(outbuff,cmds[i].cmd);
639 strcat(outbuff,cmds[i].args);
640 strcat(outbuff,"\n");
641 strcat(outbuff,cmds[i].explanation);
642 strcat(outbuff,"\n");
645 strcat(outbuff, "Unknown command ");
646 strcat(outbuff, args[1] );
649 { /* list all commands */
651 for (i=0;cmds[i].cmd;i++)
652 if (cmds[i].explanation)
654 /* sprintf(tmp, "%s %s %s\n",
655 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
657 strcat(tmp, cmds[i].cmd);
661 strcat(outbuff, tmp);
662 strcat(outbuff,"\n");
666 strcat(outbuff, cmds[i].args);
667 strcat(outbuff,"\n");
673 strcat(outbuff,"\n");
682 /* If Zebra reports an error after an operation,
683 * append it to the outbuff and log it */
684 static void Zerrors ( char *outbuff)
687 char tmp[MAX_OUT_BUFF];
690 ec=zebra_errCode (zh);
693 sprintf(tmp, " Zebra error %d: %s, (%s)",
694 ec, zebra_errString (zh),
696 strcat(outbuff, tmp);
697 strcat(outbuff, "\n");
699 zebra_clearError(zh);
703 /**************************************
710 char tmp[MAX_ARG_LEN];
711 char outbuff[MAX_OUT_BUFF]="";
712 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
715 char buf[MAX_ARG_LEN];
716 #if HAVE_READLINE_READLINE_H
718 line_in=readline(PROMPT);
721 #if HAVE_READLINE_HISTORY_H
723 add_history(line_in);
725 if(strlen(line_in) > MAX_ARG_LEN-1) {
726 fprintf(stderr,"Input line too long\n");
734 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
738 strncpy(prevout, outbuff, MAX_OUT_BUFF);
740 rc=onecommand(buf, outbuff, prevout);
743 strcat(outbuff, " OK\n");
748 sprintf(tmp, " command returned %d\n",rc);
752 printf("%s\n", outbuff);
757 /**************************************
761 int main (int argc, char ** args)