Ejemplo n.º 1
0
void edit_deinit(const char *history_file,
		 int (*filter_cb)(void *ctx, const char *cmd))
{
	rl_callback_handler_remove();
	readline_free_completions();

	eloop_unregister_read_sock(STDIN_FILENO);

	if (history_file) {
		/* Save command history, excluding lines that may contain
		 * passwords. */
		HIST_ENTRY *h;
		history_set_pos(0);
		while ((h = current_history())) {
			char *p = h->line;
			while (*p == ' ' || *p == '\t')
				p++;
			if (filter_cb && filter_cb(edit_cb_ctx, p)) {
				h = remove_history(where_history());
				if (h) {
					os_free(h->line);
					free(h->data);
					os_free(h);
				} else
					next_history();
			} else
				next_history();
		}
		write_history(history_file);
	}
}
Ejemplo n.º 2
0
set the readline word delimiters for tab-completion");

static PyObject *
py_remove_history(PyObject *self, PyObject *args)
{
	int entry_number;
	HIST_ENTRY *entry;

	if (!PyArg_ParseTuple(args, "i:remove_history", &entry_number))
		return NULL;
	if (entry_number < 0) {
		PyErr_SetString(PyExc_ValueError,
				"History index cannot be negative");
		return NULL;
	}
	entry = remove_history(entry_number);
	if (!entry) {
		PyErr_Format(PyExc_ValueError,
			     "No history item at position %d",
			      entry_number);
		return NULL;
	}
	/* free memory allocated for the history entry */
	if (entry->line)
		free(entry->line);
	if (entry->data)
		free(entry->data);
	free(entry);

	Py_RETURN_NONE;
}
Ejemplo n.º 3
0
int	__history(char **args, t_utils *utils)
{
  if (match(args[1], "-c") || match(args[1], "--clear"))
    remove_history(&(utils->history));
  else
    print_liste((t_base *)utils->history);
  return (EXIT_SUCCESS);
}
Ejemplo n.º 4
0
//------------------------------------------------------------------------------
void add_to_history(const char* line)
{
    int dupe_mode;
    const unsigned char* c;

    // Maybe we shouldn't add this line to the history at all?
    c = (const unsigned char*)line;
    if (isspace(*c) && get_clink_setting_int("history_ignore_space") > 0)
    {
        return;
    }

    // Skip leading whitespace
    while (*c)
    {
        if (!isspace(*c))
        {
            break;
        }

        ++c;
    }

    // Skip empty lines
    if (*c == '\0')
    {
        return;
    }

    // Check if the line's a duplicate of and existing history entry.
    dupe_mode = get_clink_setting_int("history_dupe_mode");
    if (dupe_mode > 0)
    {
        int where = find_duplicate(c);
        if (where >= 0)
        {
            if (dupe_mode > 1)
            {
                remove_history(where);
            }
            else
            {
                return;
            }
        }
    }

    // All's well. Add the line.
    using_history();
    add_history(line);
    ++g_new_history_count;
}
Ejemplo n.º 5
0
int history_mgmt_remove(char *cmd)
{
	int offset=history_search_pos(cmd, 0, 0), occurences=0;
	while(offset>=0) {
		occurences++;
		free_history_entry(remove_history(offset));
		offset=history_search_pos(cmd, 0, ++offset);
	}
	if(occurences) {
		write_history(get_history_file_name());
		dirty=true;
	}
	return occurences;
}
Ejemplo n.º 6
0
static void save_cb(proto_t *proto)
{
    HIST_ENTRY *entry;

    if (history_length > 0)
    {
        /* removes the save command from history */
        entry = remove_history(history_length-1);
        free_history_entry(entry);

        /* saves the history in the file */
        write_history(proto->list[1]);
        protocol_response("resp 0", proto);
    }
}
Ejemplo n.º 7
0
// clear_history wrapper
tb_int_t xm_readline_clear_history(lua_State* lua)
{
    // check
    tb_assert_and_check_return_val(lua, 0);

#ifdef TB_CONFIG_OS_MACOSX
    // call clear_history (will crash on macOS)
    for (tb_int_t i = history_length - 1; i >= 0; --i)
        remove_history(i);
#else
    clear_history();
#endif

    // ok
    return 0;
}
Ejemplo n.º 8
0
/* format line and add it to history list, avoiding duplicates if necessary */
static void
my_add_history(char *line)
{       
  int lookback, count, here;
  char *new_entry, *filtered_line;

  filtered_line =  pass_through_filter(TAG_HISTORY, line);
 
  
  switch (history_duplicate_avoidance_policy) { 
  case KEEP_ALL_DOUBLES:
    lookback = 0; break;
  case ELIMINATE_SUCCESIVE_DOUBLES:
    lookback = 1; break;
  case ELIMINATE_ALL_DOUBLES:
    lookback = history_length; break;
  }

  
  new_entry = filtered_line;    
  
  lookback = min(history_length, lookback);      
  for (count = 0, here = history_length + history_base - 1;
       count < lookback ;
       count++, here--) {
    /* DPRINTF3(DEBUG_READLINE, "strcmping <%s> and <%s> (count = %d)", line, history_get(here)->line ,count); */
    if (strncmp(new_entry, history_get(here) -> line, 10000) == 0) {
      HIST_ENTRY *entry = remove_history (here - history_base); /* according to the history library doc this should be
                                                                   remove_history(here) @@@?*/
      DPRINTF2(DEBUG_READLINE, "removing duplicate entry #%d (%s)", here, entry->line);
      free_foreign(entry->line);
      free_foreign(entry);
    }
  }
  add_history(new_entry);
  free(new_entry);
}
Ejemplo n.º 9
0
/* format line and add it to history list, avoiding duplicates if necessary */
static void
my_add_history(char *line)
{       
  int lookback, count, here;
  char *new_entry, *filtered_line;

  filtered_line =  pass_through_filter(TAG_HISTORY, line);
 
  
  switch (history_duplicate_avoidance_policy) { 
  case KEEP_ALL_DOUBLES:
    lookback = 0; break;
  case ELIMINATE_SUCCESIVE_DOUBLES:
    lookback = 1; break;
  case ELIMINATE_ALL_DOUBLES:
    lookback = history_length; break;
  }

  
  new_entry = filtered_line;    
  
  lookback = min(history_length, lookback);      
  for (count = 0, here = history_length - 1;
       count < lookback ;
       count++, here--) {
    DPRINTF4(DEBUG_READLINE, "strcmping <%s> and <%s> (count = %d, here = %d)", line, history_get(history_base + here)->line ,count, here);
    if (strncmp(new_entry, history_get(history_base + here) -> line, 10000) == 0) { /* history_get uses the logical offset history_base .. */
       HIST_ENTRY *entry = remove_history (here);                                   /* .. but remove_history doesn't!                      */
      DPRINTF2(DEBUG_READLINE, "removing duplicate entry #%d (%s)", here, entry->line);
      free_foreign(entry->line);
      free_foreign(entry);
    }
  }
  add_history(new_entry);
  free(new_entry);
}
Ejemplo n.º 10
0
static PyObject *
py_remove_history(PyObject *self, PyObject *args)
{
    int entry_number;
    HIST_ENTRY *entry;

    if (!PyArg_ParseTuple(args, "i:remove_history_item", &entry_number))
        return NULL;
    if (entry_number < 0) {
        PyErr_SetString(PyExc_ValueError,
                        "History index cannot be negative");
        return NULL;
    }
    entry = remove_history(entry_number);
    if (!entry) {
        PyErr_Format(PyExc_ValueError,
                     "No history item at position %d",
                      entry_number);
        return NULL;
    }
    /* free memory allocated for the history entry */
    _py_free_history_entry(entry);
    Py_RETURN_NONE;
}
Ejemplo n.º 11
0
int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
{
	int argc, ret;
	char *input = NULL, *args[MAX_ARGS], **argv;

	rl_readline_name = "lvm";
	rl_attempted_completion_function = (CPPFunction *) _completion;

	_read_history(cmd);

	_cmdline = cmdline;

	_cmdline->interactive = 1;
	while (1) {
		free(input);
		input = readline("lvm> ");

		/* EOF */
		if (!input) {
			printf("\n");
			break;
		}

		/* empty line */
		if (!*input)
			continue;

		add_history(input);

		argv = args;

		if (lvm_split(input, &argc, argv, MAX_ARGS) == MAX_ARGS) {
			log_error("Too many arguments, sorry.");
			continue;
		}

		if (!argc)
			continue;

		if (!strcmp(argv[0], "lvm")) {
			argv++;
			argc--;
		}

		if (!argc)
			continue;

		if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "exit")) {
			remove_history(history_length - 1);
			log_error("Exiting.");
			break;
		}

		ret = lvm_run_command(cmd, argc, argv);
		if (ret == ENO_SUCH_CMD)
			log_error("No such command '%s'.  Try 'help'.",
				  argv[0]);

                if ((ret != ECMD_PROCESSED) && !error_message_produced()) {
			log_debug(INTERNAL_ERROR "Failed command did not use log_error");
			log_error("Command failed with status code %d.", ret);
		}
		_write_history();
	}

	free(input);
	return 0;
}
Ejemplo n.º 12
0
static void wpa_cli_interactive(void)
{
#define max_args 10
	char cmdbuf[256], *cmd, *argv[max_args], *pos;
	int argc;
#ifdef CONFIG_READLINE
	char *home, *hfile = NULL;
#endif /* CONFIG_READLINE */

	printf("\nInteractive mode\n\n");

#ifdef CONFIG_READLINE
	rl_attempted_completion_function = wpa_cli_completion;
	home = getenv("HOME");
	if (home) {
		const char *fname = ".wpa_cli_history";
		int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
		hfile = os_malloc(hfile_len);
		if (hfile) {
			os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
			hfile[hfile_len - 1] = '\0';
			read_history(hfile);
			stifle_history(100);
		}
	}
#endif /* CONFIG_READLINE */

	do {
		wpa_cli_recv_pending(monitor_conn, 0, 0);
#ifndef CONFIG_NATIVE_WINDOWS
		alarm(1);
#endif /* CONFIG_NATIVE_WINDOWS */
#ifdef CONFIG_READLINE
		cmd = readline("> ");
		if (cmd && *cmd) {
			HIST_ENTRY *h;
			while (next_history())
				;
			h = previous_history();
			if (h == NULL || os_strcmp(cmd, h->line) != 0)
				add_history(cmd);
			next_history();
		}
#else /* CONFIG_READLINE */
		printf("> ");
		cmd = fgets(cmdbuf, sizeof(cmdbuf), stdin);
#endif /* CONFIG_READLINE */
#ifndef CONFIG_NATIVE_WINDOWS
		alarm(0);
#endif /* CONFIG_NATIVE_WINDOWS */
		if (cmd == NULL)
			break;
		wpa_cli_recv_pending(monitor_conn, 0, 0);
		pos = cmd;
		while (*pos != '\0') {
			if (*pos == '\n') {
				*pos = '\0';
				break;
			}
			pos++;
		}
		argc = 0;
		pos = cmd;
		for (;;) {
			while (*pos == ' ')
				pos++;
			if (*pos == '\0')
				break;
			argv[argc] = pos;
			argc++;
			if (argc == max_args)
				break;
			if (*pos == '"') {
				char *pos2 = os_strrchr(pos, '"');
				if (pos2)
					pos = pos2 + 1;
			}
			while (*pos != '\0' && *pos != ' ')
				pos++;
			if (*pos == ' ')
				*pos++ = '\0';
		}
		if (argc)
			wpa_request(ctrl_conn, argc, argv);

		if (cmd != cmdbuf)
			os_free(cmd);
	} while (!wpa_cli_quit);

#ifdef CONFIG_READLINE
	if (hfile) {
		/* Save command history, excluding lines that may contain
		 * passwords. */
		HIST_ENTRY *h;
		history_set_pos(0);
		h = next_history();
		while (h) {
			char *p = h->line;
			while (*p == ' ' || *p == '\t')
				p++;
			if (os_strncasecmp(p, "pa", 2) == 0 ||
			    os_strncasecmp(p, "o", 1) == 0 ||
			    os_strncasecmp(p, "n", 1)) {
				h = remove_history(where_history());
				if (h) {
					os_free(h->line);
					os_free(h->data);
					os_free(h);
				}
				h = current_history();
			} else {
				h = next_history();
			}
		}
		write_history(hfile);
		os_free(hfile);
	}
#endif /* CONFIG_READLINE */
}
Ejemplo n.º 13
0
/*
 * Rename entry
 * Signal handler for "activate" rename entry
 */
