/[baselayout]/trunk/src/rc-plugin.c
Gentoo

Diff of /trunk/src/rc-plugin.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2576 Revision 2577
17#include "rc-plugin.h" 17#include "rc-plugin.h"
18#include "strlist.h" 18#include "strlist.h"
19 19
20typedef struct plugin 20typedef struct plugin
21{ 21{
22 char *name; 22 char *name;
23 void *handle; 23 void *handle;
24 int (*hook) (rc_hook_t hook, const char *name); 24 int (*hook) (rc_hook_t hook, const char *name);
25 struct plugin *next; 25 struct plugin *next;
26} plugin_t; 26} plugin_t;
27 27
28static plugin_t *plugins = NULL; 28static plugin_t *plugins = NULL;
29 29
30void rc_plugin_load (void) 30void rc_plugin_load (void)
31{ 31{
32 char **files; 32 char **files;
33 char *file; 33 char *file;
34 int i; 34 int i;
35 plugin_t *plugin = plugins; 35 plugin_t *plugin = plugins;
36 36
37 /* Ensure some sanity here */ 37 /* Ensure some sanity here */
38 rc_plugin_unload (); 38 rc_plugin_unload ();
39 39
40 if (! rc_exists (RC_PLUGINDIR)) 40 if (! rc_exists (RC_PLUGINDIR))
41 return; 41 return;
42 42
43 files = rc_ls_dir (NULL, RC_PLUGINDIR, 0); 43 files = rc_ls_dir (NULL, RC_PLUGINDIR, 0);
44 STRLIST_FOREACH (files, file, i) 44 STRLIST_FOREACH (files, file, i) {
45 {
46 char *p = rc_strcatpaths (RC_PLUGINDIR, file, NULL); 45 char *p = rc_strcatpaths (RC_PLUGINDIR, file, NULL);
47 void *h = dlopen (p, RTLD_LAZY); 46 void *h = dlopen (p, RTLD_LAZY);
48 char *func; 47 char *func;
49 void *f; 48 void *f;
50 int len; 49 int len;
51 50
52 if (! h) 51 if (! h) {
53 {
54 eerror ("dlopen `%s': %s", p, dlerror ()); 52 eerror ("dlopen `%s': %s", p, dlerror ());
55 free (p); 53 free (p);
56 continue; 54 continue;
57 } 55 }
58 56
59 func = file; 57 func = file;
60 file = strsep (&func, "."); 58 file = strsep (&func, ".");
61 len = strlen (file) + 7; 59 len = strlen (file) + 7;
62 func = rc_xmalloc (sizeof (char *) * len); 60 func = rc_xmalloc (sizeof (char *) * len);
63 snprintf (func, len, "_%s_hook", file); 61 snprintf (func, len, "_%s_hook", file);
64 62
65 f = dlsym (h, func); 63 f = dlsym (h, func);
66 if (! f) 64 if (! f) {
67 {
68 eerror ("`%s' does not expose the symbol `%s'", p, func); 65 eerror ("`%s' does not expose the symbol `%s'", p, func);
69 dlclose (h); 66 dlclose (h);
70 } 67 } else {
71 else 68 if (plugin) {
72 {
73 if (plugin)
74 {
75 plugin->next = rc_xmalloc (sizeof (plugin_t)); 69 plugin->next = rc_xmalloc (sizeof (plugin_t));
76 plugin = plugin->next; 70 plugin = plugin->next;
77 } 71 } else
78 else
79 plugin = plugins = rc_xmalloc (sizeof (plugin_t)); 72 plugin = plugins = rc_xmalloc (sizeof (plugin_t));
80 73
81 memset (plugin, 0, sizeof (plugin_t)); 74 memset (plugin, 0, sizeof (plugin_t));
82 plugin->name = strdup (file); 75 plugin->name = strdup (file);
83 plugin->handle = h; 76 plugin->handle = h;
84 plugin->hook = f; 77 plugin->hook = f;
85 } 78 }
86 79
87 free (func); 80 free (func);
88 free (p); 81 free (p);
89 } 82 }
90 83
91 rc_strlist_free (files); 84 rc_strlist_free (files);
92} 85}
93 86
94void rc_plugin_run (rc_hook_t hook, const char *value) 87void rc_plugin_run (rc_hook_t hook, const char *value)
95{ 88{
96 plugin_t *plugin = plugins; 89 plugin_t *plugin = plugins;
97 90
98 while (plugin) 91 while (plugin) {
99 {
100 if (plugin->hook) 92 if (plugin->hook)
101 plugin->hook (hook, value); 93 plugin->hook (hook, value);
102 94
103 plugin = plugin->next; 95 plugin = plugin->next;
104 } 96 }
105} 97}
106 98
107void rc_plugin_unload (void) 99void rc_plugin_unload (void)
108{ 100{
109 plugin_t *plugin = plugins; 101 plugin_t *plugin = plugins;
110 plugin_t *next; 102 plugin_t *next;
111 103
112 while (plugin) 104 while (plugin) {
113 {
114 next = plugin->next; 105 next = plugin->next;
115 dlclose (plugin->handle); 106 dlclose (plugin->handle);
116 free (plugin->name); 107 free (plugin->name);
117 free (plugin); 108 free (plugin);
118 plugin = next; 109 plugin = next;
119 } 110 }
120 plugins = NULL; 111 plugins = NULL;
121} 112}

Legend:
Removed from v.2576  
changed lines
  Added in v.2577

  ViewVC Help
Powered by ViewVC 1.1.20