-
Notifications
You must be signed in to change notification settings - Fork 587
/
plugins.c
2037 lines (1667 loc) · 59.6 KB
/
plugins.c
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
/*
* plugins.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2007 The Geany contributors
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Code to manage, load and unload plugins. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_PLUGINS
#include "plugins.h"
#include "app.h"
#include "dialogs.h"
#include "documentprivate.h"
#include "encodings.h"
#include "geanyobject.h"
#include "geanywraplabel.h"
#include "highlighting.h"
#include "keybindingsprivate.h"
#include "keyfile.h"
#include "main.h"
#include "msgwindow.h"
#include "navqueue.h"
#include "plugindata.h"
#include "pluginprivate.h"
#include "pluginutils.h"
#include "prefs.h"
#include "sciwrappers.h"
#include "stash.h"
#include "support.h"
#include "symbols.h"
#include "templates.h"
#include "toolbar.h"
#include "ui_utils.h"
#include "utils.h"
#include "win32.h"
#include <gtk/gtk.h>
#include <string.h>
typedef struct
{
gchar *prefix;
GeanyDocument *document;
}
ForEachDocData;
GList *active_plugin_list = NULL; /* list of only actually loaded plugins, always valid */
static gboolean want_plugins = FALSE;
/* list of all available, loadable plugins, only valid as long as the plugin manager dialog is
* opened, afterwards it will be destroyed */
static GList *plugin_list = NULL;
static gchar **active_plugins_pref = NULL; /* list of plugin filenames to load at startup */
static GList *failed_plugins_list = NULL; /* plugins the user wants active but can't be used */
static GtkWidget *menu_separator = NULL;
static gchar *get_plugin_path(void);
static void pm_show_dialog(GtkMenuItem *menuitem, gpointer user_data);
typedef struct {
gchar extension[8];
Plugin *plugin; /* &builtin_so_proxy_plugin for native plugins */
} PluginProxy;
static gpointer plugin_load_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, const gchar *filename, gpointer pdata);
static void plugin_unload_gmodule(GeanyPlugin *proxy, GeanyPlugin *plugin, gpointer load_data, gpointer pdata);
static Plugin builtin_so_proxy_plugin = {
.proxy_cbs = {
.load = plugin_load_gmodule,
.unload = plugin_unload_gmodule,
},
/* rest of Plugin can be NULL/0 */
};
static PluginProxy builtin_so_proxy = {
.extension = G_MODULE_SUFFIX,
.plugin = &builtin_so_proxy_plugin,
};
static GQueue active_proxies = G_QUEUE_INIT;
static void plugin_free(Plugin *plugin);
static GeanyData geany_data;
static void
geany_data_init(void)
{
GeanyData gd = {
app,
&main_widgets,
documents_array,
filetypes_array,
&prefs,
&interface_prefs,
&toolbar_prefs,
&editor_prefs,
&file_prefs,
&search_prefs,
&tool_prefs,
&template_prefs,
NULL, /* Remove field on next ABI break (abi-todo) */
filetypes_by_title,
geany_object,
};
geany_data = gd;
}
/* In order to have nested proxies work the count of dependent plugins must propagate up.
* This prevents that any plugin in the tree is unloaded while a leaf plugin is active. */
static void proxied_count_inc(Plugin *proxy)
{
do
{
proxy->proxied_count += 1;
proxy = proxy->proxy;
} while (proxy != NULL);
}
static void proxied_count_dec(Plugin *proxy)
{
g_warn_if_fail(proxy->proxied_count > 0);
do
{
proxy->proxied_count -= 1;
proxy = proxy->proxy;
} while (proxy != NULL);
}
/* Prevent the same plugin filename being loaded more than once.
* Note: g_module_name always returns the .so name, even when Plugin::filename is a .la file. */
static gboolean
plugin_loaded(Plugin *plugin)
{
gchar *basename_module, *basename_loaded;
GList *item;
basename_module = g_path_get_basename(plugin->filename);
for (item = plugin_list; item != NULL; item = g_list_next(item))
{
basename_loaded = g_path_get_basename(((Plugin*)item->data)->filename);
if (utils_str_equal(basename_module, basename_loaded))
{
g_free(basename_loaded);
g_free(basename_module);
return TRUE;
}
g_free(basename_loaded);
}
/* Look also through the list of active plugins. This prevents problems when we have the same
* plugin in libdir/geany/ AND in configdir/plugins/ and the one in libdir/geany/ is loaded
* as active plugin. The plugin manager list would only take the one in configdir/geany/ and
* the plugin manager would list both plugins. Additionally, unloading the active plugin
* would cause a crash. */
for (item = active_plugin_list; item != NULL; item = g_list_next(item))
{
basename_loaded = g_path_get_basename(((Plugin*)item->data)->filename);
if (utils_str_equal(basename_module, basename_loaded))
{
g_free(basename_loaded);
g_free(basename_module);
return TRUE;
}
g_free(basename_loaded);
}
g_free(basename_module);
return FALSE;
}
static Plugin *find_active_plugin_by_name(const gchar *filename)
{
GList *item;
g_return_val_if_fail(filename, FALSE);
for (item = active_plugin_list; item != NULL; item = g_list_next(item))
{
if (utils_str_equal(filename, ((Plugin*)item->data)->filename))
return item->data;
}
return NULL;
}
/* Mimics plugin_version_check() of legacy plugins for use with plugin_check_version() below */
#define PLUGIN_VERSION_CODE(api, abi) ((abi) != GEANY_ABI_VERSION ? -1 : (api))
static gboolean
plugin_check_version(Plugin *plugin, int plugin_version_code)
{
gboolean ret = TRUE;
if (plugin_version_code < 0)
{
gchar *name = g_path_get_basename(plugin->filename);
msgwin_status_add(_("The plugin \"%s\" is not binary compatible with this "
"release of Geany - please recompile it."), name);
geany_debug("Plugin \"%s\" is not binary compatible with this "
"release of Geany - recompile it.", name);
ret = FALSE;
g_free(name);
}
else if (plugin_version_code > GEANY_API_VERSION)
{
gchar *name = g_path_get_basename(plugin->filename);
geany_debug("Plugin \"%s\" requires a newer version of Geany (API >= v%d).",
name, plugin_version_code);
ret = FALSE;
g_free(name);
}
return ret;
}
static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
{
PluginCallback *cb;
guint i, len = 0;
while (TRUE)
{
cb = &callbacks[len];
if (!cb->signal_name || !cb->callback)
break;
len++;
}
if (len == 0)
return;
for (i = 0; i < len; i++)
{
cb = &callbacks[i];
/* Pass the callback data as default user_data if none was set by the plugin itself */
plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
cb->callback, cb->user_data ? cb->user_data : plugin->cb_data);
}
}
static gint cmp_plugin_names(gconstpointer a, gconstpointer b)
{
const Plugin *pa = a;
const Plugin *pb = b;
return strcmp(pa->info.name, pb->info.name);
}
/** Register a plugin to Geany.
*
* The plugin will show up in the plugin manager. The user can interact with
* it based on the functions it provides and installed GUI elements.
*
* You must initialize the info and funcs fields of @ref GeanyPlugin
* appropriately prior to calling this, otherwise registration will fail. For
* info at least a valid name must be set (possibly localized). For funcs,
* at least init() and cleanup() functions must be implemented and set.
*
* The return value must be checked. It may be FALSE if the plugin failed to register which can
* mainly happen for two reasons (future Geany versions may add new failure conditions):
* - Not all mandatory fields of GeanyPlugin have been set.
* - The ABI or API versions reported by the plugin are incompatible with the running Geany.
*
* Do not call this directly. Use GEANY_PLUGIN_REGISTER() instead which automatically
* handles @a api_version and @a abi_version.
*
* @param plugin The plugin provided by Geany
* @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION)
* @param min_api_version The minimum API version required by the plugin
* @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION)
*
* @return TRUE if the plugin was successfully registered. Otherwise FALSE.
*
* @since 1.26 (API 225)
* @see GEANY_PLUGIN_REGISTER()
**/
GEANY_API_SYMBOL
gboolean geany_plugin_register(GeanyPlugin *plugin, gint api_version, gint min_api_version,
gint abi_version)
{
Plugin *p;
GeanyPluginFuncs *cbs = plugin->funcs;
g_return_val_if_fail(plugin != NULL, FALSE);
p = plugin->priv;
/* already registered successfully */
g_return_val_if_fail(!PLUGIN_LOADED_OK(p), FALSE);
/* Prevent registering incompatible plugins. */
if (! plugin_check_version(p, PLUGIN_VERSION_CODE(api_version, abi_version)))
return FALSE;
/* Only init and cleanup callbacks are truly mandatory. */
if (! cbs->init || ! cbs->cleanup)
{
gchar *name = g_path_get_basename(p->filename);
geany_debug("Plugin '%s' has no %s function - ignoring plugin!", name,
cbs->init ? "cleanup" : "init");
g_free(name);
}
else
{
/* Yes, name is checked again later on, however we want return FALSE here
* to signal the error back to the plugin (but we don't print the message twice) */
if (! EMPTY(p->info.name))
p->flags = LOADED_OK;
}
/* If it ever becomes necessary we can save the api version in Plugin
* and apply compat code on a per-plugin basis, because we learn about
* the requested API version here. For now it's not necessary. */
return PLUGIN_LOADED_OK(p);
}
/** Register a plugin to Geany, with plugin-defined data.
*
* This is a variant of geany_plugin_register() that also allows to set the plugin-defined data.
* Refer to that function for more details on registering in general.
*
* @p pdata is the pointer going to be passed to the individual plugin callbacks
* of GeanyPlugin::funcs. When the plugin module is unloaded, @p free_func is invoked on
* @p pdata, which connects the data to the plugin's module life time.
*
* You cannot use geany_plugin_set_data() after registering with this function. Use
* geany_plugin_register() if you need to.
*
* Do not call this directly. Use GEANY_PLUGIN_REGISTER_FULL() instead which automatically
* handles @p api_version and @p abi_version.
*
* @param plugin The plugin provided by Geany.
* @param api_version The API version the plugin is compiled against (pass GEANY_API_VERSION).
* @param min_api_version The minimum API version required by the plugin.
* @param abi_version The exact ABI version the plugin is compiled against (pass GEANY_ABI_VERSION).
* @param pdata Pointer to the plugin-defined data. Must not be @c NULL.
* @param free_func Function used to deallocate @a pdata, may be @c NULL.
*
* @return TRUE if the plugin was successfully registered. Otherwise FALSE.
*
* @since 1.26 (API 225)
* @see GEANY_PLUGIN_REGISTER_FULL()
* @see geany_plugin_register()
**/
GEANY_API_SYMBOL
gboolean geany_plugin_register_full(GeanyPlugin *plugin, gint api_version, gint min_api_version,
gint abi_version, gpointer pdata, GDestroyNotify free_func)
{
if (geany_plugin_register(plugin, api_version, min_api_version, abi_version))
{
geany_plugin_set_data(plugin, pdata, free_func);
/* We use LOAD_DATA to indicate that pdata cb_data was set during loading/registration
* as opposed to during GeanyPluginFuncs::init(). In the latter case we call free_func
* after GeanyPluginFuncs::cleanup() */
plugin->priv->flags |= LOAD_DATA;
return TRUE;
}
return FALSE;
}
struct LegacyRealFuncs
{
void (*init) (GeanyData *data);
GtkWidget* (*configure) (GtkDialog *dialog);
void (*help) (void);
void (*cleanup) (void);
};
/* Wrappers to support legacy plugins are below */
static gboolean legacy_init(GeanyPlugin *plugin, gpointer pdata)
{
struct LegacyRealFuncs *h = pdata;
h->init(plugin->geany_data);
return TRUE;
}
static void legacy_cleanup(GeanyPlugin *plugin, gpointer pdata)
{
struct LegacyRealFuncs *h = pdata;
/* Can be NULL because it's optional for legacy plugins */
if (h->cleanup)
h->cleanup();
}
static void legacy_help(GeanyPlugin *plugin, gpointer pdata)
{
struct LegacyRealFuncs *h = pdata;
h->help();
}
static GtkWidget *legacy_configure(GeanyPlugin *plugin, GtkDialog *parent, gpointer pdata)
{
struct LegacyRealFuncs *h = pdata;
return h->configure(parent);
}
static void free_legacy_cbs(gpointer data)
{
g_slice_free(struct LegacyRealFuncs, data);
}
/* This function is the equivalent of geany_plugin_register() for legacy-style
* plugins which we continue to load for the time being. */
static void register_legacy_plugin(Plugin *plugin, GModule *module)
{
gint (*p_version_check) (gint abi_version);
void (*p_set_info) (PluginInfo *info);
void (*p_init) (GeanyData *geany_data);
GeanyData **p_geany_data;
struct LegacyRealFuncs *h;
#define CHECK_FUNC(__x) \
if (! g_module_symbol(module, "plugin_" #__x, (void *) (&p_##__x))) \
{ \
geany_debug("Plugin \"%s\" has no plugin_" #__x "() function - ignoring plugin!", \
g_module_name(module)); \
return; \
}
CHECK_FUNC(version_check);
CHECK_FUNC(set_info);
CHECK_FUNC(init);
#undef CHECK_FUNC
/* We must verify the version first. If the plugin has become incompatible any
* further actions should be considered invalid and therefore skipped. */
if (! plugin_check_version(plugin, p_version_check(GEANY_ABI_VERSION)))
return;
h = g_slice_new(struct LegacyRealFuncs);
/* Since the version check passed we can proceed with setting basic fields and
* calling its set_info() (which might want to call Geany functions already). */
g_module_symbol(module, "geany_data", (void *) &p_geany_data);
if (p_geany_data)
*p_geany_data = &geany_data;
/* Read plugin name, etc. name is mandatory but that's enforced in the common code. */
p_set_info(&plugin->info);
/* If all went well we can set the remaining callbacks and let it go for good. */
h->init = p_init;
g_module_symbol(module, "plugin_configure", (void *) &h->configure);
g_module_symbol(module, "plugin_configure_single", (void *) &plugin->configure_single);
g_module_symbol(module, "plugin_help", (void *) &h->help);
g_module_symbol(module, "plugin_cleanup", (void *) &h->cleanup);
/* pointer to callbacks struct can be stored directly, no wrapper necessary */
g_module_symbol(module, "plugin_callbacks", (void *) &plugin->cbs.callbacks);
if (app->debug_mode)
{
if (h->configure && plugin->configure_single)
g_warning("Plugin '%s' implements plugin_configure_single() unnecessarily - "
"only plugin_configure() will be used!",
plugin->info.name);
if (h->cleanup == NULL)
g_warning("Plugin '%s' has no plugin_cleanup() function - there may be memory leaks!",
plugin->info.name);
}
plugin->cbs.init = legacy_init;
plugin->cbs.cleanup = legacy_cleanup;
plugin->cbs.configure = h->configure ? legacy_configure : NULL;
plugin->cbs.help = h->help ? legacy_help : NULL;
plugin->flags = LOADED_OK | IS_LEGACY;
geany_plugin_set_data(&plugin->public, h, free_legacy_cbs);
}
static gboolean
plugin_load(Plugin *plugin)
{
gboolean init_ok = TRUE;
/* Start the plugin. Legacy plugins require additional cruft. */
if (PLUGIN_IS_LEGACY(plugin) && plugin->proxy == &builtin_so_proxy_plugin)
{
GeanyPlugin **p_geany_plugin;
PluginInfo **p_info;
GModule *module = plugin->proxy_data;
/* set these symbols before plugin_init() is called
* we don't set geany_data since it is set directly by plugin_new() */
g_module_symbol(module, "geany_plugin", (void *) &p_geany_plugin);
if (p_geany_plugin)
*p_geany_plugin = &plugin->public;
g_module_symbol(module, "plugin_info", (void *) &p_info);
if (p_info)
*p_info = &plugin->info;
/* Legacy plugin_init() cannot fail. */
plugin->cbs.init(&plugin->public, plugin->cb_data);
}
else
{
init_ok = plugin->cbs.init(&plugin->public, plugin->cb_data);
}
if (! init_ok)
return FALSE;
/* new-style plugins set their callbacks in geany_load_module() */
if (plugin->cbs.callbacks)
add_callbacks(plugin, plugin->cbs.callbacks);
/* remember which plugins are active.
* keep list sorted so tools menu items and plugin preference tabs are
* sorted by plugin name */
active_plugin_list = g_list_insert_sorted(active_plugin_list, plugin, cmp_plugin_names);
proxied_count_inc(plugin->proxy);
geany_debug("Loaded: %s (%s)", plugin->filename, plugin->info.name);
return TRUE;
}
static gpointer plugin_load_gmodule(GeanyPlugin *proxy, GeanyPlugin *subplugin, const gchar *fname, gpointer pdata)
{
GModule *module;
void (*p_geany_load_module)(GeanyPlugin *);
g_return_val_if_fail(g_module_supported(), NULL);
/* Don't use G_MODULE_BIND_LAZY otherwise we can get unresolved symbols at runtime,
* causing a segfault. Without that flag the module will safely fail to load.
* G_MODULE_BIND_LOCAL also helps find undefined symbols e.g. app when it would
* otherwise not be detected due to the shadowing of Geany's app variable.
* Also without G_MODULE_BIND_LOCAL calling public functions e.g. the old info()
* function from a plugin will be shadowed. */
module = g_module_open(fname, G_MODULE_BIND_LOCAL);
if (!module)
{
geany_debug("Can't load plugin: %s", g_module_error());
return NULL;
}
/*geany_debug("Initializing plugin '%s'", plugin->info.name);*/
g_module_symbol(module, "geany_load_module", (void *) &p_geany_load_module);
if (p_geany_load_module)
{
/* set this here already so plugins can call i.e. plugin_module_make_resident()
* right from their geany_load_module() */
subplugin->priv->proxy_data = module;
/* This is a new style plugin. It should fill in plugin->info and then call
* geany_plugin_register() in its geany_load_module() to successfully load.
* The ABI and API checks are performed by geany_plugin_register() (i.e. by us).
* We check the LOADED_OK flag separately to protect us against buggy plugins
* who ignore the result of geany_plugin_register() and register anyway */
p_geany_load_module(subplugin);
}
else
{
/* This is the legacy / deprecated code path. It does roughly the same as
* geany_load_module() and geany_plugin_register() together for the new ones */
register_legacy_plugin(subplugin->priv, module);
}
/* We actually check the LOADED_OK flag later */
return module;
}
static void plugin_unload_gmodule(GeanyPlugin *proxy, GeanyPlugin *subplugin, gpointer load_data, gpointer pdata)
{
GModule *module = (GModule *) load_data;
g_return_if_fail(module != NULL);
if (! g_module_close(module))
g_warning("%s: %s", subplugin->priv->filename, g_module_error());
}
/* Load and optionally init a plugin.
* load_plugin decides whether the plugin's plugin_init() function should be called or not. If it is
* called, the plugin will be started, if not the plugin will be read only (for the list of
* available plugins in the plugin manager).
* When add_to_list is set, the plugin will be added to the plugin manager's plugin_list. */
static Plugin*
plugin_new(Plugin *proxy, const gchar *fname, gboolean load_plugin, gboolean add_to_list)
{
Plugin *plugin;
g_return_val_if_fail(fname, NULL);
g_return_val_if_fail(proxy, NULL);
/* find the plugin in the list of already loaded, active plugins and use it, otherwise
* load the module */
plugin = find_active_plugin_by_name(fname);
if (plugin != NULL)
{
geany_debug("Plugin \"%s\" already loaded.", fname);
if (add_to_list)
{
/* do not add to the list twice */
if (g_list_find(plugin_list, plugin) != NULL)
return NULL;
plugin_list = g_list_prepend(plugin_list, plugin);
}
return plugin;
}
plugin = g_new0(Plugin, 1);
plugin->filename = g_strdup(fname);
plugin->proxy = proxy;
plugin->public.geany_data = &geany_data;
plugin->public.priv = plugin;
/* Fields of plugin->info/funcs must to be initialized by the plugin */
plugin->public.info = &plugin->info;
plugin->public.funcs = &plugin->cbs;
plugin->public.proxy_funcs = &plugin->proxy_cbs;
if (plugin_loaded(plugin))
{
geany_debug("Plugin \"%s\" already loaded.", fname);
goto err;
}
/* Load plugin, this should read its name etc. It must also call
* geany_plugin_register() for the following PLUGIN_LOADED_OK condition */
plugin->proxy_data = proxy->proxy_cbs.load(&proxy->public, &plugin->public, fname, proxy->cb_data);
if (! PLUGIN_LOADED_OK(plugin))
{
geany_debug("Failed to load \"%s\" - ignoring plugin!", fname);
goto err;
}
/* The proxy assumes success, therefore we have to call unload from here
* on in case of errors */
if (EMPTY(plugin->info.name))
{
geany_debug("No plugin name set for \"%s\" - ignoring plugin!", fname);
goto err_unload;
}
/* cb_data_destroy() frees plugin->cb_data. If that pointer also passed to unload() afterwards
* then that would become a use-after-free. Disallow this combination. If a proxy
* needs the same pointer it must not use a destroy func but free manually in its unload(). */
if (plugin->proxy_data == proxy->cb_data && plugin->cb_data_destroy)
{
geany_debug("Proxy of plugin \"%s\" specified invalid data - ignoring plugin!", fname);
plugin->proxy_data = NULL;
goto err_unload;
}
if (load_plugin && !plugin_load(plugin))
{
/* Handle failing init same as failing to load for now. In future we
* could present a informational UI or something */
geany_debug("Plugin failed to initialize \"%s\" - ignoring plugin!", fname);
goto err_unload;
}
if (add_to_list)
plugin_list = g_list_prepend(plugin_list, plugin);
return plugin;
err_unload:
if (plugin->cb_data_destroy)
plugin->cb_data_destroy(plugin->cb_data);
proxy->proxy_cbs.unload(&proxy->public, &plugin->public, plugin->proxy_data, proxy->cb_data);
err:
g_free(plugin->filename);
g_free(plugin);
return NULL;
}
static void on_object_weak_notify(gpointer data, GObject *old_ptr)
{
Plugin *plugin = data;
guint i = 0;
g_return_if_fail(plugin && plugin->signal_ids);
for (i = 0; i < plugin->signal_ids->len; i++)
{
SignalConnection *sc = &g_array_index(plugin->signal_ids, SignalConnection, i);
if (sc->object == old_ptr)
{
g_array_remove_index_fast(plugin->signal_ids, i);
/* we can break the loop right after finding the first match,
* because we will get one notification per connected signal */
break;
}
}
}
/* add an object to watch for destruction, and release pointers to it when destroyed.
* this should only be used by plugin_signal_connect() to add a watch on
* the object lifetime and nuke out references to it in plugin->signal_ids */
void plugin_watch_object(Plugin *plugin, gpointer object)
{
g_object_weak_ref(object, on_object_weak_notify, plugin);
}
static void remove_callbacks(Plugin *plugin)
{
GArray *signal_ids = plugin->signal_ids;
SignalConnection *sc;
if (signal_ids == NULL)
return;
foreach_array(SignalConnection, sc, signal_ids)
{
g_signal_handler_disconnect(sc->object, sc->handler_id);
g_object_weak_unref(sc->object, on_object_weak_notify, plugin);
}
g_array_free(signal_ids, TRUE);
}
static void remove_sources(Plugin *plugin)
{
GList *item;
item = plugin->sources;
while (item != NULL)
{
GList *next = item->next; /* cache the next pointer because current item will be freed */
g_source_destroy(item->data);
item = next;
}
/* don't free the list here, it is allocated inside each source's data */
}
/* Make the GModule backing plugin resident (if it's GModule-backed at all) */
void plugin_make_resident(Plugin *plugin)
{
if (plugin->proxy == &builtin_so_proxy_plugin)
{
g_return_if_fail(plugin->proxy_data != NULL);
g_module_make_resident(plugin->proxy_data);
}
else
g_warning("Skipping g_module_make_resident() for non-native plugin");
}
/* Retrieve the address of a symbol sym located in plugin, if it's GModule-backed */
gpointer plugin_get_module_symbol(Plugin *plugin, const gchar *sym)
{
gpointer symbol;
if (plugin->proxy == &builtin_so_proxy_plugin)
{
g_return_val_if_fail(plugin->proxy_data != NULL, NULL);
if (g_module_symbol(plugin->proxy_data, sym, &symbol))
return symbol;
else
g_warning("Failed to locate signal handler for '%s': %s",
sym, g_module_error());
}
else /* TODO: Could possibly support this via a new proxy hook */
g_warning("Failed to locate signal handler for '%s': Not supported for non-native plugins",
sym);
return NULL;
}
static gboolean is_active_plugin(Plugin *plugin)
{
return (g_list_find(active_plugin_list, plugin) != NULL);
}
static void remove_each_doc_data(GQuark key_id, gpointer data, gpointer user_data)
{
const ForEachDocData *doc_data = user_data;
const gchar *key = g_quark_to_string(key_id);
if (g_str_has_prefix(key, doc_data->prefix))
g_datalist_remove_data(&doc_data->document->priv->data, key);
}
static void remove_doc_data(Plugin *plugin)
{
ForEachDocData data;
data.prefix = g_strdup_printf("geany/plugins/%s/", plugin->public.info->name);
for (guint i = 0; i < documents_array->len; i++)
{
GeanyDocument *doc = documents_array->pdata[i];
if (DOC_VALID(doc))
{
data.document = doc;
g_datalist_foreach(&doc->priv->data, remove_each_doc_data, &data);
}
}
g_free(data.prefix);
}
/* Clean up anything used by an active plugin */
static void
plugin_cleanup(Plugin *plugin)
{
GtkWidget *widget;
/* With geany_register_plugin cleanup is mandatory */
plugin->cbs.cleanup(&plugin->public, plugin->cb_data);
remove_doc_data(plugin);
remove_callbacks(plugin);
remove_sources(plugin);
if (plugin->key_group)
keybindings_free_group(plugin->key_group);
widget = plugin->toolbar_separator.widget;
if (widget)
gtk_widget_destroy(widget);
if (!PLUGIN_HAS_LOAD_DATA(plugin) && plugin->cb_data_destroy)
{
/* If the plugin has used geany_plugin_set_data(), destroy the data here. But don't
* if it was already set through geany_plugin_register_full() because we couldn't call
* its init() anymore (not without completely reloading it anyway). */
plugin->cb_data_destroy(plugin->cb_data);
plugin->cb_data = NULL;
plugin->cb_data_destroy = NULL;
}
proxied_count_dec(plugin->proxy);
geany_debug("Unloaded: %s", plugin->filename);
}
/* Remove all plugins that proxy is a proxy for from plugin_list (and free) */
static void free_subplugins(Plugin *proxy)
{
GList *item;
item = plugin_list;
while (item)
{
GList *next = g_list_next(item);
if (proxy == ((Plugin *) item->data)->proxy)
{
/* plugin_free modifies plugin_list */
plugin_free((Plugin *) item->data);
}
item = next;
}
}
/* Returns true if the removal was successful (=> never for non-proxies) */
static gboolean unregister_proxy(Plugin *proxy)
{
gboolean is_proxy = FALSE;
GList *node;
/* Remove the proxy from the proxy list first. It might appear more than once (once
* for each extension), but if it doesn't appear at all it's not actually a proxy */
foreach_list_safe(node, active_proxies.head)
{
PluginProxy *p = node->data;
if (p->plugin == proxy)
{
is_proxy = TRUE;
g_queue_delete_link(&active_proxies, node);
}
}
return is_proxy;
}
/* Cleanup a plugin and free all resources allocated on behalf of it.
*
* If the plugin is a proxy then this also takes special care to unload all
* subplugin loaded through it (make sure none of them is active!) */
static void
plugin_free(Plugin *plugin)
{
Plugin *proxy;
g_return_if_fail(plugin);
g_return_if_fail(plugin->proxy);
g_return_if_fail(plugin->proxied_count == 0);
proxy = plugin->proxy;
/* If this a proxy remove all depending subplugins. We can assume none of them is *activated*
* (but potentially loaded). Note that free_subplugins() might call us through recursion */
if (is_active_plugin(plugin))
{
if (unregister_proxy(plugin))
free_subplugins(plugin);
plugin_cleanup(plugin);
}
active_plugin_list = g_list_remove(active_plugin_list, plugin);
plugin_list = g_list_remove(plugin_list, plugin);
/* cb_data_destroy might be plugin code and must be called before unloading the module. */
if (plugin->cb_data_destroy)
plugin->cb_data_destroy(plugin->cb_data);
proxy->proxy_cbs.unload(&proxy->public, &plugin->public, plugin->proxy_data, proxy->cb_data);
g_free(plugin->filename);
g_free(plugin);
}
static gchar *get_custom_plugin_path(const gchar *plugin_path_config,
const gchar *plugin_path_system)
{
gchar *plugin_path_custom;
if (EMPTY(prefs.custom_plugin_path))
return NULL;
plugin_path_custom = utils_get_locale_from_utf8(prefs.custom_plugin_path);
utils_tidy_path(plugin_path_custom);
/* check whether the custom plugin path is one of the system or user plugin paths
* and abort if so */
if (utils_str_equal(plugin_path_custom, plugin_path_config) ||
utils_str_equal(plugin_path_custom, plugin_path_system))
{
g_free(plugin_path_custom);
return NULL;
}
return plugin_path_custom;
}
/* all 3 paths Geany looks for plugins in can change (even system path on Windows)
* so we need to check active plugins are in the right place before loading */
static gboolean check_plugin_path(const gchar *fname)
{
gchar *plugin_path_config;
gchar *plugin_path_system;
gchar *plugin_path_custom;
gboolean ret = FALSE;
plugin_path_config = g_build_filename(app->configdir, "plugins", NULL);
if (g_str_has_prefix(fname, plugin_path_config))
ret = TRUE;
plugin_path_system = get_plugin_path();
if (g_str_has_prefix(fname, plugin_path_system))
ret = TRUE;
plugin_path_custom = get_custom_plugin_path(plugin_path_config, plugin_path_system);
if (plugin_path_custom)
{
if (g_str_has_prefix(fname, plugin_path_custom))
ret = TRUE;
g_free(plugin_path_custom);
}
g_free(plugin_path_config);
g_free(plugin_path_system);