1 /* $Id: zebrash.c,v 1.14 2003-06-30 19:37:12 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>
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");
187 rc=onecommand("zebra_start",outbuff,"");
189 rc=onecommand("zebra_open",outbuff,"");
191 rc=onecommand("select_database Default",outbuff,"");
195 /**************************************
199 static int cmd_yaz_log_file( char *args[], char *outbuff)
201 char *fn = defarg(args[1],0);
203 sprintf(tmp, "sending yaz-log to %s\n",fn);
204 strcat(outbuff, tmp);
205 yaz_log_init_file(fn);
209 static int cmd_yaz_log_level( char *args[], char *outbuff)
211 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
213 sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
214 strcat(outbuff, tmp);
215 yaz_log_init_level(lev);
219 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
221 char *pref = defarg(args[1],"ZebraSh");
223 sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
224 strcat(outbuff, tmp);
225 yaz_log_init_prefix(pref);
229 static int cmd_logf( char *args[], char *outbuff)
231 int lev = defargint(args[1],0);
232 char tmp[MAX_OUT_BUFF]="";
237 lev=LOG_LOG; /* this is in the default set!*/
240 strcat(tmp, args[i++]);
250 static int cmd_err ( char *args[], char *outbuff)
252 char tmp[MAX_OUT_BUFF];
253 sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
255 zebra_errString (zh),
257 strcat(outbuff, tmp);
260 static int cmd_errcode ( char *args[], char *outbuff)
262 char tmp[MAX_OUT_BUFF];
263 sprintf(tmp, "errCode: %d \n",
265 strcat(outbuff, tmp);
268 static int cmd_errstr ( char *args[], char *outbuff)
270 char tmp[MAX_OUT_BUFF];
271 sprintf(tmp, "errStr: %s\n",
272 zebra_errString (zh));
273 strcat(outbuff, tmp);
276 static int cmd_erradd ( char *args[], char *outbuff)
278 char tmp[MAX_OUT_BUFF];
279 sprintf(tmp, "errAdd: %s \n",
281 strcat(outbuff, tmp);
285 /**************************************
289 static int cmd_init ( char *args[], char *outbuff)
295 static int cmd_select_database ( char *args[], char *outbuff)
300 strcat(outbuff,"Selecting database 'Default'\n");
302 return zebra_select_database(zh, db);
305 static int cmd_create_database( char *args[], char *outbuff)
310 strcat(outbuff,"Creating database ");
312 strcat(outbuff,"\n");
314 return zebra_create_database(zh, db);
317 static int cmd_drop_database( char *args[], char *outbuff)
322 strcat(outbuff,"Dropping database ");
324 strcat(outbuff,"\n");
326 return zebra_drop_database(zh, db);
329 static int cmd_begin_trans( char *args[], char *outbuff)
332 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
334 return zebra_begin_trans(zh,rw);
336 static int cmd_end_trans( char *args[], char *outbuff)
338 return zebra_end_trans(zh);
340 /*************************************
341 * Inserting and deleting
344 static int cmd_record_insert( char *args[], char *outbuff)
347 char buf[MAX_ARG_LEN];
355 strcat(buf, args[i++]);
358 rc=zebra_record_insert(zh,buf, strlen(buf), &sysno);
361 sprintf(buf,"ok sysno=%d\n",sysno);
368 static int cmd_exchange_record( char *args[], char *outbuff)
372 char *action = args[3];
375 char buf[MAX_ARG_LEN];
376 if (!(base && id && action && args[4] ))
378 strcat(outbuff,"Missing arguments!\n");
379 onecommand("help exchange_record", outbuff, "");
384 strcat(buf, args[i++]);
387 rc=zebra_admin_exchange_record(zh, base, buf, strlen(buf),
388 id, strlen(id), atoi(action));
393 /**************************************)
394 * Command table, parser, and help
402 int (*testfunc)(char *args[], char *outbuff);
406 struct cmdstruct cmds[] = {
408 * if text is 0, does not list the command
409 * if cmd is "", adds the args (and newline) in command listing
411 { "", "Starting and stopping:", "", 0 },
414 "starts the zebra service. You need to call this first\n"
415 "if no configfile is given, assumes " DEFAULTCONFIG,
418 "stops the zebra service",
421 "starts a zebra session. Once you have called zebra_start\n"
422 "you can call zebra_open to start working",
425 "closes a zebra session",
427 { "quickstart", "[configfile]",
428 "Does a zebra_start, zebra_open, and sets up the log",
431 { "", "Log file:","", 0},
434 "Directs the log to filename (or stderr)",
438 "Sets the logging level (or returns to default)",
442 "Sets the log prefix",
446 "writes an entry in the log",
449 { "", "Error handling:","", 0},
451 "Displays zebra's error status (code, str, add)",
454 "Displays zebra's error code",
457 "Displays zebra's error string",
460 "Displays zebra's additional error message",
463 { "", "Admin:","", 0},
465 "Initializes the zebra database, destroying all data in it",
467 { "select_database", "basename",
468 "Selects a database",
469 cmd_select_database},
470 { "create_database", "basename",
472 cmd_create_database},
473 { "drop_database", "basename",
476 { "begin_trans", "[rw]",
477 "Begins a transaction. rw=1 means write, otherwise read-only",
480 "Ends a transaction",
483 { "","Updating:","",0},
484 { "record_insert","record",
485 "inserts an sgml record into Default",
487 { "exchange_record","database record-id action record",
488 "inserts (1), updates (2), or deletes (3) a record \n"
489 "record-id must be a unique identifier for the record",
490 cmd_exchange_record},
491 { "", "Misc:","", 0},
498 { "help", "[command]",
499 "Gives help on command, or lists them all",
501 { "", "help [command] gives more info on command", "",0 },
503 {0,0,0,0} /* end marker */
507 char *line, /* input line */
508 char *outbuff, /* output goes here */
509 const char *prevout) /* prev output, for 'expect' */
512 char *args[MAX_NO_ARGS];
514 char argbuf[MAX_ARG_LEN];
515 logf(LOG_APP,"%s",line);
516 strncpy(argbuf,line, MAX_ARG_LEN-1);
517 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
518 n=split_args(argbuf, args);
521 for (i = 0; i <= n; i++)
523 const char *cp = args[i];
524 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
528 return -90; /* no command on line, too bad */
530 if (0==strcmp(args[0],"expect"))
534 rest= line + (args[1]-argbuf); /* rest of the line */
536 return -1; /* need something to expect */
537 printf("expecting '%s'\n",rest); /*!*/
538 if (0==strstr(prevout,rest))
540 printf( "Failed expectation, '%s' not found\n", rest);
545 for (i=0;cmds[i].cmd;i++)
546 if (0==strcmp(cmds[i].cmd, args[0]))
549 args[0]= line + (args[1]-argbuf); /* rest of the line */
552 return ((cmds[i].testfunc)(args,outbuff));
554 strcat(outbuff, "Unknown command '");
555 strcat(outbuff,args[0] );
556 strcat(outbuff,"'. Try help");
557 logf(LOG_APP,"Unknown command");
561 static int cmd_help( char *args[], char *outbuff)
564 char tmp[MAX_ARG_LEN];
566 { /* help for a single command */
567 for (i=0;cmds[i].cmd;i++)
568 if (0==strcmp(cmds[i].cmd, args[1]))
570 strcat(outbuff,cmds[i].cmd);
572 strcat(outbuff,cmds[i].args);
573 strcat(outbuff,"\n");
574 strcat(outbuff,cmds[i].explanation);
575 strcat(outbuff,"\n");
578 strcat(outbuff, "Unknown command ");
579 strcat(outbuff, args[1] );
582 { /* list all commands */
584 for (i=0;cmds[i].cmd;i++)
585 if (cmds[i].explanation)
587 /* sprintf(tmp, "%s %s %s\n",
588 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
590 strcat(tmp, cmds[i].cmd);
594 strcat(outbuff, tmp);
595 strcat(outbuff,"\n");
599 strcat(outbuff, cmds[i].args);
600 strcat(outbuff,"\n");
606 strcat(outbuff,"\n");
615 /* If Zebra reports an error after an operation,
616 * append it to the outbuff and log it */
617 static void Zerrors ( char *outbuff)
620 char tmp[MAX_OUT_BUFF];
623 ec=zebra_errCode (zh);
626 sprintf(tmp, " Zebra error %d: %s, (%s)",
627 ec, zebra_errString (zh),
629 strcat(outbuff, tmp);
630 strcat(outbuff, "\n");
632 zebra_clearError(zh);
636 /**************************************
643 char tmp[MAX_ARG_LEN];
644 char outbuff[MAX_OUT_BUFF]="";
645 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
648 char buf[MAX_ARG_LEN];
649 #if HAVE_READLINE_READLINE_H
651 line_in=readline(PROMPT);
654 #if HAVE_READLINE_HISTORY_H
656 add_history(line_in);
658 if(strlen(line_in) > MAX_ARG_LEN-1) {
659 fprintf(stderr,"Input line too long\n");
667 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
671 strncpy(prevout, outbuff, MAX_OUT_BUFF);
673 rc=onecommand(buf, outbuff, prevout);
676 strcat(outbuff, " OK\n");
681 sprintf(tmp, " command returned %d\n",rc);
685 printf("%s\n", outbuff);
690 /**************************************
694 int main (int argc, char ** args)