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
|
/*-------------------------------------------------------------------------
* C-Pluff, a plug-in framework for C
* Copyright 2007 Johannes Lehtinen
*
* 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.
*-----------------------------------------------------------------------*/
/** @file
* Plug-in information functions
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "../kazlib/hash.h"
#include "cpluff.h"
#include "defines.h"
#include "util.h"
#include "internal.h"
/* ------------------------------------------------------------------------
* Data types
* ----------------------------------------------------------------------*/
/// Registration of a dynamically allocated information object
typedef struct info_resource_t {
/// Pointer to the resource
void *resource;
/// Usage count for the resource
int usage_count;
/// Deallocation function
cpi_dealloc_func_t dealloc_func;
} info_resource_t;
/// A plug-in listener registration
typedef struct el_holder_t {
/// The plug-in listener
cp_plugin_listener_func_t plugin_listener;
/// The registering plug-in or NULL for the client program
cp_plugin_t *plugin;
/// Associated user data
void *user_data;
} el_holder_t;
/* ------------------------------------------------------------------------
* Function definitions
* ----------------------------------------------------------------------*/
// General information object management
CP_HIDDEN cp_status_t cpi_register_info(cp_context_t *context, void *res, cpi_dealloc_func_t df) {
cp_status_t status = CP_OK;
info_resource_t *ir = NULL;
assert(context != NULL);
assert(res != NULL);
assert(df != NULL);
assert(cpi_is_context_locked(context));
do {
if ((ir = malloc(sizeof(info_resource_t))) == NULL) {
status = CP_ERR_RESOURCE;
break;
}
ir->resource = res;
ir->usage_count = 1;
ir->dealloc_func = df;
if (!hash_alloc_insert(context->env->infos, res, ir)) {
status = CP_ERR_RESOURCE;
break;
}
} while (0);
// Report success
if (status == CP_OK) {
cpi_debugf(context, N_("Registered a new reference counted object at address %p."), res);
}
// Release resources on failure
if (status != CP_OK) {
if (ir != NULL) {
free(ir);
}
}
return status;
}
CP_HIDDEN void cpi_use_info(cp_context_t *context, void *res) {
hnode_t *node;
assert(context != NULL);
assert(res != NULL);
assert(cpi_is_context_locked(context));
if ((node = hash_lookup(context->env->infos, res)) != NULL) {
info_resource_t *ir = hnode_get(node);
ir->usage_count++;
cpi_debugf(context, N_("Reference count of the object at address %p increased to %d."), res, ir->usage_count);
} else {
cpi_fatalf(_("Attempt to increase the reference count of an unknown object at address %p."), res);
}
}
CP_HIDDEN void cpi_release_info(cp_context_t *context, void *info) {
hnode_t *node;
assert(context != NULL);
assert(info != NULL);
assert(cpi_is_context_locked(context));
if ((node = hash_lookup(context->env->infos, info)) != NULL) {
info_resource_t *ir = hnode_get(node);
assert(ir != NULL && info == ir->resource);
ir->usage_count--;
cpi_debugf(context, N_("Reference count of the object at address %p decreased to %d."), info, ir->usage_count);
if (ir->usage_count == 0) {
hash_delete_free(context->env->infos, node);
ir->dealloc_func(context, info);
cpi_debugf(context, N_("Deallocated the reference counted object at address %p."), info);
free(ir);
}
} else {
cpi_fatalf(_("Attempt to release an unknown reference counted object at address %p."), info);
}
}
CP_C_API void cp_release_info(cp_context_t *context, void *info) {
CHECK_NOT_NULL(context);
CHECK_NOT_NULL(info);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER, __func__);
cpi_release_info(context, info);
cpi_unlock_context(context);
}
CP_HIDDEN void cpi_release_infos(cp_context_t *context) {
hscan_t scan;
hnode_t *node;
hash_scan_begin(&scan, context->env->infos);
while ((node = hash_scan_next(&scan)) != NULL) {
info_resource_t *ir = hnode_get(node);
cpi_lock_context(context);
cpi_errorf(context, N_("An unreleased information object was encountered at address %p with reference count %d when destroying the associated plug-in context. Not releasing the object."), ir->resource, ir->usage_count);
cpi_unlock_context(context);
hash_scan_delfree(context->env->infos, node);
free(ir);
}
}
// Information acquiring functions
CP_C_API cp_plugin_info_t * cp_get_plugin_info(cp_context_t *context, const char *id, cp_status_t *error) {
hnode_t *node;
cp_plugin_info_t *plugin = NULL;
cp_status_t status = CP_OK;
CHECK_NOT_NULL(context);
if (id == NULL && context->plugin == NULL) {
cpi_fatalf(_("The plug-in identifier argument to cp_get_plugin_info must not be NULL when the main program calls it."));
}
// Look up the plug-in and return information
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER, __func__);
do {
// Lookup plug-in information
if (id != NULL) {
if ((node = hash_lookup(context->env->plugins, id)) == NULL) {
// cpi_warnf(context, N_("Could not return information about unknown plug-in %s."), id);
status = CP_ERR_UNKNOWN;
break;
}
plugin = ((cp_plugin_t *) hnode_get(node))->plugin;
} else {
plugin = context->plugin->plugin;
assert(plugin != NULL);
}
cpi_use_info(context, plugin);
} while (0);
cpi_unlock_context(context);
if (error != NULL) {
*error = status;
}
return plugin;
}
static void dealloc_plugins_info(cp_context_t *context, cp_plugin_info_t **plugins) {
int i;
assert(context != NULL);
assert(plugins != NULL);
for (i = 0; plugins[i] != NULL; i++) {
cpi_release_info(context, plugins[i]);
}
free(plugins);
}
CP_C_API cp_plugin_info_t ** cp_get_plugins_info(cp_context_t *context, cp_status_t *error, int *num) {
cp_plugin_info_t **plugins = NULL;
int i, n;
cp_status_t status = CP_OK;
CHECK_NOT_NULL(context);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER, __func__);
do {
hscan_t scan;
hnode_t *node;
// Allocate space for pointer array
n = hash_count(context->env->plugins);
if ((plugins = malloc(sizeof(cp_plugin_info_t *) * (n + 1))) == NULL) {
status = CP_ERR_RESOURCE;
break;
}
// Get plug-in information structures
hash_scan_begin(&scan, context->env->plugins);
i = 0;
while ((node = hash_scan_next(&scan)) != NULL) {
cp_plugin_t *rp = hnode_get(node);
assert(i < n);
cpi_use_info(context, rp->plugin);
plugins[i] = rp->plugin;
i++;
}
plugins[i] = NULL;
// Register the array
status = cpi_register_info(context, plugins, (void (*)(cp_context_t *, void *)) dealloc_plugins_info);
} while (0);
// Report error
if (status != CP_OK) {
cpi_error(context, N_("Plug-in information could not be returned due to insufficient memory."));
}
cpi_unlock_context(context);
// Release resources on error
if (status != CP_OK) {
if (plugins != NULL) {
dealloc_plugins_info(context, plugins);
plugins = NULL;
}
}
assert(status != CP_OK || n == 0 || plugins[n - 1] != NULL);
if (error != NULL) {
*error = status;
}
if (num != NULL && status == CP_OK) {
*num = n;
}
return plugins;
}
CP_C_API cp_plugin_state_t cp_get_plugin_state(cp_context_t *context, const char *id) {
cp_plugin_state_t state = CP_PLUGIN_UNINSTALLED;
hnode_t *hnode;
CHECK_NOT_NULL(context);
CHECK_NOT_NULL(id);
// Look up the plug-in state
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER, __func__);
if ((hnode = hash_lookup(context->env->plugins, id)) != NULL) {
cp_plugin_t *rp = hnode_get(hnode);
state = rp->state;
}
cpi_unlock_context(context);
return state;
}
static void dealloc_ext_points_info(cp_context_t *context, cp_ext_point_t **ext_points) {
int i;
assert(context != NULL);
assert(ext_points != NULL);
for (i = 0; ext_points[i] != NULL; i++) {
cpi_release_info(context, ext_points[i]->plugin);
}
free(ext_points);
}
CP_C_API cp_ext_point_t ** cp_get_ext_points_info(cp_context_t *context, cp_status_t *error, int *num) {
cp_ext_point_t **ext_points = NULL;
int i, n;
cp_status_t status = CP_OK;
CHECK_NOT_NULL(context);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER, __func__);
do {
hscan_t scan;
hnode_t *node;
// Allocate space for pointer array
n = hash_count(context->env->ext_points);
if ((ext_points = malloc(sizeof(cp_ext_point_t *) * (n + 1))) == NULL) {
status = CP_ERR_RESOURCE;
break;
}
// Get extension point information structures
hash_scan_begin(&scan, context->env->ext_points);
i = 0;
while ((node = hash_scan_next(&scan)) != NULL) {
cp_ext_point_t *ep = hnode_get(node);
assert(i < n);
cpi_use_info(context, ep->plugin);
ext_points[i] = ep;
i++;
}
ext_points[i] = NULL;
// Register the array
status = cpi_register_info(context, ext_points, (void (*)(cp_context_t *, void *)) dealloc_ext_points_info);
} while (0);
// Report error
if (status != CP_OK) {
cpi_error(context, N_("Extension point information could not be returned due to insufficient memory."));
}
cpi_unlock_context(context);
// Release resources on error
if (status != CP_OK) {
if (ext_points != NULL) {
dealloc_ext_points_info(context, ext_points);
ext_points = NULL;
}
}
assert(status != CP_OK || n == 0 || ext_points[n - 1] != NULL);
if (error != NULL) {
*error = status;
}
if (num != NULL && status == CP_OK) {
*num = n;
}
return ext_points;
}
static void dealloc_extensions_info(cp_context_t *context, cp_extension_t **extensions) {
int i;
assert(context != NULL);
assert(extensions != NULL);
for (i = 0; extensions[i] != NULL; i++) {
cpi_release_info(context, extensions[i]->plugin);
}
free(extensions);
}
CP_C_API cp_extension_t ** cp_get_extensions_info(cp_context_t *context, const char *extpt_id, cp_status_t *error, int *num) {
cp_extension_t **extensions = NULL;
int i, n;
cp_status_t status = CP_OK;
CHECK_NOT_NULL(context);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER, __func__);
do {
hscan_t scan;
hnode_t *hnode;
// Count the number of extensions
if (extpt_id != NULL) {
if ((hnode = hash_lookup(context->env->extensions, extpt_id)) != NULL) {
n = list_count((list_t *) hnode_get(hnode));
} else {
n = 0;
}
} else {
hscan_t scan;
n = 0;
hash_scan_begin(&scan, context->env->extensions);
while ((hnode = hash_scan_next(&scan)) != NULL) {
n += list_count((list_t *) hnode_get(hnode));
}
}
// Allocate space for pointer array
if ((extensions = malloc(sizeof(cp_extension_t *) * (n + 1))) == NULL) {
status = CP_ERR_RESOURCE;
break;
}
// Get extension information structures
if (extpt_id != NULL) {
i = 0;
if ((hnode = hash_lookup(context->env->extensions, extpt_id)) != NULL) {
list_t *el = hnode_get(hnode);
lnode_t *lnode;
lnode = list_first(el);
while (lnode != NULL) {
cp_extension_t *e = lnode_get(lnode);
assert(i < n);
cpi_use_info(context, e->plugin);
extensions[i] = e;
i++;
lnode = list_next(el, lnode);
}
}
extensions[i] = NULL;
} else {
hash_scan_begin(&scan, context->env->extensions);
i = 0;
while ((hnode = hash_scan_next(&scan)) != NULL) {
list_t *el = hnode_get(hnode);
lnode_t *lnode;
lnode = list_first(el);
while (lnode != NULL) {
cp_extension_t *e = lnode_get(lnode);
assert(i < n);
cpi_use_info(context, e->plugin);
extensions[i] = e;
i++;
lnode = list_next(el, lnode);
}
}
}
extensions[i] = NULL;
// Register the array
status = cpi_register_info(context, extensions, (void (*)(cp_context_t *, void *)) dealloc_extensions_info);
} while (0);
// Report error
if (status != CP_OK) {
cpi_error(context, N_("Extension information could not be returned due to insufficient memory."));
}
cpi_unlock_context(context);
// Release resources on error
if (status != CP_OK) {
if (extensions != NULL) {
dealloc_extensions_info(context, extensions);
extensions = NULL;
}
}
assert(status != CP_OK || n == 0 || extensions[n - 1] != NULL);
if (error != NULL) {
*error = status;
}
if (num != NULL && status == CP_OK) {
*num = n;
}
return extensions;
}
// Plug-in listeners
/**
* Compares plug-in listener holders.
*
* @param h1 the first holder to be compared
* @param h2 the second holder to be compared
* @return zero if the holders point to the same function, otherwise non-zero
*/
static int comp_el_holder(const void *h1, const void *h2) {
const el_holder_t *plh1 = h1;
const el_holder_t *plh2 = h2;
return (plh1->plugin_listener != plh2->plugin_listener);
}
/**
* Processes a node by delivering the specified event to the associated
* plug-in listener.
*
* @param list the list being processed
* @param node the node being processed
* @param event the event
*/
static void process_event(list_t *list, lnode_t *node, void *event) {
el_holder_t *h = lnode_get(node);
cpi_plugin_event_t *e = event;
h->plugin_listener(e->plugin_id, e->old_state, e->new_state, h->user_data);
}
/**
* Processes a node by unregistering the associated plug-in listener.
*
* @param list the list being processed
* @param node the node being processed
* @param plugin plugin whose listeners are to be unregistered or NULL for all
*/
static void process_unregister_plistener(list_t *list, lnode_t *node, void *plugin) {
el_holder_t *h = lnode_get(node);
if (plugin == NULL || h->plugin == plugin) {
list_delete(list, node);
lnode_destroy(node);
free(h);
}
}
CP_HIDDEN void cpi_unregister_plisteners(list_t *listeners, cp_plugin_t *plugin) {
list_process(listeners, plugin, process_unregister_plistener);
}
CP_C_API cp_status_t cp_register_plistener(cp_context_t *context, cp_plugin_listener_func_t listener, void *user_data) {
cp_status_t status = CP_ERR_RESOURCE;
el_holder_t *holder;
lnode_t *node;
CHECK_NOT_NULL(context);
CHECK_NOT_NULL(listener);
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER | CPI_CF_LISTENER, __func__);
if ((holder = malloc(sizeof(el_holder_t))) != NULL) {
holder->plugin_listener = listener;
holder->plugin = context->plugin;
holder->user_data = user_data;
if ((node = lnode_create(holder)) != NULL) {
list_append(context->env->plugin_listeners, node);
status = CP_OK;
} else {
free(holder);
}
}
// Report error or success
if (status != CP_OK) {
cpi_error(context, N_("A plug-in listener could not be registered due to insufficient memory."));
} else if (cpi_is_logged(context, CP_LOG_DEBUG)) {
char owner[64];
/* TRANSLATORS: %s is the context owner */
cpi_debugf(context, N_("%s registered a plug-in listener."), cpi_context_owner(context, owner, sizeof(owner)));
}
cpi_unlock_context(context);
return status;
}
CP_C_API void cp_unregister_plistener(cp_context_t *context, cp_plugin_listener_func_t listener) {
el_holder_t holder;
lnode_t *node;
CHECK_NOT_NULL(context);
holder.plugin_listener = listener;
cpi_lock_context(context);
cpi_check_invocation(context, CPI_CF_LOGGER | CPI_CF_LISTENER, __func__);
node = list_find(context->env->plugin_listeners, &holder, comp_el_holder);
if (node != NULL) {
process_unregister_plistener(context->env->plugin_listeners, node, NULL);
}
if (cpi_is_logged(context, CP_LOG_DEBUG)) {
char owner[64];
/* TRANSLATORS: %s is the context owner */
cpi_debugf(context, N_("%s unregistered a plug-in listener."), cpi_context_owner(context, owner, sizeof(owner)));
}
cpi_unlock_context(context);
}
CP_HIDDEN void cpi_deliver_event(cp_context_t *context, const cpi_plugin_event_t *event) {
assert(event != NULL);
assert(event->plugin_id != NULL);
cpi_lock_context(context);
context->env->in_event_listener_invocation++;
list_process(context->env->plugin_listeners, (void *) event, process_event);
context->env->in_event_listener_invocation--;
cpi_unlock_context(context);
if (cpi_is_logged(context, CP_LOG_INFO)) {
char *str;
switch (event->new_state) {
case CP_PLUGIN_UNINSTALLED:
str = N_("Plug-in %s has been uninstalled.");
break;
case CP_PLUGIN_INSTALLED:
if (event->old_state < CP_PLUGIN_INSTALLED) {
str = N_("Plug-in %s has been installed.");
} else {
str = N_("Plug-in %s runtime library has been unloaded.");
}
break;
case CP_PLUGIN_RESOLVED:
if (event->old_state < CP_PLUGIN_RESOLVED) {
str = N_("Plug-in %s runtime library has been loaded.");
} else {
str = N_("Plug-in %s has been stopped.");
}
break;
case CP_PLUGIN_STARTING:
str = N_("Plug-in %s is starting.");
break;
case CP_PLUGIN_STOPPING:
str = N_("Plug-in %s is stopping.");
break;
case CP_PLUGIN_ACTIVE:
str = N_("Plug-in %s has been started.");
break;
default:
str = NULL;
break;
}
if (str != NULL) {
cpi_infof(context, str, event->plugin_id);
}
}
}
// Configuration element helpers
static cp_cfg_element_t * lookup_cfg_element(cp_cfg_element_t *base, const char *path, int len) {
int start = 0;
CHECK_NOT_NULL(base);
CHECK_NOT_NULL(path);
// Traverse the path
while (base != NULL && path[start] != '\0' && (len == -1 || start < len)) {
int end = start;
while (path[end] != '\0' && path[end] != '/' && (len == -1 || end < len))
end++;
if (end - start == 2 && !strncmp(path + start, "..", 2)) {
base = base->parent;
} else {
unsigned int i;
int found = 0;
for (i = 0; !found && i < base->num_children; i++) {
cp_cfg_element_t *e = base->children + i;
if (end - start == strlen(e->name)
&& !strncmp(path + start, e->name, end - start)) {
base = e;
found = 1;
}
}
if (!found) {
base = NULL;
}
}
start = end;
if (path[start] == '/') {
start++;
}
}
return base;
}
CP_C_API cp_cfg_element_t * cp_lookup_cfg_element(cp_cfg_element_t *base, const char *path) {
return lookup_cfg_element(base, path, -1);
}
CP_C_API char * cp_lookup_cfg_value(cp_cfg_element_t *base, const char *path) {
cp_cfg_element_t *e;
const char *attr;
CHECK_NOT_NULL(base);
CHECK_NOT_NULL(path);
if ((attr = strrchr(path, '@')) == NULL) {
e = lookup_cfg_element(base, path, -1);
} else {
e = lookup_cfg_element(base, path, attr - path);
attr++;
}
if (e != NULL) {
if (attr == NULL) {
return e->value;
} else {
unsigned int i;
for (i = 0; i < e->num_atts; i++) {
if (!strcmp(attr, e->atts[2*i])) {
return e->atts[2*i + 1];
}
}
return NULL;
}
} else {
return NULL;
}
}
|