Skip to content

Commit

Permalink
a few more comment + fix xalloca + add new optional config function i…
Browse files Browse the repository at this point in the history
…n the plugin API + new foreach module function allowing the call of multiple callbacks
  • Loading branch information
ziirish committed May 23, 2012
1 parent 2b329a4 commit 58da8a6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/command.c
Expand Up @@ -246,6 +246,8 @@ free_command (command *ptr)
if (ptr != NULL)
{
int cpt;
/* if we have no PARSING module there is no reason we allocate memory
* for a new arguments array. In this case, we MUST NOT free memory */
unsigned int copy = (ptr->argv == ptr->argvf);
for (cpt = 0; cpt < ptr->argc; cpt++)
{
Expand Down Expand Up @@ -298,7 +300,7 @@ run_command (command_line *ptrc)
if (modules != NULL)
{
void *data[] = {(void *)&ptr};
int r = launch_each_module (modules, data);
int r = foreach_module (modules, data, MAIN);
free_sdplist (modules);
/* if something went wrong we do not execute the command */
if (r != 1)
Expand Down
6 changes: 4 additions & 2 deletions src/modules.h
Expand Up @@ -50,11 +50,13 @@ void init_modules (void);
void clear_modules (void);

/**
* Execute each main function of the modules present in the list
* Execute each given function of the modules present in the list
* @param list List of modules
* @param data Datas to pass to the function
* @param f Bitmask of functions that should be called
* @return status code: 1 on success
*/
int launch_each_module (sdplist *list, void **data);
int foreach_module (sdplist *list, void **data, function f);

/**
* Give a list of loaded modules by type
Expand Down
15 changes: 10 additions & 5 deletions src/parser.c
Expand Up @@ -803,12 +803,15 @@ dump_cmd (command_line *ptrc)
command *ptr = ptrc->content;
if (ptr != NULL) {
int i;
fprintf (stdout, "cmd: %s\n", ptr->cmd);
fprintf (stdout, "argc: %d\n", ptr->argc);
sd_info ("cmd: %s\n", ptr->cmd);
sd_info ("argc: %d\n", ptr->argc);
/* fprintf (stdout, "cmd: %s\n", ptr->cmd);*/
/* fprintf (stdout, "argc: %d\n", ptr->argc);*/
for (i = 0; i < ptr->argc; i++)
fprintf (stdout, "argv[%d]: %s (%d)\n", i,
sd_info ("argv[%d]: %s (%d)\n", i,
ptr->argv[i],
ptr->protected[i]);
/* fprintf (stdout, "argv[%d]: %s (%d)\n", i,*/
}
}
}
Expand All @@ -821,10 +824,12 @@ dump_line (input_line *ptr)
command_line *tmp = ptr->head;
int cpt = 0;
if (ptr->size > 0)
fprintf (stdout, "nb commands: %d\n", ptr->size);
sd_info ("nb commands: %d\n", ptr->size);
/* fprintf (stdout, "nb commands: %d\n", ptr->size);*/
while (tmp != NULL)
{
fprintf (stdout, "=== Dump cmd n°%d ===\n", ++cpt);
sd_info ("=== Dump cmd n°%d ===\n", ++cpt);
/* fprintf (stdout, "=== Dump cmd n°%d ===\n", ++cpt);*/
dump_cmd (tmp);
tmp = tmp->next;
}
Expand Down
41 changes: 37 additions & 4 deletions src/plugin.c
Expand Up @@ -213,6 +213,7 @@ new_sdplugindata (void)
ret->init = NULL;
ret->clean = NULL;
ret->main = NULL;
ret->config = NULL;
ret->lib = NULL;

return ret;
Expand Down Expand Up @@ -305,6 +306,8 @@ copy_sdplugindata (sdplugindata *src)
memcpy (&(ret->clean), &(src->clean), sizeof (ret->clean));
if (src->main != NULL)
memcpy (&(ret->main), &(src->main), sizeof (ret->main));
if (src->config != NULL)
memcpy (&(ret->config), &(src->config), sizeof (ret->config));
ret->name = src->name;
ret->prio = src->prio;
ret->type = src->type;
Expand Down Expand Up @@ -349,15 +352,22 @@ is_module_present (const char *name)

/* calls the main functions of the plugins present in the given list */
int
launch_each_module (sdplist *list, void **data)
foreach_module (sdplist *list, void **data, function f)
{
int r = 1;
if (list != NULL)
{
sdplugin *tmp = list->head;
while (tmp != NULL && r == 1)
{
r = tmp->content->main (data);
if ((f & CLEAN) && tmp->content->clean != NULL)
tmp->content->clean ();
if (f & INIT)
tmp->content->init (tmp->content);
if (f & MAIN)
r = tmp->content->main (data);
if ((f & CONFIG) && tmp->content->config != NULL)
tmp->content->config (data);
tmp = tmp->next;
}
}
Expand All @@ -384,7 +394,7 @@ get_modules_list_by_type (sdplugin_type type)
/* first of all, are there any plugins of the given type? */
while (curr != NULL)
{
if (curr->content->type == type)
if (curr->content->type & type)
cpt++;
curr = curr->next;
}
Expand All @@ -400,7 +410,7 @@ get_modules_list_by_type (sdplugin_type type)
while (curr != NULL && i < cpt)
{
/* we store a copy of all the plugins we found in a temporary array */
if (curr->content->type == type)
if (curr->content->type & type)
{
tmp[i] = copy_sdplugin (curr);
i++;
Expand Down Expand Up @@ -521,6 +531,14 @@ sd_plugin_main (void **data)
return 1;
}
void
sd_plugin_config (void **data)
{
(void) data;
return;
}
void
sd_plugin_clean (void)
{
Expand Down Expand Up @@ -580,12 +598,27 @@ load_module (const char *path)
error = dlerror ();
if (error != NULL)
{
sd_debug ("%s\n", error);
ptr->clean = NULL;
}
else
{
memcpy (&(ptr->clean), &func, sizeof (ptr->clean));
}

/* optionaly we search for a config function */
func = dlsym (ptr->lib, "sd_plugin_config");
error = dlerror ();
if (error != NULL)
{
sd_debug ("%s\n", error);
ptr->config = NULL;
}
else
{
memcpy (&(ptr->config), &func, sizeof (ptr->config));
}

ptr->loaded = TRUE;

/* here we have a plugin with all the requirements */
Expand Down
23 changes: 16 additions & 7 deletions src/sdlib/plugin.h
Expand Up @@ -42,22 +42,31 @@ typedef struct _sdplugindata sdplugindata;

typedef enum
{
PROMPT = 1,
PARSING,
BUILTIN,
UNKNOWN
UNKNOWN = 0x00,
PROMPT = 0x01,
PARSING = 0x02,
BUILTIN = 0x04
} sdplugin_type;

typedef enum
{
MAIN = 0x01,
INIT = 0x02,
CLEAN = 0x04,
CONFIG = 0x08
} function;

struct _sdplugindata
{
const char *name;
int prio;
unsigned int loaded;
sdplugin_type type;

void (*init) (sdplugindata *plugin);
void (*clean) (void);
int (*main) (void **data);
void (*init) (sdplugindata *plugin);
void (*clean) (void);
int (*main) (void **data);
void (*config) (void **data);

void *lib;
};
Expand Down
13 changes: 1 addition & 12 deletions src/xutils.c
Expand Up @@ -45,7 +45,6 @@
#include <err.h>
#include <stdarg.h>
#include <limits.h>
#include <alloca.h>

#include "xutils.h"

Expand Down Expand Up @@ -116,15 +115,6 @@ xmalloc (size_t size)
return ret;
}

void *
xalloca (size_t size)
{
void *ret = alloca (size);
if (ret == NULL)
err (2, "xalloca can not allocate %lu bytes", (u_long) size);
return ret;
}

void *
xcalloc (size_t nmem, size_t size)
{
Expand Down Expand Up @@ -172,6 +162,7 @@ xstrcmp (const char *c1, const char *c2)
size_t s1 = xstrlen (c1);
size_t s2 = xstrlen (c2);

/* little optimisation based on the strings length */
if (s1 == 0 && s2 == 0)
return 0;
if (s1 == 0)
Expand Down Expand Up @@ -437,6 +428,4 @@ sd_print (log level, const char *msg, ...)
vfprintf (out, msg, args);
va_end (args);
}
else
fprintf (out, msg);
}
16 changes: 14 additions & 2 deletions src/xutils.h
Expand Up @@ -36,6 +36,10 @@

#include "structs.h"

/* hate to do that, but I don't have the choice because of my alloca macro */
#include <err.h>
#include <alloca.h>

/* define a few useful MACRO */
#define TRUE 1
#define FALSE 0
Expand Down Expand Up @@ -84,9 +88,17 @@ void *xmalloc (size_t size);
/**
* Allocates temporary memory or exit and display an error
* @param size Requested size to allocate
* @return The address pointing to the memory-space
*/
void *xalloca (size_t size);
/**
* alloca() is allocated within the stack frame, that space is automatically
* freed if the function returns. that is the reason why we use a macro here
*/
#define xalloca(ret,size) \
do{ \
ret = alloca (size); \
if (ret == NULL) \
err (2, "xalloca can not allocate %lu bytes",(u_long) size); \
}while(0)

/* Same as above for the calloc function */
void *xcalloc (size_t nmem, size_t size);
Expand Down

0 comments on commit 58da8a6

Please sign in to comment.