Ticket #2988: simplify code, no logic changes - Denys Vlasenko <vda.linux@googlemail.com> - 2012-10-22 1749.patch

File simplify code, no logic changes - Denys Vlasenko <vda.linux@googlemail.com> - 2012-10-22 1749.patch, 5.8 KB (added by slavazanko, 11 years ago)
  • lib/tty/key.c

    From: Denys Vlasenko <vda.linux@googlemail.com>
    Date: Mon, 22 Oct 2012 16:49:41 +0200
    Subject: [PATCH 1/5] keyboard input: simplify code, no logic changes
    
    This change slightly simplifies and rearranges the code
    in get_key_code(), reduces indentation levels there,
    adds a few comments. The logic remains the same.
    
    This is a preparatory patch for subsequent changes.
    
    Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
    ---
     lib/tty/key.c |  126 ++++++++++++++++++++++++++++----------------------------
     1 files changed, 63 insertions(+), 63 deletions(-)
    
    diff --git a/lib/tty/key.c b/lib/tty/key.c
    index f050202..d2142fa 100644
    a b get_key_code (int no_delay) 
    18471847                pending_keys = NULL; 
    18481848                seq_append = NULL; 
    18491849            } 
    1850             if ((d == ESC_CHAR) && (pending_keys != NULL)) 
     1850            else if (d == ESC_CHAR) 
    18511851            { 
    18521852                d = ALT (*pending_keys++); 
    18531853                goto check_pend; 
    get_key_code (int no_delay) 
    19281928            this = keys->child; 
    19291929        } 
    19301930    } 
     1931 
    19311932    while (this != NULL) 
    19321933    { 
    19331934        if (c == this->ch) 
    19341935        { 
    1935             if (this->child) 
    1936             { 
    1937                 if (!push_char (c)) 
    1938                 { 
    1939                     pending_keys = seq_buffer; 
    1940                     goto pend_send; 
    1941                 } 
    1942                 parent = this; 
    1943                 this = this->child; 
    1944                 if (parent->action == MCKEY_ESCAPE && old_esc_mode) 
    1945                 { 
    1946                     if (no_delay) 
    1947                     { 
    1948                         GET_TIME (esctime); 
    1949                         if (this == NULL) 
    1950                         { 
    1951                             /* Shouldn't happen */ 
    1952                             fputs ("Internal error\n", stderr); 
    1953                             exit (EXIT_FAILURE); 
    1954                         } 
    1955                         goto nodelay_try_again; 
    1956                     } 
    1957                     esctime.tv_sec = -1; 
    1958                     c = xgetch_second (); 
    1959                     if (c == -1) 
    1960                     { 
    1961                         pending_keys = seq_append = NULL; 
    1962                         this = NULL; 
    1963                         return ESC_CHAR; 
    1964                     } 
    1965                 } 
    1966                 else 
    1967                 { 
    1968                     if (no_delay) 
    1969                         goto nodelay_try_again; 
    1970                     c = tty_lowlevel_getch (); 
    1971                 } 
    1972             } 
    1973             else 
     1936            if (!this->child) 
    19741937            { 
    19751938                /* We got a complete match, return and reset search */ 
    19761939                int code; 
    get_key_code (int no_delay) 
    19801943                this = NULL; 
    19811944                return correct_key_code (code); 
    19821945            } 
    1983         } 
    1984         else 
    1985         { 
    1986             if (this->next != NULL) 
    1987                 this = this->next; 
    1988             else 
     1946            /* No match yet, but it may be a prefix for a valid seq */ 
     1947 
     1948            if (!push_char (c)) 
    19891949            { 
    1990                 if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) 
     1950                pending_keys = seq_buffer; 
     1951                goto pend_send; 
     1952            } 
     1953            parent = this; 
     1954            this = this->child; 
     1955            if (parent->action == MCKEY_ESCAPE && old_esc_mode) 
     1956            { 
     1957                if (no_delay) 
     1958                { 
     1959                    GET_TIME (esctime); 
     1960                    if (this == NULL) 
     1961                    { 
     1962                        /* Shouldn't happen */ 
     1963                        fputs ("Internal error\n", stderr); 
     1964                        exit (EXIT_FAILURE); 
     1965                    } 
     1966                    goto nodelay_try_again; 
     1967                } 
     1968                esctime.tv_sec = -1; 
     1969                c = xgetch_second (); 
     1970                if (c == -1) 
    19911971                { 
    1992                     /* Convert escape-digits to F-keys */ 
    1993                     if (g_ascii_isdigit (c)) 
    1994                         c = KEY_F (c - '0'); 
    1995                     else if (c == ' ') 
    1996                         c = ESC_CHAR; 
    1997                     else 
    1998                         c = ALT (c); 
    1999  
    20001972                    pending_keys = seq_append = NULL; 
    20011973                    this = NULL; 
    2002                     return correct_key_code (c); 
     1974                    return ESC_CHAR; 
    20031975                } 
    2004                 /* Did not find a match or {c} was changed in the if above, 
    2005                    so we have to return everything we had skipped 
    2006                  */ 
    2007                 push_char (c); 
    2008                 pending_keys = seq_buffer; 
    2009                 goto pend_send; 
     1976                continue; 
    20101977            } 
     1978            if (no_delay) 
     1979                goto nodelay_try_again; 
     1980            c = tty_lowlevel_getch (); 
     1981            continue; 
    20111982        } 
    2012     } 
     1983 
     1984        /* c != this->ch. Try other keys with this prefix */ 
     1985        if (this->next != NULL) { 
     1986            this = this->next; 
     1987            continue; 
     1988        } 
     1989 
     1990        /* No match found. Is it one of our ESC <key> specials? */ 
     1991        if ((parent != NULL) && (parent->action == MCKEY_ESCAPE)) 
     1992        { 
     1993            /* Convert escape-digits to F-keys */ 
     1994            if (g_ascii_isdigit (c)) 
     1995                c = KEY_F (c - '0'); 
     1996            else if (c == ' ') 
     1997                c = ESC_CHAR; 
     1998            else 
     1999                c = ALT (c); 
     2000 
     2001            pending_keys = seq_append = NULL; 
     2002            this = NULL; 
     2003            return correct_key_code (c); 
     2004        } 
     2005 
     2006        /* Unknown sequence. Maybe a prefix of a longer one. Save it. */ 
     2007        push_char (c); 
     2008        pending_keys = seq_buffer; 
     2009        goto pend_send; 
     2010 
     2011    } /* while (this != NULL) */ 
     2012 
    20132013    this = NULL; 
    20142014    return correct_key_code (c); 
    20152015}