myshell 2.0.0
Loading...
Searching...
No Matches
msh_exec.cpp File Reference

Execution related utilities. More...

Functions

int msh_exec_script (const char *path)
 Executes a script line by line.
 
int msh_execve (char **argv)
 Executes a command.
 
int msh_exec_simple (simple_command &cmd, int pipe_in=STDIN_FILENO, int pipe_out=STDOUT_FILENO, int flags=0)
 Executes a simple command.
 
int msh_exec_internal (command &cmd, int in, int out, int flags)
 Internal command execution function.
 

Variables

int exec_line_no = 0
 The current line number of the script being executed.
 
std::string exec_path
 The path to the script being executed.
 

Detailed Description

Execution related utilities.

Function Documentation

◆ msh_exec_internal()

int msh_exec_internal ( command & cmd,
int in,
int out,
int flags )

Internal command execution function.

Delegates the execution to the appropriate function based on the command type holded by the cmd.cmd variant object.

Parameters
cmdThe command to execute.
inFile descriptor to use as stdin.
outFile descriptor to use as stdout.
flagsFlags to pass to the command.
Returns
Exit status of the command or the error code if any.

◆ msh_exec_script()

int msh_exec_script ( const char * path)

Executes a script line by line.

Warning
Caller must ensure that the file exists and is readable.

exec_path and exec_line_no are set for error_log().

See also
error_log()
Parameters
pathPath to the script.
Returns
Exit status of the script.
See also
msh_execve

◆ msh_exec_simple()

int msh_exec_simple ( simple_command & cmd,
int pipe_in = STDIN_FILENO,
int pipe_out = STDOUT_FILENO,
int flags = 0 )

Executes a simple command.

Parameters
cmdThe command to execute.
pipe_inFile descriptor to use as stdin.
pipe_outFile descriptor to use as stdout.
flagsFlags to pass to the command.
Returns
Exit status of the command or the error code if any.

If the command is a builtin, it is executed directly. Otherwise, it is executed using msh_execve().

If either pipe_in or pipe_out is not STDIN_FILENO or STDOUT_FILENO respectively, the command is not a builtin, or the command is executed asynchronously, the command will be executed in a forked process.

If either ASYNC or FORK_NO_WAIT is set in flags, the parent process returns immediately. One should take care of the child process by calling wait_for_process() or reap_children() explicitly if needed.

See also
msh_execve

◆ msh_execve()

int msh_execve ( char ** argv)

Executes a command.

Parameters
argvArray of arguments.
Returns
Exit status of the command.

If command contains a slash, it is executed directly, supposing it is a full path to the executable. Otherwise, the command is searched in the PATH environment variable using execvpe(). If execve() fails and errno is ENOEXEC, the command is treated as a script and executed using msh_exec_script().

See also
execve
execvpe
msh_exec_script

Variable Documentation

◆ exec_line_no

int exec_line_no = 0

The current line number of the script being executed.

Undefined if no script is being executed.

See also
msh_exec_script

◆ exec_path

std::string exec_path

The path to the script being executed.

Undefined if no script is being executed.

See also
msh_exec_script