Removed unused variable
[idzebra-moved-to-github.git] / index / zebrash.c
1 /* zebrash.c - command-line interface to zebra API 
2  *  $Id: zebrash.c,v 1.7 2003-03-05 16:44:02 adam Exp $
3  *
4  * Copyrigth 2003 Index Data Aps
5  *
6  */
7  
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h> 
11 #include <ctype.h>
12
13 #if HAVE_READLINE_READLINE_H
14 #include <readline/readline.h> 
15 #endif
16 #if HAVE_READLINE_HISTORY_H
17 #include <readline/history.h>
18 #endif
19
20 #include "zebraapi.h"
21 #include <yaz/log.h>
22
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"
28
29 /**************************************
30  * Global variables (yuck!)
31  */
32
33 ZebraService zs=0;  /* our global handle to zebra */
34 ZebraHandle  zh=0;  /* the current session */
35                   /* time being, only one session works */
36
37 /**************************************
38  * Help functions
39  */
40
41  
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 ?? */
46   char *p=line;
47   int i=0;
48   int n=0;
49   args[0]=0; /* by default */
50   while (*p==' ' || *p=='\t' || *p=='\n')
51     p++;
52   while (*p)
53   {
54     while (*p==' ' || *p=='\t' || *p=='\n')
55       p++;
56     if (*p=='#')  /* skip comments */
57       break;  
58     args[i++]=p;
59     args[i]=0;
60     while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
61       p++;
62     *p++='\0';
63   }
64   n=i;
65   while (n<MAX_NO_ARGS)
66     args[n++]=0;
67   return i;
68 }
69
70 static char *defarg( char *arg, char *def )
71 {
72   if (!arg)
73     return def;
74   if (!*arg)
75     return def;
76   return arg;
77 }
78 static int defargint( char *arg, int def )
79 {
80   int v=def;
81   char *a=defarg(arg,0);
82   if (a)
83     sscanf(a," %i", &v);
84   return v;
85 }
86
87 int onecommand( char *line, char *outbuff);
88  
89  /**************************************
90  * Simple support commands
91  */
92
93  int cmd_echo( char *args[], char *outbuff)
94  {
95    strcpy(outbuff, args[0]);
96    return 0;
97  }
98  
99  int cmd_quit( char *args[], char *outbuff)
100  {
101    strcpy(outbuff, "bye");
102    return -99; /* special stop signal */
103  }
104
105 /**************************************
106  * Tests for starting and stopping zebra, etc
107  */
108  
109  static int cmd_help( char *args[], char *outbuff); 
110  
111  static int cmd_zebra_start( char *args[], char *outbuff)
112  {
113    char *conf=args[1];
114    if (!conf || !*conf) {
115      strcat(outbuff,"no config file specified, using "
116                      DEFAULTCONFIG "\n" );
117      conf=DEFAULTCONFIG;
118    }
119    zs=zebra_start(conf);
120    if (!zs) {
121      strcpy(outbuff, "zebra_start failed" );
122      return 2;
123    }
124    return 0; /* ok */
125  }
126  
127  static int cmd_zebra_stop( char *args[], char *outbuff)
128  {
129    if (!zs)
130      strcat(outbuff,"zebra seems not to have been started, "
131                     "stopping anyway\n");
132    zebra_stop(zs);
133    zs=0;
134    return 0; /* ok */
135  }
136
137 static int cmd_zebra_open( char *args[], char *outbuff)
138 {
139   if (!zs)
140     strcat(outbuff,"zebra seems not to have been started, "
141                    "trying anyway\n");
142   zh=zebra_open(zs);
143   return 0; /* ok */
144 }
145
146 static int cmd_zebra_close( char *args[], char *outbuff)
147 {
148   if (!zh)
149     strcat(outbuff,"Seems like you have not called zebra_open,"
150                    "trying anyway\n");
151   zebra_close(zh);
152   return 0; /* ok */
153 }
154
155 static int cmd_quickstart( char *args[], char *outbuff)
156 {
157   char tmp[128];
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");
167   return 0;
168 }
169
170 /**************************************
171  * Log file handling
172  */
173
174 static int cmd_yaz_log_file( char *args[], char *outbuff)
175 {
176   char *fn = defarg(args[1],0);
177   char tmp[255];
178   sprintf(tmp, "sending yaz-log to %s\n",fn);
179   strcat(outbuff, tmp);
180   yaz_log_init_file(fn);
181   return 0; /* ok */
182 }
183
184 static int cmd_yaz_log_level( char *args[], char *outbuff)
185 {
186   int  lev = defargint(args[1],LOG_DEFAULT_LEVEL);
187   char tmp[255];
188   sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
189   strcat(outbuff, tmp);
190   yaz_log_init_level(lev);
191   return 0; /* ok */
192 }
193
194 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
195 {
196   char *pref = defarg(args[1],"ZebraSh");
197   char tmp[255];
198   sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
199   strcat(outbuff, tmp);
200   yaz_log_init_prefix(pref);
201   return 0; /* ok */
202 }
203
204 static int cmd_logf( char *args[], char *outbuff)
205 {
206   int lev = defargint(args[1],0);
207   char tmp[MAX_OUT_BUFF]="";
208   int i=1;
209   if (lev)
210     i=2;
211   else
212     lev=LOG_LOG; /* this is in the default set!*/
213   while (args[i])
214   {
215     strcat(tmp, args[i++]);
216     strcat(tmp, " ");
217   }
218   logf(lev,tmp);
219   return 0; /* ok */
220 }
221  
222 /****************
223  * Error handling 
224  */
225 static int cmd_err ( char *args[], char *outbuff)
226 {
227   char tmp[MAX_OUT_BUFF];
228   sprintf(tmp, "errCode: %d \nerrStr:  %s\nerrAdd:  %s \n",
229     zebra_errCode (zh),
230     zebra_errString (zh),  
231     zebra_errAdd (zh) );
232   strcat(outbuff, tmp);
233   return 0; /* ok */
234 }
235 static int cmd_errcode ( char *args[], char *outbuff)
236 {
237   char tmp[MAX_OUT_BUFF];
238   sprintf(tmp, "errCode: %d \n",
239     zebra_errCode (zh));
240   strcat(outbuff, tmp);
241   return 0; /* ok */
242 }
243 static int cmd_errstr ( char *args[], char *outbuff)
244 {
245   char tmp[MAX_OUT_BUFF];
246   sprintf(tmp, "errStr:  %s\n",
247     zebra_errString (zh));
248   strcat(outbuff, tmp);
249   return 0; /* ok */
250 }
251 static int cmd_erradd ( char *args[], char *outbuff)
252 {
253   char tmp[MAX_OUT_BUFF];
254   sprintf(tmp, "errAdd:  %s \n",
255     zebra_errAdd (zh) ); 
256   strcat(outbuff, tmp);
257   return 0; /* ok */
258 }
259
260 /**************************************
261  * Admin commands
262  */
263
264 static int cmd_init ( char *args[], char *outbuff)
265 {
266   zebra_init(zh);
267   return 0; /* ok */
268 }
269
270 static int cmd_select_database ( char *args[], char *outbuff)
271 {
272   char *db=args[1];
273   if (!db)
274     db="Default";
275   return zebra_select_database(zh, args[1]);
276 }
277  
278 /**************************************
279  * Command table, parser, and help 
280  */
281
282 struct cmdstruct
283 {
284   char * cmd;
285   char * args;
286   char * explanation;
287   int (*testfunc)(char *args[], char *outbuff);
288 } ;
289
290  
291 struct cmdstruct cmds[] = {
292   /* special cases:
293    *   if text is 0, does not list the command
294    *   if cmd is "", adds the args (and newline) in command listing
295    */
296   { "", "Starting and stopping:", "", 0 },
297   { "zebra_start", 
298     "[configfile]", 
299     "starts the zebra service. You need to call this first\n"
300     "if no configfile is given, assumes " DEFAULTCONFIG, 
301     cmd_zebra_start },
302   { "zebra_stop",   "", 
303     "stops the zebra service", 
304     cmd_zebra_stop },
305   { "zebra_open", "",  
306     "starts a zebra session. Once you have called zebra_start\n"
307     "you can call zebra_open to start working", 
308     cmd_zebra_open },
309   { "zebra_close", "", 
310     "closes a zebra session", 
311     cmd_zebra_close }, 
312   { "quickstart", "[configfile]", 
313     "Does a zebra_start, zebra_open, and sets up the log", 
314     cmd_quickstart }, 
315   
316   { "", "Log file:","", 0},  
317   { "yaz_log_file", 
318     "[filename]",
319     "Directs the log to filename (or stderr)",
320     cmd_yaz_log_file },
321   { "yaz_log_level", 
322     "[level]",
323     "Sets the logging level (or returns to default)",
324     cmd_yaz_log_level },
325   { "yaz_log_prefix", 
326     "[prefix]",
327     "Sets the log prefix",
328     cmd_yaz_log_prefix},    
329   { "logf", 
330     "[level] text...",
331     "writes an entry in the log",
332     cmd_logf},    
333
334   { "", "Error handling:","", 0},
335   { "err",  "",
336     "Displays zebra's error status (code, str, add)",
337     cmd_err},    
338   { "errcode",  "",
339     "Displays zebra's error code",
340     cmd_errcode},    
341   { "errstr",  "",
342     "Displays zebra's error string",
343     cmd_errstr},    
344   { "erradd",  "",
345     "Displays zebra's additional error message",
346     cmd_erradd},    
347   
348   { "", "Admin:","", 0}, 
349   { "init",  "",
350     "Initializes the zebra database, destroying all data in it",
351     cmd_init},    
352   { "select_database",  "basename",
353     "Selects a database",
354     cmd_select_database},    
355   
356   { "", "Misc:","", 0}, 
357   { "echo", "string", 
358     "ouputs the string", 
359     cmd_echo },
360   { "quit", "", 
361     "exits the program", 
362     cmd_quit },
363   { "help", "[command]", 
364     "Gives help on command, or lists them all", 
365     cmd_help },
366   { "", "help [command] gives more info on command", "",0 },   
367   
368   {0,0,0,0} /* end marker */
369 };
370  
371 int onecommand( char *line, char *outbuff)
372 {
373   int i;
374   char *args[MAX_NO_ARGS];
375   int n;
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);
381   if (0==n)
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])) 
385     {
386       if (n>1)
387         args[0]= line + (args[1]-argbuf); /* rest of the line */
388       else
389         args[0]=""; 
390       return ((cmds[i].testfunc)(args,outbuff));
391     }
392   strcat(outbuff, "Unknown command '");
393   strcat(outbuff,args[0] );
394   strcat(outbuff,"'. Try help");
395   logf(LOG_APP,"Unknown command");
396   return -2; 
397 }
398  
399  static int cmd_help( char *args[], char *outbuff)
400  { 
401   int i;
402   char tmp[MAX_ARG_LEN];
403   if (args[1]) 
404   { /* help for a single command */ 
405     for (i=0;cmds[i].cmd;i++)
406       if (0==strcmp(cmds[i].cmd, args[1])) 
407       {
408         strcat(outbuff,cmds[i].cmd);
409         strcat(outbuff,"  ");
410         strcat(outbuff,cmds[i].args);
411         strcat(outbuff,"\n");
412         strcat(outbuff,cmds[i].explanation);
413         strcat(outbuff,"\n");
414         return 0;
415       }
416     strcat(outbuff, "Unknown command ");
417     strcat(outbuff, args[1] );
418   }
419   else 
420   { /* list all commands */
421     strcpy(tmp,"    ");
422     for (i=0;cmds[i].cmd;i++)
423       if (cmds[i].explanation)
424       {
425        /* sprintf(tmp, "%s %s %s\n",
426           cmds[i].cmd, cmds[i].args, cmds[i].explanation);
427         */
428         strcat(tmp, cmds[i].cmd);
429         strcat(tmp,"  ");
430         if (!*cmds[i].cmd)
431         {
432           strcat(outbuff, tmp);
433           strcat(outbuff,"\n");
434           strcpy(tmp,"    ");
435           if (*cmds[i].args)
436           {
437             strcat(outbuff, cmds[i].args);
438             strcat(outbuff,"\n");
439           }
440         }
441         if (strlen(tmp)>50)
442         {
443           strcat(outbuff,tmp);
444           strcat(outbuff,"\n");
445           strcpy(tmp,"    ");
446         }
447       }
448     strcat(outbuff,tmp);
449   }
450   return 0;
451 }
452  
453 /* If Zebra reports an error after an operation,
454  * append it to the outbuff and log it */
455 static void Zerrors ( char *outbuff)
456 {
457   int ec;
458   char tmp[MAX_OUT_BUFF];
459   if (!zh)
460     return ;
461   ec=zebra_errCode (zh);
462   if (ec)
463   {
464     sprintf(tmp, "Zebra error %d: %s, (%s)",
465       ec, zebra_errString (zh),
466       zebra_errAdd (zh) );
467     strcat(outbuff, tmp);
468     strcat(outbuff, "\n");
469     logf(LOG_APP, tmp);
470   }
471 }
472   
473 /************************************** 
474  * The shell
475  */
476  
477 void shell()
478 {
479   int rc=0;
480   char tmp[MAX_ARG_LEN];
481   while (rc!=-99)
482   {
483     char buf[MAX_ARG_LEN];
484     char outbuff[MAX_OUT_BUFF];
485 #if HAVE_READLINE_READLINE_H
486     char* line_in;
487           line_in=readline(PROMPT);
488           if (!line_in)
489             break;
490 #if HAVE_READLINE_HISTORY_H
491           if (*line_in)
492             add_history(line_in);
493 #endif
494           if(strlen(line_in) > MAX_ARG_LEN-1) {
495             fprintf(stderr,"Input line too long\n");
496             break;
497           };
498           strcpy(buf,line_in);
499           free (line_in);
500 #else    
501           printf (PROMPT); 
502           fflush (stdout);
503           if (!fgets (buf, MAX_ARG_LEN-1, stdin))
504             break; 
505 #endif 
506     outbuff[0]='\0';
507     rc=onecommand(buf, outbuff);
508     if (rc==0)
509     {
510       strcat(outbuff, "OK\n");
511       logf(LOG_APP, "OK");
512     }
513     else if (rc > 0)
514     {
515       sprintf(tmp, "command returned %d\n",rc);
516       strcat(outbuff,tmp);
517     } 
518     Zerrors(outbuff);
519         printf("%s\n", outbuff);
520   } /* while */
521 } /* shell() */
522   
523  
524 /**************************************
525  * Main 
526  */
527  
528 int main (int argc, char ** args)
529 {
530   shell();
531   return 0;
532 } /* main */