- run
maketo create an executableshell - run command
./shellin the terminal , and there the shell runs.
This is a Linux C-Shell implemented by me.
- To exit the shell :
- Press Ctrl + D , saves the current session history , and exits the terminal
- enter
exitcommand in terminal. - Assumptions :
-
note that :
max_lengthof command names are assumed to be of max-lengthname_lendefined inheader.has1024 -
historycommand displays the10last commands entered , with latest command on the top.
-
-
The shell supports the following commands being implemented by me :
bg,baywatch,cd,echo,fg,history,jobs,ls,pinfo,pwd,replay,repeat,sig, and other externalshell commands. -
It with support for
redirectionoperations [>,<,>>] andpiping[|] . -
Error Handling
-
Signal Handling for signals like
SIGINT,SIGTSTP,SIGCHLDand external keyboard interrupts like Ctrl +C , Ctrl + Z. -
Running processes in
foreground&backgroundenvironments along with -
Command
historyimplemented, storing the latest commands , that remains saved across shell-sessions. -
Multiple commands separated by
;handled, However there's no support for handling"or'( quotes ).
The command implemented are as follows :
-
bg: used to continue the execution of stopped background processes in the background environment. -
baywatch: used to execute the given commands from the list of optionsnewborn,dirty,interruptafter a fixed interval as given input by the user, until q is pressed. Handles errors and invalid argument kind. enterbaywatch helpfor correctsyntaxinformation. -
cd: To change the current working directory of the shell , it also supports multiple flags like :-,.,~,..etc. along with error messages. -
echo: To output the input string onto the terminal ,supportsredirectionandpiping -
fg: To continue the execution of a stopped background process [ stored in thejobslist ] in the foreground. -
history: Implemented linked list kind structure to store the20latest commands entered in the shell . History remains saved across the sessions.history: displays latest entered 10 commands.history <number>: displays the latestnumberhistory commands. -
jobs: A linked list data structure , implemented to keep a track of processes invoked in the background of the shell ( either by the user directly ( using the&flag)) or when a process running in foreground is stopped due to recieving ofsignal or interrupt( like Ctrl + Z or any other signal).stores the process sorted lexographically in a linked list structure.
jobsprints the [job_number]process_statusprocess_namepidfor processes stored in the data structure. Enterjobs helpfor getting correctsyntaxand list of supported options.[-options]:-rand-sflags for specifically printing processes with statusRunningorStoppedrespectively. -
ls: executes the listing command like normal shell , along with proper handling of flags[-al]in any order and any number of times.supports multiple files and directories , and
flagsin any order.Implemented colors for different kind of files -
directory,files,executableetc. to make them visually distinguishable. -
pinfo: outputs the status information of the processes.pinfo: displays the process information for the shell .pinfo <proc_pid>: displays the process information for the process with given pid and displays appropriate error messages, in case of failures or wrong input format. -
pwd: outputs the present working directory , handles errors. -
replay: executes the given command after fixedintervalfor a givenperiodof time. Enterreplay helpfor more commandsyntaxinformation.Usage :replay -command <command> -interval <interval> -period <period>, that makes the command provided by the command flag , to replay itself ( or execute ) at fixed intervals provided by interval flag<interval>for a given<period>.- Handles errors in case of wrong syntax or missing arguments.
- Flags can be used in any order
- Multiple flags of same kind , not allowed ; considered as wrong syntax and an error message is displayed.
-
repeat:Usage:repeat <number_of_repetitions> <command>: repeats the command given ,<number_of_repetitions>times.- Handles errors , in case of wrong syntax or incorrect arguments provided.
-
sig: used to communicate with processes running in background of the shell ( stored injobsdata structure) by sending them signals to processes indicated by their job numbers .sig <job_num> <sig_num>: sends the signal indicated by<sig_num>to the process indicated by<job_num>, handles errors in case of wrong syntax or failure in sending the signals. -
other
shell commands: runs the other shell commands by creating a child process using forking , and executes the commands provided usingexecvpsys call , displays apt error message in case of any failure.
The above implemented commands commands have been organised several headers and implementation files as described by their file_name.
-
main.c- Initializes the
shellvariables. - Contains the main shell loop.
- Initializes the
-
bg.c- Implements the
bgcommand - handles error in case of an invalid
job_numberis given or invalid syntax is used. - continues execution of a stopped background process in background by using.
- Implements the
-
cd.c- Implements the
cdfunction in the shell using syscalls of chdir , throws apt error msgs in case of any error. - Changes directory to the directory given in input, gives an error if an invalid directory is given.
- Implements the
-
echo.c- Implements the standard
echocommand for the shell, takes into account of multiple spaces and tabs in the given argument. - Doesn't account for quotes currently.
- Implements the standard
-
execute.c- contains the main code for parsing the given input command string along with the apt arguments , into it's arguments
- calls the apt execute_command function from amongst the various functions implemented using
if&elseand otherstring comparefunctions. - handles
repeatcommand. - contains code for repeat implemented in it, handles nested repeats as well :)
-
header.h- Includes all the standard C-libraries used all throughout implementing the C-Shell.
- defined useful
macrosused all across the shell.
-
fg.c- Implements the
fgcommand - Does error handling and handles permissions and other signal .
- Implements the
-
history.c- Implements the functionality of storing the 20 most recent commands as an array implemented structure.
- Provides functionality like printing the entire history or printing n most recent commands as specified by the user
- Error handling for invalid argument.
- Provides functionality to store the history of the session into a file ( a .txt file) , and init history function that saves the history of commands across the sessions of the shell.
- Provides method to store back history into file ( saving the history upon exiting the current shell session )
-
input.c- contains implemented getInput command that gets the user entered command and returns the char* string it reads from the terminal.
- Ctrl + D - exits the shell .
-
ls.c- Handles the shell command
lswith flags-[al], can be given in any order. - Handles error , and input in any given order.
- Handles the shell command
-
jobs.c- Implements the jobs command , and associated utility functions for implementing the job data structure , updating it , etc .
-
pinfo.c- implements the
pinfocommand .
- implements the
-
prompt.c- Implements the shell prompt as
<USERNAME@HOSTNAME : curr_dir >
- Implements the shell prompt as
-
pwd.c- implements the shell
pwdcommand, prints to the teminal the current working directory , or displays apt error in case it fails to do so.
- implements the shell
-
pipe.c- Implements the
pipingfor shell.
- Implements the
-
redirection.c- Implements the
redirectioncommands.
- Implements the
-
replay.c- Implements the
replaycommand.
- Implements the
-
signals.c
-
SIGINT_HANDLER: ctrl + C , terminates the foreground process , does nothing for shell. -
SIGCHLD_HANDLER: Implements the apt message of the process ( with process name and pid ) on terminal once a background process ( a child process of the terminal ) stops it's execution or finishes or is stopped by a signal, also updates thejobsdata structure in case child process terminates it's execution or is killed (normallyorabnormallyoron recieving a signal). -
SIGTSTP_HANDLER: ctrl + Z , stops the foreground process and pushes it to stop state in background ( gets stored in thejobsdata structure ). Does nothing for the shell ( doesn't ) exits it. -
implements the
sigcommand .
-
system_command.c- contains code for implementing the system commands ( that arent implemented by me ) , using fork and execvp functionality.
- throws apt error in case of failures
- executes system commands in background or in foreground as specified by the user.
-
tokenize.c- contains basic code for tokenizing the commands taken from user into the constituent commands
- handles extra spaces and tabs and ";"
- returns the list of commands (char ** ) from the given input command string ( char *)
-
helper.c- Contains helper, sub-routine functions for other files , such as a function that returns the path relative to the home_dir ( our pseudo home dir).