Get input using tab instead of Enter key in C-Linux

Background:

I am developing linux-like shell in C. The first and basic requirement is to get infinite input (probably commands like rm kill etc. , which will execute) from user & upon Tab key, list of possible commands must be output to the screen.

Question:

My question is that how to get the input upon Tab Key press instead of Enter key?

A quick example will be very helpful, since I'm a college student learning C as part of OS course.

1 answer

  • answered 2018-05-16 05:31 dmitrygrach

    You want receive already entered chars for autocomplete? I suggest you to get each key press separately. Thus, it will always be known that user have already entered.

    #include <stdio.h>
    getchar()
    

    or

    #include <conio.h>
    _getch()
    

    https://en.wikibooks.org/wiki/A_Little_C_Primer/C_Console_IO

    Tab is '\t' and Enter is '\n'

    while ((c = getchar()) != EOF)
    {
        if (c == '\n')
            ++newlines;
        else if (c == '\t')
            ++tabs;
        else if (c == ' ')
            ++blanks;
    
        if (c == EOL) {
        printf("Lines: %d\nTabs: %d\nBlanks: %d\n", newlines, tabs, blanks);
        }
    }