gboolean rename_entry()
{
	FILE *fp = NULL;
	GtkWidget *name_dialog = NULL;
	GtkWidget *msg_dialog = NULL;
	GtkWidget *vbox = NULL;
	GtkWidget *hbox = NULL;
	GtkWidget *label = NULL;
	GtkWidget *name_entry = NULL;
	const gchar *entry_text = NULL;
	gchar old_entry_display_name[MAX_NAME_LEN];
	gchar new_entry_display_name[MAX_NAME_LEN];
	gchar new_entry_name[MAX_PATH_LEN];
	gchar old_entry_name[MAX_PATH_LEN];
	book_data *book = NULL;
	section_data *section = NULL;
	entry_data *entry = NULL;
	gchar *temp_string;
	gint result;

	// Assert master exists
	g_assert_nonnull(master);

	// Get currently selected
	book = get_current_book_or_return_with_warning();
	section = get_current_section_or_return_with_warning();
	entry = get_current_entry_or_return_with_warning();

	// Write current entry
	write_current_entry();

	// Create name dialog
	name_dialog = gtk_dialog_new_with_buttons(app_name, GTK_WINDOW(main_window),
			GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
			GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
			GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
	gtk_widget_set_size_request(name_dialog, SM_DLG_WIDTH, SM_DLG_HEIGHT);
	gtk_window_set_type_hint(GTK_WINDOW(name_dialog), GDK_WINDOW_TYPE_HINT_MENU);
	gtk_window_set_resizable(GTK_WINDOW(name_dialog), FALSE);

	label = gtk_label_new("Enter new entry name...");
	name_entry = gtk_entry_new();
	gtk_entry_set_max_length(GTK_ENTRY(name_entry), MAX_NAME_LEN-5);

	// Get auto name
	if(options.auto_name_entry == TRUE) {

		temp_string = g_malloc0(MAX_NAME_LEN);
		strdelchr(temp_string, get_auto_entry_name(get_text_view(book)), ILLEGAL_CHARS);
		temp_string[MAX_NAME_LEN-5] = 0;
		if(strcmp(temp_string, "") == 0)
			gtk_entry_set_text(GTK_ENTRY(name_entry), entry->name);
		else
			gtk_entry_set_text(GTK_ENTRY(name_entry), temp_string);
		g_free(temp_string);
	} else {

		gtk_entry_set_text(GTK_ENTRY(name_entry), entry->name);
	}

	vbox = gtk_dialog_get_content_area(GTK_DIALOG(name_dialog));

	hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);

	hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 5);
	gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 5);

	gtk_entry_set_activates_default(GTK_ENTRY(name_entry), TRUE);
	gtk_dialog_set_default_response (GTK_DIALOG(name_dialog),GTK_RESPONSE_ACCEPT);
	gtk_widget_show_all(name_dialog);

	// Run dialog
	result = gtk_dialog_run(GTK_DIALOG(name_dialog));

	switch (result) {
	case GTK_RESPONSE_ACCEPT:

		entry_text = gtk_entry_get_text(GTK_ENTRY(name_entry));

		// Scan name for illegal characters
		temp_string = g_malloc0(MAX_NAME_LEN);
		strdelchr(temp_string, entry_text, ILLEGAL_CHARS);
		gtk_entry_set_text(GTK_ENTRY(name_entry), temp_string);
		g_free(temp_string);
		entry_text = gtk_entry_get_text(GTK_ENTRY(name_entry));

		// Old entry file
		g_snprintf(old_entry_name, sizeof(old_entry_name),
			"%s%s%s%s%s%s%s.txt",
			note_dir, G_DIR_SEPARATOR_S,
			book->name, G_DIR_SEPARATOR_S,
			section->name, G_DIR_SEPARATOR_S,
			entry->name);

		// New entry file
		g_snprintf(new_entry_name, sizeof(new_entry_name),
			"%s%s%s%s%s%s%s.txt",
			note_dir, G_DIR_SEPARATOR_S,
			book->name, G_DIR_SEPARATOR_S,
			section->name, G_DIR_SEPARATOR_S,
			entry_text);

		// Ignore rename if names match
		if(strcmp(entry->name, entry_text) == 0) {
			gtk_widget_destroy(name_dialog);
			return TRUE;
		}

		// Set display name
		strncpy(old_entry_display_name, entry->name, MAX_NAME_LEN-5);
		if(strlen(old_entry_display_name) > 25)
			strcpy(old_entry_display_name+25, "...\0");

		strncpy(new_entry_display_name, entry_text, MAX_NAME_LEN-5);
		if(strlen(new_entry_display_name) > 25)
			strcpy(new_entry_display_name+25, "...\0");

		// Check that new entry name is valid
		if(strlen(entry_text) < 1) {

			sn_warning("Unable to rename entry [%s] to [%s].",
				entry->name, entry_text);

			msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window),
				GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
				"Unable to rename entry \"%s\" to \"%s\".",
				old_entry_display_name, new_entry_display_name);
			gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog),
				"Please ensure the entry name contains a valid character.");
			gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name);
			gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU);
			gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE);
			result = gtk_dialog_run(GTK_DIALOG(msg_dialog));

			gtk_widget_destroy(name_dialog);
			gtk_widget_destroy(msg_dialog);
			return FALSE;
		}

		// Check that new entry name is valid
		fp = fopen(new_entry_name, "wx");
		if (fp == NULL) {
			sn_warning("Unable to rename entry [%s] to [%s].",
				entry->name, entry_text);

			msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL,
				GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
				"Unable to rename entry \"%s\" to \"%s\".",
				old_entry_display_name, new_entry_display_name);
			gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog),
				"Please ensure the entry name doesn't aleady exist.");
			gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name);
			gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU);
			gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE);
			result = gtk_dialog_run(GTK_DIALOG(msg_dialog));

			gtk_widget_destroy(name_dialog);
			gtk_widget_destroy(msg_dialog);
			return FALSE;

		}
		fclose(fp);

		sn_trace("Renaming entry [%s] to [%s].", entry->name, entry_text);

		// Rename entry file
		result = rename(old_entry_name, new_entry_name);

		if(result == 0) {

			// Remove history
			remove_history();

			// Update entry
			strcpy(entry->name, entry_text);

			// Update book
			write_book(book, note_dir);

			// Update view
			populate_entries(book, section);
			on_entry_change(get_entry_view(book), book);

			gtk_widget_destroy(name_dialog);
			return TRUE;
		} else {
			sn_warning("Unable to rename entry [%s].", entry->name);
			gtk_widget_destroy(name_dialog);
			return FALSE;
		}

	default:
		gtk_widget_destroy(name_dialog);
		return FALSE;

	} // End switch

	return FALSE;
} // Rename entry
Ejemplo n.º 14
0
/*
 * Delete entry
 * Signal handler for "activate" delete entry
 */
