Example #1
0
static int
cmd_try_alias(int *argc, char ***argv)
{
	struct options_entry	 *o;
	int			  old_argc = *argc, new_argc;
	char			**old_argv = *argv, **new_argv;
	u_int			  size, idx;
	int			  i;
	size_t			  wanted;
	const char		 *s, *cp = NULL;

	o = options_get_only(global_options, "command-alias");
	if (o == NULL || options_array_size(o, &size) == -1 || size == 0)
		return (-1);

	wanted = strlen(old_argv[0]);
	for (idx = 0; idx < size; idx++) {
		s = options_array_get(o, idx);
		if (s == NULL)
			continue;

		cp = strchr(s, '=');
		if (cp == NULL || (size_t)(cp - s) != wanted)
			continue;
		if (strncmp(old_argv[0], s, wanted) == 0)
			break;
	}
	if (idx == size)
		return (-1);

	if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0)
		return (-1);

	*argc = new_argc + old_argc - 1;
	*argv = xcalloc((*argc) + 1, sizeof **argv);

	for (i = 0; i < new_argc; i++)
		(*argv)[i] = xstrdup(new_argv[i]);
	for (i = 1; i < old_argc; i++)
		(*argv)[new_argc + i - 1] = xstrdup(old_argv[i]);

	log_debug("alias: %s=%s", old_argv[0], cp + 1);
	for (i = 0; i < *argc; i++)
		log_debug("alias: argv[%d] = %s", i, (*argv)[i]);

	cmd_free_argv(new_argc, new_argv);
	return (0);
}
Example #2
0
File: cmd.c Project: tmux/tmux
static int
cmd_try_alias(int *argc, char ***argv)
{
	struct options_entry		 *o;
	struct options_array_item	 *a;
	union options_value		 *ov;
	int				  old_argc = *argc, new_argc, i;
	char				**old_argv = *argv, **new_argv;
	size_t				  wanted;
	const char			 *cp = NULL;

	o = options_get_only(global_options, "command-alias");
	if (o == NULL)
		return (-1);
	wanted = strlen(old_argv[0]);

	a = options_array_first(o);
	while (a != NULL) {
		ov = options_array_item_value(a);
		cp = strchr(ov->string, '=');
		if (cp != NULL &&
		    (size_t)(cp - ov->string) == wanted &&
		    strncmp(old_argv[0], ov->string, wanted) == 0)
			break;
		a = options_array_next(a);
	}
	if (a == NULL)
		return (-1);

	if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0)
		return (-1);

	*argc = new_argc + old_argc - 1;
	*argv = xcalloc((*argc) + 1, sizeof **argv);

	for (i = 0; i < new_argc; i++)
		(*argv)[i] = xstrdup(new_argv[i]);
	for (i = 1; i < old_argc; i++)
		(*argv)[new_argc + i - 1] = xstrdup(old_argv[i]);

	log_debug("alias: %s=%s", old_argv[0], cp + 1);
	for (i = 0; i < *argc; i++)
		log_debug("alias: argv[%d] = %s", i, (*argv)[i]);

	cmd_free_argv(new_argc, new_argv);
	return (0);
}
Example #3
0
static enum cmd_retval
cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
{
	struct args			*args = self->args;
	struct cmd_find_state		*fs = &item->target;
	struct client			*c = cmd_find_client(item, NULL, 1);
	struct session			*s = item->target.s;
	struct winlink			*wl = item->target.wl;
	struct options			*oo;
	enum options_table_scope	 scope;
	char				*argument, *name = NULL, *cause;
	const char			*target;
	int				 window, idx, ambiguous;
	struct options_entry		*o;

	window = (self->entry == &cmd_show_window_options_entry);
	if (args->argc == 0) {
		scope = options_scope_from_flags(args, window, fs, &oo, &cause);
		return (cmd_show_options_all(self, item, oo));
	}
	argument = format_single(item, args->argv[0], c, s, wl, NULL);

	name = options_match(argument, &idx, &ambiguous);
	if (name == NULL) {
		if (args_has(args, 'q'))
			goto fail;
		if (ambiguous)
			cmdq_error(item, "ambiguous option: %s", argument);
		else
			cmdq_error(item, "invalid option: %s", argument);
		goto fail;
	}
	if (*name == '@')
		scope = options_scope_from_flags(args, window, fs, &oo, &cause);
	else {
		if (options_get_only(global_options, name) != NULL)
			scope = OPTIONS_TABLE_SERVER;
		else if (options_get_only(global_s_options, name) != NULL)
			scope = OPTIONS_TABLE_SESSION;
		else if (options_get_only(global_w_options, name) != NULL)
			scope = OPTIONS_TABLE_WINDOW;
		else {
			scope = OPTIONS_TABLE_NONE;
			xasprintf(&cause, "unknown option: %s", argument);
		}
		if (scope == OPTIONS_TABLE_SERVER)
			oo = global_options;
		else if (scope == OPTIONS_TABLE_SESSION) {
			if (args_has(self->args, 'g'))
				oo = global_s_options;
			else if (s == NULL) {
				target = args_get(args, 't');
				if (target != NULL) {
					cmdq_error(item, "no such session: %s",
					    target);
				} else
					cmdq_error(item, "no current session");
				goto fail;
			} else
				oo = s->options;
		} else if (scope == OPTIONS_TABLE_WINDOW) {
			if (args_has(self->args, 'g'))
				oo = global_w_options;
			else if (wl == NULL) {
				target = args_get(args, 't');
				if (target != NULL) {
					cmdq_error(item, "no such window: %s",
					    target);
				} else
					cmdq_error(item, "no current window");
				goto fail;
			} else
				oo = wl->window->options;
		}
	}
	if (scope == OPTIONS_TABLE_NONE) {
		if (args_has(args, 'q'))
			goto fail;
		cmdq_error(item, "%s", cause);
		free(cause);
		goto fail;
	}
	o = options_get_only(oo, name);
	if (o != NULL)
		cmd_show_options_print(self, item, o, idx);

	free(name);
	free(argument);
	return (CMD_RETURN_NORMAL);

fail:
	free(name);
	free(argument);
	return (CMD_RETURN_ERROR);
}