35 #if HAVE_READLINE_READLINE_H
36 #include <readline/readline.h>
38 #if HAVE_READLINE_HISTORY_H
39 #include <readline/history.h>
44 #include <yaz/proto.h>
45 #include <yaz/sortspec.h>
46 #include <yaz/options.h>
47 #include <yaz/wrbuf.h>
48 #include <yaz/oid_db.h>
50 #define MAX_NO_ARGS 32
51 #define MAX_OUT_BUFF 4096
52 #define MAX_ARG_LEN 1024
53 #define PROMPT "ZebraSh>"
54 #define DEFAULTCONFIG "./zebra.cfg"
55 #define DEFAULTDATABASE "Default"
56 #define DEFAULTRESULTSET "MyResultSet"
84 while (*p==
' ' || *p==
'\t' || *p==
'\n')
88 while (*p==
' ' || *p==
'\t' || *p==
'\n')
94 while (*p && *p!=
' ' && *p!=
'\t' && *p!=
'\n' && *p!=
'#')
104 static char *
defarg(
char *arg,
char *def )
125 int skiplen= args[n]-args[1];
126 if (skiplen > strlen(args[0]))
128 return args[0]+skiplen;
131 int onecommand(
char *line, WRBUF outbuff,
const char *prevout);
139 wrbuf_printf(outbuff,
"%s\n",
restargs(args,1));
155 wrbuf_puts(outbuff,
"bye");
163 static int cmd_help(
char *args[], WRBUF outbuff);
168 if (!conf || !*conf) {
169 wrbuf_puts(outbuff,
"no config file specified, using ");
171 wrbuf_puts(outbuff,
"\n");
176 wrbuf_puts(outbuff,
"zebra_start failed" );
185 wrbuf_puts(outbuff,
"zebra seems not to have been started, "
186 "stopping anyway\n");
195 wrbuf_puts(outbuff,
"zebra seems not to have been started, "
204 wrbuf_puts(outbuff,
"Seems like you have not called zebra_open,"
215 rc=
onecommand(
"yaz_log_file zebrash.log",outbuff,
"");
217 rc=
onecommand(
"yaz_log_prefix ZebraSh", outbuff,
"");
218 sprintf(tmp,
"yaz_log_level 0x%x", YLOG_DEFAULT_LEVEL |
log_level );
229 rc=
onecommand(
"select_database Default",outbuff,
"");
239 char *fn =
defarg(args[1],0);
240 wrbuf_printf(outbuff,
"sending yaz-log to %s\n",fn);
241 yaz_log_init_file(fn);
247 int lev =
defargint(args[1],YLOG_DEFAULT_LEVEL);
248 wrbuf_printf(outbuff,
"setting yaz-log to level %d (ox%x)\n",lev,lev);
249 yaz_log_init_level(lev);
255 char *pref =
defarg(args[1],
"ZebraSh");
256 wrbuf_printf(outbuff,
"setting yaz-log prefix to %s\n",pref);
257 yaz_log_init_prefix(pref);
269 yaz_log( lev,
"%s",
restargs(args,i));
276 static int cmd_err (
char *args[], WRBUF outbuff)
278 wrbuf_printf(outbuff,
"errCode: %d \nerrStr: %s\nerrAdd: %s \n",
286 wrbuf_printf(outbuff,
"errCode: %d \n",
292 wrbuf_printf(outbuff,
"errStr: %s\n",
298 wrbuf_printf(outbuff,
"errAdd: %s \n",
316 wrbuf_printf(outbuff,
"Selecting database '%s'\n",db);
323 wrbuf_printf(outbuff,
"Creating database '%s'\n",db);
333 wrbuf_printf(outbuff,
"Dropping database '%s'\n",db);
340 if (args[1] && ( (args[1][0]==
'1') || (args[1][0]==
'w') ))
369 wrbuf_printf(outbuff,
"ok sysno=" ZINT_FORMAT "\n",sysno);
378 char *action = args[2];
381 if (!(
id && action && args[4] ))
383 wrbuf_puts(outbuff,
"Missing arguments!\n");
384 onecommand(
"help exchange_record", outbuff,
"");
408 wrbuf_printf(outbuff,
ZINT_FORMAT " hits found\n", hits);
417 WRBUF qry = wrbuf_alloc();
418 if (0==strstr(args[0],
"@attr"))
419 wrbuf_puts(qry,
"@attr 1=/ ");
423 wrbuf_printf(outbuff,
"find %s\n",wrbuf_cstr(qry));
427 wrbuf_printf(outbuff,
ZINT_FORMAT " hits found\n", hits);
442 Z_RecordComposition *pcomp=0;
445 odr = odr_createmem(ODR_ENCODE);
447 rc =z_RecordComposition(odr, &pcomp, 0,
"recordComposition");
449 for (i=0;i<nrecs;i++)
450 recs[i].position=start+i;
453 pcomp, yaz_oid_recsyn_xml, nrecs,recs);
456 for (i=0;i<nrecs;i++)
458 printf(
"Err %d: %d\n",i,recs[i].errCode);
461 wrbuf_printf(outbuff,
"Record %d\n", recs[i].position);
462 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
463 wrbuf_puts(outbuff,
"\n");
465 wrbuf_printf(outbuff,
"NO Record %d\n", recs[i].position);
478 Z_SortKeySpecList *spec=0;
482 odr=odr_createmem(ODR_ENCODE);
483 spec=yaz_sort_spec (odr,
restargs(args,1));
493 wrbuf_printf(outbuff,
"sort returned status %d\n",sortstatus);
535 {
"",
"Starting and stopping:",
"", 0 },
538 "starts the zebra service. You need to call this first\n"
542 "stops the zebra service",
545 "starts a zebra session. Once you have called zebra_start\n"
546 "you can call zebra_open to start working",
549 "closes a zebra session",
551 {
"quickstart",
"[configfile]",
552 "Does a zebra_start, zebra_open, and sets up the log",
555 {
"",
"Log file:",
"", 0},
558 "Directs the log to filename (or stderr)",
562 "Sets the logging level (or returns to default)",
566 "Sets the log prefix",
570 "writes an entry in the log",
573 {
"",
"Error handling:",
"", 0},
575 "Displays zebra's error status (code, str, add)",
578 "Displays zebra's error code",
581 "Displays zebra's error string",
584 "Displays zebra's additional error message",
587 {
"",
"Admin:",
"", 0},
589 "Initializes the zebra database, destroying all data in it",
591 {
"select_database",
"basename",
592 "Selects a database",
594 {
"create_database",
"basename",
597 {
"drop_database",
"basename",
600 {
"begin_trans",
"[rw]",
601 "Begins a transaction. rw=1 means write, otherwise read-only",
604 "Ends a transaction",
607 {
"",
"Updating:",
"",0},
608 {
"record_insert",
"record",
609 "inserts an sgml record into Default",
611 {
"exchange_record",
"database record-id action record",
612 "inserts (1), updates (2), or deletes (3) a record \n"
613 "record-id must be a unique identifier for the record",
616 {
"",
"Searching and retrieving:",
"",0},
617 {
"search_pqf",
"setname query",
626 {
"show",
"[start] [numrecs] [resultset]",
629 {
"s",
"[start] [numrecs] [resultset]",
633 "sorts a result set. (example spec: 1=4 >)",
636 {
"",
"Misc:",
"", 0},
646 {
"help",
"[command]",
647 "Gives help on command, or lists them all",
649 {
"",
"help [command] gives more info on command",
"",0 },
670 for (i = 0; i <= n; i++)
672 const char *cp =
args[i];
673 printf (
"args %d :%s:\n", i, cp ? cp :
"<null>");
679 if (0==strcmp(
args[0],
"expect"))
683 rest= line + (
args[1]-argbuf);
686 if (0==strstr(prevout,rest))
688 printf(
"Failed expectation, '%s' not found\n", rest);
702 wrbuf_printf(outbuff,
"Unknown command '%s'. Try help\n",
args[0]);
716 wrbuf_printf(outbuff,
"%s %s\n%s\n",
721 wrbuf_printf(outbuff,
"Unknown command '%s'",
args[1]);
732 wrbuf_puts(outbuff,
"\n ");
735 linelen += strlen(
cmds[i].
cmd) + 2;
736 wrbuf_printf(outbuff,
"%s ",
cmds[i].
cmd);
739 wrbuf_printf(outbuff,
"\n%s\n ",
cmds[i].
args);
743 wrbuf_puts(outbuff,
"\n");
758 yaz_log(
log_level,
" Zebra error %d: %s, (%s)",
761 wrbuf_printf(outbuff,
" Zebra error %d: %s, (%s)\n",
774 WRBUF outbuff=wrbuf_alloc();
776 wrbuf_puts(outbuff,
"Zebrash at your service");
782 #if HAVE_READLINE_READLINE_H
787 #if HAVE_READLINE_HISTORY_H
789 add_history(line_in);
800 fprintf(stderr,
"Input line too long\n");
813 if ((nl_cp = strchr(buf,
'\n')))
816 wrbuf_rewind(outbuff);
820 wrbuf_puts(outbuff,
" OK\n");
825 wrbuf_printf(outbuff,
" command returned %d\n",rc);
828 printf(
"%s\n", wrbuf_cstr(outbuff));
830 wrbuf_destroy(outbuff);
837 printf (
"zebrash [-c config]\n");
844 int main (
int argc,
char ** argv)
848 while ((ret = options (
"c:h", argv, argc, &arg)) != -2)
859 fprintf(stderr,
"bad option %s\n", arg);
863 log_level=yaz_log_module_level(
"zebrash");
ZEBRA_RES zebra_init(ZebraHandle zh)
ZEBRA_RES zebra_end_trans(ZebraHandle zh) ZEBRA_GCC_ATTR((warn_unused_result))
ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query, const char *setname, zint *hits)
Search using PQF Query String.
ZEBRA_RES zebra_close(ZebraHandle zh)
Destroys Zebra session handle.
ZebraService zebra_start(const char *configName) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra Service.
ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
Deletes a database (drop)
ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream, const char *setname, Z_RecordComposition *comp, const Odr_oid *input_format, int num_recs, ZebraRetrievalRecord *recs)
Retrieve records from result set (after search)
ZEBRA_RES zebra_update_record(ZebraHandle zh, enum zebra_recctrl_action_t action, const char *recordType, zint *sysno, const char *match, const char *fname, const char *buf, int buf_size)
Updates record.
ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
Creates a database.
ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) ZEBRA_GCC_ATTR((warn_unused_result))
ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream, int num_input_setnames, const char **input_setnames, const char *output_setname, Z_SortKeySpecList *sort_sequence, int *sort_status) ZEBRA_GCC_ATTR((warn_unused_result))
int zebra_errCode(ZebraHandle zh)
Returns error code for last error.
ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename) ZEBRA_GCC_ATTR((warn_unused_result))
const char * zebra_errString(ZebraHandle zh)
Returns error string for last error.
ZEBRA_RES zebra_stop(ZebraService zs)
stops a Zebra service.
char * zebra_errAdd(ZebraHandle zh)
Returns additional info for last error.
ZebraHandle zebra_open(ZebraService zs, Res res) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra session handle within service.
int(* testfunc)(char *args[], WRBUF outbuff)
static int cmd_exchange_record(char *args[], WRBUF outbuff)
static int cmd_erradd(char *args[], WRBUF outbuff)
static int cmd_begin_trans(char *args[], WRBUF outbuff)
static int split_args(char *line, char **args)
static int cmd_logf(char *args[], WRBUF outbuff)
static int cmd_zebra_start(char *args[], WRBUF outbuff)
static char * defarg(char *arg, char *def)
int onecommand(char *line, WRBUF outbuff, const char *prevout)
static int cmd_record_insert(char *args[], WRBUF outbuff)
static int cmd_create_database(char *args[], WRBUF outbuff)
static int cmd_zebra_stop(char *args[], WRBUF outbuff)
static int cmd_yaz_log_level(char *args[], WRBUF outbuff)
int cmd_echo(char *args[], WRBUF outbuff)
int main(int argc, char **argv)
static int cmd_errstr(char *args[], WRBUF outbuff)
static int cmd_quickstart(char *args[], WRBUF outbuff)
static int cmd_sort(char *args[], WRBUF outbuff)
static char * restargs(char *args[], int n)
static char * default_config
static int cmd_init(char *args[], WRBUF outbuff)
static int cmd_zebra_open(char *args[], WRBUF outbuff)
static int cmd_find(char *args[], WRBUF outbuff)
static int cmd_errcode(char *args[], WRBUF outbuff)
static int cmd_yaz_log_file(char *args[], WRBUF outbuff)
static int cmd_show(char *args[], WRBUF outbuff)
int cmd_quit(char *args[], WRBUF outbuff)
static int cmd_end_trans(char *args[], WRBUF outbuff)
static int cmd_zebra_close(char *args[], WRBUF outbuff)
static int cmd_help(char *args[], WRBUF outbuff)
static int cmd_drop_database(char *args[], WRBUF outbuff)
static int cmd_yaz_log_prefix(char *args[], WRBUF outbuff)
static void Zerrors(WRBUF outbuff)
static int cmd_search_pqf(char *args[], WRBUF outbuff)
static int defargint(char *arg, int def)
static int cmd_select_database(char *args[], WRBUF outbuff)
static int cmd_err(char *args[], WRBUF outbuff)