gboolean delete_entry()
{
	GtkWidget *msg_dialog;
	gchar filename[MAX_PATH_LEN];
	gchar entry_display_name[MAX_NAME_LEN];
	GList *entry_item = NULL;
	book_data *book = NULL;
	section_data *section = NULL;
	entry_data *entry = NULL;
	gint result;

	// Assert master exists
	g_assert_nonnull(master);

	// Get currently selected
	book = get_current_book_or_return_with_warning();
	section = get_current_section_or_return_with_warning();
	entry = get_current_entry_or_return_with_warning();

	// Set display name
	strncpy(entry_display_name, entry->name, MAX_NAME_LEN-5);
	if(strlen(entry_display_name) > 25)
		strcpy(entry_display_name+25, "...\0");

	// Confirm delete action
	msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL,
		GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
		"Are you sure you want to delete\n\"%s\"?", entry_display_name);
	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog),
		"If you delete an entry, it is permanently lost.");
	gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name);
	gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU);
	gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE);

	result = gtk_dialog_run(GTK_DIALOG(msg_dialog));
	switch (result) {
	case GTK_RESPONSE_YES:

		// Delete entry text file
		g_snprintf(filename, sizeof(filename),
			"%s%s%s%s%s%s%s.txt",
			note_dir, G_DIR_SEPARATOR_S,
			book->name, G_DIR_SEPARATOR_S,
			section->name, G_DIR_SEPARATOR_S,
			entry->name);

		sn_trace("Deleting entry [%s] in delete_entry().",
			filename);

		result = remove(filename);

		if (result == 0) {

			// Remove history
			remove_history();

			// Select next entry
			entry_item = g_list_find(section->entry_list, entry);
			entry_item = entry_item->next;
			if(entry_item != NULL)
				section->curr_entry = entry_item->data;
			else
				section->curr_entry = NULL;

			// Remove entry
			section->entry_list = g_list_remove(section->entry_list, entry);
			free_entry(entry);

			// Write book
			write_book(book, note_dir);

			// Update view
			populate_entries(book, section);
			gtk_widget_grab_focus(GTK_WIDGET(get_entry_view(book)));
			gtk_widget_destroy(msg_dialog);
			return TRUE;

		} else {
			sn_warning("Unable to delete entry [%s].", entry->name);
			gtk_widget_destroy(msg_dialog);
			return FALSE;
		}

	default:
		gtk_widget_destroy(msg_dialog);
		return FALSE;
	}
	return FALSE;
} // Delete entry
Ejemplo n.º 15
0
/*
 * Trash entry
 * Signal handler for "activate" trash entry
 */
