zebrash can now sort a result set
[idzebra-moved-to-github.git] / index / zebrash.c
1 /* $Id: zebrash.c,v 1.24 2003-12-04 11:20:39 heikki Exp $
2    Copyright (C) 2002,2003
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);
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_record_insert(zh,rec, strlen(rec), &sysno);
352     if (0==rc)
353     {
354         wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
355     }
356     return rc;
357 }
358
359
360 static int cmd_exchange_record( char *args[], WRBUF outbuff)
361 {
362     char *base=args[1];
363     char *id = args[2];
364     char *action = args[3];
365     int rc;
366     char *rec=restargs(args,4);
367     if (!(base && id && action && args[4] ))
368     {
369         wrbuf_puts(outbuff,"Missing arguments!\n");
370         onecommand("help exchange_record", outbuff, "");
371         return -90;
372     }
373     rc=zebra_admin_exchange_record(zh, base, rec, strlen(rec),
374         id, strlen(id), atoi(action));
375     return rc;
376 }
377
378 /**********************************
379  * Searching and retrieving
380  */
381
382 static int cmd_search_pqf( char *args[], WRBUF outbuff)
383 {
384     int hits=0;
385     char *set=args[1];
386     char *qry=restargs(args,2);
387     int rc;
388     rc=zebra_search_PQF(zh, qry, set, &hits);
389     if (0==rc)
390         wrbuf_printf(outbuff,"%d hits found\n",hits);
391     return rc;
392 }
393
394 static int cmd_find( char *args[], WRBUF outbuff)
395 {
396     char *setname=DEFAULTRESULTSET;
397     int rc;
398     int hits=0;
399     WRBUF qry=wrbuf_alloc();
400     if (0==strstr(args[0],"@attr"))
401         wrbuf_puts(qry, "@attr 1=/ ");
402     wrbuf_puts(qry,restargs(args,1));
403     if (!zh)
404         onecommand("quickstart", outbuff, "");
405     wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
406     rc=zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
407     if (0==rc)
408     {
409         wrbuf_printf(outbuff,"%d hits found\n",hits);
410         nextrecno=1;
411     }
412     wrbuf_free(qry,1);
413     return rc;
414 }
415
416 static int cmd_show( char *args[], WRBUF outbuff)
417 {
418     int start=defargint(args[1], nextrecno);
419     int nrecs=defargint(args[2],1);
420     char *setname=defarg(args[3],DEFAULTRESULTSET);
421     int rc=0;
422     ZebraRetrievalRecord *recs;
423     ODR odr;
424     Z_RecordComposition *pcomp=0;
425     int i;
426     oid_value format;
427
428     odr=odr_createmem(ODR_ENCODE);
429     recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
430     rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
431     format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
432     for (i=0;i<nrecs;i++)
433         recs[i].position=start+i;
434
435     rc = zebra_records_retrieve (zh, odr, setname,
436                                  pcomp, format, nrecs,recs);
437     if (0==rc)
438     {
439         for (i=0;i<nrecs;i++)
440         {
441             printf("Err %d: %d\n",i,recs[i].errCode);
442             if (recs[i].buf)
443             {
444                 wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
445                 wrbuf_write(outbuff, recs[i].buf, recs[i].len);
446                 wrbuf_puts(outbuff, "\n");
447             } else
448                 wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
449         }
450         nextrecno=start+nrecs;
451     }
452     odr_destroy(odr);
453     return rc;
454 } /* cmd_show */
455
456 static int cmd_sort( char *args[], WRBUF outbuff)
457 {
458     int rc=0;
459     ODR odr;
460     int sortstatus=0;
461     Z_SortKeySpecList *spec=0;
462     const char * inpsets[]={ DEFAULTRESULTSET, 0};
463     /* FIXME - allow the user to specify result sets in/out */
464
465     odr=odr_createmem(ODR_ENCODE);
466     spec=yaz_sort_spec (odr, restargs(args,1));
467     if (!spec)
468         rc=1;
469     if (!rc)
470         rc=zebra_sort(zh, odr,
471                         1, inpsets,
472                         DEFAULTRESULTSET,
473                         spec,
474                         &sortstatus);
475     if (!rc)
476         wrbuf_printf(outbuff, "sort returned status %d\n",sortstatus);
477
478     odr_destroy(odr);
479     return rc;
480 } /* cmd_sort */
481 /*
482  *
483  * int bend_sort (void *handle, bend_sort_rr *rr)
484  * {
485  *     ZebraHandle zh = (ZebraHandle) handle;
486  *
487  *     zebra_sort (zh, rr->stream,
488  *                     rr->num_input_setnames, (const char **)
489  *                     rr->input_setnames,
490  *                     rr->output_setname,
491  *                     rr->sort_sequence,
492  *                     &rr->sort_status);
493  *     zebra_result (zh, &rr->errcode,
494  *                  &rr->errstring);
495  *     return 0;
496  *  }
497  *
498  */
499
500 /**************************************)
501  * Command table, parser, and help 
502  */
503
504 struct cmdstruct
505 {
506     char *cmd;
507     char *args;
508     char *explanation;
509     int (*testfunc)(char *args[], WRBUF outbuff);
510 } ;
511
512  
513 struct cmdstruct cmds[] = {
514     /* special cases:
515      *   if text is 0, does not list the command
516      *   if cmd is "", adds the args (and newline) in command listing
517      */
518     { "", "Starting and stopping:", "", 0 },
519     { "zebra_start", 
520       "[configfile]", 
521       "starts the zebra service. You need to call this first\n"
522       "if no configfile is given, assumes " DEFAULTCONFIG, 
523       cmd_zebra_start },
524     { "zebra_stop",   "", 
525       "stops the zebra service", 
526       cmd_zebra_stop },
527     { "zebra_open", "",  
528       "starts a zebra session. Once you have called zebra_start\n"
529       "you can call zebra_open to start working", 
530       cmd_zebra_open },
531     { "zebra_close", "", 
532       "closes a zebra session", 
533       cmd_zebra_close }, 
534     { "quickstart", "[configfile]", 
535       "Does a zebra_start, zebra_open, and sets up the log", 
536       cmd_quickstart }, 
537   
538     { "", "Log file:","", 0},  
539     { "yaz_log_file", 
540       "[filename]",
541       "Directs the log to filename (or stderr)",
542       cmd_yaz_log_file },
543     { "yaz_log_level", 
544       "[level]",
545       "Sets the logging level (or returns to default)",
546       cmd_yaz_log_level },
547     { "yaz_log_prefix", 
548       "[prefix]",
549       "Sets the log prefix",
550       cmd_yaz_log_prefix},    
551     { "logf", 
552       "[level] text...",
553       "writes an entry in the log",
554       cmd_logf},    
555
556     { "", "Error handling:","", 0},
557     { "err",  "",
558       "Displays zebra's error status (code, str, add)",
559       cmd_err},    
560     { "errcode",  "",
561       "Displays zebra's error code",
562       cmd_errcode},    
563     { "errstr",  "",
564       "Displays zebra's error string",
565       cmd_errstr},    
566     { "erradd",  "",
567       "Displays zebra's additional error message",
568       cmd_erradd},    
569   
570     { "", "Admin:","", 0}, 
571     { "init",  "",
572       "Initializes the zebra database, destroying all data in it",
573       cmd_init},    
574     { "select_database",  "basename",
575       "Selects a database",
576       cmd_select_database},    
577     { "create_database", "basename",
578       "Create database",
579       cmd_create_database},
580     { "drop_database", "basename",
581       "Drop database",
582       cmd_drop_database},
583     { "begin_trans", "[rw]",
584       "Begins a transaction. rw=1 means write, otherwise read-only",
585       cmd_begin_trans},
586     { "end_trans","",
587       "Ends a transaction",
588       cmd_end_trans},
589
590     { "","Updating:","",0},
591     { "record_insert","record",
592       "inserts an sgml record into Default",
593       cmd_record_insert},
594     { "exchange_record","database record-id action record",
595       "inserts (1), updates (2), or deletes (3) a record \n"
596       "record-id must be a unique identifier for the record",
597       cmd_exchange_record},
598
599     { "","Searching and retrieving:","",0},
600     { "search_pqf","setname query",
601       "search ",
602       cmd_search_pqf},
603     { "find","query",
604       "simplified search",
605       cmd_find},
606     { "f","query",
607       "simplified search",
608       cmd_find},
609     { "show","[start] [numrecs] [resultset]",
610       "shows a result",
611       cmd_show},
612     { "s","[start] [numrecs] [resultset]",
613       "shows a result",
614       cmd_show},
615     { "sort","sortspec",
616       "sorts a result set. (example spec: 1=4 >)",
617       cmd_sort},
618       
619     { "", "Misc:","", 0}, 
620     { "echo", "string", 
621       "ouputs the string", 
622       cmd_echo },
623     { "q", "", 
624       "exits the program", 
625       cmd_quit },
626     { "quit", "", 
627       "exits the program", 
628       cmd_quit },
629     { "help", "[command]", 
630       "Gives help on command, or lists them all", 
631       cmd_help },
632     { "", "help [command] gives more info on command", "",0 },   
633   
634     {0,0,0,0} /* end marker */
635 };
636  
637 int onecommand( 
638                 char *line,     /* input line */
639                 WRBUF outbuff,  /* output goes here */
640                 const char *prevout) /* prev output, for 'expect' */
641 {
642     int i;
643     char *args[MAX_NO_ARGS];
644     int nargs;
645     char argbuf[MAX_ARG_LEN];
646     logf(LOG_APP,"%s",line);
647     strncpy(argbuf,line, MAX_ARG_LEN-1);
648     argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
649     /*memset(args,'\0',MAX_NO_ARGS*sizeof(char *));*/
650     nargs=split_args(argbuf, args);
651     
652 #if 0
653     for (i = 0; i <= n; i++)
654     {
655         const char *cp = args[i];
656         printf ("args %d :%s:\n", i, cp ? cp : "<null>");
657     }
658 #endif
659     if (0==nargs)
660             return -90; /* no command on line, too bad */
661
662     if (0==strcmp(args[0],"expect")) 
663     {
664         char *rest;
665         if (nargs>1) /* args[0] is not yet set, can't use restargs */
666             rest= line + (args[1]-argbuf); /* rest of the line */
667         else
668             return -1; /* need something to expect */
669         if (0==strstr(prevout,rest))
670         {
671             printf( "Failed expectation, '%s' not found\n", rest);
672             exit(9); 
673         }
674         return 0;
675     }
676     for (i=0;cmds[i].cmd;i++)
677         if (0==strcmp(cmds[i].cmd, args[0])) 
678         {
679             if (nargs>1)
680                 args[0]= line + (args[1]-argbuf); /* rest of the line */
681             else
682                 args[0]=""; 
683             return ((cmds[i].testfunc)(args,outbuff));
684         }
685     wrbuf_printf(outbuff, "Unknown command '%s'. Try help\n",args[0]);
686     logf(LOG_APP,"Unknown command");
687     return -90; 
688 }
689  
690 static int cmd_help( char *args[], WRBUF outbuff)
691
692     int i;
693     int linelen;
694     if (args[1]) 
695     { /* help for a single command */ 
696         for (i=0;cmds[i].cmd;i++)
697             if (0==strcmp(cmds[i].cmd, args[1])) 
698             {
699                 wrbuf_printf(outbuff,"%s  %s\n%s\n",
700                              cmds[i].cmd, cmds[i].args, 
701                              cmds[i].explanation);
702                 return 0;
703             }
704         wrbuf_printf(outbuff, "Unknown command '%s'", args[1]);
705     }
706     else 
707     { /* list all commands */
708         linelen=9999;
709         for (i=0;cmds[i].cmd;i++)
710         {
711             if (*cmds[i].cmd)
712             { /* ordinary command */
713                 if (linelen>50)
714                 {
715                     wrbuf_puts(outbuff,"\n   ");
716                     linelen=0;
717                 }
718                 linelen += strlen(cmds[i].cmd) + 2;
719                 wrbuf_printf(outbuff,"%s ", cmds[i].cmd);
720             } else
721             { /* section head */
722                 wrbuf_printf(outbuff,"\n%s\n   ",cmds[i].args);
723                 linelen=0;
724             }
725             } /* for */
726         wrbuf_puts(outbuff,"\n");
727     }
728     return 0;
729 }
730  
731 /* If Zebra reports an error after an operation,
732  * append it to the outbuff and log it */
733 static void Zerrors ( WRBUF outbuff)
734 {
735     int ec;
736     if (!zh)
737         return ;
738     ec=zebra_errCode (zh);
739     if (ec)
740     {
741         logf(LOG_APP, "   Zebra error %d: %s, (%s)",
742              ec, zebra_errString (zh),
743              zebra_errAdd (zh) );
744         wrbuf_printf(outbuff, "   Zebra error %d: %s, (%s)\n",
745                      ec, zebra_errString (zh),
746                      zebra_errAdd (zh) );
747         zebra_clearError(zh);
748     }
749 }
750
751 /************************************** 
752  * The shell
753  */
754  
755 void shell()
756 {
757     int rc=0;
758     WRBUF outbuff=wrbuf_alloc();
759     char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */
760     wrbuf_puts(outbuff,"Zebrash at your service");
761     while (rc!=-99)
762     {
763         char *nl_cp;
764         char buf[MAX_ARG_LEN];
765         char* line_in = 0;
766 #if HAVE_READLINE_READLINE_H
767         if (isatty(0)) {
768             line_in=readline(PROMPT);
769             if (!line_in)
770                 break;
771 #if HAVE_READLINE_HISTORY_H
772             if (*line_in)
773                 add_history(line_in);
774 #endif
775         }
776 #endif
777         /* line_in != NULL if readine is present and input is a tty */
778         
779         printf (PROMPT); 
780         fflush (stdout);
781         if (line_in)
782         {
783             if(strlen(line_in) > MAX_ARG_LEN-1) {
784                 fprintf(stderr,"Input line too long\n");
785                 break;
786             }
787             strcpy(buf,line_in);
788             free (line_in);
789         }
790         else 
791         {
792             if (!fgets (buf, MAX_ARG_LEN-1, stdin))
793                 break; 
794         }
795         
796         /* get rid of \n in line */
797         if ((nl_cp = strchr(buf, '\n')))
798             *nl_cp = '\0';
799         strncpy(prevout, wrbuf_buf(outbuff), MAX_OUT_BUFF);
800         wrbuf_rewind(outbuff);
801         rc=onecommand(buf, outbuff, prevout);
802         if (rc==0)
803         {
804             wrbuf_puts(outbuff, "   OK\n");
805             logf(LOG_APP, "OK");
806         }
807         else if (rc>-90)
808         {
809             wrbuf_printf(outbuff, "   command returned %d\n",rc);
810         } 
811         Zerrors(outbuff);
812         printf("%s\n", wrbuf_buf(outbuff));
813     } /* while */
814     wrbuf_free(outbuff,1);
815 } /* shell() */
816
817
818 /**************************************
819  * Main 
820  */
821  
822 int main (int argc, char ** args)
823 {
824     shell();
825     return 0;
826 } /* main */