1 /* zebrash.c - command-line interface to zebra API
4 * Copyrigth 2003 Index Data Aps
13 #if HAVE_READLINE_READLINE_H
14 #include <readline/readline.h>
16 #if HAVE_READLINE_HISTORY_H
17 #include <readline/history.h>
22 #define MAX_NO_ARGS 32
23 #define MAX_OUT_BUFF 4096
24 #define MAX_ARG_LEN 1024
25 #define PROMPT "ZebraSh>"
26 #define DEFAULTCONFIG "./zebra.cfg"
28 /**************************************
29 * Global variables (yuck!)
32 ZebraService zs=0; /* our global handle to zebra */
33 ZebraHandle zh=0; /* the current session */
34 /* time being, only one session works */
36 /**************************************
41 static int split_args( char *line, char** args )
42 { /* splits line into individual null-terminated strings,
43 * returns pointers to them in args */
46 args[0]=0; /* by default */
47 while (*p==' ' || *p=='\t' || *p=='\n')
51 while (*p==' ' || *p=='\t' || *p=='\n')
55 while (*p && *p!=' ' && *p!='\t' && *p!='\n')
63 /**************************************
67 int cmd_echo( char *args[], char *outbuff)
69 strcpy(outbuff, args[0]);
73 int cmd_quit( char *args[], char *outbuff)
75 strcpy(outbuff, "bye");
76 return -99; /* special stop signal */
79 int cmd_help( char *args[], char *outbuff);
81 int cmd_zebra_start( char *args[], char *outbuff)
84 if (!conf || !*conf) {
85 strcat(outbuff,"no config file specified, using "
91 strcpy(outbuff, "zebra_open failed" );
97 int cmd_zebra_stop( char *args[], char *outbuff)
100 strcat(outbuff,"zebra seems not to have been started, "
107 int cmd_zebra_open( char *args[], char *outbuff)
110 strcat(outbuff,"zebra seems not to have been started, "
116 int cmd_zebra_close( char *args[], char *outbuff)
119 strcat(outbuff,"Seems like you have not called zebra_open,"
126 /**************************************
127 * Command table, parser, and help
135 int (*testfunc)(char *args[], char *outbuff);
139 struct cmdstruct cmds[] = {
141 * if text is 0, does not list the command
142 * if cmd is "", adds the args (and newline) in command listing
144 { "", "Starting and stopping:", "", 0 },
147 "starts the zebra service. You need to call this first\n"
148 "if no configfile is given, assumes " DEFAULTCONFIG,
151 "stops the zebra service",
154 "starts a zebra session. Once you have called zebra_start\n"
155 "you can call zebra_open to start working",
158 "closes a zebra session",
160 { "", "Misc:","", 0},
167 { "help", "[command]",
168 "Gives help on command, or lists them all",
170 { "", "help [command] gives more info on command", "",0 },
171 {0,0,0,0} /* end marker */
174 int onecommand( char *line, char *outbuff)
177 char *args[MAX_NO_ARGS];
179 char argbuf[MAX_ARG_LEN];
180 strncpy(argbuf,line, MAX_ARG_LEN-1);
181 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
182 n=split_args(argbuf, args);
184 return 0; /* no command on line, too bad */
185 for (i=0;cmds[i].cmd;i++)
186 if (0==strcmp(cmds[i].cmd, args[0]))
189 args[0]= line + (args[1]-argbuf); /* rest of the line */
192 return ((cmds[i].testfunc)(args,outbuff));
194 sprintf (outbuff, "Unknown command '%s'. Try help",args[0] );
198 int cmd_help( char *args[], char *outbuff)
201 char tmp[MAX_ARG_LEN];
203 { /* help for a single command */
204 for (i=0;cmds[i].cmd;i++)
205 if (0==strcmp(cmds[i].cmd, args[0]))
207 strcat(outbuff,cmds[i].cmd);
209 strcat(outbuff,cmds[i].args);
210 strcat(outbuff,"\n");
211 strcat(outbuff,cmds[i].explanation);
212 strcat(outbuff,"\n");
216 { /* list all commands */
218 for (i=0;cmds[i].cmd;i++)
219 if (cmds[i].explanation)
221 /* sprintf(tmp, "%s %s %s\n",
222 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
224 strcat(tmp, cmds[i].cmd);
228 strcat(outbuff, tmp);
229 strcat(outbuff,"\n");
233 strcat(outbuff, cmds[i].args);
234 strcat(outbuff,"\n");
240 strcat(outbuff,"\n");
250 /**************************************
259 char buf[MAX_ARG_LEN];
260 char outbuff[MAX_OUT_BUFF];
261 #if HAVE_READLINE_READLINE_H
263 line_in=readline(PROMPT);
266 #if HAVE_READLINE_HISTORY_H
268 add_history(line_in);
270 if(strlen(line_in) > MAX_ARG_LEN-1) {
271 fprintf(stderr,"Input line too long\n");
279 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
283 rc=onecommand(buf, outbuff);
284 printf("%s\n", outbuff);
290 /**************************************
294 int main (int argc, char ** args)