- cleaning and refactoring functionality

- parser function new WIP (old included as comment-> must be integrated)
This commit is contained in:
2017-02-28 17:11:57 +00:00
parent 05dd972ec7
commit 0974cc3ecf
11 changed files with 627 additions and 188 deletions

325
src/stubser/cfile_parser.c Normal file
View File

@@ -0,0 +1,325 @@
/*!
* @file cfile_parser.c
* @brief
* @details
* Project: \n
* Subsystem: \n
* Module: \n
* Code: GNU-C\n
*
* @date 28.02.2017
* @author SESA354004
*/
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include "xtypes.h"
#include "xstring.h"
#include "cfile_parser_loc.h"
#include "cfunction_if.h"
STATIC bool cfile_parser_initialized = false;
STATIC regex_t regX;
STATIC regex_t regXsl;
STATIC regex_t regXmlStart;
STATIC regex_t regXmlPar;
STATIC regex_t regXmlEnd;
STATIC regex_t regXmlProto;
STATIC regex_t regXproto;
int8_t cfile_parser_init()
{
if (0 > regcomp(&regX, FUNCTION_BASE, (REG_EXTENDED | REG_NEWLINE)))
{
perror("Error regex\n");
return -1;
}
if (0 > regcomp(&regXsl, FUNCTION_SL, (REG_EXTENDED | REG_NEWLINE)))
{
perror("Error regex\n");
return -1;
}
if (0 > regcomp(&regXmlStart, FUNCTION_ML_SOF, (REG_EXTENDED | REG_NEWLINE)))
{
perror("Error regex\n");
return -1;
}
if (0 > regcomp(&regXmlPar, FUNCTION_ML_PAR, (REG_EXTENDED | REG_NEWLINE)))
{
perror("Error regex\n");
return -1;
}
if (0 > regcomp(&regXmlEnd, FUNCTION_ML_END, (REG_EXTENDED | REG_NEWLINE)))
{
perror("Error regex\n");
return -1;
}
if (0 > regcomp(&regXmlProto, FUNCTION_ML_PROTO, (REG_EXTENDED | REG_NEWLINE)))
{
perror("Error regex\n");
return -1;
}
if (0 > regcomp(&regXproto, FUNCTION_PROTO, (REG_EXTENDED | REG_NEWLINE)))
{
perror("Error regex\n");
return -1;
}
cfile_parser_initialized = true;
return 0;
}
// TODO adapt in cfile_parser_evaluateLine()
/*!
* @brief Process a string (e.g. from getline()) if it is a single or multiple line function definition
* @todo closing parenthesis within parameter list will break the detection
* @retval -1 Invalid regula expressions
* @retval -2 Given String not a function
* @retval 0 function evaluated
*/
//STATIC int matchFunction(char *aLine)
//{
// const size_t maxGroup = 10;
// regmatch_t matchGroup[maxGroup];
// const char *pattern = STUBI_FUNCTION_REGEX;
// const char *patternend = STUBI_FUNCTION_REGEX
// "\\)";
// regex_t regX;
// regex_t regXEnd;
// regex_t regXMulti;
// regex_t regXMultiEnd;
// uint8_t i = 0;
// char funPrefix[256] =
// { 0 };
// static char funTest[2056] =
// { 0 };
// static uint8_t multiParamFlag = 0;
// uint8_t multiParamEndFlag = 0;
//
// if (0 > regcomp(&regX, pattern, REG_EXTENDED)) // | REG_NEWLINE))
// {
// perror("Error regex\n");
// return -1;
// }
//
// if (0 > regcomp(&regXEnd, patternend, REG_EXTENDED)) // | REG_NEWLINE))
// {
// perror("Error regexend\n");
// return -1;
// }
//
// if (0 > regcomp(&regXMulti, STUBI_FUNCTION_MULTI, REG_EXTENDED)) // | REG_NEWLINE))
// {
// perror("Error regXMulti\n");
// return -1;
// }
//
// if (0 > regcomp(&regXMultiEnd, STUBI_FUNCTION_MULTI_END, REG_EXTENDED)) // | REG_NEWLINE))
// {
// perror("Error regXMultiEnd\n");
// return -1;
// }
//
// if (multiParamFlag)
// {
// if (0 == regexec(&regXMultiEnd, aLine, maxGroup, matchGroup, 0))
// {
// // end of multi line function
// ++multiParamFlag;
// multiParamEndFlag = 1;
// }
// else
// {
// // continuation of multi line function
// (void) regexec(&regXMulti, aLine, maxGroup, matchGroup, 0);
// ++multiParamFlag;
// }
// }
// else if (0 == regexec(&regXEnd, aLine, maxGroup, matchGroup, 0))
// {
// // single line function
// multiParamFlag = 0;
// }
// else if (0 == regexec(&regX, aLine, maxGroup, matchGroup, 0))
// {
// // start of multi line function
// ++multiParamFlag;
// printf("[ mls][%d]:", multiParamFlag);
// }
// else
// {
// // no fit
// return -2;
// }
//
// switch (multiParamFlag)
// {
// case 0: // single line function definition
// case 1: // first line of multi line function definition
// {
// (void) strlcpy(funPrefix, &aLine[matchGroup[1].rm_so], (size_t) (matchGroup[1].rm_eo - matchGroup[1].rm_so) + 1);
// (void) strlcat(funTest, &aLine[matchGroup[1].rm_so], (size_t) (matchGroup[1].rm_eo - matchGroup[1].rm_so) + 1);
//
// if ( //
// NULL == strstr(funPrefix, "extern") //
// && NULL == strstr(funPrefix, "EXTERN") //
// && NULL == strstr(funPrefix, "static") //
// && NULL == strstr(funPrefix, "STATIC") //
// && matchGroup[2].rm_so < matchGroup[2].rm_eo)
// {
// printf("[ sl ]:%s", funPrefix);
// for (i = 2; i < maxGroup && 0 <= matchGroup[i].rm_so; ++i)
// {
// printf(" (%.3ld - %.3ld): ", matchGroup[i].rm_so, matchGroup[i].rm_eo);
// printf("%.*s", (int) (matchGroup[i].rm_eo - matchGroup[i].rm_so), &aLine[matchGroup[i].rm_so]);
// (void) strncat(funTest, &aLine[matchGroup[i].rm_so], (int) (matchGroup[i].rm_eo - matchGroup[i].rm_so) + 1);
// }
// printf("\n");
// }
// // TODO special handling for static functions (as extern in stub header)
// else if (NULL != strstr(funPrefix, "static") || NULL != strstr(funPrefix, "STATIC"))
// {
// printf("[todo] %s %.*s\n", funPrefix, (int) (matchGroup[2].rm_eo - matchGroup[2].rm_so), &aLine[matchGroup[2].rm_so]);
// }
// // ignore functions declared as extern
// else if (NULL != strstr(funPrefix, "extern") || NULL != strstr(funPrefix, "EXTERN"))
// {
// printf("[ nu ] %s %.*s\n", funPrefix, (int) (matchGroup[2].rm_eo - matchGroup[2].rm_so), &aLine[matchGroup[2].rm_so]);
// }
// break;
// }
// default: // further lines
// {
// if (multiParamEndFlag)
// {
// printf("[ mle][%d]:", multiParamFlag);
// }
// else
// {
// printf("[ ml ][%d]:", multiParamFlag);
// }
//
// if (matchGroup[1].rm_so < matchGroup[1].rm_eo)
// {
// for (i = 1; i < maxGroup && 0 <= matchGroup[i].rm_so; ++i)
// {
// printf("(%.3ld-%.3ld):", matchGroup[i].rm_so, matchGroup[i].rm_eo);
// printf("%.*s", (int) (matchGroup[i].rm_eo - matchGroup[i].rm_so), &aLine[matchGroup[i].rm_so]);
// }
// printf("\n");
// }
//
// if (multiParamEndFlag)
// {
// multiParamFlag = 0;
// multiParamEndFlag = 0;
// }
//
// break;
// }
// }
//
// return 0;
//}
STATIC void cfile_parser_evaluateLine(char *aLine)
{
uint8_t i;
const size_t maxGroup = 10;
regmatch_t matchGroup[maxGroup];
char match[1024] =
{ 0 };
static uint8_t mlFlag = 0;
if (mlFlag)
{
if (0 == regexec(&regXmlEnd, aLine, maxGroup, matchGroup, REG_NOTEOL))
{
printf("[ mle]");
mlFlag = 0;
}
else if (0 == regexec(&regXmlProto, aLine, maxGroup, matchGroup, REG_NOTEOL))
{
printf("[ mlp]");
mlFlag = 0;
}
else if (0 == regexec(&regXmlPar, aLine, maxGroup, matchGroup, REG_NOTEOL))
{
printf("[ ml+]");
++mlFlag;
}
else
{
// fallback
mlFlag = 0;
return;
}
}
else if (0 == regexec(&regXsl, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo)
{
printf("[ sl ]");
}
else if (0 == regexec(&regXmlStart, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo)
{
printf("[ mls]");
++mlFlag;
}
else if (0 == regexec(&regXproto, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo)
{
printf("[prot]");
}
else if (0 == regexec(&regX, aLine, maxGroup, matchGroup, REG_NOTEOL) && matchGroup[2].rm_so < matchGroup[2].rm_eo)
{
printf("[ fuX]");
}
else
{
return;
}
for (i = 0; i < maxGroup && 0 <= matchGroup[i].rm_so; ++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");
}
}
}
int8_t cfile_parser(char *aPath, cfunction_list_t *aList)
{
FILE *fdesc;
ssize_t charRead = 0;
size_t lineSize = 0;
char *fileLine = NULL; // will be allocated by getline(); must be freed
if (false == cfile_parser_initialized)
{
cfile_parser_init();
}
fdesc = fopen(aPath, "r");
if (NULL == fdesc)
{
perror("Error open file");
return -1;
}
do
{
charRead = getline(&fileLine, &lineSize, fdesc);
if (0 <= charRead)
{
(void) cfile_parser_evaluateLine(fileLine);
}
} while (0 <= charRead);
free(fileLine);
(void) fclose(fdesc);
return 0;
}