2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
14 typedef struct DArray
*(*plug_init_t
) (struct env_t
*env
);
16 mod_t
* findPluginID (int ID
)
21 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
22 plug
= (mod_t
*) ENV
->modding
->get (i
);
30 char modding_optparse (int * i
, int step
)
35 for (pos
= 0; pos
< ENV
->modding
->get_size (); pos
++) {
36 plug
= (mod_t
*) ENV
->modding
->get (pos
);
39 if (plug
->opt
&& plug
->opt (ENV
->d_opts
, i
))
44 if (plug
->opt2
&& plug
->opt2 (ENV
->d_opts
, i
))
52 mod_t
* find_language (char *S
)
57 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
58 plug
= (mod_t
*) ENV
->modding
->get (i
);
61 if (EQ (plug
->language
, S
))
65 if (i
< ENV
->modding
->get_size ())
71 void mods_short_info ()
73 struct mod_t
*pm
= NULL
;
76 printf ("Available plugins: \n");
77 for (i
= 0; i
< ENV
->modding
->get_size (); i
++) {
78 pm
= (mod_t
*) ENV
->modding
->get (i
);
88 printf ("[%d]\t", pm
->ID
);
94 void plugins_viewer ()
103 printf ("Plugin id: ");
105 if (!fgets (m_buf
, 255, stdin
))
113 plug
= findPluginID (id
);
115 printf ("Bad plugin ID !\n");
119 if (! plug
->long_info
) {
120 printf ("No plugin info !\n");
126 printf ("\n<enter>\n");
128 fgets (m_buf
, 255, stdin
);
133 void mod_plugin_info (int ID
)
137 plug
= findPluginID (ID
);
139 fprintf (stderr
, "No such plugin [%d]", ID
);
147 FILE * mkctags_prepare ()
151 if (SB_FLGET (SB_FLCTAGSAPPEND
)) {
152 if (rename ("./tags", ENV
->tmp_tags
) < 0) {
153 fprintf (stderr
, "$ rename: ./tags %s\n", ENV
->tmp_tags
);
157 ofile
= fopen (ENV
->tmp_tags
, "a");
159 unlink (ENV
->tmp_tags
);
161 ofile
= fopen (ENV
->tmp_tags
, "w");
165 fprintf (stderr
, "fopen (\"%s\", \"w\")\n", ENV
->tmp_tags
);
173 int modding_start (int i_cmd
)
176 struct tt_state_t
* tt
;
181 // printf ("Find language plugin ...\n"); fflush (stdout);
182 plug
= find_language (ENV
->language
);
186 if (i_cmd
== cmd_makectags
)
187 ofile
= mkctags_prepare ();
189 size
= ENV
->d_files
->get_size ();
193 if (! plug
->make_ctags
)
195 plug
->make_ctags (NULL
, ofile
);
199 printf ("No such tags !\n");
206 if (i_cmd
== cmd_tags
) {
207 if (! plug
->print_tags
)
215 for (i
= 0; i
< size
; i
++) {
216 if (S_ISDIR (DLSTAT (ENV
->d_files
->get (i
))->st_mode
))
221 tt
= CNEW (tt_state_t
, 1);
222 memset (tt
, 0, sizeof (struct tt_state_t
));
223 tt
->fileName
= ENV
->d_files
->get (i
);
229 if (! SB_FLGET (SB_FLSIMULATE
)) {
230 write (fileno (stdout
), tt
->result
,
239 if (! plug
->make_ctags
)
242 plug
->make_ctags (ENV
->d_files
->get (i
), ofile
);
246 if (! plug
->call_tags
)
249 plug
->call_tags (ENV
->d_files
->get (i
));
255 plug
->file (ENV
->d_files
->get (i
));
269 if (i_cmd
== cmd_makectags
)
270 mk_tags ((char *) "tags", NULL
);
275 int modding_load_dir (char * path
)
280 files
= Dfiles (path
);
284 for (i
= 0; i
< files
->get_size (); i
++) {
285 if (EQ (files
->get (i
), "."))
287 if (EQ (files
->get (i
), ".."))
289 modding_load_plugin (files
->get (i
), path
);
292 files
->foreach (free
);
299 ENV
->modding
= new EArray(32);
300 modding_load_dir (ENV
->home_plugins
);
301 modding_load_dir (ENV
->shared_plugins
);
305 int modding_load_plugin (char * name
, char * path
)
312 struct mod_t
* sb_plugin
;
313 plug_init_t func_handle
;
316 if (! path
|| ! name
)
319 if (*stail (path
) == '/')
320 *stail(m_buf
) = '\0';
323 snprintf (m_buf
, 511, "%s/%s", path
, name
);
326 if (! S_ISREG (st
.st_mode
))
330 lib_handle
= dlopen (m_buf
, RTLD_NOW
);
331 s_dlerror
= (char *) dlerror ();
333 bug_plugin (s_dlerror
);
337 func_handle
= (plug_init_t
) dlsym (lib_handle
, "plugin_init");
338 s_dlerror
= (char *) dlerror ();
340 bug_plugin (s_dlerror
);
344 plug_list
= func_handle (ENV
);
345 if (plug_list
== NULL
)
348 for (n
= 0; n
< plug_list
->get_size (); n
++) {
349 sb_plugin
= (struct mod_t
*) plug_list
->get (n
);
350 sb_plugin
->mod_file
= strdup (m_buf
);
351 if (sb_plugin
->short_info
)
352 sb_plugin
->ID
= newPluginID ();
354 ENV
->modding
->add (LPCHAR (sb_plugin
));