Exemple #1
0
void
complete_variables(const char *cmd, const char **start)
{
	int i;
	size_t len;
	assert(initialized);

	/* currently we support only environment variables */
	if(*cmd != '$')
	{
		*start = cmd;
		add_completion(cmd);
		return;
	}
	cmd++;
	*start = cmd;

	/* add all variables that start with given beginning */
	len = strlen(cmd);
	for(i = 0; i < nvars; i++)
	{
		if(vars[i].name == NULL)
			continue;
		if(vars[i].removed)
			continue;
		if(strnoscmp(vars[i].name, cmd, len) == 0)
			add_completion(vars[i].name);
	}
	completion_group_end();
	add_completion(cmd);
}
Exemple #2
0
void
complete_colorschemes(const char name[])
{
	int i;
	size_t len;
	int schemes_len;
	char **schemes;

	len = strlen(name);
	schemes = list_color_schemes(&schemes_len);

	for(i = 0; i < schemes_len; i++)
	{
		if(schemes[i][0] != '.' || name[0] == '.')
		{
			if(strnoscmp(name, schemes[i], len) == 0)
			{
				vle_compl_add_match(schemes[i]);
			}
		}
	}

	free_string_array(schemes, schemes_len);

	vle_compl_finish_group();
	vle_compl_add_last_match(name);
}
Exemple #3
0
static node_t *
find_node(node_t *root, const char *name, int create, node_t **last)
{
	const char *end;
	size_t name_len;
	node_t *prev = NULL, *curr;
	node_t *new_node;

	name = skip_char(name, '/');
	if(*name == '\0')
		return root;

	end = until_first(name, '/');

	name_len = end - name;
	curr = root->child;
	while(curr != NULL)
	{
		int comp = strnoscmp(name, curr->name, name_len);
		if(comp == 0 && curr->name_len == name_len)
		{
			if(curr->valid && last != NULL)
				*last = curr;
			return find_node(curr, end, create, last);
		}
		else if(comp < 0)
		{
			break;
		}
		prev = curr;
		curr = curr->next;
	}

	if(!create)
		return NULL;

	new_node = malloc(sizeof(*new_node));
	if(new_node == NULL)
		return NULL;

	if((new_node->name = malloc(name_len + 1)) == NULL)
	{
		free(new_node);
		return NULL;
	}
	strncpy(new_node->name, name, name_len);
	new_node->name[name_len] = '\0';
	new_node->name_len = name_len;
	new_node->valid = 0;
	new_node->child = NULL;
	new_node->next = curr;

	if(root->child == curr)
		root->child = new_node;
	else
		prev->next = new_node;

	return find_node(new_node, end, create, last);
}
Exemple #4
0
/* Implementation of :. filename modifier. */
static int
apply_dot_mod(const char *path, char *buf, size_t buf_len)
{
	size_t len = strlen(curr_view->curr_dir);
	if(strnoscmp(path, curr_view->curr_dir, len) != 0 || path[len] == '\0')
		copy_str(buf, buf_len, path);
	else
		copy_str(buf, buf_len, path + len + 1);
	return 0;
}
Exemple #5
0
const char *
make_rel_path(const char path[], const char base[])
{
	static char buf[PATH_MAX];

	const char *p = path, *b = base;
	int i;
	int nslashes;

#ifdef _WIN32
	if(path[1] == ':' && base[1] == ':' && path[0] != base[0])
	{
		canonicalize_path(path, buf, sizeof(buf));
		return buf;
	}
#endif

	while(p[0] != '\0' && p[1] != '\0' && b[0] != '\0' && b[1] != '\0')
	{
		const char *op = p, *ob = b;
		if((p = strchr(p + 1, '/')) == NULL)
			p = path + strlen(path);
		if((b = strchr(b + 1, '/')) == NULL)
			b = base + strlen(base);
		if(p - path != b - base || strnoscmp(path, base, p - path) != 0)
		{
			p = op;
			b = ob;
			break;
		}
	}

	canonicalize_path(b, buf, sizeof(buf));
	chosp(buf);

	nslashes = 0;
	for(i = 0; buf[i] != '\0'; i++)
		if(buf[i] == '/')
			nslashes++;

	buf[0] = '\0';
	while(nslashes-- > 0)
		strcat(buf, "../");
	if(*p == '/')
		p++;
	canonicalize_path(p, buf + strlen(buf), sizeof(buf) - strlen(buf));
	chosp(buf);

	if(buf[0] == '\0')
		strcpy(buf, ".");

	return buf;
}
Exemple #6
0
/* Implementation of :~ filename modifier. */
static int
apply_tilde_mod(const char *path, char *buf, size_t buf_len)
{
	size_t home_len = strlen(cfg.home_dir);
	if(strnoscmp(path, cfg.home_dir, home_len - 1) != 0)
	{
		snprintf(buf, buf_len, "%s", path);
		return 0;
	}

	snprintf(buf, buf_len, "~%s", path + home_len - 1);
	return 0;
}
Exemple #7
0
int
path_starts_with(const char path[], const char prefix[])
{
	size_t len = strlen(prefix);

	if(len > 0 && prefix[len - 1] == '/')
	{
		len--;
	}

	return strnoscmp(path, prefix, len) == 0
	    && (path[len] == '\0' || path[len] == '/');
}
Exemple #8
0
int
path_starts_with(const char *path, const char *begin)
{
	size_t len = strlen(begin);

	if(len > 0 && begin[len - 1] == '/')
		len--;

	if(strnoscmp(path, begin, len) != 0)
		return 0;

	return (path[len] == '\0' || path[len] == '/');
}
Exemple #9
0
int
paths_are_equal(const char s[], const char t[])
{
	size_t s_len = strlen(s);
	size_t t_len = strlen(t);

	if(s_len > 0 && s[s_len - 1] == '/')
		s_len--;
	if(t_len > 0 && t[t_len - 1] == '/')
		t_len--;

	if(s_len == t_len)
	{
		return strnoscmp(s, t, s_len) == 0;
	}
	return 0;
}
Exemple #10
0
static void
complete_with_shared(const char *server, const char *file)
{
	NET_API_STATUS res;
	size_t len = strlen(file);

	do
	{
		PSHARE_INFO_502 buf_ptr;
		DWORD er = 0, tr = 0, resume = 0;
		wchar_t *wserver = to_wide(server + 2);

		if(wserver == NULL)
		{
			show_error_msg("Memory Error", "Unable to allocate enough memory");
			return;
		}

		res = NetShareEnum(wserver, 502, (LPBYTE *)&buf_ptr, -1, &er, &tr, &resume);
		free(wserver);
		if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
		{
			PSHARE_INFO_502 p;
			DWORD i;

			p = buf_ptr;
			for(i = 1; i <= er; i++)
			{
				char buf[512];
				WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)p->shi502_netname, -1, buf,
						sizeof(buf), NULL, NULL);
				strcat(buf, "/");
				if(strnoscmp(buf, file, len) == 0)
				{
					char *const escaped = escape_filename(buf, 1);
					vle_compl_add_match(escaped);
					free(escaped);
				}
				p++;
			}
			NetApiBufferFree(buf_ptr);
		}
	}
	while(res == ERROR_MORE_DATA);
}
Exemple #11
0
char *
replace_home_part_strict(const char path[])
{
	static char buf[PATH_MAX];
	size_t len;

	len = strlen(cfg.home_dir) - 1;
	if(strnoscmp(path, cfg.home_dir, len) == 0 &&
			(path[len] == '\0' || path[len] == '/'))
	{
		strncat(strcpy(buf, "~"), path + len, sizeof(buf) - strlen(buf) - 1);
	}
	else
	{
		copy_str(buf, sizeof(buf), path);
	}

	return buf;
}
Exemple #12
0
static void
complete_progs(const char *str, assoc_records_t records)
{
	int i;
	const size_t len = strlen(str);

	for(i = 0; i < records.count; i++)
	{
		char command[NAME_MAX];

		(void)extract_cmd_name(records.list[i].command, 1, sizeof(command),
				command);

		if(strnoscmp(command, str, len) == 0)
		{
			char *const escaped = escape_chars(command, "|");
			vle_compl_add_match(escaped);
			free(escaped);
		}
	}
}
Exemple #13
0
static void
filename_completion_internal(DIR * dir, const char * dirname,
		const char * filename, CompletionType type)
{
	struct dirent *d;

	size_t filename_len = strlen(filename);
	while((d = readdir(dir)) != NULL)
	{
		if(filename[0] == '\0' && d->d_name[0] == '.')
			continue;
		if(strnoscmp(d->d_name, filename, filename_len) != 0)
			continue;

		if(type == CT_DIRONLY && !is_dirent_targets_dir(d))
			continue;
		else if(type == CT_EXECONLY && !is_dirent_targets_exec(d))
			continue;
		else if(type == CT_DIREXEC && !is_dirent_targets_dir(d) &&
				!is_dirent_targets_exec(d))
			continue;

		if(is_dirent_targets_dir(d) && type != CT_ALL_WOS)
		{
			char with_slash[strlen(d->d_name) + 1 + 1];
			snprintf(with_slash, sizeof(with_slash), "%s/", d->d_name);
			vle_compl_add_path_match(with_slash);
		}
		else
		{
			vle_compl_add_path_match(d->d_name);
		}
	}

	vle_compl_finish_group();
	if(type != CT_EXECONLY)
	{
		vle_compl_add_last_path_match(filename);
	}
}
Exemple #14
0
char *
replace_home_part(const char directory[])
{
	static char buf[PATH_MAX];
	size_t len;

	len = strlen(cfg.home_dir) - 1;
	if(strnoscmp(directory, cfg.home_dir, len) == 0 &&
			(directory[len] == '\0' || directory[len] == '/'))
	{
		strncat(strcpy(buf, "~"), directory + len, sizeof(buf) - strlen(buf) - 1);
	}
	else
	{
		copy_str(buf, sizeof(buf), directory);
	}

	if(!is_root_dir(buf))
		chosp(buf);

	return buf;
}