Exemplo n.º 1
0
void 
completion(const char *buf, linenoiseCompletions *lc)
{
    if (buf[0] == 'h') {
        linenoiseAddCompletion(lc,"hello");
        linenoiseAddCompletion(lc,"hello there");
    }
}
Exemplo n.º 2
0
Arquivo: websh.c Projeto: zhemao/websh
void completionCallback(const char * input, linenoiseCompletions * lc){
	int len = strlen(input);
	char temp[len+1];
	strcpy(temp, input);
	str_lower(temp);
	if(len < 3){
		if(temp[0]=='g')
			linenoiseAddCompletion(lc, "get ");
		else if(temp[0]=='p' && len>1){
			if(temp[1]=='u')
				linenoiseAddCompletion(lc, "put ");
			else if(temp[1]=='o')
				linenoiseAddCompletion(lc, "post ");
		}
		else if(temp[0]=='d')
			linenoiseAddCompletion(lc, "delete ");
		else if(temp[0]=='h')
			linenoiseAddCompletion(lc, "head ");
		else if(temp[0]=='e')
			linenoiseAddCompletion(lc, "exit");
	}
	else if(len < 10){
		if(strncmp(temp, "get", 3)==0)
			linenoiseAddCompletion(lc, "getheader ");
		else if(strncmp(temp, "set", 3)==0)
			linenoiseAddCompletion(lc, "setheader ");
		else if(strncmp(temp, "del", 3)==0)
			linenoiseAddCompletion(lc, "delheader ");
	}
}
Exemplo n.º 3
0
void completion(const char *buf, linenoiseCompletions *lc) {
  switch (buf[0]) {
  case 'b':
    linenoiseAddCompletion(lc, "begin");
    break;
  case 'c':
    linenoiseAddCompletion(lc, "commit");
    break;
  case 's':
    linenoiseAddCompletion(lc, "status");
    linenoiseAddCompletion(lc, "set");
    break;
  case 'r':
    linenoiseAddCompletion(lc, "rollback");
  }
}
Exemplo n.º 4
0
static void completionCallback(const char *buf, linenoiseCompletions *lc) {
    size_t startpos = 0;
    int mask;
    int i;
    size_t matchlen;
    sds tmp;

    if (strncasecmp(buf,"help ",5) == 0) {
        startpos = 5;
        while (isspace(buf[startpos])) startpos++;
        mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
    } else {
        mask = CLI_HELP_COMMAND;
    }

    for (i = 0; i < helpEntriesLen; i++) {
        if (!(helpEntries[i].type & mask)) continue;

        matchlen = strlen(buf+startpos);
        if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
            tmp = sdsnewlen(buf,startpos);
            tmp = sdscat(tmp,helpEntries[i].full);
            linenoiseAddCompletion(lc,tmp);
            sdsfree(tmp);
        }
    }
}
Exemplo n.º 5
0
static void add_completions( linenoiseCompletions *lc, variable_t *vars, const char *buf ){
	const char        *pos, *move;
	unsigned k, pos_num;
	bool has_punct = false;

	if ( vars ){
		for ( pos = move = buf, pos_num = k = 0; *move; move++, k++ ){
			if ( strchr( " ()[]{}\t\n", *move )) {
				pos = move + 1;
				pos_num = k;
				has_punct = true;
			}
		}

		if ( strstr( vars->key, pos )){
			char *newbuf = malloc( sizeof( char[ strlen(buf) + strlen(vars->key) + 4 ]));
			strcpy( newbuf, buf );
			strncpy( newbuf + pos_num + has_punct, vars->key, strlen(vars->key) + 1 );
			linenoiseAddCompletion( lc, newbuf );
			free( newbuf );
		}

		add_completions( lc, vars->left, buf );
		add_completions( lc, vars->right, buf );
	}
}
Exemplo n.º 6
0
static void autocomplete(const char *buf, linenoiseCompletions *lc)
{
	command_list_t *p;

	for (p = command_list_head; p != NULL; p = p->next)
		if (strncmp(buf, p->command, strlen(buf)) == 0)
			linenoiseAddCompletion(lc, p->command);
}
Exemplo n.º 7
0
void R_readline_completion(const char *buf, linenoiseCompletions *lc) {
  int i, n = LENGTH(R_readline_completions);
  int len = strlen(buf);
  for (i = 0; i < n; i++) {
    const char *c = CHAR(STRING_ELT(R_readline_completions, i));
    if (!strncmp(buf, c, len)) { linenoiseAddCompletion(lc, c); }
  }
}
Exemplo n.º 8
0
Arquivo: repl.c Projeto: mfikes/planck
void completion(const char *buf, linenoiseCompletions *lc) {
    int num_completions = 0;
    char **completions = cljs_get_completions(buf, &num_completions);
    for (int i = 0; i < num_completions; i++) {
        linenoiseAddCompletion(lc, completions[i]);
        free(completions[i]);
    }
    free(completions);
}
static int l_addcompletion(lua_State *L)
{
    linenoiseCompletions *completions = *((linenoiseCompletions **) luaL_checkudata(L, 1, LN_COMPLETION_TYPE));
    const char *entry                 = luaL_checkstring(L, 2);

    linenoiseAddCompletion(completions, (char *) entry);

    return handle_ln_ok(L);
}
Exemplo n.º 10
0
void completion(const char *buf, linenoiseCompletions *lc)
{
	int i;
	for (i=0; i < cc_len; i++)
	{
		if (complet(buf, CC[i].key) == 0)
			linenoiseAddCompletion(lc, CC[i].key);
	}
}
Exemplo n.º 11
0
static void completion(const char *buf, linenoiseCompletions *lc) {
    int numCompletionStrings;
    int i;
    int completionPosIndex;

    if (!strncmp(buf, "quit", strlen(buf))) {
        linenoiseAddCompletion(lc,"quit");
    }
    if (!strncmp(buf, "help", strlen(buf))) {
        linenoiseAddCompletion(lc,"help");
    }

    completionPosIndex = 0;
    numCompletionStrings = persfile_get_command_completions(buf, &completionPosIndex, completionStrings, maxStrings);

    for (i = 0; i < numCompletionStrings; i++) {
        linenoiseAddCompletion(lc,completionStrings[i]);
        free(completionStrings[i]);
    }
}
Exemplo n.º 12
0
void REPL::offerCompletion(const char* buf, linenoiseCompletions* lc)
{
    // IF there is a ( or ), then do nothing (we can't complete)
    QString buffer(buf);
    int lastIndexOfDot = -1;
    QString toInspect, toComplete;
    QRegExp nonCompletableChars(REGEXP_NON_COMPLETABLE_CHARS);

    // If we encounter a non acceptable character (see above)
    if (buffer.contains(nonCompletableChars)) {
        return;
    }

    // Decompose what user typed so far in 2 parts: what toInspect and what toComplete.
    lastIndexOfDot = buffer.lastIndexOf('.');
    if (lastIndexOfDot > -1) {
        toInspect = buffer.left(lastIndexOfDot);
        toComplete = buffer.right(buffer.length() - lastIndexOfDot - 1);
    } else {
        // Nothing to inspect: use the global "window" object
        toInspect = "window";
        toComplete = buffer;
    }

    // This will return an array of String with the possible completions
    QStringList completions = REPL::getInstance()->m_webframe->evaluateJavaScript(
                                  QString(JS_RETURN_POSSIBLE_COMPLETIONS).arg(
                                      toInspect,
                                      toComplete) 
                                  
                              ).toStringList();

    foreach(QString c, completions) {
        if (lastIndexOfDot > -1) {
            // Preserve the "toInspect" portion of the string to complete
            linenoiseAddCompletion(lc, QString("%1.%2").arg(toInspect, c).toLocal8Bit().data());
        } else {
            linenoiseAddCompletion(lc, c.toLocal8Bit().data());
        }
    }
}
Exemplo n.º 13
0
void table_completion_function(char const *prefix, linenoiseCompletions *lc) {
  std::vector<std::string> tables = osquery::Registry::names("table");
  size_t index = 0;

  while (index < tables.size()) {
    const std::string &table = tables.at(index);
    ++index;

    if (boost::algorithm::starts_with(table, prefix)) {
      linenoiseAddCompletion(lc, table.c_str());
    }
  }
}
Exemplo n.º 14
0
int console_init(void)
{
    int i = 0;
    char *full_name;
    char *short_name;
    int takes_filename_as_arg;
    while (mon_get_nth_command(i++, (const char **)&full_name, (const char **)&short_name, &takes_filename_as_arg)) {
        if (strlen(full_name)) {
            linenoiseAddCompletion(&command_lc, full_name);
            if (strlen(short_name)) {
                linenoiseAddCompletion(&command_lc, short_name);
            }
            if (takes_filename_as_arg) {
                linenoiseAddCompletion(&need_filename_lc, full_name);
                if (strlen(short_name)) {
                    linenoiseAddCompletion(&need_filename_lc, short_name);
                }
            }
        }
    }
    return 0;
}
Exemplo n.º 15
0
/**
 * Command line completion.
 * @param	buf	Text to complete.
 * @param	lc	Readline object.
 */
