Пример #1
0
void recv_request(int newfd)
{
	char buf[2048];
	int ret;
	while(1)
	{
		bzero(buf,sizeof(buf));
		ret=recv(newfd,buf,sizeof(buf),0);
		if(ret==0)
		{
			break;
		}
		if(strncmp("cd",buf,2)==0)
		{
			do_cd(newfd,buf);
	}else if(strncmp("ls",buf,2)==0)
	{
		do_ls(newfd,buf);
	}else if(strncmp("puts",buf,4)==0)
	{
		do_puts(newfd);
	}else if(strncmp("gets",buf,4)==0)
	{
		do_gets(newfd,buf);
	}else if(strncmp("remove",buf,6)==0)
	{
		do_remove(newfd,buf);
	}else if(strncmp("pwd",buf,3)==0)
	{
		do_pwd(newfd,buf);
	}else{
		continue;
	}
	}
}
Пример #2
0
static void
tree_chdir_sel (WTree * tree)
{
    if (tree->is_panel)
    {
        change_panel ();

        if (do_cd (tree->selected_ptr->name, cd_exact))
            select_item (current_panel);
        else
            message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
                     vfs_path_as_str (tree->selected_ptr->name), unix_error_string (errno));

        widget_redraw (WIDGET (current_panel));
        change_panel ();
        show_tree (tree);
    }
    else
    {
        WDialog *h = WIDGET (tree)->owner;

        h->ret_value = B_ENTER;
        dlg_stop (h);
    }
}
Пример #3
0
/**
 * Run viewer (internal or external) on the currently selected file.
 * If normal is TRUE, force internal viewer and raw mode (used for F13).
 */
static void
do_view_cmd (gboolean normal)
{
    /* Directories are viewed by changing to them */
    if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
    {
        vfs_path_t *fname_vpath;

        if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
        {
            if (query_dialog
                (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL, 2,
                 _("&Yes"), _("&No")) != 0)
            {
                return;
            }
        }
        fname_vpath = vfs_path_from_str (selection (current_panel)->fname);
        if (!do_cd (fname_vpath, cd_exact))
            message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
        vfs_path_free (fname_vpath);
    }
    else
    {
        int file_idx;
        vfs_path_t *filename_vpath;

        file_idx = current_panel->selected;
        filename_vpath = vfs_path_from_str (current_panel->dir.list[file_idx].fname);
        view_file (filename_vpath, normal, use_internal_view != 0);
        vfs_path_free (filename_vpath);
    }

    repaint_screen ();
}
Пример #4
0
/*
 * Run viewer (internal or external) on the currently selected file.
 * If normal is 1, force internal viewer and raw mode (used for F13).
 */
