Skip to content
Snippets Groups Projects
cli.c 32.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    				return help_workhorse(fd, argv + 1);
    			} else {
    				join(fullcmd, sizeof(fullcmd), argv+1);
    				ast_cli(fd, "No such command '%s'.\n", fullcmd);
    			}
    		}
    	} else {
    		return help_workhorse(fd, NULL);
    	}
    	return RESULT_SUCCESS;
    }
    
    static char *parse_args(char *s, int *max, char *argv[])
    {
    	char *dup, *cur;
    	int x=0;
    	int quoted=0;
    	int escaped=0;
    	int whitespace=1;
    
    	dup = strdup(s);
    	if (dup) {
    		cur = dup;
    		while(*s) {
    			switch(*s) {
    			case '"':
    				/* If it's escaped, put a literal quote */
    				if (escaped) 
    					goto normal;
    				else 
    					quoted = !quoted;
    
    Mark Spencer's avatar
    Mark Spencer committed
    				if (quoted && whitespace) {
    					/* If we're starting a quote, coming off white space start a new word, too */
    					argv[x++] = cur;
    					whitespace=0;
    				}
    
    Mark Spencer's avatar
    Mark Spencer committed
    				escaped = 0;
    				break;
    			case ' ':
    			case '\t':
    				if (!quoted && !escaped) {
    					/* If we're not quoted, mark this as whitespace, and
    					   end the previous argument */
    					whitespace = 1;
    					*(cur++) = '\0';
    				} else
    					/* Otherwise, just treat it as anything else */ 
    					goto normal;
    				break;
    			case '\\':
    				/* If we're escaped, print a literal, otherwise enable escaping */
    				if (escaped) {
    					goto normal;
    				} else {
    					escaped=1;
    				}
    				break;
    			default:
    normal:
    				if (whitespace) {
    					if (x >= AST_MAX_ARGS -1) {
    						ast_log(LOG_WARNING, "Too many arguments, truncating\n");
    						break;
    					}
    					/* Coming off of whitespace, start the next argument */
    					argv[x++] = cur;
    					whitespace=0;
    				}
    				*(cur++) = *s;
    				escaped=0;
    			}
    			s++;
    		}
    		/* Null terminate */
    		*(cur++) = '\0';
    		argv[x] = NULL;
    		*max = x;
    	}
    	return dup;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    /* This returns the number of unique matches for the generator */
    int ast_cli_generatornummatches(char *text, char *word)
    {
    	int matches = 0, i = 0;
    
    	char *buf = NULL, *oldbuf = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	while ( (buf = ast_cli_generator(text, word, i)) ) {
    
    		if (++i > 1 && strcmp(buf,oldbuf) == 0)  {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				continue;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		oldbuf = buf;
    		matches++;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return matches;
    }
    
    char **ast_cli_completion_matches(char *text, char *word)
    {
    	char **match_list = NULL, *retstr, *prevstr;
    	size_t match_list_len, max_equal, which, i;
    	int matches = 0;
    
    	match_list_len = 1;
    	while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
    		if (matches + 1 >= match_list_len) {
    			match_list_len <<= 1;
    			match_list = realloc(match_list, match_list_len * sizeof(char *));
    		}
    		match_list[++matches] = retstr;
    	}
    
    	if (!match_list)
    		return (char **) NULL;
    
    	which = 2;
    	prevstr = match_list[1];
    	max_equal = strlen(prevstr);
    	for (; which <= matches; which++) {
    
    		for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
    
    Mark Spencer's avatar
    Mark Spencer committed
    			continue;
    		max_equal = i;
    	}
    
    	retstr = malloc(max_equal + 1);
    	(void) strncpy(retstr, match_list[1], max_equal);
    	retstr[max_equal] = '\0';
    	match_list[0] = retstr;
    
    	if (matches + 1 >= match_list_len)
    		match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
    	match_list[matches + 1] = (char *) NULL;
    
    	return (match_list);
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    static char *__ast_cli_generator(char *text, char *word, int state, int lock)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	char *argv[AST_MAX_ARGS];
    	struct ast_cli_entry *e, *e1, *e2;
    	int x;
    	int matchnum=0;
    	char *dup, *res;
    
    	char fullcmd1[80] = "";
    	char fullcmd2[80] = "";
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char matchstr[80];
    
    	char *fullcmd = NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	if ((dup = parse_args(text, &x, argv))) {
    		join(matchstr, sizeof(matchstr), argv);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (lock)
    
    Mark Spencer's avatar
    Mark Spencer committed
    		e1 = builtins;
    		e2 = helpers;
    		while(e1->cmda[0] || e2) {
    			if (e2)
    				join(fullcmd2, sizeof(fullcmd2), e2->cmda);
    			if (e1->cmda[0])
    				join(fullcmd1, sizeof(fullcmd1), e1->cmda);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (!e1->cmda[0] || 
    
    Mark Spencer's avatar
    Mark Spencer committed
    					(e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
    				/* Use e2 */
    				e = e2;
    				fullcmd = fullcmd2;
    				/* Increment by going to next */
    				e2 = e2->next;
    			} else {
    				/* Use e1 */
    				e = e1;
    				fullcmd = fullcmd1;
    				e1++;
    			}
    
    			if ((fullcmd[0] != '_') && !strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				/* We contain the first part of one or more commands */
    
    				/* Now, what we're supposed to return is the next word... */
    				if (!ast_strlen_zero(word) && x>0) {
    					res = e->cmda[x-1];
    				} else {
    					res = e->cmda[x];
    				}
    				if (res) {
    					matchnum++;
    					if (matchnum > state) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    						if (lock)
    
    						return strdup(res);
    
    Mark Spencer's avatar
    Mark Spencer committed
    					}
    				}
    			}
    			if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
    				/* We have a command in its entirity within us -- theoretically only one
    				   command can have this occur */
    
    				fullcmd = e->generator(matchstr, word, (!ast_strlen_zero(word) ? (x - 1) : (x)), state);
    
    				if (fullcmd) {
    					if (lock)
    						ast_mutex_unlock(&clilock);
    					free(dup);
    					return fullcmd;
    				}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (lock)
    
    Mark Spencer's avatar
    Mark Spencer committed
    		free(dup);
    	}
    	return NULL;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    char *ast_cli_generator(char *text, char *word, int state)
    {
    	return __ast_cli_generator(text, word, state, 1);
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    int ast_cli_command(int fd, char *s)
    {
    	char *argv[AST_MAX_ARGS];
    	struct ast_cli_entry *e;
    	int x;
    	char *dup;
    	x = AST_MAX_ARGS;
    	if ((dup = parse_args(s, &x, argv))) {
    		/* We need at least one entry, or ignore */
    		if (x > 0) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			e = find_cli(argv, 0);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (e)
    				e->inuse++;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (e) {
    				switch(e->handler(fd, x, argv)) {
    				case RESULT_SHOWUSAGE:
    					ast_cli(fd, e->usage);
    					break;
    				}
    			} else 
    
    Mark Spencer's avatar
    Mark Spencer committed
    				ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (e) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    				e->inuse--;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		}
    		free(dup);
    	} else {
    		ast_log(LOG_WARNING, "Out of memory\n");	
    		return -1;
    	}
    	return 0;
    }