1 /* zebrash.c - command-line interface to zebra API
2 * $Id: zebrash.c,v 1.11 2003-06-23 14:35:41 heikki 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, const char *prevout);
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)
160 rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
162 rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
163 sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP);
165 rc=onecommand(tmp,outbuff,"");
166 logf(LOG_APP,"quickstart");
168 rc=onecommand("zebra_start",outbuff,"");
170 rc=onecommand("zebra_open",outbuff,"");
172 rc=onecommand("select_database Default",outbuff,"");
176 /**************************************
180 static int cmd_yaz_log_file( char *args[], char *outbuff)
182 char *fn = defarg(args[1],0);
184 sprintf(tmp, "sending yaz-log to %s\n",fn);
185 strcat(outbuff, tmp);
186 yaz_log_init_file(fn);
190 static int cmd_yaz_log_level( char *args[], char *outbuff)
192 int lev = defargint(args[1],LOG_DEFAULT_LEVEL);
194 sprintf(tmp, "setting yaz-log to level %d (ox%x)\n",lev,lev);
195 strcat(outbuff, tmp);
196 yaz_log_init_level(lev);
200 static int cmd_yaz_log_prefix( char *args[], char *outbuff)
202 char *pref = defarg(args[1],"ZebraSh");
204 sprintf(tmp, "setting yaz-log prefix to %s\n",pref);
205 strcat(outbuff, tmp);
206 yaz_log_init_prefix(pref);
210 static int cmd_logf( char *args[], char *outbuff)
212 int lev = defargint(args[1],0);
213 char tmp[MAX_OUT_BUFF]="";
218 lev=LOG_LOG; /* this is in the default set!*/
221 strcat(tmp, args[i++]);
231 static int cmd_err ( char *args[], char *outbuff)
233 char tmp[MAX_OUT_BUFF];
234 sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
236 zebra_errString (zh),
238 strcat(outbuff, tmp);
241 static int cmd_errcode ( char *args[], char *outbuff)
243 char tmp[MAX_OUT_BUFF];
244 sprintf(tmp, "errCode: %d \n",
246 strcat(outbuff, tmp);
249 static int cmd_errstr ( char *args[], char *outbuff)
251 char tmp[MAX_OUT_BUFF];
252 sprintf(tmp, "errStr: %s\n",
253 zebra_errString (zh));
254 strcat(outbuff, tmp);
257 static int cmd_erradd ( char *args[], char *outbuff)
259 char tmp[MAX_OUT_BUFF];
260 sprintf(tmp, "errAdd: %s \n",
262 strcat(outbuff, tmp);
266 /**************************************
270 static int cmd_init ( char *args[], char *outbuff)
276 static int cmd_select_database ( char *args[], char *outbuff)
281 strcat(outbuff,"Selecting database 'Default'\n");
283 return zebra_select_database(zh, db);
286 static int cmd_create_database( char *args[], char *outbuff)
292 strcat(outbuff,"Creating database ");
294 strcat(outbuff,"\n");
296 return zebra_create_database(zh, db);
299 static int cmd_begin_trans( char *args[], char *outbuff)
302 if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
304 return zebra_begin_trans(zh,rw);
306 static int cmd_end_trans( char *args[], char *outbuff)
308 return zebra_end_trans(zh);
310 /*************************************
311 * Inserting and deleting
314 static int cmd_record_insert( char *args[], char *outbuff)
317 char buf[MAX_ARG_LEN];
325 strcat(buf, args[i++]);
328 rc=zebra_record_insert(zh,buf, strlen(buf), &sysno);
331 sprintf(buf,"ok sysno=%d\n",sysno);
338 static int cmd_exchange_record( char *args[], char *outbuff)
342 char *action = args[3];
345 char buf[MAX_ARG_LEN];
346 if (!(base && id && action && args[4] ))
348 strcat(outbuff,"Missing arguments!\n");
349 onecommand("help exchange_record", outbuff, "");
354 strcat(buf, args[i++]);
357 rc=zebra_admin_exchange_record(zh, base, buf, strlen(buf),
358 id, strlen(id), atoi(action));
363 /**************************************)
364 * Command table, parser, and help
372 int (*testfunc)(char *args[], char *outbuff);
376 struct cmdstruct cmds[] = {
378 * if text is 0, does not list the command
379 * if cmd is "", adds the args (and newline) in command listing
381 { "", "Starting and stopping:", "", 0 },
384 "starts the zebra service. You need to call this first\n"
385 "if no configfile is given, assumes " DEFAULTCONFIG,
388 "stops the zebra service",
391 "starts a zebra session. Once you have called zebra_start\n"
392 "you can call zebra_open to start working",
395 "closes a zebra session",
397 { "quickstart", "[configfile]",
398 "Does a zebra_start, zebra_open, and sets up the log",
401 { "", "Log file:","", 0},
404 "Directs the log to filename (or stderr)",
408 "Sets the logging level (or returns to default)",
412 "Sets the log prefix",
416 "writes an entry in the log",
419 { "", "Error handling:","", 0},
421 "Displays zebra's error status (code, str, add)",
424 "Displays zebra's error code",
427 "Displays zebra's error string",
430 "Displays zebra's additional error message",
433 { "", "Admin:","", 0},
435 "Initializes the zebra database, destroying all data in it",
437 { "select_database", "basename",
438 "Selects a database",
439 cmd_select_database},
440 { "create_database", "basename",
441 "Creates a database",
442 cmd_create_database},
443 { "begin_trans", "[rw]",
444 "Begins a transaction. rw=1 means write, otherwise read-only",
447 "Ends a transaction",
450 { "","Updating:","",0},
451 { "record_insert","record",
452 "inserts an sgml record into Default",
454 { "exchange_record","database record-id action record",
455 "inserts (1), updates (2), or deletes (3) a record \n"
456 "record-id must be a unique identifier for the record",
457 cmd_exchange_record},
458 { "", "Misc:","", 0},
465 { "help", "[command]",
466 "Gives help on command, or lists them all",
468 { "", "help [command] gives more info on command", "",0 },
470 {0,0,0,0} /* end marker */
474 char *line, /* input line */
475 char *outbuff, /* output goes here */
476 const char *prevout) /* prev output, for 'expect' */
479 char *args[MAX_NO_ARGS];
481 char argbuf[MAX_ARG_LEN];
482 logf(LOG_APP,"%s",line);
483 strncpy(argbuf,line, MAX_ARG_LEN-1);
484 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
485 n=split_args(argbuf, args);
488 for (i = 0; i <= n; i++)
490 const char *cp = args[i];
491 printf ("args %d :%s:\n", i, cp ? cp : "<null>");
495 return -90; /* no command on line, too bad */
497 if (0==strcmp(args[0],"expect"))
501 rest= line + (args[1]-argbuf); /* rest of the line */
503 return -1; /* need something to expect */
504 printf("expecting '%s'\n",rest); /*!*/
505 if (0==strstr(prevout,rest))
507 printf( "Failed expectation, '%s' not found\n");
512 for (i=0;cmds[i].cmd;i++)
513 if (0==strcmp(cmds[i].cmd, args[0]))
516 args[0]= line + (args[1]-argbuf); /* rest of the line */
519 return ((cmds[i].testfunc)(args,outbuff));
521 strcat(outbuff, "Unknown command '");
522 strcat(outbuff,args[0] );
523 strcat(outbuff,"'. Try help");
524 logf(LOG_APP,"Unknown command");
528 static int cmd_help( char *args[], char *outbuff)
531 char tmp[MAX_ARG_LEN];
533 { /* help for a single command */
534 for (i=0;cmds[i].cmd;i++)
535 if (0==strcmp(cmds[i].cmd, args[1]))
537 strcat(outbuff,cmds[i].cmd);
539 strcat(outbuff,cmds[i].args);
540 strcat(outbuff,"\n");
541 strcat(outbuff,cmds[i].explanation);
542 strcat(outbuff,"\n");
545 strcat(outbuff, "Unknown command ");
546 strcat(outbuff, args[1] );
549 { /* list all commands */
551 for (i=0;cmds[i].cmd;i++)
552 if (cmds[i].explanation)
554 /* sprintf(tmp, "%s %s %s\n",
555 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
557 strcat(tmp, cmds[i].cmd);
561 strcat(outbuff, tmp);
562 strcat(outbuff,"\n");
566 strcat(outbuff, cmds[i].args);
567 strcat(outbuff,"\n");
573 strcat(outbuff,"\n");
582 /* If Zebra reports an error after an operation,
583 * append it to the outbuff and log it */
584 static void Zerrors ( char *outbuff)
587 char tmp[MAX_OUT_BUFF];
590 ec=zebra_errCode (zh);
593 sprintf(tmp, " Zebra error %d: %s, (%s)",
594 ec, zebra_errString (zh),
596 strcat(outbuff, tmp);
597 strcat(outbuff, "\n");
599 zebra_clearError(zh);
603 /**************************************
610 char tmp[MAX_ARG_LEN];
611 char outbuff[MAX_OUT_BUFF]="";
612 char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
615 char buf[MAX_ARG_LEN];
616 #if HAVE_READLINE_READLINE_H
618 line_in=readline(PROMPT);
621 #if HAVE_READLINE_HISTORY_H
623 add_history(line_in);
625 if(strlen(line_in) > MAX_ARG_LEN-1) {
626 fprintf(stderr,"Input line too long\n");
634 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
638 strncpy(prevout, outbuff, MAX_OUT_BUFF);
640 rc=onecommand(buf, outbuff, prevout);
643 strcat(outbuff, " OK\n");
648 sprintf(tmp, " command returned %d\n",rc);
652 printf("%s\n", outbuff);
657 /**************************************
661 int main (int argc, char ** args)