evil-qemacs/charsetjis.c
Charlie Gordon e3c5bfd457
Remove references to global qe_state (#45)
* Remove references to global `qe_state`

Add `QEmacsState *qs` field in context and object structures:
- `QEditScreen`, `EditState` (renamed from `qe_state`), `QuitState`
- `WinWindow` (win32.c), `X11State` (x11.c), `QEArgs` (cmd line arguments)
- `CmdLineOptionDef::func_arg()`, `QEModeData`
- move `QEKeyContext` to `QEmacsState`

Pass `QEmacsState *qs` to configuration and state functions:
- all `module_xxx_init` and static `xxx_init` functions
- `QEDisplay::dpy_init()`, `qe_register_display()`, `screen_init()̀`
- `load_input_methods()`, `unload_input_methods()`
- `register_input_method()`, `find_input_method()`
- `find_resource_file()`, `open_resource_file()`
- `qe_find_current_binding()`, `qe_list_bindings()`
- `qe_register_mode()`, `qe_find_mode()`, `qe_find_mode_filename()`
- `qe_register_cmd_line_options()`, `set_user_option()`, `set_tty_charset()`
- `qe_register_commands()`, `qe_register_bindings()`, `qe_find_cmd()`
- `qe_register_completion()`, `qe_register_variables()`
- `eb_register_data_type()`, `parse_command_line()`
- `eb_trace_bytes()`, `qe_toggle_control_h()` (split from `do_toggle_control_h()`)
- `unget_key()`, `qe_grab_keys()`, `qe_ungrab_keys()`, `qe_key_process()`
- `eb_new()`, `eb_find()`, `eb_find_new()`, `eb_scratch()`, `eb_find_file()`
- `new_shell_buffer()` `macro_add_key()`, `clear_macro(), `stop_macro()`
- `check_window()`, `check_buffer()`, `qe_kill_buffer()`, `qe_get_history()`
- `qe_register_charset()`, `find_charset()`

Add error message handlers:
- add `QEDisplay::dpy_error()` method
- add `qe_cfg_error()`, use for all expression parsing errors
- pass opaque pointer to `css_error()`
- add `qe_put_error()` and `qe_dpy_error()`

Add context argument:
- always pass a non-null window pointer to `do_refresh()`, `new_help_buffer()`

Rename functions:
- `find_resource_file` -> `qe_find_resource_file`
- `open_resource_file` -> `qe_open_resource_file`
- `input_methods_init` -> `qe_input_methods_init`
- `load_input_methods` -> `qe_load_input_methods`
- `unload_input_methods` -> `qe_unload_input_methods`
- `find_input_methods` -> `qe_find_input_methods`
- `register_input_method` -> `qe_register_input_method`
- `parse_command_line` -> `qe_parse_command_line`
- `eb_trace_bytes` -> `qe_trace_bytes`
- `eb_init` -> `qe_data_init`
- `eb_count_buffers` -> `qe_count_buffers`
- `eb_get_buffer_from_index` -> `qe_get_buffer_from_index`
- `init_all_modules` -> `qe_init_all_modules`
- `exit_all_modules` -> `qe_exit_all_modules`
- `load_all_modules` -> `qe_load_all_modules`
- `maybe_set_mark` -> `do_maybe_set_mark`
- `unget_key` -> `qe_unget_key`
- `minibuffer_init` -> `qe_minibuffer_init`
- `edit_display` -> `qe_display`
- `new_yank_buffer` -> `qe_new_yank_buffer`
- `new_shell_buffer` -> `qe_new_shell_buffer`
- `shell_find_mode` -> `qe_shell_find_mode`
- `save_selection` -> `qe_save_selection`
- `register_emulation_bindings` -> `qe_register_emulation_bindings`
- `macro_add_key` -> `qe_macro_add_key`
- `clear_macro` -> `qe_clear_macro`
- `stop_macro` -> `qe_stop_macro`
- `eb_format_message` -> `qe_format_message`
- `screen_init` -> `qe_screen_init`
- `charset_jis_init` -> `qe_charset_jis_init`
- `charset_more_init` -> `qe_charset_more_init`
- `check_window` -> `qe_check_window`
- `check_buffer` -> `qe_check_buffer`
- `eb_find_file` -> `qe_find_buffer_file`
- `eb_find` -> `qe_find_buffer_name`
- `eb_new` -> `qe_new_buffer`
- `eb_find_new` -> `qe_new_buffer` with `BC_REUSE`
- `eb_scratch` -> `qe_new_buffer` with `BC_REUSE | BC_CLEAR`
- `find_charset` -> `qe_find_charset`

Add asan, msan and ubsan targets
Fix `qe_encode64` buffer length computation.
Add `qe_new_shell_buffer` flags `SF_REUSE_BUFFER` and `SF_ERASE_BUFFER`
Bumped version to 6.3.3
fix redundant typedef
fix compilation errors
2024-12-01 23:34:00 +01:00

254 lines
6 KiB
C

/*
* JIS Charset handling for QEmacs
*
* Copyright (c) 2002 Fabrice Bellard.
* Copyright (c) 2002-2024 Charlie Gordon.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qe.h"
#include "charsetjis.def"
static char32_t jis0208_decode(int b1, int b2) {
b1 -= 0x21;
b2 -= 0x21;
if (b1 > 83)
return 0;
if (b1 < 8) {
/* do nothing */
} else
if (b1 <= 14) {
return 0;
} else {
b1 -= 7;
}
return table_jis208[b1 * 94 + b2];
}
static char32_t jis0212_decode(int b1, int b2) {
b1 -= 0x21;
b2 -= 0x21;
if (b1 > 76)
return 0;
switch (b1) {
case 0:
case 2:
case 3:
case 4:
case 7:
case 11:
case 12:
case 13:
case 14:
return 0;
case 1:
b1 = 0;
break;
case 5:
case 6:
b1 = b1 - 5 + 1;
break;
case 8:
case 9:
case 10:
b1 = b1 - 8 + 3;
break;
default:
b1 = b1 - 15 + 6;
break;
}
return table_jis212[b1 * 94 + b2];
}
static void decode_euc_jp_init(CharsetDecodeState *s)
{
/* XXX: should use static table instead of removing const qualifier */
unsigned short *table = unconst(unsigned short *)s->table;
int i;
for (i = 0; i < 256; i++)
table[i] = i;
table[0x8e] = ESCAPE_CHAR;
table[0x8f] = ESCAPE_CHAR;
for (i = 0xa1; i <= 0xfe; i++)
table[i] = ESCAPE_CHAR;
}
/* XXX: add state */
static char32_t decode_euc_jp_func(CharsetDecodeState *s) {
const unsigned char *p;
char32_t c, c2;
p = s->p;
c = *p++;
if (c == 0x8e) {
c = *p;
if (c >= 0xa1 && c <= 0xdf) {
/* 2 byte sequence for HALFWIDTH KANA FF61..FF9F */
c = c - 0xa1 + 0xff61;
p++;
}
} else
if (c >= 0xa1) {
c2 = *p;
if (c2 >= 0xa1 && c2 <= 0xfe) {
/* 2 byte sequence for 77x94 KANJI block */
c2 = jis0208_decode(c & 0x7f, c2 & 0x7f);
if (c2) {
c = c2;
p++;
}
}
} else {
/* 8f case */
if (p[0] >= 0xa1 && p[0] <= 0xfe &&
p[1] >= 0xa1 && p[1] <= 0xfe) {
/* 3 byte sequence for 68x94 KANJI block */
c2 = jis0212_decode(p[0] & 0x7f, p[1] & 0x7f);
if (c2) {
c = c2;
p += 2;
}
}
}
s->p = p;
return c;
}
static unsigned char *encode_euc_jp(qe__unused__ QECharset *s, u8 *q, char32_t c) {
if (c <= 0x7f) {
*q++ = c;
} else
if (c >= 0xff61 && c <= 0xff9f) {
*q++ = 0x8e;
*q++ = c - 0xff61 + 0xa1;
} else {
/* XXX: do it */
return NULL;
}
return q;
}
static struct QECharset charset_euc_jp = {
"euc-jp",
NULL,
NULL,
decode_euc_jp_init,
decode_euc_jp_func,
encode_euc_jp,
charset_get_pos_8bit,
charset_get_chars_8bit,
charset_goto_char_8bit,
charset_goto_line_8bit,
.char_size = 1,
.variable_size = 1,
.table_alloc = 1,
.eol_char = 10,
};
static void decode_sjis_init(CharsetDecodeState *s)
{
/* XXX: should use static table instead of removing const qualifier */
unsigned short *table = unconst(unsigned short *)s->table;
int i;
for (i = 0; i < 256; i++)
table[i] = i;
table['\\'] = 0x00a5;
table[0x80] = '\\';
for (i = 0x81; i <= 0x9f; i++)
table[i] = ESCAPE_CHAR;
for (i = 0xa1; i <= 0xdf; i++)
table[i] = i - 0xa1 + 0xff61;
for (i = 0xe0; i <= 0xef; i++)
table[i] = ESCAPE_CHAR;
for (i = 0xf0; i <= 0xfc; i++)
table[i] = ESCAPE_CHAR;
table[0xfd] = 0xa9;
table[0xfe] = 0x2122;
table[0xff] = 0x2026;
}
/* XXX: add state */
static char32_t decode_sjis_func(CharsetDecodeState *s) {
const unsigned char *p;
char32_t c, c1, c2, adjust, row, col;
p = s->p;
c = *p++;
if (c >= 0xf0) {
/* user data */
} else {
c1 = *p;
if ((c1 >= 0x40 && c1 <= 0x7e) || (c1 >= 0x80 && c1 <= 0xfc)) {
row = c < 0xa0 ? 0x70 : 0xb0;
adjust = (c1 < 0x9f);
col = adjust ? (c1 >= 0x80 ? 32 : 31) : 0x7e;
c2 = jis0208_decode(((c - row) << 1) - adjust, c1 - col);
if (c2) {
c = c2;
p++;
}
}
}
s->p = p;
return c;
}
static unsigned char *encode_sjis(qe__unused__ QECharset *s, u8 *q, char32_t c) {
if (c <= 0x7f) {
*q++ = c;
} else {
/* XXX: do it */
return NULL;
}
return q;
}
static struct QECharset charset_sjis = {
"sjis",
NULL,
NULL,
decode_sjis_init,
decode_sjis_func,
encode_sjis,
charset_get_pos_8bit,
charset_get_chars_8bit,
charset_goto_char_8bit,
charset_goto_line_8bit,
.char_size = 1,
.variable_size = 1,
.table_alloc = 1,
.eol_char = 10,
};
int qe_charset_jis_init(QEmacsState *qs)
{
qe_register_charset(qs, &charset_sjis);
qe_register_charset(qs, &charset_euc_jp);
return 0;
}
qe_module_init(qe_charset_jis_init);