gboolean trash_entry()
{
	FILE *fp = NULL;
	GtkWidget *msg_dialog = NULL;
	gchar entry_display_name[MAX_NAME_LEN];
	gchar curr_entry_path[MAX_PATH_LEN];
	gchar trash_entry_path[MAX_PATH_LEN];
	GList *entry_item = NULL;
	book_data *book = NULL;
	section_data *section = NULL;
	section_data *trash_section = NULL;
	entry_data *entry = NULL;
	gint result;

	// Assert master exists
	g_assert_nonnull(master);

	// Get currently selected
	book = get_current_book_or_return_with_warning();
	section = get_current_section_or_return_with_warning();
	entry = get_current_entry_or_return_with_warning();
	
	// Get trash section
	trash_section = book->trash_section;
	
	// Curr entry file path
	g_snprintf(curr_entry_path, sizeof(curr_entry_path),
		"%s%s%s%s%s%s%s.txt",
		note_dir, G_DIR_SEPARATOR_S,
		book->name, G_DIR_SEPARATOR_S,
		section->name, G_DIR_SEPARATOR_S,
		entry->name);

	// Trash entry file path
	g_snprintf(trash_entry_path, sizeof(trash_entry_path),
		"%s%s%s%s%s%s%s.txt",
		note_dir, G_DIR_SEPARATOR_S,
		book->name, G_DIR_SEPARATOR_S,
		trash_section_name, G_DIR_SEPARATOR_S,
		entry->name);

	// Set display name
	strncpy(entry_display_name, entry->name, MAX_NAME_LEN-5);
	if(strlen(entry_display_name) > 25)
		strcpy(entry_display_name+25, "...\0");

	// Check that new entry path is valid
	fp = fopen(trash_entry_path, "wx");

	if (fp == NULL) {
		sn_warning("Unable to trash entry [%s].", trash_entry_path);

		msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL,
			GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
			"Unable to trash entry \"%s\".", entry_display_name);
		gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog),
			"Please ensure the entry name doesn't aleady exist.");
		gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU);
		gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE);
		gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name);
		result = gtk_dialog_run(GTK_DIALOG(msg_dialog));

		gtk_widget_destroy(msg_dialog);
		return FALSE;
	}
	fclose(fp);

	// Remove file created by previous open
	result = remove(trash_entry_path);

	sn_trace("Trashing entry [%s] to [%s].", curr_entry_path, trash_entry_path);

	// Move the entry file to the new path
	result = rename(curr_entry_path, trash_entry_path);

	if(result == 0) {

		// Remove history
		remove_history();

		// Select next entry
		entry_item = g_list_find(section->entry_list, entry);
		entry_item = entry_item->next;
		if(entry_item != NULL)
			section->curr_entry = entry_item->data;
		else
			section->curr_entry = NULL;

		// Remove entry
		section->entry_list = g_list_remove(section->entry_list, entry);

		// Append entry
		trash_section->entry_list = g_list_append(trash_section->entry_list, entry);

		// Update entry
		entry->parent_section = trash_section;

		// Write book
		write_book(book, note_dir);

		// Update view
		populate_entries(book, section);

		return TRUE;

	} else {
		sn_warning("Unable to trash entry [%s].", curr_entry_path);
		return FALSE;
	}

	return FALSE;
} // Trash entry
static void wpa_cli_reconnect(void)
{
	wpa_cli_close_connection();
	ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
	if (ctrl_conn) {
		printf("Connection to wpa_supplicant re-established\n");
#ifdef ANDROID
		if (wpa_ctrl_attach(monitor_conn) == 0) {
#else
		if (wpa_ctrl_attach(ctrl_conn) == 0) {
#endif
			wpa_cli_attached = 1;
		} else {
			printf("Warning: Failed to attach to "
			       "wpa_supplicant.\n");
		}
	}
}


static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
				 int action_monitor)
{
	int first = 1;
#ifdef ANDROID
	if (ctrl == NULL) {
#else
	if (ctrl_conn == NULL) {
#endif 
		wpa_cli_reconnect();
		return;
	}
	while (wpa_ctrl_pending(ctrl) > 0) {
		char buf[256];
		size_t len = sizeof(buf) - 1;
		if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
			buf[len] = '\0';
			if (action_monitor)
				wpa_cli_action_process(buf);
			else {
				if (in_read && first)
					printf("\n");
				first = 0;
				printf("%s\n", buf);
			}
		} else {
			printf("Could not read pending message.\n");
			break;
		}
	}

	if (wpa_ctrl_pending(ctrl) < 0) {
		printf("Connection to wpa_supplicant lost - trying to "
		       "reconnect\n");
		wpa_cli_reconnect();
	}
}


