Compile a simple shell command parser

Source: Internet
Author: User

Compile a simple shell command parser

Environment:

RedHat 9.0 kernel 2.4.20

VI document editor 6.1.320

GCC 3.2.2-5

 

Steps:

 

Step 2: Write the simplest shell command interpreter. This program is taken from apue Example 1-5. The command cannot contain parameters.

 

The content to be completed is as follows:

  1. The command interpreter is an endless loop first.

  2. Print a command prompt.

  3. Put the command line input in the array without the command with parameters. GETC (), fgets (), and scanf.

  4. If fgets () is used, the obtained string includes the last line break. Therefore, remove "/N" at the end of the command string and change it to "/0 ".

  5. Create a sub-process and call exec to execute the command.

  6. The parent process calls waitpid () to wait for the child process to exit, and then enters the next loop.

 

Step 2: Write a shell command interpreter to enable processing of commands with parameters.

 

The content to be completed is as follows:

  1. The command interpreter is an endless loop first.

  2. Print a command prompt that contains the current path information. Obtain the command line input. This program saves the command line input in the address pointed to by a character pointer.

  3. Analyze the command line and separate the commands and parameters separated by Spaces

Put them in the ARG [] character pointer array. The command line string obtained here is saved at the address pointed to by input. To separate the commands and parameters in this string, a temporary array TMP is required (this program is to reuse the previously used Buf array ), point input to the commands and parameters in the command line and save them in Arg [0], Arg [1], and so on.

4. Create a sub-process and call exec to execute the command.

5. The parent process (that is, the shell command interpreter) determines whether to call waitpid () based on whether the command runs on the foreground or background (). Then enter the next cycle.

 

Step 2: add the Internal commands CD and exit.

 

The content to be completed is as follows:

1. based on step 1, after the command line input is analyzed, check whether the input command Arg [0] is "exit" or "cd". If yes, run this command as an internal command before executing the create sub-process to execute this command.

2. To implement the exit command, just print a sentence "Bye bye !", Then, release the previously allocated memory space and exit.

3. The implementation of the CD command uses the chdir (ARG [1]) function to change the current working directory.

 

Step 2: Program Implementation is put into several files and the concept of header files is introduced. Introduce the redirection and pipeline functions, but you do not need to implement these two functions.

 

The content to be completed is as follows:

1. Introduce the header file and put public variables and functions in the header file. Be sure to prevent the header file from being repeatedly contained.

2. Handle situations where user-entered command lines contain redirection and pipeline symbols. Use the Redirect () and my_pipe () functions to process redirection and pipelines. The implementation of these two functions is placed in a separate file, so you do not need to implement these two functions.

 

Step 2: Introduce the environment variable configuration file mysh_profile, read the environment variable, and determine whether the file exists. If yes, run the command; otherwise, print "command not found ".

 

The content to be completed is as follows:

1. Create an environment variable configuration file with the following PATH environment variables: Path =/bin:/sbin:/usr/bin:/usr/sbin.

2. Create the init_environ () function to read environment variables to the array.

3. Create the function is_founded () to find the file and check whether the file exists.

 

Step 2: implement the redirection function.

 

The content to be completed is as follows:

Implement the Redirect () function in the Redirect. c file.

 

Step 2: implement the MPs queue Function

 

The content to be completed is as follows:

Implement the my_pipe () function in the pipe. c file.

 

Step 2: implement the history command history

 

The content to be completed is as follows:

1. In the header file mysh. H, add the Data Type of the loop array that represents the history and define the corresponding variables.

2. Implement the add_history () and history_cmd () functions in history. C ().

3. In Main. C, add the command line content to the command history loop array before pipeline, redirection, internal commands, and Common commands are executed.

 

Step 2: implement the background job queue and add the Internal commands jobs, BG, and Fg.

 

 

 

 

 

Features completed:

Step 0th:
1. After each command is complete, print the prompt again.
2. Test: ls CAT and other commands

1st step: Parameters
1. The current path is displayed in the prompt.
2. Test the command "ls", "ls <space>", "<space> ls", and "<space> ls <space>"
"Ls-a-l", "<space> ls <multiple spaces>-A <multiple spaces>-l"

2nd step: CD exit
1. Test "CD ..", "CD/Home", "CD-", "CD ~ "And other commands

4th step: Environment Variables
1. Copy the/bin/ls file to the/home directory,
Rename/bin/ls to/bin/LS-BK and test the LS command.
2. Add the "/home" directory to the environment variable file mysh_profile to test the LS command.

5th step: Redirect
1. Test the "ls> test", "ls (Space)> (Space) test", "ls (Space)> test" command.
2. Test the "ls> test", "ls (Space)> (Space) test", "ls (Space)> test" command.
3. Test the "cat <Test", "Cat (Space) <(Space) test", "Cat (Space) <Test" command.
4. Test the "cat> test <test1", "Cat> test1 <Test" command. Test is generated by "ls-Al> test ".

Step 6th: MPS queue
1. Test the "ls (Space) | (Space) More" command.

7th step: History
1. Use the up/down key to extract commands from history

Step 8th: Manage jobs
1. Test the jobs, BG, FG, "Ctrl + C", "Ctrl + z" commands.

 

 

Summary: The main knowledge involved in this project is process control and Process Communication. Of course, the basics of C language are the most important.

The basic functions of the project have been implemented, but there are some issues to be resolved: 1. after History commands are called up, the corresponding commands cannot be executed after they are added or removed. When the 2.jobs linked list is created, no judgment conditions are added, duplicate contents are saved in the linked list when the background commands are executed.

 

Source code location:

Http://download.csdn.net/source/1237870

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.