Beispiel #1
0
static void run_command(const struct command *cmds, char **av)
{
	const struct command *cmd = find_command(cmds, av[0]);
	const char *pf;
	char **args;

	if (!cmd) {
		PTR_ARRAY(array);
		const char *alias_name = av[0];
		const char *alias_value = find_alias(alias_name);
		struct error *err = NULL;
		int i;

		if (alias_value == NULL) {
			error_msg("No such command or alias: %s", alias_name);
			return;
		}
		if (!parse_commands(&array, alias_value, &err)) {
			error_msg("Parsing alias %s: %s", alias_name, err->msg);
			error_free(err);
			ptr_array_free(&array);
			return;
		}

		/* remove NULL */
		array.count--;

		for (i = 1; av[i]; i++)
			ptr_array_add(&array, xstrdup(av[i]));
		ptr_array_add(&array, NULL);

		run_commands(cmds, &array);
		ptr_array_free(&array);
		return;
	}

	if (config_file && cmds == commands && !allowed_command(cmd->name)) {
		error_msg("Command %s not allowed in config file.", cmd->name);
		return;
	}

	// By default change can't be merged with previous on.
	// Any command can override this by calling begin_change() again.
	begin_change(CHANGE_MERGE_NONE);

	current_command = cmd;
	args = av + 1;
	pf = parse_args(args, cmd->flags, cmd->min_args, cmd->max_args);
	if (pf)
		cmd->cmd(pf, args);
	current_command = NULL;

	end_change();
}
Beispiel #2
0
void ptr_array_insert(struct ptr_array *array, void *ptr, long pos)
{
	long count = array->count - pos;
	ptr_array_add(array, NULL);
	memmove(array->ptrs + pos + 1, array->ptrs + pos, count * sizeof(void *));
	array->ptrs[pos] = ptr;
}
Beispiel #3
0
// ----- _cd_list_add ----------------------------------------------------------
int _cd_list_add(cd_info_list_t tList, SCDestInfo sDInfo) {
  SCDestInfo * pDInfo = (SCDestInfo *) MALLOC(sizeof(SCDestInfo));
  *pDInfo = sDInfo;
  fprintf(stdout, "Aggiungo dest ");
  ip_prefix_dump(stdout, *pDInfo);
fprintf(stdout, "\n");
  return ptr_array_add(tList, &pDInfo);
} 
Beispiel #4
0
static void cmd_default(const char *pf, char **args)
{
	close_state();
	if (no_syntax())
		return;

	ptr_array_add(&current_syntax->default_colors, copy_string_array(args, count_strings(args)));
}
Beispiel #5
0
void add_binding(const char *keys, const char *command)
{
	struct binding *b;

	b = xnew(struct binding, 1);
	if (!parse_keys(&b->chain, keys)) {
		free(b);
		return;
	}

	b->command = xstrdup(command);
	ptr_array_add(&bindings, b);
}
Beispiel #6
0
static struct state *find_or_add_state(const char *name)
{
	struct state *st = find_state(current_syntax, name);

	if (st)
		return st;

