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++]);
216 /**************************************
217 * Command table, parser, and help
225 int (*testfunc)(char *args[], char *outbuff);
229 struct cmdstruct cmds[] = {
231 * if text is 0, does not list the command
232 * if cmd is "", adds the args (and newline) in command listing
234 { "", "Starting and stopping:", "", 0 },
237 "starts the zebra service. You need to call this first\n"
238 "if no configfile is given, assumes " DEFAULTCONFIG,
241 "stops the zebra service",
244 "starts a zebra session. Once you have called zebra_start\n"
245 "you can call zebra_open to start working",
248 "closes a zebra session",
250 { "quickstart", "[configfile]",
251 "Does a zebra_start, zebra_open, and sets up the log",
254 { "", "Log file:","", 0},
257 "Directs the log to filename (or stderr)",
261 "Sets the logging level (or returns to default)",
265 "Sets the log prefix",
269 "writes an entry in the log",
272 { "", "Misc:","", 0},
279 { "help", "[command]",
280 "Gives help on command, or lists them all",
282 { "", "help [command] gives more info on command", "",0 },
284 {0,0,0,0} /* end marker */
287 int onecommand( char *line, char *outbuff)
290 char *args[MAX_NO_ARGS];
292 char argbuf[MAX_ARG_LEN];
293 strncpy(argbuf,line, MAX_ARG_LEN-1);
294 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
295 n=split_args(argbuf, args);
297 return 0; /* no command on line, too bad */
298 for (i=0;cmds[i].cmd;i++)
299 if (0==strcmp(cmds[i].cmd, args[0]))
302 args[0]= line + (args[1]-argbuf); /* rest of the line */
305 return ((cmds[i].testfunc)(args,outbuff));
307 sprintf (outbuff, "Unknown command '%s'. Try help",args[0] );
311 static int cmd_help( char *args[], char *outbuff)
314 char tmp[MAX_ARG_LEN];
316 { /* help for a single command */
317 for (i=0;cmds[i].cmd;i++)
318 if (0==strcmp(cmds[i].cmd, args[1]))
320 strcat(outbuff,cmds[i].cmd);
322 strcat(outbuff,cmds[i].args);
323 strcat(outbuff,"\n");
324 strcat(outbuff,cmds[i].explanation);
325 strcat(outbuff,"\n");
328 strcat(outbuff, "Unknown command ");
329 strcat(outbuff, args[1] );
332 { /* list all commands */
334 for (i=0;cmds[i].cmd;i++)
335 if (cmds[i].explanation)
337 /* sprintf(tmp, "%s %s %s\n",
338 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
340 strcat(tmp, cmds[i].cmd);
344 strcat(outbuff, tmp);
345 strcat(outbuff,"\n");
349 strcat(outbuff, cmds[i].args);
350 strcat(outbuff,"\n");
356 strcat(outbuff,"\n");
366 /**************************************
375 char buf[MAX_ARG_LEN];
376 char outbuff[MAX_OUT_BUFF];
377 #if HAVE_READLINE_READLINE_H
379 line_in=readline(PROMPT);
382 #if HAVE_READLINE_HISTORY_H
384 add_history(line_in);
386 if(strlen(line_in) > MAX_ARG_LEN-1) {
387 fprintf(stderr,"Input line too long\n");
395 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
399 rc=onecommand(buf, outbuff);
400 printf("%s\n", outbuff);
406 /**************************************
410 int main (int argc, char ** args)