Example #1
0
void shell_loop() {
    char user_in[INPUT_BUFF_LEN];
    char **argv;
    int loop = 1;

    /* variables if bg_exec == 1 */
    pid_t cpid;
    int status = 0;

    do {
        flag_io = 0;
        redir_in = 0;
        redir_out = 0;
        redir_in_index = 0;
        redir_out_index = 0;
        append = 0;
        pipe_child_index = 0;
        bg_exec = 0;

        /* variales if batch == 1 */
        
        if(batch) {
            fgets(user_in, INPUT_BUFF_LEN, batchfp);
            if(feof(batchfp))
                break;
        } else /* read from command line interpreter */ {
            fputs(prompt, stdout);
            fgets(user_in, INPUT_BUFF_LEN, stdin);

            if(feof(stdin)) {
                printf("\n");
                break;
            }
        }

        argv = parse_string(user_in);
        evaluate_flags(argv);

        /* finish shell in child process if bg_exec == 1 */
        if(bg_exec) {
            cpid = fork();
            if (cpid == -1) {
                perror("bg_exec fork");
                free_arg(argv);
            }
            if (cpid == 0) /* child */ {
                loop = shell_execute(argv);
                free_arg(argv);
                exit(1);
            }
        }

        else {
            loop = shell_execute(argv);
            free_arg(argv);
        }
    } while(loop);

}
Example #2
0
void shell_loop
    (
    void
    )
{
    // Local variables
    char*   line;
    char**  args;
    int     status;
    char    cwd[SHELL_CWD_BUFFER_SIZE];
    char*   printable_cwd;

    do
    {
        // Display the prompt
        getcwd( cwd, sizeof( cwd ) );
        printable_cwd = strrchr( cwd, '/' );
        if( '/' == printable_cwd[0] )
        {
            printable_cwd++;
        }
        printf( "%s::%s> ", SHELL_NAME, printable_cwd );

        line    = shell_read_line();
        args    = shell_split_line( line );
        status  = shell_execute( args );

        free( line );
        free( args );
    }   while( status );

} /* shell_loop() */
Example #3
0
void shell_parse( char * buf )
{
	int argc = 0;
	char *argv[8] = {};
	int stat = 0;
	
	while ( *buf ) {
		if ( *buf==' ' && stat==0 ) {
			*buf = 0;
		} else if ( *buf!=' ' && stat==0 ) {
			stat = 1;
			argv[argc++] = buf;
		} else if ( *buf==' ' && stat==1 ) {
			stat = 0;
			*buf = 0;
		}
		buf++;
		if ( *buf == '\n' )
			*buf = 0;
	}
	
	argv[argc] = NULL;
	
	shell_execute(argc, argv);
}
Example #4
0
/**
 * Execute a read-evaluate-print loop.
 * 
 * Reads user input, tokenizes it, and attempts
 * to run it as a command.
 */
void shell_repl(void)
{
    int should_exit = 0;

    while (should_exit != 1)
    {
        printf("%s", SHELL_PROMPT);

        char* input = shell_read_input();
        
        // User only entered a newline.
        // If we don't do this, we get
        // a segfault.
        if (input[0] == '\0')
        {
            continue;
        }

        // This needs to come before the call to shell_tokenize_input
        // since shell_tokenize_input modifies the input.
        int background_execute = shell_is_background(input);        

        char** execute_args = shell_tokenize_input(input);

        should_exit = shell_execute(execute_args, background_execute);

        // Free allocated memory. 
        free(input);
        free(execute_args);
    }

    puts("");
}
Example #5
0
int ClientThread::OpenPty(char* ttyName, char* clientIp, char* screenNum) {
	char key[128];
	sprintf(key, "%s.%s", clientIp, screenNum);
	string tty = this->FindTty(key);
	if (tty == "") {
		printf(ERROR_CAN_NOT_FOUND_CFG, clientIp, screenNum);
		return -1;
	}

	sprintf(ttyName, "/dev/%s", tty.c_str());
	ttyName[5] = 't';

	char ptyName[128];
	strcpy(ptyName, ttyName);
	ptyName[5] = 'p';

	char cmd[128];
	sprintf(cmd, "fuser -k %s %s", ttyName, ptyName);
	shell_execute(cmd);

	struct stat stb;
	if (stat(ptyName, &stb) < 0) {
		return -1;
	}
	int p = open(ptyName, O_RDWR | O_NOCTTY);
	if (p >= 0) {
		return p;
	}
	return -1;
}
Example #6
0
int main(int argc, char **argv) {
  shell_register_signals(); /* Hook SIGINT (namely ctrl-c) */

  while(true) {
    char *line = shell_getline("%u@dtsh: %d$ ", line);
    shell_execute(line);
    free(line);
  }
}
Example #7
0
/* this is an implimentation of piping output of a command as input of
   another command.

       output: arguments argv[0] through argv[pipe_child_index - 1]
       input:  arguments argv[pipe_child_index] through argv[NULL] */
