|
From: <cli...@li...> - 2007-06-21 16:07:07
|
Revision: 68
http://cligg.svn.sourceforge.net/cligg/?rev=68&view=rev
Author: sithhell
Date: 2007-06-21 09:07:02 -0700 (Thu, 21 Jun 2007)
Log Message:
-----------
various error correction, and additional implementations for cligg_remote
Modified Paths:
--------------
src/bin/cligg.c
src/bin/cligg_ipc.c
src/bin/cligg_mainloop.c
src/bin/cligg_modulehandler.c
src/bin/cligg_modulehandler.h
src/bin/cligg_remote.c
src/lib/cligg_list.c
src/modules/print/print.c
src/modules/read/read.c
Modified: src/bin/cligg.c
===================================================================
--- src/bin/cligg.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/bin/cligg.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -77,7 +77,6 @@
/* exit handlers ... */
/* setting up ipc */
- printf("IPC\n");
cligg_ipc_start();
/* starting the event loop */
Modified: src/bin/cligg_ipc.c
===================================================================
--- src/bin/cligg_ipc.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/bin/cligg_ipc.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -9,6 +9,7 @@
#include <errno.h>
#include <cligg_ipc.h>
#include <cligg_eventhandler.h>
+#include <cligg_modulehandler.h>
#define SOCKET_NAME "/tmp/cligg"
#define QLEN 10
@@ -17,6 +18,7 @@
static int fd;
static void *cligg_ipc(void *);
+static int send_cb(void *);
int cligg_ipc_start(void)
{
@@ -53,8 +55,6 @@
exit(EXIT_FAILURE);
}
- printf("socket fd %d\n", fd);
-
return TRUE;
}
@@ -72,18 +72,42 @@
struct sockaddr_un un;
char buf[2*1024];
char *message[2];
+ char **test;
+ int send = FALSE;
+ int size;
while(1) {
+ int i = 0;
len = sizeof(un);
if((clifd = accept(fd, (struct sockaddr *)&un, &len)) < 0) {
perror("accept");
continue;
}
- len = read(clifd, buf, 1024);
- message[0] = buf;
- message[1] = buf+1024;
- cligg_trigger_event(message[0], message[1]);
- close(clifd);
+ len = read(clifd, buf, 2*1024);
+ if(len == 0) continue;
+ message[0] = (char *)buf;
+ message[1] = (char *)buf+1024;
+
+ if(strcmp(message[1], "") == 0) message[1] = NULL;
+
+
+ if(strcmp(message[0], "list_modules") == 0) {
+ test = cligg_list_modules(&size);
+ write(clifd, &size, sizeof(int));
+ while(test[i] != NULL) {
+ size = strlen(test[i]);
+ test[i][size] = 0;
+ size = write(clifd, test[i], PATH_MAX);
+ i++;
+ }
+ send = TRUE;
+ }
+ else
+ cligg_trigger_event(message[0], message[1]);
+
+ if(!send) {
+ close(clifd);
+ }
}
return NULL;
}
Modified: src/bin/cligg_mainloop.c
===================================================================
--- src/bin/cligg_mainloop.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/bin/cligg_mainloop.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -45,7 +45,7 @@
void *
cligg_loop(void *arg)
{
- cligg_event_data *new;
+ static cligg_event_data *new;
cligg_event_cb cb = NULL;
while(TRUE) {
Modified: src/bin/cligg_modulehandler.c
===================================================================
--- src/bin/cligg_modulehandler.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/bin/cligg_modulehandler.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -54,18 +54,28 @@
int ret = TRUE;
cligg_module *new = cligg_module_new(path);
+ printf("\n\nloading module ...\n");
+
if(new == NULL) {
perror("Error loading module");
dlerror();
return FALSE;
}
-
+ if(pthread_mutex_lock(&module_lock) != 0) {
+ return FALSE;
+ }
if(cligg_list_element_find(module_list, cmp, new) > 0) {
fprintf(stderr, "Couldn't add module %s: already there\n", new->path);
cligg_module_del(new);
dlerror();
+ if(pthread_mutex_unlock(&module_lock) != 0) {
+ return FALSE;
+ }
return FALSE;
}
+ if(pthread_mutex_unlock(&module_lock) != 0) {
+ return FALSE;
+ }
/* FIXME this is better, perhaps, but creates some memory leaks */
/*if((test = dlopen(path, RTLD_LAZY | RTLD_NOLOAD)) != NULL) {
dlclose(test);
@@ -113,6 +123,8 @@
int ret = TRUE;
dlerror();
+ printf("\n\nunloading module ...\n");
+
if(pthread_mutex_lock(&module_lock) != 0) {
fprintf(stderr, "Couldn't obtain module lock!\n");
cligg_module_del(tmp);
@@ -126,7 +138,7 @@
ret = FALSE;
}
else {
- tmp = (cligg_module *)module_list->current->data;
+ tmp = (cligg_module *)cligg_list_element_get_current(module_list);
if(dlclose(tmp->handle) != 0)
fprintf(stderr, "Error unloading module: %s\n", dlerror());
@@ -146,6 +158,40 @@
return ret;
}
+char **cligg_list_modules(int *size)
+{
+ char **list;
+ char *path;
+ int i = 0;
+ int len;
+ int total = 0;
+ cligg_list_element *tmp;
+
+ if(size == NULL) return NULL;
+
+ list = (char **)malloc(module_list->size * sizeof(char *) + 1);
+ if(list == NULL) return 0;
+
+ tmp = module_list->first;
+ while(tmp != NULL) {
+ path = ((cligg_module *)tmp->data)->path;
+ len = strlen(path);
+ total += len;
+
+ list[i] = (char *)malloc(len * sizeof(char));
+ if(list[i] == NULL) return 0;
+
+ strcpy(list[i], path);
+ i++;
+ tmp = tmp->next;
+ }
+ list[i] = NULL;
+
+ *size = module_list->size;
+
+ return list;
+}
+
static int cmp(void *a, void *b)
{
return strcmp(((cligg_module *)a)->path, ((cligg_module *)b)->path);
@@ -158,6 +204,5 @@
static int module_unreg_event_cb(void *data)
{
- printf("unregister\n");
- return cligg_unregister_module((cligg_module *)data);
+ return cligg_unregister_module(cligg_module_new((char *)data));
}
Modified: src/bin/cligg_modulehandler.h
===================================================================
--- src/bin/cligg_modulehandler.h 2007-06-19 18:31:55 UTC (rev 67)
+++ src/bin/cligg_modulehandler.h 2007-06-21 16:07:02 UTC (rev 68)
@@ -8,5 +8,6 @@
int cligg_register_module(char *);
int cligg_unregister_module(cligg_module *);
+char **cligg_list_modules(int *);
#endif
Modified: src/bin/cligg_remote.c
===================================================================
--- src/bin/cligg_remote.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/bin/cligg_remote.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -13,15 +13,20 @@
#define SOCKET_NAME "/tmp/cligg"
static void help(void);
+static void deep_help(const char *, const char *, const char *);
static int cmp_opt(const char *, const char *, const char *);
+static void send_event(int, const char *, const char *);
int main(int argc, char **argv)
{
int fd;
int len;
int err;
+ int noopt = TRUE;
struct sockaddr_un un;
- char message[2][1024];
+ char buf[PATH_MAX];
+ int i = 0;
+ int readlen;
if(argc < 2) {
help();
@@ -47,10 +52,41 @@
return EXIT_FAILURE;
}
if(cmp_opt("-q", "--quit", argv[1])) {
- strcpy(message[0], "quit_cligg");
- strcpy(message[1], "");
- write(fd, message, strlen(message[0])+strlen(message[1]));
+ noopt = FALSE;
+ send_event(fd, "quit_cligg", "");
}
+ if(cmp_opt("-lm", "--load-module", argv[1])) {
+ if(argc < 3) {
+ deep_help(argv[0], argv[1], "module_name");
+ exit(EXIT_FAILURE);
+ }
+ noopt = FALSE;
+ send_event(fd, "load_module", argv[2]);
+ }
+ if(cmp_opt("-um", "--unload-module", argv[1])) {
+ if(argc < 3) {
+ deep_help(argv[0], argv[1], "module_name");
+ exit(EXIT_FAILURE);
+ }
+ noopt = FALSE;
+ send_event(fd, "unload_module", argv[2]);
+ }
+ if(cmp_opt("-l", "--list-modules", argv[1])) {
+ noopt = FALSE;
+ send_event(fd, "list_modules", "");
+ read(fd, &readlen, sizeof(int));
+ printf("Geladene Module:\n");
+ for(i = 0; i < readlen; i++) {
+ len = read(fd, buf, PATH_MAX);
+ buf[len] = 0;
+ printf("\t- %s\n", buf);
+ }
+ }
+ if(noopt) {
+ help();
+ close(fd);
+ exit(EXIT_FAILURE);
+ }
close(fd);
return EXIT_SUCCESS;
}
@@ -58,16 +94,32 @@
static void help(void)
{
printf("Usage: \n");
- printf("\t -h, --help show this help\n");
- printf("\t -q, --quit quit cligg\n");
- printf("\t -lm --load-module load module\n");
- printf("\t -um --unload-module unload module\n");
+ printf("\t -h, --help show this help\n");
+ printf("\t -q, --quit quit cligg\n");
+ printf("\t -l, --list-modules list loaded modules\n");
+ printf("\t -lm, --load-module load module\n");
+ printf("\t -um, --unload-module unload module\n");
}
+static void deep_help(const char *arg0, const char *arg1, const char *message)
+{
+ printf("Usage: \n");
+ printf("\t%s %s %s\n", arg0, arg1, message);
+}
+
static int cmp_opt(const char *opt1, const char *opt2, const char *arg)
{
if((strcmp(opt1, arg) == 0) || (strcmp(opt2, arg) == 0))
return TRUE;
return FALSE;
}
-
+
+static void send_event(int fd, const char *mes1, const char *mes2)
+{
+ char message[2][1024];
+ strcpy(message[0], mes1);
+ strcpy(message[1], mes2);
+ printf("sending: %s %s (%d)\n",message[0], message[1],
+ strlen(message[0])+strlen(message[1]));
+ write(fd, message, 2*1024);
+}
Modified: src/lib/cligg_list.c
===================================================================
--- src/lib/cligg_list.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/lib/cligg_list.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -122,7 +122,7 @@
}
new_element->data = data;
new_element->next = NULL;
- new_element->prev = NULL;
+ /*new_element->prev = NULL;*/
/*append to list*/
new_element->prev = list->last;
@@ -153,7 +153,7 @@
return FALSE;
}
new_element->data = data;
- new_element->next = NULL;
+ /*new_element->next = NULL;*/
new_element->prev = NULL;
/* prepend to list */
@@ -279,14 +279,18 @@
if(list->current->prev != NULL) {
list->current->prev->next = list->current->next;
}
- else
+ else {
list->first = list->current->next;
+ /*list->first->prev = NULL;*/
+ }
if(list->current->next != NULL)
list->current->next->prev = list->current->prev;
else {
- list->first = NULL;
- list->last = NULL;
+ /*list->first = NULL;
+ list->last = NULL;*/
+ list->last = list->current->prev;
+ /*list->last->next = NULL;*/
}
list->current = NULL;
@@ -348,7 +352,7 @@
CLIGG int cligg_list_element_find(cligg_list *list, compare_func_cb compare_func, void *data)
{
- int i = 0;
+ int i = 1;
cligg_list_element *tmp = NULL;
if(cligg_list_isempty(list)) {
@@ -359,22 +363,23 @@
list->current = list->last;
return list->size;
}
- if(compare_func(list->first->data, data) == 0) {
+ /*if(compare_func(list->first->data, data) == 0) {
list->current = list->first;
return 1;
- }
+ }*/
tmp = list->first;
- while(tmp->next != NULL && (compare_func(tmp->data, data) == 0)) {
+ while(tmp != NULL){/* && (compare_func(tmp->data, data) != 0)) {*/
+ if(compare_func(tmp->data, data) == 0) {
+ printf("YAY!\n");
+ list->current = tmp;
+ return i;
+ }
tmp = tmp->next;
i++;
}
- list->current = tmp;
-
- if(i > list->size)
- return -1;
- return i;
+ return -1;
}
CLIGG int cligg_list_element_add_sorted(cligg_list *list, compare_func_cb compare_func, void *data)
Modified: src/modules/print/print.c
===================================================================
--- src/modules/print/print.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/modules/print/print.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -19,5 +19,6 @@
cligg_module_finit print_finit(void)
{
+ cligg_unregister_event("print");
fprintf(stderr, "Print Module is unloaded!\n");
}
Modified: src/modules/read/read.c
===================================================================
--- src/modules/read/read.c 2007-06-19 18:31:55 UTC (rev 67)
+++ src/modules/read/read.c 2007-06-21 16:07:02 UTC (rev 68)
@@ -28,5 +28,6 @@
cligg_module_finit print_finit(void)
{
+ cligg_unregister_event("read");
fprintf(stderr, "Read Module is unloaded!\n");
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|