Quash Shell  0.1
A simple yet powerfull shell program
Classes | Macros | Typedefs | Enumerations | Functions
command.h File Reference

Command structures and functions for defining and managing commands. More...

#include <stdbool.h>

Go to the source code of this file.

Classes

struct  SimpleCommand
 A command which has no arguments. More...
 
struct  GenericCommand
 Commands that take any number of arguments and are not built into Quash. More...
 
struct  ExportCommand
 Command to set environment variables. More...
 
struct  CDCommand
 Command to change directories. More...
 
struct  KillCommand
 Command to kill a process based on it's job id. More...
 
union  Command
 Make all command types the same size and interchangable. More...
 
struct  CommandHolder
 Contains information about the properties of the command. More...
 

Macros

#define REDIRECT_IN   (0x01)
 Flag bit indicating whether a GenericCommand should read from standard in.
 
#define REDIRECT_OUT   (0x04)
 Flag bit indicating whether a GenericCommand should write to standard out truncating the original file.
 
#define REDIRECT_APPEND   (0x08)
 Flag bit indicating whether a GenericCommand should write to standard out appending its output.
 
#define PIPE_IN   (0x10)
 Flag bit indicating whether a GenericCommand should read from a pipe.
 
#define PIPE_OUT   (0x20)
 Flag bit indicating whether a GenericCommand should write to a pipe.
 
#define BACKGROUND   (0x40)
 Flag bit indicating whether a GenericCommand should be run in the background.
 

Typedefs

typedef enum CommandType CommandType
 All possible types of commands. More...
 
typedef struct SimpleCommand SimpleCommand
 A command which has no arguments. More...
 
typedef struct GenericCommand GenericCommand
 Commands that take any number of arguments and are not built into Quash. More...
 
typedef GenericCommand EchoCommand
 Alias for GenericCommand to denote a command to print strings. More...
 
typedef struct ExportCommand ExportCommand
 Command to set environment variables. More...
 
typedef struct CDCommand CDCommand
 Command to change directories. More...
 
typedef struct KillCommand KillCommand
 Command to kill a process based on it's job id. More...
 
typedef SimpleCommand PWDCommand
 Alias for SimpleCommand to denote a print working directory command. More...
 
typedef SimpleCommand JobsCommand
 Alias for SimpleCommand to denote a print jobs list. More...
 
typedef SimpleCommand ExitCommand
 Alias for SimpleCommand to denote a termination of the program. More...
 
typedef SimpleCommand EOCCommand
 Alias for SimpleCommand to denote the end of a command. More...
 
typedef union Command Command
 Make all command types the same size and interchangable. More...
 
typedef struct CommandHolder CommandHolder
 Contains information about the properties of the command. More...
 

Enumerations

enum  CommandType {
  EOC = 0, GENERIC, ECHO, EXPORT,
  KILL, CD, PWD, JOBS,
  EXIT
}
 All possible types of commands. More...
 

Functions

CommandHolder mk_command_holder (char *redirect_in, char *redirect_out, char flags, Command cmd)
 Create the properties for a command. More...
 
Command mk_generic_command (char **args)
 Create a GenericCommand structure and return a copy. More...
 
Command mk_echo_command (char **args)
 Create a GenericCommand structure and return a copy. More...
 
Command mk_export_command (char *env_var, char *val)
 Create a ExportCommand structure and return a copy. More...
 
Command mk_cd_command (char *dir)
 Create a CDCommand structure and return a copy. More...
 
Command mk_kill_command (char *sig, char *job)
 Create a KillCommand structure and return a copy. More...
 
Command mk_pwd_command ()
 Create a PWDCommand structure and return a copy. More...
 
Command mk_jobs_command ()
 Create a JobsCommand structure and return a copy. More...
 
Command mk_exit_command ()
 Create a ExitCommand structure and return a copy. More...
 
Command mk_eoc ()
 Create a EOCCommand structure and return a copy. More...
 
CommandType get_command_type (Command cmd)
 Get the type of the command. More...
 
CommandType get_command_holder_type (CommandHolder holder)
 Get the type of the Command in the CommandHolder. More...
 
void debug_print_script (const CommandHolder *holders)
 Print all commands in the script with print_command() More...
 

Detailed Description

Command structures and functions for defining and managing commands.

Typedef Documentation

◆ CDCommand

typedef struct CDCommand CDCommand

Command to change directories.

See also
realpath(), Command

◆ Command

typedef union Command Command

Make all command types the same size and interchangable.

This is useful for arrays or making a common entry point for all command types. The exact type information can be recovered later with the get_command_type() function.

See also
get_command_type, SimpleCommand, GenericCommand, EchoCommand, ExportCommand, CDCommand, KillCommand, PWDCommand, JobsCommand, ExitCommand, EOCCommand

◆ CommandHolder

typedef struct CommandHolder CommandHolder

Contains information about the properties of the command.

See also
REDIRECT_IN, REDIRECT_OUT, REDIRECT_APPEND, PIPE_IN, PIPE_OUT, BACKGROUND, Command

◆ CommandType

typedef enum CommandType CommandType

All possible types of commands.

These are useful for recovering what the actual type of a command is if the command is placed in a Command union

See also
Command

◆ EchoCommand

Alias for GenericCommand to denote a command to print strings.

Note
EchoCommand is similar to a generic command but is a builtin command
See also
GenericCommand, Command

◆ EOCCommand

Alias for SimpleCommand to denote the end of a command.