static void
do_view_cmd (int normal)
{
    int dir, file_idx;

    /* Directories are viewed by changing to them */
    if (S_ISDIR (selection (current_panel)->st.st_mode)
	|| link_isdir (selection (current_panel))) {
	if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
	    if (query_dialog
		(_(" Confirmation "), _("Files tagged, want to cd?"), 0, 2,
		 _("&Yes"), _("&No")) != 0) {
		return;
	    }
	}
	if (!do_cd (selection (current_panel)->fname, cd_exact))
	    message (1, MSG_ERROR, _("Cannot change directory"));

	return;

    }

    file_idx = current_panel->selected;
    while (1) {
	char *filename;

	filename = current_panel->dir.list[file_idx].fname;

	dir = view_file (filename, normal, use_internal_view);
	if (dir == 0)
	    break;
	file_idx = scan_for_file (current_panel, file_idx, dir);
    }
}
Пример #5
0
int main()
{
	char* cmd = malloc(256*sizeof(char));
	char* cmd_arg[10];
	char* path = "user";
	int cmdlen,i,j,tag;
	if(chdir("/home/user")<0)
	{
		perror("chdir err");
		return -1;
	}
	do
	{
		memset(cmd,0,256);
		
		printf("-=Mini Shell %s=-$",path);
			
		fgets(cmd,256,stdin);
		cmdlen = strlen(cmd);
		cmdlen--;
		cmd[cmdlen] = '\0';
		i=0;j=0;tag =0;
		memset(cmd_arg,0,sizeof(cmd_arg));
		while(i<cmdlen && j <10)
		{
			if(cmd[i] == ' ')
			{
				cmd[i] = '\0';
				tag =0;
			}
			else
			{
				if(tag == 0)
					cmd_arg[j++] = cmd+i;
				tag =1;
			}
			i++;
		}
		if(j>=10 && i< cmdlen)
		{
			printf("Too Many Arguments");
			continue;
		}
		/// quit command
		if(strcmp(cmd_arg[0],"quit") == 0)
			break;
		if(strcmp(cmd_arg[0],"cd") == 0)
		{
			path = do_cd(cmd_arg);
			continue;
		}
		// new execute
		new_exec(cmd_arg);
		
	}while(1);
	
}
Пример #6
0
static void
do_possible_cd (const vfs_path_t * new_dir_vpath)
{
    if (!do_cd (new_dir_vpath, cd_exact))
        message (D_ERROR, _("Warning"),
                 _("The Commander can't change to the directory that\n"
                   "the subshell claims you are in. Perhaps you have\n"
                   "deleted your working directory, or given yourself\n"
                   "extra access permissions with the \"su\" command?"));
}
Пример #7
0
void reselect_vfs (void)
{
    char *target;

    target = hotlist_cmd (LIST_VFSLIST);
    if (!target)
	return;

    if (!do_cd (target, cd_exact))
        message (1, MSG_ERROR, _("Cannot change directory") );
    g_free (target);
}
Пример #8
0
int
main(void)
{
    int cmd, ret;

    vfs_init(&vfs);

    // Set up filesystem
    create_dir(&vfs, "/etc");
    crond = create_dir(&vfs, "/etc/crond");
    create_dir(&vfs, "/home");
    pwd = create_dir(&vfs, "/home/user");
    pwd->owner = USER_UID;

    while (1) {
        // Simulate a period cron job

        do_cron();

        if (read_all(STDIN, &cmd, sizeof(cmd)) != sizeof(cmd))
            continue;

        if (cmd == -1)
            break;

        switch (cmd) {
        case CD:
            ret = do_cd();
            break;
        case READ:
            ret = do_read();
            break;
        case WRITE:
            ret = do_write();
            break;
        case LN:
            ret = do_ln();
            break;
        case RM:
            ret = do_rm();
            break;
        default:
            continue;
        }

        write_all(STDOUT, &ret, sizeof(ret));
    }

    vfs_destroy(&vfs);

    return 0;
}
Пример #9
0
void
vfs_list (void)
{
    char *target;

    target = hotlist_show (LIST_VFSLIST);
    if (!target)
        return;

    if (!do_cd (target, cd_exact))
        message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
    g_free (target);
}
Пример #10
0
void		builtin_cd(t_env *c)
{
	char		*pwd;
	const char	error[] = "cd: Too many arguments.\n";

	if (c->ac > 2)
		write(1, error, sizeof(error) - 1);
	else if (c->ac == 1)
	{
		if ((pwd = get_env_value(c->my_env, "HOME")))
			do_cd(c, pwd + 5);
	}
	else if (ft_strcmp(c->av[1], "-") == 0)
	{
		if (!(pwd = get_env_value(c->my_env, "OLDPWD")))
			write(1, ": No such file or directory.\n", 29);
		else
			do_cd(c, pwd + 7);
	}
	else
		do_cd(c, c->av[1]);
}
Пример #11
0
void	do_builtins(t_lst *list, char **cmd) //, char **paths)
{
	t_lst	*new_list;

	new_list = NULL;
	if (check_is_builtin(cmd[0]) == 1)
		do_cd(list, cmd);
	if (check_is_builtin(cmd[0]) == 2)
		do_setenv(list, cmd, 1);
	else if (check_is_builtin(cmd[0]) == 3)
		do_unsetenv(list, cmd);
	else if (check_is_builtin(cmd[0]) == 4)
		do_env(list, cmd); //, paths);
}
Пример #12
0
void quick_chdir_cmd (void)
{
    char *target;

    target = hotlist_cmd (LIST_HOTLIST);
    if (!target)
	return;

    if (get_current_type () == view_tree)
	tree_chdir (the_tree, target);
    else
        if (!do_cd (target, cd_exact))
	    message (1, MSG_ERROR, _("Cannot change directory") );
    g_free (target);
}
Пример #13
0
void
vfs_list (void)
{
    char *target;
    vfs_path_t *target_vpath;

    target = hotlist_show (LIST_VFSLIST);
    if (!target)
        return;

    target_vpath = vfs_path_from_str (target);
    if (!do_cd (target_vpath, cd_exact))
        message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
    vfs_path_free (target_vpath);
    g_free (target);
}
Пример #14
0
void client_handle(psession_t ps)
{

	socket_t fd_client = ps -> sess_sfd ;
	int cmd_len = 0 ;
	int recv_ret ;
	while(1 )
	{
		bzero(ps -> sess_buf, BUF_SIZE);
		recv_ret = recv(fd_client, &cmd_len, sizeof(int),0);
		if(cmd_len == 0 || recv_ret == 0)
		{
			printf("client exit !\n");
			close(ps ->sess_sfd);
			free(ps);
			exit(1);
		}
		recvn(fd_client, ps->sess_buf, cmd_len);
		if(strncmp("cd", ps ->sess_buf, 2) == 0)
		{
			do_cd(ps);
		}else if(strncmp("ls", ps ->sess_buf, 2) == 0)
		{
			do_ls(ps);
		}else if( strncmp("puts", ps ->sess_buf, 4)== 0)
		{
			do_puts(ps);
		}else if( strncmp("gets", ps ->sess_buf, 4)== 0)
		{
			do_gets(ps);

		}else if( strncmp("remove", ps ->sess_buf, 6)== 0)
		{
			do_remove(ps);

		}else if(strncmp("pwd", ps ->sess_buf, 3) == 0) 
		{
			do_pwd(ps);

		}else 
		{
			continue ;
		}


	}
}
Пример #15
0
static void
chdir_sel (WTree *tree)
{
    if (!tree->is_panel) {
	return;
    }
    change_panel ();
    if (do_cd (tree->selected_ptr->name, cd_exact)) {
	select_item (current_panel);
    } else {
	message (1, MSG_ERROR, _(" Cannot chdir to \"%s\" \n %s "),
		 tree->selected_ptr->name, unix_error_string (errno));
    }
    change_panel ();
    show_tree (tree);
    return;
}
Пример #16
0
void runbuildin(BuildInType command)
{
	switch(command)
	{
		case CD:
			do_cd();
			break;
		case PWD:
			do_pwd();
			break;
		case EXIT:
			do_exit();
			break;
		case HISTORY:
			do_history();
			break;
		case NO:/* nothing */
		default:break;	
	}
}
Пример #17
0
Файл: tree.c Проект: ryanlee/mc
static void
tree_chdir_sel (WTree * tree)
{
    char *tmp_path;

    if (!tree->is_panel)
        return;

    change_panel ();

    tmp_path = vfs_path_to_str (tree->selected_ptr->name);
    if (do_cd (tree->selected_ptr->name, cd_exact))
        select_item (current_panel);
    else
        message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
                 tmp_path, unix_error_string (errno));

    g_free (tmp_path);
    change_panel ();
    show_tree (tree);
}
Пример #18
0
Файл: command.c Проект: V07D/mc
/* CDPATH handling */
static gboolean
handle_cdpath (const char *path)
{
    gboolean result = FALSE;

    /* CDPATH handling */
    if (*path != PATH_SEP)
    {
        char *cdpath, *p;
        char c;

        cdpath = g_strdup (getenv ("CDPATH"));
        p = cdpath;
        c = (p == NULL) ? '\0' : ':';

        while (!result && c == ':')
        {
            char *s;

            s = strchr (p, ':');
            if (s == NULL)
                s = strchr (p, '\0');
            c = *s;
            *s = '\0';
            if (*p != '\0')
            {
                vfs_path_t *r_vpath;

                r_vpath = vfs_path_build_filename (p, path, NULL);
                result = do_cd (r_vpath, cd_parse_command);
                vfs_path_free (r_vpath);
            }
            *s = c;
            p = s + 1;
        }
        g_free (cdpath);
    }

    return result;
}
Пример #19
0
Файл: ext.c Проект: inso/mc
static void
exec_extension_cd (void)
{
    char *q;
    vfs_path_t *p_vpath;

    *pbuffer = '\0';
    pbuffer = 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 = pbuffer + strlen (pbuffer) - 1;
    while (q >= pbuffer && (*q == ' ' || *q == '\t'))
        q--;
    q[1] = 0;

    p_vpath = vfs_path_from_str_flags (pbuffer, VPF_NO_CANON);
    do_cd (p_vpath, cd_parse_command);
    vfs_path_free (p_vpath);
}
Пример #20
0
int main(int argc, char * argv[]){
	char buf[MAXLINE];
	pid_t pid;
	int status;
	char * args[64];
	
    show();
	setup();

	while(fgets(buf,MAXLINE,stdin) != NULL) {
        if(buf[strlen(buf) - 1]='\n'){
            buf[strlen(buf) - 1]=0;
        }
        getcmd(buf, args);
        
//        printf("%s\n", *args);
//        printf("%s\n", *(args+1));  // need to:  ls -al, then can ture!
//        printf("%x\n", args);
        
        if(!strcmp(*args, "exit")){
            exit(EXIT_SUCCESS);
        }else if(!strcmp(*args, "cd")){
            do_cd(*(args+1));
            show();
            continue;
        }
        
        if((pid = fork())<0){
			printf("fork error");
		}else if(pid == 0){
			execvp(*args,args);
			printf("couldn't execute: %s\n",buf);
            show();
        }
	   	if(pid = waitpid(pid, &status, 0))
          show();
	}
	exit(0);
}
Пример #21
0
static int	do_arg(t_lst *node, char *arg)
{
	int		i;
	char	**cmd;

	i = 0;
	cmd = ft_strsplit_ws(arg);
	if (!*cmd)
		return (-1);
	if (ft_strcmp(*cmd, "exit") == 0)
		i = do_exit(arg);
	else if (ft_strcmp(*cmd, "env") == 0)
		i = deal_with_env(node, arg);
	else if (ft_strcmp(*cmd, "setenv") == 0)
		i = do_setenv(node, arg);
	else if (ft_strcmp(*cmd, "unsetenv") == 0)
		i = do_unsetenv(node, arg);
	else if (ft_strcmp(*cmd, "cd") == 0)
		i = do_cd(node, arg);
	else if (*cmd)
		i = deal_with_command(node, cmd);
	ft_strdel(cmd);
	return (i);
}
Пример #22
0
int main(int argc, char** argv) {
    ssize_t cmd_size;
    int i;
    int pipefd[2], fin, fout;
    char buf[BUFFER_SIZE];
    char *cmd;
    char **args;
    job *j;
    job_list = init_list();
    msg_q = init_list();
    pid_t pgid;

    /* initial setup */
    shell_pgid = getpgid(0);
    sync_pwd();

    /* disable (ignore) job control signals */
    signal(SIGTTOU, SIG_IGN);
    signal(SIGTTIN, SIG_IGN);
    signal(SIGTERM, SIG_IGN);
    signal(SIGTSTP, SIG_IGN);
    signal(SIGINT,  SIG_IGN);

    set_handler();

    /*** Shell loop ***/
    for(;;) {

        /* Print out queued messages */
        for(i=0;i<msg_q->size;i++) {
            message *m = (message *) get(msg_q, i);
            printf("%s: %s\n", m->status ? "Finished" : "Stopped", m->msg);
        }

        // clear the queue
        while(msg_q->size > 0)
            free(del(msg_q,0));

        /* TODO: shell print macro */
        printf("%s %s %s ", SHELL, pwd, PROMPT);
        fflush(stdout);

        do 
            cmd_size = read(STDIN_FILENO, buf, BUFFER_SIZE);
        while(cmd_size == -1 && errno == EINTR); // ignore system call interrupts

        if(cmd_size == -1) {
            perror("read");
            continue;
        } else if(cmd_size == 0) { // EOF (quit)
            write(STDOUT_FILENO, EXIT_MSG, STRING_SIZE(EXIT_MSG));
            cleanup();
            _exit(EXIT_SUCCESS);
        } else if(cmd_size == 1 && buf[0] == '\n') {
            continue;
        }

        if(buf[cmd_size-1] != '\n') { // overflow 
            write(STDOUT_FILENO, OFLOW_MSG, STRING_SIZE(OFLOW_MSG));
            pflush();
            continue;
        }

        buf[cmd_size-1] = '\0'; // strip the newline

        j = parse(buf);
        if(j == (job *) NULL) {
            printf("Invalid redirections you moron!\n");
            continue;
        } 

        args = j->cmds[0];
        if (!my_strcmp(args[0], "fg")) {
            do_fg(args);
            free_job(j);
            continue;
        } else if (!my_strcmp(args[0], "bg")) {
            do_bg(args);
            free_job(j);
            continue;
        } else if (!my_strcmp(args[0], "jobs")) {
            print_bg(job_list);
            free_job(j);
            continue;
        } else if (!my_strcmp(args[0], "cd")) {
            do_cd(args);
            free_job(j);
            continue;
        }

        j->job_id = gen_job_id(job_list);
        j->running = 1;
        j->complete = 0;
        push(job_list, JOB, (void *)j);
    
        pgid = 0; // set the job pgid to be the first child's pid

        fin = STDIN_FILENO;
        for(i=0;i<(j->numcmds);i++) {
            args = j->cmds[i];
            cmd = args[0];

            if(i + 1 < (j->numcmds)) { // not last process in job
                //printf("Creating a pipe!\n");
                if(pipe(pipefd) == -1) {
                    perror("pipe");
                    exit(EXIT_FAILURE);
                }
                fout = pipefd[1];
            }
            else
                fout = STDOUT_FILENO;

            /*printf("Forking %s\n", cmd);
            k = 0;
            while( args[k] != (char *) NULL ) {
                printf("\targv[%d] = %s\n", k, args[k]);
                ++k;
            }*/

            pid = fork();
            if(pid == -1) {
                perror("fork");
                continue;
            }

            /*** CHILD ***/
            if(!pid) {

                // <
                if(j->fin[i] != (char *) NULL) {
                    if( (fin = open(j->fin[i], O_RDONLY)) == -1 ) {
                        perror("open");
                    }
                }
                // >
                if(j->fout[i] != (char *) NULL) {
                    if( (fout = open(j->fout[i], O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1 ) {
                        perror("open");
                    }
                }

                exec_process(cmd, args, pgid, fin, fout, j->fg); 
            }
            
            /*** PARENT ***/
            else {
                if(!pgid) { // set to first child's pid (process group leader)
                    pgid = pid;
                    j->pgid = pgid;
                    //printf("Set job's pgid to %d\n", pgid);
                }

                process *proc = malloc(sizeof(process));
                proc->complete = 0;
                proc->pid = pid;
                proc->pgid = pgid;
                push(j->pid_list, PROCESS, (void *)proc);
                
                if( setpgid(pid, pgid) == -1 ) {
                    perror("setpgid");
                    _exit(EXIT_FAILURE);
                }

            }

            if(fin != STDIN_FILENO)
                close(fin);
            if(fout != STDOUT_FILENO)
                close(fout);

            fin = pipefd[0];
        }

        //print_jobs();

        if(j->fg) {  // foreground

            // give terminal control to job
            if( tcsetpgrp(STDIN_FILENO, pgid) == -1 ) {
                perror("tcsetpgrp");
                _exit(EXIT_FAILURE);
            }

            // wait for job to finish
            jwait(j);

            // give pmsh terminal control again
            if( tcsetpgrp(STDIN_FILENO, shell_pgid) == -1 ) {
                perror("tcsetpgrp");
                _exit(EXIT_FAILURE);
            }
        }
        else  {      // background
            printf("Running: %s\n", j->rawcmd);
        }
    }
}
Пример #23
0
int main(int argc, char * const argv[])
{
	int r, c, long_optind = 0, err = 0;
	char *line;
	int cargc;
	char *cargv[260];
	sc_context_param_t ctx_param;
	int lcycle = SC_CARDCTRL_LIFECYCLE_ADMIN;

	printf("OpenSC Explorer version %s\n", sc_get_version());

	while (1) {
		c = getopt_long(argc, argv, "r:c:vwm:", options, &long_optind);
		if (c == -1)
			break;
		if (c == '?')
			util_print_usage_and_die(app_name, options, option_help);
		switch (c) {
		case 'r':
			opt_reader = optarg;
			break;
		case 'c':
			opt_driver = optarg;
			break;
		case 'w':
			opt_wait = 1;
			break;
		case 'v':
			verbose++;
			break;
		case 'm':
			opt_startfile = optarg;
			break;
		}
	}

	memset(&ctx_param, 0, sizeof(ctx_param));
	ctx_param.ver      = 0;
	ctx_param.app_name = app_name;

	r = sc_context_create(&ctx, &ctx_param);
	if (r) {
		fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r));
		return 1;
	}

	if (verbose > 1) {
		ctx->debug = verbose;
		ctx->debug_file = stderr;
        }

	if (opt_driver != NULL) {
		err = sc_set_card_driver(ctx, opt_driver);
		if (err) {
			fprintf(stderr, "Driver '%s' not found!\n", opt_driver);
			err = 1;
			goto end;
		}
	}

	err = util_connect_card(ctx, &card, opt_reader, opt_wait, 0);
	if (err)
		goto end;

	if (opt_startfile) {
		if(*opt_startfile) {
			char startpath[1024];
			char *args[] = { startpath };

			strncpy(startpath, opt_startfile, sizeof(startpath)-1);
			r = do_cd(1, args);
			if (r) {
				printf("unable to select file %s: %s\n",
					opt_startfile, sc_strerror(r));
				return -1;
			}
		}
	} else {
		sc_format_path("3F00", &current_path);
		r = sc_select_file(card, &current_path, &current_file);
		if (r) {
			printf("unable to select MF: %s\n", sc_strerror(r));
			return 1;
		}
	}

	r = sc_card_ctl(card, SC_CARDCTL_LIFECYCLE_SET, &lcycle);
	if (r && r != SC_ERROR_NOT_SUPPORTED)
		printf("unable to change lifecycle: %s\n", sc_strerror(r));

	while (1) {
		struct command *cmd;
		char prompt[3*SC_MAX_PATH_STRING_SIZE];

		sprintf(prompt, "OpenSC [%s]> ", path_to_filename(&current_path, '/'));
		line = my_readline(prompt);
		if (line == NULL)
			break;
		cargc = parse_line(line, cargv, DIM(cargv));
		if (cargc < 1)
			continue;
		for (r=cargc; r < (int)DIM(cargv); r++)
			cargv[r] = "";
		cmd = ambiguous_match(cmds, cargv[0]);
		if (cmd == NULL) {
			do_help(0, NULL);
		} else {
			cmd->func(cargc-1, cargv+1);
		}
	}
end:
	die(err);

	return 0; /* not reached */
}
int
main (int argc, char **argv)
{
	int exit = 0;
	char *buffer = g_new (char, 1024) ;
	GIOChannel *ioc;
	guint watch_id = 0;
	GOptionContext *ctx = NULL;
	GError *error = NULL;

	/* default to interactive on a terminal */
	interactive = isatty (0);

	ctx = g_option_context_new("test-vfs");
	g_option_context_add_main_entries(ctx, options, NULL);

	if (!g_option_context_parse(ctx, &argc, &argv, &error)) {
		g_printerr("main: %s\n", error->message);

		g_error_free(error);
		g_option_context_free(ctx);
		return 1;
	}

	g_option_context_free(ctx);

	files = g_hash_table_new (g_str_hash, g_str_equal);

	if (noninteractive)
		interactive = FALSE;

	if (interactive)
		vfserr = stderr;
	else
		vfserr = stdout;

	if (!mate_vfs_init ()) {
		fprintf (vfserr, "Cannot initialize mate-vfs.\n");
		return 1;
	}
	mate_vfs_module_callback_push
		(MATE_VFS_MODULE_CALLBACK_AUTHENTICATION,
		 authentication_callback, NULL, NULL);

	if (argc == 1)
		cur_dir = g_get_current_dir ();
	else
		cur_dir = g_strdup(argv[1]);

	if (cur_dir && !G_IS_DIR_SEPARATOR (cur_dir [strlen (cur_dir) - 1]))
		cur_dir = g_strconcat (cur_dir, G_DIR_SEPARATOR_S, NULL);

	if (interactive == TRUE) {
		main_loop = g_main_loop_new (NULL, TRUE);
		ioc = g_io_channel_unix_new (0 /* stdin */);
		g_io_channel_set_encoding (ioc, NULL, NULL);
		g_io_channel_set_buffered (ioc, FALSE);
		watch_id = g_io_add_watch (ioc,
					   G_IO_IN | G_IO_HUP | G_IO_ERR,
					   callback, buffer);
		g_io_channel_unref (ioc);
	}

	while (!exit) {
		char *ptr;

		if (interactive) {
			fprintf (stdout,"\n%s > ", cur_dir);
			fflush (stdout);

			strcpy (buffer, "");
			g_main_loop_run (main_loop);
		} else {
			/* In non-interactive mode we just do this evil
			 * thingie */
			buffer[0] = '\0';
			fgets (buffer, 1023, stdin);
			if (!buffer [0]) {
				exit = 1;
				continue;
			}
		}

		if (!buffer || buffer [0] == '#')
			continue;

		arg_data = g_strsplit (g_strchomp (buffer), delim, -1);
		arg_cur  = 0;
		if ((!arg_data || !arg_data[0]) && interactive) continue;
		if (!interactive)
			printf ("Command : '%s'\n", arg_data [0]);
		ptr = arg_data[arg_cur++];
		if (!ptr)
			continue;

		if (g_ascii_strcasecmp (ptr, "ls") == 0)
			do_ls ();
		else if (g_ascii_strcasecmp (ptr, "cd") == 0)
			do_cd ();
		else if (g_ascii_strcasecmp (ptr, "dump") == 0)
			do_dump ();
		else if (g_ascii_strcasecmp (ptr, "type") == 0 ||
			 g_ascii_strcasecmp (ptr, "cat") == 0)
			do_cat ();
		else if (g_ascii_strcasecmp (ptr, "cp") == 0)
			do_cp ();
		else if (g_ascii_strcasecmp (ptr, "rm") == 0)
			do_rm ();
		else if (g_ascii_strcasecmp (ptr, "mkdir") == 0)
			do_mkdir ();
		else if (g_ascii_strcasecmp (ptr, "rmdir") == 0)
			do_rmdir ();
		else if (g_ascii_strcasecmp (ptr, "mv") == 0)
			do_mv ();
		else if (g_ascii_strcasecmp (ptr, "info") == 0 ||
			 g_ascii_strcasecmp (ptr, "stat") == 0)
			do_info ();
		else if (g_ascii_strcasecmp (ptr, "findtrash") == 0)
			do_findtrash ();
		else if (g_ascii_strcasecmp (ptr, "ssl") == 0)
			do_ssl ();
		else if (g_ascii_strcasecmp (ptr, "sync") == 0)
			fprintf (vfserr, "a shell is like a boat, it lists or syncs (RMS)\n");
		else if (g_ascii_strcasecmp (ptr,"help") == 0 ||
			 g_ascii_strcasecmp (ptr,"?")    == 0 ||
			 g_ascii_strcasecmp (ptr,"info") == 0 ||
			 g_ascii_strcasecmp (ptr,"man")  == 0)
			list_commands ();
		else if (g_ascii_strcasecmp (ptr,"exit") == 0 ||
			 g_ascii_strcasecmp (ptr,"quit") == 0 ||
			 g_ascii_strcasecmp (ptr,"q")    == 0 ||
			 g_ascii_strcasecmp (ptr,"bye") == 0)
			exit = 1;

		/* File ops */
		else if (g_ascii_strcasecmp (ptr, "open") == 0)
			do_open ();
		else if (g_ascii_strcasecmp (ptr, "create") == 0)
			do_create ();
		else if (g_ascii_strcasecmp (ptr, "close") == 0)
			do_close ();
		else if (g_ascii_strcasecmp (ptr, "handleinfo") == 0)
			do_handleinfo ();
		else if (g_ascii_strcasecmp (ptr, "read") == 0)
			do_read ();
		else if (g_ascii_strcasecmp (ptr, "seek") == 0)
			do_seek ();
		
		else
			fprintf (vfserr, "Unknown command '%s'", ptr);

		g_strfreev (arg_data);
		arg_data = NULL;
	}

	if (interactive) {
		g_source_remove (watch_id);
		g_main_loop_unref (main_loop);
		main_loop = NULL;
	}

	g_free (buffer);
	g_free (cur_dir);

	close_files ();

	return 0;
}
Пример #25
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);
    }
}
Пример #26
0
void CommandParser::executeCommand(int cmd){
	switch(cmd){
		case WRI  :  toAbsolute(first_arg);
					 do_wri(first_arg);     break;
		case LIST :  toAbsolute(first_arg);
					 do_list(first_arg);        break;
		case SHOW :  toAbsolute(first_arg);
					 do_show(first_arg);    break;
		case APP  :  toAbsolute(first_arg);
					 do_app(first_arg);        break;
		case CD   :  do_cd();               break;
		case HELP :  if (strlen(first_arg) == 0)
						do_helpd();
					 else
						do_help(first_arg);
					 break;
		case MKFS :  do_mkfs();             break;
		case REN  :  toAbsolute(first_arg);
					 do_ren(first_arg , second_arg);
					 break;
		case FINFO:  toAbsolute(first_arg);
					 do_info(first_arg);
					 break;
		case LINK :  toAbsolute(first_arg);
					 toAbsolute(second_arg);
					 do_link(first_arg , second_arg);
					 break;
		case QUIT :  do_quit();break;
		case CP   :  toAbsolute(first_arg);
					 toAbsolute(second_arg);
					 do_cp(first_arg , second_arg);
					 break;
		case LOAD :  do_load();
					 break;
		case ULOAD:     do_uload();
					 break;
		case RF   :  toAbsolute(first_arg);
					 do_rf(first_arg);
					 break;
		case RMD  :  toAbsolute(first_arg);
					 do_rmd(first_arg);
					 break;
		case MKD  :  toAbsolute(first_arg);
					 do_mkd(first_arg);
					 break;
		case CHOWN : toAbsolute(first_arg);
					 do_chown(first_arg,second_arg);
					 break ;
		case CHPERM : toAbsolute(first_arg);
					  do_chperm(first_arg,second_arg);
					  break;
		case ADDUSER :
					  do_adduser() ;
					  break;
		case CLS     :
					  clrscr();
					  break;
		case REBOOT  :
					 //
					  break;
		default:
					 if ( strlen(command) )
						 vd_puts("Invalid Command\n");
	}
}
Пример #27
0
void runProcess( Proc *proc, pid_t pgid, int fg, int inputFile, int outputFile, int errorFile)
{
    // wrzucenie procesu do grupy procesow i danie przekazanie grupy terminalowu jezeli jest to stosowne
    // utowrzenie grupy lub dolaczenie do juz istniejacej
    pid_t pid = getpid();

    if( pgid == 0 )
        pgid = pid;

    setpgid(pid, pgid);

    // jesli w foreground to oddajemy terminal dla grupy procesow
    if(fg == 1)
    {
        tcsetpgrp( shellTerminal, pgid );
    }

    struct sigaction act;
    act.sa_handler = SIG_DFL;  /* set up signal handler */
    act.sa_flags = 0;

    // kiedy shell przejmuje kontrole, powinien ignorowac ponizsze sygnaly, zeby samemu sie przypadkowo nie killnac
    sigaction(SIGINT, &act, NULL);
    // powrot do ustawien domyslnych (przez nasz shell te sygnaly byly ignorowane)
    sigaction(SIGINT, &act, NULL);
    sigaction(SIGQUIT, &act, NULL);
    sigaction(SIGCHLD, &act, NULL);
    sigaction(SIGTSTP, &act, NULL);
    sigaction(SIGTTIN, &act, NULL);
    sigaction(SIGTTOU, &act, NULL);

    // ustawienie standardowego I/O dla nowego procesu
    // jesli wyjscia sa inne niz standardowe to zamieniamy je - przekierowanie wyjscia
    if(inputFile != STDIN_FILENO)
    {
        dup2(inputFile, STDIN_FILENO);
        close(inputFile);
    }

    if(outputFile != STDOUT_FILENO)
    {
        dup2(outputFile, STDOUT_FILENO);
        close(outputFile);
    }

    if(errorFile != STDERR_FILENO)
    {
        dup2(errorFile, STDERR_FILENO);
        close(errorFile);
    }   if(strcmp(proc->argv[0], "pwd") == 0)
    {
        do_pwd();
    }
    else if(strcmp(proc->argv[0], "cd") == 0)
    {
        do_cd(proc->argv[1]);
    }
    else if(strcmp(proc->argv[0], "ls") == 0)
    {
        do_ls(proc->argv[1]);
    }
    else if(strcmp(proc->argv[0], "mkdir") == 0)
    {
        do_mkdir(proc->argv[1]);
    }
    else if(strcmp(proc->argv[0], "rmdir") == 0)
    {
        do_rmdir(proc->argv[1]);
    }
    else if(strcmp(proc->argv[0], "touch") == 0)
    {
        do_touch(proc->argv[1]);
    }
    else if(strcmp(proc->argv[0], "rm") == 0)
    {
        do_rm(proc->argv[1]);
    }
    else if(strcmp(proc->argv[0], "cp") == 0)
    {
        do_cp(proc->argv[1], proc->argv[2]);
    }
    else if(strcmp(proc->argv[0], "echo") == 0)
    {
        do_echo(proc->argv[1]);
    }
	else 
		run(proc->argv[0], proc->argv);
    //execvp(proc->argv[0], proc->argv);
    // nie powinien wykonac exit'a przy poprawnym wykonaniu
    //exit(-1);
}
Пример #28
0
Файл: command.c Проект: V07D/mc
void
do_cd_command (char *orig_cmd)
{
    int len;
    int operand_pos = CD_OPERAND_OFFSET;
    const char *cmd;

    /* Any final whitespace should be removed here
       (to see why, try "cd fred "). */
    /* NOTE: I think we should not remove the extra space,
       that way, we can cd into hidden directories */
    /* FIXME: what about interpreting quoted strings like the shell.
       so one could type "cd <tab> M-a <enter>" and it would work. */
    len = strlen (orig_cmd) - 1;
    while (len >= 0 && (orig_cmd[len] == ' ' || orig_cmd[len] == '\t' || orig_cmd[len] == '\n'))
    {
        orig_cmd[len] = 0;
        len--;
    }

    cmd = orig_cmd;
    if (cmd[CD_OPERAND_OFFSET - 1] == 0)
        cmd = "cd ";            /* 0..2 => given text, 3 => \0 */

    /* allow any amount of white space in front of the path operand */
    while (cmd[operand_pos] == ' ' || cmd[operand_pos] == '\t')
        operand_pos++;

    if (get_current_type () == view_tree)
    {
        if (cmd[0] == 0)
        {
            sync_tree (mc_config_get_home_dir ());
        }
        else if (DIR_IS_DOTDOT (cmd + operand_pos))
        {
            if (vfs_path_elements_count (current_panel->cwd_vpath) != 1 ||
                strlen (vfs_path_get_by_index (current_panel->cwd_vpath, 0)->path) > 1)
            {
                vfs_path_t *tmp_vpath = current_panel->cwd_vpath;

                current_panel->cwd_vpath =
                    vfs_path_vtokens_get (tmp_vpath, 0, vfs_path_tokens_count (tmp_vpath) - 1);
                vfs_path_free (tmp_vpath);
            }
            sync_tree (vfs_path_as_str (current_panel->cwd_vpath));
        }
        else if (cmd[operand_pos] == PATH_SEP)
        {
            sync_tree (cmd + operand_pos);
        }
        else
        {
            vfs_path_t *new_vpath;

            new_vpath = vfs_path_append_new (current_panel->cwd_vpath, cmd + operand_pos, NULL);
            sync_tree (vfs_path_as_str (new_vpath));
            vfs_path_free (new_vpath);
        }
    }
    else
    {
        char *path;
        vfs_path_t *q_vpath;
        gboolean ok;

        path = examine_cd (&cmd[operand_pos]);

        if (*path == '\0')
            q_vpath = vfs_path_from_str (mc_config_get_home_dir ());
        else
            q_vpath = vfs_path_from_str_flags (path, VPF_NO_CANON);

        ok = do_cd (q_vpath, cd_parse_command);
        if (!ok)
            ok = handle_cdpath (path);

        if (!ok)
        {
            char *d;

            d = vfs_path_to_str_flags (q_vpath, 0, VPF_STRIP_PASSWORD);
            message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"), d,
                     unix_error_string (errno));
            g_free (d);
        }

        vfs_path_free (q_vpath);
        g_free (path);
    }
}
Пример #29
0
int
shell_do_cmd(struct cmd_def **defs,
             int argc,
             char **argv)
{
  struct cmd_def *def, *tmp;
  int ret, len, found;
  char *p;
  int i;

  if (argc == 0)
    return SHELL_CONT;

  if ('!' == argv[0][0])
    {
      pid_t pid;

      pid = fork();
      if (-1 == pid)
        {
          perror("fork");
          return SHELL_CONT;
        }
      if (0 == pid)
        {
          argv[0]++;

          ret = execvp(argv[0], argv);
          if (-1 == ret)
            {
              perror("execvp");
              exit(1);
            }
        }
      else
        {
        retry:
          ret = wait(0);
          if (-1 == ret)
            {
              if (EINTR == errno)
                goto retry;
              else
                {
                  perror("wait");
                  exit(1);
                }
            }
        }
      return SHELL_CONT;
    }
  else if (NULL != (p = index(argv[0], '=')))
    {
      *p++ = 0;
      var_set(argv[0], p, VAR_CMD_SET, NULL);
      return SHELL_CONT;
    }
  else if (argc == 1 && (NULL != (p = index(argv[0], ':'))))
    {
      (void) do_cd(argv[0]);
      return SHELL_CONT;
    }
  len = strlen(argv[0]);

  def = NULL;
  found = 0;
  for (i = 0;defs[i];i++)
    {
      tmp = defs[i];
      //printf("'%s' '%s'\n", argv[0], tmp->name);
      if (!strcmp(argv[0], tmp->name))
        {
          found = 1;
          def = tmp;
          break ;
        }
      else if (!strncmp(argv[0], tmp->name, len))
	{
          if (1 == found)
            {
              fprintf(stderr, "ambiguous command: %s\n", argv[0]);
              return SHELL_CONT;
            }
          found = 1;
          def = tmp;
	}
    }

  if (0 == found)
    {
      fprintf(stderr, "cmd %s: not found\n", argv[0]);
      return SHELL_CONT;
    }

  ret = def->func(argc, argv);
  return ret;
}
Пример #30
0
int main(int argc,char *argv[])
{
    char cline[COMMAND_LINE];
    struct command_line command;
    int sock_fd;
    struct sockaddr_in sin;
    printf("myftp$: ");
    fflush(stdout);

    while (fgets(cline,MAX_LINE,stdin) != NULL)
    {
        if (cline[0] == '\n')
        {
            printf("myftp$: ");
            continue;
        }

        if (split(&command,cline) == -1)
        {
            exit(1);
        }

        if (strcasecmp(command.name,"get") == 0)
        {
            if (do_get(command.argv[1],command.argv[2],sock_fd) == -1)
            {
                exit(1);
            }
        } else if (strcasecmp(command.name,"put") == 0)
        {
            if (do_put(command.argv[1],command.argv[2],sock_fd) == -1)
            {
                exit(1);
            }
        } else if (strcasecmp(command.name,"cd") == 0)
        {
            if (do_cd(command.argv[1]) == -1)
            {
                exit(1);
            }
        } else if (strcasecmp(command.name,"!cd") == 0)
        {
            if (do_serv_cd(command.argv[1],sock_fd) == -1)
            {
                exit(1);
            }
        } else if (strcasecmp(command.name,"ls") == 0)
        {
            if (do_ls(command.argv[1]) == -1)
            {
                exit(1);
            }
        } else if (strcasecmp(command.name,"!ls") == 0)
        {
            if (do_serv_ls(command.argv[1],sock_fd) == -1)
            {
                exit(1);
            }
        } else if (strcasecmp(command.name,"connect") == 0)
        {
            if (do_connect(command.argv[1],&sin,&sock_fd) == -1)
            {
                exit(1);
            }
        } else if (strcasecmp(command.name,"bye") == 0)
        {
            if (do_bye(sock_fd) == -1)
            {
                exit(1);
            }
            break;
        } else
        {
            printf("wrong command\n");
            printf("usage: command arg1 arg2 ... argn\n");
        }
        printf("myftp$: ");
        fflush(stdout);
    }

    if (close(sock_fd) == -1)
    {
        perror("fail to close");
        exit(1);
    }

    return 0;
}