2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
14 #define PLUGINS_MASK "find ~/plugins/ -name \"libplugin_*.so.*\""
16 typedef struct DArray
*(*plug_init_t
) (struct env_t
*env
);
18 mod_t
* findPluginID (int ID
)
23 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
24 plug
= (mod_t
*) ENV
->modding
->get (i
);
32 char modding_optparse (int * i
, int step
)
37 for (pos
= 0; pos
< ENV
->modding
->get_size (); pos
++) {
38 plug
= (mod_t
*) ENV
->modding
->get (pos
);
41 if (plug
->opt
&& plug
->opt (ENV
->d_opts
, i
))
46 if (plug
->opt2
&& plug
->opt2 (ENV
->d_opts
, i
))
54 mod_t
* find_language (char *S
)
59 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
60 plug
= (mod_t
*) ENV
->modding
->get (i
);
63 if (EQ (plug
->language
, S
))
67 if (i
< ENV
->modding
->get_size ())
73 void mods_short_info ()
75 struct mod_t
*pm
= NULL
;
78 printf ("Available plugins: \n");
79 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
80 pm
= (mod_t
*) ENV
->modding
->get (i
);
87 printf ("[%d]\t", pm
->ID
);
93 void plugins_viewer ()
102 printf ("Plugin id: ");
104 if (!fgets (m_buf
, 255, stdin
))
112 plug
= findPluginID (id
);
114 printf ("Bad plugin ID !\n");
118 if (! plug
->long_info
) {
119 printf ("No plugin info !\n");
125 printf ("\n<enter>\n");
127 fgets (m_buf
, 255, stdin
);
132 void mod_plugin_info (int ID
)
136 plug
= findPluginID (ID
);
138 fprintf (stderr
, "No such plugin [%d]", ID
);
146 FILE * mkctags_prepare ()
150 if (SB_FLGET (SB_FLCTAGSAPPEND
)) {
151 if (rename ("./tags", ENV
->tmp_tags
) < 0) {
152 fprintf (stderr
, "$ rename: ./tags %s\n", ENV
->tmp_tags
);
156 ofile
= fopen (ENV
->tmp_tags
, "a");
158 unlink (ENV
->tmp_tags
);
160 ofile
= fopen (ENV
->tmp_tags
, "w");
164 fprintf (stderr
, "fopen (\"%s\", \"w\")\n", ENV
->tmp_tags
);
172 int modding_start (int i_cmd
)
175 struct tt_state_t
* tt
;
180 // printf ("Find language plugin ...\n"); fflush (stdout);
181 plug
= find_language (ENV
->language
);
185 if (i_cmd
== cmd_makectags
)
186 ofile
= mkctags_prepare ();
188 size
= ENV
->d_files
->get_size ();
192 if (! plug
->make_ctags
)
194 plug
->make_ctags (NULL
, ofile
);
198 printf ("No such tags !\n");
205 if (i_cmd
== cmd_tags
) {
206 if (! plug
->print_tags
)
214 for (i
= 0; i
< size
; i
++) {
215 if (S_ISDIR (DLSTAT (ENV
->d_files
->get (i
))->st_mode
))
220 tt
= CNEW (tt_state_t
, 1);
221 memset (tt
, 0, sizeof (struct tt_state_t
));
222 tt
->fileName
= ENV
->d_files
->get (i
);
228 if (! SB_FLGET (SB_FLSIMULATE
)) {
229 write (fileno (stdout
), tt
->result
,
238 if (! plug
->make_ctags
)
241 plug
->make_ctags (ENV
->d_files
->get (i
), ofile
);
245 if (! plug
->call_tags
)
248 plug
->call_tags (ENV
->d_files
->get (i
));
254 plug
->file (ENV
->d_files
->get (i
));
268 if (i_cmd
== cmd_makectags
)
269 mk_tags ((char *) "tags", NULL
);
274 int modding_load_dir (char * path
)
279 files
= Dfiles (path
);
283 for (i
= 0; i
< files
->get_size (); i
++) {
284 if (EQ (files
->get (i
), "."))
286 if (EQ (files
->get (i
), ".."))
288 modding_load_plugin (files
->get (i
), path
);
291 files
->foreach (free
);
298 ENV
->modding
= new EArray(32);
299 modding_load_dir (ENV
->home_plugins
);
300 modding_load_dir (ENV
->shared_plugins
);
304 int modding_load_plugin (char * name
, char * path
)
311 struct mod_t
* sb_plugin
;
312 plug_init_t func_handle
;
315 if (! path
|| ! name
)
318 if (*stail (path
) == '/')
319 *stail(m_buf
) = '\0';
322 snprintf (m_buf
, 511, "%s/%s", path
, name
);
325 if (! S_ISREG (st
.st_mode
))
329 lib_handle
= dlopen (m_buf
, RTLD_NOW
);
330 s_dlerror
= (char *) dlerror ();
332 bug_plugin (s_dlerror
);
336 func_handle
= (plug_init_t
) dlsym (lib_handle
, "plugin_init");
337 s_dlerror
= (char *) dlerror ();
339 bug_plugin (s_dlerror
);
343 plug_list
= func_handle (ENV
);
344 if (plug_list
== NULL
)
347 for (n
= 0; n
< plug_list
->get_size (); n
++) {
348 sb_plugin
= (struct mod_t
*) plug_list
->get (n
);
349 sb_plugin
->mod_file
= strdup (m_buf
);
350 if (sb_plugin
->short_info
)
351 sb_plugin
->ID
= newPluginID ();
353 ENV
->modding
->add (LPCHAR (sb_plugin
));