	st = xnew0(struct state, 1);
	st->name = xstrdup(name);
	st->defined = false;
	st->type = -1;
	ptr_array_add(&current_syntax->states, st);
	return st;
}
Beispiel #7
0
static void cmd_list(const char *pf, char **args)
{
	const char *name = args[0];
	struct string_list *list;
	int i;

	close_state();
	if (no_syntax())
		return;

	list = find_string_list(current_syntax, name);
	if (list == NULL) {
		list = xnew0(struct string_list, 1);
		list->name = xstrdup(name);
		ptr_array_add(&current_syntax->string_lists, list);
	} else if (list->defined) {
Beispiel #8
0
static adlb_code xlb_workq_add_serial(xlb_work_unit* wu)
{
  TRACE("xlb_workq_add_serial()");
  uint32_t wu_idx;

  bool ok = ptr_array_add(&wu_array, wu, &wu_idx);
  CHECK_MSG(ok, "Could not add work unit");

  if (wu->target >= 0)
  {
    return add_targeted(wu, wu_idx);
  }
  else
  {
    return add_untargeted(wu, wu_idx);
  }
}
Beispiel #9
0
void finalize_syntax(struct syntax *syn, int saved_nr_errors)
{
	int i;

	if (syn->states.count == 0)
		error_msg("Empty syntax");

	for (i = 0; i < syn->states.count; i++) {
		struct state *s = syn->states.ptrs[i];
		if (!s->defined) {
			// this state has been referenced but not defined
			error_msg("No such state %s", s->name);
		}
	}
	for (i = 0; i < syn->string_lists.count; i++) {
		struct string_list *list = syn->string_lists.ptrs[i];
		if (!list->defined)
			error_msg("No such list %s", list->name);
	}

	if (syn->heredoc && !is_subsyntax(syn))
		error_msg("heredocend can be used only in subsyntaxes");

	if (find_any_syntax(syn->name))
		error_msg("Syntax %s already exists", syn->name);

	if (nr_errors != saved_nr_errors) {
		free_syntax(syn);
		return;
	}

	// unused states and lists cause warning only
	visit(syn->states.ptrs[0]);
	for (i = 0; i < syn->states.count; i++) {
		struct state *s = syn->states.ptrs[i];
		if (!s->visited && !s->copied)
			error_msg("State %s is unreachable", s->name);
	}
	for (i = 0; i < syn->string_lists.count; i++) {
		struct string_list *list = syn->string_lists.ptrs[i];
		if (!list->used)
			error_msg("List %s never used", list->name);
	}

	ptr_array_add(&syntaxes, syn);
}
Beispiel #10
0
bool find_tags(const char *name, struct ptr_array *tags)
{
	struct tag *t;
	size_t pos = 0;

	if (!load_tag_file())
		return false;

	t = xnew(struct tag, 1);
	while (next_tag(tag_file, &pos, name, 1, t)) {
		ptr_array_add(tags, t);
		t = xnew(struct tag, 1);
	}
	free(t);
	sort_tags(tags);
	return true;
}
Beispiel #11
0
static struct condition *add_condition(enum condition_type type, const char *dest, const char *emit)
{
	struct condition *c;
	struct state *d = NULL;

	if (no_state())
		return NULL;

	if (dest && !destination_state(dest, &d))
		return NULL;

	c = xnew0(struct condition, 1);
	c->a.destination = d;
	c->a.emit_name = emit ? xstrdup(emit) : NULL;
	c->type = type;
	ptr_array_add(&current_state->conds, c);
	return c;
}
Beispiel #12
0
// ----- _cr_info_list_add ----------------------------------------------------
int _cr_info_list_add(cr_info_list_t tList, SCRouterInfo * pCRInfo) {
  return ptr_array_add(tList, &pCRInfo);
} 
Beispiel #13
0
// -----[ _ip_trace_add ]--------------------------------------------
static inline ip_trace_item_t * _ip_trace_add(ip_trace_t * trace,
					      ip_trace_item_t * item)
{
  assert(ptr_array_add(trace->items, &item) >= 0);
  return item;
}
Beispiel #14
0
void push_file_location(struct file_location *loc)
{
	ptr_array_add(&file_locations, loc);
}
Beispiel #15
0
int main(int argc, char *argv[])
{
	const char *term = getenv("TERM");
	const char *home = getenv("HOME");
	const char *tag = NULL;
	const char *rc = NULL;
	const char *command = NULL;
	char *command_history_filename;
	char *search_history_filename;
	char *editor_dir;
	bool read_rc = true;
	int i;

	if (!home)
		home = "";
	home_dir = xstrdup(home);

	for (i = 1; i < argc; i++) {
		const char *opt = argv[i];

		if (opt[0] != '-' || !opt[1])
			break;
		if (!opt[2]) {
			switch (opt[1]) {
			case 'R':
				read_rc = false;
				continue;
			case 't':
				tag = opt_arg(opt, argv[++i]);
				continue;
			case 'r':
				rc = opt_arg(opt, argv[++i]);
				continue;
			case 'c':
				command = opt_arg(opt, argv[++i]);
				continue;
			case 'V':
				printf("%s %s\nWritten by Timo Hirvonen\n", program, version);
				return 0;
			}
			if (opt[1] == '-') {
				i++;
				break;
			}
		}
		printf("Usage: %s [-R] [-V] [-c command] [-t tag] [-r rcfile] [file]...\n", argv[0]);
		return 1;
	}

	if (!isatty(1)) {
		fprintf(stderr, "stdout doesn't refer to a terminal\n");
		return 1;
	}
	if (term == NULL || term[0] == 0) {
		fprintf(stderr, "TERM not set\n");
		return 1;
	}
	switch (term_init(term)) {
	case -1:
		fprintf(stderr, "terminal is hardcopy\n");
		return 1;
	case -2:
		fprintf(stderr, "terminal could not be found\n");
		return 1;
	case -3:
		fprintf(stderr, "terminfo database could not be found\n");
		return 1;
	}

	// create this early. needed if lock-files is true
	editor_dir = editor_file("");
	mkdir(editor_dir, 0755);
	free(editor_dir);

	setlocale(LC_CTYPE, "");
	charset = nl_langinfo(CODESET);
	if (streq(charset, "UTF-8"))
		term_utf8 = true;

	exec_builtin_rc(builtin_rc);
	fill_builtin_colors();

	// NOTE: syntax_changed() uses window. should possibly create window after reading rc
	window = new_window();
	root_frame = new_root_frame(window);

	if (read_rc) {
		if (rc) {
			read_config(commands, rc, true);
		} else {
			char *filename = editor_file("rc");
			if (read_config(commands, filename, false)) {
				free(filename);
				filename = xsprintf("%s/rc", pkgdatadir);
				read_config(commands, filename, true);
			}
			free(filename);
		}
	}

	update_all_syntax_colors();
	sort_aliases();

	/* Terminal does not generate signals for control keys. */
	set_signal_handler(SIGINT, SIG_IGN);
	set_signal_handler(SIGQUIT, SIG_IGN);
	set_signal_handler(SIGPIPE, SIG_IGN);

	/* Terminal does not generate signal for ^Z but someone can send
	 * us SIGTSTP nevertheless. SIGSTOP can't be caught.
	 */
	set_signal_handler(SIGTSTP, handle_sigtstp);

	set_signal_handler(SIGCONT, handle_sigcont);
	set_signal_handler(SIGWINCH, handle_sigwinch);

	load_file_history();
	command_history_filename = editor_file("command-history");
	search_history_filename = editor_file("search-history");
	history_load(&command_history, command_history_filename, command_history_size);
	history_load(&search_history, search_history_filename, search_history_size);
	if (search_history.count)
		search_set_regexp(search_history.ptrs[search_history.count - 1]);

	/* Initialize terminal but don't update screen yet.  Also display
	 * "Press any key to continue" prompt if there were any errors
	 * during reading configuration files.
	 */
	term_raw();
	if (nr_errors) {
		any_key();
		clear_error();
	}

	editor_status = EDITOR_RUNNING;

	for (; i < argc; i++)
		window_open_buffer(window, argv[i], false, NULL);
	if (window->views.count == 0)
		window_open_empty_buffer(window);
	set_view(window->views.ptrs[0]);

	if (command || tag)
		resize();

	if (command)
		handle_command(commands, command);
	if (tag) {
		PTR_ARRAY(array);
		ptr_array_add(&array, xstrdup("tag"));
		ptr_array_add(&array, xstrdup(tag));
		ptr_array_add(&array, NULL);
		run_commands(commands, &array);
		ptr_array_free(&array);
	}
	resize();
	main_loop();
	ui_end();

	// unlock files and add files to file history
	remove_frame(root_frame);

	history_save(&command_history, command_history_filename);
	history_save(&search_history, search_history_filename);
	free(command_history_filename);
	free(search_history_filename);
	save_file_history();
	return 0;
}