File: plugins.c

package info (click to toggle)
crank 0.1.4-2
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 952 kB
  • ctags: 449
  • sloc: ansic: 3,052; makefile: 231
file content (139 lines) | stat: -rw-r--r-- 5,369 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* plugins.c - plugin handling code
 * 
 * This program is part of Crank, a cryptanalysis tool
 * Copyright (C) 2000 Matthew Russell
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License (LICENSE) for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

#include "crank.h"

/* External variables */
extern char *text;

/* Global variables */
plugin_data *plugin_list = NULL;
plugin_data *current_plugin; /* The current (mode) plugin */
int number_of_plugins = 0;

/* Null Widget for default start */
static void null_init(void) {;}
static int null_boot(void) {return PLUGIN_BOOT_OK;}
static float null_fitness(char *text) {return 0.0;}
static GtkWidget *null_widget(char *text) {return gtk_hbox_new(TRUE, 0);}
plugin_data null_plugin = {"", "", "", "", INTERFACE_VERSION, PLUGIN_MODE, "",  
                           null_boot, null_init, null_init, null_widget, (char *(*)(char *)) g_strdup, null_init, null_fitness, NULL};
float (*global_fitness)(char *) = null_fitness;

/* Load a symbol from the plugin into the identically named element */
/* of a plugin_data structure casting it appropriately along the way. */
/* If an error is encountered, the loop is continued to the next directory entry */
#define LOAD_SYMBOL(SYMBOL, CAST)     if(!g_module_symbol(plugin, #SYMBOL, plugin_symbol)) { \
                         	         g_warning("Error loading plugin: %s - '" #SYMBOL "' not found", d->d_name); \
                                         free(new_plugin); \
				         continue; \
                                      } \
                                      new_plugin->SYMBOL = CAST (*plugin_symbol); \

/* Initial finding and 'parsing' plugins */
void load_plugins(void) {
    DIR *plugin_dir;
    struct dirent *d;
    GModule *plugin;
    char buf[1024]; 
    plugin_data *new_plugin;  /* LOAD_SYMBOL uses this to build up a new plugin record */
    gpointer *plugin_symbol;  /* temp var for LOAD_SYMBOL */

    plugin_symbol = malloc(sizeof(gpointer));

    if (!g_module_supported()) {
	g_warning("Plugins not supported on this platform.");
	return;
    }
    if (!(plugin_dir = opendir(PLUGIN_DIR))) {
	g_warning("Could not open plugin directory - %s", PLUGIN_DIR);
	return;
    }

    /* Loop over each file in the plugin directory */
    while ((d = readdir(plugin_dir))) {

	if (d->d_name[0] == '.') /* Skip dotfiles, dirup etc. */
	    continue;

	sprintf(buf, "%s%c%s", PLUGIN_DIR, G_DIR_SEPARATOR, d->d_name);

	if (!(plugin = g_module_open(buf, 0))) {
	    g_warning("Error loading plugin: %s", g_module_error());
	    continue;
	}

	new_plugin = malloc(sizeof(plugin_data));

	/*          Symbol              Type to cast into (or dereference)*/
        /*          ------              ----------------- */
	LOAD_SYMBOL(name,               (char *)                 );
	LOAD_SYMBOL(version,            (char *)                 );
	LOAD_SYMBOL(author,             (char *)                 );
	LOAD_SYMBOL(plugin_type,        * (int *)                );
	LOAD_SYMBOL(description,        (char *)                 );
	LOAD_SYMBOL(interface_version,  * (int *)                );
	LOAD_SYMBOL(boot,               (int (*)())              );
      if (new_plugin->plugin_type == PLUGIN_FLOATING || new_plugin->plugin_type == PLUGIN_MODE) {
	LOAD_SYMBOL(menu_string,        (char *)                 );
	LOAD_SYMBOL(make_widget,        (GtkWidget *(*)(char *)) );
      }
      if (new_plugin->plugin_type == PLUGIN_MODE) {
	LOAD_SYMBOL(init_plugin,        (void (*)())             );
	LOAD_SYMBOL(deactivate_plugin,  (void (*)())             );
	LOAD_SYMBOL(transform,          (char *(*)(char *))      );
	LOAD_SYMBOL(reset,              (void (*)())             );
      }
      if (new_plugin->plugin_type == PLUGIN_FITNESS) {
         LOAD_SYMBOL(fitness,           (float (*)(char *))      );
	 /* FIXME: Fitness manager */
	 global_fitness = new_plugin->fitness;
      }
	/* Check for incompatible version */ 
	if (new_plugin->interface_version != INTERFACE_VERSION)
	    g_warning("%s - plugin uses newer interface than Crank.", d->d_name);
	
	/* Run the plugin's boot function */
	if(new_plugin->boot() == PLUGIN_BOOT_FAIL) {
	    g_warning("Error loading plugin: %s - plugin boot failed.", d->d_name);
	    continue;
	}
	
	/* Add the plugin to the list */
	new_plugin->next = plugin_list;
	plugin_list = new_plugin;
	
	g_message("Plugin loaded: %s v%s", new_plugin->name, new_plugin->version);
	number_of_plugins++;
    }

    if (!number_of_plugins)
	g_warning("Warning: No valid plugins found.");
    else
	g_message("%d plugin%s loaded.", number_of_plugins, (number_of_plugins == 1) ? "" : "s");

    free(plugin_symbol);
}

/* Return source text (should this be strcpy'd?) */
char *get_text(void) {
    return text;
}