You should define lexical conventions for your shell. For instance in POSIX shells you can pass an argument with a space by e.g. quoting it or escaping it with a backslash, like in
ls > 'file name with spaces'
which get the output of ls
into a single file named file name with spaces
If coding in C++, you should use genuine C++ features. So you would read the line into a std::string
using std::getline
. Then you should tokenize that string, i.e. transform it into a sequence of tokens. Some tokens are special (so >
is not the same as the quoted '>'
etc...). So define a class Token
and implement a parser which gives a std::vector<Token>
from a line (of type std::string
) that you have previously read.
Of course you'll need to know some syscalls, so read e.g. Advanced Linux Programming. For instance, cd
has to be a shell builtin which calls chdir(2). Redirections need to use dup2(2) and open(2). Pipes need to use pipe(2) etc....
BTW, you should look into the source code of free software shells (and use strace
to find out what they do). Most are coded in C (e.g. GNU bash, or sash
), but fish is coded in C++.