int invoke_with_pipe(char **argv) {
    pid_t childpid1;
    pid_t childpid2;
    int pipefd[2];
    int status_pipe1;
    int status_pipe2;
    pipe(pipefd);

    childpid1 = fork();

    if(childpid1 == -1) {
        perror("fork1");
        return 1;
    }

    if(childpid1 == 0) {
        dup2(pipefd[1],1);
        close(pipefd[0]);
        shell_execute(argv);
        exit(1);
    }

    childpid2 = fork();

    if(childpid2 == -1) {
        perror("fork2");
        return 1;
    }

    if(childpid2 == 0) {
        dup2(pipefd[0], 0);
        close(pipefd[1]);
        shell_execute(argv+pipe_child_index);
        exit(1);
    }

    close(pipefd[0]);
    close(pipefd[1]);

    waitpid(childpid1, &status_pipe1, WUNTRACED);
    waitpid(childpid2, &status_pipe2, WUNTRACED);

    return 1;
}
Example #8
0
/**
* Main and Execute Commands
*/
int main(int argc, char* argv[])
{
    history_count = 0;
    char input_buffer[COMMAND_LENGTH];
    char *tokens[NUM_TOKENS];
    char wd[1024], prompt[1024];

    strcpy(prompt, "> ");

    while (true) {

        // Get command
        // Use write because we need to use read()/write() to work with
        // signals, and they are incompatible with printf().
        write(STDOUT_FILENO, prompt, strlen(prompt));
        _Bool in_background = false;
        read_command(input_buffer, tokens, &in_background);

        if (tokens[0] == NULL)
            continue;

        if (strcmp(tokens[0], "exit") == 0)
            break;
        else if (strcmp(tokens[0], "cd") == 0) {
            if (tokens[1] == NULL) {
                char out[60];
                strcpy(out, "shell: Expected argument for \"cd\", no action taken.\n");
                write(STDOUT_FILENO, out, strlen(out));
            }   
            else if (chdir(tokens[1]) != 0) {
                perror("shell");
            }
            else {
                getcwd(prompt, sizeof(prompt));
                strcat(prompt, "> ");
            }         
        }
        else if (strcmp(tokens[0], "pwd") == 0) {
            char out[30];
            getcwd(wd, sizeof(wd));
            strcpy(out, "shell: Working directory: ");
            write(STDOUT_FILENO, out, strlen(out));
            write(STDOUT_FILENO, strcat(wd, "\n"), strlen(strcat(wd, "\n")));
        }
        else if (strcmp(tokens[0], "history") == 0) {
            shell_print_history();
        }  
        else {
            shell_execute(tokens, in_background);
        }    
    }

    return 0;
} 
Example #9
0
void shell_interactive(shell_context_t *ctx) {
	ctx->shell_exit = 0;

#ifndef WITH_READLINE
	char *line;

	while(!ctx->shell_exit) {
		fputs("jzboot> ", stdout);
		fflush(stdout);

		line = fgets(ctx->linebuf, sizeof(ctx->linebuf), stdin);

		if(line == NULL)
			break;

		shell_execute(ctx, line);
	}
#else
	rl_completion_entry_function = shell_completion;
	completion_context = ctx;

	rl_set_signals();

	rl_filename_quote_characters = "\" ";
	while(!ctx->shell_exit) {
		char *line = readline("jzboot> ");

		if(line == NULL) {
			break;
		}

		add_history(line);

		shell_execute(ctx, line);

		free(line);
	}

	rl_clear_signals();
#endif
}
        inline void execute_(std::string const& file_path, std::string const& filename)
        {
#if defined CL_TRACE_OUTPUT
            std::cout << "file path: " << (file_path + ".plt").c_str() << std::endl;
#endif
            shell_execute(file_path + ".plt");

            // Copy pdf files from output directory
            // perhaps it is in a current directory
            std::string result_pdf_path = fs::current_path().string() + std::string("//") + filename;
            if (fs::exists(result_pdf_path))
            {
                fs::copy_file(result_pdf_path, file_path + ".pdf", fs::copy_option::overwrite_if_exists);
                fs::remove(result_pdf_path);
            }
        }
