Self-compiled simple shell command interpreter requirements and code

Source: Internet
Author: User
Tags printf strcmp strtok

Requirements:
No. 0 Step: Write the simplest shell command interpreter, this program is taken from Apue example 1-5, command can not take parameters.

What the trainees need to do is as follows:
The command interpreter is first a dead loop.
Prints a command prompt.
The command line input is placed inside the array without requiring the command to take parameters. You can getc (), fgets (), scanf (), and so on.
If you use Fgets (), the resulting string includes the last input newline character, so remove the "\ n" at the end of the command string and change to "\".
Create a child process and call exec to execute the command.
The parent process call Waitpid () waits for the child process to exit and then goes to the next loop.


1th Step: Write a shell command interpreter that enables you to handle commands with parameters.

What the trainees need to do is as follows:
The command interpreter is first a dead loop.
Prints a command prompt that contains the current path information. Get command line input, this program is to save the command line input in a character pointer to the address.
Parse the command line, separating the commands and arguments with a space
Remove them separately from the character pointer array arg[]. The command line string obtained here is saved at the address pointed to by input. In order to separate the commands and arguments in this line of string, a temporary array of TMP is required (this program is to reuse the BUF array used earlier), and input points to commands and arguments in the command line are saved in arg[0], arg[1] and so on.
Create a child process and call exec to execute the command.
The parent process (that is, the shell command interpreter) runs in the foreground or background according to the command, deciding whether to call Waitpid (). Then go to the next loop.

2nd Step: Add the Internal command CD, exit.

What the trainees need to do is as follows:
Based on the 1th step, after parsing the command line input, look at the input command arg[0] is not "exit" or "CD", if so, in the execution of the creation of the child process to execute this command as an internal command execution.
The exit command is implemented simply by printing a word "Bye bye!", then releasing the previously allocated memory space and then exiting.
The implementation of the CD command is used with the function chdir (arg[1]), which is used to change the current working directory.


The 3rd step: the implementation of the program is placed in several files, the introduction of the concept of header files. Redirects and pipe functions are introduced, but these two functions are not implemented specifically.

What the trainees need to do is as follows:
Introduce the header file, put the common variables and functions in the header file, and pay attention to prevent the header file from being contained repeatedly.
A case in which the command line that handles user input contains redirection symbols and pipe symbols. Using functions redirect () and My_pipe () to handle redirects and pipelines, the implementations of the two functions are placed in a separate file without implementing the two functions specifically.

4th Step: Introduction and not into the environment variable configuration file Mysh_profile, read the environment variable, determine whether the file exists, if present, execute the command, otherwise print "comm found".

What the trainees need to do is as follows:
Create an environment variable configuration file that contains a line of PATH environment variables as follows: Path=/bin:/sbin:/usr/bin:/usr/sbin
Create the function Init_environ () and read the environment variable into the array.
Create the function is_founded (), find the file, and see if the file exists.

5th step: Implement the redirect function.

What the trainees need to do is as follows:
Implement the redirect () function in the redirect.c file.

6th step: Implement the Pipeline function

What the trainees need to do is as follows:
Implement the My_pipe () function in the pipe.c file.

7th Step: Implementing History commands

What the trainees need to do is as follows:
In the header file mysh.h, add the data type of the loop array that represents the history, and define the corresponding variables.
Implement functions Add_history () and History_cmd () in history.c.
In Main.c, the command line content is added to the command history loop array before pipeline, redirect, Internal command, and normal command execution.

8th step: Implement the Background job queue, add internal command jobs, BG, FG command.

#include 
#include 
#include 
#include 
int main (void)
{

int i,status,s;
pid_t Pid1;
Char a[100];
Char *arg[100], *b= "(char *) 0";
while (1)
{
printf ("---->");
Fgets (A,100,stdin);
S=sizeof (a);
Arg[0]=strtok (A, "\ n");
for (i=0;arg!=null;i++)
{ 
Arg[i+1]=strtok (NULL, "\ n");
}
arg[i+1]=b;
if (strcmp (arg[0], "exit") ==0)
{
printf ("Bye bye!\n");
Exit (0);
} else if (strcmp (Arg[0], "CD") ==0)
{
chdir (arg[1]);
Break * How to skip Zhis Functino only? 
} else
{
if ((Pid1=fork ()) < 0)
{
perror ("fork");
Exit (1);
}
if (pid1==0)
{
EXECVP (arg[0],arg);
Exit ( -1);
}
Wait (&status);
}
}
return 0;
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.