示例#1
0
文件: foldersel.c 项目: twolife/claws
static void foldersel_new_folder(GtkButton *button, gpointer data)
{
	FolderItem *new_item;
	gchar *new_folder;
	gchar *disp_name;
	gchar *p;
	GtkTreeIter selected, new_child;
	GtkTreePath *selected_p, *new_child_p;
	GtkTreeStore *store;
	GtkTreeModel *model;
	GtkTreeSelection *selection;

	if (!selected_item || FOLDER_TYPE(selected_item->folder) == F_NEWS)
		return;

	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
	if (!gtk_tree_selection_get_selected(selection, &model, &selected))
		return;
	store = GTK_TREE_STORE(model);

	new_folder = input_dialog(_("New folder"),
				  _("Input the name of new folder:"),
				  _("NewFolder"));
	if (!new_folder) return;
	AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
示例#2
0
void rssyl_new_feed_cb(GtkAction *action,
		gpointer data)
{
	FolderView *folderview = (FolderView*)data;
	FolderItem *item;
	gchar *url;

	debug_print("RSSyl: new_feed_cb\n");

	g_return_if_fail(folderview->selected != NULL);

	item = folderview_get_selected_item(folderview);
	g_return_if_fail(item != NULL);
	g_return_if_fail(item->folder != NULL);

	url = input_dialog(_("Subscribe feed"),
			_("Input the URL of the news feed you wish to subscribe:"),
			"");
	if( url == NULL )	/* User cancelled */
		return;

	rssyl_subscribe(item, url, TRUE);

	g_free(url);
}
示例#3
0
void rssyl_new_folder_cb(GtkAction *action,
		gpointer data)
{
	FolderView *folderview = (FolderView*)data;
	FolderItem *item;
	FolderItem *new_item;
	gchar *new_folder, *p, *tmp;
	gint i = 1;

	if (!folderview->selected) return;

	item = folderview_get_selected_item(folderview);
	g_return_if_fail(item != NULL);
	g_return_if_fail(item->folder != NULL);

	new_folder = input_dialog(_("New folder"),
				  _("Input the name of new folder:"),
				  _("NewFolder"));
	if (!new_folder) return;

	p = strchr(new_folder, G_DIR_SEPARATOR);
	if (p) {
		alertpanel_error(_("'%c' can't be used in folder name."),
				 G_DIR_SEPARATOR);
		g_free(new_folder);
		return;
	}

	if (!folder_local_name_ok(new_folder)) {
		g_free(new_folder);
		return;
	}

	/* Find an unused name for new folder */
	/* TODO: Perhaps stop after X attempts? */
	tmp = g_strdup(new_folder);
	while (folder_find_child_item_by_name(item, tmp)) {
		debug_print("RSSyl: Folder '%s' already exists, trying another name\n",
				new_folder);
		g_free(tmp);
		tmp = g_strdup_printf("%s__%d", new_folder, ++i);
	}

	g_free(new_folder);
	new_folder = tmp;

	new_item = folder_create_folder(item, new_folder);
	if (!new_item) {
		alertpanel_error(_("Can't create the folder '%s'."), new_folder);
		g_free(new_folder);
		return;
	}

	g_free(new_folder);

	folder_write_list();
}
示例#4
0
void
action_off_status(PurplePluginAction *action)
{
	char buf[STRLEN];
	strncpy(buf, purple_prefs_get_string("/plugins/core/musictracker/string_off"), STRLEN);
	if (input_dialog("Status to Set When Player is OFF:", buf, STRLEN)) {
		purple_prefs_set_string("/plugins/core/musictracker/string_off",
				buf);
	}
}
示例#5
0
static void
select_unselect_cmd (const char *title, int cmd)
{
    char *reg_exp, *reg_exp_t;
    int i;
    int c;
    int dirflag = 0;

    reg_exp = input_dialog (title, "", easy_patterns ? "*" : ".");
    if (!reg_exp)
	return;
    if (!*reg_exp) {
	g_free (reg_exp);
	return;
    }

    reg_exp_t = reg_exp;

    /* Check if they specified a directory */
    if (*reg_exp_t == PATH_SEP) {
	dirflag = 1;
	reg_exp_t++;
    }
    if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) {
	dirflag = 1;
	reg_exp_t[strlen (reg_exp_t) - 1] = 0;
    }

    for (i = 0; i < current_panel->count; i++) {
	if (!strcmp (current_panel->dir.list[i].fname, ".."))
	    continue;
	if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) {
	    if (!dirflag)
		continue;
	} else {
	    if (dirflag)
		continue;
	}
	c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname,
			  match_file);
	if (c == -1) {
	    message (1, MSG_ERROR, _("  Malformed regular expression  "));
	    g_free (reg_exp);
	    return;
	}
	if (c) {
	    do_file_mark (current_panel, i, cmd);
	}
    }
    g_free (reg_exp);
}
void edit_wrap_cmd ()
{
    char *f;
    char s[12];
    sprintf (s, "%d", option_word_wrap_line_length);
    f = input_dialog (_(" Word wrap "), 
/* Not essential to translate */
    _(" Enter line length, 0 for off: "), s);
    if (f) {
	if (*f) {
	    option_word_wrap_line_length = atoi (f);
	    free (f);
	}
    }
}
示例#7
0
void
filtered_view_cmd (void)
{
    char *command;

    command =
	input_dialog (_(" Filtered view "),
		      _(" Filter command and arguments:"),
		      selection (current_panel)->fname);
    if (!command)
	return;

    view (command, "", 0, 0);

    g_free (command);
}
示例#8
0
文件: cmd.c 项目: ebichu/dd-wrt
void
filtered_view_cmd (void)
{
    char *command;

    command =
	input_dialog (_(" Filtered view "),
		      _(" Filter command and arguments:"),
		      MC_HISTORY_FM_FILTERED_VIEW,
		      selection (current_panel)->fname);
    if (!command)
	return;

    mc_internal_viewer (command, "", NULL, 0);

    g_free (command);
}
示例#9
0
static void add_mailbox(GtkAction *action, gpointer callback_data)
{
	MainWindow *mainwin = (MainWindow *) callback_data;
	gchar *path, *basename;
	Folder *folder;

	path = input_dialog(_("Add mailbox"),
			    _("Input the location of mailbox.\n"
			      "If the existing mailbox is specified, it will be\n"
			      "scanned automatically."),
			    "Mail");
	if (!path) return;
	if (folder_find_from_path(path)) {
		alertpanel_error(_("The mailbox '%s' already exists."), path);
		g_free(path);
		return;
	}
	basename = g_path_get_basename(path);

	if (!folder_local_name_ok(basename)) {
		g_free(path);
		g_free(basename);
		return;
	}

	folder = folder_new(folder_get_class_from_string("mailmbox"), 
			    !strcmp(path, "Mail") ? _("Mailbox") : basename,
			    path);
	g_free(basename);
	g_free(path);

	if (folder->klass->create_tree(folder) < 0) {
		alertpanel_error(_("Creation of the mailbox failed.\n"
				   "Maybe some files already exist, or you don't have the permission to write there."));
		folder_destroy(folder);
		return;
	}

	folder_add(folder);
	folder_scan_tree(folder, TRUE);

	folderview_set(mainwin->folderview);

	return;
}
示例#10
0
static void
other_btn_cb (GtkWidget *widget, gpointer data)
{
    struct select_keys_s *sk = data;
    char *uid;

    cm_return_if_fail (sk);
    uid = input_dialog ( _("Add key"),
                         _("Enter another user or key ID:"),
                         NULL );
    if (!uid)
        return;
    if (fill_clist (sk, uid, sk->proto) != NULL) {
	    gpgme_release(sk->select_ctx);
	    sk->select_ctx = NULL;
    }
    update_progress (sk, 0, sk->pattern);
    g_free (uid);
}
示例#11
0
static void new_folder_cb(GtkAction *action, gpointer data)
{
	FolderView *folderview = (FolderView *)data;
	FolderItem *item;
	FolderItem *new_item;
	gchar *new_folder;
	gchar *name;
	gchar *p;

	if (!folderview->selected) return;

	item = folderview_get_selected_item(folderview);
	g_return_if_fail(item != NULL);
	g_return_if_fail(item->folder != NULL);

	new_folder = input_dialog(_("New folder"),
				  _("Input the name of new folder:"),
				  _("NewFolder"));
	if (!new_folder) return;
	AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
示例#12
0
static void rename_newsgroup_cb(GtkAction *action, gpointer data)
{
	FolderView *folderview = (FolderView *)data;
	FolderItem *item;
	gchar *new_folder;
	gchar *name;
	gchar *message;

	item = folderview_get_selected_item(folderview);
	cm_return_if_fail(item != NULL);
	cm_return_if_fail(item->path != NULL);
	cm_return_if_fail(item->folder != NULL);

	name = trim_string(item->name, 32);
	message = g_strdup_printf(_("Input new name for '%s':"), name);
	new_folder = input_dialog(_("Rename newsgroup folder"), message, item->name);
	g_free(message);
	g_free(name);

	if (!new_folder) return;
	AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
示例#13
0
static void
view_goto_cmd (WDiff *view)
{
    static char prev[256];
    /* XXX some statics here, to be remembered between runs */

    off_t address;
    char *input;

    input = input_dialog(_(" Goto Address "), _(" Enter Address: "), prev);
    if (input != NULL) {
	const char *s = input;
	if (scan_unsigned(&s, &address) == 0 && *s == '\0') {
	    if (view->df[0].move) view->df[0].offs = address;
	    if (view->df[1].move) view->df[1].offs = address;
	    view->last_found = -1;
	    view_update(view);
	}
	g_free(input);
    }
}
示例#14
0
void rssyl_new_folder_cb(GtkAction *action, gpointer data)
{
	FolderView *folderview = (FolderView *)data;
	GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
	FolderItem *item;
	FolderItem *new_item;
	gchar *new_folder;
	gchar *name;
	gchar *p;
	RSSylFolderItem *ritem = NULL;

	if (!folderview->selected) return;

	item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
	g_return_if_fail(item != NULL);
	g_return_if_fail(item->folder != NULL);

	new_folder = input_dialog(_("New folder"),
				  _("Input the name of new folder:"),
				  _("NewFolder"));
	if (!new_folder) return;
	AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
示例#15
0
void
view_filtered_cmd (void)
{
    char *command;
    const char *initial_command;

    if (cmdline->buffer[0] == '\0')
        initial_command = selection (current_panel)->fname;
    else
        initial_command = cmdline->buffer;

    command =
        input_dialog (_("Filtered view"),
                      _("Filter command and arguments:"),
                      MC_HISTORY_FM_FILTERED_VIEW, initial_command);

    if (command != NULL)
    {
        mcview_viewer (command, "", 0);
        g_free (command);
        dialog_switch_process_pending ();
    }
}
示例#16
0
void rssyl_new_feed_cb(GtkAction *action, gpointer data)
{
	FolderView *folderview = (FolderView *)data;
	GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
	FolderItem *item;
	gchar *new_feed;

	debug_print("RSSyl: new_feed_cb\n");

	g_return_if_fail(folderview->selected != NULL);

	item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
	g_return_if_fail(item != NULL);
	g_return_if_fail(item->folder != NULL);

	new_feed = input_dialog(_("Subscribe feed"),
					 _("Input the URL of the news feed you wish to subscribe:"),
					 "");
	g_return_if_fail(new_feed != NULL);

	rssyl_subscribe_new_feed(item, new_feed, TRUE);

	g_free(new_feed);
}
示例#17
0
char *
vfs_get_password (const char *msg)
{
    return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD,
                         INPUT_COMPLETE_NONE);
}
示例#18
0
static void
exec_extension (const char *filename, const char *data, int *move_dir,
		int start_line)
{
    char *file_name;
    int cmd_file_fd;
    FILE *cmd_file;
    char *cmd = NULL;
    int expand_prefix_found = 0;
    int parameter_found = 0;
    char prompt[80];
    int run_view = 0;
    int def_hex_mode = default_hex_mode, changed_hex_mode = 0;
    int def_nroff_flag = default_nroff_flag, changed_nroff_flag = 0;
    int written_nonspace = 0;
    int is_cd = 0;
    char buffer[1024];
    char *p = 0;
    char *localcopy = NULL;
    int do_local_copy;
    time_t localmtime = 0;
    struct stat mystat;
    quote_func_t quote_func = name_quote;

    g_return_if_fail (filename != NULL);
    g_return_if_fail (data != NULL);

    /* Avoid making a local copy if we are doing a cd */
    if (!vfs_file_is_local (filename))
	do_local_copy = 1;
    else
	do_local_copy = 0;

    /*
     * All commands should be run in /bin/sh regardless of user shell.
     * To do that, create temporary shell script and run it.
     * Sometimes it's not needed (e.g. for %cd and %view commands),
     * but it's easier to create it anyway.
     */
    cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);

    if (cmd_file_fd == -1) {
	message (1, MSG_ERROR,
		 _(" Cannot create temporary command file \n %s "),
		 unix_error_string (errno));
	return;
    }
    cmd_file = fdopen (cmd_file_fd, "w");
    fputs ("#! /bin/sh\n", cmd_file);

    prompt[0] = 0;
    for (; *data && *data != '\n'; data++) {
	if (parameter_found) {
	    if (*data == '}') {
		char *parameter;
		parameter_found = 0;
		parameter = input_dialog (_(" Parameter "), prompt, "");
		if (!parameter) {
		    /* User canceled */
		    fclose (cmd_file);
		    unlink (file_name);
		    if (localcopy) {
			mc_ungetlocalcopy (filename, localcopy, 0);
			g_free (localcopy);
		    }
		    g_free (file_name);
		    return;
		}
		fputs (parameter, cmd_file);
		written_nonspace = 1;
		g_free (parameter);
	    } else {
		size_t len = strlen (prompt);

		if (len < sizeof (prompt) - 1) {
		    prompt[len] = *data;
		    prompt[len + 1] = 0;
		}
	    }
	} else if (expand_prefix_found) {
	    expand_prefix_found = 0;
	    if (*data == '{')
		parameter_found = 1;
	    else {
		int i = check_format_view (data);
		char *v;

		if (i) {
		    data += i - 1;
		    run_view = 1;
		} else if ((i = check_format_cd (data)) > 0) {
		    is_cd = 1;
		    quote_func = fake_name_quote;
		    do_local_copy = 0;
		    p = buffer;
		    data += i - 1;
		} else if ((i = check_format_var (data, &v)) > 0 && v) {
		    fputs (v, cmd_file);
		    g_free (v);
		    data += i;
		} else {
		    char *text;

		    if (*data == 'f') {
			if (do_local_copy) {
			    localcopy = mc_getlocalcopy (filename);
			    if (localcopy == NULL) {
				fclose (cmd_file);
				unlink (file_name);
				g_free (file_name);
				return;
			    }
			    mc_stat (localcopy, &mystat);
			    localmtime = mystat.st_mtime;
			    text = (*quote_func) (localcopy, 0);
			} else {
			    text = (*quote_func) (filename, 0);
			}
		    } else
			text = expand_format (NULL, *data, !is_cd);
		    if (!is_cd)
			fputs (text, cmd_file);
		    else {
			strcpy (p, text);
			p = strchr (p, 0);
		    }
		    g_free (text);
		    written_nonspace = 1;
		}
	    }
	} else {
	    if (*data == '%')
		expand_prefix_found = 1;
	    else {
		if (*data != ' ' && *data != '\t')
		    written_nonspace = 1;
		if (is_cd)
		    *(p++) = *data;
		else
		    fputc (*data, cmd_file);
	    }
	}
    }				/* for */

    /*
     * Make the script remove itself when it finishes.
     * Don't do it for the viewer - it may need to rerun the script,
     * so we clean up after calling view().
     */
    if (!run_view) {
	fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
    }

    fclose (cmd_file);

    if ((run_view && !written_nonspace) || is_cd) {
	unlink (file_name);
	g_free (file_name);
	file_name = NULL;
    } else {
	/* Set executable flag on the command file ... */
	chmod (file_name, S_IRWXU);
	/* ... but don't rely on it - run /bin/sh explicitly */
	cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
    }

    if (run_view) {
	altered_hex_mode = 0;
	altered_nroff_flag = 0;
	if (def_hex_mode != default_hex_mode)
	    changed_hex_mode = 1;
	if (def_nroff_flag != default_nroff_flag)
	    changed_nroff_flag = 1;

	/* If we've written whitespace only, then just load filename
	 * into view
	 */
	if (written_nonspace) {
	    view (cmd, filename, move_dir, start_line);
	    unlink (file_name);
	} else {
	    view (0, filename, move_dir, start_line);
	}
	if (changed_hex_mode && !altered_hex_mode)
	    default_hex_mode = def_hex_mode;
	if (changed_nroff_flag && !altered_nroff_flag)
	    default_nroff_flag = def_nroff_flag;
	repaint_screen ();
    } else if (is_cd) {
	char *q;
	*p = 0;
	p = buffer;
/*	while (*p == ' ' && *p == '\t')
 *	    p++;
 */
	/* Search last non-space character. Start search at the end in order
	   not to short filenames containing spaces. */
	q = p + strlen (p) - 1;
	while (q >= p && (*q == ' ' || *q == '\t'))
	    q--;
	q[1] = 0;
	do_cd (p, cd_parse_command);
    } else {
	shell_execute (cmd, EXECUTE_INTERNAL);
	if (console_flag) {
	    handle_console (CONSOLE_SAVE);
	    if (output_lines && keybar_visible) {
		show_console_contents (output_start_y,
				       LINES - keybar_visible -
				       output_lines - 1,
				       LINES - keybar_visible - 1);

	    }
	}
    }

    g_free (file_name);
    g_free (cmd);

    if (localcopy) {
	mc_stat (localcopy, &mystat);
	mc_ungetlocalcopy (filename, localcopy,
			   localmtime != mystat.st_mtime);
	g_free (localcopy);
    }
}
示例#19
0
文件: user.c 项目: dborca/mc
/* FIXME: recode this routine on version 3.0, it could be cleaner */
static void
execute_menu_command (WEdit *edit_widget, const char *commands)
{
    FILE *cmd_file;
    int  cmd_file_fd;
    int  expand_prefix_found = 0;
    char *parameter = 0;
    int  do_quote = 0;
    char prompt [80];
    int  col;
    char *file_name;
    int run_view = 0;

    /* Skip menu entry title line */
    commands = strchr (commands, '\n');
    if (!commands){
	return;
    }

    cmd_file_fd = mc_mkstemps (&file_name, "mcusr", SCRIPT_SUFFIX);

    if (cmd_file_fd == -1){
	message (1, MSG_ERROR, _(" Cannot create temporary command file \n %s "),
		 unix_error_string (errno));
	return;
    }
    cmd_file = fdopen (cmd_file_fd, "w");
    fputs ("#! /bin/sh\n", cmd_file);
    commands++;
    
    for (col = 0; *commands; commands++){
	if (col == 0) {
	    if (*commands != ' ' && *commands != '\t')
		break;
	    while (*commands == ' ' || *commands == '\t')
	        commands++;
	    if (*commands == 0)
		break;
	}
	col++;
	if (*commands == '\n')
	    col = 0;
	if (parameter){
	    if (*commands == '}'){
		char *tmp;
		*parameter = 0;
		parameter = input_dialog (_(" Parameter "), prompt, INPUT_LAST_TEXT);
		if (!parameter){
		    /* User canceled */
		    fclose (cmd_file);
		    unlink (file_name);
                    g_free (file_name);
		    return;
		}
		if (do_quote) {
    		    fputs (tmp = name_quote (parameter, 0), cmd_file);
		    g_free (tmp);
		} else
		    fputs (parameter, cmd_file);
		g_free (parameter);
		parameter = 0;
	    } else {
		if (parameter < &prompt [sizeof (prompt) - 1]) {
		    *parameter++ = *commands;
		} 
	    }
	} else if (expand_prefix_found){
	    expand_prefix_found = 0;
	    if (isdigit ((unsigned char) *commands)) {
		do_quote = atoi (commands);
		while (isdigit ((unsigned char) *commands))
		    commands++;
	    }
	    if (*commands == '{')
		parameter = prompt;
	    else{
		char *text = expand_format (edit_widget, *commands, do_quote);
		fputs (text, cmd_file);
		g_free (text);
	    }
	} else {
	    if (*commands == '%') {
		int i = check_format_view (commands + 1);
		if (i) {
		    commands += i;
		    run_view = 1;
		} else {
		    do_quote = 1; /* Default: Quote expanded macro */
		    expand_prefix_found = 1;
		}
	    } else
		fputc (*commands, cmd_file);
	}
    }
    fclose (cmd_file);
    chmod (file_name, S_IRWXU);
    if (run_view) {
	run_view = 0;
	mc_internal_viewer (file_name, NULL, &run_view, 0);
#ifdef USE_DLGSWITCH
	dlgswitch_process_pending();
#endif
    } else {
	/* execute the command indirectly to allow execution even
	 * on no-exec filesystems. */
	char *cmd = g_strconcat("/bin/sh ", file_name, (char *)NULL);
	shell_execute (cmd, EXECUTE_HIDE);
	g_free(cmd);
    }
    unlink (file_name);
    g_free (file_name);
}
示例#20
0
文件: ext.c 项目: inso/mc
static char *
exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
{
    GString *shell_string;
    char lc_prompt[80] = "\0";
    gboolean parameter_found = FALSE;
    gboolean expand_prefix_found = FALSE;

    shell_string = g_string_new ("");

    for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
    {
        if (parameter_found)
        {
            if (*lc_data == '}')
            {
                char *parameter;

                parameter_found = FALSE;
                parameter =
                    input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "",
                                  INPUT_COMPLETE_NONE);
                if (parameter == NULL)
                {
                    /* User canceled */
                    g_string_free (shell_string, TRUE);
                    exec_cleanup_file_name (filename_vpath, FALSE);
                    return NULL;
                }
                g_string_append (shell_string, parameter);
                written_nonspace = TRUE;
                g_free (parameter);
            }
            else
            {
                size_t len = strlen (lc_prompt);

                if (len < sizeof (lc_prompt) - 1)
                {
                    lc_prompt[len] = *lc_data;
                    lc_prompt[len + 1] = '\0';
                }
            }
        }
        else if (expand_prefix_found)
        {
            expand_prefix_found = FALSE;
            if (*lc_data == '{')
                parameter_found = TRUE;
            else
            {
                int i;
                char *v;

                i = check_format_view (lc_data);
                if (i != 0)
                {
                    lc_data += i - 1;
                    run_view = TRUE;
                }
                else
                {
                    i = check_format_cd (lc_data);
                    if (i > 0)
                    {
                        is_cd = TRUE;
                        quote_func = fake_name_quote;
                        do_local_copy = FALSE;
                        pbuffer = buffer;
                        lc_data += i - 1;
                    }
                    else
                    {
                        i = check_format_var (lc_data, &v);
                        if (i > 0 && v != NULL)
                        {
                            g_string_append (shell_string, v);
                            g_free (v);
                            lc_data += i;
                        }
                        else
                        {
                            char *text;

                            if (*lc_data != 'f')
                                text = expand_format (NULL, *lc_data, !is_cd);
                            else
                            {
                                text = exec_get_file_name (filename_vpath);
                                if (text == NULL)
                                {
                                    g_string_free (shell_string, TRUE);
                                    return NULL;
                                }
                            }

                            if (!is_cd)
                                g_string_append (shell_string, text);
                            else
                            {
                                strcpy (pbuffer, text);
                                pbuffer = strchr (pbuffer, 0);
                            }

                            g_free (text);
                            written_nonspace = TRUE;
                        }
                    }
                }
            }
        }
        else if (*lc_data == '%')
            expand_prefix_found = TRUE;
        else
        {
            if (*lc_data != ' ' && *lc_data != '\t')
                written_nonspace = TRUE;
            if (is_cd)
                *(pbuffer++) = *lc_data;
            else
                g_string_append_c (shell_string, *lc_data);
        }
    }                           /* for */
    return g_string_free (shell_string, FALSE);
}
示例#21
0
int
main (int argc, char **argv)
{
  gchar *confdir;

  gchar *host = g_strdup ("localhost");
  gchar *port = g_strdup ("22");
  gchar *sshkey = NULL;

  gchar *user = NULL;
  gchar *password = NULL;

  gchar *session = g_strdup ("thinnx");
  gchar *type = g_strdup ("unix-gnome");
  gchar *cookie = NULL;

  gboolean use_ssl = 0;

  gchar *link = g_strdup ("adsl");
  gchar *kbdtype = g_strdup ("pc104/us");

  gchar *geometry = g_strdup ("fullscreen");
  gchar *screeninfo = NULL;

  gchar *session_id = NULL;
  gchar *session_display = NULL;
  gchar *pcookie = NULL;
  
  gchar **nxssh_argv = (gchar**) g_malloc (sizeof(gchar*) * 9);

  gchar *restore_id = NULL;

  pid_t pid;

  int parent_pipe[2];	/* For talking to the parent */
  int child_pipe[2];	/* For talking to the child */

  gint in = 0, out = 0;

  gtk_init (&argc, &argv);

  homedir = g_get_home_dir ();
  confdir = g_strdup_printf ("%s/.nx/", homedir);
  sshkey = g_strdup_printf ("%s/.ssh/id_dsa", homedir);

  {
    struct stat info;
    
    if (stat (confdir, &info) == -1)
      {
      if (mkdir (confdir, 0777) == -1)
	g_critical ("Could not create directory %s: %s\n", 
		    confdir, strerror (errno));
      }
  }    

  {
#if GTK_MAJOR_VERSION == 2
    GdkScreen *screen;

    screen = gdk_screen_get_default ();
    
    screeninfo = g_strdup_printf ("%dx%dx%d+render", 
				  gdk_screen_get_width (screen),
				  gdk_screen_get_height (screen),
				  gdk_visual_get_best_depth ());
#else
    screeninfo = g_strdup_printf ("%dx%dx%d+render",
				  gdk_screen_width (),
				  gdk_screen_height (),
				  gdk_visual_get_best_depth ());
#endif
  }

  { /* get X authentication cookie information */
    FILE *xauth_output;
    gchar xauth[256] = {0};

    gchar *tmp = NULL;
    gchar **tmpv = NULL;
    gchar *display = NULL;

    /* avoid problems with "network" DISPLAY's */
    display = g_strdup (getenv ("DISPLAY"));
    tmpv = g_strsplit (display, ":", 3);
    g_free (display);
  
    display = g_strdup_printf (":%s", tmpv[1]);
    g_strfreev (tmpv);

    /* get the authorization token */
    tmp = g_strdup_printf (XAUTHBINDIR"/xauth list %s | "
			   "grep 'MIT-MAGIC-COOKIE-1' | "
			   "cut -d ' ' -f 5",
			   display);

    if ((xauth_output = popen (tmp, "r")) == NULL)
      {
	g_critical ("Failed to obtain xauth key: %s", strerror(errno));
	exit (1);
      }

    fread (xauth, sizeof(char), 256, xauth_output);
    xauth[strlen(xauth) - 1] = '\0';
    pclose (xauth_output);
    g_free (tmp);

    if (!strcmp (xauth, ""))
      {
	message_dialog ("Não foi possível obter um cookie de autenticação\n"
			"do servidor X. Impossível continuar.");
	exit (1);
      }

    cookie = g_strdup (xauth);
  }


  { /* read configuration file */
    FILE *fconf;
    gint fconf_fd;
    gchar **tmp, *key, *value;
    struct stat info;
    
    if (stat (CONFFILE, &info) == -1)
      {
	g_warning ("WARNING: Could not stat %s: %s.\n", 
		   CONFFILE, strerror (errno));
      }

    fconf = fopen (CONFFILE, "r");
    if (fconf == NULL)
      {
	g_critical ("Could not open %s: %s.\n", 
		    CONFFILE, strerror (errno));
      }
    else
      {
	fconf_fd = fileno (fconf);

	while (!feof (fconf))
	  {

	    buffer = read_line (fconf_fd);
	    if (!buffer)
	      break;

	    /* remove comments */
	    tmp = g_strsplit (buffer, "#", 2);
	    g_free (buffer);

	    buffer = g_strdup (tmp[0]);
	    g_strfreev (tmp);

	    /* check if we still have a key/value pair */
	    tmp = g_strsplit (buffer, "=", 2);
	    g_free (buffer);
	    if (tmp[1] == NULL || tmp[0] == NULL)
	      {
		g_strfreev (tmp);
		continue;
	      }

	    key = tmp[0];
	    value = tmp[1];

	    g_strstrip (key);
	    g_strstrip (value);

	    if (!strcmp ("host", key))
	      {
		g_free (host);
		host = g_strdup (value);
	      }
	    else if (!strcmp ("port", key))
	      {
		g_free (port);
		port = g_strdup (value);
	      }
	    else if (!strcmp ("sshkey", key))
	      {
		g_free (sshkey);
		sshkey = g_strdup (value);
	      }
	    else if (!strcmp ("session", key))
	      {
		g_free (session);
		session = g_strdup (value);
	      }
	    else if (!strcmp ("ssl", key))
	      {
		if (!strcmp (value, "yes"))
		    use_ssl = 1;
	      }
	    else if (!strcmp ("type", key))
	      {
		g_free (type);
		type = g_strdup (value);
	      }
	    else if (!strcmp ("link", key))
	      {
		g_free (link);
		link = g_strdup (value);
	      }
	    else if (!strcmp ("kbdtype", key))
	      {
		g_free (kbdtype);
		kbdtype = g_strdup (value);
	      }
	    else if (!strcmp ("geometry", key))
	      {
		g_free (geometry);
		geometry = g_strdup (value);
	      }
	    else
	      g_warning ("Unknown option in %s: %s=%s\n", 
			 CONFFILE, key, value);

	    g_strfreev (tmp);
	  }
	fclose (fconf);
      }
  }

  /* grab auth information from the user before anything else */
  input_dialog (&user, &password);

  if (!strcmp (user, "root"))
    {
      message_dialog ("O usuário root não pode entrar por aqui!");
      exit (1);
    }

  pipe (parent_pipe);
  pipe (child_pipe);

  pid = fork ();
  if (pid == -1)
    {
      g_critical ("Could not fork!\n");
      exit (1);
    }
  else if (pid == 0)
    {
      close (child_pipe[1]);
      dup2 (child_pipe[0], STDIN_FILENO);
      dup2 (parent_pipe[1], STDOUT_FILENO);

      nxssh_argv[0] = g_strdup (BINDIR"/nxssh");
      nxssh_argv[1] = g_strdup ("-nx");
      nxssh_argv[2] = g_strdup_printf ("-p%s", port);
      nxssh_argv[3] = g_strdup ("-i");
      nxssh_argv[4] = g_strdup (sshkey);
      nxssh_argv[5] = g_strdup_printf ("nx@%s", host);
      nxssh_argv[6] = g_strdup ("-2");
      nxssh_argv[7] = g_strdup ("-S");
      nxssh_argv[8] = NULL;

      execv (nxssh_argv[0], nxssh_argv);
    }
  else
    {
      close(parent_pipe[1]);

      out = parent_pipe[0];
      in = child_pipe[1];

      /* Handle initial hand-shaking */
      {
	gboolean ssh_authed = FALSE;

	while (!ssh_authed)
	  {
	    buffer = read_code (out);
	    if (!strcmp (buffer, "NX> 205"))
	      {
		flush_buffer (buffer);
		drop_line (out);
		drop_line (out);
		drop_chars (out, 56);
		write_line (in, "yes");
		drop_line (out);
		drop_line (out);

		/* buffer != NULL? but why?! */
		buffer = NULL;
	      }
	    else if (!strcmp (buffer, "NX> 208"))
	      { 
		/* OK, authenticating... */
	      }
	    else if (!strcmp (buffer, "NX> 203") || 
		     (!strcmp (buffer, "NX> 285")) ||
		     (!strcmp (buffer, "NX> 200")) ||
		     (!strcmp (buffer, "NX> 202")))
	      {
		/* ignored stderr */
	      }
	    else if (!strncmp (buffer, "nxssh", 5))
	      {
		gchar *msg;

		flush_buffer (buffer);
		buffer = read_line (out);
		msg = get_info_after_colon (buffer);
		g_free (buffer);

		if (!strcmp (msg, "Name or service not known"))
		  message_dialog ("Não foi possível resolver o nome do servidor.");
		else if (!strcmp (msg, "Connection refused"))
		  message_dialog ("A conexão foi recusada!\n"
				  "Verifique a porta.");
		else if (!strcmp (msg, "Connection timed out"))
		  message_dialog ("Tempo limite da conexão expirou!\n"
				  "Verifique servidor e porta.");

		flush_buffer (msg);
		fprintf (stderr, "\n");
		exit (1);
	      }
	    else if (!strcmp (buffer, "NX> 204"))
	      {
		message_dialog ("Falha na autenticação inicial!\n"
				"Confira a chave privada.");
		g_critical ("Failed to authenticate to SSH using the public key!\n");
		exit (1);
	      }
	    else if (!strcmp (buffer, "HELLO N"))
	      {
		/* OK, time to say HELLO! */
		ssh_authed = TRUE;
	      }
	    else
	      protocol_error ("problems waiting for HELLO");

	    flush_buffer (buffer);

	    buffer = read_line (out);
	    flush_buffer (buffer);
	  }
      }

      /* Handle HELLO */
      buffer = read_code (out);
      if (!strcmp (buffer, "NX> 105"))
	{
	  flush_buffer (buffer);
	  drop_chars (out, 1);
	  write_line (in, "HELLO NXCLIENT - Version 1.4.0");
	  drop_line (out);
	}
      else
	protocol_error ("problems during HELLO");

      /* Handle Login */
      buffer = read_code (out);
      if (!strcmp (buffer, "NX> 134"))
	{
	  flush_buffer (buffer);
	  buffer = read_line (out);
	  flush_buffer (buffer);
	}
      else
	protocol_error ("HELLO failed?");

      buffer = read_code (out);
      if (!strcmp (buffer, "NX> 105"))
	{
	  flush_buffer (buffer);
	  drop_chars (out, 1);
	  write_line (in, "login");
	  drop_line (out);
	}
      else
	protocol_error ("No login? How come!");

      buffer = read_code (out);
      if (!strcmp (buffer, "NX> 101"))
	{
	  flush_buffer (buffer);
	  drop_chars (out, 7);
	  write_line (in, user);
	  drop_line (out);
	}
      else
	protocol_error ("who took my login prompt away?");

      buffer = read_code (out);
      if (!strcmp (buffer, "NX> 102"))
	{
	  flush_buffer (buffer);
	  drop_chars (out, 11);
	  write_line (in, password);
	  drop_line (out);
	}
      else
	protocol_error ("where is my password prompt?");

      buffer = read_code (out);
      if (!strcmp (buffer, "NX> 103"))
	{
	  flush_buffer (buffer);
	  drop_line (out);
	}
      else
	{
	  flush_buffer (buffer);
	  message_dialog ("Login ou senha incorretos!");
	  exit (1);
	}

      buffer = read_code (out);
      if (!strcmp (buffer, "NX> 105"))
	{
	  gchar *m;

	  flush_buffer (buffer);
	  drop_chars (out, 1);

	  m = g_strdup_printf ("list %s", user);
	  write_line (in, m);
	  g_free (m);

	  drop_lines (out, 5);

	  while (1)
	    {
	      buffer = read_code (out);
	      if (!strcmp (buffer, "NX> 105"))
		{
		  flush_buffer (buffer);
		  drop_chars (out, 1);
		  break;
		}

	      flush_buffer (buffer);
	      buffer = read_line (out);
	      restore_id = get_restore_id (buffer);
	    }
	}
      else
	protocol_error ("session startup, buddy, I don't want problems!");
      

      {
	gchar *cmdline;
	
	if (!restore_id)
	  cmdline = g_strdup_printf ("startsession --session=\"%s\" --type=\"%s\" --cache=\"8M\" --images=\"32M\" --cookie=\"%s\" --link=\"%s\" --kbtype=\"%s\" --nodelay=\"1\" --backingstore=\"never\" --geometry=\"%s\" --media=\"0\" --agent_server=\"\" --agent_user=\"\" --agent_password=\"\" --screeninfo=\"%s\" --encryption=\"%d\"", session, type, cookie, link, kbdtype, geometry, screeninfo, use_ssl);
	else
	  cmdline = g_strdup_printf ("restoresession --session=\"%s\" --type=\"%s\" --cache=\"8M\" --images=\"32M\" --cookie=\"%s\" --link=\"%s\" --kbtype=\"%s\" --nodelay=\"1\" --backingstore=\"never\" --geometry=\"%s\" --media=\"0\" --agent_server=\"\" --agent_user=\"\" --agent_password=\"\" --screeninfo=\"%s\" --encryption=\"%d\" --id=\"%s\"", session, type, cookie, link, kbdtype, geometry, screeninfo, use_ssl, restore_id);
	
	write_line (in, cmdline);
	g_free (cmdline);
	cmdline = NULL;
	
	drop_lines (out, 4);
      }

      session_id = get_session_info (out, "NX> 700");
      session_display = get_session_info (out, "NX> 705");
      drop_line (out); /* 703 (session type) */
      pcookie = get_session_info (out, "NX> 701");
      drop_line (out); /* 702 proxy ip */
      drop_line (out); /* 706 agent cookie */
      drop_line (out); /* 704 session cache */
      {
	gchar *tmp = get_session_info (out, "NX> 707"); /* 707 ssl tunneling */
	
	use_ssl = atoi (tmp);
	g_free (tmp);
      }
      drop_line (out); /* 710 session status: running */
      drop_line (out); /* 1002 commit */
      drop_line (out); /* 1006 session status: running */

      read_code (out);
      drop_chars (out, 1);

      /* now prepare to run nxproxy */
      {
	FILE *options;
		
	gchar *dirname;
	gchar *fname;
	gchar *cmdline;
		
	dirname = g_strdup_printf ("%s/.nx/S-%s", homedir, session_id);
	fname = g_strdup_printf ("%s/options", dirname);
		
	g_print ("Dir: %s\nFname: %s\n", dirname, fname);
		
	if (mkdir (dirname, 0777) == -1)
	  {
	    /* BOMB or handle 'directory already exists' */
	  }
	g_free (dirname);
		
	if (use_ssl)
	  buffer = g_strdup_printf ("cookie=%s,root=%s/.nx,session=%s,id=%s,listen=%d:%s", pcookie, homedir, session, session_id, 8008, session_display);
	else
	  buffer = g_strdup_printf ("cookie=%s,root=%s/.nx,session=%s,id=%s,connect=%s:%s", pcookie, homedir, session, session_id, host, session_display);
		
	options = fopen (fname, "w");
	fwrite (buffer, sizeof(char), strlen (buffer), options);
	fclose (options);
		
	g_free (buffer);
		
	cmdline = g_strdup_printf (BINDIR"/nxproxy -S options=%s:%s", fname, session_display);
	system (cmdline);
	g_free (cmdline);
		
	g_free (fname);
      }
    }

  write_line (in, "bye");
  drop_line (out);

  if (use_ssl)
    write_line (in, "switch");

  drop_line (out);
  drop_line (out);
  drop_line (out);

  return 0;
}
示例#22
0
static void
execute_menu_command (WEdit * edit_widget, const char *commands, gboolean show_prompt)
{
    FILE *cmd_file;
    int cmd_file_fd;
    int expand_prefix_found = 0;
    char *parameter = 0;
    gboolean do_quote = FALSE;
    char lc_prompt[80];
    int col;
    vfs_path_t *file_name_vpath;
    int run_view = 0;

    /* Skip menu entry title line */
    commands = strchr (commands, '\n');
    if (!commands)
    {
        return;
    }

    cmd_file_fd = mc_mkstemps (&file_name_vpath, "mcusr", SCRIPT_SUFFIX);

    if (cmd_file_fd == -1)
    {
        message (D_ERROR, MSG_ERROR, _("Cannot create temporary command file\n%s"),
                 unix_error_string (errno));
        vfs_path_free (file_name_vpath);
        return;
    }
    cmd_file = fdopen (cmd_file_fd, "w");
    fputs ("#! /bin/sh\n", cmd_file);
    commands++;

    for (col = 0; *commands; commands++)
    {
        if (col == 0)
        {
            if (*commands != ' ' && *commands != '\t')
                break;
            while (*commands == ' ' || *commands == '\t')
                commands++;
            if (*commands == 0)
                break;
        }
        col++;
        if (*commands == '\n')
            col = 0;
        if (parameter)
        {
            if (*commands == '}')
            {
                *parameter = 0;
                parameter =
                    input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_FM_MENU_EXEC_PARAM, "",
                                  INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD |
                                  INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_VARIABLES |
                                  INPUT_COMPLETE_USERNAMES);
                if (!parameter || !*parameter)
                {
                    /* User canceled */
                    fclose (cmd_file);
                    mc_unlink (file_name_vpath);
                    vfs_path_free (file_name_vpath);
                    return;
                }
                if (do_quote)
                {
                    char *tmp;

                    tmp = name_quote (parameter, 0);
                    fputs (tmp, cmd_file);
                    g_free (tmp);
                }
                else
                    fputs (parameter, cmd_file);
                g_free (parameter);
                parameter = 0;
            }
            else
            {
                if (parameter < &lc_prompt[sizeof (lc_prompt) - 1])
                {
                    *parameter++ = *commands;
                }
            }
        }
        else if (expand_prefix_found)
        {
            expand_prefix_found = 0;
            if (g_ascii_isdigit ((gchar) * commands))
            {
                do_quote = (atoi (commands) != 0);
                while (g_ascii_isdigit ((gchar) * commands))
                    commands++;
            }
            if (*commands == '{')
                parameter = lc_prompt;
            else
            {
                char *text = expand_format (edit_widget, *commands, do_quote);
                fputs (text, cmd_file);
                g_free (text);
            }
        }
        else
        {
            if (*commands == '%')
            {
                int i = check_format_view (commands + 1);
                if (i)
                {
                    commands += i;
                    run_view = 1;
                }
                else
                {
                    do_quote = TRUE;    /* Default: Quote expanded macro */
                    expand_prefix_found = 1;
                }
            }
            else
                fputc (*commands, cmd_file);
        }
    }
    fclose (cmd_file);
    mc_chmod (file_name_vpath, S_IRWXU);
    if (run_view)
    {
        mcview_viewer (vfs_path_as_str (file_name_vpath), NULL, 0);
        dialog_switch_process_pending ();
    }
    else
    {
        /* execute the command indirectly to allow execution even
         * on no-exec filesystems. */
        char *cmd;

        cmd = g_strconcat ("/bin/sh ", vfs_path_as_str (file_name_vpath), (char *) NULL);
        if (!show_prompt)
        {
            if (system (cmd) == -1)
                message (D_ERROR, MSG_ERROR, "%s", _("Error calling program"));
        }
        else
        {
            shell_execute (cmd, EXECUTE_HIDE);
        }
        g_free (cmd);
    }
    mc_unlink (file_name_vpath);
    vfs_path_free (file_name_vpath);
}
示例#23
0
文件: user.c 项目: malikcjm/mc-nt
/* FIXME: recode this routine on version 3.0, it could be cleaner */
void execute_menu_command (char *s)
{
    char *commands;
    FILE *cmd_file;
    int  cmd_file_fd;
    int  expand_prefix_found = 0;
    int parameter_found = 0;
    int do_quote;
    char prompt [80] = "";
    int  col;
    char *file_name = tmpnam (0);

#ifdef OS2_NT
    /* OS/2 and NT requires the command to end in .cmd */
    file_name = copy_strings (file_name, ".cmd", NULL);
    file_name = "temp.bat";  // $$ fixme

    if ((cmd_file_fd = open (file_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL
                                                         | O_TEXT, 0600)) == -1){
    message (1, MSG_ERROR, _(" Can't create temporary command file \n %s "),
         unix_error_string (errno));
    return;
    }
#else
    if ((cmd_file_fd = open (file_name, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600)) == -1){
    message (1, MSG_ERROR, _(" Can't create temporary command file \n %s "),
         unix_error_string (errno));
    return;
    }
#endif
    cmd_file = fdopen (cmd_file_fd, "w");
    commands = strchr (s, '\n');
    if (!commands){
    fclose (cmd_file);
    unlink (file_name);
    return;
    }
    commands++;

    for (col = 0; *commands; commands++){
    if (col == 0 && (*commands != ' ' && *commands != '\t'))
        break;
        else if (col == 0)
        while (*commands == ' ' || *commands == '\t')
            commands++;
    col++;
    if (*commands == '\n')
        col = 0;
    if (parameter_found){
        if (*commands == '}'){
        char *parameter;
        char *tmp;
        parameter_found = 0;
        parameter = input_dialog (_(" Parameter "), prompt, "");
        if (!parameter || !*parameter){
            /* User canceled */
            fclose (cmd_file);
            unlink (file_name);
            return;
        }
        if (do_quote) {
                fputs (tmp = name_quote (parameter, 0), cmd_file);
            free (tmp);
        } else
            fputs (parameter, cmd_file);
        free (parameter);
        } else {
        int len = strlen (prompt);

        if (len+1 < sizeof (prompt)){
            prompt [len] = *commands;
            prompt [len+1] = 0;
        } else
            prompt [sizeof (prompt)-1] = 0;
        }
    } else if (expand_prefix_found){
        expand_prefix_found = 0;
        if (isdigit (*commands)) {
        do_quote = atoi (commands);
        for ( ; isdigit (*commands); commands++)
            ;
        }
        if (*commands == '{')
        parameter_found = 1;
        else{
        char *text = expand_format (*commands, do_quote);
        fputs (text, cmd_file);
        free (text);
        }
    } else {
        if (*commands == '%') {
        do_quote = 1; /* Default: Quote expanded macro */
        expand_prefix_found = 1;
        } else
        fputc (*commands, cmd_file);
    }
    }
    fclose (cmd_file);
    chmod (file_name, S_IRWXU);
    execute (file_name);
    unlink (file_name);
}