#ifdef CONFIG_READLINE
static char * wpa_cli_cmd_gen(const char *text, int state)
{
	static int i, len;
	const char *cmd;

	if (state == 0) {
		i = 0;
		len = os_strlen(text);
	}

	while ((cmd = wpa_cli_commands[i].cmd)) {
		i++;
		if (os_strncasecmp(cmd, text, len) == 0)
			return os_strdup(cmd);
	}

	return NULL;
}


static char * wpa_cli_dummy_gen(const char *text, int state)
{
	return NULL;
}


static char ** wpa_cli_completion(const char *text, int start, int end)
{
	return rl_completion_matches(text, start == 0 ?
				     wpa_cli_cmd_gen : wpa_cli_dummy_gen);
}
#endif /* CONFIG_READLINE */


static void wpa_cli_interactive(void)
{
#define max_args 10
	char cmdbuf[256], *cmd, *argv[max_args], *pos;
	int argc;
#ifdef CONFIG_READLINE
	char *home, *hfile = NULL;
#endif /* CONFIG_READLINE */

	printf("\nInteractive mode\n\n");

#ifdef CONFIG_READLINE
	rl_attempted_completion_function = wpa_cli_completion;
	home = getenv("HOME");
	if (home) {
		const char *fname = ".wpa_cli_history";
		int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
		hfile = os_malloc(hfile_len);
		if (hfile) {
			int res;
			res = os_snprintf(hfile, hfile_len, "%s/%s", home,
					  fname);
			if (res >= 0 && res < hfile_len) {
				hfile[hfile_len - 1] = '\0';
				read_history(hfile);
				stifle_history(100);
			}
		}
	}
#endif /* CONFIG_READLINE */

	do {
#ifdef ANDROID
		wpa_cli_recv_pending(monitor_conn, 0, 0);
#else
		wpa_cli_recv_pending(ctrl_conn, 0, 0);
#endif
#ifndef CONFIG_NATIVE_WINDOWS
		alarm(ping_interval);
#endif /* CONFIG_NATIVE_WINDOWS */
#ifdef CONFIG_READLINE
		cmd = readline("> ");
		if (cmd && *cmd) {
			HIST_ENTRY *h;
			while (next_history())
				;
			h = previous_history();
			if (h == NULL || os_strcmp(cmd, h->line) != 0)
				add_history(cmd);
			next_history();
		}
#else /* CONFIG_READLINE */
		printf("> ");
		cmd = fgets(cmdbuf, sizeof(cmdbuf), stdin);
#endif /* CONFIG_READLINE */
#ifndef CONFIG_NATIVE_WINDOWS
		alarm(0);
#endif /* CONFIG_NATIVE_WINDOWS */
		if (cmd == NULL)
			break;
#ifdef ANDROID
		wpa_cli_recv_pending(monitor_conn, 0, 0);
#else
		wpa_cli_recv_pending(ctrl_conn, 0, 0);
#endif
		pos = cmd;
		while (*pos != '\0') {
			if (*pos == '\n') {
				*pos = '\0';
				break;
			}
			pos++;
		}
		argc = 0;
		pos = cmd;
		for (;;) {
			while (*pos == ' ')
				pos++;
			if (*pos == '\0')
				break;
			argv[argc] = pos;
			argc++;
			if (argc == max_args)
				break;
			if (*pos == '"') {
				char *pos2 = os_strrchr(pos, '"');
				if (pos2)
					pos = pos2 + 1;
			}
			while (*pos != '\0' && *pos != ' ')
				pos++;
			if (*pos == ' ')
				*pos++ = '\0';
		}
		if (argc)
			wpa_request(ctrl_conn, argc, argv);

		if (cmd != cmdbuf)
			os_free(cmd);
	} while (!wpa_cli_quit);

#ifdef CONFIG_READLINE
	if (hfile) {
		/* Save command history, excluding lines that may contain
		 * passwords. */
		HIST_ENTRY *h;
		history_set_pos(0);
		while ((h = current_history())) {
			char *p = h->line;
			while (*p == ' ' || *p == '\t')
				p++;
			if (cmd_has_sensitive_data(p)) {
				h = remove_history(where_history());
				if (h) {
					os_free(h->line);
					os_free(h->data);
					os_free(h);
				} else
					next_history();
			} else
				next_history();
		}
		write_history(hfile);
		os_free(hfile);
	}
#endif /* CONFIG_READLINE */
}
Ejemplo n.º 17
0
/*
 * Move entry
 * Signal handler for "activate" move entry
 */
