fixed makefile to run safely on compile errors + added -V option to cline

Tue, 20 Sep 2011 15:19:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 20 Sep 2011 15:19:28 +0200
changeset 16
bc9a0fefd892
parent 15
9a262e046ab8
child 17
5f43f733cc12

fixed makefile to run safely on compile errors + added -V option to cline

.cproject file | annotate | diff | comparison | revisions
Makefile file | annotate | diff | comparison | revisions
cline.c file | annotate | diff | comparison | revisions
scanner.c file | annotate | diff | comparison | revisions
settings.c file | annotate | diff | comparison | revisions
settings.h file | annotate | diff | comparison | revisions
stream.c file | annotate | diff | comparison | revisions
stream.h file | annotate | diff | comparison | revisions
--- a/.cproject	Mon Sep 19 08:11:08 2011 +0200
+++ b/.cproject	Tue Sep 20 15:19:28 2011 +0200
@@ -20,7 +20,7 @@
 					<folderInfo id="cdt.managedbuild.toolchain.gnu.mingw.base.1677102573.925482417" name="/" resourcePath="">
 						<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.base.1515980604" name="cdt.managedbuild.toolchain.gnu.mingw.base" superClass="cdt.managedbuild.toolchain.gnu.mingw.base">
 							<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"/>
-							<builder id="cdt.managedbuild.target.gnu.builder.base.349472496" managedBuildOn="false" superClass="cdt.managedbuild.target.gnu.builder.base"/>
+							<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"/>
 							<tool id="cdt.managedbuild.tool.gnu.assembler.mingw.base.1362600845" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.base">
 								<inputType id="cdt.managedbuild.tool.gnu.assembler.input.902665459" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
 							</tool>
--- a/Makefile	Mon Sep 19 08:11:08 2011 +0200
+++ b/Makefile	Tue Sep 20 15:19:28 2011 +0200
@@ -1,23 +1,32 @@
 CC = gcc
 BUILDDIR = build/
-OBJ = $(shell ls | grep \.c | sed 's/^\([^.]*\)\.c/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ')
+OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ')
 BIN = ${BUILDDIR}cline
 
-all: addrnum ${OBJ} remrnum
+.PHONY: setup run-compile teardown
+
+all: setup run-compile teardown
+	
+	
+run-compile:
+	-${MAKE} compile
+
+compile: ${OBJ}
 	${CC} -o ${BIN} ${OBJ}
 
-addrnum:
-	rm build/cline.o
+setup:
+	mkdir -p ${BUILDDIR}
+	rm -f build/cline.o
 	mv cline.h cline.src
-	cat cline.src | sed "s/VERSION.*/VERSION=\"$(shell hg identify -i)\";/g" > cline.h
+	cat cline.src | sed "s/VERSION.*/VERSION=\"$(shell hg identify -n) ($(shell hg identify -i))\";/g" > cline.h
 	
-remrnum:
-	rm cline.h
+teardown:
+	rm -f cline.h
 	mv cline.src cline.h
 
 ${BUILDDIR}%.o: %.c
-	mkdir -p ${BUILDDIR}
 	${CC} -c -std=c99 -o ${BUILDDIR}$*.o $<
 
 clean:
-	rm build/*
+	rm -f build/*
+	
\ No newline at end of file
--- a/cline.c	Mon Sep 19 08:11:08 2011 +0200
+++ b/cline.c	Tue Sep 20 15:19:28 2011 +0200
@@ -9,6 +9,7 @@
 #include "scanner.h"
 #include "settings.h"
 #include "arguments.h"
+#include "stream.h"
 
 void printHelpText() {
   const char* helpText = 
@@ -26,6 +27,7 @@
     "\n                        (separated by commas)"
     "\n  -r, -R              - includes subdirectories"
     "\n  -v, --version       - print out version information"
+    "\n  -V                  - turn verbose output off, print the result only"
     "\n\n"
     "The default call without any options is:"    
     "\n  cline ./\n"
@@ -37,7 +39,7 @@
 }
 
 int exit_with_version(settings_t* settings) {
-  printf("cline - Version: %s", VERSION);
+  printf("cline - Revision: %s", VERSION);
   destroy_settings_t(settings);
   return 0;
 }
@@ -64,7 +66,7 @@
 
   for (int t = 1 ; t < argc ; t++) {
 
-    int argflags = checkArgument(argv[t], "hsSrRmv");
+    int argflags = checkArgument(argv[t], "hsSrRmvV");
 
     // s, S
     if ((argflags & 6) > 0) {
@@ -100,6 +102,13 @@
     if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
       return exit_with_version(settings);
     }
+    // V
+    if ((argflags & 128) > 0) {
+      if (registerArgument(&checked, 128)) {
+        return exit_with_help(settings, 1);
+      }
+      settings->verbose = false;
+    }
     // Path
     if (argflags == 0) {
       if (registerArgument(&checked, 1024)) {
@@ -109,6 +118,11 @@
     }
   }
 
+  // Configure output
+  if (!settings->verbose) {
+    close_stdout();
+  }
+
   // Find tokens
   char* finder = strtok(suffix, ",");
   while (finder != NULL) {
@@ -130,20 +144,26 @@
   destroy_settings_t(settings);
 
   // Print double line and line count
-  #ifdef _WIN32
+#ifdef _WIN32
     const int columns = 79;
-  #else
+#else
     const int columns = 80;
-  #endif /* _WIN32 */
+#endif /* _WIN32 */
 
   for (int t = 0 ; t < columns ; t++) {
     printf("=");
   }
