1 /* zebrash.c - command-line interface to zebra API
2 * $Id: zebrash.c,v 1.7 2003-03-05 16:44:02 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);
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)
158 onecommand("yaz_log_file zebrash.log",outbuff);
159 onecommand("yaz_log_prefix ZebraSh", outbuff);
160 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
161 onecommand(tmp,outbuff);
162 logf(LOG_APP,"quickstart");
163 onecommand("zebra_start",outbuff);
164 onecommand("zebra_open",outbuff);
165 onecommand("select_database Default",outbuff);
166 strcat(outbuff,"ok\n");
170 /**************************************
174 static int cmd_yaz_log_file( char *args[], char *outbuff)
176 char *fn = defarg(args[1],0);
178 sprintf(tmp, "sending yaz-log to %s\n",fn);
179 strcat(outbuff, tmp);
180 yaz_log_init_file(fn);
184 static int cmd_yaz_log_level( char *args[], char *outbuff)
186 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
188 sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
189 strcat(outbuff, tmp);
190 yaz_log_init_level(lev);
194 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
196 char *pref = defarg(args[1],"ZebraSh");
198 sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
199 strcat(outbuff, tmp);
200 yaz_log_init_prefix(pref);
204 static int cmd_logf( char *args[], char *outbuff)
206 int lev = defargint(args[1],0);
207 char tmp[MAX_OUT_BUFF]="";
212 lev=LOG_LOG; /* this is in the default set!*/
215 strcat(tmp, args[i++]);
225 static int cmd_err ( char *args[], char *outbuff)
227 char tmp[MAX_OUT_BUFF];
228 sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
230 zebra_errString (zh),
232 strcat(outbuff, tmp);
235 static int cmd_errcode ( char *args[], char *outbuff)
237 char tmp[MAX_OUT_BUFF];
238 sprintf(tmp, "errCode: %d \n",
240 strcat(outbuff, tmp);
243 static int cmd_errstr ( char *args[], char *outbuff)
245 char tmp[MAX_OUT_BUFF];
246 sprintf(tmp, "errStr: %s\n",
247 zebra_errString (zh));
248 strcat(outbuff, tmp);
251 static int cmd_erradd ( char *args[], char *outbuff)
253 char tmp[MAX_OUT_BUFF];
254 sprintf(tmp, "errAdd: %s \n",
256 strcat(outbuff, tmp);
260 /**************************************
264 static int cmd_init ( char *args[], char *outbuff)
270 static int cmd_select_database ( char *args[], char *outbuff)
275 return zebra_select_database(zh, args[1]);
278 /**************************************
279 * Command table, parser, and help
287 int (*testfunc)(char *args[], char *outbuff);
291 struct cmdstruct cmds[] = {
293 * if text is 0, does not list the command
294 * if cmd is "", adds the args (and newline) in command listing
296 { "", "Starting and stopping:", "", 0 },
299 "starts the zebra service. You need to call this first\n"
300 "if no configfile is given, assumes " DEFAULTCONFIG,
303 "stops the zebra service",
306 "starts a zebra session. Once you have called zebra_start\n"
307 "you can call zebra_open to start working",
310 "closes a zebra session",
312 { "quickstart", "[configfile]",
313 "Does a zebra_start, zebra_open, and sets up the log",
316 { "", "Log file:","", 0},
319 "Directs the log to filename (or stderr)",
323 "Sets the logging level (or returns to default)",
327 "Sets the log prefix",
331 "writes an entry in the log",
334 { "", "Error handling:","", 0},
336 "Displays zebra's error status (code, str, add)",
339 "Displays zebra's error code",
342 "Displays zebra's error string",
345 "Displays zebra's additional error message",
348 { "", "Admin:","", 0},
350 "Initializes the zebra database, destroying all data in it",
352 { "select_database", "basename",
353 "Selects a database",
354 cmd_select_database},
356 { "", "Misc:","", 0},
363 { "help", "[command]",
364 "Gives help on command, or lists them all",
366 { "", "help [command] gives more info on command", "",0 },
368 {0,0,0,0} /* end marker */
371 int onecommand( char *line, char *outbuff)
374 char *args[MAX_NO_ARGS];
376 char argbuf[MAX_ARG_LEN];
377 logf(LOG_APP,"%s",line);
378 strncpy(argbuf,line, MAX_ARG_LEN-1);
379 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
380 n=split_args(argbuf, args);
382 return -1; /* no command on line, too bad */
383 for (i=0;cmds[i].cmd;i++)
384 if (0==strcmp(cmds[i].cmd, args[0]))
387 args[0]= line + (args[1]-argbuf); /* rest of the line */
390 return ((cmds[i].testfunc)(args,outbuff));
392 strcat(outbuff, "Unknown command '");
393 strcat(outbuff,args[0] );
394 strcat(outbuff,"'. Try help");
395 logf(LOG_APP,"Unknown command");
399 static int cmd_help( char *args[], char *outbuff)
402 char tmp[MAX_ARG_LEN];
404 { /* help for a single command */
405 for (i=0;cmds[i].cmd;i++)
406 if (0==strcmp(cmds[i].cmd, args[1]))
408 strcat(outbuff,cmds[i].cmd);
410 strcat(outbuff,cmds[i].args);
411 strcat(outbuff,"\n");
412 strcat(outbuff,cmds[i].explanation);
413 strcat(outbuff,"\n");
416 strcat(outbuff, "Unknown command ");
417 strcat(outbuff, args[1] );
420 { /* list all commands */
422 for (i=0;cmds[i].cmd;i++)
423 if (cmds[i].explanation)
425 /* sprintf(tmp, "%s %s %s\n",
426 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
428 strcat(tmp, cmds[i].cmd);
432 strcat(outbuff, tmp);
433 strcat(outbuff,"\n");
437 strcat(outbuff, cmds[i].args);
438 strcat(outbuff,"\n");
444 strcat(outbuff,"\n");
453 /* If Zebra reports an error after an operation,
454 * append it to the outbuff and log it */
455 static void Zerrors ( char *outbuff)
458 char tmp[MAX_OUT_BUFF];
461 ec=zebra_errCode (zh);
464 sprintf(tmp, "Zebra error %d: %s, (%s)",
465 ec, zebra_errString (zh),
467 strcat(outbuff, tmp);
468 strcat(outbuff, "\n");
473 /**************************************
480 char tmp[MAX_ARG_LEN];
483 char buf[MAX_ARG_LEN];
484 char outbuff[MAX_OUT_BUFF];
485 #if HAVE_READLINE_READLINE_H
487 line_in=readline(PROMPT);
490 #if HAVE_READLINE_HISTORY_H
492 add_history(line_in);
494 if(strlen(line_in) > MAX_ARG_LEN-1) {
495 fprintf(stderr,"Input line too long\n");
503 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
507 rc=onecommand(buf, outbuff);
510 strcat(outbuff, "OK\n");
515 sprintf(tmp, "command returned %d\n",rc);
519 printf("%s\n", outbuff);
524 /**************************************
528 int main (int argc, char ** args)