diff options
author | Matej Cepl <mcepl@redhat.com> | 2008-02-26 17:27:08 +0100 |
---|---|---|
committer | Matej Cepl <mcepl@redhat.com> | 2008-02-26 17:27:08 +0100 |
commit | c50ce75f2e06b6b8abe49e34775d343b74f7e8cd (patch) | |
tree | 14c314e39296b0c3abe781e4dac194949e220136 /wlp/C | |
download | pyg-0.9.7.tar.gz |
The original upstream from Debian orig.tar.gz tarball.0.9.7
Diffstat (limited to 'wlp/C')
-rw-r--r-- | wlp/C/Makefile | 43 | ||||
-rw-r--r-- | wlp/C/commands.l | 75 | ||||
-rw-r--r-- | wlp/C/commands.y | 110 | ||||
-rw-r--r-- | wlp/C/macro.h | 44 | ||||
-rw-r--r-- | wlp/C/structs.c | 164 | ||||
-rw-r--r-- | wlp/C/structs.h | 35 | ||||
-rw-r--r-- | wlp/C/test.c | 113 | ||||
-rw-r--r-- | wlp/C/wlp.c | 183 | ||||
-rw-r--r-- | wlp/C/yytest.c | 25 |
9 files changed, 792 insertions, 0 deletions
diff --git a/wlp/C/Makefile b/wlp/C/Makefile new file mode 100644 index 0000000..6bc8fc3 --- /dev/null +++ b/wlp/C/Makefile @@ -0,0 +1,43 @@ +CC=gcc +AR=ar +FLEX=flex +YACC=bison + +CCOPTS=-Wall -ansi +CCSHARED=-fPIC +AROPTS=-rs +FLEXOPTS= +YACCOPTS=-d + +SRCDIR=. +BINDIR=. + +OBJFILE=structs.o commands.tab.o lex.yy.o + +all: archive bin + +# archive file for python module +archive: structs bison flex /usr/lib/libfl.a macro.h structs.h commands.tab.h + $(AR) $(AROPTS) $(BINDIR)/wlp.a $(OBJFILE) \ + /usr/lib/libfl.a + +# binary (executable) file for testing +executable: bin + +bin: structs bison flex /usr/lib/libfl.a macro.h structs.h commands.tab.h + $(CC) $(CCSHARED) $(CCOPTS) $(OBJFILE) \ + yytest.c /usr/lib/libfl.a -o ./yytest + +flex: + $(FLEX) $(FLEXOPTS) commands.l + $(CC) $(CCSHARED) -c lex.yy.c -o lex.yy.o + +bison: + $(YACC) $(YACCOPTS) -d commands.y -b commands + $(CC) $(CCSHARED) -c commands.tab.c -o commands.tab.o + +structs: + $(CC) $(CCSHARED) $(CCOPTS) -c structs.c -o structs.o + +clean: + -rm $(OBJFILE) lex.yy.c commands.tab.h commands.tab.c wlp.a yytest diff --git a/wlp/C/commands.l b/wlp/C/commands.l new file mode 100644 index 0000000..8f0f28e --- /dev/null +++ b/wlp/C/commands.l @@ -0,0 +1,75 @@ +%{ +/* Lex analyzer for bison grammar */ + +#include "commands.tab.h" +/*#define DEBUG*/ +#include "macro.h" +%} + +OWNER "<"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9._-]+">" +VAL ['`"][a-zA-Z0-9@_+.<>() -]+['`"] +VAR [a-zA-Z0-9_<>-]+[:]? + +%option noyywrap + +%% + + + + +{OWNER} { + DBG("OWNER %s\n",yytext); + yylval.text=yytext; + return(OWNERID); + } + + +{VAL} { + DBG("VAL %s\n",yytext); + yylval.text=yytext; + return(VALID); + } + +{VAR} { + DBG("VAR %s\n",yytext); + yylval.text=yytext; + return(VARID); + } + + +"{" | +"}" | +"=" { + yylval.text=yytext; + return(*yytext); + } + + +"#"[^\n]* | +"\n" | +[[:space:]] ; /* comments 'til EOL and strip all spaces and \n */ + + +"." { + DBG("NOTSTR %s\n",yytext); + yylval.text=yytext; + return(ERROR); + } + + +%% + +#include "structs.h" + +extern struct wlp_list_t *list; + +int parse(FILE *file) +{ + struct wlp_node_t *tmp; + int count; + yyin=file; + + DBG("go!\n"); + yyparse(); + DBG("EOF found\n"); +} diff --git a/wlp/C/commands.y b/wlp/C/commands.y new file mode 100644 index 0000000..8b720d7 --- /dev/null +++ b/wlp/C/commands.y @@ -0,0 +1,110 @@ + +%{ +/*#define YYSTYPE char**/ +/*#define DEBUG*/ +#include "macro.h" +%} + +%union { + char *text; + char c; +} + +%token <text> VARID +%token <text> VALID +%token <text> OWNERID + +%token ERROR +%token EOFTOK + + +%{ +char left[80], right[80], owner[80]; +char type = 0; /*unused*/ +%} + +%% + + +block: + blockstatement + | block blockstatement + ; + +blockstatement: + owner '{' commandline '}' + ; + +commandline: + command | commandline command + ; + +command: + varpart '=' valpart { found(left,right,owner); } + ; + +owner: + OWNERID { + DBG("Owner %s\n",$1); + strncpy(owner,$1,strlen($1)+1); + } + ; + +varpart: + VARID { + DBG("Left %s\n",$1); + strncpy(left,$1,strlen($1)+1); + } + ; + +valpart: + VALID { + DBG("Right %s\n",$1); + strncpy(right,$1,strlen($1)+1); + } + ; + +%% + +#include <ctype.h> +#include <stdio.h> +#include "structs.h" + +extern struct wlp_list_t *list; + +int yyerror (char *s) /* Called by yyparse on error */ +{ + printf ("error: %s\n", s); + return 1; +} + +int found(const char* left, const char* right, const char *owner) +{ + static struct wlp_node_t *node; + + /* alloc node with non-empty fields (ie alloc them too)*/ + node = wlpn_alloc(FALSE); + if(!node) { + DBG("wlpn_alloc in found returned NULL\n"); + } + + strncpy(node->right,right,strlen(right)); + strncpy(node->left,left,strlen(left)); + strncpy(node->owner,owner,strlen(owner)); + + #ifndef WITHQUOTES + /* remove quotes of value part */ + node->right += 1; + node->right[strlen(node->right)-1] = '\0'; + #endif + #ifndef WITHANGBRACKETS + /* remove angle brackets of owner part */ + node->owner += 1; + node->owner[strlen(node->owner)-1] = '\0'; + #endif + + if(!list) + list = wlpl_init(node); + else + wlpn_add(list,node); +} diff --git a/wlp/C/macro.h b/wlp/C/macro.h new file mode 100644 index 0000000..303b3c6 --- /dev/null +++ b/wlp/C/macro.h @@ -0,0 +1,44 @@ +/* + * macro.h - Copyright 2000, 2001 Cosimo Alfarano <Alfarano@CS.UniBo.It> + * You can use this software under the terms of the GPL. If we meet some day, + * and you think this stuff is worth it, you can buy me a beer in return. + * + * Thanks to md for this useful formula. Beer is beer. + */ + + +#ifndef _macro_h_ +#define _macro_h_ + +#define LINELEN 2048 +#define TRUE 1 +#define FALSE 0 + +/* +#define WITHQUOTES +#define WITHANGBRACKETS +*/ + +#define ERR(a,b...) fprintf(stderr, a, ## b) + +/* Define it for debug info on stderr (better if in .c module ...) */ +/* +#ifndef DEBUG + #define DEBUG +#endif +*/ + + +#ifdef DEBUG + #define DBG(a...) fprintf(stderr, ## a) + /* for a verbose debug*/ + #define VDBG(a,b...) fprintf(stderr, "%s(): " a, __FUNCTION__ , ## b) +#else + #define DBG(a...) + #define VDBG(a...) +#endif + + +#endif + +/* EOF */ diff --git a/wlp/C/structs.c b/wlp/C/structs.c new file mode 100644 index 0000000..8c33443 --- /dev/null +++ b/wlp/C/structs.c @@ -0,0 +1,164 @@ +/* + * structs.c - Copyright 2000 by Cosimo Alfarano <Alfarano@CS.UniBo.It> + * You can use this software under the terms of the GPL. If we meet some day, + * and you think this stuff is worth it, you can buy me a beer in return. + * + * Thanks to md for this useful formula. Beer is beer. + */ + + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> + +#include "structs.h" +#include "macro.h" + +struct wlp_node_t *wlpn_add(struct wlp_list_t *list,struct wlp_node_t *node) +{ + struct wlp_node_t *ret; + + if(node) { + node->next = list->head; + node->prev = list->tail; + + list->tail->next = node; + list->head->prev = node; + + list->tail = node; + + list->count++; + + ret = node; + } else { + DBG("cannot add %s %s to list. NULL pointer?\n", + node->left,node->right); + ret = NULL; + } + + return ret; +} + +void wlpn_free(struct wlp_node_t *node) +{ + free(node->left); + free(node->right); + free(node->owner); + free(node); +} + + +struct wlp_node_t *wlpn_alloc(const char empty) +{ + static struct wlp_node_t *node; + + node = calloc(sizeof(struct wlp_node_t),1); + + if (!node) { + perror("wlpn_create malloc"); + + /* calloc set *node to zero, it I don't want alloc anything, + leave it untouched, else alloc fields */ + } else if (!empty) { + node->left = calloc(LINELEN+1,1); + if (!node->left) { + perror("wlpn_create malloc (left)"); + free(node); + node = NULL; + } + + /*if node is NULL, previous calloc returned error...*/ + if(node && !(node->right = calloc(LINELEN+1,1))) { + perror("wlpn_create malloc (right)"); + free(node); + node = NULL; + } + + if(node && !(node->owner = calloc(LINELEN+1,1))) { + perror("wlpn_create malloc (owner)"); + free(node); + node = NULL; + } + } + return node; +} + +struct wlp_list_t *wlpl_init(struct wlp_node_t *node) +{ + static struct wlp_list_t *list; + + list = malloc(sizeof(struct wlp_list_t)); + + if (list) { + list->head = node; + list->tail = list->head; + + list->head->next = list->head; + list->head->prev = list->head; + + list->count=1; + } else { + perror("wlpl_init malloc"); + return NULL; + } + + return list; +} + + +struct wlp_node_t *wlpn_searchowner(struct wlp_list_t *mbl,const char *owner) +{ + struct wlp_node_t *ret; + + DBG("searching for %s\n",owner); + + if(!mbl) + ret = NULL; + else { + int found = FALSE; + ret = mbl->head; + + do { + if(!strcmp(owner,ret->owner)) { + DBG("found!\n"); + found = TRUE; + } else { + DBG("not found: %s\n",ret->onwer); + ret = ret->next; + } + } while(ret != mbl->head && !found); + + if(!found) + ret = NULL; + } + + DBG("%s\n", (ret)?ret->owner:"not found"); + + return ret; +} + + +struct wlp_node_t *wlpn_extract(struct wlp_list_t *list,struct wlp_node_t *node) +{ + struct wlp_node_t *ret; + + if(list && node) { + node->prev->next = node->next; + node->next->prev = node->prev; + + if(list->tail == node) + list->tail = node->prev; + if(list->head == node) + list->head = node->next; + + list->count--; + + ret = node; + } else { + DBG("wlpn_extract: list addr %l and node %l (one is NULL)\n",list,addr) + ret = NULL; + } + + return ret; +} diff --git a/wlp/C/structs.h b/wlp/C/structs.h new file mode 100644 index 0000000..92036a7 --- /dev/null +++ b/wlp/C/structs.h @@ -0,0 +1,35 @@ +/* + * structs.h - Copyright 2000, 2001 by Cosimo Alfarano <Alfarano@CS.UniBo.It> + * You can use this software under the terms of the GPL. If we meet some day, + * and you think this stuff is worth it, you can buy me a beer in return. + * + * Thanks to md for this useful formula. Beer is beer. + */ + +#ifndef _structs_h_ +#define _structs_h_ + +typedef struct wlp_node_t { + char *left,*right; + char *owner; + char type; /* unused */ + struct wlp_node_t *next, *prev; +} wlp_node_t; + +typedef struct wlp_list_t { + int count; + struct wlp_node_t *head, *tail; +} wlp_list_t; + + +/* white list parser data structure manipulation */ +struct wlp_list_t *wlpl_init(struct wlp_node_t *node); +struct wlp_node_t *wlpn_alloc(const char empty); +void wlpn_free(struct wlp_node_t *node); +struct wlp_node_t *wlpn_add(struct wlp_list_t *wlpl,struct wlp_node_t *wlpn); +struct wlp_node_t *wlpn_extract(struct wlp_list_t *wlpl,struct wlp_node_t *wlpn); +/*struct wlp_node_t *wlpn_search(struct wlp_list_t *wlpl,const char id);*/ + +#endif /* _structs_h_ */ + +/* EOF */ diff --git a/wlp/C/test.c b/wlp/C/test.c new file mode 100644 index 0000000..47717b5 --- /dev/null +++ b/wlp/C/test.c @@ -0,0 +1,113 @@ +#include <python2.1/Python.h> /* should be modified to be pythonX.Y */ +#include <stdio.h> +#include <unistd.h> + +#include "structs.h" +#include "macro.h" + +/* first declare static functions */ + +struct wlp_list_t *list; + +static FILE *fd = NULL; + +static PyObject *node2dict(struct wlp_node_t *node); + +static PyObject *wlp_setfilebyname(PyObject *self, PyObject *args) { + char *file; + + DBG("setfilebyname\n"); + if (!PyArg_ParseTuple(args, "s", &file)) + return NULL; + + fd = fopen(file,"r"); + + return Py_None; +} + +static PyObject *wlp_setfilebyfd(PyObject *self, PyObject *args) { + PyObject *file = NULL; + + if (!PyArg_ParseTuple(args, "O", &file)) + return NULL; + + if(!file) + return NULL; + + if(!PyFile_Check(file)) + return NULL; + + fd = PyFile_AsFile(file); + + return Py_None; + +} + + + +static PyObject *wlp_mklist(PyObject *self, PyObject *args) { + struct wlp_node_t *tmp; + int count; + + PyObject *pylist = NULL; + + DBG("a\n"); + parse(fd); + + DBG("count %d\n"list->count); + pylist = PyList_New(0); + + DBG("a\n"); + if(!pylist) + return NULL; + + DBG("a\n"); + if(list) + for(tmp = list->head, count=0; + tmp != list->head || count == 0; + tmp = tmp->next, count++) { + DBG("FOUND(%d) '%s' ('%s': '%s')\n",count,tmp->owner,tmp->left,tmp->right); + if(PyList_Append(pylist,node2dict(tmp))==-1) { + DBG("List failed\n"); + return NULL; + } + DBG("a\n"); + } + + return pylist; +} + + +static PyObject *node2dict(struct wlp_node_t *node) { + PyObject *dict = PyDict_New(); + + if(!dict) + return NULL; + + PyDict_SetItem(dict, + Py_BuildValue("s","owner"), + Py_BuildValue("s",node->owner)); + + PyDict_SetItem(dict, + Py_BuildValue("s",node->left), + Py_BuildValue("s",node->right)); + + return dict; + +} + +/* second a table with methods/functions matching */ + +static PyMethodDef wlp_methods[] = { + {"mklist", wlp_mklist, METH_VARARGS}, + {"setfilebyname", wlp_setfilebyname, METH_VARARGS}, + {"setfilebyfd", wlp_setfilebyfd, METH_VARARGS}, + {NULL,NULL} +}; + + +/* last the init function, the only one non-static */ + +void initwlp() { + (void) Py_InitModule("wlp",wlp_methods); +} diff --git a/wlp/C/wlp.c b/wlp/C/wlp.c new file mode 100644 index 0000000..8c0779d --- /dev/null +++ b/wlp/C/wlp.c @@ -0,0 +1,183 @@ +/* + * wlp.c - Copyright 2000, 2001 by Cosimo Alfarano <Alfarano@CS.UniBo.It> + * You can use this software under the terms of the GPL. If we meet some day, + * and you think this stuff is worth it, you can buy me a beer in return. + * + * Thanks to md for this useful formula. Beer is beer. + */ + +//#include <python/Python.h> +#include <Python.h> +#include <stdio.h> +#include <unistd.h> + +#include "structs.h" +#include "macro.h" + + +static FILE *fd = NULL; + +struct wlp_list_t *list; + +static PyObject *node2dict(struct wlp_node_t *node); +static PyObject *node2dict2(struct wlp_node_t *node); + + + + +/* + * wlp_setfilebyname(): get FILE* fd from filename string. + */ + +static PyObject *wlp_setfilebyname(PyObject *self, PyObject *args) { + char *file; + + + DBG("setfilebyname\n"); + if (!PyArg_ParseTuple(args, "s", &file)) + return NULL; + + if(fd = fopen(file,"r")) { + return Py_None; + } else { + PyErr_SetFromErrno(PyExc_Exception); +/* + PyErr_SetString(PyExc_Exception, + (errno<=sys_nerr-1)? + sys_errlist[errno]: + "Unknown Error on fopen() of confifuration file"); +*/ + return NULL; + } +} + + + +/* + * wlp_setfilebydf(): get FILE* fd from FileObject. + */ + +static PyObject *wlp_setfilebyfd(PyObject *self, PyObject *args) { + PyObject *file = NULL; + + if(!PyArg_ParseTuple(args, "O", &file)) + return NULL; + + if(!file) + return NULL; + + if(!PyFile_Check(file)) + return NULL; + + fd = PyFile_AsFile(file); + + return Py_None; +} + +/* + * wlp_mkdict(): make a dictonary of the form + * {ownername: {var1: val1, var2: val2,...}} + */ + +static PyObject *wlp_mkdict(PyObject *self, PyObject *args) { + PyObject *pydicttmp = NULL; + PyObject *pydict = PyDict_New(); + struct wlp_node_t *tmp; + int count; + + if(!pydict) + return NULL; + + /* fopen()*/ + if(fd) + parse(fd); + else + return Py_None; + + if(list) + for(tmp = list->head, count = 0; + tmp != list->head || count == 0; + tmp = tmp->next, count++) { + DBG("FOUND(%d) '%s' ('%s': '%s')\n",count,tmp->owner,tmp->left,tmp->right); + pydicttmp = PyDict_GetItem(pydict, + PyString_FromString(tmp->owner)); + + if(!pydicttmp) { + DBG("%s: owner not found, create new item\n", + tmp->owner); + PyDict_SetItemString(pydict, + tmp->owner, + node2dict(tmp)); + } else { + DBG("%s: owner found,appendig items\n", + tmp->owner); + PyDict_SetItemString(pydicttmp, + tmp->left, + Py_BuildValue("s",tmp->right)); + PyDict_SetItemString(pydict, + tmp->owner, + pydicttmp); + } + } + + return pydict; +} + +/* + * node2dict(): transoform a wlp_node_t node in a python dictionary of the form + * { var: val } + * to be used by mkdict() + */ + +static PyObject *node2dict(struct wlp_node_t *node) { + PyObject *dict = PyDict_New(); + + if(!dict) + dict = Py_None; + else { + PyDict_SetItem(dict, + Py_BuildValue("s",node->left), + Py_BuildValue("s",node->right)); + } + + return dict; + +} + + +/* + * node2dict2(): transoform a wlp_node_t node in a python dictionary of the form + * { 'owner': ownername, var: val} + * it's currently unused (obsoleted) + */ + +static PyObject *node2dict2(struct wlp_node_t *node) { + PyObject *dict = PyDict_New(); + + if(!dict) + dict = Py_None; + else { + PyDict_SetItem(dict, + Py_BuildValue("s","owner"), + Py_BuildValue("s",node->owner)); + + PyDict_SetItem(dict, + Py_BuildValue("s",node->left), + Py_BuildValue("s",node->right)); + } + + return dict; + +} + +static PyMethodDef wlp_methods[] = { + {"mkdict", wlp_mkdict, METH_VARARGS}, + {"setfilebyname", wlp_setfilebyname, METH_VARARGS}, + {"setfilebyfd", wlp_setfilebyfd, METH_VARARGS}, + {NULL,NULL} +}; + + +void initwlp() { + (void) Py_InitModule("wlp",wlp_methods); +} diff --git a/wlp/C/yytest.c b/wlp/C/yytest.c new file mode 100644 index 0000000..74c6fe7 --- /dev/null +++ b/wlp/C/yytest.c @@ -0,0 +1,25 @@ +#include <stdio.h> + +#include "structs.h" +#include "macro.h" + + + +int parse(FILE *file); + +struct wlp_list_t *list; + +int main(int argc,char **argv) { + struct wlp_node_t *tmp; + int count; + + parse(NULL); + + if(list) + for(tmp = list->head, count=0; + tmp != list->head || count == 0; + tmp = tmp->next, count++) + printf("FOUND(%d) '%s' '%s' '%s'\n",count,tmp->left,tmp->right,tmp->owner); + + return(0); +} |