9 #define READLINE_LIBRARY
10 #include <readline/readline.h>
12 extern void using_history(void);
13 extern void stifle_history(int);
14 extern void add_history(char *);
17 #define CMD_COMPLETE 0
18 #define CMD_INCOMPLETE 1
22 static command_t * top_level; /* Top level of commands, initialized by
24 static command_t * match_tbl; /* Command completion against this table */
25 static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
26 static int done; /* Set to 1 if user types exit or quit */
29 /* static functions */
30 static char *skipwhitespace(char *s);
31 static char *skiptowhitespace(char *s);
32 static command_t *find_cmd(char *name, command_t cmds[], char **next);
33 static int process(char *s, char **next, command_t *lookup, command_t **result, char **prev);
34 static char *command_generator(const char *text, int state);
35 static char **command_completion(char *text, int start, int end);
36 static void print_commands(char *str, command_t *table);
38 static char * skipwhitespace(char * s)
44 for (t = s; t <= s + len && isspace(*t); t++);
49 static char * skiptowhitespace(char * s)
53 for (t = s; *t && !isspace(*t); t++);
57 static int line2args(char *line, char **argv, int maxargs)
62 arg = strtok(line, " \t");
69 while( (arg = strtok(NULL, " \t")) && (i <= maxargs)) {
76 /* find a command -- return it if unique otherwise print alternatives */
78 static command_t *Parser_findargcmd(char *name, command_t cmds[])
83 for (i = 0; cmds[i].pc_name; i++) {
86 if (strlen(name) != strlen(cmd->pc_name))
89 if (strlen(name) == strlen(cmd->pc_name)) {
90 if (strcmp(name, cmd->pc_name) == 0)
100 int Parser_execarg(int argc, char **argv, command_t cmds[])
105 cmd = Parser_findargcmd(argv[0], cmds);
107 return (cmd->pc_func)(argc, argv);
109 printf("Try interactive use without arguments or use one of: ");
110 for (i=0 ; cmds[i].pc_name ; i++) {
112 printf("\"%s\" ", cmd->pc_name);
114 printf("as argument.\n");
119 /* returns the command_t * (NULL if not found) corresponding to a
120 _partial_ match with the first token in name. It sets *next to
121 point to the following token. Does not modify *name. */
122 static command_t * find_cmd(char * name, command_t cmds[], char ** next)
129 /* This sets name to point to the first non-white space character,
130 and next to the first whitespace after name, len to the length: do
132 name = skipwhitespace(name);
133 *next = skiptowhitespace(name);
138 for (i = 0; cmds[i].pc_name; i++) {
139 if (strncasecmp(name, cmds[i].pc_name, len) == 0) {
140 *next = skipwhitespace(*next);
147 /* Recursively process a command line string s and find the command
148 corresponding to it. This can be ambiguous, full, incomplete,
150 static int process(char *s, char ** next, command_t *lookup,
151 command_t **result, char **prev)
153 *result = find_cmd(s, lookup, next);
160 /* found entry: is it ambigous, i.e. not exact command name and
161 more than one command in the list matches. Note that find_cmd
162 points to the first ambiguous entry */
163 if ( strncasecmp(s, (*result)->pc_name, strlen((*result)->pc_name)) &&
164 find_cmd(s, (*result) + 1, next))
167 /* found a unique command: component or full? */
168 if ( (*result)->pc_func ) {
171 if ( *next == '\0' ) {
172 return CMD_INCOMPLETE;
174 return process(*next, next, (*result)->pc_sub_cmd, result, prev);
179 static char * command_generator(const char * text, int state)
185 /* Do we have a match table? */
189 /* If this is the first time called on this word, state is 0 */
192 len = (int)strlen(text);
195 /* Return the next name in the command list that paritally matches test */
196 while ( (name = (match_tbl + index)->pc_name) ) {
199 if (strncasecmp(name, text, len) == 0) {
200 return(strdup(name));
204 /* No more matches */
208 /* probably called by readline */
209 static char **command_completion(char * text, int start, int end)
214 match_tbl = top_level;
215 for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
217 table = find_cmd(pos, match_tbl, &pos)) {
219 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
222 return(rl_completion_matches(text, command_generator));
225 /* take a string and execute the function or print help */
226 void execute_line(char * line)
228 command_t *cmd, *ambig;
234 switch( process(line, &next, top_level, &cmd, &prev) ) {
236 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
237 while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
238 fprintf(stderr, "%s ", ambig->pc_name);
241 fprintf(stderr, "\n");
244 fprintf(stderr, "No such command, type help\n");
248 "'%s' incomplete command. Use '%s x' where x is one of:\n",
250 fprintf(stderr, "\t");
251 for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
252 fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
254 fprintf(stderr, "\n");
257 i = line2args(line, argv, MAXARGS);
258 (cmd->pc_func)(i, argv);
265 /* this is the command execution machine */
266 void Parser_commands(void)
272 stifle_history(HISTORY);
274 rl_attempted_completion_function =
275 (rl_completion_func_t *)command_completion;
276 rl_completion_entry_function = (rl_compentry_func_t *)command_generator;
279 line = readline(parser_prompt);
283 s = skipwhitespace(line);
295 /* sets the parser prompt */
296 void Parser_init(char * prompt, command_t * cmds)
300 if (parser_prompt) free(parser_prompt);
301 parser_prompt = strdup(prompt);
304 /* frees the parser prompt */
305 void Parser_exit(int argc, char *argv[])
309 parser_prompt = NULL;
312 /* convert a string to an integer */
313 int Parser_int(char *s, int *val)
318 ret = sscanf(s, "%d", val);
319 else if (*(s+1) != 'x')
320 ret = sscanf(s, "%o", val);
323 ret = sscanf(++s, "%x", val);
331 void Parser_qhelp(int argc, char *argv[]) {
333 printf("Available commands are:\n");
335 print_commands(NULL, top_level);
336 printf("For more help type: help command-name\n");
339 int Parser_help(int argc, char **argv)
342 char *next, *prev, *tmp;
343 command_t *result, *ambig;
347 Parser_qhelp(argc, argv);
352 for ( i = 1 ; i < argc ; i++ ) {
353 strcat(line, argv[i]);
356 switch ( process(line, &next, top_level, &result, &prev) ) {
358 fprintf(stderr, "%s: %s\n",line, result->pc_help);
361 fprintf(stderr, "%s: Unknown command.\n", line);
365 "'%s' incomplete command. Use '%s x' where x is one of:\n",
367 fprintf(stderr, "\t");
368 for (i = 0; result->pc_sub_cmd[i].pc_name; i++) {
369 fprintf(stderr, "%s ", result->pc_sub_cmd[i].pc_name);
371 fprintf(stderr, "\n");
374 fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
375 while( (ambig = find_cmd(prev, result, &tmp)) ) {
376 fprintf(stderr, "%s ", ambig->pc_name);
379 fprintf(stderr, "\n");
385 /*************************************************************************
387 *************************************************************************/
390 static void print_commands(char * str, command_t * table) {
394 for (cmds = table; cmds->pc_name; cmds++) {
396 if (str) printf("\t%s %s\n", str, cmds->pc_name);
397 else printf("\t%s\n", cmds->pc_name);
399 if (cmds->pc_sub_cmd) {
401 sprintf(buf, "%s %s", str, cmds->pc_name);
402 print_commands(buf, cmds->pc_sub_cmd);
404 print_commands(cmds->pc_name, cmds->pc_sub_cmd);
410 char *Parser_getstr(const char *prompt, const char *deft, char *res,
414 int size = strlen(prompt) + strlen(deft) + 8;
416 theprompt = malloc(size);
419 sprintf(theprompt, "%s [%s]: ", prompt, deft);
421 line = readline(theprompt);
424 if ( line == NULL || *line == '\0' ) {
425 strncpy(res, deft, len);
427 strncpy(res, line, len);
438 /* get integer from prompt, loop forever to get it */
439 int Parser_getint(const char *prompt, long min, long max, long deft, int base)
444 int size = strlen(prompt) + 40;
445 char *theprompt = malloc(size);
447 sprintf(theprompt,"%s [%ld, (0x%lx)]: ", prompt, deft, deft);
453 line = readline(theprompt);
455 fprintf(stdout, "Please enter an integer.\n");
459 if ( *line == '\0' ) {
464 rc = Parser_arg2int(line, &result, base);
467 fprintf(stdout, "Invalid string.\n");
469 } else if ( result > max || result < min ) {
470 fprintf(stdout, "Error: response must lie between %ld and %ld.\n",
484 /* get boolean (starting with YyNn; loop forever */
485 int Parser_getbool(const char *prompt, int deft)
489 int size = strlen(prompt) + 8;
490 char *theprompt = malloc(size);
495 if ( deft != 0 && deft != 1 ) {
496 fprintf(stderr, "Error: Parser_getbool given bad default (%d).\n",
500 sprintf(theprompt, "%s [%s]: ", prompt, (deft==0)? "N" : "Y");
504 line = readline(theprompt);
505 if ( line == NULL ) {
509 if ( *line == '\0' ) {
513 if ( *line == 'y' || *line == 'Y' ) {
517 if ( *line == 'n' || *line == 'N' ) {
523 fprintf(stdout, "Invalid string. Must start with yY or nN\n");
534 /* parse int out of a string or prompt for it */
535 long Parser_intarg(const char *inp, const char *prompt, int deft,
536 int min, int max, int base)
541 rc = Parser_arg2int(inp, &result, base);
546 return Parser_getint(prompt, deft, min, max, base);
550 /* parse int out of a string or prompt for it */
551 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
552 char *answer, int len)
555 if ( inp == NULL || *inp == '\0' ) {
556 return Parser_getstr(prompt, deft, answer, len);
561 /* change a string into a number: return 0 on success. No invalid characters
562 allowed. The processing of base and validity follows strtol(3)*/
563 int Parser_arg2int(const char *inp, long *result, int base)
567 if ( (base !=0) && (base < 2 || base > 36) )
570 *result = strtol(inp, &endptr, base);
572 if ( *inp != '\0' && *endptr == '\0' )
578 int Parser_quit(int argc, char **argv)