From da56a3b6cf6d20b533417add851e0a668bdaa53e Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Wed, 1 Mar 2017 08:02:44 +0000 Subject: [PATCH] - WIP (dynamic function structure implementation started; only function type at the moment; running) --- src/ext/xregex.h | 21 ++++++ src/main.c | 2 +- src/stubser/cfile_parser.c | 130 +++++++++++++++++++++++++-------- src/stubser/cfile_parser_loc.h | 2 + src/stubser/cfunction.c | 88 ++++++++++++++++------ src/stubser/cfunction_if.h | 18 ++++- src/stubser/stubser.c | 3 + 7 files changed, 208 insertions(+), 56 deletions(-) create mode 100644 src/ext/xregex.h diff --git a/src/ext/xregex.h b/src/ext/xregex.h new file mode 100644 index 0000000..20647cf --- /dev/null +++ b/src/ext/xregex.h @@ -0,0 +1,21 @@ +/*! + * @file xregex.h + * @brief + * @details + * Project: \n + * Subsystem: \n + * Module: \n + * Code: GNU-C\n + * + * @date 01.03.2017 + * @author SESA354004 + */ + +#ifndef EXT_XREGEX_H_ +#define EXT_XREGEX_H_ + +#include + +#define XREGEX_IS_MATCHGROUP(groupArray, groupIndex) (groupArray[groupIndex].rm_so < groupArray[groupIndex].rm_eo) + +#endif /* EXT_XREGEX_H_ */ diff --git a/src/main.c b/src/main.c index b56df2c..3eeac96 100644 --- a/src/main.c +++ b/src/main.c @@ -18,7 +18,7 @@ #include "xstring.h" #include "stubser/stubser_if.h" -static int one(const struct dirent *unused) +STATIC int one(const struct dirent *unused) { return 1; } diff --git a/src/stubser/cfile_parser.c b/src/stubser/cfile_parser.c index 312ba00..1f8e164 100644 --- a/src/stubser/cfile_parser.c +++ b/src/stubser/cfile_parser.c @@ -12,13 +12,17 @@ */ #include #include -#include +#include "xregex.h" #include "xtypes.h" #include "xstring.h" -#include "cfile_parser_loc.h" #include "cfunction_if.h" +#include "cfile_parser_loc.h" + +#define CFILE_PARSTER_MAX_REGEX_MATCHING_GROUPS 10 STATIC bool cfile_parser_initialized = false; +STATIC uint8_t cfile_parser_functionLine = 0; +STATIC cfunction_t *cfile_parser_function = NULL; STATIC regex_t regX; STATIC regex_t regXsl; STATIC regex_t regXmlStart; @@ -221,74 +225,135 @@ int8_t cfile_parser_init() // // return 0; //} -STATIC void cfile_parser_evaluateLine(char *aLine) +STATIC cfunction_t* cfile_parser_evaluateLine(char *aLine) { - uint8_t i; - const size_t maxGroup = 10; + const size_t maxGroup = CFILE_PARSTER_MAX_REGEX_MATCHING_GROUPS; regmatch_t matchGroup[maxGroup]; + bool functionEnd = false; + cfunction_type_t tempFunctionType = CFUNCTION_TYPE_UNDEF; - char match[1024] = - { 0 }; - static uint8_t mlFlag = 0; - - if (mlFlag) + if (cfile_parser_functionLine) { if (0 == regexec(®XmlEnd, aLine, maxGroup, matchGroup, REG_NOTEOL)) { + tempFunctionType = CFUNCTION_TYPE_REGULAR; printf("[ mle]"); - mlFlag = 0; + ++cfile_parser_functionLine; + functionEnd = true; } else if (0 == regexec(®XmlProto, aLine, maxGroup, matchGroup, REG_NOTEOL)) { + tempFunctionType = CFUNCTION_TYPE_PROTO; printf("[ mlp]"); - mlFlag = 0; + ++cfile_parser_functionLine; + functionEnd = true; } else if (0 == regexec(®XmlPar, aLine, maxGroup, matchGroup, REG_NOTEOL)) { printf("[ ml+]"); - ++mlFlag; + ++cfile_parser_functionLine; } else { - // fallback - mlFlag = 0; - return; + tempFunctionType = CFUNCTION_TYPE_UNDEF; + ++cfile_parser_functionLine; + functionEnd = true; + // TODO fallback (free currently used memory) + return NULL; } } - else if (0 == regexec(®Xsl, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo) + else if (0 == regexec(®Xsl, aLine, maxGroup, matchGroup, REG_NOTEOL) && CFILE_PARSER_IS_MATCHGROUP_FUNCTION(matchGroup)) { + tempFunctionType = CFUNCTION_TYPE_REGULAR; printf("[ sl ]"); } - else if (0 == regexec(®XmlStart, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo) + else if (0 == regexec(®XmlStart, aLine, maxGroup, matchGroup, REG_NOTEOL) && CFILE_PARSER_IS_MATCHGROUP_FUNCTION(matchGroup)) { + tempFunctionType = CFUNCTION_TYPE_REGULAR; printf("[ mls]"); - ++mlFlag; + ++cfile_parser_functionLine; } - else if (0 == regexec(®Xproto, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo) + else if (0 == regexec(®Xproto, aLine, maxGroup, matchGroup, REG_NOTEOL) && CFILE_PARSER_IS_MATCHGROUP_FUNCTION(matchGroup)) { + tempFunctionType = CFUNCTION_TYPE_PROTO; printf("[prot]"); } - else if (0 == regexec(®X, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo) + else if (0 == regexec(®X, aLine, maxGroup, matchGroup, REG_NOTEOL) && CFILE_PARSER_IS_MATCHGROUP_FUNCTION(matchGroup)) { + tempFunctionType = CFUNCTION_TYPE_UNDEF; printf("[ fuX]"); } else { - return; + return NULL; } - for (i = 0; i < maxGroup && 0 <= matchGroup[i].rm_so; ++i) +#if (0) { - if (matchGroup[i].rm_so < matchGroup[i].rm_eo) + uint8_t i = 0; + char *tempChar = NULL; + char match[1024] = + { 0}; + + for (i = 0; i < maxGroup && 0 <= matchGroup[i].rm_so; ++i) { - if (0 < i) + if (matchGroup[i].rm_so < matchGroup[i].rm_eo) + { + if (0 < i) printf(" "); - printf("[%02d](%.3ld - %.3ld)[%03d]: ", i, matchGroup[i].rm_so, matchGroup[i].rm_eo, (int) (matchGroup[i].rm_eo - matchGroup[i].rm_so)); - printf("%.*s", (int) (matchGroup[i].rm_eo - matchGroup[i].rm_so), &aLine[matchGroup[i].rm_so]); - strlcpy(match, &aLine[matchGroup[i].rm_so], (size_t) (matchGroup[i].rm_eo - matchGroup[i].rm_so + 1)); - printf("\n"); + printf("[%02d](%.3ld - %.3ld)[%03d]: ", i, matchGroup[i].rm_so, matchGroup[i].rm_eo, (int) (matchGroup[i].rm_eo - matchGroup[i].rm_so)); + //printf("%.*s", (int) (matchGroup[i].rm_eo - matchGroup[i].rm_so), &aLine[matchGroup[i].rm_so]); + strlcpy(match, &aLine[matchGroup[i].rm_so], (size_t) (matchGroup[i].rm_eo - matchGroup[i].rm_so + 1)); + tempChar = strstr(match, "//"); + if (NULL != tempChar) + { + *tempChar = '\0'; + } + printf("%s", match); + printf("\n"); + } } } +#endif + + switch (cfile_parser_functionLine) + { + case 0: // single line function definition + case 1: // first line of multi line function definition + { + cfile_parser_function = cfunction_newFunction(); + cfile_parser_function->type = tempFunctionType; + break; + } + default: // further lines + { + // TODO process parameter + if (functionEnd) + { + // TODO clean function + if (CFUNCTION_TYPE_UNDEF == tempFunctionType) + { + cfunction_freeFunction(&cfile_parser_function); + } + else + { + cfile_parser_function->type = tempFunctionType; + } + cfile_parser_functionLine = 0; + } + break; + } + } + + // return evaluated function + if (0 == cfile_parser_functionLine) + { + cfunction_t *funTemp = cfile_parser_function; + cfile_parser_function = NULL; + return funTemp; + } + + return NULL; } int8_t cfile_parser(char *aPath, cfunction_list_t *aList) @@ -315,7 +380,12 @@ int8_t cfile_parser(char *aPath, cfunction_list_t *aList) charRead = getline(&fileLine, &lineSize, fdesc); if (0 <= charRead) { - (void) cfile_parser_evaluateLine(fileLine); + cfunction_t *function = NULL; + function = cfile_parser_evaluateLine(fileLine); + if (NULL != function) + { + (void) cfunction_addFunction(aList, function); + } } } while (0 <= charRead); diff --git a/src/stubser/cfile_parser_loc.h b/src/stubser/cfile_parser_loc.h index 4a5dd20..9372a7d 100644 --- a/src/stubser/cfile_parser_loc.h +++ b/src/stubser/cfile_parser_loc.h @@ -22,4 +22,6 @@ #define FUNCTION_ML_PROTO FUNCTION_ML_PAR "\\).*[[:blank:]]*;" #define FUNCTION_PROTO FUNCTION_BASE "\\).*[[:blank:]]*;" +#define CFILE_PARSER_IS_MATCHGROUP_FUNCTION(matchGroup) (XREGEX_IS_MATCHGROUP(matchGroup, 1) && XREGEX_IS_MATCHGROUP(matchGroup, 2)) + #endif /* STUBSER_CFILE_PARSER_LOC_H_ */ diff --git a/src/stubser/cfunction.c b/src/stubser/cfunction.c index 122cb36..ac8a71a 100644 --- a/src/stubser/cfunction.c +++ b/src/stubser/cfunction.c @@ -16,32 +16,41 @@ #include "xmalloc.h" #include "cfunction_if.h" -cfunction_t* cfunction_newFunction(cfunction_list_t *aList) +cfunction_t* cfunction_newFunction() { - cfunction_t *next = NULL; + cfunction_t *new = NULL; - if (NULL == aList) + new = (cfunction_t*) xmalloc(sizeof(cfunction_t)); + memset(new, 0, sizeof(cfunction_t)); + + return new; +} + +int8_t cfunction_addFunction(cfunction_list_t *aList, cfunction_t *aNew) +{ + if (NULL == aList || NULL == aNew) { perror("Null pointer"); - return NULL; + return -1; } - next = (cfunction_t*) xmalloc(sizeof(cfunction_t)); - memset(next, 0, sizeof(cfunction_t)); - if (NULL == aList->head) { - aList->head = next; + aList->head = aNew; + aList->current = aNew; + } + else + { + aList->current->next = aNew; + aList->current = aNew; } - aList->current = next; - - return next; + return 0; } cfunction_parameter_t* cfunction_newParameter(struct _CFUNCTION_PARAMETER_LIST_T *aList) { - cfunction_parameter_t *next = NULL; + cfunction_parameter_t *new = NULL; if (NULL == aList) { @@ -49,17 +58,18 @@ cfunction_parameter_t* cfunction_newParameter(struct _CFUNCTION_PARAMETER_LIST_T return NULL; } - next = (cfunction_parameter_t*) xmalloc(sizeof(cfunction_parameter_t)); - memset(next, 0, sizeof(cfunction_parameter_t)); + new = (cfunction_parameter_t*) xmalloc(sizeof(cfunction_parameter_t)); + memset(new, 0, sizeof(cfunction_parameter_t)); if (NULL == aList->head) { - aList->head = next; + aList->head = new; } - aList->current = next; + aList->current = new; + ++aList->amount; - return next; + return new; } char* cfunction_newDetail(char **aDest, char *aSource, size_t aSize) @@ -79,7 +89,7 @@ char* cfunction_newDetail(char **aDest, char *aSource, size_t aSize) return *aDest; } -int8_t cfunction_freeParameter(struct _CFUNCTION_PARAMETER_LIST_T *aParameter) +STATIC int8_t cfunction_freeParameter(struct _CFUNCTION_PARAMETER_LIST_T *aParameter) { cfunction_parameter_t *work = NULL; cfunction_parameter_t *next = NULL; @@ -107,6 +117,26 @@ int8_t cfunction_freeParameter(struct _CFUNCTION_PARAMETER_LIST_T *aParameter) return 0; } +int8_t cfunction_freeFunction(cfunction_t **aFunction) +{ + cfunction_t *work = *aFunction; + + if (NULL == aFunction || NULL == *aFunction) + { + return -1; + } + + free(work->prefix); + free(work->dataType); + free(work->name); + cfunction_freeParameter(&work->parameter); + free(work); + + *aFunction = NULL; + + return 0; +} + int8_t cfunction_freeList(cfunction_list_t *aList) { cfunction_t *work = NULL; @@ -123,11 +153,7 @@ int8_t cfunction_freeList(cfunction_list_t *aList) while (work) { next = work->next; - free(work->prefix); - free(work->dataType); - free(work->name); - cfunction_freeParameter(&work->parameter); - free(work); + cfunction_freeFunction(&work); work = next; } @@ -136,3 +162,19 @@ int8_t cfunction_freeList(cfunction_list_t *aList) return 0; } + +void cfunction_printList(cfunction_list_t *aList) +{ + cfunction_t *work = aList->head; + + if (NULL == aList) + { + return; + } + + while (work) + { + printf("[ cfu]: %d\n", work->type); + work = work->next; + } +} diff --git a/src/stubser/cfunction_if.h b/src/stubser/cfunction_if.h index 565bf88..67715f3 100644 --- a/src/stubser/cfunction_if.h +++ b/src/stubser/cfunction_if.h @@ -14,6 +14,16 @@ #ifndef STUBSER_CFUNCTION_IF_H_ #define STUBSER_CFUNCTION_IF_H_ +/*! @brief Type of function definition */ +typedef enum _CFUNCTION_TYPE_T +{ + CFUNCTION_TYPE_UNDEF = 0, /*!< @brief undefined */ + CFUNCTION_TYPE_REGULAR = 1, /*!< @brief Regular c function definition */ + CFUNCTION_TYPE_STATIC = 2, /*!< @brief Static c function definition */ + CFUNCTION_TYPE_PROTO = 3, /*!< @brief Prototype c function definition */ + CFUNCTION_TYPE_LAST_ENUM +} cfunction_type_t; + /*! @brief Parameter list node */ typedef struct _CFUNCTION_PARAMETER_T { @@ -25,11 +35,13 @@ typedef struct _CFUNCTION_PARAMETER_T /*! @brief brief_description */ typedef struct _CFUNCTION_T { + cfunction_type_t type; char *prefix; /*!< @brief prefix like static or extern */ char *dataType; /*!< @brief return type */ char *name; /*!< @brief name */ struct _CFUNCTION_PARAMETER_LIST_T { + uint8_t amount; cfunction_parameter_t *head; cfunction_parameter_t *current; } parameter;/*!< @brief parameter array */ @@ -47,10 +59,12 @@ typedef struct _CFUNCTION_LIST_T .head = NULL, \ .current = NULL } -cfunction_t* cfunction_newFunction(cfunction_list_t *aList); +cfunction_t* cfunction_newFunction(); +int8_t cfunction_addFunction(cfunction_list_t *aList, cfunction_t *aNew); cfunction_parameter_t* cfunction_newParameter(struct _CFUNCTION_PARAMETER_LIST_T *aList); char* cfunction_newDetail(char **aDest, char *aSource, size_t aSize); -int8_t cfunction_freeParameter(struct _CFUNCTION_PARAMETER_LIST_T *aParameter); +int8_t cfunction_freeFunction(cfunction_t **aFunction); int8_t cfunction_freeList(cfunction_list_t *aList); +void cfunction_printList(cfunction_list_t *aList); #endif /* STUBSER_CFUNCTION_IF_H_ */ diff --git a/src/stubser/stubser.c b/src/stubser/stubser.c index 66cfc6b..a9f7743 100644 --- a/src/stubser/stubser.c +++ b/src/stubser/stubser.c @@ -31,6 +31,9 @@ int stubser_createStub(char *path) cfunction_list_t functionList = CFUNCTION_LIST_DEFAULT; (void) cfile_parser(path, &functionList); + + cfunction_printList(&functionList); + cfunction_freeList(&functionList); return 0; }