1 /* zebrash.c - command-line interface to zebra API
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 /**************************************
88 * Simple support commands
91 int cmd_echo( char *args[], char *outbuff)
93 strcpy(outbuff, args[0]);
97 int cmd_quit( char *args[], char *outbuff)
99 strcpy(outbuff, "bye");
100 return -99; /* special stop signal */
103 /**************************************
104 * Tests for starting and stopping zebra, etc
107 static int cmd_help( char *args[], char *outbuff);
109 static int cmd_zebra_start( char *args[], char *outbuff)
112 if (!conf || !*conf) {
113 strcat(outbuff,"no config file specified, using "
114 DEFAULTCONFIG "\n" );
117 zs=zebra_start(conf);
119 strcpy(outbuff, "zebra_start failed" );
125 static int cmd_zebra_stop( char *args[], char *outbuff)
128 strcat(outbuff,"zebra seems not to have been started, "
135 static int cmd_zebra_open( char *args[], char *outbuff)
138 strcat(outbuff,"zebra seems not to have been started, "
144 static int cmd_zebra_close( char *args[], char *outbuff)
147 strcat(outbuff,"Seems like you have not called zebra_open,"
153 static int cmd_quickstart( char *args[], char *outbuff)
155 cmd_zebra_start(args,outbuff);
156 cmd_zebra_open(args,outbuff);
157 yaz_log_init_file("zebrash.log");
158 yaz_log_init_prefix("ZebraSh");
159 strcat(outbuff,"Started zebra, log in zebrash.log");
163 /**************************************
167 static int cmd_yaz_log_file( char *args[], char *outbuff)
169 char *fn = defarg(args[1],0);
171 sprintf(tmp, "sending yaz-log to %s ",fn);
172 strcat(outbuff, tmp);
173 yaz_log_init_file(fn);
177 static int cmd_yaz_log_level( char *args[], char *outbuff)
179 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
181 sprintf(tmp, "setting yaz-log to level %d (ox%x)",lev,lev);
182 strcat(outbuff, tmp);
183 yaz_log_init_level(lev);
187 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
189 char *pref = defarg(args[1],"ZebraSh");
191 sprintf(tmp, "setting yaz-log prefix to %s",pref);
192 strcat(outbuff, tmp);
193 yaz_log_init_prefix(pref);
197 static int cmd_logf( char *args[], char *outbuff)
199 int lev = defargint(args[1],0);
200 char tmp[MAX_OUT_BUFF]="";
205 lev=LOG_LOG; /* this is in the default set!*/
208 strcat(tmp, args[i++]);
218 static int cmd_err ( char *args[], char *outbuff)
220 char tmp[MAX_OUT_BUFF];
221 sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
223 zebra_errString (zh),
225 strcat(outbuff, tmp);
228 static int cmd_errcode ( char *args[], char *outbuff)
230 char tmp[MAX_OUT_BUFF];
231 sprintf(tmp, "errCode: %d ",
233 strcat(outbuff, tmp);
236 static int cmd_errstr ( char *args[], char *outbuff)
238 char tmp[MAX_OUT_BUFF];
239 sprintf(tmp, "errStr: %s",
240 zebra_errString (zh));
241 strcat(outbuff, tmp);
244 static int cmd_erradd ( char *args[], char *outbuff)
246 char tmp[MAX_OUT_BUFF];
247 sprintf(tmp, "errAdd: %s \n",
249 strcat(outbuff, tmp);
254 /**************************************
255 * Command table, parser, and help
263 int (*testfunc)(char *args[], char *outbuff);
267 struct cmdstruct cmds[] = {
269 * if text is 0, does not list the command
270 * if cmd is "", adds the args (and newline) in command listing
272 { "", "Starting and stopping:", "", 0 },
275 "starts the zebra service. You need to call this first\n"
276 "if no configfile is given, assumes " DEFAULTCONFIG,
279 "stops the zebra service",
282 "starts a zebra session. Once you have called zebra_start\n"
283 "you can call zebra_open to start working",
286 "closes a zebra session",
288 { "quickstart", "[configfile]",
289 "Does a zebra_start, zebra_open, and sets up the log",
292 { "", "Log file:","", 0},
295 "Directs the log to filename (or stderr)",
299 "Sets the logging level (or returns to default)",
303 "Sets the log prefix",
307 "writes an entry in the log",
310 { "", "Error handling:","", 0},
312 "Displays zebra's error status (code, str, add)",
315 "Displays zebra's error code",
318 "Displays zebra's error string",
321 "Displays zebra's additional error message",
324 { "", "Misc:","", 0},
331 { "help", "[command]",
332 "Gives help on command, or lists them all",
334 { "", "help [command] gives more info on command", "",0 },
336 {0,0,0,0} /* end marker */
339 int onecommand( char *line, char *outbuff)
342 char *args[MAX_NO_ARGS];
344 char argbuf[MAX_ARG_LEN];
346 strncpy(argbuf,line, MAX_ARG_LEN-1);
347 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
348 n=split_args(argbuf, args);
350 return 0; /* no command on line, too bad */
351 for (i=0;cmds[i].cmd;i++)
352 if (0==strcmp(cmds[i].cmd, args[0]))
355 args[0]= line + (args[1]-argbuf); /* rest of the line */
358 return ((cmds[i].testfunc)(args,outbuff));
360 sprintf (outbuff, "Unknown command '%s'. Try help",args[0] );
364 static int cmd_help( char *args[], char *outbuff)
367 char tmp[MAX_ARG_LEN];
369 { /* help for a single command */
370 for (i=0;cmds[i].cmd;i++)
371 if (0==strcmp(cmds[i].cmd, args[1]))
373 strcat(outbuff,cmds[i].cmd);
375 strcat(outbuff,cmds[i].args);
376 strcat(outbuff,"\n");
377 strcat(outbuff,cmds[i].explanation);
378 strcat(outbuff,"\n");
381 strcat(outbuff, "Unknown command ");
382 strcat(outbuff, args[1] );
385 { /* list all commands */
387 for (i=0;cmds[i].cmd;i++)
388 if (cmds[i].explanation)
390 /* sprintf(tmp, "%s %s %s\n",
391 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
393 strcat(tmp, cmds[i].cmd);
397 strcat(outbuff, tmp);
398 strcat(outbuff,"\n");
402 strcat(outbuff, cmds[i].args);
403 strcat(outbuff,"\n");
409 strcat(outbuff,"\n");
418 /* If Zebra reports an error after an operation,
419 * append it to the outbuff */
420 static void Zerrors ( char *outbuff)
423 char tmp[MAX_OUT_BUFF];
426 ec=zebra_errCode (zh);
429 sprintf(tmp, "Zebra error %d: %s, (%s) \n",
430 ec, zebra_errString (zh),
432 strcat(outbuff, tmp);
436 /**************************************
445 char buf[MAX_ARG_LEN];
446 char outbuff[MAX_OUT_BUFF];
447 #if HAVE_READLINE_READLINE_H
449 line_in=readline(PROMPT);
452 #if HAVE_READLINE_HISTORY_H
454 add_history(line_in);
456 if(strlen(line_in) > MAX_ARG_LEN-1) {
457 fprintf(stderr,"Input line too long\n");
465 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
469 rc=onecommand(buf, outbuff);
471 printf("%s\n", outbuff);
477 /**************************************
481 int main (int argc, char ** args)