IDZEBRA  2.1.2
zebrash.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 */
19 
20 /*
21  zebrash.c - command-line interface to zebra API
22 */
23 
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 
35 #if HAVE_READLINE_READLINE_H
36 #include <readline/readline.h>
37 #endif
38 #if HAVE_READLINE_HISTORY_H
39 #include <readline/history.h>
40 #endif
41 
42 #include <idzebra/api.h>
43 #include <yaz/log.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>
49 
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"
57 
58 
59 /**************************************
60  * Global variables (yuck!)
61  */
62 
63 ZebraService zs=0; /* our global handle to zebra */
64 ZebraHandle zh=0; /* the current session */
65 /* time being, only one session works */
66 int nextrecno=1; /* record number to show next */
68 static int log_level=0;
69 
70 
71 /**************************************
72  * Help functions
73  */
74 
75 
76 static int split_args( char *line, char** args )
77 { /* splits line into individual null-terminated strings,
78  * returns pointers to them in args */
79  /* FIXME - do we need to handle quoted args ?? */
80  char *p=line;
81  int i=0;
82  int n=0;
83  args[0]=0; /* by default */
84  while (*p==' ' || *p=='\t' || *p=='\n')
85  p++;
86  while (*p)
87  {
88  while (*p==' ' || *p=='\t' || *p=='\n')
89  p++;
90  if (*p=='#') /* skip comments */
91  break;
92  args[i++]=p;
93  args[i]=0;
94  while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='#')
95  p++;
96  *p++='\0';
97  }
98  n=i;
99  while (n<MAX_NO_ARGS)
100  args[n++]=0;
101  return i;
102 }
103 
104 static char *defarg( char *arg, char *def )
105 {
106  if (!arg)
107  return def;
108  if (!*arg)
109  return def;
110  return arg;
111 }
112 static int defargint( char *arg, int def )
113 {
114  int v=def;
115  char *a=defarg(arg,0);
116  if (a)
117  sscanf(a," %i", &v);
118  return v;
119 }
120 
121 static char *restargs( char *args[], int n)
122 { /* Returns the rest of the arguments, starting at the nth, */
123  /* to the end of the command line. Assumes args[0] contains */
124  /* the original line, minus the command itself */
125  int skiplen= args[n]-args[1];
126  if (skiplen > strlen(args[0]))
127  return "";
128  return args[0]+skiplen;
129 }
130 
131 int onecommand( char *line, WRBUF outbuff, const char *prevout);
132 
133 /**************************************
134  * Simple support commands
135  */
136 
137 int cmd_echo( char *args[], WRBUF outbuff)
138 {
139  wrbuf_printf(outbuff,"%s\n",restargs(args,1));
140  return 0;
141 }
142 
143 int cmd_quit( char *args[], WRBUF outbuff)
144 {
145  if (zs)
146  {
147  onecommand("zebra_close",outbuff,"");
148  zs=0;
149  }
150  if (zh)
151  {
152  onecommand("zebra_stop",outbuff,"");
153  zh=0;
154  }
155  wrbuf_puts(outbuff, "bye");
156  return -99; /* special stop signal */
157 }
158 
159 /**************************************
160  * Tests for starting and stopping zebra, etc
161  */
162 
163 static int cmd_help( char *args[], WRBUF outbuff);
164 
165 static int cmd_zebra_start( char *args[], WRBUF outbuff)
166 {
167  char *conf=args[1];
168  if (!conf || !*conf) {
169  wrbuf_puts(outbuff,"no config file specified, using ");
170  wrbuf_puts(outbuff, default_config);
171  wrbuf_puts(outbuff, "\n");
172  conf=default_config;
173  }
174  zs=zebra_start(conf);
175  if (!zs) {
176  wrbuf_puts(outbuff, "zebra_start failed" );
177  return 2;
178  }
179  return 0; /* ok */
180 }
181 
182 static int cmd_zebra_stop( char *args[], WRBUF outbuff)
183 {
184  if (!zs)
185  wrbuf_puts(outbuff,"zebra seems not to have been started, "
186  "stopping anyway\n");
187  zebra_stop(zs);
188  zs=0;
189  return 0; /* ok */
190 }
191 
192 static int cmd_zebra_open( char *args[], WRBUF outbuff)
193 {
194  if (!zs)
195  wrbuf_puts(outbuff,"zebra seems not to have been started, "
196  "trying anyway\n");
197  zh = zebra_open(zs, 0);
198  return 0; /* ok */
199 }
200 
201 static int cmd_zebra_close( char *args[], WRBUF outbuff)
202 {
203  if (!zh)
204  wrbuf_puts(outbuff,"Seems like you have not called zebra_open,"
205  "trying anyway\n");
206  zebra_close(zh);
207  return 0; /* ok */
208 }
209 
210 static int cmd_quickstart( char *args[], WRBUF outbuff)
211 {
212  char tmp[128];
213  int rc=0;
214  if (!rc)
215  rc=onecommand("yaz_log_file zebrash.log",outbuff,"");
216  if (!rc)
217  rc=onecommand("yaz_log_prefix ZebraSh", outbuff,"");
218  sprintf(tmp, "yaz_log_level 0x%x", YLOG_DEFAULT_LEVEL | log_level );
219  if (!rc)
220  rc=onecommand(tmp,outbuff,"");
221  yaz_log(log_level,"quickstart");
222  if (!zs)
223  if (!rc)
224  rc=onecommand("zebra_start",outbuff,"");
225  if (!zh)
226  if (!rc)
227  rc=onecommand("zebra_open",outbuff,"");
228  if (!rc)
229  rc=onecommand("select_database Default",outbuff,"");
230  return rc;
231 }
232 
233 /**************************************
234  * Log file handling
235  */
236 
237 static int cmd_yaz_log_file( char *args[], WRBUF outbuff)
238 {
239  char *fn = defarg(args[1],0);
240  wrbuf_printf(outbuff, "sending yaz-log to %s\n",fn);
241  yaz_log_init_file(fn);
242  return 0; /* ok */
243 }
244 
245 static int cmd_yaz_log_level( char *args[], WRBUF outbuff)
246 {
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);
250  return 0; /* ok */
251 }
252 
253 static int cmd_yaz_log_prefix( char *args[], WRBUF outbuff)
254 {
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);
258  return 0; /* ok */
259 }
260 
261 static int cmd_logf( char *args[], WRBUF outbuff)
262 {
263  int lev = defargint(args[1],0);
264  int i=1;
265  if (lev)
266  i=2;
267  else
268  lev=YLOG_LOG; /* this is in the default set!*/
269  yaz_log( lev, "%s", restargs(args,i));
270  return 0; /* ok */
271 }
272 
273 /****************
274  * Error handling
275  */
276 static int cmd_err ( char *args[], WRBUF outbuff)
277 {
278  wrbuf_printf(outbuff, "errCode: %d \nerrStr: %s\nerrAdd: %s \n",
279  zebra_errCode (zh),
280  zebra_errString (zh),
281  zebra_errAdd (zh) );
282  return 0; /* ok */
283 }
284 static int cmd_errcode ( char *args[], WRBUF outbuff)
285 {
286  wrbuf_printf(outbuff, "errCode: %d \n",
287  zebra_errCode (zh));
288  return 0; /* ok */
289 }
290 static int cmd_errstr ( char *args[], WRBUF outbuff)
291 {
292  wrbuf_printf(outbuff, "errStr: %s\n",
293  zebra_errString (zh));
294  return 0; /* ok */
295 }
296 static int cmd_erradd ( char *args[], WRBUF outbuff)
297 {
298  wrbuf_printf(outbuff, "errAdd: %s \n",
299  zebra_errAdd (zh) );
300  return 0; /* ok */
301 }
302 
303 /**************************************
304  * Admin commands
305  */
306 
307 static int cmd_init ( char *args[], WRBUF outbuff)
308 {
309  zebra_init(zh);
310  return 0; /* ok */
311 }
312 
313 static int cmd_select_database ( char *args[], WRBUF outbuff)
314 {
315  char *db=defarg(args[1],DEFAULTDATABASE);
316  wrbuf_printf(outbuff,"Selecting database '%s'\n",db);
317  return zebra_select_database(zh, db);
318 }
319 
320 static int cmd_create_database( char *args[], WRBUF outbuff)
321 {
322  char *db=defarg(args[1],DEFAULTDATABASE);
323  wrbuf_printf(outbuff,"Creating database '%s'\n",db);
324 
325  return zebra_create_database(zh, db);
326 }
327 
328 static int cmd_drop_database( char *args[], WRBUF outbuff)
329 {
330  char *db=args[1];
331  if (!db)
332  db="Default";
333  wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
334  return zebra_drop_database(zh, db);
335 }
336 
337 static int cmd_begin_trans( char *args[], WRBUF outbuff)
338 {
339  int rw=0;
340  if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
341  rw=1;
342  return zebra_begin_trans(zh,rw);
343 }
344 
345 static int cmd_end_trans( char *args[], WRBUF outbuff)
346 {
347  return zebra_end_trans(zh);
348 }
349 /*************************************
350  * Inserting and deleting
351  */
352 
353 static int cmd_record_insert( char *args[], WRBUF outbuff)
354 {
355  zint sysno = 0;
356  int rc;
357  char *rec=restargs(args,1);
358 
359  rc = zebra_update_record(zh,
361  0, /* record type */
362  &sysno,
363  0, /* match */
364  0, /* fname */
365  rec,
366  strlen(rec));
367  if (0==rc)
368  {
369  wrbuf_printf(outbuff,"ok sysno=" ZINT_FORMAT "\n",sysno);
370  }
371  return rc;
372 }
373 
374 
375 static int cmd_exchange_record( char *args[], WRBUF outbuff)
376 {
377  char *id = args[1];
378  char *action = args[2];
379  int rc;
380  char *rec=restargs(args,3);
381  if (!(id && action && args[4] ))
382  {
383  wrbuf_puts(outbuff,"Missing arguments!\n");
384  onecommand("help exchange_record", outbuff, "");
385  return -90;
386  }
387 
388  rc = zebra_update_record(zh, action_update, 0 /* record_type */,
389  0 /* sysno */,
390  id /* match */,
391  0 /* fname */,
392  rec, strlen(rec));
393  return rc;
394 }
395 
396 /**********************************
397  * Searching and retrieving
398  */
399 
400 static int cmd_search_pqf(char *args[], WRBUF outbuff)
401 {
402  zint hits = 0;
403  char *set = args[1];
404  char *qry = restargs(args,2);
405  int rc;
406  rc = zebra_search_PQF(zh, qry, set, &hits);
407  if (0==rc)
408  wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
409  return rc;
410 }
411 
412 static int cmd_find( char *args[], WRBUF outbuff)
413 {
414  char *setname=DEFAULTRESULTSET;
415  int rc;
416  zint hits = 0;
417  WRBUF qry = wrbuf_alloc();
418  if (0==strstr(args[0],"@attr"))
419  wrbuf_puts(qry, "@attr 1=/ ");
420  wrbuf_puts(qry,restargs(args,1));
421  if (!zh)
422  onecommand("quickstart", outbuff, "");
423  wrbuf_printf(outbuff, "find %s\n",wrbuf_cstr(qry));
424  rc = zebra_search_PQF(zh, wrbuf_cstr(qry), setname, &hits);
425  if (0==rc)
426  {
427  wrbuf_printf(outbuff, ZINT_FORMAT " hits found\n", hits);
428  nextrecno = 1;
429  }
430  wrbuf_destroy(qry);
431  return rc;
432 }
433 
434 static int cmd_show( char *args[], WRBUF outbuff)
435 {
436  int start=defargint(args[1], nextrecno);
437  int nrecs=defargint(args[2],1);
438  char *setname=defarg(args[3],DEFAULTRESULTSET);
439  int rc=0;
440  ZebraRetrievalRecord *recs;
441  ODR odr;
442  Z_RecordComposition *pcomp=0;
443  int i;
444 
445  odr = odr_createmem(ODR_ENCODE);
446  recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
447  rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
448 
449  for (i=0;i<nrecs;i++)
450  recs[i].position=start+i;
451 
452  rc = zebra_records_retrieve(zh, odr, setname,
453  pcomp, yaz_oid_recsyn_xml, nrecs,recs);
454  if (0==rc)
455  {
456  for (i=0;i<nrecs;i++)
457  {
458  printf("Err %d: %d\n",i,recs[i].errCode);
459  if (recs[i].buf)
460  {
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");
464  } else
465  wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
466  }
467  nextrecno=start+nrecs;
468  }
469  odr_destroy(odr);
470  return rc;
471 } /* cmd_show */
472 
473 static int cmd_sort( char *args[], WRBUF outbuff)
474 {
475  int rc=0;
476  ODR odr;
477  int sortstatus=0;
478  Z_SortKeySpecList *spec=0;
479  const char * inpsets[]={ DEFAULTRESULTSET, 0};
480  /* FIXME - allow the user to specify result sets in/out */
481 
482  odr=odr_createmem(ODR_ENCODE);
483  spec=yaz_sort_spec (odr, restargs(args,1));
484  if (!spec)
485  rc=1;
486  if (!rc)
487  rc=zebra_sort(zh, odr,
488  1, inpsets,
490  spec,
491  &sortstatus);
492  if (!rc)
493  wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
494 
495  odr_destroy(odr);
496  return rc;
497 } /* cmd_sort */
498 /*
499  *
500  * int bend_sort (void *handle, bend_sort_rr *rr)
501  * {
502  * ZebraHandle zh = (ZebraHandle) handle;
503  *
504  * zebra_sort (zh, rr->stream,
505  * rr->num_input_setnames, (const char **)
506  * rr->input_setnames,
507  * rr->output_setname,
508  * rr->sort_sequence,
509  * &rr->sort_status);
510  * zebra_result (zh, &rr->errcode,
511  * &rr->errstring);
512  * return 0;
513  * }
514  *
515  */
516 
517 /**************************************)
518  * Command table, parser, and help
519  */
520 
521 struct cmdstruct
522 {
523  char *cmd;
524  char *args;
525  char *explanation;
526  int (*testfunc)(char *args[], WRBUF outbuff);
527 } ;
528 
529 
530 struct cmdstruct cmds[] = {
531  /* special cases:
532  * if text is 0, does not list the command
533  * if cmd is "", adds the args (and newline) in command listing
534  */
535  { "", "Starting and stopping:", "", 0 },
536  { "zebra_start",
537  "[configfile]",
538  "starts the zebra service. You need to call this first\n"
539  "if no configfile is given, assumes " DEFAULTCONFIG,
540  cmd_zebra_start },
541  { "zebra_stop", "",
542  "stops the zebra service",
543  cmd_zebra_stop },
544  { "zebra_open", "",
545  "starts a zebra session. Once you have called zebra_start\n"
546  "you can call zebra_open to start working",
547  cmd_zebra_open },
548  { "zebra_close", "",
549  "closes a zebra session",
550  cmd_zebra_close },
551  { "quickstart", "[configfile]",
552  "Does a zebra_start, zebra_open, and sets up the log",
553  cmd_quickstart },
554 
555  { "", "Log file:","", 0},
556  { "yaz_log_file",
557  "[filename]",
558  "Directs the log to filename (or stderr)",
560  { "yaz_log_level",
561  "[level]",
562  "Sets the logging level (or returns to default)",
564  { "yaz_log_prefix",
565  "[prefix]",
566  "Sets the log prefix",
568  { "yaz_log",
569  "[level] text...",
570  "writes an entry in the log",
571  cmd_logf},
572 
573  { "", "Error handling:","", 0},
574  { "err", "",
575  "Displays zebra's error status (code, str, add)",
576  cmd_err},
577  { "errcode", "",
578  "Displays zebra's error code",
579  cmd_errcode},
580  { "errstr", "",
581  "Displays zebra's error string",
582  cmd_errstr},
583  { "erradd", "",
584  "Displays zebra's additional error message",
585  cmd_erradd},
586 
587  { "", "Admin:","", 0},
588  { "init", "",
589  "Initializes the zebra database, destroying all data in it",
590  cmd_init},
591  { "select_database", "basename",
592  "Selects a database",
594  { "create_database", "basename",
595  "Create database",
597  { "drop_database", "basename",
598  "Drop database",
600  { "begin_trans", "[rw]",
601  "Begins a transaction. rw=1 means write, otherwise read-only",
603  { "end_trans","",
604  "Ends a transaction",
605  cmd_end_trans},
606 
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",
615 
616  { "","Searching and retrieving:","",0},
617  { "search_pqf","setname query",
618  "search ",
620  { "find","query",
621  "simplified search",
622  cmd_find},
623  { "f","query",
624  "simplified search",
625  cmd_find},
626  { "show","[start] [numrecs] [resultset]",
627  "shows a result",
628  cmd_show},
629  { "s","[start] [numrecs] [resultset]",
630  "shows a result",
631  cmd_show},
632  { "sort","sortspec",
633  "sorts a result set. (example spec: 1=4 >)",
634  cmd_sort},
635 
636  { "", "Misc:","", 0},
637  { "echo", "string",
638  "ouputs the string",
639  cmd_echo },
640  { "q", "",
641  "exits the program",
642  cmd_quit },
643  { "quit", "",
644  "exits the program",
645  cmd_quit },
646  { "help", "[command]",
647  "Gives help on command, or lists them all",
648  cmd_help },
649  { "", "help [command] gives more info on command", "",0 },
650 
651  {0,0,0,0} /* end marker */
652 };
653 
655  char *line, /* input line */
656  WRBUF outbuff, /* output goes here */
657  const char *prevout) /* prev output, for 'expect' */
658 {
659  int i;
660  char *args[MAX_NO_ARGS];
661  int nargs;
662  char argbuf[MAX_ARG_LEN];
663  yaz_log(log_level,"%s",line);
664  strncpy(argbuf,line, MAX_ARG_LEN-1);
665  argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
666  /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
667  nargs=split_args(argbuf, args);
668 
669 #if 0
670  for (i = 0; i <= n; i++)
671  {
672  const char *cp = args[i];
673  printf ("args %d :%s:\n", i, cp ? cp : "<null>");
674  }
675 #endif
676  if (0==nargs)
677  return -90; /* no command on line, too bad */
678 
679  if (0==strcmp(args[0],"expect"))
680  {
681  char *rest;
682  if (nargs>1) /* args[0] is not yet set, can't use restargs */
683  rest= line + (args[1]-argbuf); /* rest of the line */
684  else
685  return -1; /* need something to expect */
686  if (0==strstr(prevout,rest))
687  {
688  printf( "Failed expectation, '%s' not found\n", rest);
689  exit(9);
690  }
691  return 0;
692  }
693  for (i=0;cmds[i].cmd;i++)
694  if (0==strcmp(cmds[i].cmd, args[0]))
695  {
696  if (nargs>1)
697  args[0]= line + (args[1]-argbuf); /* rest of the line */
698  else
699  args[0]="";
700  return ((cmds[i].testfunc)(args,outbuff));
701  }
702  wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
703  yaz_log(log_level,"Unknown command");
704  return -90;
705 }
706 
707 static int cmd_help( char *args[], WRBUF outbuff)
708 {
709  int i;
710  int linelen;
711  if (args[1])
712  { /* help for a single command */
713  for (i=0;cmds[i].cmd;i++)
714  if (0==strcmp(cmds[i].cmd, args[1]))
715  {
716  wrbuf_printf(outbuff,"%s %s\n%s\n",
717  cmds[i].cmd, cmds[i].args,
718  cmds[i].explanation);
719  return 0;
720  }
721  wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
722  }
723  else
724  { /* list all commands */
725  linelen=9999;
726  for (i=0;cmds[i].cmd;i++)
727  {
728  if (*cmds[i].cmd)
729  { /* ordinary command */
730  if (linelen>50)
731  {
732  wrbuf_puts(outbuff,"\n ");
733  linelen=0;
734  }
735  linelen += strlen(cmds[i].cmd) + 2;
736  wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
737  } else
738  { /* section head */
739  wrbuf_printf(outbuff,"\n%s\n ",cmds[i].args);
740  linelen=0;
741  }
742  } /* for */
743  wrbuf_puts(outbuff,"\n");
744  }
745  return 0;
746 }
747 
748 /* If Zebra reports an error after an operation,
749  * append it to the outbuff and log it */
750 static void Zerrors (WRBUF outbuff)
751 {
752  int ec;
753  if (!zh)
754  return ;
755  ec=zebra_errCode (zh);
756  if (ec)
757  {
758  yaz_log(log_level, " Zebra error %d: %s, (%s)",
759  ec, zebra_errString (zh),
760  zebra_errAdd (zh) );
761  wrbuf_printf(outbuff, " Zebra error %d: %s, (%s)\n",
762  ec, zebra_errString (zh),
763  zebra_errAdd (zh) );
764  }
765 }
766 
767 /**************************************
768  * The shell
769  */
770 
771 void shell(void)
772 {
773  int rc=0;
774  WRBUF outbuff=wrbuf_alloc();
775  char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
776  wrbuf_puts(outbuff,"Zebrash at your service");
777  while (rc!=-99)
778  {
779  char *nl_cp;
780  char buf[MAX_ARG_LEN];
781  char* line_in = 0;
782 #if HAVE_READLINE_READLINE_H
783  if (isatty(0)) {
784  line_in=readline(PROMPT);
785  if (!line_in)
786  break;
787 #if HAVE_READLINE_HISTORY_H
788  if (*line_in)
789  add_history(line_in);
790 #endif
791  }
792 #endif
793  /* line_in != NULL if readine is present and input is a tty */
794 
795  printf (PROMPT);
796  fflush (stdout);
797  if (line_in)
798  {
799  if(strlen(line_in) > MAX_ARG_LEN-1) {
800  fprintf(stderr,"Input line too long\n");
801  break;
802  }
803  strcpy(buf,line_in);
804  free (line_in);
805  }
806  else
807  {
808  if (!fgets (buf, MAX_ARG_LEN-1, stdin))
809  break;
810  }
811 
812  /* get rid of \n in line */
813  if ((nl_cp = strchr(buf, '\n')))
814  *nl_cp = '\0';
815  strncpy(prevout, wrbuf_cstr(outbuff), MAX_OUT_BUFF);
816  wrbuf_rewind(outbuff);
817  rc=onecommand(buf, outbuff, prevout);
818  if (rc==0)
819  {
820  wrbuf_puts(outbuff, " OK\n");
821  yaz_log(log_level, "OK");
822  }
823  else if (rc>-90)
824  {
825  wrbuf_printf(outbuff, " command returned %d\n",rc);
826  }
827  Zerrors(outbuff);
828  printf("%s\n", wrbuf_cstr(outbuff));
829  } /* while */
830  wrbuf_destroy(outbuff);
831 } /* shell() */
832 
833 
834 static void usage(void)
835 {
836  printf ("usage:\n");
837  printf ("zebrash [-c config]\n");
838  exit(1);
839 }
840 /**************************************
841  * Main
842  */
843 
844 int main (int argc, char ** argv)
845 {
846  int ret;
847  char *arg = 0;
848  while ((ret = options ("c:h", argv, argc, &arg)) != -2)
849  {
850  switch(ret)
851  {
852  case 'c':
853  default_config = arg;
854  break;
855  case 'h':
856  usage();
857  /* FIXME - handle -v */
858  default:
859  fprintf(stderr, "bad option %s\n", arg);
860  usage();
861  }
862  }
863  log_level=yaz_log_module_level("zebrash");
864 
865  shell();
866  return 0;
867 } /* main */
868 /*
869  * Local variables:
870  * c-basic-offset: 4
871  * c-file-style: "Stroustrup"
872  * indent-tabs-mode: nil
873  * End:
874  * vim: shiftwidth=4 tabstop=8 expandtab
875  */
876 
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.
Definition: zebraapi.c:2610
ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:1709
ZEBRA_RES zebra_close(ZebraHandle zh)
Destroys Zebra session handle.
Definition: zebraapi.c:656
ZEBRA_RES zebra_init(ZebraHandle zh)
Definition: zebraapi.c:2153
#define MAX_OUT_BUFF
Definition: zebrash.c:51
#define PROMPT
Definition: zebrash.c:53
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))
Definition: zebraapi.c:1290
Zebra API.
static char * defarg(char *arg, char *def)
Definition: zebrash.c:104
static int cmd_zebra_open(char *args[], WRBUF outbuff)
Definition: zebrash.c:192
char * explanation
Definition: zebrash.c:525
int cmd_quit(char *args[], WRBUF outbuff)
Definition: zebrash.c:143
static int cmd_err(char *args[], WRBUF outbuff)
Definition: zebrash.c:276
int zebra_errCode(ZebraHandle zh)
Returns error code for last error.
Definition: zebraapi.c:1342
static int cmd_find(char *args[], WRBUF outbuff)
Definition: zebrash.c:412
static int cmd_select_database(char *args[], WRBUF outbuff)
Definition: zebrash.c:313
#define DEFAULTCONFIG
Definition: zebrash.c:54
static int cmd_zebra_stop(char *args[], WRBUF outbuff)
Definition: zebrash.c:182
char * cmd
Definition: zebrash.c:523
int(* testfunc)(char *args[], WRBUF outbuff)
Definition: zebrash.c:526
ZebraHandle zh
Definition: zebrash.c:64
ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
Creates a database.
Definition: zebraapi.c:1561
static int cmd_init(char *args[], WRBUF outbuff)
Definition: zebrash.c:307
static int cmd_begin_trans(char *args[], WRBUF outbuff)
Definition: zebrash.c:337
static int cmd_record_insert(char *args[], WRBUF outbuff)
Definition: zebrash.c:353
static void usage(void)
Definition: zebrash.c:834
static int cmd_show(char *args[], WRBUF outbuff)
Definition: zebrash.c:434
static int cmd_exchange_record(char *args[], WRBUF outbuff)
Definition: zebrash.c:375
void shell(void)
Definition: zebrash.c:771
int main(int argc, char **argv)
Definition: zebrash.c:844
int cmd_echo(char *args[], WRBUF outbuff)
Definition: zebrash.c:137
static int cmd_yaz_log_level(char *args[], WRBUF outbuff)
Definition: zebrash.c:245
ZebraService zebra_start(const char *configName) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra Service.
Definition: zebraapi.c:191
char * zebra_errAdd(ZebraHandle zh)
Returns additional info for last error.
Definition: zebraapi.c:1362
static int cmd_quickstart(char *args[], WRBUF outbuff)
Definition: zebrash.c:210
static int cmd_errstr(char *args[], WRBUF outbuff)
Definition: zebrash.c:290
static char * restargs(char *args[], int n)
Definition: zebrash.c:121
struct cmdstruct cmds[]
Definition: zebrash.c:530
#define DEFAULTRESULTSET
Definition: zebrash.c:56
static int split_args(char *line, char **args)
Definition: zebrash.c:76
#define MAX_ARG_LEN
Definition: zebrash.c:52
char * args
Definition: zebrash.c:524
ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
Deletes a database (drop)
Definition: zebraapi.c:1517
static int log_level
Definition: zebrash.c:68
#define DEFAULTDATABASE
Definition: zebrash.c:55
ZEBRA_RES zebra_stop(ZebraService zs)
stops a Zebra service.
Definition: zebraapi.c:626
static int cmd_yaz_log_file(char *args[], WRBUF outbuff)
Definition: zebrash.c:237
static int cmd_end_trans(char *args[], WRBUF outbuff)
Definition: zebrash.c:345
static int cmd_create_database(char *args[], WRBUF outbuff)
Definition: zebrash.c:320
int nextrecno
Definition: zebrash.c:66
ZebraService zs
Definition: zebrash.c:63
ZEBRA_RES zebra_end_trans(ZebraHandle zh) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:1909
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)
Definition: zebraapi.c:1117
static int cmd_erradd(char *args[], WRBUF outbuff)
Definition: zebrash.c:296
int onecommand(char *line, WRBUF outbuff, const char *prevout)
Definition: zebrash.c:654
static int cmd_search_pqf(char *args[], WRBUF outbuff)
Definition: zebrash.c:400
static void Zerrors(WRBUF outbuff)
Definition: zebrash.c:750
ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query, const char *setname, zint *hits)
Search using PQF Query String.
Definition: zebraapi.c:2650
ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename) ZEBRA_GCC_ATTR((warn_unused_result))
Definition: zebraapi.c:938
long zint
Zebra integer.
Definition: util.h:66
static int cmd_errcode(char *args[], WRBUF outbuff)
Definition: zebrash.c:284
static int cmd_zebra_close(char *args[], WRBUF outbuff)
Definition: zebrash.c:201
#define MAX_NO_ARGS
Definition: zebrash.c:50
static int cmd_logf(char *args[], WRBUF outbuff)
Definition: zebrash.c:261
const char * zebra_errString(ZebraHandle zh)
Returns error string for last error.
Definition: zebraapi.c:1353
static int cmd_help(char *args[], WRBUF outbuff)
Definition: zebrash.c:707
static int cmd_yaz_log_prefix(char *args[], WRBUF outbuff)
Definition: zebrash.c:253
ZebraHandle zebra_open(ZebraService zs, Res res) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra session handle within service.
Definition: zebraapi.c:112
static char * default_config
Definition: zebrash.c:67
static int cmd_drop_database(char *args[], WRBUF outbuff)
Definition: zebrash.c:328
static int cmd_sort(char *args[], WRBUF outbuff)
Definition: zebrash.c:473
static int cmd_zebra_start(char *args[], WRBUF outbuff)
Definition: zebrash.c:165
static int defargint(char *arg, int def)
Definition: zebrash.c:112
#define ZINT_FORMAT
Definition: util.h:72