1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: command.c |
2 | * File: command.c |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: command.C,v 1.19 2003/12/18 05:45:11 pcg Exp $ |
4 | * $Id: command.C,v 1.20 2003/12/18 07:31:18 pcg Exp $ |
5 | * |
5 | * |
6 | * All portions of code are copyright by their respective author/s. |
6 | * All portions of code are copyright by their respective author/s. |
7 | * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk> |
7 | * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk> |
8 | * - original version |
8 | * - original version |
9 | * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> |
9 | * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> |
… | |
… | |
48 | #include "../config.h" /* NECESSARY */ |
48 | #include "../config.h" /* NECESSARY */ |
49 | #include "rxvt.h" /* NECESSARY */ |
49 | #include "rxvt.h" /* NECESSARY */ |
50 | #include "version.h" |
50 | #include "version.h" |
51 | #include "command.h" |
51 | #include "command.h" |
52 | |
52 | |
|
|
53 | #include <wchar.h> |
|
|
54 | |
53 | /*----------------------------------------------------------------------*/ |
55 | /*----------------------------------------------------------------------*/ |
54 | |
56 | |
55 | /*{{{ Convert the keypress event into a string */ |
57 | /*{{{ Convert the keypress event into a string */ |
56 | /* INTPROTO */ |
58 | /* INTPROTO */ |
57 | void |
59 | void |
… | |
… | |
88 | if (R->Input_Context) |
90 | if (R->Input_Context) |
89 | { |
91 | { |
90 | Status status_return; |
92 | Status status_return; |
91 | |
93 | |
92 | #ifdef X_HAVE_UTF8_STRING |
94 | #ifdef X_HAVE_UTF8_STRING |
|
|
95 | if (R->enc_utf8) |
93 | len = Xutf8LookupString (R->Input_Context, ev, (char *)kbuf, |
96 | len = Xutf8LookupString (R->Input_Context, ev, (char *)kbuf, |
94 | KBUFSZ, &keysym, &status_return); |
97 | KBUFSZ, &keysym, &status_return); |
95 | #else |
98 | else |
96 | wchar_t wkbuf[KBUFSZ + 1]; |
99 | #endif |
97 | |
|
|
98 | // assume wchar_t == unicode or better |
|
|
99 | len = XwcLookupString (R->Input_Context, ev, wkbuf, |
|
|
100 | KBUFSZ, &keysym, &status_return); |
|
|
101 | |
|
|
102 | if (status_return == XLookupChars |
|
|
103 | || status_return == XLookupBoth) |
|
|
104 | { |
100 | { |
|
|
101 | wchar_t wkbuf[KBUFSZ + 1]; |
|
|
102 | |
|
|
103 | // the XOpenIM manpage lies about hardcoding the locale |
|
|
104 | // at the point of XOpenIM, so temporarily switch locales |
|
|
105 | if (R->rs[Rs_imLocale]) |
|
|
106 | SET_LOCALE (R->rs[Rs_imLocale]); |
|
|
107 | // assume wchar_t == unicode or better |
|
|
108 | len = XwcLookupString (R->Input_Context, ev, wkbuf, |
|
|
109 | KBUFSZ, &keysym, &status_return); |
|
|
110 | if (R->rs[Rs_imLocale]) |
|
|
111 | SET_LOCALE (R->locale); |
|
|
112 | |
|
|
113 | if (status_return == XLookupChars |
|
|
114 | || status_return == XLookupBoth) |
|
|
115 | { |
105 | wkbuf[len] = 0; |
116 | wkbuf[len] = 0; |
106 | len = wcstombs ((char *)kbuf, wkbuf, KBUFSZ); |
117 | len = wcstombs ((char *)kbuf, wkbuf, KBUFSZ); |
|
|
118 | if (len < 0) |
|
|
119 | len = 0; |
|
|
120 | } |
|
|
121 | else |
|
|
122 | len = 0; |
107 | } |
123 | } |
108 | else |
124 | |
109 | len = 0; |
|
|
110 | #endif |
|
|
111 | valid_keysym = status_return == XLookupKeySym |
125 | valid_keysym = status_return == XLookupKeySym |
112 | || status_return == XLookupBoth; |
126 | || status_return == XLookupBoth; |
113 | } |
127 | } |
114 | else |
128 | else |
115 | #endif |
129 | #endif |
… | |
… | |
903 | // read the next character, currently handles UTF-8 |
917 | // read the next character, currently handles UTF-8 |
904 | // will probably handle all sorts of other stuff in the future |
918 | // will probably handle all sorts of other stuff in the future |
905 | uint32_t |
919 | uint32_t |
906 | rxvt_term::next_char () |
920 | rxvt_term::next_char () |
907 | { |
921 | { |
908 | struct mbstate &s = mbstate; |
|
|
909 | |
|
|
910 | while (cmdbuf_ptr < cmdbuf_endp) |
922 | while (cmdbuf_ptr < cmdbuf_endp) |
911 | { |
923 | { |
912 | uint8_t ch = *cmdbuf_ptr; |
924 | if (*cmdbuf_ptr < 0x80) // assume < 0x80 to be ascii ALWAYS (all shift-states etc.) uh-oh |
913 | |
|
|
914 | if (s.cnt) |
|
|
915 | { |
|
|
916 | if ((ch & 0xc0) == 0x80) |
|
|
917 | { |
|
|
918 | cmdbuf_ptr++; |
|
|
919 | |
|
|
920 | /* continuation */ |
|
|
921 | s.reg = (s.reg << 6) | (ch & 0x7f); |
|
|
922 | |
|
|
923 | if (--s.cnt == 0 && s.reg >= 128) /* if !inrange then corruption or Racking */ |
|
|
924 | return s.reg; |
|
|
925 | |
|
|
926 | continue; |
|
|
927 | } |
|
|
928 | else |
|
|
929 | { |
|
|
930 | s.cnt = 0; |
|
|
931 | return s.orig; /* the _occasional_ non-utf-8 character may slip through... */ |
|
|
932 | } |
|
|
933 | } |
|
|
934 | |
|
|
935 | if ((ch & 0xc0) == 0xc0) |
|
|
936 | { |
|
|
937 | cmdbuf_ptr++; |
925 | return *cmdbuf_ptr++; |
938 | |
926 | |
939 | /* first byte */ |
927 | wchar_t wc; |
940 | s.orig = ch; /* for broken encodings */ |
928 | int len = mbrtowc (&wc, (char *)cmdbuf_ptr, cmdbuf_endp - cmdbuf_ptr, &mbstate.mbs); |
941 | s.reg = ch; |
929 | |
942 | if ((ch & 0xe0) == 0xc0) { s.reg &= 0x1f; s.cnt = 1; } |
930 | if (len == (size_t)-2) |
943 | if ((ch & 0xf0) == 0xe0) { s.reg &= 0x0f; s.cnt = 2; } |
931 | return NOCHAR; |
944 | if ((ch & 0xf8) == 0xf0) { s.reg &= 0x07; s.cnt = 3; } |
932 | |
945 | if ((ch & 0xfc) == 0xf8) { s.reg &= 0x03; s.cnt = 4; } |
933 | if (len == (size_t)-1) |
946 | if ((ch & 0xfe) == 0xfc) { s.reg &= 0x01; s.cnt = 5; } |
934 | return *cmdbuf_ptr++; // the _occasional_ latin1 character is allowed to slip through |
947 | } |
935 | |
948 | else |
936 | // assume wchar == unicode |
949 | { |
937 | cmdbuf_ptr += len; |
950 | cmdbuf_ptr++; /* _occasional_ non-utf8 may slip through... */ |
|
|
951 | return ch; |
938 | return wc; |
952 | } |
|
|
953 | } |
939 | } |
954 | |
940 | |
955 | return NOCHAR; |
941 | return NOCHAR; |
956 | } |
942 | } |
957 | |
943 | |