summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrhaas <rhaas@17b73243-c579-4c4c-a9d2-2d5706c11dac>2012-06-05 01:21:05 +0000
committerrhaas <rhaas@17b73243-c579-4c4c-a9d2-2d5706c11dac>2012-06-05 01:21:05 +0000
commitebd9d3057b889f2fe224c7cd7a0c539aa33a4d5c (patch)
tree90810093d4c416d77aff036ad0a4c321580284a4
parentfe89bb0b6590b24f4c8c6cee06b26a985661ef5a (diff)
have CCTK_RegexMatch return a distinct error code if patterns is invalid
This patch changes the return value in the "does not compile" case to -1 and updates all source files that I could find that use it. Note that this patch changes behaviour of a routine. It used to return 0 for non-compiling patterns so thorns that test for C-like true would interpret invalid patterns as does-not-match, but will interpret the -1 return value as does-match. git-svn-id: http://svn.cactuscode.org/flesh/trunk@4831 17b73243-c579-4c4c-a9d2-2d5706c11dac
-rw-r--r--doc/ReferenceManual.pdfbin1186265 -> 916133 bytes
-rw-r--r--doc/ReferenceManual/CCTKReference.tex69
-rw-r--r--src/include/cctk_Misc.h7
-rw-r--r--src/main/Parameters.c23
-rw-r--r--src/util/Misc.c24
5 files changed, 105 insertions, 18 deletions
diff --git a/doc/ReferenceManual.pdf b/doc/ReferenceManual.pdf
index a90197c4..3e657e12 100644
--- a/doc/ReferenceManual.pdf
+++ b/doc/ReferenceManual.pdf
Binary files differ
diff --git a/doc/ReferenceManual/CCTKReference.tex b/doc/ReferenceManual/CCTKReference.tex
index 1908547b..e1b86845 100644
--- a/doc/ReferenceManual/CCTKReference.tex
+++ b/doc/ReferenceManual/CCTKReference.tex
@@ -659,6 +659,9 @@ from Fortran.
\item[\code{CCTK\_ReductionHandle}] [\pageref{CCTK-ReductionHandle}]
Get the handle for a registered reduction operator
+\item[\code{CCTK\_RegexMatch}] [\pageref{CCTK-RegexMatch}]
+ Perform a regular expression match of string against pattern
+
\item[\code{CCTK\_RegisterBanner}] [\pageref{CCTK-RegisterBanner}]
Register a banner for a thorn
@@ -10647,6 +10650,72 @@ call CCTK_ReductionHandle(handle,"maximum")
\end{ExampleSection}
\end{FunctionDescription}
+% Misc.c
+\begin{FunctionDescription}{CCTK\_RegexMatch}{Perform a regular expression match of string against pattern}
+\label{CCTK-RegexMatch}
+\begin{SynopsisSection}
+\begin{Synopsis}{C}
+\begin{verbatim}success = CCTK_RegexMatch( const char *string, const char *pattern,
+ const int nmatch, regmatch_t *pmatch)\end{verbatim}
+\end{Synopsis}
+\end{SynopsisSection}
+\begin{ParameterSection}
+\begin{Parameter}{string}
+String to match against
+\end{Parameter}
+\begin{Parameter}{pattern}
+Regex pattern
+\end{Parameter}
+\begin{Parameter}{nmatch}
+The size of the pmatch array
+\end{Parameter}
+\begin{Parameter}{pmatch}
+Array in which to place the matches
+\end{Parameter}
+\end{ParameterSection}
+\begin{ResultSection}
+\begin{Result}{0} pattern does not match\end{Result}
+\begin{Result}{1} pattern matches\end{Result}
+\begin{Result}{< 0} indicates an error condition (pattern did not compile as a
+ regular expression) \end{Result}
+\end{ResultSection}
+\begin{Discussion}
+Perform a regular expression match of string against pattern.
+Also returns the specified number of matched substrings as
+give by regexec.
+This is a modified form of the example routine given in the SGI
+man page for regcomp.
+\end{Discussion}
+\begin{ExampleSection}
+\begin{Example}{C}
+\begin{verbatim}
+#define R_BEGIN "(\\[|\\()?"
+#define R_VALUE "([^]):]*)"
+#define R_SEP ":"
+#define R_END "(\\]|\\))?"
+#define R_MAYBE(x) "(" x ")?"
+
+ int matched;
+ const char pattern[] =
+ R_BEGIN
+ R_VALUE
+ R_MAYBE(R_SEP R_VALUE R_MAYBE(R_SEP R_VALUE))
+ R_END;
+
+ if( (matched = CCTK_RegexMatch(range, pattern, 8, pmatch)) > 0) {
+ CCTK_VInfo(CCTK_THORNSTRING, "'%s' is a valid range specifier",
+ range);
+ } else if(!matched) {
+ CCTK_VInfo(CCTK_THORNSTRING, "'%s' is not a valid range specifier",
+ range);
+ } else {
+ CCTK_VInfo(CCTK_THORNSTRING, "invalid pattern '%s'", pattern);
+ }
+\end{verbatim}
+\end{Example}
+\end{ExampleSection}
+\end{FunctionDescription}
+
% Coord.c
\begin{FunctionDescription}{CCTK\_RegisterBanner}{Register a banner for a thorn}
\label{CCTK-RegisterBanner}
diff --git a/src/include/cctk_Misc.h b/src/include/cctk_Misc.h
index 6ac16e56..c1147c47 100644
--- a/src/include/cctk_Misc.h
+++ b/src/include/cctk_Misc.h
@@ -13,6 +13,8 @@
#ifndef _CCTK_MISC_H_
#define _CCTK_MISC_H_
+#include "cctk_GNU.h"
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -33,6 +35,11 @@ int Util_DoubleInRange(double inval, const char *range);
int Util_IntInRangeList(int inval, int n_elements, ...);
int Util_DoubleInRangeList(double inval, int n_elements, ...);
+int CCTK_RegexMatch (const char *string,
+ const char *pattern,
+ const int nmatch,
+ regmatch_t *pmatch);
+
int CCTK_SetDoubleInRangeList(CCTK_REAL *data, const char *value,
int n_elements, ...);
int CCTK_SetIntInRangeList(CCTK_INT *data, const char *value,
diff --git a/src/main/Parameters.c b/src/main/Parameters.c
index 3130be96..48467434 100644
--- a/src/main/Parameters.c
+++ b/src/main/Parameters.c
@@ -203,11 +203,6 @@ static void ParameterActivate(t_param *param);
********************* Other Routine Prototypes *********************
********************************************************************/
-int CCTK_RegexMatch (const char *string,
- const char *pattern,
- const int nmatch,
- regmatch_t *pmatch);
-
extern void CCTKi_SetParameterSetMask (int mask);
/********************************************************************
@@ -2107,13 +2102,20 @@ static int ParameterSetString (t_param *param, const char *value)
CCTK_Equals (param->props->thorn, range->origin))
{
#ifndef CCTK_PARAMUNCHECKED
- if (CCTK_RegexMatch (value, range->range, 0, NULL))
+ const int matched = CCTK_RegexMatch (value, range->range, 0, NULL);
+ if (matched > 0)
{
#endif
retval = CCTK_SetString (param->data, value);
break;
#ifndef CCTK_PARAMUNCHECKED
}
+ else if (matched < 0)
+ {
+ CCTK_VWarn(CCTK_WARN_ALERT, __LINE__, __FILE__, "Cactus",
+ "Invalid regular expression '%s' used as range for string %s::%s",
+ range->range, param->props->thorn, param->props->name);
+ }
#endif
}
}
@@ -2151,13 +2153,20 @@ static int ParameterSetSentence (t_param *param, const char *value)
CCTK_Equals (param->props->thorn, range->origin))
{
#ifndef CCTK_PARAMUNCHECKED
- if (CCTK_RegexMatch (value, range->range, 0, NULL))
+ const int matched = CCTK_RegexMatch (value, range->range, 0, NULL);
+ if (matched > 0)
{
#endif
retval = CCTK_SetString (param->data, value);
break;
#ifndef CCTK_PARAMUNCHECKED
}
+ else if (matched < 0)
+ {
+ CCTK_VWarn(CCTK_WARN_ALERT, __LINE__, __FILE__, "Cactus",
+ "Invalid regular expression '%s' used as range for sequence %s::%s",
+ range->range, param->props->thorn, param->props->name);
+ }
#endif
}
}
diff --git a/src/util/Misc.c b/src/util/Misc.c
index 96b9dd0b..f1316341 100644
--- a/src/util/Misc.c
+++ b/src/util/Misc.c
@@ -19,8 +19,6 @@
#include <math.h>
#include <float.h>
-#include "cctk_GNU.h"
-
#include "cctk_Flesh.h"
#include "cctk_Misc.h"
#include "cctk_FortranString.h"
@@ -40,10 +38,6 @@ CCTK_FILEVERSION(util_Misc_c);
********************* Local Routine Prototypes *********************
********************************************************************/
-int CCTK_RegexMatch(const char *string,
- const char *pattern,
- const int nmatch,
- regmatch_t *pmatch);
int CCTK_SetStringInRegexList(char **data, const char *value,
int n_elements, ...);
void CCTK_PrintString(const char *data);
@@ -367,6 +361,7 @@ int Util_IntInRange(int inval, const char *range)
regmatch_t pmatch[8];
int start_closed, end_closed;
int start, end, step;
+ int matched;
/* Valid ranges are of the form start:end:step
* possibly preceeded by a [ or ( and ended by a ) or ] to indicate
@@ -407,7 +402,8 @@ int Util_IntInRange(int inval, const char *range)
#undef R_MAYBE
- if(CCTK_RegexMatch(range, pattern, 8, pmatch) != 0)
+ matched = CCTK_RegexMatch(range, pattern, 8, pmatch);
+ if(matched > 0)
{
/* First work out if the range is closed at the lower end. */
if(pmatch[1].rm_so != -1)
@@ -528,10 +524,15 @@ int Util_IntInRange(int inval, const char *range)
}
}
- else
+ else if(!matched)
{
CCTK_Warn(1, __LINE__, __FILE__, "Flesh", "Invalid range");
}
+ else
+ {
+ CCTK_VWarn(0, __LINE__, __FILE__, "Flesh",
+ "Invalid patten '%s' used to parse range", pattern);
+ }
return retval;
}
@@ -609,7 +610,7 @@ int Util_DoubleInRange(double inval, const char *range)
#undef R_MAYBE
- if(CCTK_RegexMatch(range, pattern, 8, pmatch) != 0)
+ if(CCTK_RegexMatch(range, pattern, 8, pmatch) > 0)
{
/* First work out if the range is closed at the lower end. */
if(pmatch[1].rm_so != -1)
@@ -1170,7 +1171,7 @@ int CCTK_SetStringInRegexList(char **data, const char *value,
{
element = va_arg(ap, char *);
- if(CCTK_RegexMatch(value, element, 0, NULL))
+ if(CCTK_RegexMatch(value, element, 0, NULL) > 0)
{
retval = CCTK_SetString(data, value);
break;
@@ -1312,6 +1313,7 @@ int CCTK_SetBoolean(CCTK_INT *data, const char *value)
@returndesc
1 - pattern matches
0 - pattern doesn't match
+ <0 - pattern is invalid
@endreturndesc
@@*/
@@ -1347,7 +1349,7 @@ int CCTK_RegexMatch(const char *string,
}
else
{
- retval = 0;
+ retval = -1; /* repost error */
}
}