*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
* Foundation
*/
* Returns 1 on success or 0 on error.
* The error code is set on reg->error.
*/
-int OSRegex_Compile(char *pattern, OSRegex *reg, int flags)
+int OSRegex_Compile(const char *pattern, OSRegex *reg, int flags)
{
- int i = 0;
- int count = 0;
+ size_t i = 0;
+ size_t count = 0;
int end_of_string = 0;
int parenthesis = 0;
- int prts_size = 0;
- int max_prts_size = 0;
-
+ unsigned prts_size = 0;
+ unsigned max_prts_size = 0;
+
char *pt;
char *new_str;
char *new_str_free = NULL;
-
+
/* Checking for references not initialized */
if(reg == NULL)
{
return(0);
}
-
+
/* Initializing OSRegex structure */
reg->error = 0;
reg->error = OS_REGEX_MAXSIZE;
goto compile_error;
}
-
-
+
+
/* Duping the pattern for our internal work */
new_str = strdup(pattern);
if(!new_str)
}
new_str_free = new_str;
pt = new_str;
-
-
+
+
/* Getting the number of sub patterns */
do
{
if(*pt == BACKSLASH)
{
pt++;
- if(!((*pt == 'w') ||
- (*pt == 'W') ||
- (*pt == 's') ||
- (*pt == 'S') ||
- (*pt == 'd') ||
- (*pt == 'D') ||
- (*pt == '.') ||
- (*pt == '(') ||
- (*pt == ')') ||
- (*pt == 'p') ||
- (*pt == 't') ||
- (*pt == '$') ||
- (*pt == '|') ||
- (*pt == '<') ||
- (*pt == '\\')))
- {
- reg->error = OS_REGEX_BADREGEX;
- goto compile_error;
- }
-
/* Giving the new values for each regex */
switch(*pt)
{
case '$': *pt = 13;break;
case '|': *pt = 14;break;
case '<': *pt = 15;break;
+ default:
+ reg->error = OS_REGEX_BADREGEX;
+ goto compile_error;
}
pt++;
parenthesis--;
prts_size++;
}
-
+
/* We only allow one level of parenthesis */
if(parenthesis != 0 && parenthesis != 1)
{
reg->error = OS_REGEX_BADPARENTHESIS;
goto compile_error;
}
-
- /* The pattern must be always lower case if
+
+ /* The pattern must be always lower case if
* case sensitive is set
*/
if(!(flags & OS_CASE_SENSITIVE))
{
- *pt = charmap[(uchar)*pt];
+ *pt = (char) charmap[(uchar)*pt];
}
-
+
if(*pt == OR)
{
/* Each sub pattern must be closed on parenthesis */
}
count++;
}
- pt++;
+ pt++;
}while(*pt != '\0');
-
+
/* After the whole pattern is read, the parenthesis must all be closed */
if(parenthesis != 0)
reg->error = OS_REGEX_BADPARENTHESIS;
goto compile_error;
}
-
-
+
+
/* Allocating the memory for the sub patterns */
count++;
- reg->patterns = calloc(count +1, sizeof(char *));
- reg->flags = calloc(count +1, sizeof(int));
-
-
+ reg->patterns = (char **) calloc(count +1, sizeof(char *));
+ reg->flags = (int *) calloc(count +1, sizeof(int));
+
+ /* Memory allocation error check */
+ if(!reg->patterns || !reg->flags)
+ {
+ reg->error = OS_REGEX_OUTOFMEMORY;
+ goto compile_error;
+ }
+
+
/* For the substrings */
if((prts_size > 0) && (flags & OS_RETURN_SUBSTRING))
{
- reg->prts_closure = calloc(count +1, sizeof(char **));
- reg->prts_str = calloc(count +1, sizeof(char **));
+ reg->prts_closure = (const char ***) calloc(count +1, sizeof(const char **));
+ reg->prts_str = (const char ***) calloc(count +1, sizeof(const char **));
if(!reg->prts_closure || !reg->prts_str)
{
reg->error = OS_REGEX_OUTOFMEMORY;
goto compile_error;
}
}
-
-
- /* Memory allocation error check */
- if(!reg->patterns || !reg->flags)
- {
- reg->error = OS_REGEX_OUTOFMEMORY;
- goto compile_error;
- }
+
/* Initializing each sub pattern */
for(i = 0; i<=count; i++)
}
}
i = 0;
-
-
+
+
/* Reassigning pt to the beginning of the string */
pt = new_str;
-
+
/* Getting the sub patterns */
do
{
/* The parenthesis closure if set */
if(reg->prts_closure)
{
- int tmp_int = 0;
+ unsigned tmp_int = 0;
char *tmp_str;
{
max_prts_size = prts_size;
}
-
+
/* Allocating the memory */
- reg->prts_closure[i] = calloc(prts_size + 1, sizeof(char *));
- reg->prts_str[i] = calloc(prts_size + 1, sizeof(char *));
+ reg->prts_closure[i] = (const char **) calloc(prts_size + 1, sizeof(const char *));
+ reg->prts_str[i] = (const char **) calloc(prts_size + 1, sizeof(const char *));
if((reg->prts_closure[i] == NULL)||(reg->prts_str[i] == NULL))
{
reg->error = OS_REGEX_OUTOFMEMORY;
}while(!end_of_string);
/* Allocating sub string for the maximum number of parenthesis */
- reg->sub_strings = calloc(max_prts_size + 1, sizeof(char *));
+ reg->sub_strings = (char **) calloc(max_prts_size + 1, sizeof(char *));
if(reg->sub_strings == NULL)
{
reg->error = OS_REGEX_OUTOFMEMORY;
goto compile_error;
}
-
+
/* Success return */
free(new_str_free);
return(1);
-
-
+
+
/* Error handling */
compile_error:
-
+
if(new_str_free)
{
free(new_str_free);
}
-
+
OSRegex_FreePattern(reg);
return(0);