Lines 30-40
Link Here
|
30 |
/* Include pkg */ |
30 |
/* Include pkg */ |
31 |
#include <pkg.h> |
31 |
#include <pkg.h> |
32 |
|
32 |
|
33 |
/* Include any plugin headers here */ |
33 |
/* Define plugin name and configuration settings */ |
34 |
#include "template.h" |
34 |
static const char PLUGIN_NAME[] = "template"; |
|
|
35 |
static const char CFG_TEXT[] = "text"; |
36 |
static const char CFG_COUNT[] = "count"; |
37 |
static const char CFG_FLAG[] = "flag"; |
38 |
static const char CFG_COLORS[] = "colors"; |
35 |
|
39 |
|
36 |
/* Define the plugin name */ |
40 |
/* Maintain a reference to ourself */ |
37 |
#define PLUGIN_NAME "template" |
41 |
static struct pkg_plugin *self; |
38 |
|
42 |
|
39 |
/* |
43 |
/* |
40 |
* The plugin *must* provide an init function that is called by the library. |
44 |
* The plugin *must* provide an init function that is called by the library. |
Lines 47-53
Link Here
|
47 |
* |
51 |
* |
48 |
* The plugin's init function prototype should be in the following form: |
52 |
* The plugin's init function prototype should be in the following form: |
49 |
* |
53 |
* |
50 |
* int pkg_plugins_init_<plugin> (void); |
54 |
* int pkg_plugins_init (void); |
51 |
* |
55 |
* |
52 |
* No arguments are passed to the plugin's init function. |
56 |
* No arguments are passed to the plugin's init function. |
53 |
* |
57 |
* |
Lines 55-62
Link Here
|
55 |
* upon failure EPKG_FATAL ( > 0 ) is returned to the caller. |
59 |
* upon failure EPKG_FATAL ( > 0 ) is returned to the caller. |
56 |
*/ |
60 |
*/ |
57 |
int |
61 |
int |
58 |
pkg_plugins_init_template(void) |
62 |
pkg_plugin_init(struct pkg_plugin *p) |
59 |
{ |
63 |
{ |
|
|
64 |
/* Keep a reference to our plugin object, so it can be used inside the callback functions */ |
65 |
self = p; |
66 |
|
67 |
/* |
68 |
* Declare the plugin's metadata |
69 |
* |
70 |
* This information is shown by 'pkg plugins'. |
71 |
* |
72 |
*/ |
73 |
|
74 |
pkg_plugin_set(p, PKG_PLUGIN_NAME, PLUGIN_NAME); |
75 |
pkg_plugin_set(p, PKG_PLUGIN_DESC, "Template plugin for pkgng"); |
76 |
pkg_plugin_set(p, PKG_PLUGIN_VERSION, "1.0.0"); |
77 |
|
78 |
/* |
79 |
* Register configuration settings |
80 |
* |
81 |
* Declares |
82 |
* - key (the name of the setting in the configuration file) |
83 |
* - type (type of the setting, one of PKG_STRING, PKG_BOOL, PKG_INT, PKG_ARRAY) |
84 |
* - default value (value if not set in the configuration file, provide empty string for no default) |
85 |
* |
86 |
*/ |
87 |
|
88 |
pkg_plugin_conf_add(p, PKG_STRING, CFG_TEXT, ""); |
89 |
pkg_plugin_conf_add(p, PKG_INT, CFG_COUNT, "0"); |
90 |
pkg_plugin_conf_add(p, PKG_BOOL, CFG_FLAG, "false"); |
91 |
pkg_plugin_conf_add(p, PKG_ARRAY, CFG_COLORS, ""); |
92 |
|
93 |
/* Parse the configuration file for above settings. Do not forget this! */ |
94 |
pkg_plugin_parse(p); |
95 |
|
60 |
/* |
96 |
/* |
61 |
* Register two functions for hooking into the library |
97 |
* Register two functions for hooking into the library |
62 |
* |
98 |
* |
Lines 68-82
pkg_plugins_init_template(void)
Link Here
|
68 |
* |
104 |
* |
69 |
*/ |
105 |
*/ |
70 |
|
106 |
|
71 |
/* printf(">>> Plugin '%s' is about to hook into pkgng.. yay! :)\n", PLUGIN_NAME); */ |
107 |
/* printf(">>> Plugin '%s' is about to hook into pkgng.. yay! :)\n", pkg_plugin_get(p, PKG_PLUGIN_NAME)); */ |
72 |
|
108 |
|
73 |
if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_PRE_INSTALL, &my_callback1) != EPKG_OK) { |
109 |
if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_PRE_INSTALL, &my_callback1) != EPKG_OK) { |
74 |
fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME); |
110 |
pkg_plugin_error(p, "failed to hook into the library"); |
75 |
return (EPKG_FATAL); |
111 |
return (EPKG_FATAL); |
76 |
} |
112 |
} |
77 |
|
113 |
|
78 |
if (pkg_plugins_hook(PLUGIN_NAME, PKG_PLUGINS_HOOK_POST_INSTALL, &my_callback2) != EPKG_OK) { |
114 |
if (pkg_plugin_hook_register(p, PKG_PLUGIN_HOOK_POST_INSTALL, &my_callback2) != EPKG_OK) { |
79 |
fprintf(stderr, "Plugin '%s' failed to hook into the library\n", PLUGIN_NAME); |
115 |
pkg_plugin_error(p, "failed to hook into the library"); |
80 |
return (EPKG_FATAL); |
116 |
return (EPKG_FATAL); |
81 |
} |
117 |
} |
82 |
|
118 |
|
Lines 93-107
pkg_plugins_init_template(void)
Link Here
|
93 |
* |
129 |
* |
94 |
* The plugin's shutdown function prototype should be in the following form: |
130 |
* The plugin's shutdown function prototype should be in the following form: |
95 |
* |
131 |
* |
96 |
* int pkg_plugins_shutdown_<plugin> (void); |
132 |
* int pkg_plugins_shutdown (struct pkg_plugin *); |
97 |
* |
133 |
* |
98 |
* Upon successful shutdown of the plugin EPKG_OK (0) is returned and |
134 |
* Upon successful shutdown of the plugin EPKG_OK (0) is returned and |
99 |
* upon failure EPKG_FATAL ( > 0 ) is returned to the caller. |
135 |
* upon failure EPKG_FATAL ( > 0 ) is returned to the caller. |
100 |
*/ |
136 |
*/ |
101 |
int |
137 |
int |
102 |
pkg_plugins_shutdown_template(void) |
138 |
pkg_plugin_shutdown(struct pkg_plugin *p __unused) |
103 |
{ |
139 |
{ |
104 |
/* printf(">>> Plugin '%s' is shutting down, enough working for today.. :)\n", PLUGIN_NAME); */ |
140 |
/* printf(">>> Plugin '%s' is shutting down, enough working for today.. :)\n", pkg_plugin_get(p, PKG_PLUGIN_NAME)); */ |
105 |
|
141 |
|
106 |
/* |
142 |
/* |
107 |
* Perform any cleanup if needed, e.g.: |
143 |
* Perform any cleanup if needed, e.g.: |
Lines 120-127
pkg_plugins_shutdown_template(void)
Link Here
|
120 |
* And now we need to define our workers, |
156 |
* And now we need to define our workers, |
121 |
* the plugin functions that carry out the real work. |
157 |
* the plugin functions that carry out the real work. |
122 |
* |
158 |
* |
123 |
* A plugin callback function accepts only one argument and |
159 |
* A plugin callback function must satisfy the following function pointer signature: |
124 |
* should return EPKG_OK (0) on success and EPKG_FATAL ( > 0 ) on failure. |
160 |
* |
|
|
161 |
* int(*pkg_plugin_callback)(void *data, struct pkgdb *db); |
162 |
* |
163 |
* It should return EPKG_OK (0) on success and EPKG_FATAL ( > 0 ) on failure. |
125 |
* |
164 |
* |
126 |
* Plugin callbacks must also take care of proper casting of the (void *data) argument. |
165 |
* Plugin callbacks must also take care of proper casting of the (void *data) argument. |
127 |
* |
166 |
* |
Lines 130-145
pkg_plugins_shutdown_template(void)
Link Here
|
130 |
* |
169 |
* |
131 |
* For example if a plugin hooks into PKG_PLUGINS_HOOK_PRE_INSTALL the (void *data) passed to the |
170 |
* For example if a plugin hooks into PKG_PLUGINS_HOOK_PRE_INSTALL the (void *data) passed to the |
132 |
* called is (struct pkg_jobs *), so the plugin callback must cast it explicitely. |
171 |
* called is (struct pkg_jobs *), so the plugin callback must cast it explicitely. |
|
|
172 |
* |
173 |
* If the callback needs to access configuration data, a reference to the plugin object |
174 |
* can be kept in the global scope of this compilation unit: |
175 |
* |
176 |
* static struct pkg_plugin *self; |
177 |
* |
133 |
*/ |
178 |
*/ |
134 |
int |
179 |
int |
135 |
my_callback1(void *data, struct pkgdb *db) |
180 |
my_callback1(void *data, struct pkgdb *db) |
136 |
{ |
181 |
{ |
137 |
printf("Hey, I was just called by the library, lets see what we've got here..\n"); |
182 |
const char *text = NULL; |
|
|
183 |
int64_t count = 0; |
184 |
bool flag = false; |
185 |
const pkg_object *colors = NULL; |
186 |
const pkg_object *color = NULL; |
187 |
const pkg_object *cfg = NULL; |
188 |
pkg_iter it = NULL; |
189 |
|
190 |
/* Get configuration object */ |
191 |
cfg = pkg_plugin_conf(self); |
192 |
|
193 |
/* Get configuration data */ |
194 |
flag = pkg_object_bool(pkg_object_find(cfg, CFG_FLAG)); |
195 |
count = pkg_object_int(pkg_object_find(cfg, CFG_COUNT)); |
196 |
text = pkg_object_string(pkg_object_find(cfg, CFG_TEXT)); |
197 |
colors = pkg_object_find(cfg, CFG_COLORS); |
198 |
|
199 |
|
200 |
pkg_plugin_info(self, "Hey, I was just called by the library, lets see what we've got here.."); |
201 |
|
202 |
pkg_plugin_info(self, " Text: '%s'", text); |
203 |
pkg_plugin_info(self, " Count: %i", count); |
204 |
pkg_plugin_info(self, " Flag: %s", flag ? "true" : "false"); |
205 |
while ((color = pkg_object_iterate(colors, &it)) != NULL) { |
206 |
pkg_plugin_info(self, " Color: %s", pkg_object_string(color)); |
207 |
} |
138 |
|
208 |
|
139 |
if (data == NULL) |
209 |
if (data == NULL) |
140 |
printf("Hmm.. no data for me today, guess I'll just go and grab a mohito then..\n"); |
210 |
pkg_plugin_info(self, "Hmm.. no data for me today, guess I'll just go and grab a mohito then.."); |
141 |
else |
211 |
else |
142 |
printf("Got some data.. okay, okay.. I'll do something useful then..\n"); |
212 |
pkg_plugin_info(self, "Got some data.. okay, okay.. I'll do something useful then.."); |
143 |
|
213 |
|
144 |
return (EPKG_OK); |
214 |
return (EPKG_OK); |
145 |
} |
215 |
} |
Lines 150-161
my_callback1(void *data, struct pkgdb *db)
Link Here
|
150 |
int |
220 |
int |
151 |
my_callback2(void *data, struct pkgdb *db) |
221 |
my_callback2(void *data, struct pkgdb *db) |
152 |
{ |
222 |
{ |
153 |
printf("Hey, I was just called again, lets see what its all about this time..\n"); |
223 |
pkg_plugin_info(self, "Hey, I was just called again, lets see what its all about this time.."); |
154 |
|
224 |
|
155 |
if (data == NULL) |
225 |
if (data == NULL) |
156 |
printf("Hmm.. no data, no work.. today is my lucky day!\n"); |
226 |
pkg_plugin_info(self, "Hmm.. no data, no work.. today is my lucky day!"); |
157 |
else |
227 |
else |
158 |
printf("Work never ends.. I'll do something useful again..\n"); |
228 |
pkg_plugin_info(self, "Work never ends.. I'll do something useful again.."); |
159 |
|
229 |
|
160 |
return (EPKG_OK); |
230 |
return (EPKG_OK); |
161 |
} |
231 |
} |