void cli_completion(const char *buf, linenoiseCompletions *lc) {
	unsigned int i;
	size_t bufsz = strlen(buf);

	for (i = 0; commands[i]; ++i) {
		char *cmd = commands[i];

		if (strlen(cmd) < bufsz)
			continue;
		if (!strncasecmp(buf, cmd, bufsz))
			linenoiseAddCompletion(lc, cmd);
	}
}
Exemplo n.º 16
0
Arquivo: lamb.c Projeto: sunlaobo/lamb
void completion(const char *buf, linenoiseCompletions *lc) {
    if (!strcasecmp(buf, "show") || !strcasecmp(buf, "show ")) {
        linenoiseAddCompletion(lc, "show client");
        linenoiseAddCompletion(lc, "show account");
        linenoiseAddCompletion(lc, "show company");
        linenoiseAddCompletion(lc, "show gateway");
    }
    
    if (!strcasecmp(buf, "kill") || !strcasecmp(buf, "kill ")) {
        linenoiseAddCompletion(lc, "kill account");
        linenoiseAddCompletion(lc, "kill gateway");
    }

    if (!strcasecmp(buf, "start") || !strcasecmp(buf, "start ")) {
        linenoiseAddCompletion(lc, "start account");
        linenoiseAddCompletion(lc, "start gateway");
    }

}
Exemplo n.º 17
0
int rl_complete(int ignore, int invoking_key) {
	const char *last_word;
	size_t start, end;
	char **matches, **match, buff[SHELL_INPUT_BUFF_SZ];

	if (invoking_key != '\t') {
		return EINVAL;
	}

	end = strlen(complcb_text);
	last_word = complcb_text + end;
	while ((last_word >= complcb_text) && !isspace(*last_word)) {
		--last_word;
	}
	start = ++last_word - complcb_text;

	if (rl_attempted_completion_function != NULL) {
		matches = rl_attempted_completion_function(last_word,
				start, end);
	}
	else {
		matches = NULL;
	}

	if (matches == NULL) {
		matches = rl_completion_matches(last_word,
				rl_completion_entry_function != NULL
					? rl_completion_entry_function
					: rl_filename_completion_function);
		if (matches == NULL) {
			return 0;
		}
	}

	strncpy(&buff[0], complcb_text, start);
	for (match = matches + 1; *match != NULL; ++match) {
		strcpy(&buff[start], *match);
		linenoiseAddCompletion(complcb_lc, &buff[0]);
		free(*match);
	}
	free(matches);

	return 0;
}
Exemplo n.º 18
0
static void fill_completions(const char *string_so_far, int initial_chars, int token_len, const linenoiseCompletions *possible_lc, linenoiseCompletions *lc)
{
    int word_index;

    lc->len = 0;
    for(word_index = 0; word_index < possible_lc->len; word_index++) {
        int i;
        for(i = 0; i < token_len; i++) {
            if (string_so_far[initial_chars + i] != possible_lc->cvec[word_index][i]) {
                break;
            }
        }
        if (i == token_len && possible_lc->cvec[word_index][token_len] != 0) {
            char *string_to_append = concat_strings(string_so_far, initial_chars, possible_lc->cvec[word_index]);
            linenoiseAddCompletion(lc, string_to_append);
            free(string_to_append);
        }
    }
}
Exemplo n.º 19
0
void 
file_completion_cb (const char *buf, linenoiseCompletions *lc) 
{
	DIR *dirp;
	struct dirent *dp;
	char *basec, *basen, *dirc, *dirn;
	int baselen, dirlen;
	char *fullpath;
	struct stat filestat;

	basec = strdup(buf);
	dirc = strdup(buf);
	dirn = dirname(dirc);
	dirlen = strlen(dirn);
	basen = basename(basec);
	baselen = strlen(basen);
	dirp = opendir(dirn);

	if (dirp) {
		while ((dp = readdir(dirp)) != NULL) {
			if (strncmp(basen, dp->d_name, baselen) == 0) {
				/* add 2 extra bytes for possible / in middle & at end */
				fullpath = (char *) malloc(strlen(dp->d_name) + dirlen + 3);
				strcpy(fullpath, dirn);
				if (fullpath[dirlen-1] != '/')
					strcat(fullpath, "/");
				strcat(fullpath, dp->d_name);
				if (stat(fullpath, &filestat) == 0) {
					if (S_ISDIR(filestat.st_mode)) {
						strcat(fullpath, "/");
					}
					linenoiseAddCompletion(lc,fullpath);
				}
				free(fullpath);
			}
		}

		closedir(dirp);
	}
	free(basec);
	free(dirc);
}
void completion_callbk(const char *buf, linenoiseCompletions *lc, size_t pos)
{
	const char **terms = completion_terms;
	const size_t n_terms = sizeof(completion_terms) / sizeof(char*);
	size_t i, j, n, start, termlen;
	char lookahread[1024];
	char lookback[1024];
	char newbuf[1024];

	if (pos == 0)
		return;

	/* Find the start position to compare lookup terms. */
	for (start = pos - 1; start > 0 && buf[start] != '\\'; start --);

	/* For each lookup terms, see if it has prefix matches the input string. */
	for (n = 0; n < n_terms; n++) {
		termlen = strlen(terms[n]);
		for (i = start, j = 0; i < pos && j < termlen; i++, j++) {
			if (buf[i] != terms[n][j])
				break;
		}

		/* If it matches, it is a completion option to add. */
		if (i == pos) {
			/* Setup the `lookback' buffer containing string before the
			 * position where lookup term being inserted. */
			snprintf(lookback, start + 1, "%s", buf);
			/* Setup the `lookahread' buffer containing string after the
			 * position where lookup term being inserted. */
			strcpy(lookahread, buf + pos);
			/* Concatenate everything. */
			sprintf(newbuf, "%s%s%s", lookback, terms[n], lookahread);

			/* Add this completion option. */
			linenoiseAddCompletion(lc, newbuf, start + termlen);
		}
	}
}
Exemplo n.º 21
0
static void linenoiseAddCompletionSuffix(linenoiseCompletions *lc, const char *buf, size_t completionPoint,
        const char *suffix, bool endSpace, bool endSlash) {
    char concat[2048];

    if (endSpace) {
        if (endSlash) {
            snprintf(concat, 2048, "%.*s%s/ ", (int) completionPoint, buf, suffix);
        }
        else {
            snprintf(concat, 2048, "%.*s%s ", (int) completionPoint, buf, suffix);
        }
    }
    else {
        if (endSlash) {
            snprintf(concat, 2048, "%.*s%s/", (int) completionPoint, buf, suffix);
        }
        else {
            snprintf(concat, 2048, "%.*s%s", (int) completionPoint, buf, suffix);
        }
    }

    linenoiseAddCompletion(lc, concat);
}
Exemplo n.º 22
0
static void completion_cb(const char *buf, linenoiseCompletions *lc)
{
  if (!buf || !strlen(buf) || !lc)
  {
    return;
  }
  
  for (CompletionMap::iterator completionIter = completionMap.lower_bound(buf);
    completionIter != completionMap.end(); completionIter++)
  {
    if (OSS::string_starts_with(completionIter->first, buf))
    {
      for (std::vector<std::string>::iterator iter = completionIter->second.begin();
        iter != completionIter->second.end(); iter++)
      {
        linenoiseAddCompletion(lc, iter->c_str());
      }
    }
    else
    {
      break;
    }
  }
}
Exemplo n.º 23
0
static void monitor_completions(const char *string_so_far, linenoiseCompletions *lc)
{
    int start_of_token, token_len;
    char *help_commands[] = {"help", "?"};
    const linenoiseCompletions help_lc = {
         sizeof(help_commands)/sizeof(*help_commands),
         help_commands
    };

    find_next_token(string_so_far, 0, &start_of_token, &token_len);
    if (!string_so_far[start_of_token + token_len]) {
         fill_completions(string_so_far, start_of_token, token_len, &command_lc, lc);
         return;
    }
    if (is_token_in(string_so_far + start_of_token, token_len, &help_lc)) {
        find_next_token(string_so_far, start_of_token + token_len, &start_of_token, &token_len);
        if (!string_so_far[start_of_token + token_len]){
             fill_completions(string_so_far, start_of_token, token_len, &command_lc, lc);
             return;
        }
    }
    if (is_token_in(string_so_far + start_of_token, token_len, &need_filename_lc)) {
        int start_of_path;
        DIR* dir;
        struct dirent *direntry;
        struct linenoiseCompletions files_lc = {0, NULL};
        int i;

        for (start_of_token += token_len; string_so_far[start_of_token] && isspace(string_so_far[start_of_token]); start_of_token++);
        if (string_so_far[start_of_token] != '"') {
            char *string_to_append = concat_strings(string_so_far, start_of_token, "\"");
            linenoiseAddCompletion(lc, string_to_append);
            free(string_to_append);
            return;
        }
        for (start_of_path = ++start_of_token, token_len = 0; string_so_far[start_of_token + token_len]; token_len++) {
            if(string_so_far[start_of_token + token_len] == '"'
            && string_so_far[start_of_token + token_len - 1] != '\\') {
                return;
            }
            if(string_so_far[start_of_token + token_len] == '/') {
                start_of_token += token_len + 1;
                token_len = -1;
            }
        }
        if (start_of_token == start_of_path) {
            dir = opendir(".");
        } else {
            char *path = concat_strings(string_so_far + start_of_path, start_of_token - start_of_path, "");
            dir = opendir(path);
            free(path);
        }
        if (dir) {
            for (direntry = readdir(dir); direntry; direntry = readdir(dir)) {
                if (strcmp(direntry->d_name, ".") && strcmp(direntry->d_name, "..")) {
                    char *entryname = lib_msprintf("%s%s", direntry->d_name, is_dir(direntry) ? "/" : "\"");
                    linenoiseAddCompletion(&files_lc, entryname);
                    lib_free(entryname);
                }
            }
            fill_completions(string_so_far, start_of_token, token_len, &files_lc, lc);
            for(i = 0; i < files_lc.len; i++) {
                free(files_lc.cvec[i]);
            }
            closedir(dir);
            return;
        }
    }
}