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