I'm trying my best on creating a simple shell on linux. Just something I can create to learn how to use basic system calls with.
Scenario: user types in the command, presses tab (so the shell auto-completes his command), the auto-completed command pops out (or suggestions), user presses enter, command evals and executes.
Just like in bash.
I have figured out how to make evals, convert command into tokens, execute it with pipes and stuff. What I can't figure out is the input part. Namely those tab keystrokes.
I know what options I have:
getc()
- get each character separately, store it in a buffer. Can't figure out how to get tab keystrokes, because it pauses execution until it sees '\n' or Ctrl+D. Kinda expensive, since there will be 1 getc() for every character in the command. Also, I will have to deal with buffer reallocation, amortization... boo...scanf("%s")
- too much worrying about buffer overflow. Can't get those tab keystrokes I wan't. Pauses executionread()
(from unistd.h) - could be something I wan't. But I've seen people here that said that it is a real pain to use it to do this. I checked. It is.getline()
- can't get tab keystrokes.
I looked into bash source code, to see how it deals with input, and OH MY GOD. There are 450 lines of code dedicated to do this one simple thing (input.c file).
Are there really no simpler solutions than this? I don't want to use ncurses, I don't care about portability, I just wan't to achieve one goal: getting user input and knowing when he pressed tab key. Do it elegantly, with as little effort as possible.