Example #11
0
void shell_loop(void)
{
  char *line;
  char **args;
  int status;

  char buffer[1024];
  printf("> ");
  fflush(stdout);
  while (fgets(buffer, 1024, stdin) != NULL){ 
    while (status){
    printf("> ");
    line = shell_read_line();
    args = tokenify(line);
    status = shell_execute(args);

    free(line);
    free(args);
  } 
 }
}
Example #12
0
void shell_main()
{
    char line[SHELL_LINE_MAX_LEN + 1] = { '\0' };

    puts(SHELL_PROMPT "/> ");    

    size_t n = getline(line, SHELL_LINE_MAX_LEN+1); 
    line[n-1] = '\0'; // remove '\r'

#ifdef DEBUG_SHELL
    debug_printf("read %u characters from command line\r\n", n);
    debug_printf("\tline = \"%s\"\r\n", line);
#endif

    if (n > 1) {
        char* argv[SHELL_ARGS_MAX_NB] = { NULL };

        uint8_t argc = shell_split_line(line, argv);

        shell_execute(argc, argv);
    }
}
Example #13
0
int shell_source(shell_context_t *ctx, const char *filename) {
	ctx->shell_exit = 0;

	FILE *file = fopen(filename, "r");

	if(file == NULL) {
		return -1;
	}

	char *line;

	while((line = fgets(ctx->linebuf, sizeof(ctx->linebuf), file)) && !ctx->shell_exit) {
		if(shell_execute(ctx, line) == -1) {
			fclose(file);

			return -1;
		}
	}

	fclose(file);

	return 0;
}
Example #14
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);
}
Example #15
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);
    }
}
Example #16
0
File: user.c Project: 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);
}
Example #17
0
File: command.c Project: V07D/mc
static cb_ret_t
enter (WInput * lc_cmdline)
{
    char *cmd = lc_cmdline->buffer;

    if (!command_prompt)
        return MSG_HANDLED;

    /* Any initial whitespace should be removed at this point */
    while (*cmd == ' ' || *cmd == '\t' || *cmd == '\n')
        cmd++;

    if (!*cmd)
        return MSG_HANDLED;

    if (strncmp (cmd, "cd ", 3) == 0 || strcmp (cmd, "cd") == 0)
    {
        do_cd_command (cmd);
        input_clean (lc_cmdline);
        return MSG_HANDLED;
    }
    else if (strcmp (cmd, "exit") == 0)
    {
        input_assign_text (lc_cmdline, "");
        if (!quiet_quit_cmd ())
            return MSG_NOT_HANDLED;
    }
    else
    {
        GString *command;
        size_t i;

        if (!vfs_current_is_local ())
        {
            message (D_ERROR, MSG_ERROR, _("Cannot execute commands on non-local filesystems"));
            return MSG_NOT_HANDLED;
        }
#ifdef ENABLE_SUBSHELL
        /* Check this early before we clean command line
         * (will be checked again by shell_execute) */
        if (mc_global.tty.use_subshell && subshell_state != INACTIVE)
        {
            message (D_ERROR, MSG_ERROR, _("The shell is already running a command"));
            return MSG_NOT_HANDLED;
        }
#endif
        command = g_string_sized_new (32);

        for (i = 0; cmd[i] != '\0'; i++)
        {
            if (cmd[i] != '%')
                g_string_append_c (command, cmd[i]);
            else
            {
                char *s;

                s = expand_format (NULL, cmd[++i], TRUE);
                g_string_append (command, s);
                g_free (s);
            }
        }

        input_clean (lc_cmdline);
        shell_execute (command->str, 0);
        g_string_free (command, TRUE);

#ifdef ENABLE_SUBSHELL
        if ((quit & SUBSHELL_EXIT) != 0)
        {
            if (quiet_quit_cmd ())
                return MSG_HANDLED;

            quit = 0;
            /* restart subshell */
            if (mc_global.tty.use_subshell)
                init_subshell ();
        }

        if (mc_global.tty.use_subshell)
            do_load_prompt ();
#endif
    }
    return MSG_HANDLED;
}
Example #18
0
static void test_filename(void)
{
    char filename[MAX_PATH];
    const filename_tests_t* test;
    char* c;
    int rc;

    test=filename_tests;
    while (test->basename)
    {
        sprintf(filename, test->basename, tmpdir);
        if (strchr(filename, '/'))
        {
            c=filename;
            while (*c)
            {
                if (*c=='\\')
                    *c='/';
                c++;
            }
        }
        rc=shell_execute(test->verb, filename, NULL, NULL);
        if (rc>=32)
            rc=0;
        if ((test->todo & 0x1)==0)
        {
            ok(rc==test->rc, "%s failed: rc=%d err=%ld\n", shell_call,
               rc, GetLastError());
        }
        else todo_wine
        {
            ok(rc==test->rc, "%s failed: rc=%d err=%ld\n", shell_call,
               rc, GetLastError());
        }
        if (rc==0)
        {
            const char* verb;
            if ((test->todo & 0x2)==0)
            {
                okChildInt("argcA", 5);
            }
            else todo_wine
            {
                okChildInt("argcA", 5);
            }
            verb=(test->verb ? test->verb : "Open");
            if ((test->todo & 0x4)==0)
            {
                okChildString("argvA3", verb);
            }
            else todo_wine
            {
                okChildString("argvA3", verb);
            }
            if ((test->todo & 0x8)==0)
            {
                okChildPath("argvA4", filename);
            }
            else todo_wine
            {
                okChildPath("argvA4", filename);
            }
        }
        test++;
    }

    test=noquotes_tests;
    while (test->basename)
    {
        sprintf(filename, test->basename, tmpdir);
        rc=shell_execute(test->verb, filename, NULL, NULL);
        if (rc>=32)
            rc=0;
        if ((test->todo & 0x1)==0)
        {
            ok(rc==test->rc, "%s failed: rc=%d err=%ld\n", shell_call,
               rc, GetLastError());
        }
        else todo_wine
        {
            ok(rc==test->rc, "%s failed: rc=%d err=%ld\n", shell_call,
               rc, GetLastError());
        }
        if (rc==0)
        {
            int count;
            const char* verb;
            char* str;

            verb=(test->verb ? test->verb : "Open");
            if ((test->todo & 0x4)==0)
            {
                okChildString("argvA3", verb);
            }
            else todo_wine
            {
                okChildString("argvA3", verb);
            }

            count=4;
            str=filename;
            while (1)
            {
                char attrib[18];
                char* space;
                space=strchr(str, ' ');
                if (space)
                    *space='\0';
                sprintf(attrib, "argvA%d", count);
                if ((test->todo & 0x8)==0)
                {
                    okChildPath(attrib, str);
                }
                else todo_wine
                {
                    okChildPath(attrib, str);
                }
                count++;
                if (!space)
                    break;
                str=space+1;
            }
            if ((test->todo & 0x2)==0)
            {
                okChildInt("argcA", count);
            }
            else todo_wine
            {
                okChildInt("argcA", count);
            }
        }
        test++;
    }
Example #19
0
File: ext.c Project: inso/mc
static vfs_path_t *
exec_extension (void *target, const vfs_path_t * filename_vpath, const char *lc_data,
                int start_line)
{
    char *shell_string, *export_variables;
    vfs_path_t *script_vpath = NULL;
    int cmd_file_fd;
    FILE *cmd_file;
    char *cmd = NULL;

    g_return_val_if_fail (lc_data != NULL, NULL);

    pbuffer = NULL;
    localmtime = 0;
    quote_func = name_quote;
    run_view = FALSE;
    is_cd = FALSE;
    written_nonspace = FALSE;

    /* Avoid making a local copy if we are doing a cd */
    do_local_copy = !vfs_file_is_local (filename_vpath);

    shell_string = exec_make_shell_string (lc_data, filename_vpath);

    if (shell_string == NULL)
        goto ret;

    if (is_cd)
    {
        exec_extension_cd ();
        g_free (shell_string);
        goto ret;
    }

    /*
     * 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 (&script_vpath, "mcext", SCRIPT_SUFFIX);

    if (cmd_file_fd == -1)
    {
        message (D_ERROR, MSG_ERROR,
                 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
        goto ret;
    }

    cmd_file = fdopen (cmd_file_fd, "w");
    fputs ("#! /bin/sh\n\n", cmd_file);

    export_variables = exec_get_export_variables (filename_vpath);
    if (export_variables != NULL)
    {
        fprintf (cmd_file, "%s\n", export_variables);
        g_free (export_variables);
    }

    fputs (shell_string, cmd_file);
    g_free (shell_string);

    /*
     * 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", vfs_path_as_str (script_vpath));

    fclose (cmd_file);

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

    if (run_view)
    {
        /* If we've written whitespace only, then just load filename into view */
        if (!written_nonspace)
            exec_extension_view (target, NULL, filename_vpath, start_line);
        else
            exec_extension_view (target, cmd, filename_vpath, start_line);
    }
    else
    {
        shell_execute (cmd, EXECUTE_INTERNAL);
        if (mc_global.tty.console_flag != '\0')
        {
            handle_console (CONSOLE_SAVE);
            if (output_lines && mc_global.keybar_visible)
                show_console_contents (output_start_y,
                                       LINES - mc_global.keybar_visible -
                                       output_lines - 1, LINES - mc_global.keybar_visible - 1);
        }
    }

    g_free (cmd);

    exec_cleanup_file_name (filename_vpath, TRUE);
  ret:
    return script_vpath;
}