10 #define TKUTIL_RELEASE_DATE "2010-03-26"
26 #if !defined(RHASH_TBL)
27 #define RHASH_TBL(h) (RHASH(h)->tbl)
29 #if !defined(RSTRING_PTR)
30 #define RSTRING_PTR(s) (RSTRING(s)->ptr)
31 #define RSTRING_LEN(s) (RSTRING(s)->len)
33 #if !defined(RARRAY_PTR)
34 #define RARRAY_PTR(s) (RARRAY(s)->ptr)
35 #define RARRAY_LEN(s) (RARRAY(s)->len)
38 #if defined(HAVE_STRNDUP) && !defined(_GNU_SOURCE)
39 extern char *strndup(
const char* _ptr,
size_t _len);
79 #if defined(HAVE_RB_OBJ_INSTANCE_EXEC) && !defined(RUBY_VM)
91 #ifndef HAVE_RB_OBJ_INSTANCE_EXEC
119 tk_obj_untrust(
self, obj)
123 #ifdef HAVE_RB_OBJ_TAINT
126 #ifdef HAVE_RB_OBJ_UNTRUST
134 tk_eval_cmd(argc, argv,
self)
146 tk_do_callback(argc, argv,
self)
179 tk_install_cmd(argc, argv,
self)
201 tk_uninstall_cmd(
self,
cmd_id)
205 int head_len =
strlen(cmd_id_head);
212 if (strncmp(cmd_id_prefix,
213 RSTRING_PTR(cmd_id) + head_len, prefix_len) != 0) {
231 tk_fromUTF8(argc, argv,
self)
240 fromDefaultEnc_toUTF8(
str,
self)
252 fromUTF8_toDefaultEnc(
str,
self)
259 return tk_fromUTF8(1, argv,
self);
275 tk_symbolkey2str(
self,
keys)
288 static VALUE ary2list
_((VALUE, VALUE, VALUE));
289 static VALUE ary2list2
_((VALUE, VALUE, VALUE));
290 static VALUE hash2list
_((VALUE, VALUE));
291 static VALUE hash2list_enc
_((VALUE, VALUE));
292 static VALUE hash2kv
_((VALUE, VALUE, VALUE));
293 static VALUE hash2kv_enc
_((VALUE, VALUE, VALUE));
307 if (
NIL_P(sys_enc)) {
339 str_val = ary2list(val, enc_flag,
self);
344 if (!
NIL_P(str_enc)) {
359 if (
RTEST(enc_flag)) {
360 val = hash2kv_enc(val,
Qnil,
self);
362 val = hash2kv(val,
Qnil,
self);
365 for(idx2 = 0; idx2 < size2; idx2++) {
369 str_val = ary2list(val2, enc_flag,
self);
374 if (
RTEST(enc_flag)) {
375 str_val = hash2list_enc(val2,
self);
377 str_val = hash2list(val2,
self);
391 if (!
NIL_P(str_enc)) {
411 if (!
NIL_P(str_enc)) {
483 str_val = ary2list(val, enc_flag,
self);
487 if (
RTEST(enc_flag)) {
488 str_val = hash2list(val,
self);
490 str_val = hash2list_enc(val,
self);
500 if (!
NIL_P(str_val)) {
505 if (!
NIL_P(str_enc)) {
549 assoc2kv(
assoc, ary,
self)
561 for(i = 0; i <
len; i++) {
597 assoc2kv_enc(assoc, ary,
self)
609 for(i = 0; i <
len; i++) {
669 hash2kv(
hash, ary,
self)
687 push_kv_enc(key, val, args)
714 hash2kv_enc(hash, ary,
self)
732 hash2list(hash,
self)
736 return ary2list2(hash2kv(hash,
Qnil,
self),
Qfalse,
self);
741 hash2list_enc(hash,
self)
745 return ary2list2(hash2kv_enc(hash,
Qnil,
self),
Qfalse,
self);
749 tk_hash_kv(argc, argv,
self)
774 if (
RTEST(enc_flag)) {
775 return assoc2kv_enc(hash, ary,
self);
777 return assoc2kv(hash, ary,
self);
781 if (
RTEST(enc_flag)) {
782 return hash2kv_enc(hash, ary,
self);
784 return hash2kv(hash, ary,
self);
819 if (
RTEST(enc_flag)) {
823 return fromDefaultEnc_toUTF8(obj,
self);
830 if (
RTEST(enc_flag)) {
838 #ifdef HAVE_RB_SYM_TO_S
846 if (
RTEST(enc_flag)) {
847 return hash2list_enc(obj,
self);
849 return hash2list(obj,
self);
853 return ary2list(obj, enc_flag,
self);
899 rb_warning(
"fail to convert '%s' to string for Tk",
906 tk_get_eval_string(argc, argv,
self)
921 tk_get_eval_enc_str(
self, obj)
933 tk_conv_args(argc, argv,
self)
951 for(size = 0, idx = 2; idx <
argc; idx++) {
960 for(idx = 2; idx <
argc; idx++) {
962 if (
RTEST(argv[1])) {
963 hash2kv_enc(argv[idx], dst,
self);
965 hash2kv(argv[idx], dst,
self);
967 }
else if (argv[idx] !=
TK_None) {
982 tcl2rb_bool(
self,
value)
1032 tkstr_to_float(value)
1039 tkstr_invalid_numstr(value)
1043 "invalid value for Number: '%s'",
RSTRING_PTR(value));
1048 tkstr_rescue_float(value)
1052 tkstr_invalid_numstr, value,
1057 tkstr_to_number(value)
1065 tkstr_rescue_float, value,
1070 tcl2rb_number(
self, value)
1074 return tkstr_to_number(value);
1087 if (len > 1 && *ptr ==
'{' && *(ptr + len - 1) ==
'}') {
1094 tcl2rb_string(
self, value)
1102 return tkstr_to_str(value);
1106 tcl2rb_num_or_str(
self, value)
1115 tkstr_to_str, value,
1120 tcl2rb_num_or_nil(
self, value)
1128 return tkstr_to_number(value);
1134 #define CBSUBST_TBL_MAX (256)
1135 struct cbsubst_info {
1136 int full_subst_length;
1147 struct cbsubst_info *ptr;
1155 struct cbsubst_info *ptr;
1161 if (ptr->key[i] !=
NULL) {
1173 struct cbsubst_info *
inf;
1177 inf =
ALLOC(
struct cbsubst_info);
1179 inf->full_subst_length = 0;
1182 inf->keylen[
idx] = 0;
1184 inf->type[
idx] =
'\0';
1185 inf->ivar[
idx] = (
ID) 0;
1192 inf->aliases = aliases;
1194 if (inf_ptr != (
struct cbsubst_info **)
NULL) *inf_ptr =
inf;
1207 cbsubst_initialize(argc, argv,
self)
1212 struct cbsubst_info *
inf;
1216 struct cbsubst_info, inf);
1220 if ( inf->ivar[iv_idx] == (
ID) 0 )
continue;
1221 rb_ivar_set(
self, inf->ivar[iv_idx], argv[idx++]);
1222 if (idx >= argc)
break;
1229 cbsubst_ret_val(
self, val)
1240 each_attr_def(key, value, klass)
1241 VALUE key, value, klass;
1243 ID key_id, value_id;
1256 "includes invalid key(s). expected a String or a Symbol");
1264 value_id =
SYM2ID(value);
1268 "includes invalid value(s). expected a String or a Symbol");
1277 cbsubst_def_attr_aliases(
self,
tbl)
1281 struct cbsubst_info *
inf;
1288 struct cbsubst_info, inf);
1296 cbsubst_sym_to_subst(
self,
sym)
1300 struct cbsubst_info *
inf;
1310 struct cbsubst_info, inf);
1321 if (inf->ivar[idx] ==
id)
break;
1323 if (idx >= CBSUBST_TBL_MAX)
return sym;
1325 ptr = buf =
ALLOC_N(
char, inf->full_subst_length + 1);
1329 if (len = inf->keylen[idx]) {
1331 strncpy(ptr, inf->key[idx], len);
1335 *(ptr++) = (
unsigned char)
idx;
1349 cbsubst_get_subst_arg(argc, argv,
self)
1354 struct cbsubst_info *
inf;
1362 struct cbsubst_info, inf);
1364 ptr = buf =
ALLOC_N(
char, inf->full_subst_length + 1);
1366 for(i = 0; i <
argc; i++) {
1367 switch(
TYPE(argv[i])) {
1387 if (inf->ivar[idx] ==
id)
break;
1389 if (idx >= CBSUBST_TBL_MAX) {
1395 if (len = inf->keylen[idx]) {
1397 strncpy(ptr, inf->key[idx], len);
1401 *(ptr++) = (
unsigned char)
idx;
1417 cbsubst_get_subst_key(
self, str)
1421 struct cbsubst_info *
inf;
1432 struct cbsubst_info, inf);
1434 ptr = buf =
ALLOC_N(
char, inf->full_subst_length + len + 1);
1436 for(i = 0; i <
len; i++) {
1440 if (*(key + 2) ==
'\0') {
1442 *(ptr++) = *(key + 1);
1447 if (inf->keylen[idx] != keylen)
continue;
1448 if ((
unsigned char)inf->key[idx][0] != (
unsigned char)*(key + 1))
continue;
1449 if (strncmp(inf->key[idx], key + 1, keylen))
continue;
1452 if (idx < CBSUBST_TBL_MAX) {
1453 *(ptr++) = (
unsigned char)
idx;
1470 cbsubst_get_all_subst_keys(
self)
1473 struct cbsubst_info *
inf;
1480 struct cbsubst_info, inf);
1482 ptr = buf =
ALLOC_N(
char, inf->full_subst_length + 1);
1483 keys_ptr = keys_buf =
ALLOC_N(
char, CBSUBST_TBL_MAX + 1);
1486 if (inf->ivar[idx] == (
ID) 0)
continue;
1488 *(keys_ptr++) = (
unsigned char)
idx;
1492 if (len = inf->keylen[idx]) {
1494 strncpy(ptr, inf->key[idx], len);
1498 *(ptr++) = (
unsigned char)
idx;
1516 cbsubst_table_setup(argc, argv,
self)
1532 if (
rb_scan_args(argc, argv,
"21", &key_inf, &longkey_inf, &proc_inf) == 2) {
1552 for(idx = 0; idx <
len; idx++) {
1567 subst_inf->full_subst_length += 3;
1583 for(idx = 0; idx <
len; idx++) {
1587 chr = (
unsigned char)(0x80 + idx);
1594 if (subst_inf->key[chr]) {
1606 subst_inf->full_subst_length += (subst_inf->keylen[
chr] + 2);
1620 for(idx = 0; idx <
len; idx++) {
1636 cbsubst_get_extra_args_tbl(
self)
1648 struct cbsubst_info *
inf;
1665 struct cbsubst_info, inf);
1667 for(idx = 0; idx <
vallen; idx++) {
1668 if (idx >= keylen) {
1670 }
else if (*(keyptr + idx) ==
' ') {
1673 if (type_chr = inf->type[*(keyptr + idx)]) {
1695 cbsubst_inspect(
self)
1702 substinfo_inspect(
self)
1711 tk_cbe_inspect(
self)
1770 cbsubst_sym_to_subst, 1);
1772 cbsubst_get_subst_arg, -1);
1774 cbsubst_get_subst_key, 1);
1776 cbsubst_get_all_subst_keys, 0);
1778 cbsubst_table_setup, -1);
1780 cbsubst_get_extra_args_tbl, 0);
1782 cbsubst_def_attr_aliases, 1);
1831 tk_get_eval_string, -1);
1833 tk_get_eval_enc_str, 1);
VALUE rb_apply(VALUE, ID, VALUE)
Calls a method.
return tk_toUTF8(1, argv, self)
static unsigned int register const char * str
size_t strlen(const char *)
static ID ID_encoding_system
#define Data_Get_Struct(obj, type, sval)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
return rb_hash_delete(CALLBACK_TABLE, rb_str_new2(RSTRING_PTR(cmd_id)+head_len))
rb_hash_aset(CALLBACK_TABLE, id_num, cmd)
VALUE rb_ary_push(VALUE ary, VALUE item)
static const char cmd_id_head[]
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_ivar_get(VALUE, ID)
static const char cmd_id_prefix[]
static ID ID_merge_tklist
VALUE rb_convert_type(VALUE, int, const char *, const char *)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
VALUE rb_tainted_str_new2(const char *)
VALUE rb_ary_new3(long n,...)
int rb_str_cmp(VALUE, VALUE)
RUBY_EXTERN VALUE rb_cProc
rb_alias(klass, key_id, value_id)
VALUE rb_obj_untrust(VALUE)
void rb_global_variable(VALUE *var)
return rb_str_append(rb_str_new2(cmd_id_head), id_num)
VALUE rb_require(const char *)
return Data_Wrap_Struct(CLASS_OF(interp), 0, ip_free, slave)
VALUE rb_class_new_instance(int, VALUE *, VALUE)
int rb_block_given_p(void)
VALUE rb_obj_taint(VALUE)
RUBY_EXTERN VALUE rb_cObject
void rb_attr(VALUE, ID, int, int, int)
static ID ID_split_tklist
VALUE rb_str_cat2(VALUE, const char *)
static VALUE CALLBACK_TABLE
const char tkutil_release_date[]
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rb_define_const(VALUE, const char *, VALUE)
void rb_check_type(VALUE x, int t)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_obj_alloc(VALUE)
VALUE rb_const_get(VALUE, ID)
return rb_eval_cmd(cmd, rest, 0)
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
static VALUE cTkCallbackEntry
int rb_obj_respond_to(VALUE, ID, int)
static unsigned long CALLBACK_ID_NUM
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static void cbsubst_init()
rb_hash_foreach(tbl, each_attr_def, self)
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
register unsigned int len
VALUE rb_gc_disable(void)
#define RARRAY_PTR(ARRAY)
return tk_install_cmd_core(cmd)
VALUE rb_float_new(double)
static VALUE to_hash(VALUE hash)
static VALUE ULONG2NUM(unsigned long v)
VALUE rb_block_proc(void)
VALUE rb_ary_plus(VALUE x, VALUE y)
VALUE rb_hash_aref(VALUE hash, VALUE key)
static VALUE ENCODING_NAME_UTF8
rb_ivar_set(einfo, ID_at_interp, interp)
struct rb_encoding_entry * list
VALUE rb_ary_concat(VALUE x, VALUE y)
VALUE rb_ary_new2(long capa)
rb_const_set(self, ID_SUBST_INFO, cbsubst_obj)
static char NUM2CHR(VALUE x)
volatile VALUE longkey_inf
const char * rb_id2name(ID id)
return rb_funcall(q->proc, ID_call, 0)
VALUE rb_obj_instance_exec(int, VALUE *, VALUE)
VALUE rb_obj_instance_eval(int, VALUE *, VALUE)
#define TKUTIL_RELEASE_DATE
void rb_warning(const char *fmt,...)
static VALUE allocate_cbsubst_info(struct cbsubst_info **inf_ptr)
VALUE rb_obj_freeze(VALUE)
struct cbsubst_info * subst_inf
VALUE rb_define_module(const char *name)
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
return get_eval_string_core(obj, enc_flag, self)
VALUE rb_str_new2(const char *)
double rb_cstr_to_dbl(const char *, int)
VALUE rb_obj_class(VALUE)
VALUE rb_str_new(const char *, long)