forked from blackav/ejudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_plugin.c
160 lines (137 loc) · 4.54 KB
/
common_plugin.c
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* -*- mode: c -*- */
/* $Id$ */
/* Copyright (C) 2008-2014 Alexander Chernov <[email protected]> */
/*
* 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 for more details.
*/
#include "ejudge/common_plugin.h"
#include "ejudge/errlog.h"
#include "ejudge/xalloc.h"
#include <string.h>
static int plugins_num = 0;
static int plugins_size = 0;
static struct common_loaded_plugin *plugins = 0;
const struct common_loaded_plugin *
plugin_register_builtin(
struct common_plugin_iface *iface,
const struct ejudge_cfg *config)
{
int i;
struct common_plugin_data *data;
const unsigned char *fname = __FUNCTION__;
struct xml_tree *pcfg;
if (iface->common_version != COMMON_PLUGIN_IFACE_VERSION) {
err("%s: incompatible version of common plugin", fname);
return NULL;
}
if (iface->b.version != EJUDGE_PLUGIN_IFACE_VERSION) {
err("%s: incompatible version of base plugin", fname);
return NULL;
}
if (iface->b.size < sizeof(*iface)) {
err("%s: invalid size of common plugin", fname);
return NULL;
}
// check that the plugin is already loaded
for (i = 0; i < plugins_num; ++i) {
if (!strcmp(plugins[i].type, iface->b.type)
&& !strcmp(plugins[i].name, iface->b.name)) {
if (plugins[i].iface != iface) {
err("%s: iface mismatch", fname);
return NULL;
}
return &plugins[i];
}
}
// create the plugin
if (plugins_num == plugins_size) {
if (!plugins_size) plugins_size = 8;
plugins_size *= 2;
XREALLOC(plugins, plugins_size);
}
pcfg = ejudge_cfg_get_plugin_config(config, iface->b.type, iface->b.name);
if (!(data = iface->init())) {
err("%s: init failed for %s, %s", fname, iface->b.type, iface->b.name);
return NULL;
}
if (iface->prepare(data, config, pcfg) < 0) {
err("%s: prepare failed for %s, %s", fname, iface->b.type, iface->b.name);
return NULL;
}
plugins[plugins_num].type = xstrdup(iface->b.type);
plugins[plugins_num].name = xstrdup(iface->b.name);
plugins[plugins_num].iface = iface;
plugins[plugins_num].data = data;
return &plugins[plugins_num++];
}
const struct common_loaded_plugin *
plugin_load_external(
const unsigned char *path,
const unsigned char *type,
const unsigned char *name,
const struct ejudge_cfg *config)
{
int i;
struct common_plugin_data *data;
const unsigned char *fname = __FUNCTION__;
struct ejudge_plugin_iface *base_iface = 0;
struct common_plugin_iface *common_iface = 0;
struct xml_tree *pcfg;
for (i = 0; i < plugins_num; ++i)
if (!strcmp(plugins[i].type, type) && !strcmp(plugins[i].name, name))
return &plugins[i];
// create the plugin
if (plugins_num == plugins_size) {
if (!plugins_size) plugins_size = 8;
plugins_size *= 2;
XREALLOC(plugins, plugins_size);
}
plugin_set_directory(config->plugin_dir);
if (!(base_iface = plugin_load(path, type, name))) {
err("%s: cannot load plugin %s, %s", fname, type, name);
return NULL;
}
common_iface = (struct common_plugin_iface*) base_iface;
if (base_iface->size < sizeof(*common_iface)) {
err("%s: plugin %s, %s size mismatch", fname, type, name);
return NULL;
}
if (common_iface->common_version != COMMON_PLUGIN_IFACE_VERSION) {
err("%s: plugin %s, %s version mismatch", fname, type, name);
return NULL;
}
pcfg = ejudge_cfg_get_plugin_config(config, type, name);
if (!(data = common_iface->init())) {
err("%s: init failed for %s, %s", fname, type, name);
return NULL;
}
if (common_iface->prepare(data, config, pcfg) < 0) {
err("%s: prepare failed for %s, %s", fname, type, name);
common_iface->finish(data);
return NULL;
}
plugins[plugins_num].type = xstrdup(type);
plugins[plugins_num].name = xstrdup(name);
plugins[plugins_num].iface = common_iface;
plugins[plugins_num].data = data;
return &plugins[plugins_num++];
}
const struct common_loaded_plugin *
plugin_get(
const unsigned char *type,
const unsigned char *name)
{
int i;
for (i = 0; i < plugins_num; ++i)
if (!strcmp(plugins[i].type, type) && !strcmp(plugins[i].name, name))
return &plugins[i];
return NULL;
}