1 /* zebrash.c - command-line interface to zebra API
2 * $Id: zebrash.c,v 1.13 2003-06-23 15:38:16 adam Exp $
4 * Copyrigth 2003 Index Data Aps
13 #if HAVE_READLINE_READLINE_H
14 #include <readline/readline.h>
16 #if HAVE_READLINE_HISTORY_H
17 #include <readline/history.h>
23 #define MAX_NO_ARGS 32
24 #define MAX_OUT_BUFF 4096
25 #define MAX_ARG_LEN 1024
26 #define PROMPT "ZebraSh>"
27 #define DEFAULTCONFIG "./zebra.cfg"
29 /**************************************
30 * Global variables (yuck!)
33 ZebraService zs=0; /* our global handle to zebra */
34 ZebraHandle zh=0; /* the current session */
35 /* time being, only one session works */
37 /**************************************
42 static int split_args( char *line, char** args )
43 { /* splits line into individual null-terminated strings,
44 * returns pointers to them in args */
45 /* FIXME - do we need to handle quoted args ?? */
49 args[0]=0; /* by default */
50 while (*p==' ' || *p=='\t' || *p=='\n')
54 while (*p==' ' || *p=='\t' || *p=='\n')
56 if (*p=='#') /* skip comments */
60 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
70 static char *defarg( char *arg, char *def )
78 static int defargint( char *arg, int def )
81 char *a=defarg(arg,0);
87 int onecommand( char *line, char *outbuff, const char *prevout);
89 /**************************************
90 * Simple support commands
93 int cmd_echo( char *args[], char *outbuff)
95 strcpy(outbuff, args[0]);
99 int cmd_quit( char *args[], char *outbuff)
101 strcpy(outbuff, "bye");
102 return -99; /* special stop signal */
105 /**************************************
106 * Tests for starting and stopping zebra, etc
109 static int cmd_help( char *args[], char *outbuff);
111 static int cmd_zebra_start( char *args[], char *outbuff)
114 if (!conf || !*conf) {
115 strcat(outbuff,"no config file specified, using "
116 DEFAULTCONFIG "\n" );
119 zs=zebra_start(conf);
121 strcpy(outbuff, "zebra_start failed" );
127 static int cmd_zebra_stop( char *args[], char *outbuff)
130 strcat(outbuff,"zebra seems not to have been started, "
131 "stopping anyway\n");
137 static int cmd_zebra_open( char *args[], char *outbuff)
140 strcat(outbuff,"zebra seems not to have been started, "
146 static int cmd_zebra_close( char *args[], char *outbuff)
149 strcat(outbuff,"Seems like you have not called zebra_open,"
155 static int cmd_quickstart( char *args[], char *outbuff)
160 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
162 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
163 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
165 rc=onecommand(tmp,outbuff,"");
166 logf(LOG_APP,"quickstart");
168 rc=onecommand("zebra_start",outbuff,"");
170 rc=onecommand("zebra_open",outbuff,"");
172 rc=onecommand("select_database Default",outbuff,"");
176 /**************************************
180 static int cmd_yaz_log_file( char *args[], char *outbuff)
182 char *fn = defarg(args[1],0);
184 sprintf(tmp, "sending yaz-log to %s\n",fn);
185 strcat(outbuff, tmp);
186 yaz_log_init_file(fn);
190 static int cmd_yaz_log_level( char *args[], char *outbuff)
192 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
194 sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
195 strcat(outbuff, tmp);
196 yaz_log_init_level(lev);
200 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
202 char *pref = defarg(args[1],"ZebraSh");
204 sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
205 strcat(outbuff, tmp);
206 yaz_log_init_prefix(pref);
210 static int cmd_logf( char *args[], char *outbuff)
212 int lev = defargint(args[1],0);
213 char tmp[MAX_OUT_BUFF]="";
218 lev=LOG_LOG; /* this is in the default set!*/
221 strcat(tmp, args[i++]);
231 static int cmd_err ( char *args[], char *outbuff)
233 char tmp[MAX_OUT_BUFF];
234 sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
236 zebra_errString (zh),
238 strcat(outbuff, tmp);
241 static int cmd_errcode ( char *args[], char *outbuff)
243 char tmp[MAX_OUT_BUFF];
244 sprintf(tmp, "errCode: %d \n",
246 strcat(outbuff, tmp);
249 static int cmd_errstr ( char *args[], char *outbuff)
251 char tmp[MAX_OUT_BUFF];
252 sprintf(tmp, "errStr: %s\n",
253 zebra_errString (zh));
254 strcat(outbuff, tmp);
257 static int cmd_erradd ( char *args[], char *outbuff)
259 char tmp[MAX_OUT_BUFF];
260 sprintf(tmp, "errAdd: %s \n",
262 strcat(outbuff, tmp);
266 /**************************************
270 static int cmd_init ( char *args[], char *outbuff)
276 static int cmd_select_database ( char *args[], char *outbuff)
281 strcat(outbuff,"Selecting database 'Default'\n");
283 return zebra_select_database(zh, db);
286 static int cmd_create_database( char *args[], char *outbuff)
291 strcat(outbuff,"Creating database ");
293 strcat(outbuff,"\n");
295 return zebra_create_database(zh, db);
298 static int cmd_begin_trans( char *args[], char *outbuff)
301 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
303 return zebra_begin_trans(zh,rw);
305 static int cmd_end_trans( char *args[], char *outbuff)
307 return zebra_end_trans(zh);
309 /*************************************
310 * Inserting and deleting
313 static int cmd_record_insert( char *args[], char *outbuff)
316 char buf[MAX_ARG_LEN];
324 strcat(buf, args[i++]);
327 rc=zebra_record_insert(zh,buf, strlen(buf), &sysno);
330 sprintf(buf,"ok sysno=%d\n",sysno);
337 static int cmd_exchange_record( char *args[], char *outbuff)
341 char *action = args[3];
344 char buf[MAX_ARG_LEN];
345 if (!(base && id && action && args[4] ))
347 strcat(outbuff,"Missing arguments!\n");
348 onecommand("help exchange_record", outbuff, "");
353 strcat(buf, args[i++]);
356 rc=zebra_admin_exchange_record(zh, base, buf, strlen(buf),
357 id, strlen(id), atoi(action));
362 /**************************************)
363 * Command table, parser, and help
371 int (*testfunc)(char *args[], char *outbuff);
375 struct cmdstruct cmds[] = {
377 * if text is 0, does not list the command
378 * if cmd is "", adds the args (and newline) in command listing
380 { "", "Starting and stopping:", "", 0 },
383 "starts the zebra service. You need to call this first\n"
384 "if no configfile is given, assumes " DEFAULTCONFIG,
387 "stops the zebra service",
390 "starts a zebra session. Once you have called zebra_start\n"
391 "you can call zebra_open to start working",
394 "closes a zebra session",
396 { "quickstart", "[configfile]",
397 "Does a zebra_start, zebra_open, and sets up the log",
400 { "", "Log file:","", 0},
403 "Directs the log to filename (or stderr)",
407 "Sets the logging level (or returns to default)",
411 "Sets the log prefix",
415 "writes an entry in the log",
418 { "", "Error handling:","", 0},
420 "Displays zebra's error status (code, str, add)",
423 "Displays zebra's error code",
426 "Displays zebra's error string",
429 "Displays zebra's additional error message",
432 { "", "Admin:","", 0},
434 "Initializes the zebra database, destroying all data in it",
436 { "select_database", "basename",
437 "Selects a database",
438 cmd_select_database},
439 { "create_database", "basename",
440 "Creates a database",
441 cmd_create_database},
442 { "begin_trans", "[rw]",
443 "Begins a transaction. rw=1 means write, otherwise read-only",
446 "Ends a transaction",
449 { "","Updating:","",0},
450 { "record_insert","record",
451 "inserts an sgml record into Default",
453 { "exchange_record","database record-id action record",
454 "inserts (1), updates (2), or deletes (3) a record \n"
455 "record-id must be a unique identifier for the record",
456 cmd_exchange_record},
457 { "", "Misc:","", 0},
464 { "help", "[command]",
465 "Gives help on command, or lists them all",
467 { "", "help [command] gives more info on command", "",0 },
469 {0,0,0,0} /* end marker */
473 char *line, /* input line */
474 char *outbuff, /* output goes here */
475 const char *prevout) /* prev output, for 'expect' */
478 char *args[MAX_NO_ARGS];
480 char argbuf[MAX_ARG_LEN];
481 logf(LOG_APP,"%s",line);
482 strncpy(argbuf,line, MAX_ARG_LEN-1);
483 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
484 n=split_args(argbuf, args);
487 for (i = 0; i <= n; i++)
489 const char *cp = args[i];
490 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
494 return -90; /* no command on line, too bad */
496 if (0==strcmp(args[0],"expect"))
500 rest= line + (args[1]-argbuf); /* rest of the line */
502 return -1; /* need something to expect */
503 printf("expecting '%s'\n",rest); /*!*/
504 if (0==strstr(prevout,rest))
506 printf( "Failed expectation, '%s' not found\n", rest);
511 for (i=0;cmds[i].cmd;i++)
512 if (0==strcmp(cmds[i].cmd, args[0]))
515 args[0]= line + (args[1]-argbuf); /* rest of the line */
518 return ((cmds[i].testfunc)(args,outbuff));
520 strcat(outbuff, "Unknown command '");
521 strcat(outbuff,args[0] );
522 strcat(outbuff,"'. Try help");
523 logf(LOG_APP,"Unknown command");
527 static int cmd_help( char *args[], char *outbuff)
530 char tmp[MAX_ARG_LEN];
532 { /* help for a single command */
533 for (i=0;cmds[i].cmd;i++)
534 if (0==strcmp(cmds[i].cmd, args[1]))
536 strcat(outbuff,cmds[i].cmd);
538 strcat(outbuff,cmds[i].args);
539 strcat(outbuff,"\n");
540 strcat(outbuff,cmds[i].explanation);
541 strcat(outbuff,"\n");
544 strcat(outbuff, "Unknown command ");
545 strcat(outbuff, args[1] );
548 { /* list all commands */
550 for (i=0;cmds[i].cmd;i++)
551 if (cmds[i].explanation)
553 /* sprintf(tmp, "%s %s %s\n",
554 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
556 strcat(tmp, cmds[i].cmd);
560 strcat(outbuff, tmp);
561 strcat(outbuff,"\n");
565 strcat(outbuff, cmds[i].args);
566 strcat(outbuff,"\n");
572 strcat(outbuff,"\n");
581 /* If Zebra reports an error after an operation,
582 * append it to the outbuff and log it */
583 static void Zerrors ( char *outbuff)
586 char tmp[MAX_OUT_BUFF];
589 ec=zebra_errCode (zh);
592 sprintf(tmp, " Zebra error %d: %s, (%s)",
593 ec, zebra_errString (zh),
595 strcat(outbuff, tmp);
596 strcat(outbuff, "\n");
598 zebra_clearError(zh);
602 /**************************************
609 char tmp[MAX_ARG_LEN];
610 char outbuff[MAX_OUT_BUFF]="";
611 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
614 char buf[MAX_ARG_LEN];
615 #if HAVE_READLINE_READLINE_H
617 line_in=readline(PROMPT);
620 #if HAVE_READLINE_HISTORY_H
622 add_history(line_in);
624 if(strlen(line_in) > MAX_ARG_LEN-1) {
625 fprintf(stderr,"Input line too long\n");
633 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
637 strncpy(prevout, outbuff, MAX_OUT_BUFF);
639 rc=onecommand(buf, outbuff, prevout);
642 strcat(outbuff, " OK\n");
647 sprintf(tmp, " command returned %d\n",rc);
651 printf("%s\n", outbuff);
656 /**************************************
660 int main (int argc, char ** args)