Thu, 02 Feb 2012 14:17:35 +0100
added regex parser
.cproject | file | annotate | diff | comparison | revisions | |
.project | file | annotate | diff | comparison | revisions | |
Makefile | file | annotate | diff | comparison | revisions | |
cline.c | file | annotate | diff | comparison | revisions | |
gcc-debug.mk | file | annotate | diff | comparison | revisions | |
gcc.mk | file | annotate | diff | comparison | revisions | |
mingw-debug.mk | file | annotate | diff | comparison | revisions | |
mingw.mk | file | annotate | diff | comparison | revisions | |
regex_parser.c | file | annotate | diff | comparison | revisions | |
regex_parser.h | file | annotate | diff | comparison | revisions | |
scanner.c | file | annotate | diff | comparison | revisions |
1.1 --- a/.cproject Thu Jan 26 15:55:52 2012 +0100 1.2 +++ b/.cproject Thu Feb 02 14:17:35 2012 +0100 1.3 @@ -20,7 +20,7 @@ 1.4 <folderInfo id="cdt.managedbuild.toolchain.gnu.mingw.base.1677102573.925482417" name="/" resourcePath=""> 1.5 <toolChain id="cdt.managedbuild.toolchain.gnu.mingw.base.1515980604" name="cdt.managedbuild.toolchain.gnu.mingw.base" superClass="cdt.managedbuild.toolchain.gnu.mingw.base"> 1.6 <targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.PE" id="cdt.managedbuild.target.gnu.platform.mingw.base.572787976" name="Debug Platform" osList="win32" superClass="cdt.managedbuild.target.gnu.platform.mingw.base"/> 1.7 - <builder id="cdt.managedbuild.target.gnu.builder.base.349472496" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" stopOnErr="true" superClass="cdt.managedbuild.target.gnu.builder.base"/> 1.8 + <builder id="cdt.managedbuild.target.gnu.builder.base.349472496" incrementalBuildTarget="all CONF=mingw" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" stopOnErr="true" superClass="cdt.managedbuild.target.gnu.builder.base"/> 1.9 <tool id="cdt.managedbuild.tool.gnu.assembler.mingw.base.1362600845" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.base"> 1.10 <inputType id="cdt.managedbuild.tool.gnu.assembler.input.902665459" superClass="cdt.managedbuild.tool.gnu.assembler.input"/> 1.11 </tool> 1.12 @@ -44,6 +44,47 @@ 1.13 </storageModule> 1.14 <storageModule moduleId="org.eclipse.cdt.core.externalSettings"/> 1.15 </cconfiguration> 1.16 + <cconfiguration id="cdt.managedbuild.toolchain.gnu.mingw.base.1677102573.679714212"> 1.17 + <storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.mingw.base.1677102573.679714212" moduleId="org.eclipse.cdt.core.settings" name="Debug"> 1.18 + <externalSettings/> 1.19 + <extensions> 1.20 + <extension id="org.eclipse.cdt.core.PE" point="org.eclipse.cdt.core.BinaryParser"/> 1.21 + <extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/> 1.22 + <extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/> 1.23 + <extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/> 1.24 + <extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/> 1.25 + <extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/> 1.26 + </extensions> 1.27 + </storageModule> 1.28 + <storageModule moduleId="cdtBuildSystem" version="4.0.0"> 1.29 + <configuration artifactName="cline" buildProperties="" description="Debug Configuration" id="cdt.managedbuild.toolchain.gnu.mingw.base.1677102573.679714212" name="Debug" parent="org.eclipse.cdt.build.core.emptycfg"> 1.30 + <folderInfo id="cdt.managedbuild.toolchain.gnu.mingw.base.1677102573.679714212." name="/" resourcePath=""> 1.31 + <toolChain id="cdt.managedbuild.toolchain.gnu.mingw.base.135920029" name="cdt.managedbuild.toolchain.gnu.mingw.base" superClass="cdt.managedbuild.toolchain.gnu.mingw.base"> 1.32 + <targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.PE" id="cdt.managedbuild.target.gnu.platform.mingw.base.880199945" name="Debug Platform" osList="win32" superClass="cdt.managedbuild.target.gnu.platform.mingw.base"/> 1.33 + <builder id="cdt.managedbuild.target.gnu.builder.base.1688966472" incrementalBuildTarget="all CONF=mingw-debug" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" stopOnErr="true" superClass="cdt.managedbuild.target.gnu.builder.base"/> 1.34 + <tool id="cdt.managedbuild.tool.gnu.assembler.mingw.base.1558326557" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.base"> 1.35 + <inputType id="cdt.managedbuild.tool.gnu.assembler.input.1325272903" superClass="cdt.managedbuild.tool.gnu.assembler.input"/> 1.36 + </tool> 1.37 + <tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.914408551" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/> 1.38 + <tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.base.306736786" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.base"> 1.39 + <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1972355005" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/> 1.40 + </tool> 1.41 + <tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.base.471019641" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.base"> 1.42 + <inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.238480503" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/> 1.43 + </tool> 1.44 + <tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.base.1465337709" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.base"/> 1.45 + <tool id="cdt.managedbuild.tool.gnu.cpp.linker.mingw.base.1366910272" name="MinGW C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.mingw.base"> 1.46 + <inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.718128139" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input"> 1.47 + <additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/> 1.48 + <additionalInput kind="additionalinput" paths="$(LIBS)"/> 1.49 + </inputType> 1.50 + </tool> 1.51 + </toolChain> 1.52 + </folderInfo> 1.53 + </configuration> 1.54 + </storageModule> 1.55 + <storageModule moduleId="org.eclipse.cdt.core.externalSettings"/> 1.56 + </cconfiguration> 1.57 </storageModule> 1.58 <storageModule moduleId="cdtBuildSystem" version="4.0.0"> 1.59 <project id="cline.null.1826762082" name="cline"/>
2.1 --- a/.project Thu Jan 26 15:55:52 2012 +0100 2.2 +++ b/.project Thu Feb 02 14:17:35 2012 +0100 2.3 @@ -51,7 +51,7 @@ 2.4 </dictionary> 2.5 <dictionary> 2.6 <key>org.eclipse.cdt.make.core.fullBuildTarget</key> 2.7 - <value>all</value> 2.8 + <value>all CONF=mingw</value> 2.9 </dictionary> 2.10 <dictionary> 2.11 <key>org.eclipse.cdt.make.core.stopOnError</key>
3.1 --- a/Makefile Thu Jan 26 15:55:52 2012 +0100 3.2 +++ b/Makefile Thu Feb 02 14:17:35 2012 +0100 3.3 @@ -1,8 +1,43 @@ 3.4 -CC = gcc 3.5 -CARG = -Wall -std=gnu99 -O 3.6 -BUILDDIR = build/ 3.7 -OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') 3.8 -BIN = ${BUILDDIR}cline 3.9 +# 3.10 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.11 +# 3.12 +# Copyright 2011 Mike Becker. All rights reserved. 3.13 +# 3.14 +# Redistribution and use in source and binary forms, with or without 3.15 +# modification, are permitted provided that the following conditions are met: 3.16 +# 3.17 +# 1. Redistributions of source code must retain the above copyright 3.18 +# notice, this list of conditions and the following disclaimer. 3.19 +# 3.20 +# 2. Redistributions in binary form must reproduce the above copyright 3.21 +# notice, this list of conditions and the following disclaimer in the 3.22 +# documentation and/or other materials provided with the distribution. 3.23 +# 3.24 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.25 +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.26 +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 3.27 +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 3.28 +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3.29 +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 3.30 +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 3.31 +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 3.32 +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3.33 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3.34 +# 3.35 +# available configurations: 3.36 +# gcc (default) (will use libc regex implicitly) 3.37 +# mingw (will use libregex as static lib) 3.38 +# 3.39 +# debug configurations: 3.40 +# gcc-debug 3.41 +# mingw-debug 3.42 +# 3.43 + 3.44 +#ifndef CONF 3.45 +CONF = gcc 3.46 +#endif 3.47 + 3.48 +include ${CONF}.mk 3.49 3.50 .PHONY: setup run-compile teardown 3.51 3.52 @@ -13,11 +48,11 @@ 3.53 -${MAKE} compile 3.54 3.55 compile: ${OBJ} 3.56 - ${CC} -o ${BIN} ${OBJ} 3.57 + ${LD} -o ${BIN} ${OBJ} ${LDFLAGS} 3.58 3.59 setup: 3.60 mkdir -p ${BUILDDIR} 3.61 - rm -f build/cline.o 3.62 + rm -f ${BUILDDIR}cline.o 3.63 mv cline.h cline.src 3.64 cat cline.src | sed "s/VERSION.*/VERSION=\"$(shell hg identify -n) ($(shell hg identify -i))\";/g" > cline.h 3.65 3.66 @@ -26,8 +61,8 @@ 3.67 mv cline.src cline.h 3.68 3.69 ${BUILDDIR}%.o: %.c 3.70 - ${CC} ${CARG} -c -o ${BUILDDIR}$*.o $< 3.71 + ${CC} ${CFLAGS} -c -o ${BUILDDIR}$*.o $< 3.72 3.73 clean: 3.74 - rm -f build/* 3.75 + rm -f ${BUILDDIR}*.o 3.76 3.77 \ No newline at end of file
4.1 --- a/cline.c Thu Jan 26 15:55:52 2012 +0100 4.2 +++ b/cline.c Thu Feb 02 14:17:35 2012 +0100 4.3 @@ -22,8 +22,9 @@ 4.4 "\n\nOptions:" 4.5 "\n -b <level> - binary file heuristics level (default medium)" 4.6 "\n One of: ignore low medium high" 4.7 + "\n -E <pattern> - Excludes any line matching the <pattern>" 4.8 "\n -e <start> <end> - Excludes lines between <start> and <end>" 4.9 - "\n You may use this option multiple times" 4.10 + "\n You may use these options multiple times" 4.11 "\n -h, --help - this help text" 4.12 "\n -m - print information about matching files only" 4.13 "\n -s <suffixes> - only count files with these suffixes (separated" 4.14 @@ -35,13 +36,18 @@ 4.15 "\n -V - turn verbose output off, print the result only" 4.16 "\n\n" 4.17 "The default call without any options is:" 4.18 - "\n cline ./\n" 4.19 + "\n cline ./\n\n" 4.20 "So each file in the working directory is counted. If you want to count C" 4.21 "\nsource code in your working directory and its subdirectories, type:" 4.22 "\n cline -rs .c\n" 4.23 - "\nIf you want to exclude comment lines, you may use the -e option." 4.24 + "\nIf you want to exclude comment lines, you may use the -e/-E option." 4.25 "\nAfter a line matches the regex pattern <start> any following line is" 4.26 - "\nnot counted unless a line matches the <end> pattern."; 4.27 + "\nnot counted unless a line matches the <end> pattern. A line is still " 4.28 + "\ncounted when it does not start or end with the respective patterns." 4.29 + "\nPlease note, that cline does not remove whitespace characters as this" 4.30 + "\nmight not be reasonable in some cases." 4.31 + "\n\nExample (C comments):" 4.32 + "\n cline -s .c,.h -E \"\\s*//\" -e \"\\s*/\\*\" \"\\*/\\s*\""; 4.33 4.34 printf(helpText); 4.35 } 4.36 @@ -74,7 +80,7 @@ 4.37 4.38 for (int t = 1 ; t < argc ; t++) { 4.39 4.40 - int argflags = checkArgument(argv[t], "hsSrRmvVbe"); 4.41 + int argflags = checkArgument(argv[t], "hsSrRmvVbeE"); 4.42 4.43 /* s, S */ 4.44 if ((argflags & 6) > 0) { 4.45 @@ -138,6 +144,7 @@ 4.46 return exit_with_help(settings, 1); 4.47 } 4.48 } 4.49 + /* e */ 4.50 if ((argflags & 512) > 0) { 4.51 if (t + 2 >= argc) { 4.52 return exit_with_help(settings, 1); 4.53 @@ -145,6 +152,15 @@ 4.54 t++; add_string(settings->regex->pattern_list, argv[t]); 4.55 t++; add_string(settings->regex->pattern_list, argv[t]); 4.56 } 4.57 + /* E */ 4.58 + if ((argflags & 1024) > 0) { 4.59 + t++; 4.60 + if (t >= argc) { 4.61 + return exit_with_help(settings, 1); 4.62 + } 4.63 + add_string(settings->regex->pattern_list, argv[t]); 4.64 + add_string(settings->regex->pattern_list, "$"); 4.65 + } 4.66 /* Path */ 4.67 if (argflags == 0) { 4.68 if (registerArgument(&checked, 1024)) { 4.69 @@ -167,27 +183,29 @@ 4.70 } 4.71 4.72 /* Scan directory */ 4.73 - regex_compile_all(settings->regex); 4.74 - int lines = scanDirectory((scanner_t){directory, 0}, settings); 4.75 - destroy_settings_t(settings); 4.76 + if (regex_compile_all(settings->regex)) { 4.77 + int lines = scanDirectory((scanner_t){directory, 0}, settings); 4.78 + destroy_settings_t(settings); 4.79 4.80 - /* Print double line and line count */ 4.81 - for (int t = 0 ; t < 79 ; t++) { 4.82 - printf("="); 4.83 - } 4.84 - printf("\n%73d lines\n", lines); 4.85 + /* Print double line and line count */ 4.86 + for (int t = 0 ; t < 79 ; t++) { 4.87 + printf("="); 4.88 + } 4.89 + printf("\n%73d lines\n", lines); 4.90 4.91 - if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { 4.92 - printf("\nSome files contain too long lines.\n" 4.93 - "The regex parser currently supports a maximum line length of %d." 4.94 - "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); 4.95 - } 4.96 + if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { 4.97 + printf("\nSome files contain too long lines.\n" 4.98 + "The regex parser currently supports a maximum line length of %d." 4.99 + "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); 4.100 + } 4.101 4.102 - if (!settings->verbose) { 4.103 - reopen_stdout(); 4.104 - printf("%d", lines); 4.105 + if (!settings->verbose) { 4.106 + reopen_stdout(); 4.107 + printf("%d", lines); 4.108 + } 4.109 } 4.110 4.111 fflush(stdout); 4.112 + fflush(stderr); 4.113 return 0; 4.114 }
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/gcc-debug.mk Thu Feb 02 14:17:35 2012 +0100 5.3 @@ -0,0 +1,34 @@ 5.4 +# 5.5 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5.6 +# 5.7 +# Copyright 2011 Mike Becker. All rights reserved. 5.8 +# 5.9 +# Redistribution and use in source and binary forms, with or without 5.10 +# modification, are permitted provided that the following conditions are met: 5.11 +# 5.12 +# 1. Redistributions of source code must retain the above copyright 5.13 +# notice, this list of conditions and the following disclaimer. 5.14 +# 5.15 +# 2. Redistributions in binary form must reproduce the above copyright 5.16 +# notice, this list of conditions and the following disclaimer in the 5.17 +# documentation and/or other materials provided with the distribution. 5.18 +# 5.19 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 5.20 +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5.21 +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 5.22 +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 5.23 +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5.24 +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 5.25 +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 5.26 +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 5.27 +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 5.28 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 5.29 +# 5.30 + 5.31 +CC = gcc 5.32 +LD = gcc 5.33 +CFLAGS = -Wall -std=gnu99 -O0 -ggdb 5.34 +LDFLAGS = 5.35 +BUILDDIR = build/ 5.36 +OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') 5.37 +BIN = ${BUILDDIR}cline
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/gcc.mk Thu Feb 02 14:17:35 2012 +0100 6.3 @@ -0,0 +1,34 @@ 6.4 +# 6.5 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 6.6 +# 6.7 +# Copyright 2011 Mike Becker. All rights reserved. 6.8 +# 6.9 +# Redistribution and use in source and binary forms, with or without 6.10 +# modification, are permitted provided that the following conditions are met: 6.11 +# 6.12 +# 1. Redistributions of source code must retain the above copyright 6.13 +# notice, this list of conditions and the following disclaimer. 6.14 +# 6.15 +# 2. Redistributions in binary form must reproduce the above copyright 6.16 +# notice, this list of conditions and the following disclaimer in the 6.17 +# documentation and/or other materials provided with the distribution. 6.18 +# 6.19 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 6.20 +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 6.21 +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 6.22 +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 6.23 +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 6.24 +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 6.25 +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 6.26 +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 6.27 +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 6.28 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6.29 +# 6.30 + 6.31 +CC = gcc 6.32 +LD = gcc 6.33 +CFLAGS = -Wall -std=gnu99 -O 6.34 +LDFLAGS = 6.35 +BUILDDIR = build/ 6.36 +OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') 6.37 +BIN = ${BUILDDIR}cline
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/mingw-debug.mk Thu Feb 02 14:17:35 2012 +0100 7.3 @@ -0,0 +1,34 @@ 7.4 +# 7.5 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7.6 +# 7.7 +# Copyright 2011 Mike Becker. All rights reserved. 7.8 +# 7.9 +# Redistribution and use in source and binary forms, with or without 7.10 +# modification, are permitted provided that the following conditions are met: 7.11 +# 7.12 +# 1. Redistributions of source code must retain the above copyright 7.13 +# notice, this list of conditions and the following disclaimer. 7.14 +# 7.15 +# 2. Redistributions in binary form must reproduce the above copyright 7.16 +# notice, this list of conditions and the following disclaimer in the 7.17 +# documentation and/or other materials provided with the distribution. 7.18 +# 7.19 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 7.20 +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 7.21 +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 7.22 +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 7.23 +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 7.24 +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 7.25 +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 7.26 +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 7.27 +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 7.28 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 7.29 +# 7.30 + 7.31 +CC = gcc 7.32 +LD = gcc 7.33 +CFLAGS = -Wall -std=gnu99 -O0 -g 7.34 +LDFLAGS = -static -lregex 7.35 +BUILDDIR = build/ 7.36 +OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') 7.37 +BIN = ${BUILDDIR}cline
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/mingw.mk Thu Feb 02 14:17:35 2012 +0100 8.3 @@ -0,0 +1,34 @@ 8.4 +# 8.5 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 8.6 +# 8.7 +# Copyright 2011 Mike Becker. All rights reserved. 8.8 +# 8.9 +# Redistribution and use in source and binary forms, with or without 8.10 +# modification, are permitted provided that the following conditions are met: 8.11 +# 8.12 +# 1. Redistributions of source code must retain the above copyright 8.13 +# notice, this list of conditions and the following disclaimer. 8.14 +# 8.15 +# 2. Redistributions in binary form must reproduce the above copyright 8.16 +# notice, this list of conditions and the following disclaimer in the 8.17 +# documentation and/or other materials provided with the distribution. 8.18 +# 8.19 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 8.20 +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 8.21 +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 8.22 +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 8.23 +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 8.24 +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 8.25 +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 8.26 +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 8.27 +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 8.28 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 8.29 +# 8.30 + 8.31 +CC = gcc 8.32 +LD = gcc 8.33 +CFLAGS = -Wall -std=gnu99 -O 8.34 +LDFLAGS = -static -lregex 8.35 +BUILDDIR = build/ 8.36 +OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') 8.37 +BIN = ${BUILDDIR}cline
9.1 --- a/regex_parser.c Thu Jan 26 15:55:52 2012 +0100 9.2 +++ b/regex_parser.c Thu Feb 02 14:17:35 2012 +0100 9.3 @@ -14,11 +14,26 @@ 9.4 ret->matched_lines = 0; 9.5 ret->pattern_match = 0; 9.6 ret->compiled_patterns = NULL; 9.7 + ret->compiled_pattern_count = 0; 9.8 } 9.9 return ret; 9.10 } 9.11 9.12 +void regex_destcomppats(regex_parser_t* parser) { 9.13 + if (parser->compiled_patterns != NULL) { 9.14 + for (int i = 0 ; i < parser->compiled_pattern_count ; i++) { 9.15 + if (parser->compiled_patterns[i] != NULL) { 9.16 + free(parser->compiled_patterns[i]); 9.17 + } 9.18 + } 9.19 + free(parser->compiled_patterns); 9.20 + parser->compiled_patterns = NULL; 9.21 + parser->compiled_pattern_count = 0; 9.22 + } 9.23 +} 9.24 + 9.25 void destroy_regex_parser_t(regex_parser_t* parser) { 9.26 + regex_destcomppats(parser); 9.27 destroy_string_list_t(parser->pattern_list); 9.28 free(parser); 9.29 } 9.30 @@ -27,24 +42,69 @@ 9.31 return parser->pattern_match > 0; 9.32 } 9.33 9.34 -void regex_compile_all(regex_parser_t* parser) { 9.35 - size_t pcount = parser->pattern_list->count; 9.36 - if (pcount > 0) { 9.37 - if (parser->compiled_patterns != NULL) { 9.38 - free(parser->compiled_patterns); 9.39 - } 9.40 - parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); 9.41 +int regex_parser_do(regex_parser_t* parser, char* input) { 9.42 + int err = REG_NOMATCH; 9.43 + if (parser->compiled_pattern_count > 0) { 9.44 + regmatch_t match; 9.45 9.46 - regex_t* re = malloc(sizeof(regex_t)); 9.47 - for (int i = 0 ; i < pcount ; i++) { 9.48 - if (regcomp(re, parser->pattern_list->items[i], 9.49 - REG_EXTENDED|REG_NOSUB) == 0) { 9.50 - parser->compiled_patterns[i] = re; 9.51 - } else { 9.52 - fprintf(stderr, "Cannot compile: %s\n", 9.53 - (parser->pattern_list->items[i])); 9.54 - parser->compiled_patterns[i] = NULL; 9.55 + if (regex_parser_matching(parser)) { 9.56 + parser->matched_lines++; 9.57 + 9.58 + err = regexec(parser->compiled_patterns[parser->pattern_match], 9.59 + input, 1, &match, 0); 9.60 + if (err > 0 && err != REG_NOMATCH) { 9.61 + fprintf(stderr, "Regex-Error: 0x%08x", err); 9.62 + } 9.63 + if (err == 0) { 9.64 + parser->pattern_match = 0; 9.65 + /* do not match line, if it does not end with the pattern */ 9.66 + if (match.rm_eo < strlen(input)) { 9.67 + parser->matched_lines--; 9.68 + } 9.69 + } 9.70 + } else { 9.71 + for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { 9.72 + err = regexec(parser->compiled_patterns[i], input, 1, &match, 0); 9.73 + if (err > 0 && err != REG_NOMATCH) { 9.74 + fprintf(stderr, "Regex-Error: 0x%08x", err); 9.75 + } 9.76 + if (err == 0) { 9.77 + parser->pattern_match = i+1; 9.78 + parser->matched_lines = 0; 9.79 + /* Check, if end pattern is also in this line */ 9.80 + regex_parser_do(parser, input); 9.81 + /* do not match line, if it does not start with the pattern */ 9.82 + if (match.rm_so > 0 && parser->matched_lines > 0) { 9.83 + parser->matched_lines--; 9.84 + } 9.85 + break; 9.86 + } 9.87 } 9.88 } 9.89 } 9.90 + return err; 9.91 } 9.92 + 9.93 +bool regex_compile_all(regex_parser_t* parser) { 9.94 + bool success = true; 9.95 + size_t pcount = parser->pattern_list->count; 9.96 + if (pcount > 0) { 9.97 + regex_destcomppats(parser); 9.98 + parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); 9.99 + parser->compiled_pattern_count = pcount; 9.100 + 9.101 + regex_t* re; 9.102 + for (int i = 0 ; i < pcount ; i++) { 9.103 + re = malloc(sizeof(regex_t)); 9.104 + if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) { 9.105 + parser->compiled_patterns[i] = re; 9.106 + } else { 9.107 + fprintf(stderr, "Cannot compile pattern: %s\n", 9.108 + (parser->pattern_list->items[i])); 9.109 + parser->compiled_patterns[i] = NULL; 9.110 + success = false; 9.111 + } 9.112 + } 9.113 + } 9.114 + return success; 9.115 +}
10.1 --- a/regex_parser.h Thu Jan 26 15:55:52 2012 +0100 10.2 +++ b/regex_parser.h Thu Feb 02 14:17:35 2012 +0100 10.3 @@ -18,6 +18,7 @@ 10.4 typedef struct { 10.5 string_list_t* pattern_list; /* even entries: start ; odd entries: end */ 10.6 regex_t** compiled_patterns; 10.7 + size_t compiled_pattern_count; 10.8 unsigned int pattern_match; /* save position of end pattern to match - 10.9 NULL when a start pattern shall match first */ 10.10 unsigned int matched_lines; 10.11 @@ -31,7 +32,8 @@ 10.12 void destroy_regex_parser_t(regex_parser_t*); 10.13 10.14 bool regex_parser_matching(regex_parser_t*); 10.15 -void regex_compile_all(regex_parser_t*); 10.16 +bool regex_compile_all(regex_parser_t*); 10.17 +int regex_parser_do(regex_parser_t*, char*); 10.18 10.19 #ifdef _cplusplus 10.20 }
11.1 --- a/scanner.c Thu Jan 26 15:55:52 2012 +0100 11.2 +++ b/scanner.c Thu Feb 02 14:17:35 2012 +0100 11.3 @@ -77,9 +77,14 @@ 11.4 11.5 bfile = bfile_check(settings->bfileHeuristics, a); 11.6 11.7 - if (a == 10) { 11.8 + if (a == 10 || a == EOF) { 11.9 line_buffer[line_buffer_offset] = 0; 11.10 - /* TODO: do regex parsing */ 11.11 + if (regex_parser_do(settings->regex, line_buffer) == 0) { 11.12 + /* Only subtract lines when matching has finished */ 11.13 + if (!regex_parser_matching(settings->regex)) { 11.14 + lines -= settings->regex->matched_lines; 11.15 + } 11.16 + } 11.17 11.18 line_buffer_offset = 0; 11.19 lines++;