0

How do I do autofill a text when Tab key is pressed after a period from STDIN using C?

Input would look like,

C:\>autofil.exe Hello
C:\>autofil.exe Hello.  ( When I enter a period, it should autofil Hello after the period)
C:\>autofil.exe Hello.World  *World is autofilled when period was entered.

Second requirement is if I am at

C:\>autofil.exe Hello.World  (And when i press Tab key, `World` should print with other suggestions, as `Friend`, and if I press Tab again, print `Matt`, and scroll so on when tab is pressed... )

Well, that is my requirement. Tab complete to autofill text. Also, want to know how to read the Tab key from STDIN.

Thanks!

  • See [How to Implement Tab Completion](http://stackoverflow.com/questions/5255372/how-to-implement-tab-completion) – Shawn Chin Dec 20 '12 at 11:37
  • 3
    You'd have to implement a custom shell. Your program is not yet running when you're issuing the command to start it. – Fred Foo Dec 20 '12 at 12:29
  • 1
    Had it been `bash`, it was quite easy to implement a simple bash-completion function. I however, see `C:\>` here ;) – anishsane Dec 20 '12 at 12:53
  • You cannot "read a key from stdin", because stdin is *not* a tty. Often, stdin is associated with a tty, and a tty driver is detecting key events and sending characters to the stream from which your program is reading, but stdin is *NOT* a tty. Thinking about key press events simply does not make any sense. – William Pursell Aug 30 '13 at 17:21

1 Answers1

1

You could use Readline GNU library For Windows, is pretty easy to use, the other way around is to use PDcurses/Ncurses (are almost the same) and do it by hand (handling console behavior and such).

In case you use readline, to acomplish autocomplete is as simple as doing:

rl_bind_key('\t', rl_complete);
char *input = readline("C:\>");

In case you use Ncurses/PDcurses, you will have to do a little more work :)

  • First you save actual console parameters.
  • Then you set echo of the input off with noecho().
  • You will have to handle input and parse the arguments to call programs.
  • Also you will have to search in dirs (current and the ones on PATH variable) for things matching your current input.

Before ending your program set again the saved console parameters.

Ncurses How-To Is an easy right to the point guide.

Posix curses definition Contains functions and key definitions.

someoneigna
  • 556
  • 4
  • 10