See also
SimpleCommand, Command

◆ ExitCommand

Alias for SimpleCommand to denote a termination of the program.

See also
end_main_loop(), SimpleCommand, Command

◆ ExportCommand

typedef struct ExportCommand ExportCommand

Command to set environment variables.

See also
CommandType, lookup_env(), write_env(), Command

◆ GenericCommand

Commands that take any number of arguments and are not built into Quash.

See also
Command

◆ JobsCommand

Alias for SimpleCommand to denote a print jobs list.

See also
SimpleCommand, Command, Job

◆ KillCommand

typedef struct KillCommand KillCommand

Command to kill a process based on it's job id.

See also
CommandType, Command

◆ PWDCommand

Alias for SimpleCommand to denote a print working directory command.

See also
SimpleCommand, Command

◆ SimpleCommand

typedef struct SimpleCommand SimpleCommand

A command which has no arguments.

All command structures can be correctly read as a SimpleCommand since they all have the CommandType field as the first field.

See also
Command, CommandType

Enumeration Type Documentation

◆ CommandType

All possible types of commands.

These are useful for recovering what the actual type of a command is if the command is placed in a Command union

See also
Command

Function Documentation

◆ debug_print_script()

void debug_print_script ( const CommandHolder holders)

Print all commands in the script with print_command()

Note
This only works when the DEBUG macro is defined
Parameters
holdersCommandHolder array to print
See also
CommandHolder

◆ get_command_holder_type()

CommandType get_command_holder_type ( CommandHolder  holder)

Get the type of the Command in the CommandHolder.

Uses the property that all Command variants can be cast to SimpleCommand to extract the CommandType of the Command.

Parameters
holderCommandHolder from which this function extracts the CommandType
Returns
The resulting CommandType of the cmd parameter
See also
CommandType, CommandHolder, SimpleCommand

◆ get_command_type()

CommandType get_command_type ( Command  cmd)

Get the type of the command.

Uses the property that all Command variants can be cast to SimpleCommand to extract the CommandType of the Command.

Parameters
cmdCommand from which this function extracts the CommandType
Returns
The resulting CommandType of the cmd parameter
See also
CommandType, Command, SimpleCommand

◆ mk_cd_command()

Command mk_cd_command ( char *  dir)

Create a CDCommand structure and return a copy.

Parameters
dirPath to the directory we wish to change to
Returns
Copy of constructed CDCommand
See also
realpath(), Command, CDCommand

◆ mk_command_holder()

CommandHolder mk_command_holder ( char *  redirect_in,
char *  redirect_out,
char  flags,
Command  cmd 
)

Create the properties for a command.

Parameters
redirect_inRedirect standard in of this command to a file name redirect_in
redirect_outRedirect standard out of this command to a file name redirect_out
flagsA set of bits that hold information about how to execute the command. The properties can be extracted from the flags field by using a bit-wise & (i.e. flags & PIPE_IN) are macro defined as:
  • REDIRECT_IN
  • REDIRECT_OUT
  • REDIRECT_APPEND
  • PIPE_IN
  • PIPE_OUT
  • BACKGROUND
cmdCommand to hold
Returns
A CommandHolder
See also
CommandType, REDIRECT_IN, REDIRECT_OUT, REDIRECT_APPEND, PIPE_IN, PIPE_OUT, BACKGROUND, Command, CommandHolder

◆ mk_echo_command()

Command mk_echo_command ( char **  args)

Create a GenericCommand structure and return a copy.

Returns
Copy of constructed GenericCommand
See also
CommandType, REDIRECT_IN, REDIRECT_OUT, REDIRECT_APPEND, PIPE_IN, PIPE_OUT, BACKGROUND, Command, GenericCommand

◆ mk_eoc()

Command mk_eoc ( )

Create a EOCCommand structure and return a copy.

Returns
Copy of constructed EOCCommand
See also
Command, EOCCommand

◆ mk_exit_command()

Command mk_exit_command ( )

Create a ExitCommand structure and return a copy.

Returns
Copy of constructed ExitCommand
See also
Command, ExitCommand

◆ mk_export_command()

Command mk_export_command ( char *  env_var,
char *  val 
)

Create a ExportCommand structure and return a copy.

Parameters
env_varName of environment variable to set
valString that should be stored in env_var environment variable
Returns
Copy of constructed ExportCommand
See also
lookup_env(), write_env(), Command, ExportCommand

◆ mk_generic_command()

Command mk_generic_command ( char **  args)

Create a GenericCommand structure and return a copy.

Parameters
argsA NULL terminated array of c-strings ready to pass to exec functions
Returns
Copy of constructed GenericCommand
See also
CommandType, REDIRECT_IN, REDIRECT_OUT, REDIRECT_APPEND, PIPE_IN, PIPE_OUT, BACKGROUND, Command, GenericCommand

◆ mk_jobs_command()

Command mk_jobs_command ( )

Create a JobsCommand structure and return a copy.

Returns
Copy of constructed JobsCommand
See also
Command, JobsCommand

◆ mk_kill_command()

Command mk_kill_command ( char *  sig,
char *  job 
)

Create a KillCommand structure and return a copy.

Parameters
sigSignal to send to the job
jobJob id number
Returns
Copy of constructed KillCommand
See also
Command, KillCommand

◆ mk_pwd_command()

Command mk_pwd_command ( )

Create a PWDCommand structure and return a copy.

Returns
Copy of constructed PWDCommand
See also
Command, PWDCommand