-  #ifdef _WIN32
+#ifdef _WIN32
     printf("\n%73d lines\n", lines);
-  #else
+#else
     printf("\n%74d lines\n", lines);
-  #endif /* _WIN32 */
+#endif /* _WIN32 */
 
+  if (!settings->verbose) {
+    reopen_stdout();
+    printf("%d", lines);
+  }
+
+  fflush(stdout);
   return 0;
 }
--- a/scanner.c	Mon Sep 19 08:11:08 2011 +0200
+++ b/scanner.c	Tue Sep 20 15:19:28 2011 +0200
@@ -44,10 +44,6 @@
 
       // Count lines
       lines = 0;
-      /* char filename[(1+strlen(currdir)+strlen(entry->d_name))];
-      strcpy(filename, currdir);
-      strncat(filename, &settings->fileSeparator, 1);
-      strcat(filename, entry->d_name); */
       if (testSuffix(filename, settings)) {
         FILE *file = fopen(filename, "r");
         if (file == NULL) {
@@ -72,8 +68,7 @@
         #endif /* _WIN32 */
 
         lineSum += lines;
-      }
-      else {
+      } else {
         if (!settings->matchesOnly) {
           // Print hint
           #ifdef _WIN32
--- a/settings.c	Mon Sep 19 08:11:08 2011 +0200
+++ b/settings.c	Tue Sep 20 15:19:28 2011 +0200
@@ -19,6 +19,7 @@
     settings->includeSuffixes    = false;
     settings->matchesOnly        = false;
     settings->suffixList         = new_suffix_list_t();
+    settings->verbose            = true;
   }
 
   return settings;
--- a/settings.h	Mon Sep 19 08:11:08 2011 +0200
+++ b/settings.h	Tue Sep 20 15:19:28 2011 +0200
@@ -17,6 +17,7 @@
   bool recursive;
   bool includeSuffixes;
   bool matchesOnly;
+  bool verbose;
 } settings_t;
 
 #ifdef _cplusplus
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stream.c	Tue Sep 20 15:19:28 2011 +0200
@@ -0,0 +1,25 @@
+/*
+ * stream.c
+ *
+ *  Created on: 20.09.2011
+ *      Author: beckermi
+ */
+
+#include "stream.h"
+
+void close_stdout() {
+#ifdef _WIN32
+  _STREAM_STDOUT = dup(STDOUT_FILENO);
+#endif
+  freopen("/dev/null", "w", stdout);
+}
+
+void reopen_stdout() {
+#ifdef _WIN32
+  close(STDOUT_FILENO);
+  fdopen(dup(_STREAM_STDOUT), "wa");
+  close(_STREAM_STDOUT);
+#else
+  freopen("/dev/stdout", "w", stdout);
+#endif
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stream.h	Tue Sep 20 15:19:28 2011 +0200
@@ -0,0 +1,28 @@
+/*
+ * stream.h
+ *
+ *  Created on: 20.09.2011
+ *      Author: beckermi
+ */
+
+#ifndef STREAM_H_
+#define STREAM_H_
+
+#include "stdinc.h"
+
+#ifdef _WIN32
+int _STREAM_STDOUT;
+#endif
+
+#ifdef _cplusplus
+extern "C" {
+#endif
+
+void close_stdout();
+void reopen_stdout();
+
+#ifdef _cplusplus
+extern "C" }
+#endif
+
+#endif /* STREAM_H_ */

mercurial