9 #define READLINE_LIBRARY
10 #include <readline/readline.h>
12 extern char **completion_matches __P((char *, rl_compentry_func_t *));
13 extern void using_history(void);
14 extern void stifle_history(int);
15 extern void add_history(char *);
18 #define CMD_COMPLETE 0
19 #define CMD_INCOMPLETE 1
23 static command_t * top_level; /* Top level of commands, initialized by
25 static command_t * match_tbl; /* Command completion against this table */
26 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
27 static int done; /* Set to 1 if user types exit or quit */
30 /* static functions */
31 static char *skipwhitespace(char *s);
32 static char *skiptowhitespace(char *s);
33 static command_t *find_cmd(char *name, command_t cmds[], char **next);
34 static int process(char *s, char **next, command_t *lookup, command_t **result, char **prev);
35 static char *command_generator(const char *text, int state);
36 static char **command_completion(char *text, int start, int end);
37 static void print_commands(char *str, command_t *table);
39 static char * skipwhitespace(char * s)
45 for (t = s; t <= s + len && isspace(*t); t++);
50 static char * skiptowhitespace(char * s)
54 for (t = s; *t && !isspace(*t); t++);
58 static int line2args(char *line, char **argv, int maxargs)
63 arg = strtok(line, " \t");
70 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
77 /* find a command -- return it if unique otherwise print alternatives */
79 static command_t *Parser_findargcmd(char *name, command_t cmds[])
84 for (i = 0; cmds[i].pc_name; i++) {
87 if (strlen(name) != strlen(cmd->pc_name))
90 if (strlen(name) == strlen(cmd->pc_name)) {
91 if (strcmp(name, cmd->pc_name) == 0)
101 int Parser_execarg(int argc, char **argv, command_t cmds[])
106 cmd = Parser_findargcmd(argv[0], cmds);
108 return (cmd->pc_func)(argc, argv);
110 printf("Try interactive use without arguments or use one of: ");
111 for (i=0 ; cmds[i].pc_name ; i++) {
113 printf("\"%s\" ", cmd->pc_name);
115 printf("as argument.\n");
120 /* returns the command_t * (NULL if not found) corresponding to a
121 _partial_ match with the first token in name. It sets *next to
122 point to the following token. Does not modify *name. */
123 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
130 /* This sets name to point to the first non-white space character,
131 and next to the first whitespace after name, len to the length: do
133 name = skipwhitespace(name);
134 *next = skiptowhitespace(name);
139 for (i = 0; cmds[i].pc_name; i++) {
140 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
141 *next = skipwhitespace(*next);
148 /* Recursively process a command line string s and find the command
149 corresponding to it. This can be ambiguous, full, incomplete,
151 static int process(char *s, char ** next, command_t *lookup,
152 command_t **result, char **prev)
154 *result = find_cmd(s, lookup, next);
161 /* found entry: is it ambigous, i.e. not exact command name and
162 more than one command in the list matches. Note that find_cmd
163 points to the first ambiguous entry */
164 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
165 find_cmd(s, (*result) + 1, next))
168 /* found a unique command: component or full? */
169 if ( (*result)->pc_func ) {
172 if ( *next == '\0' ) {
173 return CMD_INCOMPLETE;
175 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
180 static char * command_generator(const char * text, int state)
186 /* Do we have a match table? */
190 /* If this is the first time called on this word, state is 0 */
193 len = (int)strlen(text);
196 /* Return the next name in the command list that paritally matches test */
197 while ( (name = (match_tbl + index)->pc_name) ) {
200 if (strncasecmp(name, text, len) == 0) {
201 return(strdup(name));
205 /* No more matches */
209 /* probably called by readline */
210 static char **command_completion(char * text, int start, int end)
215 match_tbl = top_level;
216 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
218 table = find_cmd(pos, match_tbl, &pos)) {
220 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
223 return(completion_matches(text, command_generator));
226 /* take a string and execute the function or print help */
227 void execute_line(char * line)
229 command_t *cmd, *ambig;
235 switch( process(line, &next, top_level, &cmd, &prev) ) {
237 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
238 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
239 fprintf(stderr, "%s ", ambig->pc_name);
242 fprintf(stderr, "\n");
245 fprintf(stderr, "No such command, type help\n");
249 "'%s' incomplete command. Use '%s x' where x is one of:\n",
251 fprintf(stderr, "\t");
252 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
253 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
255 fprintf(stderr, "\n");
258 i = line2args(line, argv, MAXARGS);
259 (cmd->pc_func)(i, argv);
266 /* this is the command execution machine */
267 void Parser_commands(void)
273 stifle_history(HISTORY);
275 rl_attempted_completion_function =
276 (CPPFunction *)command_completion;
277 rl_completion_entry_function = (void *)command_generator;
280 line = readline(parser_prompt);
284 s = skipwhitespace(line);
296 /* sets the parser prompt */
297 void Parser_init(char * prompt, command_t * cmds)
301 if (parser_prompt) free(parser_prompt);
302 parser_prompt = strdup(prompt);
305 /* frees the parser prompt */
306 void Parser_exit(int argc, char *argv[])
310 parser_prompt = NULL;
313 /* convert a string to an integer */
314 int Parser_int(char *s, int *val)
319 ret = sscanf(s, "%d", val);
320 else if (*(s+1) != 'x')
321 ret = sscanf(s, "%o", val);
324 ret = sscanf(++s, "%x", val);
332 void Parser_qhelp(int argc, char *argv[]) {
334 printf("Available commands are:\n");
336 print_commands(NULL, top_level);
337 printf("For more help type: help command-name\n");
340 int Parser_help(int argc, char **argv)
343 char *next, *prev, *tmp;
344 command_t *result, *ambig;
348 Parser_qhelp(argc, argv);
353 for ( i = 1 ; i < argc ; i++ ) {
354 strcat(line, argv[i]);
357 switch ( process(line, &next, top_level, &result, &prev) ) {
359 fprintf(stderr, "%s: %s\n",line, result->pc_help);
362 fprintf(stderr, "%s: Unknown command.\n", line);
366 "'%s' incomplete command. Use '%s x' where x is one of:\n",
368 fprintf(stderr, "\t");
369 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
370 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
372 fprintf(stderr, "\n");
375 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
376 while( (ambig = find_cmd(prev, result, &tmp)) ) {
377 fprintf(stderr, "%s ", ambig->pc_name);
380 fprintf(stderr, "\n");
386 /*************************************************************************
388 *************************************************************************/
391 static void print_commands(char * str, command_t * table) {
395 for (cmds = table; cmds->pc_name; cmds++) {
397 if (str) printf("\t%s %s\n", str, cmds->pc_name);
398 else printf("\t%s\n", cmds->pc_name);
400 if (cmds->pc_sub_cmd) {
402 sprintf(buf, "%s %s", str, cmds->pc_name);
403 print_commands(buf, cmds->pc_sub_cmd);
405 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
411 char *Parser_getstr(const char *prompt, const char *deft, char *res,
415 int size = strlen(prompt) + strlen(deft) + 8;
417 theprompt = malloc(size);
420 sprintf(theprompt, "%s [%s]: ", prompt, deft);
422 line = readline(theprompt);
425 if ( line == NULL || *line == '\0' ) {
426 strncpy(res, deft, len);
428 strncpy(res, line, len);
439 /* get integer from prompt, loop forever to get it */
440 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
445 int size = strlen(prompt) + 40;
446 char *theprompt = malloc(size);
448 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
454 line = readline(theprompt);
456 fprintf(stdout, "Please enter an integer.\n");
460 if ( *line == '\0' ) {
465 rc = Parser_arg2int(line, &result, base);
468 fprintf(stdout, "Invalid string.\n");
470 } else if ( result > max || result < min ) {
471 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
485 /* get boolean (starting with YyNn; loop forever */
486 int Parser_getbool(const char *prompt, int deft)
490 int size = strlen(prompt) + 8;
491 char *theprompt = malloc(size);
496 if ( deft != 0 && deft != 1 ) {
497 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
501 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
505 line = readline(theprompt);
506 if ( line == NULL ) {
510 if ( *line == '\0' ) {
514 if ( *line == 'y' || *line == 'Y' ) {
518 if ( *line == 'n' || *line == 'N' ) {
524 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
535 /* parse int out of a string or prompt for it */
536 long Parser_intarg(const char *inp, const char *prompt, int deft,
537 int min, int max, int base)
542 rc = Parser_arg2int(inp, &result, base);
547 return Parser_getint(prompt, deft, min, max, base);
551 /* parse int out of a string or prompt for it */
552 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
553 char *answer, int len)
556 if ( inp == NULL || *inp == '\0' ) {
557 return Parser_getstr(prompt, deft, answer, len);
562 /* change a string into a number: return 0 on success. No invalid characters
563 allowed. The processing of base and validity follows strtol(3)*/
564 int Parser_arg2int(const char *inp, long *result, int base)
568 if ( (base !=0) && (base < 2 || base > 36) )
571 *result = strtol(inp, &endptr, base);
573 if ( *inp != '\0' && *endptr == '\0' )
579 int Parser_quit(int argc, char **argv)