/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2009-2019 Olof Hagsand and Benny Holmgren This file is part of CLIXON. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Alternatively, the contents of this file may be used under the terms of the GNU General Public License Version 3 or later (the "GPL"), in which case the provisions of the GPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of the GPL, and not to allow others to use your version of this file under the terms of Apache License version 2, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the Apache License version 2 or the GPL. ***** END LICENSE BLOCK ***** * Utility for compiling regexp and checking validity * gcc -I /usr/include/libxml2 regex.c -o regex -lxml2 */ #ifdef HAVE_CONFIG_H #include "clixon_config.h" /* generated by config & autoconf */ #endif #include /* unistd */ #include #include /* posix regex */ #include #include #include #undef HAVE_LIBXML2 #ifdef HAVE_LIBXML2 /* Actually it should check for a header file */ #include #endif /* cligen */ #include /* clixon */ #include "clixon/clixon.h" /*! libxml2 regex implementation * @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028 * @retval -1 Error * @retval 0 Not match * @retval 1 Match */ static int regex_libxml2(char *regexp0, char *content0, int nr) { int retval = -1; #ifdef HAVE_LIBXML2 xmlChar *regexp = (xmlChar*)regexp0; xmlChar *content = (xmlChar*)content0; xmlRegexp *xrp = NULL; int ret; int i; if ((xrp = xmlRegexpCompile(regexp)) == NULL) goto done; if (nr==0) return 1; for (i=0; i sizeof(pattern)-5){ fprintf(stderr, "pattern too long\n"); return -1; } strncpy(pattern, "^(", 2); strncpy(pattern+2, posix, sizeof(pattern)-2); strncat(pattern, ")$", sizeof(pattern)-len0-1); if (regcomp(&re, pattern, REG_NOSUB|REG_EXTENDED) != 0) return(0); /* report error */ if (nr==0) return 1; for (i=0; i\tDebug\n" "\t-p \txsd->posix translation regexp\n" "\t-x \tlibxml2 regexp\n" "\t-n \tIterate content match (0 means only compile)\n" "\t-r \tregexp (mandatory)\n" "\t-c \tValue content string(mandatory)\n", argv0 ); exit(0); } int main(int argc, char **argv) { int retval = -1; char *argv0 = argv[0]; int c; char *regexp = NULL; char *content = NULL; int posix = 0; int libxml2 = 0; int ret; int nr = 1; optind = 1; opterr = 0; while ((c = getopt(argc, argv, "hD:pxn:r:c:")) != -1) switch (c) { case 'h': usage(argv0); break; case 'D': if (sscanf(optarg, "%d", &debug) != 1) usage(argv0); break; case 'p': /* xsd->posix */ posix++; break; case 'n': /* Number of iterations */ if ((nr = atoi(optarg)) < 0) usage(argv0); break; case 'x': /* libxml2 */ libxml2++; break; case 'r': /* regexp */ regexp = optarg; break; case 'c': /* value content string */ content = optarg; break; default: usage(argv[0]); break; } clicon_log_init(__FILE__, debug?LOG_DEBUG:LOG_INFO, CLICON_LOG_STDERR); if (regexp == NULL || content == NULL) usage(argv0); if (posix == libxml2) usage(argv0); clicon_debug(1, "regexp:%s", regexp); clicon_debug(1, "content:%s", content); if (libxml2){ if ((ret = regex_libxml2(regexp, content, nr)) < 0) goto done; } else if (posix){ if ((ret = regex_posix(regexp, content, nr)) < 0) goto done; } else goto done; fprintf(stdout, "%d\n", ret); exit(ret); retval = 0; done: return retval; }