1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
|
/*
* wee-hdata.c - direct access to WeeChat data using hashtables
*
* Copyright (C) 2011-2018 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "weechat.h"
#include "wee-hdata.h"
#include "wee-eval.h"
#include "wee-hashtable.h"
#include "wee-log.h"
#include "wee-string.h"
#include "../plugins/plugin.h"
struct t_hashtable *weechat_hdata = NULL;
/* hashtables used in hdata_search() for evaluating expression */
struct t_hashtable *hdata_search_pointers = NULL;
struct t_hashtable *hdata_search_extra_vars = NULL;
struct t_hashtable *hdata_search_options = NULL;
char *hdata_type_string[9] =
{ "other", "char", "integer", "long", "string", "pointer", "time",
"hashtable", "shared_string" };
/*
* Frees a hdata variable.
*/
void
hdata_free_var (struct t_hashtable *hashtable,
const void *key, void *value)
{
struct t_hdata_var *var;
/* make C compiler happy */
(void) hashtable;
(void) key;
var = (struct t_hdata_var *)value;
if (var)
{
if (var->array_size)
free (var->array_size);
if (var->hdata_name)
free (var->hdata_name);
free (var);
}
}
/*
* Frees a hdata list.
*/
void
hdata_free_list (struct t_hashtable *hashtable,
const void *key, void *value)
{
/* make C compiler happy */
(void) hashtable;
(void) key;
free (value);
}
/*
* Creates a new hdata.
*
* Returns pointer to new hdata, NULL if error.
*/
struct t_hdata *
hdata_new (struct t_weechat_plugin *plugin, const char *hdata_name,
const char *var_prev, const char *var_next,
int create_allowed, int delete_allowed,
int (*callback_update)(void *data,
struct t_hdata *hdata,
void *pointer,
struct t_hashtable *hashtable),
void *callback_update_data)
{
struct t_hdata *new_hdata;
if (!hdata_name || !hdata_name[0])
return NULL;
new_hdata = malloc (sizeof (*new_hdata));
if (new_hdata)
{
new_hdata->name = strdup (hdata_name);
new_hdata->plugin = plugin;
new_hdata->var_prev = (var_prev) ? strdup (var_prev) : NULL;
new_hdata->var_next = (var_next) ? strdup (var_next) : NULL;
new_hdata->hash_var = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
NULL);
new_hdata->hash_var->callback_free_value = &hdata_free_var;
new_hdata->hash_list = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
NULL);
new_hdata->hash_list->callback_free_value = &hdata_free_list;
hashtable_set (weechat_hdata, hdata_name, new_hdata);
new_hdata->create_allowed = create_allowed;
new_hdata->delete_allowed = delete_allowed;
new_hdata->callback_update = callback_update;
new_hdata->callback_update_data = callback_update_data;
new_hdata->update_pending = 0;
}
return new_hdata;
}
/*
* Adds a new variable in a hdata.
*/
void
hdata_new_var (struct t_hdata *hdata, const char *name, int offset, int type,
int update_allowed, const char *array_size,
const char *hdata_name)
{
struct t_hdata_var *var;
if (!hdata || !name)
return;
var = malloc (sizeof (*var));
if (var)
{
var->offset = offset;
var->type = type;
var->update_allowed = update_allowed;
var->array_size = (array_size && array_size[0]) ? strdup (array_size) : NULL;
var->hdata_name = (hdata_name && hdata_name[0]) ? strdup (hdata_name) : NULL;
hashtable_set (hdata->hash_var, name, var);
}
}
/*
* Adds a new list pointer in a hdata.
*/
void
hdata_new_list (struct t_hdata *hdata, const char *name, void *pointer,
int flags)
{
struct t_hdata_list *list;
if (!hdata || !name)
return;
list = malloc (sizeof (*list));
if (list)
{
list->pointer = pointer;
list->flags = flags;
hashtable_set (hdata->hash_list, name, list);
}
}
/*
* Gets offset of variable in hdata.
*/
int
hdata_get_var_offset (struct t_hdata *hdata, const char *name)
{
struct t_hdata_var *var;
if (!hdata || !name)
return -1;
var = hashtable_get (hdata->hash_var, name);
if (var)
return var->offset;
return -1;
}
/*
* Gets type of variable in hdata (as integer).
*/
int
hdata_get_var_type (struct t_hdata *hdata, const char *name)
{
struct t_hdata_var *var;
if (!hdata || !name)
return -1;
var = hashtable_get (hdata->hash_var, name);
if (var)
return var->type;
return -1;
}
/*
* Gets type of variable in hdata (as string).
*/
const char *
hdata_get_var_type_string (struct t_hdata *hdata, const char *name)
{
struct t_hdata_var *var;
if (!hdata || !name)
return NULL;
var = hashtable_get (hdata->hash_var, name);
if (var)
return hdata_type_string[(int)var->type];
return NULL;
}
/*
* Gets size of array for a variable (if variable is an array).
*
* Returns size of array, -1 if variable is not an array (or if error).
*/
int
hdata_get_var_array_size (struct t_hdata *hdata, void *pointer,
const char *name)
{
struct t_hdata_var *var;
const char *ptr_size;
char *error;
long value;
int i, offset;
void *ptr_value;
if (!hdata || !name)
return -1;
var = hashtable_get (hdata->hash_var, name);
if (!var)
return -1;
ptr_size = var->array_size;
if (!ptr_size)
return -1;
if (strcmp (ptr_size, "*") == 0)
{
/*
* automatic size: look for NULL in array
* (this automatic size is possible only with pointers, so with
* types: string, pointer, hashtable)
*/
if ((var->type == WEECHAT_HDATA_STRING)
|| (var->type == WEECHAT_HDATA_SHARED_STRING)
|| (var->type == WEECHAT_HDATA_POINTER)
|| (var->type == WEECHAT_HDATA_HASHTABLE))
{
if (!(*((void **)(pointer + var->offset))))
return 0;
i = 0;
while (1)
{
ptr_value = NULL;
switch (var->type)
{
case WEECHAT_HDATA_STRING:
case WEECHAT_HDATA_SHARED_STRING:
ptr_value = (*((char ***)(pointer + var->offset)))[i];
break;
case WEECHAT_HDATA_POINTER:
ptr_value = (*((void ***)(pointer + var->offset)))[i];
break;
case WEECHAT_HDATA_HASHTABLE:
ptr_value = (*((struct t_hashtable ***)(pointer + var->offset)))[i];
break;
}
if (!ptr_value)
break;
i++;
}
return i;
}
}
else
{
/* fixed size: the size can be a name of variable or integer */
offset = hdata_get_var_offset (hdata, ptr_size);
if (offset >= 0)
{
/* size is the name of a variable in hdata, read it */
switch (hdata_get_var_type (hdata, ptr_size))
{
case WEECHAT_HDATA_CHAR:
return (int)(*((char *)(pointer + offset)));
case WEECHAT_HDATA_INTEGER:
return *((int *)(pointer + offset));
case WEECHAT_HDATA_LONG:
return (int)(*((long *)(pointer + offset)));
default:
break;
}
}
else
{
/* check if the size is a valid integer */
error = NULL;
value = strtol (ptr_size, &error, 10);
if (error && !error[0])
return (int)value;
}
}
return -1;
}
/*
* Gets size of array for variable as string.
*/
const char *
hdata_get_var_array_size_string (struct t_hdata *hdata, void *pointer,
const char *name)
{
struct t_hdata_var *var;
/* make C compiler happy */
(void) pointer;
if (!hdata || !name)
return NULL;
var = hashtable_get (hdata->hash_var, name);
if (var)
return (const char *)var->array_size;
return NULL;
}
/*
* Gets hdata name for a variable.
*
* Returns hdata name, NULL if variable has no hdata.
*/
const char *
hdata_get_var_hdata (struct t_hdata *hdata, const char *name)
{
struct t_hdata_var *var;
if (!hdata || !name)
return NULL;
var = hashtable_get (hdata->hash_var, name);
if (var)
return (const char *)var->hdata_name;
return NULL;
}
/*
* Gets pointer to content of variable using hdata variable name.
*/
void *
hdata_get_var (struct t_hdata *hdata, void *pointer, const char *name)
{
int offset;
if (!hdata || !pointer)
return NULL;
offset = hdata_get_var_offset (hdata, name);
if (offset >= 0)
return pointer + offset;
return NULL;
}
/*
* Gets pointer to content of variable using hdata variable offset.
*/
void *
hdata_get_var_at_offset (struct t_hdata *hdata, void *pointer, int offset)
{
if (!hdata || !pointer)
return NULL;
return pointer + offset;
}
/*
* Gets a list pointer in hdata.
*/
void *
hdata_get_list (struct t_hdata *hdata, const char *name)
{
struct t_hdata_list *ptr_list;
if (!hdata || !name)
return NULL;
ptr_list = hashtable_get (hdata->hash_list, name);
if (ptr_list)
return *((void **)(ptr_list->pointer));
return NULL;
}
/*
* Checks if a pointer is in the list.
*
* Returns:
* 1: pointer exists in list
* 0: pointer does not exist
*/
int
hdata_check_pointer_in_list (struct t_hdata *hdata, void *list, void *pointer)
{
void *ptr_current;
if (!hdata || !pointer)
return 0;
if (pointer == list)
return 1;
ptr_current = list;
while (ptr_current)
{
ptr_current = hdata_move (hdata, ptr_current, 1);
if (ptr_current && (ptr_current == pointer))
return 1;
}
return 0;
}
/*
* Checks if a pointer is in a list with flag "check_pointers".
*/
void
hdata_check_pointer_map_cb (void *data, struct t_hashtable *hashtable,
const void *key, const void *value)
{
void **pointers, *pointer, **num_lists, **found;
struct t_hdata *ptr_hdata;
struct t_hdata_list *ptr_list;
/* make C compiler happy */
(void) hashtable;
(void) key;
pointers = (void **)data;
ptr_hdata = pointers[0];
pointer = pointers[1];
num_lists = &pointers[2];
found = &pointers[3];
/* pointer already found in another list? just exit */
if (*found)
return;
ptr_list = (struct t_hdata_list *)value;
if (!ptr_list || !(ptr_list->flags & WEECHAT_HDATA_LIST_CHECK_POINTERS))
return;
*found = (void *)((unsigned long)hdata_check_pointer_in_list (
ptr_hdata,
*((void **)(ptr_list->pointer)),
pointer));
(*num_lists)++;
}
/*
* Checks if a pointer is valid for a given hdata/list.
*
* If argument "list" is NULL, the check is made with all lists in hdata
* that have flag "check_pointers". If no list is defined with this flag,
* the pointer is considered valid (so this function returns 1); if the
* pointer is not found in any list, this function returns 0.
*
* Returns:
* 1: pointer exists in the given list (or a list with check_pointers flag)
* 0: pointer does not exist
*/
int
hdata_check_pointer (struct t_hdata *hdata, void *list, void *pointer)
{
void *pointers[4];
if (!hdata || !pointer)
return 0;
if (list)
{
/* search pointer in the given list */
return hdata_check_pointer_in_list (hdata, list, pointer);
}
else
{
/* search pointer in all lists with flag "check_pointers" */
pointers[0] = hdata;
pointers[1] = pointer;
pointers[2] = 0; /* number of lists with flag check_pointers */
pointers[3] = 0; /* pointer found? (0/1) */
hashtable_map (hdata->hash_list,
&hdata_check_pointer_map_cb,
pointers);
return ((pointers[2] == 0) || pointers[3]) ? 1 : 0;
}
}
/*
* Moves pointer to another element in list.
*/
void *
hdata_move (struct t_hdata *hdata, void *pointer, int count)
{
char *ptr_var;
int i, abs_count;
if (!hdata || !pointer || (count == 0))
return NULL;
ptr_var = (count < 0) ? hdata->var_prev : hdata->var_next;
abs_count = abs (count);
for (i = 0; i < abs_count; i++)
{
pointer = hdata_pointer (hdata, pointer, ptr_var);
if (!pointer)
break;
}
return pointer;
}
/*
* Searches for an element in list using expression.
*
* Returns pointer to element found, NULL if not found.
*/
void *
hdata_search (struct t_hdata *hdata, void *pointer, const char *search, int move)
{
char *result;
int rc;
if (!hdata || !pointer || !search || !search[0] || (move == 0))
return NULL;
/* clear or create hashtable with pointer for search */
if (hdata_search_pointers)
{
hashtable_remove_all (hdata_search_pointers);
}
else
{
hdata_search_pointers = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
NULL);
}
/*
* create hashtable with extra vars (empty hashtable)
* (hashtable would be created in eval_expression(), but it's created here
* so it will not be created for each call to eval_expression())
*/
if (!hdata_search_extra_vars)
{
hdata_search_extra_vars = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
}
if (!hdata_search_options)
{
hdata_search_options = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (hdata_search_options)
hashtable_set (hdata_search_options, "type", "condition");
}
while (pointer)
{
/* set pointer in hashtable (used for evaluating expression) */
hashtable_set (hdata_search_pointers, hdata->name, pointer);
/* evaluate expression */
result = eval_expression (search, hdata_search_pointers,
hdata_search_extra_vars,
hdata_search_options);
rc = eval_is_true (result);
if (result)
free (result);
if (rc)
return pointer;
pointer = hdata_move (hdata, pointer, move);
}
return NULL;
}
/*
* Extracts index from name of a variable.
*
* A name can contain index with this format: "NNN|name" (where NNN is an
* integer >= 0).
* Argument "index" is set to N, "ptr_name" points to start of name in string
* (after the "|" if found).
*/
void
hdata_get_index_and_name (const char *name, int *index, const char **ptr_name)
{
char *pos, *str_index, *error;
long number;
if (index)
*index = -1;
if (ptr_name)
*ptr_name = name;
if (!name)
return;
pos = strchr (name, '|');
if (pos)
{
str_index = string_strndup (name, pos - name);
if (str_index)
{
error = NULL;
number = strtol (str_index, &error, 10);
if (error && !error[0])
{
if (index)
*index = number;
if (ptr_name)
*ptr_name = pos + 1;
}
free (str_index);
}
}
}
/*
* Gets char value of a variable in hdata.
*/
char
hdata_char (struct t_hdata *hdata, void *pointer, const char *name)
{
int index;
const char *ptr_name;
struct t_hdata_var *var;
if (!hdata || !pointer || !name)
return '\0';
hdata_get_index_and_name (name, &index, &ptr_name);
var = hashtable_get (hdata->hash_var, ptr_name);
if (var && (var->offset >= 0))
{
if (var->array_size && (index >= 0))
return (*((char **)(pointer + var->offset)))[index];
else
return *((char *)(pointer + var->offset));
}
return '\0';
}
/*
* Gets integer value of a variable in hdata.
*/
int
hdata_integer (struct t_hdata *hdata, void *pointer, const char *name)
{
int index;
const char *ptr_name;
struct t_hdata_var *var;
if (!hdata || !pointer || !name)
return 0;
hdata_get_index_and_name (name, &index, &ptr_name);
var = hashtable_get (hdata->hash_var, ptr_name);
if (var && (var->offset >= 0))
{
if (var->array_size && (index >= 0))
return ((int *)(pointer + var->offset))[index];
else
return *((int *)(pointer + var->offset));
}
return 0;
}
/*
* Gets long value of a variable in hdata.
*/
long
hdata_long (struct t_hdata *hdata, void *pointer, const char *name)
{
int index;
const char *ptr_name;
struct t_hdata_var *var;
if (!hdata || !pointer || !name)
return 0;
hdata_get_index_and_name (name, &index, &ptr_name);
var = hashtable_get (hdata->hash_var, ptr_name);
if (var && (var->offset >= 0))
{
if (var->array_size && (index >= 0))
return ((long *)(pointer + var->offset))[index];
else
return *((long *)(pointer + var->offset));
}
return 0;
}
/*
* Gets string value of a variable in hdata.
*/
const char *
hdata_string (struct t_hdata *hdata, void *pointer, const char *name)
{
int index;
const char *ptr_name;
struct t_hdata_var *var;
if (!hdata || !pointer || !name)
return NULL;
hdata_get_index_and_name (name, &index, &ptr_name);
var = hashtable_get (hdata->hash_var, ptr_name);
if (var && (var->offset >= 0))
{
if (var->array_size && (index >= 0))
return (*((char ***)(pointer + var->offset)))[index];
else
return *((char **)(pointer + var->offset));
}
return NULL;
}
/*
* Gets pointer value of a variable in hdata.
*/
void *
hdata_pointer (struct t_hdata *hdata, void *pointer, const char *name)
{
int index;
const char *ptr_name;
struct t_hdata_var *var;
if (!hdata || !pointer || !name)
return NULL;
hdata_get_index_and_name (name, &index, &ptr_name);
var = hashtable_get (hdata->hash_var, ptr_name);
if (var && (var->offset >= 0))
{
if (var->array_size && (index >= 0))
return (*((void ***)(pointer + var->offset)))[index];
else
return *((void **)(pointer + var->offset));
}
return NULL;
}
/*
* Gets time value of a variable in hdata.
*/
time_t
hdata_time (struct t_hdata *hdata, void *pointer, const char *name)
{
int index;
const char *ptr_name;
struct t_hdata_var *var;
if (!hdata || !pointer || !name)
return 0;
hdata_get_index_and_name (name, &index, &ptr_name);
var = hashtable_get (hdata->hash_var, ptr_name);
if (var && (var->offset >= 0))
{
if (var->array_size && (index >= 0))
return ((time_t *)(pointer + var->offset))[index];
else
return *((time_t *)(pointer + var->offset));
}
return 0;
}
/*
* Gets hashtable value of a variable in hdata.
*/
struct t_hashtable *
hdata_hashtable (struct t_hdata *hdata, void *pointer, const char *name)
{
int index;
const char *ptr_name;
struct t_hdata_var *var;
if (!hdata || !pointer || !name)
return NULL;
hdata_get_index_and_name (name, &index, &ptr_name);
var = hashtable_get (hdata->hash_var, ptr_name);
if (var && (var->offset >= 0))
{
if (var->array_size && (index >= 0))
return (*((struct t_hashtable ***)(pointer + var->offset)))[index];
else
return *((struct t_hashtable **)(pointer + var->offset));
}
return NULL;
}
/*
* Compares a hdata variable of two objects.
*
* If case_sensitive == 1, the comparison of strings is case sensitive.
*
* Returns:
* -1: variable1 < variable2
* 0: variable1 == variable2
* 1: variable1 > variable2
*/
int
hdata_compare (struct t_hdata *hdata, void *pointer1, void *pointer2,
const char *name, int case_sensitive)
{
int rc, int_value1, int_value2;
long long_value1, long_value2;
char char_value1, char_value2;
const char *ptr_name, *str_value1, *str_value2;
void *ptr_value1, *ptr_value2;
time_t time_value1, time_value2;
if (!pointer1 && pointer2)
return -1;
if (pointer1 && !pointer2)
return 1;
if (!pointer1 && !pointer2)
return 0;
rc = 0;
hdata_get_index_and_name (name, NULL, &ptr_name);
switch (hdata_get_var_type (hdata, ptr_name))
{
case WEECHAT_HDATA_CHAR:
char_value1 = hdata_char (hdata, pointer1, name);
char_value2 = hdata_char (hdata, pointer2, name);
rc = (char_value1 < char_value2) ?
-1 : ((char_value1 > char_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_INTEGER:
int_value1 = hdata_integer (hdata, pointer1, name);
int_value2 = hdata_integer (hdata, pointer2, name);
rc = (int_value1 < int_value2) ?
-1 : ((int_value1 > int_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_LONG:
long_value1 = hdata_long (hdata, pointer1, name);
long_value2 = hdata_long (hdata, pointer2, name);
rc = (long_value1 < long_value2) ?
-1 : ((long_value1 > long_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_STRING:
case WEECHAT_HDATA_SHARED_STRING:
str_value1 = hdata_string (hdata, pointer1, name);
str_value2 = hdata_string (hdata, pointer2, name);
if (!str_value1 && !str_value2)
rc = 0;
else if (str_value1 && !str_value2)
rc = 1;
else if (!str_value1 && str_value2)
rc = -1;
else
{
if (case_sensitive)
rc = strcmp (str_value1, str_value2);
else
rc = string_strcasecmp (str_value1, str_value2);
if (rc < 0)
rc = -1;
else if (rc > 0)
rc = 1;
}
break;
case WEECHAT_HDATA_POINTER:
ptr_value1 = hdata_pointer (hdata, pointer1, name);
ptr_value2 = hdata_pointer (hdata, pointer2, name);
rc = (ptr_value1 < ptr_value2) ?
-1 : ((ptr_value1 > ptr_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_TIME:
time_value1 = hdata_time (hdata, pointer1, name);
time_value2 = hdata_time (hdata, pointer2, name);
rc = (time_value1 < time_value2) ?
-1 : ((time_value1 > time_value2) ? 1 : 0);
break;
case WEECHAT_HDATA_HASHTABLE:
/* no comparison for hashtables */
rc = 0;
break;
case WEECHAT_HDATA_OTHER:
/* no comparison for other types */
rc = 0;
break;
}
return rc;
}
/*
* Sets value for a variable in hdata.
*
* WARNING: this is dangerous, and only some variables can be set by this
* function (this depends on hdata, see API doc for more info) and this
* function can be called *ONLY* in an "update" callback (in hdata).
*
* Returns:
* 1: OK (value set)
* 0: error (or not allowed)
*/
int
hdata_set (struct t_hdata *hdata, void *pointer, const char *name,
const char *value)
{
struct t_hdata_var *var;
char **ptr_string, *error;
long number;
unsigned long ptr;
int rc;
if (!hdata->update_pending)
return 0;
var = hashtable_get (hdata->hash_var, name);
if (!var)
return 0;
if (!var->update_allowed)
return 0;
switch (var->type)
{
case WEECHAT_HDATA_OTHER:
break;
case WEECHAT_HDATA_CHAR:
*((char *)(pointer + var->offset)) = (value) ? value[0] : '\0';
return 1;
break;
case WEECHAT_HDATA_INTEGER:
error = NULL;
number = strtol (value, &error, 10);
if (error && !error[0])
{
*((int *)(pointer + var->offset)) = (int)number;
return 1;
}
break;
case WEECHAT_HDATA_LONG:
error = NULL;
number = strtol (value, &error, 10);
if (error && !error[0])
{
*((long *)(pointer + var->offset)) = number;
return 1;
}
break;
case WEECHAT_HDATA_STRING:
ptr_string = (char **)(pointer + var->offset);
if (*ptr_string)
free (*ptr_string);
*ptr_string = (value) ? strdup (value) : NULL;
return 1;
break;
case WEECHAT_HDATA_SHARED_STRING:
ptr_string = (char **)(pointer + var->offset);
if (*ptr_string)
string_shared_free (*ptr_string);
*ptr_string = (value) ? (char *)string_shared_get (value) : NULL;
return 1;
break;
case WEECHAT_HDATA_POINTER:
rc = sscanf (value, "%lx", &ptr);
if ((rc != EOF) && (rc != 0))
{
*((void **)(pointer + var->offset)) = (void *)ptr;
return 1;
}
break;
case WEECHAT_HDATA_TIME:
error = NULL;
number = strtol (value, &error, 10);
if (error && !error[0])
{
*((time_t *)(pointer + var->offset)) = (time_t)number;
return 1;
}
break;
case WEECHAT_HDATA_HASHTABLE:
break;
}
return 0;
}
/*
* Updates some data in hdata.
*
* The hashtable contains keys with new values.
* A special key "__delete" can be used to delete the whole structure at
* pointer (if allowed for this hdata).
*
* WARNING: this is dangerous, and only some data can be updated by this
* function (this depends on hdata, see API doc for more info).
*
* Returns number of variables updated, 0 if nothing has been updated.
* In case of deletion, returns 1 if OK, 0 if error.
*/
int
hdata_update (struct t_hdata *hdata, void *pointer,
struct t_hashtable *hashtable)
{
const char *value;
struct t_hdata_var *var;
int rc;
if (!hdata || !hashtable || !hdata->callback_update)
return 0;
/* check if create of structure is allowed */
if (hashtable_has_key (hashtable, "__create_allowed"))
return (int)hdata->create_allowed;
/* check if delete of structure is allowed */
if (hashtable_has_key (hashtable, "__delete_allowed"))
return (int)hdata->delete_allowed;
/* check if update of variable is allowed */
value = hashtable_get (hashtable, "__update_allowed");
if (value)
{
if (!hdata->callback_update)
return 0;
var = hashtable_get (hdata->hash_var, value);
if (!var)
return 0;
return (var->update_allowed) ? 1 : 0;
}
/* update data */
hdata->update_pending = 1;
rc = (hdata->callback_update) (hdata->callback_update_data,
hdata, pointer, hashtable);
hdata->update_pending = 0;
return rc;
}
/*
* Gets a hdata property as string.
*/
const char *
hdata_get_string (struct t_hdata *hdata, const char *property)
{
if (!hdata || !property)
return NULL;
if (string_strcasecmp (property, "var_keys") == 0)
return hashtable_get_string (hdata->hash_var, "keys");
else if (string_strcasecmp (property, "var_values") == 0)
return hashtable_get_string (hdata->hash_var, "values");
else if (string_strcasecmp (property, "var_keys_values") == 0)
return hashtable_get_string (hdata->hash_var, "keys_values");
else if (string_strcasecmp (property, "var_prev") == 0)
return hdata->var_prev;
else if (string_strcasecmp (property, "var_next") == 0)
return hdata->var_next;
else if (string_strcasecmp (property, "list_keys") == 0)
return hashtable_get_string (hdata->hash_list, "keys");
else if (string_strcasecmp (property, "list_values") == 0)
return hashtable_get_string (hdata->hash_list, "values");
else if (string_strcasecmp (property, "list_keys_values") == 0)
return hashtable_get_string (hdata->hash_list, "keys_values");
return NULL;
}
/*
* Frees a hdata.
*/
void
hdata_free (struct t_hdata *hdata)
{
if (!hdata)
return;
if (hdata->hash_var)
hashtable_free (hdata->hash_var);
if (hdata->var_prev)
free (hdata->var_prev);
if (hdata->var_next)
free (hdata->var_next);
if (hdata->hash_list)
hashtable_free (hdata->hash_list);
if (hdata->name)
free (hdata->name);
free (hdata);
}
/*
* Frees hdata for a plugin (callback called for each hdata in memory).
*/
void
hdata_free_all_plugin_map_cb (void *data, struct t_hashtable *hashtable,
const void *key, const void *value)
{
struct t_hdata *ptr_hdata;
ptr_hdata = (struct t_hdata *)value;
if (ptr_hdata->plugin == (struct t_weechat_plugin *)data)
{
hdata_free (ptr_hdata);
hashtable_remove (hashtable, key);
}
}
/*
* Frees all hdata created by a plugin.
*/
void
hdata_free_all_plugin (struct t_weechat_plugin *plugin)
{
hashtable_map (weechat_hdata, &hdata_free_all_plugin_map_cb, plugin);
}
/*
* Frees hdata (callback called for each hdata in memory).
*/
void
hdata_free_all_map_cb (void *data, struct t_hashtable *hashtable,
const void *key, const void *value)
{
struct t_hdata *ptr_hdata;
/* make C compiler happy */
(void) data;
ptr_hdata = (struct t_hdata *)value;
hdata_free (ptr_hdata);
hashtable_remove (hashtable, key);
}
/*
* Frees all hdata.
*/
void
hdata_free_all ()
{
hashtable_map (weechat_hdata, &hdata_free_all_map_cb, NULL);
}
/*
* Prints variable of a hdata in WeeChat log file (callback called for each
* variable in hdata).
*/
void
hdata_print_log_var_map_cb (void *data, struct t_hashtable *hashtable,
const void *key, const void *value)
{
struct t_hdata_var *var;
/* make C compiler happy */
(void) data;
(void) hashtable;
var = (struct t_hdata_var *)value;
log_printf ("");
log_printf (" [hdata var '%s']", (const char *)key);
log_printf (" offset . . . . . . . . : %d", var->offset);
log_printf (" type . . . . . . . . . : %d ('%s')", var->type, hdata_type_string[(int)var->type]);
log_printf (" update_allowed . . . . : %d", (int)var->update_allowed);
log_printf (" array_size . . . . . . : '%s'", var->array_size);
log_printf (" hdata_name . . . . . . : '%s'", var->hdata_name);
}
/*
* Prints hdata in WeeChat log file (callback called for each hdata in memory).
*/
void
hdata_print_log_map_cb (void *data, struct t_hashtable *hashtable,
const void *key, const void *value)
{
struct t_hdata *ptr_hdata;
/* make C compiler happy */
(void) data;
(void) hashtable;
(void) key;
ptr_hdata = (struct t_hdata *)value;
log_printf ("");
log_printf ("[hdata (addr:0x%lx)]", ptr_hdata);
log_printf (" name . . . . . . . . . : '%s'", ptr_hdata->name);
log_printf (" plugin . . . . . . . . : 0x%lx", ptr_hdata->plugin);
log_printf (" var_prev . . . . . . . : '%s'", ptr_hdata->var_prev);
log_printf (" var_next . . . . . . . : '%s'", ptr_hdata->var_next);
log_printf (" hash_var . . . . . . . : 0x%lx (hashtable: '%s')",
ptr_hdata->hash_var,
hashtable_get_string (ptr_hdata->hash_var, "keys_values"));
log_printf (" hash_list. . . . . . . : 0x%lx (hashtable: '%s')",
ptr_hdata->hash_list,
hashtable_get_string (ptr_hdata->hash_list, "keys_values"));
log_printf (" create_allowed . . . . : %d", (int)ptr_hdata->create_allowed);
log_printf (" delete_allowed . . . . : %d", (int)ptr_hdata->delete_allowed);
log_printf (" callback_update. . . . : 0x%lx", ptr_hdata->callback_update);
log_printf (" callback_update_data . : 0x%lx", ptr_hdata->callback_update_data);
log_printf (" update_pending . . . . : %d", (int)ptr_hdata->update_pending);
hashtable_map (ptr_hdata->hash_var, &hdata_print_log_var_map_cb, NULL);
}
/*
* Prints hdata in WeeChat log file (usually for crash dump).
*/
void
hdata_print_log ()
{
hashtable_map (weechat_hdata, &hdata_print_log_map_cb, NULL);
}
/*
* Initializes hdata: creates a hashtable with hdata.
*/
void
hdata_init ()
{
weechat_hdata = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER,
NULL,
NULL);
}
/*
* Frees all hdata and hashtable with hdata.
*/
void
hdata_end ()
{
hdata_free_all ();
hashtable_free (weechat_hdata);
weechat_hdata = NULL;
if (hdata_search_pointers)
{
hashtable_free (hdata_search_pointers);
hdata_search_pointers = NULL;
}
if (hdata_search_extra_vars)
{
hashtable_free (hdata_search_extra_vars);
hdata_search_extra_vars = NULL;
}
if (hdata_search_options)
{
hashtable_free (hdata_search_options);
hdata_search_options = NULL;
}
}
|