X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=index%2Fzebrash.c;h=577f9424255438031069243a126e51bd92c7edd3;hp=b95737a57684a8a932cc9f0b92c5f74d661668e3;hb=6c9fcd3b5d3108702fa1ffc92dab4ab6060f9a19;hpb=c163115e3641d9f586c7aa5723e2685a31084ab3 diff --git a/index/zebrash.c b/index/zebrash.c index b95737a..577f942 100644 --- a/index/zebrash.c +++ b/index/zebrash.c @@ -1,6 +1,6 @@ -/* $Id: zebrash.c,v 1.19 2003-07-04 14:27:19 heikki Exp $ - Copyright (C) 2002,2003 - Index Data Aps +/* $Id: zebrash.c,v 1.32 2005-01-15 19:38:29 adam Exp $ + Copyright (C) 1995-2005 + Index Data ApS This file is part of the Zebra server. @@ -28,6 +28,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include +#include /* for isatty */ #if HAVE_READLINE_READLINE_H #include @@ -36,9 +37,11 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #endif -#include "zebraapi.h" +#include #include #include +#include +#include #include #define MAX_NO_ARGS 32 @@ -58,6 +61,9 @@ ZebraService zs=0; /* our global handle to zebra */ ZebraHandle zh=0; /* the current session */ /* time being, only one session works */ int nextrecno=1; /* record number to show next */ +static char *default_config = DEFAULTCONFIG; +static int log_level=0; + /************************************** * Help functions @@ -76,28 +82,28 @@ static int split_args( char *line, char** args ) p++; while (*p) { - while (*p==' ' || *p=='\t' || *p=='\n') - p++; - if (*p=='#') /* skip comments */ - break; - args[i++]=p; - args[i]=0; - while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#') - p++; - *p++='\0'; + while (*p==' ' || *p=='\t' || *p=='\n') + p++; + if (*p=='#') /* skip comments */ + break; + args[i++]=p; + args[i]=0; + while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#') + p++; + *p++='\0'; } n=i; while (nstream, + * rr->num_input_setnames, (const char **) + * rr->input_setnames, + * rr->output_setname, + * rr->sort_sequence, + * &rr->sort_status); + * zebra_result (zh, &rr->errcode, + * &rr->errstring); + * return 0; + * } + * + */ + /**************************************) * Command table, parser, and help */ @@ -501,7 +559,7 @@ struct cmdstruct cmds[] = { "[prefix]", "Sets the log prefix", cmd_yaz_log_prefix}, - { "logf", + { "yaz_log", "[level] text...", "writes an entry in the log", cmd_logf}, @@ -548,6 +606,7 @@ struct cmdstruct cmds[] = { "inserts (1), updates (2), or deletes (3) a record \n" "record-id must be a unique identifier for the record", cmd_exchange_record}, + { "","Searching and retrieving:","",0}, { "search_pqf","setname query", "search ", @@ -564,6 +623,10 @@ struct cmdstruct cmds[] = { { "s","[start] [numrecs] [resultset]", "shows a result", cmd_show}, + { "sort","sortspec", + "sorts a result set. (example spec: 1=4 >)", + cmd_sort}, + { "", "Misc:","", 0}, { "echo", "string", "ouputs the string", @@ -591,12 +654,12 @@ int onecommand( char *args[MAX_NO_ARGS]; int nargs; char argbuf[MAX_ARG_LEN]; - logf(LOG_APP,"%s",line); + yaz_log(log_level,"%s",line); strncpy(argbuf,line, MAX_ARG_LEN-1); argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */ /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/ nargs=split_args(argbuf, args); - + #if 0 for (i = 0; i <= n; i++) { @@ -609,29 +672,29 @@ int onecommand( if (0==strcmp(args[0],"expect")) { - char *rest; + char *rest; if (nargs>1) /* args[0] is not yet set, can't use restargs */ rest= line + (args[1]-argbuf); /* rest of the line */ else return -1; /* need something to expect */ - if (0==strstr(prevout,rest)) - { - printf( "Failed expectation, '%s' not found\n", rest); + if (0==strstr(prevout,rest)) + { + printf( "Failed expectation, '%s' not found\n", rest); exit(9); - } - return 0; + } + return 0; } for (i=0;cmds[i].cmd;i++) - if (0==strcmp(cmds[i].cmd, args[0])) - { - if (nargs>1) - args[0]= line + (args[1]-argbuf); /* rest of the line */ - else - args[0]=""; - return ((cmds[i].testfunc)(args,outbuff)); - } + if (0==strcmp(cmds[i].cmd, args[0])) + { + if (nargs>1) + args[0]= line + (args[1]-argbuf); /* rest of the line */ + else + args[0]=""; + return ((cmds[i].testfunc)(args,outbuff)); + } wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]); - logf(LOG_APP,"Unknown command"); + yaz_log(log_level,"Unknown command"); return -90; } @@ -641,20 +704,20 @@ static int cmd_help( char *args[], WRBUF outbuff) int linelen; if (args[1]) { /* help for a single command */ - for (i=0;cmds[i].cmd;i++) - if (0==strcmp(cmds[i].cmd, args[1])) - { + for (i=0;cmds[i].cmd;i++) + if (0==strcmp(cmds[i].cmd, args[1])) + { wrbuf_printf(outbuff,"%s %s\n%s\n", cmds[i].cmd, cmds[i].args, - cmds[i].explanation); - return 0; - } - wrbuf_printf(outbuff, "Unknown command '%s'", args[1]); - } + cmds[i].explanation); + return 0; + } + wrbuf_printf(outbuff, "Unknown command '%s'", args[1]); + } else { /* list all commands */ linelen=9999; - for (i=0;cmds[i].cmd;i++) + for (i=0;cmds[i].cmd;i++) { if (*cmds[i].cmd) { /* ordinary command */ @@ -682,20 +745,20 @@ static void Zerrors ( WRBUF outbuff) { int ec; if (!zh) - return ; + return ; ec=zebra_errCode (zh); if (ec) { - logf(LOG_APP, " Zebra error %d: %s, (%s)", - ec, zebra_errString (zh), - zebra_errAdd (zh) ); - wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n", - ec, zebra_errString (zh), - zebra_errAdd (zh) ); - zebra_clearError(zh); + yaz_log(log_level, " Zebra error %d: %s, (%s)", + ec, zebra_errString (zh), + zebra_errAdd (zh) ); + wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n", + ec, zebra_errString (zh), + zebra_errAdd (zh) ); + zebra_clearError(zh); } } - + /************************************** * The shell */ @@ -708,54 +771,92 @@ void shell() wrbuf_puts(outbuff,"Zebrash at your service"); while (rc!=-99) { - char buf[MAX_ARG_LEN]; + char *nl_cp; + char buf[MAX_ARG_LEN]; + char* line_in = 0; #if HAVE_READLINE_READLINE_H - char* line_in; + if (isatty(0)) { line_in=readline(PROMPT); if (!line_in) - break; + break; #if HAVE_READLINE_HISTORY_H if (*line_in) - add_history(line_in); + add_history(line_in); +#endif + } #endif + /* line_in != NULL if readine is present and input is a tty */ + + printf (PROMPT); + fflush (stdout); + if (line_in) + { if(strlen(line_in) > MAX_ARG_LEN-1) { - fprintf(stderr,"Input line too long\n"); - break; + fprintf(stderr,"Input line too long\n"); + break; } strcpy(buf,line_in); free (line_in); -#else - printf (PROMPT); - fflush (stdout); + } + else + { if (!fgets (buf, MAX_ARG_LEN-1, stdin)) - break; -#endif + break; + } - strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF); + /* get rid of \n in line */ + if ((nl_cp = strchr(buf, '\n'))) + *nl_cp = '\0'; + strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF); wrbuf_rewind(outbuff); - rc=onecommand(buf, outbuff, prevout); - if (rc==0) - { - wrbuf_puts(outbuff, " OK\n"); - logf(LOG_APP, "OK"); - } - else if (rc>-90) - { - wrbuf_printf(outbuff, " command returned %d\n",rc); - } - Zerrors(outbuff); - printf("%s\n", wrbuf_buf(outbuff)); + rc=onecommand(buf, outbuff, prevout); + if (rc==0) + { + wrbuf_puts(outbuff, " OK\n"); + yaz_log(log_level, "OK"); + } + else if (rc>-90) + { + wrbuf_printf(outbuff, " command returned %d\n",rc); + } + Zerrors(outbuff); + printf("%s\n", wrbuf_buf(outbuff)); } /* while */ wrbuf_free(outbuff,1); } /* shell() */ - - + + +static void usage() +{ + printf ("usage:\n"); + printf ("zebrash [-c config]\n"); + exit(1); +} /************************************** * Main */ - -int main (int argc, char ** args) + +int main (int argc, char ** argv) { + int ret; + char *arg = 0; + while ((ret = options ("c:h", argv, argc, &arg)) != -2) + { + switch(ret) + { + case 'c': + default_config = arg; + break; + case 'h': + usage(); + /* FIXME - handle -v */ + default: + fprintf(stderr, "bad option %s\n", arg); + usage(); + } + } + log_level=yaz_log_module_level("zebrash"); + shell(); return 0; } /* main */