1 /* zebrash.c - command-line interface to zebra API
2 * $Id: zebrash.c,v 1.10 2003-06-20 14:21:23 heikki 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)
292 strcat(outbuff,"Creating database ");
294 strcat(outbuff,"\n");
296 return zebra_create_database(zh, db);
298 /**************************************
299 * Command table, parser, and help
307 int (*testfunc)(char *args[], char *outbuff);
311 struct cmdstruct cmds[] = {
313 * if text is 0, does not list the command
314 * if cmd is "", adds the args (and newline) in command listing
316 { "", "Starting and stopping:", "", 0 },
319 "starts the zebra service. You need to call this first\n"
320 "if no configfile is given, assumes " DEFAULTCONFIG,
323 "stops the zebra service",
326 "starts a zebra session. Once you have called zebra_start\n"
327 "you can call zebra_open to start working",
330 "closes a zebra session",
332 { "quickstart", "[configfile]",
333 "Does a zebra_start, zebra_open, and sets up the log",
336 { "", "Log file:","", 0},
339 "Directs the log to filename (or stderr)",
343 "Sets the logging level (or returns to default)",
347 "Sets the log prefix",
351 "writes an entry in the log",
354 { "", "Error handling:","", 0},
356 "Displays zebra's error status (code, str, add)",
359 "Displays zebra's error code",
362 "Displays zebra's error string",
365 "Displays zebra's additional error message",
368 { "", "Admin:","", 0},
370 "Initializes the zebra database, destroying all data in it",
372 { "select_database", "basename",
373 "Selects a database",
374 cmd_select_database},
375 { "create_database", "basename",
376 "Creates a database",
377 cmd_create_database},
378 { "", "Misc:","", 0},
385 { "help", "[command]",
386 "Gives help on command, or lists them all",
388 { "", "help [command] gives more info on command", "",0 },
390 {0,0,0,0} /* end marker */
394 char *line, /* input line */
395 char *outbuff, /* output goes here */
396 const char *prevout) /* prev output, for 'expect' */
399 char *args[MAX_NO_ARGS];
401 char argbuf[MAX_ARG_LEN];
402 logf(LOG_APP,"%s",line);
403 strncpy(argbuf,line, MAX_ARG_LEN-1);
404 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
405 n=split_args(argbuf, args);
408 for (i = 0; i <= n; i++)
410 const char *cp = args[i];
411 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
415 return -90; /* no command on line, too bad */
417 if (0==strcmp(args[0],"expect"))
421 rest= line + (args[1]-argbuf); /* rest of the line */
423 return -1; /* need something to expect */
424 printf("expecting '%s'\n",rest); /*!*/
425 if (0==strstr(prevout,rest))
427 printf( "Failed expectation, '%s' not found\n");
432 for (i=0;cmds[i].cmd;i++)
433 if (0==strcmp(cmds[i].cmd, args[0]))
436 args[0]= line + (args[1]-argbuf); /* rest of the line */
439 return ((cmds[i].testfunc)(args,outbuff));
441 strcat(outbuff, "Unknown command '");
442 strcat(outbuff,args[0] );
443 strcat(outbuff,"'. Try help");
444 logf(LOG_APP,"Unknown command");
448 static int cmd_help( char *args[], char *outbuff)
451 char tmp[MAX_ARG_LEN];
453 { /* help for a single command */
454 for (i=0;cmds[i].cmd;i++)
455 if (0==strcmp(cmds[i].cmd, args[1]))
457 strcat(outbuff,cmds[i].cmd);
459 strcat(outbuff,cmds[i].args);
460 strcat(outbuff,"\n");
461 strcat(outbuff,cmds[i].explanation);
462 strcat(outbuff,"\n");
465 strcat(outbuff, "Unknown command ");
466 strcat(outbuff, args[1] );
469 { /* list all commands */
471 for (i=0;cmds[i].cmd;i++)
472 if (cmds[i].explanation)
474 /* sprintf(tmp, "%s %s %s\n",
475 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
477 strcat(tmp, cmds[i].cmd);
481 strcat(outbuff, tmp);
482 strcat(outbuff,"\n");
486 strcat(outbuff, cmds[i].args);
487 strcat(outbuff,"\n");
493 strcat(outbuff,"\n");
502 /* If Zebra reports an error after an operation,
503 * append it to the outbuff and log it */
504 static void Zerrors ( char *outbuff)
507 char tmp[MAX_OUT_BUFF];
510 ec=zebra_errCode (zh);
513 sprintf(tmp, " Zebra error %d: %s, (%s)",
514 ec, zebra_errString (zh),
516 strcat(outbuff, tmp);
517 strcat(outbuff, "\n");
519 zebra_clearError(zh);
523 /**************************************
530 char tmp[MAX_ARG_LEN];
531 char outbuff[MAX_OUT_BUFF]="";
532 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
535 char buf[MAX_ARG_LEN];
536 #if HAVE_READLINE_READLINE_H
538 line_in=readline(PROMPT);
541 #if HAVE_READLINE_HISTORY_H
543 add_history(line_in);
545 if(strlen(line_in) > MAX_ARG_LEN-1) {
546 fprintf(stderr,"Input line too long\n");
554 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
558 strncpy(prevout, outbuff, MAX_OUT_BUFF);
560 rc=onecommand(buf, outbuff, prevout);
563 strcat(outbuff, " OK\n");
568 sprintf(tmp, " command returned %d\n",rc);
572 printf("%s\n", outbuff);
577 /**************************************
581 int main (int argc, char ** args)