=== modified file 'termkey.c' --- termkey.c 2012-01-26 10:13:03 +0000 +++ termkey.c 2012-01-26 10:07:23 +0000 @@ -211,11 +211,38 @@ return tk; } -static int termkey_init(TermKey *tk, const char *term) +TermKey *termkey_new(int fd, int flags) { + TermKey *tk = termkey_alloc(); + if(!tk) + return NULL; + + tk->fd = fd; + + if(!(flags & (TERMKEY_FLAG_RAW|TERMKEY_FLAG_UTF8))) { + int locale_is_utf8 = 0; + char *e; + + if((e = getenv("LANG")) && strstr(e, "UTF-8")) + locale_is_utf8 = 1; + + if(!locale_is_utf8 && (e = getenv("LC_MESSAGES")) && strstr(e, "UTF-8")) + locale_is_utf8 = 1; + + if(!locale_is_utf8 && (e = getenv("LC_ALL")) && strstr(e, "UTF-8")) + locale_is_utf8 = 1; + + if(locale_is_utf8) + flags |= TERMKEY_FLAG_UTF8; + else + flags |= TERMKEY_FLAG_RAW; + } + + termkey_set_flags(tk, flags); + tk->buffer = malloc(tk->buffsize); if(!tk->buffer) - return 0; + goto abort_free_tk; tk->keynames = malloc(sizeof(tk->keynames[0]) * tk->nkeynames); if(!tk->keynames) @@ -234,6 +261,8 @@ register_c0(tk, TERMKEY_SYM_ENTER, 0x0d, NULL); register_c0(tk, TERMKEY_SYM_ESCAPE, 0x1b, NULL); + const char *term = getenv("TERM"); + struct TermKeyDriverNode *tail = NULL; for(i = 0; drivers[i]; i++) { @@ -270,9 +299,9 @@ goto abort_free_keynames; } - if(tk->fd != -1 && !(tk->flags & TERMKEY_FLAG_NOTERMIOS)) { + if(fd != -1 && !(flags & TERMKEY_FLAG_NOTERMIOS)) { struct termios termios; - if(tcgetattr(tk->fd, &termios) == 0) { + if(tcgetattr(fd, &termios) == 0) { tk->restore_termios = termios; tk->restore_termios_valid = 1; @@ -281,7 +310,7 @@ termios.c_cc[VMIN] = 1; termios.c_cc[VTIME] = 0; - if(tk->flags & TERMKEY_FLAG_CTRLC) + if(flags & TERMKEY_FLAG_CTRLC) /* want no signal keys at all, so just disable ISIG */ termios.c_lflag &= ~ISIG; else { @@ -297,7 +326,7 @@ #ifdef DEBUG fprintf(stderr, "Setting termios(3) flags\n"); #endif - tcsetattr(tk->fd, TCSANOW, &termios); + tcsetattr(fd, TCSANOW, &termios); } } @@ -311,7 +340,7 @@ fprintf(stderr, "Drivers started; termkey instance %p is ready\n", tk); #endif - return 1; + return tk; abort_free_drivers: for(p = tk->drivers; p; ) { @@ -327,46 +356,10 @@ abort_free_buffer: free(tk->buffer); - return 0; -} - -TermKey *termkey_new(int fd, int flags) -{ - TermKey *tk = termkey_alloc(); - if(!tk) - return NULL; - - tk->fd = fd; - - if(!(flags & (TERMKEY_FLAG_RAW|TERMKEY_FLAG_UTF8))) { - int locale_is_utf8 = 0; - char *e; - - if((e = getenv("LANG")) && strstr(e, "UTF-8")) - locale_is_utf8 = 1; - - if(!locale_is_utf8 && (e = getenv("LC_MESSAGES")) && strstr(e, "UTF-8")) - locale_is_utf8 = 1; - - if(!locale_is_utf8 && (e = getenv("LC_ALL")) && strstr(e, "UTF-8")) - locale_is_utf8 = 1; - - if(locale_is_utf8) - flags |= TERMKEY_FLAG_UTF8; - else - flags |= TERMKEY_FLAG_RAW; - } - - termkey_set_flags(tk, flags); - - const char *term = getenv("TERM"); - - if(!termkey_init(tk, term)) { - free(tk); - return NULL; - } - - return tk; +abort_free_tk: + free(tk); + + return NULL; } void termkey_free(TermKey *tk)