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** argv )
42 { /* splits line into individual null-terminated strings,
43 * returns pointers to them in argv */
46 argv[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);
138 struct cmdstruct cmds[] = {
139 { "zebra_start", "[configfile]",
140 "starts the zebra service", cmd_zebra_start },
142 "stops the zebra service", cmd_zebra_stop },
144 "starts a zebra session", cmd_zebra_open },
146 "closes a zebra session", cmd_zebra_close },
147 { "echo", "string", "ouputs the string", cmd_echo },
148 { "quit", "", "exits the program", cmd_quit },
149 { "help", "", 0, cmd_help },
151 {0,0,0,0} /* end marker */
154 int onecommand( char *line, char *outbuff)
157 char *argv[MAX_NO_ARGS];
159 char argbuf[MAX_ARG_LEN];
160 strncpy(argbuf,line, MAX_ARG_LEN-1);
161 argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */
162 n=split_args(argbuf, argv);
164 return 0; /* no command on line, too bad */
165 for (i=0;cmds[i].cmd;i++)
166 if (0==strcmp(cmds[i].cmd, argv[0]))
169 argv[0]= line + (argv[1]-argbuf); /* rest of the line */
172 return ((cmds[i].testfunc)(argv,outbuff));
174 sprintf (outbuff, "Unknown command '%s'. Try help",argv[0] );
178 int cmd_help( char *args[], char *outbuff)
181 char tmp[MAX_ARG_LEN];
182 for (i=0;cmds[i].cmd;i++)
183 if (cmds[i].explanation)
185 sprintf(tmp, "%s %s %s\n",
186 cmds[i].cmd, cmds[i].args, cmds[i].explanation);
193 /**************************************
202 char buf[MAX_ARG_LEN];
203 char outbuff[MAX_OUT_BUFF];
204 #if HAVE_READLINE_READLINE_H
206 line_in=readline(PROMPT);
209 #if HAVE_READLINE_HISTORY_H
211 add_history(line_in);
213 if(strlen(line_in) > MAX_ARG_LEN-1) {
214 fprintf(stderr,"Input line too long\n");
222 if (!fgets (buf, MAX_ARG_LEN-1, stdin))
226 rc=onecommand(buf, outbuff);
227 printf("%s\n", outbuff);
233 /**************************************
237 int main (int argc, char ** argv)