Cleaning zebraapi (return conventions etc)
[idzebra-moved-to-github.git] / index / zebrash.c
1 /* zebrash.c - command-line interface to zebra API 
2  *  $Id: zebrash.c,v 1.10 2003-06-20 14:21:23 heikki 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, const char *prevout); 
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     int rc=0;
159     if (!rc)
160         rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
161     if (!rc)
162         rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
163     sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
164     if (!rc)
165         rc=onecommand(tmp,outbuff,"");
166     logf(LOG_APP,"quickstart");
167     if (!rc)
168         rc=onecommand("zebra_start",outbuff,"");
169     if (!rc)
170         rc=onecommand("zebra_open",outbuff,"");
171     if (!rc)
172         rc=onecommand("select_database Default",outbuff,"");
173     return rc;
174 }
175
176 /**************************************
177  * Log file handling
178  */
179
180 static int cmd_yaz_log_file( char *args[], char *outbuff)
181 {
182     char *fn = defarg(args[1],0);
183     char tmp[255];
184     sprintf(tmp, "sending yaz-log to %s\n",fn);
185     strcat(outbuff, tmp);
186     yaz_log_init_file(fn);
187     return 0; /* ok */
188 }
189
190 static int cmd_yaz_log_level( char *args[], char *outbuff)
191 {
192     int  lev = defargint(args[1],LOG_DEFAULT_LEVEL);
193     char tmp[255];
194     sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
195     strcat(outbuff, tmp);
196     yaz_log_init_level(lev);
197     return 0; /* ok */
198 }
199
200 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
201 {
202     char *pref = defarg(args[1],"ZebraSh");
203     char tmp[255];
204     sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
205     strcat(outbuff, tmp);
206     yaz_log_init_prefix(pref);
207     return 0; /* ok */
208 }
209
210 static int cmd_logf( char *args[], char *outbuff)
211 {
212     int lev = defargint(args[1],0);
213     char tmp[MAX_OUT_BUFF]="";
214     int i=1;
215     if (lev)
216         i=2;
217     else
218         lev=LOG_LOG; /* this is in the default set!*/
219     while (args[i])
220     {
221         strcat(tmp, args[i++]);
222         strcat(tmp, " ");
223     }
224     logf(lev,tmp);
225     return 0; /* ok */
226 }
227  
228 /****************
229  * Error handling 
230  */
231 static int cmd_err ( char *args[], char *outbuff)
232 {
233     char tmp[MAX_OUT_BUFF];
234     sprintf(tmp, "errCode: %d \nerrStr:  %s\nerrAdd:  %s \n",
235             zebra_errCode (zh),
236             zebra_errString (zh),  
237             zebra_errAdd (zh) );
238     strcat(outbuff, tmp);
239     return 0; /* ok */
240 }
241 static int cmd_errcode ( char *args[], char *outbuff)
242 {
243     char tmp[MAX_OUT_BUFF];
244     sprintf(tmp, "errCode: %d \n",
245             zebra_errCode (zh));
246     strcat(outbuff, tmp);
247     return 0; /* ok */
248 }
249 static int cmd_errstr ( char *args[], char *outbuff)
250 {
251     char tmp[MAX_OUT_BUFF];
252     sprintf(tmp, "errStr:  %s\n",
253             zebra_errString (zh));
254     strcat(outbuff, tmp);
255     return 0; /* ok */
256 }
257 static int cmd_erradd ( char *args[], char *outbuff)
258 {
259     char tmp[MAX_OUT_BUFF];
260     sprintf(tmp, "errAdd:  %s \n",
261             zebra_errAdd (zh) ); 
262     strcat(outbuff, tmp);
263     return 0; /* ok */
264 }
265
266 /**************************************
267  * Admin commands
268  */
269
270 static int cmd_init ( char *args[], char *outbuff)
271 {
272     zebra_init(zh);
273     return 0; /* ok */
274 }
275
276 static int cmd_select_database ( char *args[], char *outbuff)
277 {
278     char *db=args[1];
279     if (!db) {
280         db="Default";
281         strcat(outbuff,"Selecting database 'Default'\n");
282     }
283     return zebra_select_database(zh, db);
284 }
285  
286 static int cmd_create_database( char *args[], char *outbuff)
287 {
288     char *db=args[1];
289         int rc;
290     if (!db)
291         db="Default";
292     strcat(outbuff,"Creating database ");
293     strcat(outbuff,db);
294     strcat(outbuff,"\n");
295         
296     return zebra_create_database(zh, db);
297 }
298 /**************************************
299  * Command table, parser, and help 
300  */
301
302 struct cmdstruct
303 {
304     char * cmd;
305     char * args;
306     char * explanation;
307     int (*testfunc)(char *args[], char *outbuff);
308 } ;
309
310  
311 struct cmdstruct cmds[] = {
312     /* special cases:
313      *   if text is 0, does not list the command
314      *   if cmd is "", adds the args (and newline) in command listing
315      */
316     { "", "Starting and stopping:", "", 0 },
317     { "zebra_start", 
318       "[configfile]", 
319       "starts the zebra service. You need to call this first\n"
320       "if no configfile is given, assumes " DEFAULTCONFIG, 
321       cmd_zebra_start },
322     { "zebra_stop",   "", 
323       "stops the zebra service", 
324       cmd_zebra_stop },
325     { "zebra_open", "",  
326       "starts a zebra session. Once you have called zebra_start\n"
327       "you can call zebra_open to start working", 
328       cmd_zebra_open },
329     { "zebra_close", "", 
330       "closes a zebra session", 
331       cmd_zebra_close }, 
332     { "quickstart", "[configfile]", 
333       "Does a zebra_start, zebra_open, and sets up the log", 
334       cmd_quickstart }, 
335   
336     { "", "Log file:","", 0},  
337     { "yaz_log_file", 
338       "[filename]",
339       "Directs the log to filename (or stderr)",
340       cmd_yaz_log_file },
341     { "yaz_log_level", 
342       "[level]",
343       "Sets the logging level (or returns to default)",
344       cmd_yaz_log_level },
345     { "yaz_log_prefix", 
346       "[prefix]",
347       "Sets the log prefix",
348       cmd_yaz_log_prefix},    
349     { "logf", 
350       "[level] text...",
351       "writes an entry in the log",
352       cmd_logf},    
353
354     { "", "Error handling:","", 0},
355     { "err",  "",
356       "Displays zebra's error status (code, str, add)",
357       cmd_err},    
358     { "errcode",  "",
359       "Displays zebra's error code",
360       cmd_errcode},    
361     { "errstr",  "",
362       "Displays zebra's error string",
363       cmd_errstr},    
364     { "erradd",  "",
365       "Displays zebra's additional error message",
366       cmd_erradd},    
367   
368     { "", "Admin:","", 0}, 
369     { "init",  "",
370       "Initializes the zebra database, destroying all data in it",
371       cmd_init},    
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}, 
379     { "echo", "string", 
380       "ouputs the string", 
381       cmd_echo },
382     { "quit", "", 
383       "exits the program", 
384       cmd_quit },
385     { "help", "[command]", 
386       "Gives help on command, or lists them all", 
387       cmd_help },
388     { "", "help [command] gives more info on command", "",0 },   
389   
390     {0,0,0,0} /* end marker */
391 };
392  
393 int onecommand( 
394                 char *line,     /* input line */
395                 char *outbuff,  /* output goes here */
396                 const char *prevout) /* prev output, for 'expect' */
397 {
398     int i;
399     char *args[MAX_NO_ARGS];
400     int n;
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);
406
407 #if 0
408     for (i = 0; i <= n; i++)
409     {
410         const char *cp = args[i];
411         printf ("args %d :%s:\n", i, cp ? cp : "<null>");
412     }
413 #endif
414     if (0==n)
415         return -90; /* no command on line, too bad */
416
417     if (0==strcmp(args[0],"expect")) 
418     {
419         char *rest;
420         if (n>1)
421             rest= line + (args[1]-argbuf); /* rest of the line */
422         else
423             return -1; /* need something to expect */
424         printf("expecting '%s'\n",rest); /*!*/
425         if (0==strstr(prevout,rest))
426         {
427             printf( "Failed expectation, '%s' not found\n");
428             exit(9); 
429         }
430         return 0;
431     }
432     for (i=0;cmds[i].cmd;i++)
433         if (0==strcmp(cmds[i].cmd, args[0])) 
434         {
435             if (n>1)
436                 args[0]= line + (args[1]-argbuf); /* rest of the line */
437             else
438                 args[0]=""; 
439             return ((cmds[i].testfunc)(args,outbuff));
440         }
441     strcat(outbuff, "Unknown command '");
442     strcat(outbuff,args[0] );
443     strcat(outbuff,"'. Try help");
444     logf(LOG_APP,"Unknown command");
445     return -90; 
446 }
447  
448 static int cmd_help( char *args[], char *outbuff)
449
450     int i;
451     char tmp[MAX_ARG_LEN];
452     if (args[1]) 
453     { /* help for a single command */ 
454         for (i=0;cmds[i].cmd;i++)
455             if (0==strcmp(cmds[i].cmd, args[1])) 
456             {
457                 strcat(outbuff,cmds[i].cmd);
458                 strcat(outbuff,"  ");
459                 strcat(outbuff,cmds[i].args);
460                 strcat(outbuff,"\n");
461                 strcat(outbuff,cmds[i].explanation);
462                 strcat(outbuff,"\n");
463                 return 0;
464             }
465         strcat(outbuff, "Unknown command ");
466         strcat(outbuff, args[1] );
467     }
468     else 
469     { /* list all commands */
470         strcpy(tmp,"    ");
471         for (i=0;cmds[i].cmd;i++)
472             if (cmds[i].explanation)
473             {
474                 /* sprintf(tmp, "%s %s %s\n",
475                    cmds[i].cmd, cmds[i].args, cmds[i].explanation);
476                 */
477                 strcat(tmp, cmds[i].cmd);
478                 strcat(tmp,"  ");
479                 if (!*cmds[i].cmd)
480                 {
481                     strcat(outbuff, tmp);
482                     strcat(outbuff,"\n");
483                     strcpy(tmp,"    ");
484                     if (*cmds[i].args)
485                     {
486                         strcat(outbuff, cmds[i].args);
487                         strcat(outbuff,"\n");
488                     }
489                 }
490                 if (strlen(tmp)>50)
491                 {
492                     strcat(outbuff,tmp);
493                     strcat(outbuff,"\n");
494                     strcpy(tmp,"    ");
495                 }
496             }
497         strcat(outbuff,tmp);
498     }
499     return 0;
500 }
501  
502 /* If Zebra reports an error after an operation,
503  * append it to the outbuff and log it */
504 static void Zerrors ( char *outbuff)
505 {
506     int ec;
507     char tmp[MAX_OUT_BUFF];
508     if (!zh)
509         return ;
510     ec=zebra_errCode (zh);
511     if (ec)
512     {
513         sprintf(tmp, "   Zebra error %d: %s, (%s)",
514                 ec, zebra_errString (zh),
515                 zebra_errAdd (zh) );
516         strcat(outbuff, tmp);
517         strcat(outbuff, "\n");
518         logf(LOG_APP, tmp);
519         zebra_clearError(zh);
520     }
521 }
522   
523 /************************************** 
524  * The shell
525  */
526  
527 void shell()
528 {
529     int rc=0;
530     char tmp[MAX_ARG_LEN];
531     char outbuff[MAX_OUT_BUFF]="";
532     char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
533     while (rc!=-99)
534     {
535         char buf[MAX_ARG_LEN];
536 #if HAVE_READLINE_READLINE_H
537         char* line_in;
538         line_in=readline(PROMPT);
539         if (!line_in)
540             break;
541 #if HAVE_READLINE_HISTORY_H
542         if (*line_in)
543             add_history(line_in);
544 #endif
545         if(strlen(line_in) > MAX_ARG_LEN-1) {
546             fprintf(stderr,"Input line too long\n");
547             break;
548         };
549         strcpy(buf,line_in);
550         free (line_in);
551 #else    
552         printf (PROMPT); 
553         fflush (stdout);
554         if (!fgets (buf, MAX_ARG_LEN-1, stdin))
555             break; 
556 #endif 
557         
558         strncpy(prevout, outbuff, MAX_OUT_BUFF);
559         outbuff[0]='\0';
560         rc=onecommand(buf, outbuff, prevout);
561         if (rc==0)
562         {
563             strcat(outbuff, "   OK\n");
564             logf(LOG_APP, "OK");
565         }
566         else if (rc>-90)
567         {
568             sprintf(tmp, "   command returned %d\n",rc);
569             strcat(outbuff,tmp);
570         } 
571         Zerrors(outbuff);
572         printf("%s\n", outbuff);
573     } /* while */
574 } /* shell() */
575   
576  
577 /**************************************
578  * Main 
579  */
580  
581 int main (int argc, char ** args)
582 {
583     shell();
584     return 0;
585 } /* main */