gboolean move_entry()
{
	FILE *fp = NULL;
	GtkWidget *msg_dialog = NULL;
	GtkWidget *section_dialog = NULL;
	GtkWidget *section_view = NULL;
	GtkTreeModel *section_model = NULL;
	GtkTreeSelection *section_selection = NULL;
	GtkTreeIter section_iter;
	gchar entry_display_name[MAX_NAME_LEN];
	gchar curr_entry_path[MAX_PATH_LEN];
	gchar move_entry_path[MAX_PATH_LEN];
	GList *entry_item = NULL;
	book_data *book = NULL;
	section_data *section = NULL;
	entry_data *entry = NULL;
	book_data *book_copy = NULL;
	section_data *section_copy = NULL;
	gint result;

	// Assert master exists
	g_assert_nonnull(master);

	// Get currently selected
	book = get_current_book_or_return_with_warning();
	section = get_current_section_or_return_with_warning();
	entry = get_current_entry_or_return_with_warning();

	// Create section view
	section_view = gtk_tree_view_new();

	// Create section dialog
	section_dialog = create_book_section_dialog(section_view,
		"Select the section to move the entry to...");

	// Init section view
	init_book_section_view(GTK_TREE_VIEW(section_view));

	// Create section entry model / store
	section_model = create_book_section_model(master);

	// Set section model on section view
	gtk_tree_view_set_model(GTK_TREE_VIEW(section_view),
		GTK_TREE_MODEL(section_model));
	if(section_model != NULL)
		g_object_unref(section_model);

	// Show and run view section dialog
	gtk_widget_show_all(section_dialog);
	result = gtk_dialog_run(GTK_DIALOG(section_dialog));

	switch (result) {
	case GTK_RESPONSE_ACCEPT:

		section_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(section_view));
		if(gtk_tree_selection_get_selected(section_selection,
				&section_model, &section_iter)) {

			gtk_tree_model_get(section_model, &section_iter, SECTION_ITEM,
				&section_copy, END_OF_LIST);

			if(section == NULL) {
				gtk_widget_destroy(section_dialog);
				return FALSE;
			}
		} else {
			gtk_widget_destroy(section_dialog);
			return FALSE;
		}

		book_copy = section_copy->parent_book;

		// Curr entry file path
		g_snprintf(curr_entry_path, sizeof(curr_entry_path),
			"%s%s%s%s%s%s%s.txt",
			note_dir, G_DIR_SEPARATOR_S,
			book->name, G_DIR_SEPARATOR_S,
			section->name, G_DIR_SEPARATOR_S,
			entry->name);

		// Move entry file path
		g_snprintf(move_entry_path, sizeof(move_entry_path),
			"%s%s%s%s%s%s%s.txt",
			note_dir, G_DIR_SEPARATOR_S,
			book_copy->name, G_DIR_SEPARATOR_S,
			section_copy->name, G_DIR_SEPARATOR_S,
			entry->name);

		// Set display name
		strncpy(entry_display_name, entry->name, MAX_NAME_LEN-5);
		if(strlen(entry_display_name) > 25)
			strcpy(entry_display_name+25, "...\0");

		// Check that new entry path is valid
		fp = fopen(move_entry_path, "wx");

		if (fp == NULL) {
			sn_warning("Unable to move entry [%s].", move_entry_path);

			msg_dialog = gtk_message_dialog_new(GTK_WINDOW(main_window), GTK_DIALOG_MODAL,
				GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
				"Unable to move entry \"%s\".", entry_display_name);
			gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog),
				"Please ensure the entry name doesn't aleady exist.");
			gtk_window_set_title(GTK_WINDOW(msg_dialog), app_name);
			gtk_window_set_type_hint(GTK_WINDOW(msg_dialog), GDK_WINDOW_TYPE_HINT_MENU);
			gtk_window_set_resizable(GTK_WINDOW(msg_dialog), FALSE);
			result = gtk_dialog_run(GTK_DIALOG(msg_dialog));

			gtk_widget_destroy(msg_dialog);
			gtk_widget_destroy(section_dialog);
			return FALSE;
		}
		fclose(fp);

		// Remove file created by previous open
		result = remove(move_entry_path);

		sn_trace("Moving entry [%s] to [%s].", curr_entry_path, move_entry_path);

		// Move the entry file to the new path
		result = rename(curr_entry_path, move_entry_path);

		if(result == 0) {

			// Remove history
			remove_history();

			// Select next entry
			entry_item = g_list_find(section->entry_list, entry);
			entry_item = entry_item->next;
			if(entry_item != NULL)
				section->curr_entry = entry_item->data;
			else
				section->curr_entry = NULL;

			// Remove entry
			section->entry_list = g_list_remove(section->entry_list, entry);

			// Append entry
			section_copy->entry_list = g_list_append(section_copy->entry_list, entry);

			// Update entry
			entry->parent_section = section_copy;

			// Write book
			write_book(book, note_dir);

			// Update view
			populate_entries(book, section);
			
			gtk_widget_destroy(section_dialog);
			return TRUE;

		} else {
			gtk_widget_destroy(section_dialog);
			sn_warning("Unable to move entry [%s].", curr_entry_path);
			return FALSE;
		}
		gtk_widget_destroy(section_dialog);
		return FALSE;

	default:
		gtk_widget_destroy(section_dialog);
		return FALSE;

	} // End switch

	return FALSE;
} // Move entry
Ejemplo n.º 18
0
main ()
{
  char line[1024], *t;
  int len, done = 0;

  line[0] = 0;

  using_history ();
  while (!done)
    {
      printf ("history$ ");
      fflush (stdout);
      t = fgets (line, sizeof (line) - 1, stdin);
      if (t && *t)
        {
          len = strlen (t);
          if (t[len - 1] == '\n')
            t[len - 1] = '\0';
        }

      if (!t)
        strcpy (line, "quit");

      if (line[0])
        {
          char *expansion;
          int result;

          using_history ();

          result = history_expand (line, &expansion);
          if (result)
            fprintf (stderr, "%s\n", expansion);

          if (result < 0 || result == 2)
            {
              free (expansion);
              continue;
            }

          add_history (expansion);
          strncpy (line, expansion, sizeof (line) - 1);
          free (expansion);
        }

      if (strcmp (line, "quit") == 0)
        done = 1;
      else if (strcmp (line, "save") == 0)
        write_history ("history_file");
      else if (strcmp (line, "read") == 0)
        read_history ("history_file");
      else if (strcmp (line, "list") == 0)
        {
          register HIST_ENTRY **the_list;
          register int i;

          the_list = history_list ();
          if (the_list)
            for (i = 0; the_list[i]; i++)
              printf ("%d: %s\n", i + history_base, the_list[i]->line);
        }
      else if (strncmp (line, "delete", 6) == 0)
        {
          int which;
          if ((sscanf (line + 6, "%d", &which)) == 1)
            {
              HIST_ENTRY *entry = remove_history (which);
              if (!entry)
                fprintf (stderr, "No such entry %d\n", which);
              else
                {
                  free (entry->line);
                  free (entry);
                }
            }
          else
            {
              fprintf (stderr, "non-numeric arg given to `delete'\n");
            }
        }
    }
}