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
)
39 for (pos
= 0; pos
< ENV
->modding
->get_size (); pos
++) {
40 plug
= (mod_t
*) ENV
->modding
->get (pos
);
41 f
= (mod_feature
*) plug
;
43 if (plug
->Type
== TYPE_FEATURE
) {
44 f
= (mod_feature
*) plug
;
47 if (f
->opt
&& f
->opt (ENV
->d_opts
, i
))
52 if (f
->opt2
&& f
->opt2 (ENV
->d_opts
, i
))
62 struct mod_language
* find_language (char *S
)
64 struct mod_language
*pm
= NULL
;
67 //printf ("Plugins count : %i\n", ENV->modding->get_size ());
68 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
69 pm
= (struct mod_language
*) ENV
->modding
->get (i
);
73 if (pm
->mod
.Type
!= TYPE_LANGUAGE
)
76 if (EQ (pm
->language
, S
))
80 if (i
< ENV
->modding
->get_size ())
88 struct mod_t
*pm
= NULL
;
91 printf ("Available plugins: \n");
92 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
93 pm
= (mod_t
*) ENV
->modding
->get (i
);
100 printf ("[%d]\t", pm
->ID
);
106 void mod_plugin_info (int ID
)
110 plug
= findPluginID (ID
);
112 fprintf (stderr
, "No such plugin [%d]", ID
);
120 FILE * mkctags_prepare ()
124 if (SB_FLGET (SB_FLCTAGSAPPEND
)) {
125 if (rename ("./tags", ENV
->tmp_tags
) < 0) {
126 fprintf (stderr
, "$ rename: ./tags %s\n", ENV
->tmp_tags
);
130 ofile
= fopen (ENV
->tmp_tags
, "a");
132 unlink (ENV
->tmp_tags
);
134 ofile
= fopen (ENV
->tmp_tags
, "w");
138 fprintf (stderr
, "fopen (\"%s\", \"w\")\n", ENV
->tmp_tags
);
146 int modding_start (int i_cmd
)
148 struct mod_language
*pm
= NULL
;
149 struct tt_state_t
* tt
;
154 pm
= find_language (ENV
->language
);
155 if (pm
== NULL
|| pm
->the
== NULL
) {
156 assert (true, "Language plugin not found !\n");
160 if (i_cmd
== cmd_makectags
)
161 ofile
= mkctags_prepare ();
163 size
= ENV
->d_files
->get_size ();
167 if (! pm
->make_ctags
)
169 pm
->make_ctags (NULL
, ofile
);
174 for (i
= 0; i
< size
; i
++) {
175 if (S_ISDIR (DLSTAT (ENV
->d_files
->get (i
))->st_mode
))
180 tt
= CNEW (tt_state_t
, 1);
181 memset (tt
, 0, sizeof (struct tt_state_t
));
182 tt
->fileName
= ENV
->d_files
->get (i
);
188 if (! SB_FLGET (SB_FLSIMULATE
)) {
189 write (fileno (stdout
), tt
->result
,
198 if (! pm
->make_ctags
|| !pm
->the
)
201 pm
->make_ctags (ENV
->d_files
->get (i
), ofile
);
208 pm
->call_tags (ENV
->d_files
->get (i
));
222 if (i_cmd
== cmd_makectags
)
223 mk_tags ((char *) "tags", NULL
);
228 int modding_load_dir (char * path
)
233 files
= Dfiles (path
);
237 for (i
= 0; i
< files
->get_size (); i
++) {
238 if (EQ (files
->get (i
), "."))
240 if (EQ (files
->get (i
), ".."))
242 modding_load_plugin (files
->get (i
), path
);
245 files
->foreach (free
);
252 ENV
->modding
= new EArray(32);
253 modding_load_dir (ENV
->home_plugins
);
254 modding_load_dir (ENV
->shared_plugins
);
258 int modding_load_plugin (char * name
, char * path
)
265 struct mod_t
* sb_plugin
;
266 plug_init_t func_handle
;
269 if (! path
|| ! name
)
272 if (*stail (path
) == '/')
273 *stail(m_buf
) = '\0';
276 snprintf (m_buf
, 511, "%s/%s", path
, name
);
279 if (! S_ISREG (st
.st_mode
))
283 lib_handle
= dlopen (m_buf
, RTLD_NOW
);
284 s_dlerror
= (char *) dlerror ();
286 bug_plugin (s_dlerror
);
290 func_handle
= (plug_init_t
) dlsym (lib_handle
, "plugin_init");
291 s_dlerror
= (char *) dlerror ();
293 bug_plugin (s_dlerror
);
297 plug_list
= func_handle (ENV
);
298 if (plug_list
== NULL
)
301 for (n
= 0; n
< plug_list
->get_size (); n
++) {
302 sb_plugin
= (struct mod_t
*) plug_list
->get (n
);
303 sb_plugin
->mod_file
= strdup (m_buf
);
304 if (sb_plugin
->short_info
)
305 sb_plugin
->ID
= newPluginID ();
307 ENV
->modding
->add (LPCHAR (sb_plugin
));