/usr/share/doc/libcpluff0-dev/examples/cpfile/plugins/core/core.c is in libcpluff0-dev 0.1.4+dfsg1-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | /*
* Copyright 2007 Johannes Lehtinen
* This file is free software; Johannes Lehtinen gives unlimited
* permission to copy, distribute and modify it.
*/
#include <stdlib.h>
#include <stdio.h>
#include <cpluff.h>
#include "core.h"
/* ------------------------------------------------------------------------
* Data types
* ----------------------------------------------------------------------*/
/** Type for plugin_data_t structure */
typedef struct plugin_data_t plugin_data_t;
/** Type for registered_classifier_t structure */
typedef struct registered_classifier_t registered_classifier_t;
/** Plug-in instance data */
struct plugin_data_t {
/** The plug-in context */
cp_context_t *ctx;
/** Number of registered classifiers */
int num_classifiers;
/** An array of registered classifiers */
registered_classifier_t *classifiers;
};
/** Registered classifier information */
struct registered_classifier_t {
/** The priority of the classifier */
int priority;
/** The classifier data */
classifier_t *classifier;
};
/* ------------------------------------------------------------------------
* Internal functions
* ----------------------------------------------------------------------*/
/**
* A run function for the core plug-in. In this case this function acts as
* the application main function so there is no need for us to split the
* execution into small steps. Rather, we execute the whole main loop at
* once to make it simpler.
*/
static int run(void *d) {
plugin_data_t *data = d;
char **argv;
int argc;
int i;
// Read arguments and print usage, if no arguments given
argv = cp_get_context_args(data->ctx, &argc);
if (argc < 2) {
fputs("usage: cpfile <file> [<file>...]\n", stdout);
return 0;
}
// Go through all files listed as command arguments
for (i = 1; argv[i] != NULL; i++) {
int j;
int classified = 0;
// Print file name
printf("%s: ", argv[i]);
// Try classifiers in order of descending priority
for (j = 0; !classified && j < data->num_classifiers; j++) {
classifier_t *cl
= data->classifiers[j].classifier;
classified = cl->classify(cl->data, argv[i]);
}
// Check if unknown file
if (!classified) {
fputs("unknown file type\n", stdout);
}
}
// All done
return 0;
}
/**
* Creates a new plug-in instance.
*/
static void *create(cp_context_t *ctx) {
plugin_data_t *data = malloc(sizeof(plugin_data_t));
if (data != NULL) {
data->ctx = ctx;
data->num_classifiers = 0;
data->classifiers = NULL;
} else {
cp_log(ctx, CP_LOG_ERROR,
"Insufficient memory for plug-in data.");
}
return data;
}
/**
* Compares two registered classifiers according to priority.
*/
static int comp_classifiers(const registered_classifier_t *c1,
const registered_classifier_t *c2) {
return c2->priority - c1->priority;
}
/**
* Initializes and starts the plug-in.
*/
static int start(void *d) {
plugin_data_t *data = d;
cp_extension_t **cl_exts;
int num_cl_exts;
cp_status_t status;
int i;
// Obtain list of registered classifiers
cl_exts = cp_get_extensions_info(
data->ctx,
"org.c-pluff.examples.cpfile.core.classifiers",
&status,
&num_cl_exts
);
if (cl_exts == NULL) {
// An error occurred and framework logged it
return status;
}
// Allocate memory for classifier information, if any
if (num_cl_exts > 0) {
data->classifiers = malloc(
num_cl_exts * sizeof(registered_classifier_t)
);
if (data->classifiers == NULL) {
// Memory allocation failed
cp_log(data->ctx, CP_LOG_ERROR,
"Insufficient memory for classifier list.");
return CP_ERR_RESOURCE;
}
}
/* Resolve classifier functions. This will implicitly start
* plug-ins providing the classifiers. */
for (i = 0; i < num_cl_exts; i++) {
const char *str;
int pri;
classifier_t *cl;
// Get the classifier function priority
str = cp_lookup_cfg_value(
cl_exts[i]->configuration, "@priority"
);
if (str == NULL) {
// Classifier is missing mandatory priority
cp_log(data->ctx, CP_LOG_ERROR,
"Ignoring classifier without priority.");
continue;
}
pri = atoi(str);
// Resolve classifier data pointer
str = cp_lookup_cfg_value(
cl_exts[i]->configuration, "@classifier");
if (str == NULL) {
// Classifier symbol name is missing
cp_log(data->ctx, CP_LOG_ERROR,
"Ignoring classifier without symbol name.");
continue;
}
cl = cp_resolve_symbol(
data->ctx,
cl_exts[i]->plugin->identifier,
str,
NULL
);
if (cl == NULL) {
// Could not resolve classifier symbol
cp_log(data->ctx, CP_LOG_ERROR,
"Ignoring classifier which could not be resolved.");
continue;
}
// Add classifier to the list of registered classifiers
data->classifiers[data->num_classifiers].priority = pri;
data->classifiers[data->num_classifiers].classifier = cl;
data->num_classifiers++;
}
// Release extension information
cp_release_info(data->ctx, cl_exts);
// Sort registered classifiers according to priority
if (data->num_classifiers > 1) {
qsort(data->classifiers,
data->num_classifiers,
sizeof(registered_classifier_t),
(int (*)(const void *, const void *)) comp_classifiers);
}
// Register run function to do the real work
cp_run_function(data->ctx, run);
// Successfully started
return CP_OK;
}
/**
* Releases resources from other plug-ins.
*/
static void stop(void *d) {
plugin_data_t *data = d;
int i;
// Release classifier data, if any
if (data->classifiers != NULL) {
// Release classifier pointers
for (i = 0; i < data->num_classifiers; i++) {
cp_release_symbol(
data->ctx, data->classifiers[i].classifier
);
}
// Free local data
free(data->classifiers);
data->classifiers = NULL;
data->num_classifiers = 0;
}
}
/**
* Destroys a plug-in instance.
*/
static void destroy(void *d) {
free(d);
}
/* ------------------------------------------------------------------------
* Exported runtime information
* ----------------------------------------------------------------------*/
/**
* Plug-in runtime information for the framework. The name of this symbol
* is stored in the plug-in descriptor.
*/
CP_EXPORT cp_plugin_runtime_t cp_ex_cpfile_core_funcs = {
create,
start,
stop,
destroy
};
|