mirror of
https://github.com/tmux/tmux.git
synced 2025-09-08 04:18:19 +00:00
Merge branch 'obsd-master'
This commit is contained in:
@@ -406,11 +406,11 @@ args_string_percentage(const char *value, long long minval, long long maxval,
|
|||||||
}
|
}
|
||||||
ll = (curval * ll) / 100;
|
ll = (curval * ll) / 100;
|
||||||
if (ll < minval) {
|
if (ll < minval) {
|
||||||
*cause = xstrdup("too large");
|
*cause = xstrdup("too small");
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
if (ll > maxval) {
|
if (ll > maxval) {
|
||||||
*cause = xstrdup("too small");
|
*cause = xstrdup("too large");
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
11
cmd-parse.y
11
cmd-parse.y
@@ -26,6 +26,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
#include "tmux.h"
|
#include "tmux.h"
|
||||||
|
|
||||||
@@ -1251,10 +1252,9 @@ error:
|
|||||||
static int
|
static int
|
||||||
yylex_token_escape(char **buf, size_t *len)
|
yylex_token_escape(char **buf, size_t *len)
|
||||||
{
|
{
|
||||||
int ch, type, o2, o3;
|
int ch, type, o2, o3, mlen;
|
||||||
u_int size, i, tmp;
|
u_int size, i, tmp;
|
||||||
char s[9];
|
char s[9], m[MB_LEN_MAX];
|
||||||
struct utf8_data ud;
|
|
||||||
|
|
||||||
ch = yylex_getc();
|
ch = yylex_getc();
|
||||||
|
|
||||||
@@ -1339,11 +1339,12 @@ unicode:
|
|||||||
yyerror("invalid \\%c argument", type);
|
yyerror("invalid \\%c argument", type);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
if (utf8_split(tmp, &ud) != UTF8_DONE) {
|
mlen = wctomb(m, tmp);
|
||||||
|
if (mlen <= 0 || mlen > (int)sizeof m) {
|
||||||
yyerror("invalid \\%c argument", type);
|
yyerror("invalid \\%c argument", type);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
yylex_append(buf, len, ud.data, ud.size);
|
yylex_append(buf, len, m, mlen);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -117,7 +117,7 @@ cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
|
|||||||
if (args_has(args, 'y')) {
|
if (args_has(args, 'y')) {
|
||||||
y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
|
y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
|
||||||
if (cause != NULL) {
|
if (cause != NULL) {
|
||||||
cmdq_error(item, "width %s", cause);
|
cmdq_error(item, "height %s", cause);
|
||||||
free(cause);
|
free(cause);
|
||||||
return (CMD_RETURN_ERROR);
|
return (CMD_RETURN_ERROR);
|
||||||
}
|
}
|
||||||
|
@@ -91,8 +91,8 @@ cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
|
|||||||
struct args *args, int i)
|
struct args *args, int i)
|
||||||
{
|
{
|
||||||
const char *s = args->argv[i];
|
const char *s = args->argv[i];
|
||||||
struct utf8_data *ud, *uc;
|
struct utf8_data *ud, *loop;
|
||||||
wchar_t wc;
|
utf8_char uc;
|
||||||
key_code key;
|
key_code key;
|
||||||
char *endptr;
|
char *endptr;
|
||||||
long n;
|
long n;
|
||||||
@@ -117,10 +117,10 @@ cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
|
|||||||
}
|
}
|
||||||
if (literal) {
|
if (literal) {
|
||||||
ud = utf8_fromcstr(s);
|
ud = utf8_fromcstr(s);
|
||||||
for (uc = ud; uc->size != 0; uc++) {
|
for (loop = ud; loop->size != 0; loop++) {
|
||||||
if (utf8_combine(uc, &wc) != UTF8_DONE)
|
if (utf8_from_data(loop, &uc) != UTF8_DONE)
|
||||||
continue;
|
continue;
|
||||||
after = cmd_send_keys_inject_key(item, after, wc);
|
after = cmd_send_keys_inject_key(item, after, uc);
|
||||||
}
|
}
|
||||||
free(ud);
|
free(ud);
|
||||||
}
|
}
|
||||||
|
10
grid.c
10
grid.c
@@ -76,7 +76,7 @@ grid_need_extended_cell(const struct grid_cell_entry *gce,
|
|||||||
return (1);
|
return (1);
|
||||||
if (gc->attr > 0xff)
|
if (gc->attr > 0xff)
|
||||||
return (1);
|
return (1);
|
||||||
if (gc->data.size != 1 || gc->data.width != 1)
|
if (gc->data.size > 1 || gc->data.width > 1)
|
||||||
return (1);
|
return (1);
|
||||||
if ((gc->fg & COLOUR_FLAG_RGB) || (gc->bg & COLOUR_FLAG_RGB))
|
if ((gc->fg & COLOUR_FLAG_RGB) || (gc->bg & COLOUR_FLAG_RGB))
|
||||||
return (1);
|
return (1);
|
||||||
@@ -114,7 +114,7 @@ grid_extended_cell(struct grid_line *gl, struct grid_cell_entry *gce,
|
|||||||
gl->flags |= GRID_LINE_EXTENDED;
|
gl->flags |= GRID_LINE_EXTENDED;
|
||||||
|
|
||||||
gee = &gl->extddata[gce->offset];
|
gee = &gl->extddata[gce->offset];
|
||||||
gee->data = utf8_map_big(&gc->data);
|
utf8_from_data(&gc->data, &gee->data);
|
||||||
gee->attr = gc->attr;
|
gee->attr = gc->attr;
|
||||||
gee->flags = flags;
|
gee->flags = flags;
|
||||||
gee->fg = gc->fg;
|
gee->fg = gc->fg;
|
||||||
@@ -496,7 +496,8 @@ grid_get_cell1(struct grid_line *gl, u_int px, struct grid_cell *gc)
|
|||||||
gc->fg = gee->fg;
|
gc->fg = gee->fg;
|
||||||
gc->bg = gee->bg;
|
gc->bg = gee->bg;
|
||||||
gc->us = gee->us;
|
gc->us = gee->us;
|
||||||
utf8_get_big(gee->data, &gc->data);
|
log_debug("!!! %x", gc->flags);
|
||||||
|
utf8_to_data(gee->data, &gc->data);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -541,6 +542,7 @@ grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
|
|||||||
gl->cellused = px + 1;
|
gl->cellused = px + 1;
|
||||||
|
|
||||||
gce = &gl->celldata[px];
|
gce = &gl->celldata[px];
|
||||||
|
if (gc->flags & GRID_FLAG_PADDING) log_debug("!!! padding %d\n", grid_need_extended_cell(gce, gc));
|
||||||
if (grid_need_extended_cell(gce, gc))
|
if (grid_need_extended_cell(gce, gc))
|
||||||
grid_extended_cell(gl, gce, gc);
|
grid_extended_cell(gl, gce, gc);
|
||||||
else
|
else
|
||||||
@@ -570,7 +572,7 @@ grid_set_cells(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc,
|
|||||||
gce = &gl->celldata[px + i];
|
gce = &gl->celldata[px + i];
|
||||||
if (grid_need_extended_cell(gce, gc)) {
|
if (grid_need_extended_cell(gce, gc)) {
|
||||||
gee = grid_extended_cell(gl, gce, gc);
|
gee = grid_extended_cell(gl, gce, gc);
|
||||||
gee->data = utf8_set_big(s[i], 1);
|
gee->data = utf8_build_one(s[i], 1);
|
||||||
} else
|
} else
|
||||||
grid_store_cell(gce, gc, s[i]);
|
grid_store_cell(gce, gc, s[i]);
|
||||||
}
|
}
|
||||||
|
@@ -468,10 +468,9 @@ input_key(struct screen *s, struct bufferevent *bev, key_code key)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
if (justkey > 0x7f && justkey < KEYC_BASE) {
|
if (justkey > 0x7f && justkey < KEYC_BASE) {
|
||||||
if (utf8_split(justkey, &ud) != UTF8_DONE)
|
|
||||||
return (-1);
|
|
||||||
if (key & KEYC_META)
|
if (key & KEYC_META)
|
||||||
bufferevent_write(bev, "\033", 1);
|
bufferevent_write(bev, "\033", 1);
|
||||||
|
utf8_to_data(justkey, &ud);
|
||||||
bufferevent_write(bev, ud.data, ud.size);
|
bufferevent_write(bev, ud.data, ud.size);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
@@ -169,7 +169,7 @@ key_string_lookup_string(const char *string)
|
|||||||
struct utf8_data ud;
|
struct utf8_data ud;
|
||||||
u_int i;
|
u_int i;
|
||||||
enum utf8_state more;
|
enum utf8_state more;
|
||||||
wchar_t wc;
|
utf8_char uc;
|
||||||
|
|
||||||
/* Is this no key or any key? */
|
/* Is this no key or any key? */
|
||||||
if (strcasecmp(string, "None") == 0)
|
if (strcasecmp(string, "None") == 0)
|
||||||
@@ -210,9 +210,9 @@ key_string_lookup_string(const char *string)
|
|||||||
more = utf8_append(&ud, (u_char)string[i]);
|
more = utf8_append(&ud, (u_char)string[i]);
|
||||||
if (more != UTF8_DONE)
|
if (more != UTF8_DONE)
|
||||||
return (KEYC_UNKNOWN);
|
return (KEYC_UNKNOWN);
|
||||||
if (utf8_combine(&ud, &wc) != UTF8_DONE)
|
if (utf8_from_data(&ud, &uc) != UTF8_DONE)
|
||||||
return (KEYC_UNKNOWN);
|
return (KEYC_UNKNOWN);
|
||||||
return (wc|modifiers);
|
return (uc|modifiers);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Otherwise look the key up in the table. */
|
/* Otherwise look the key up in the table. */
|
||||||
@@ -349,13 +349,12 @@ key_string_lookup_key(key_code key, int with_flags)
|
|||||||
|
|
||||||
/* Is this a UTF-8 key? */
|
/* Is this a UTF-8 key? */
|
||||||
if (key > 127 && key < KEYC_BASE) {
|
if (key > 127 && key < KEYC_BASE) {
|
||||||
if (utf8_split(key, &ud) == UTF8_DONE) {
|
utf8_to_data(key, &ud);
|
||||||
off = strlen(out);
|
off = strlen(out);
|
||||||
memcpy(out + off, ud.data, ud.size);
|
memcpy(out + off, ud.data, ud.size);
|
||||||
out[off + ud.size] = '\0';
|
out[off + ud.size] = '\0';
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Invalid keys are errors. */
|
/* Invalid keys are errors. */
|
||||||
if (key > 255) {
|
if (key > 255) {
|
||||||
|
@@ -39,7 +39,7 @@ static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
|
|||||||
const struct utf8_data *, u_int *);
|
const struct utf8_data *, u_int *);
|
||||||
|
|
||||||
static const struct grid_cell screen_write_pad_cell = {
|
static const struct grid_cell screen_write_pad_cell = {
|
||||||
{ { 0 }, 0, 0, 0 }, 0, GRID_FLAG_PADDING, 0, 8, 8
|
{ { 0 }, 0, 0, 0 }, 0, GRID_FLAG_PADDING, 8, 8, 0
|
||||||
};
|
};
|
||||||
|
|
||||||
struct screen_write_collect_item {
|
struct screen_write_collect_item {
|
||||||
|
3
status.c
3
status.c
@@ -1275,8 +1275,7 @@ process_key:
|
|||||||
append_key:
|
append_key:
|
||||||
if (key <= 0x1f || key >= KEYC_BASE)
|
if (key <= 0x1f || key >= KEYC_BASE)
|
||||||
return (0);
|
return (0);
|
||||||
if (utf8_split(key, &tmp) != UTF8_DONE)
|
utf8_to_data(key, &tmp);
|
||||||
return (0);
|
|
||||||
|
|
||||||
c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
|
c->prompt_buffer = xreallocarray(c->prompt_buffer, size + 2,
|
||||||
sizeof *c->prompt_buffer);
|
sizeof *c->prompt_buffer);
|
||||||
|
18
tmux.h
18
tmux.h
@@ -27,7 +27,6 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <wchar.h>
|
|
||||||
|
|
||||||
#ifdef HAVE_UTEMPTER
|
#ifdef HAVE_UTEMPTER
|
||||||
#include <utempter.h>
|
#include <utempter.h>
|
||||||
@@ -598,10 +597,13 @@ struct msg_write_close {
|
|||||||
#define ALL_MOUSE_MODES (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON|MODE_MOUSE_ALL)
|
#define ALL_MOUSE_MODES (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON|MODE_MOUSE_ALL)
|
||||||
#define MOTION_MOUSE_MODES (MODE_MOUSE_BUTTON|MODE_MOUSE_ALL)
|
#define MOTION_MOUSE_MODES (MODE_MOUSE_BUTTON|MODE_MOUSE_ALL)
|
||||||
|
|
||||||
|
/* A single UTF-8 character. */
|
||||||
|
typedef u_int utf8_char;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A single UTF-8 character. UTF8_SIZE must be big enough to hold combining
|
* An expanded UTF-8 character. UTF8_SIZE must be big enough to hold combining
|
||||||
* characters as well. It can't be more than 32 bytes without changes to how
|
* characters as well. It can't be more than 32 bytes without changes to how
|
||||||
* big characters are stored.
|
* characters are stored.
|
||||||
*/
|
*/
|
||||||
#define UTF8_SIZE 21
|
#define UTF8_SIZE 21
|
||||||
struct utf8_data {
|
struct utf8_data {
|
||||||
@@ -675,7 +677,7 @@ struct grid_cell {
|
|||||||
|
|
||||||
/* Grid extended cell entry. */
|
/* Grid extended cell entry. */
|
||||||
struct grid_extd_entry {
|
struct grid_extd_entry {
|
||||||
u_int data;
|
utf8_char data;
|
||||||
u_short attr;
|
u_short attr;
|
||||||
u_char flags;
|
u_char flags;
|
||||||
int fg;
|
int fg;
|
||||||
@@ -2891,15 +2893,13 @@ u_int session_group_attached_count(struct session_group *);
|
|||||||
void session_renumber_windows(struct session *);
|
void session_renumber_windows(struct session *);
|
||||||
|
|
||||||
/* utf8.c */
|
/* utf8.c */
|
||||||
u_int utf8_set_big(char, u_int);
|
utf8_char utf8_build_one(char, u_int);
|
||||||
u_int utf8_map_big(const struct utf8_data *);
|
enum utf8_state utf8_from_data(const struct utf8_data *, utf8_char *);
|
||||||
void utf8_get_big(u_int, struct utf8_data *);
|
void utf8_to_data(utf8_char, struct utf8_data *);
|
||||||
void utf8_set(struct utf8_data *, u_char);
|
void utf8_set(struct utf8_data *, u_char);
|
||||||
void utf8_copy(struct utf8_data *, const struct utf8_data *);
|
void utf8_copy(struct utf8_data *, const struct utf8_data *);
|
||||||
enum utf8_state utf8_open(struct utf8_data *, u_char);
|
enum utf8_state utf8_open(struct utf8_data *, u_char);
|
||||||
enum utf8_state utf8_append(struct utf8_data *, u_char);
|
enum utf8_state utf8_append(struct utf8_data *, u_char);
|
||||||
enum utf8_state utf8_combine(const struct utf8_data *, wchar_t *);
|
|
||||||
enum utf8_state utf8_split(wchar_t, struct utf8_data *);
|
|
||||||
int utf8_isvalid(const char *);
|
int utf8_isvalid(const char *);
|
||||||
int utf8_strvis(char *, const char *, size_t, int);
|
int utf8_strvis(char *, const char *, size_t, int);
|
||||||
int utf8_stravis(char **, const char *, int);
|
int utf8_stravis(char **, const char *, int);
|
||||||
|
@@ -578,8 +578,8 @@ tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
|
|||||||
struct tty_key *tk, *tk1;
|
struct tty_key *tk, *tk1;
|
||||||
struct utf8_data ud;
|
struct utf8_data ud;
|
||||||
enum utf8_state more;
|
enum utf8_state more;
|
||||||
|
utf8_char uc;
|
||||||
u_int i;
|
u_int i;
|
||||||
wchar_t wc;
|
|
||||||
|
|
||||||
log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
|
log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
|
||||||
(int)len, buf, expired);
|
(int)len, buf, expired);
|
||||||
@@ -611,12 +611,12 @@ tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
|
|||||||
if (more != UTF8_DONE)
|
if (more != UTF8_DONE)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
||||||
if (utf8_combine(&ud, &wc) != UTF8_DONE)
|
if (utf8_from_data(&ud, &uc) != UTF8_DONE)
|
||||||
return (-1);
|
return (-1);
|
||||||
*key = wc;
|
*key = uc;
|
||||||
|
|
||||||
log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
|
log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
|
||||||
buf, *key);
|
ud.data, *key);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
313
utf8.c
313
utf8.c
@@ -26,153 +26,157 @@
|
|||||||
|
|
||||||
#include "tmux.h"
|
#include "tmux.h"
|
||||||
|
|
||||||
static int utf8_width(wchar_t);
|
struct utf8_item {
|
||||||
|
u_int offset;
|
||||||
struct utf8_big_item {
|
RB_ENTRY(utf8_item) entry;
|
||||||
u_int index;
|
|
||||||
RB_ENTRY(utf8_big_item) entry;
|
|
||||||
|
|
||||||
char data[UTF8_SIZE];
|
char data[UTF8_SIZE];
|
||||||
u_char size;
|
u_char size;
|
||||||
};
|
};
|
||||||
RB_HEAD(utf8_big_tree, utf8_big_item);
|
RB_HEAD(utf8_tree, utf8_item);
|
||||||
|
|
||||||
static int
|
static int
|
||||||
utf8_big_cmp(struct utf8_big_item *bi1, struct utf8_big_item *bi2)
|
utf8_cmp(struct utf8_item *ui1, struct utf8_item *ui2)
|
||||||
{
|
{
|
||||||
if (bi1->size < bi2->size)
|
if (ui1->size < ui2->size)
|
||||||
return (-1);
|
return (-1);
|
||||||
if (bi1->size > bi2->size)
|
if (ui1->size > ui2->size)
|
||||||
return (1);
|
return (1);
|
||||||
return (memcmp(bi1->data, bi2->data, bi1->size));
|
return (memcmp(ui1->data, ui2->data, ui1->size));
|
||||||
}
|
}
|
||||||
RB_GENERATE_STATIC(utf8_big_tree, utf8_big_item, entry, utf8_big_cmp);
|
RB_GENERATE_STATIC(utf8_tree, utf8_item, entry, utf8_cmp);
|
||||||
static struct utf8_big_tree utf8_big_tree = RB_INITIALIZER(utf8_big_tree);
|
static struct utf8_tree utf8_tree = RB_INITIALIZER(utf8_tree);
|
||||||
|
|
||||||
static struct utf8_big_item *utf8_big_list;
|
static struct utf8_item *utf8_list;
|
||||||
static u_int utf8_big_list_size;
|
static u_int utf8_list_size;
|
||||||
static u_int utf8_big_list_used;
|
static u_int utf8_list_used;
|
||||||
|
|
||||||
union utf8_big_map {
|
union utf8_map {
|
||||||
u_int value;
|
utf8_char uc;
|
||||||
struct {
|
struct {
|
||||||
u_char flags;
|
u_char flags;
|
||||||
#define UTF8_BIG_SIZE 0x1f
|
#define UTF8_FLAG_SIZE 0x1f
|
||||||
#define UTF8_BIG_WIDTH2 0x20
|
#define UTF8_FLAG_WIDTH2 0x20
|
||||||
|
|
||||||
u_char data[3];
|
u_char data[3];
|
||||||
};
|
};
|
||||||
} __packed;
|
} __packed;
|
||||||
|
|
||||||
static const union utf8_big_map utf8_big_space1 = {
|
static const union utf8_map utf8_space1 = {
|
||||||
.flags = 1,
|
.flags = 1,
|
||||||
.data = " "
|
.data = " "
|
||||||
};
|
};
|
||||||
static const union utf8_big_map utf8_big_space2 = {
|
static const union utf8_map utf8_space2 = {
|
||||||
.flags = UTF8_BIG_WIDTH2|2,
|
.flags = UTF8_FLAG_WIDTH2|2,
|
||||||
.data = " "
|
.data = " "
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Get a big item by index. */
|
/* Get a UTF-8 item by offset. */
|
||||||
static struct utf8_big_item *
|
static struct utf8_item *
|
||||||
utf8_get_big_item(const char *data, size_t size)
|
utf8_get_item(const char *data, size_t size)
|
||||||
{
|
{
|
||||||
struct utf8_big_item bi;
|
struct utf8_item ui;
|
||||||
|
|
||||||
memcpy(bi.data, data, size);
|
memcpy(ui.data, data, size);
|
||||||
bi.size = size;
|
ui.size = size;
|
||||||
|
|
||||||
return (RB_FIND(utf8_big_tree, &utf8_big_tree, &bi));
|
return (RB_FIND(utf8_tree, &utf8_tree, &ui));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add a big item. */
|
/* Expand UTF-8 list. */
|
||||||
static int
|
static int
|
||||||
utf8_put_big_item(const char *data, size_t size, u_int *index)
|
utf8_expand_list(void)
|
||||||
{
|
{
|
||||||
struct utf8_big_item *bi;
|
if (utf8_list_size == 0xffffff)
|
||||||
|
|
||||||
bi = utf8_get_big_item(data, size);
|
|
||||||
if (bi != NULL) {
|
|
||||||
*index = bi->index;
|
|
||||||
log_debug("%s: have %.*s at %u", __func__, (int)size, data,
|
|
||||||
*index);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (utf8_big_list_used == utf8_big_list_size) {
|
|
||||||
if (utf8_big_list_size == 0xffffff)
|
|
||||||
return (-1);
|
return (-1);
|
||||||
if (utf8_big_list_size == 0)
|
if (utf8_list_size == 0)
|
||||||
utf8_big_list_size = 256;
|
utf8_list_size = 256;
|
||||||
else if (utf8_big_list_size > 0x7fffff)
|
else if (utf8_list_size > 0x7fffff)
|
||||||
utf8_big_list_size = 0xffffff;
|
utf8_list_size = 0xffffff;
|
||||||
else
|
else
|
||||||
utf8_big_list_size *= 2;
|
utf8_list_size *= 2;
|
||||||
utf8_big_list = xreallocarray(utf8_big_list, utf8_big_list_size,
|
utf8_list = xreallocarray(utf8_list, utf8_list_size, sizeof *utf8_list);
|
||||||
sizeof *utf8_big_list);
|
|
||||||
}
|
|
||||||
*index = utf8_big_list_used++;
|
|
||||||
|
|
||||||
bi = &utf8_big_list[*index];
|
|
||||||
bi->index = *index;
|
|
||||||
memcpy(bi->data, data, size);
|
|
||||||
bi->size = size;
|
|
||||||
RB_INSERT(utf8_big_tree, &utf8_big_tree, bi);
|
|
||||||
|
|
||||||
log_debug("%s: added %.*s at %u", __func__, (int)size, data, *index);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get UTF-8 as index into buffer. */
|
/* Add a UTF-8 item. */
|
||||||
u_int
|
static int
|
||||||
utf8_map_big(const struct utf8_data *ud)
|
utf8_put_item(const char *data, size_t size, u_int *offset)
|
||||||
{
|
{
|
||||||
union utf8_big_map m = { .value = 0 };
|
struct utf8_item *ui;
|
||||||
u_int o;
|
|
||||||
const char *data = ud->data;
|
ui = utf8_get_item(data, size);
|
||||||
size_t size = ud->size;
|
if (ui != NULL) {
|
||||||
|
*offset = ui->offset;
|
||||||
|
log_debug("%s: have %.*s at %u", __func__, (int)size, data,
|
||||||
|
*offset);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (utf8_list_used == utf8_list_size && utf8_expand_list() != 0)
|
||||||
|
return (-1);
|
||||||
|
*offset = utf8_list_used++;
|
||||||
|
|
||||||
|
ui = &utf8_list[*offset];
|
||||||
|
ui->offset = *offset;
|
||||||
|
memcpy(ui->data, data, size);
|
||||||
|
ui->size = size;
|
||||||
|
RB_INSERT(utf8_tree, &utf8_tree, ui);
|
||||||
|
|
||||||
|
log_debug("%s: added %.*s at %u", __func__, (int)size, data, *offset);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get UTF-8 character from data. */
|
||||||
|
enum utf8_state
|
||||||
|
utf8_from_data(const struct utf8_data *ud, utf8_char *uc)
|
||||||
|
{
|
||||||
|
union utf8_map m = { .uc = 0 };
|
||||||
|
u_int offset;
|
||||||
|
|
||||||
if (ud->width != 1 && ud->width != 2)
|
if (ud->width != 1 && ud->width != 2)
|
||||||
return (utf8_big_space1.value);
|
return (utf8_space1.uc);
|
||||||
|
|
||||||
if (size > UTF8_BIG_SIZE)
|
if (ud->size > UTF8_FLAG_SIZE)
|
||||||
goto fail;
|
goto fail;
|
||||||
if (size == 1)
|
if (ud->size == 1)
|
||||||
return (utf8_set_big(data[0], 1));
|
return (utf8_build_one(ud->data[0], 1));
|
||||||
|
|
||||||
m.flags = size;
|
m.flags = ud->size;
|
||||||
if (ud->width == 2)
|
if (ud->width == 2)
|
||||||
m.flags |= UTF8_BIG_WIDTH2;
|
m.flags |= UTF8_FLAG_WIDTH2;
|
||||||
|
|
||||||
if (size <= 3) {
|
if (ud->size <= 3)
|
||||||
memcpy(&m.data, data, size);
|
memcpy(m.data, ud->data, ud->size);
|
||||||
return (m.value);
|
else {
|
||||||
}
|
if (utf8_put_item(ud->data, ud->size, &offset) != 0)
|
||||||
|
|
||||||
if (utf8_put_big_item(data, size, &o) != 0)
|
|
||||||
goto fail;
|
goto fail;
|
||||||
m.data[0] = (o & 0xff);
|
m.data[0] = (offset & 0xff);
|
||||||
m.data[1] = (o >> 8) & 0xff;
|
m.data[1] = (offset >> 8) & 0xff;
|
||||||
m.data[2] = (o >> 16);
|
m.data[2] = (offset >> 16);
|
||||||
return (m.value);
|
}
|
||||||
|
*uc = m.uc;
|
||||||
|
return (UTF8_DONE);
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
if (ud->width == 1)
|
if (ud->width == 1)
|
||||||
return (utf8_big_space1.value);
|
*uc = utf8_space1.uc;
|
||||||
return (utf8_big_space2.value);
|
else
|
||||||
|
*uc = utf8_space2.uc;
|
||||||
|
return (UTF8_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get UTF-8 from index into buffer. */
|
/* Get UTF-8 data from character. */
|
||||||
void
|
void
|
||||||
utf8_get_big(u_int v, struct utf8_data *ud)
|
utf8_to_data(utf8_char uc, struct utf8_data *ud)
|
||||||
{
|
{
|
||||||
union utf8_big_map m = { .value = v };
|
union utf8_map m = { .uc = uc };
|
||||||
struct utf8_big_item *bi;
|
struct utf8_item *ui;
|
||||||
u_int o;
|
u_int offset;
|
||||||
|
|
||||||
memset(ud, 0, sizeof *ud);
|
memset(ud, 0, sizeof *ud);
|
||||||
ud->size = ud->have = (m.flags & UTF8_BIG_SIZE);
|
ud->size = ud->have = (m.flags & UTF8_FLAG_SIZE);
|
||||||
if (m.flags & UTF8_BIG_WIDTH2)
|
if (m.flags & UTF8_FLAG_WIDTH2)
|
||||||
ud->width = 2;
|
ud->width = 2;
|
||||||
else
|
else
|
||||||
ud->width = 1;
|
ud->width = 1;
|
||||||
@@ -182,24 +186,24 @@ utf8_get_big(u_int v, struct utf8_data *ud)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
o = ((u_int)m.data[2] << 16)|((u_int)m.data[1] << 8)|m.data[0];
|
offset = ((u_int)m.data[2] << 16)|((u_int)m.data[1] << 8)|m.data[0];
|
||||||
if (o >= utf8_big_list_used)
|
if (offset >= utf8_list_used)
|
||||||
memset(ud->data, ' ', ud->size);
|
memset(ud->data, ' ', ud->size);
|
||||||
else {
|
else {
|
||||||
bi = &utf8_big_list[o];
|
ui = &utf8_list[offset];
|
||||||
memcpy(ud->data, bi->data, ud->size);
|
memcpy(ud->data, ui->data, ud->size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get big value for UTF-8 single character. */
|
/* Get UTF-8 character from a single ASCII character. */
|
||||||
u_int
|
u_int
|
||||||
utf8_set_big(char c, u_int width)
|
utf8_build_one(char c, u_int width)
|
||||||
{
|
{
|
||||||
union utf8_big_map m = { .flags = 1, .data[0] = c };
|
union utf8_map m = { .flags = 1, .data[0] = c };
|
||||||
|
|
||||||
if (width == 2)
|
if (width == 2)
|
||||||
m.flags |= UTF8_BIG_WIDTH2;
|
m.flags |= UTF8_FLAG_WIDTH2;
|
||||||
return (m.value);
|
return (m.uc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set a single character. */
|
/* Set a single character. */
|
||||||
@@ -224,6 +228,30 @@ utf8_copy(struct utf8_data *to, const struct utf8_data *from)
|
|||||||
to->data[i] = '\0';
|
to->data[i] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Get width of Unicode character. */
|
||||||
|
static enum utf8_state
|
||||||
|
utf8_width(struct utf8_data *ud, int *width)
|
||||||
|
{
|
||||||
|
wchar_t wc;
|
||||||
|
|
||||||
|
switch (mbtowc(&wc, ud->data, ud->size)) {
|
||||||
|
case -1:
|
||||||
|
log_debug("UTF-8 %.*s, mbtowc() %d", (int)ud->size, ud->data,
|
||||||
|
errno);
|
||||||
|
mbtowc(NULL, NULL, MB_CUR_MAX);
|
||||||
|
return (UTF8_ERROR);
|
||||||
|
case 0:
|
||||||
|
return (UTF8_ERROR);
|
||||||
|
}
|
||||||
|
*width = wcwidth(wc);
|
||||||
|
if (*width < 0 || *width > 0xff) {
|
||||||
|
log_debug("UTF-8 %.*s, wcwidth() %d", (int)ud->size, ud->data,
|
||||||
|
*width);
|
||||||
|
return (UTF8_ERROR);
|
||||||
|
}
|
||||||
|
return (UTF8_DONE);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Open UTF-8 sequence.
|
* Open UTF-8 sequence.
|
||||||
*
|
*
|
||||||
@@ -251,7 +279,6 @@ utf8_open(struct utf8_data *ud, u_char ch)
|
|||||||
enum utf8_state
|
enum utf8_state
|
||||||
utf8_append(struct utf8_data *ud, u_char ch)
|
utf8_append(struct utf8_data *ud, u_char ch)
|
||||||
{
|
{
|
||||||
wchar_t wc;
|
|
||||||
int width;
|
int width;
|
||||||
|
|
||||||
if (ud->have >= ud->size)
|
if (ud->have >= ud->size)
|
||||||
@@ -268,10 +295,7 @@ utf8_append(struct utf8_data *ud, u_char ch)
|
|||||||
|
|
||||||
if (ud->width == 0xff)
|
if (ud->width == 0xff)
|
||||||
return (UTF8_ERROR);
|
return (UTF8_ERROR);
|
||||||
|
if (utf8_width(ud, &width) != UTF8_DONE)
|
||||||
if (utf8_combine(ud, &wc) != UTF8_DONE)
|
|
||||||
return (UTF8_ERROR);
|
|
||||||
if ((width = utf8_width(wc)) < 0)
|
|
||||||
return (UTF8_ERROR);
|
return (UTF8_ERROR);
|
||||||
ud->width = width;
|
ud->width = width;
|
||||||
|
|
||||||
@@ -310,50 +334,6 @@ utf8_width(wchar_t wc)
|
|||||||
return (width);
|
return (width);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Combine UTF-8 into Unicode. */
|
|
||||||
enum utf8_state
|
|
||||||
utf8_combine(const struct utf8_data *ud, wchar_t *wc)
|
|
||||||
{
|
|
||||||
#ifdef HAVE_UTF8PROC
|
|
||||||
switch (utf8proc_mbtowc(wc, ud->data, ud->size)) {
|
|
||||||
#else
|
|
||||||
switch (mbtowc(wc, ud->data, ud->size)) {
|
|
||||||
#endif
|
|
||||||
case -1:
|
|
||||||
log_debug("UTF-8 %.*s, mbtowc() %d", (int)ud->size, ud->data,
|
|
||||||
errno);
|
|
||||||
mbtowc(NULL, NULL, MB_CUR_MAX);
|
|
||||||
return (UTF8_ERROR);
|
|
||||||
case 0:
|
|
||||||
return (UTF8_ERROR);
|
|
||||||
default:
|
|
||||||
return (UTF8_DONE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Split Unicode into UTF-8. */
|
|
||||||
enum utf8_state
|
|
||||||
utf8_split(wchar_t wc, struct utf8_data *ud)
|
|
||||||
{
|
|
||||||
char s[MB_LEN_MAX];
|
|
||||||
int slen;
|
|
||||||
|
|
||||||
#ifdef HAVE_UTF8PROC
|
|
||||||
slen = utf8proc_wctomb(s, wc);
|
|
||||||
#else
|
|
||||||
slen = wctomb(s, wc);
|
|
||||||
#endif
|
|
||||||
if (slen <= 0 || slen > (int)sizeof ud->data)
|
|
||||||
return (UTF8_ERROR);
|
|
||||||
|
|
||||||
memcpy(ud->data, s, slen);
|
|
||||||
ud->size = slen;
|
|
||||||
|
|
||||||
ud->width = utf8_width(wc);
|
|
||||||
return (UTF8_DONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Encode len characters from src into dst, which is guaranteed to have four
|
* Encode len characters from src into dst, which is guaranteed to have four
|
||||||
* bytes available for each character from src (for \abc or UTF-8) plus space
|
* bytes available for each character from src (for \abc or UTF-8) plus space
|
||||||
* for \0.
|
* for \0.
|
||||||
@@ -362,13 +342,10 @@ int
|
|||||||
utf8_strvis(char *dst, const char *src, size_t len, int flag)
|
utf8_strvis(char *dst, const char *src, size_t len, int flag)
|
||||||
{
|
{
|
||||||
struct utf8_data ud;
|
struct utf8_data ud;
|
||||||
const char *start, *end;
|
const char *start = dst, *end = src + len;
|
||||||
enum utf8_state more;
|
enum utf8_state more;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
start = dst;
|
|
||||||
end = src + len;
|
|
||||||
|
|
||||||
while (src < end) {
|
while (src < end) {
|
||||||
if ((more = utf8_open(&ud, *src)) == UTF8_MORE) {
|
if ((more = utf8_open(&ud, *src)) == UTF8_MORE) {
|
||||||
while (++src < end && more == UTF8_MORE)
|
while (++src < end && more == UTF8_MORE)
|
||||||
@@ -394,7 +371,6 @@ utf8_strvis(char *dst, const char *src, size_t len, int flag)
|
|||||||
dst = vis(dst, src[0], flag, '\0');
|
dst = vis(dst, src[0], flag, '\0');
|
||||||
src++;
|
src++;
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst = '\0';
|
*dst = '\0';
|
||||||
return (dst - start);
|
return (dst - start);
|
||||||
}
|
}
|
||||||
@@ -445,15 +421,12 @@ utf8_isvalid(const char *s)
|
|||||||
char *
|
char *
|
||||||
utf8_sanitize(const char *src)
|
utf8_sanitize(const char *src)
|
||||||
{
|
{
|
||||||
char *dst;
|
char *dst = NULL;
|
||||||
size_t n;
|
size_t n = 0;
|
||||||
enum utf8_state more;
|
enum utf8_state more;
|
||||||
struct utf8_data ud;
|
struct utf8_data ud;
|
||||||
u_int i;
|
u_int i;
|
||||||
|
|
||||||
dst = NULL;
|
|
||||||
|
|
||||||
n = 0;
|
|
||||||
while (*src != '\0') {
|
while (*src != '\0') {
|
||||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||||
if ((more = utf8_open(&ud, *src)) == UTF8_MORE) {
|
if ((more = utf8_open(&ud, *src)) == UTF8_MORE) {
|
||||||
@@ -474,7 +447,6 @@ utf8_sanitize(const char *src)
|
|||||||
dst[n++] = '_';
|
dst[n++] = '_';
|
||||||
src++;
|
src++;
|
||||||
}
|
}
|
||||||
|
|
||||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||||
dst[n] = '\0';
|
dst[n] = '\0';
|
||||||
return (dst);
|
return (dst);
|
||||||
@@ -496,9 +468,8 @@ u_int
|
|||||||
utf8_strwidth(const struct utf8_data *s, ssize_t n)
|
utf8_strwidth(const struct utf8_data *s, ssize_t n)
|
||||||
{
|
{
|
||||||
ssize_t i;
|
ssize_t i;
|
||||||
u_int width;
|
u_int width = 0;
|
||||||
|
|
||||||
width = 0;
|
|
||||||
for (i = 0; s[i].size != 0; i++) {
|
for (i = 0; s[i].size != 0; i++) {
|
||||||
if (n != -1 && n == i)
|
if (n != -1 && n == i)
|
||||||
break;
|
break;
|
||||||
@@ -514,13 +485,10 @@ utf8_strwidth(const struct utf8_data *s, ssize_t n)
|
|||||||
struct utf8_data *
|
struct utf8_data *
|
||||||
utf8_fromcstr(const char *src)
|
utf8_fromcstr(const char *src)
|
||||||
{
|
{
|
||||||
struct utf8_data *dst;
|
struct utf8_data *dst = NULL;
|
||||||
size_t n;
|
size_t n = 0;
|
||||||
enum utf8_state more;
|
enum utf8_state more;
|
||||||
|
|
||||||
dst = NULL;
|
|
||||||
|
|
||||||
n = 0;
|
|
||||||
while (*src != '\0') {
|
while (*src != '\0') {
|
||||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||||
if ((more = utf8_open(&dst[n], *src)) == UTF8_MORE) {
|
if ((more = utf8_open(&dst[n], *src)) == UTF8_MORE) {
|
||||||
@@ -536,7 +504,6 @@ utf8_fromcstr(const char *src)
|
|||||||
n++;
|
n++;
|
||||||
src++;
|
src++;
|
||||||
}
|
}
|
||||||
|
|
||||||
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
dst = xreallocarray(dst, n + 1, sizeof *dst);
|
||||||
dst[n].size = 0;
|
dst[n].size = 0;
|
||||||
return (dst);
|
return (dst);
|
||||||
@@ -546,18 +513,14 @@ utf8_fromcstr(const char *src)
|
|||||||
char *
|
char *
|
||||||
utf8_tocstr(struct utf8_data *src)
|
utf8_tocstr(struct utf8_data *src)
|
||||||
{
|
{
|
||||||
char *dst;
|
char *dst = NULL;
|
||||||
size_t n;
|
size_t n = 0;
|
||||||
|
|
||||||
dst = NULL;
|
|
||||||
|
|
||||||
n = 0;
|
|
||||||
for(; src->size != 0; src++) {
|
for(; src->size != 0; src++) {
|
||||||
dst = xreallocarray(dst, n + src->size, 1);
|
dst = xreallocarray(dst, n + src->size, 1);
|
||||||
memcpy(dst + n, src->data, src->size);
|
memcpy(dst + n, src->data, src->size);
|
||||||
n += src->size;
|
n += src->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
dst = xreallocarray(dst, n + 1, 1);
|
dst = xreallocarray(dst, n + 1, 1);
|
||||||
dst[n] = '\0';
|
dst[n] = '\0';
|
||||||
return (dst);
|
return (dst);
|
||||||
|
@@ -2571,7 +2571,7 @@ window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size,
|
|||||||
return (&gce->data.data);
|
return (&gce->data.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
utf8_get_big(gl->extddata[gce->offset].data, &ud);
|
utf8_to_data(gl->extddata[gce->offset].data, &ud);
|
||||||
*size = ud.size;
|
*size = ud.size;
|
||||||
*allocated = 1;
|
*allocated = 1;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user