How can I implement my own basic unix shell in C?

后端 未结 3 898
梦谈多话
梦谈多话 2020-12-23 12:15

I\'m a newbie to process and thread management. My Shell should understand PATH environment variable. It can be set and modified. It runs in two ways -interacti

3条回答
  •  粉色の甜心
    2020-12-23 12:44

    Your main loop is:

    • read a line (use fgets(3) for a simple shell, readline(3) for a fancy one)
    • parse the command
    • fork and execute the pipelines

    To parse the command, there are two common choices. Write a recursive descent parser or use yacc(1) to generate one. It's a lot easier to bang out an initial parser using yacc, but you can totally get stuck debugging it and it really wants to be context-free. I prefer recursive descent but just about everyone else in the world prefers using yacc. (Technically, bison.) If the shell is really really simple, like a homework shell, yacc may be overkill.

    To do the lexical analysis you can also roll your own or use flex.

    You won't need to use any threads.

提交回复
热议问题