#include <stdio.h>
#include <termios.h>
#include <unistd.h>

#include "378.h"

char mygetch ( void )
{
        int ch;
        struct termios oldt, newt;
        
        tcgetattr ( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
        
        return ch;
}

l378_DECLARE();

l378_KEYMAP = {
        ['a'] = '2', ['b'] = '2', ['c'] = '2',
        ['d'] = '3', ['e'] = '3', ['f'] = '3',
        ['g'] = '4', ['h'] = '4', ['i'] = '4',
        ['j'] = '5', ['k'] = '5', ['l'] = '5',
        ['m'] = '6', ['n'] = '6', ['o'] = '6',
        ['p'] = '7', ['q'] = '7', ['r'] = '7', ['s'] = '7',
        ['t'] = '8', ['u'] = '8', ['v'] = '8',
        ['w'] = '9', ['x'] = '9', ['y'] = '9', ['z'] = '9'
};

static FILE* l378_dictionary;

int main( const int argc, const char *argv[] )
{        
        char c;
        char* s = NULL;

        if( !(l378_dictionary = fopen( "compressed.dict", "r" )) )
                return -1;

        printf( "     1     2     3\n" );
        printf( "     C    abc   def\n\n" );
        printf( "     4     5     6\n" );
        printf( "    ghi   jkl   mno\n\n" );
        printf( "     7     8     9\n" );
        printf( "    pqrs  tuv  wxyz\n\n" );
        printf( "    TAB    0     - \n" );
        printf( "    auto   _   next\n\n" );

        while( '\n' != (c = mygetch()) )
        {
                switch( c )
                {
                        //case '0':
                        //        break;
                        case '\t':
                                if( (s = l378_match()) )
                                        printf( "%s\r", s );
                                continue;
                        case '-':
                                break;
                        case '1':
                                printf( "\t\t\t\t\r" );
                                l378_popc();
                                break;
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                                l378_pushc( c );
                                break;
                        default:
                                continue;        
                }
                
                if( (s = l378_matche()) )
                        printf( "%s\r", s );
                else
                        printf( "%s\r", l378_pattern );
        }
        
        printf( "\n" );
        
        fclose( l378_dictionary );

        return 0;
}

// eof