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
     1.1 --- a/.cproject	Mon Sep 19 08:11:08 2011 +0200
     1.2 +++ b/.cproject	Tue Sep 20 15:19:28 2011 +0200
     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" managedBuildOn="false" superClass="cdt.managedbuild.target.gnu.builder.base"/>
     1.8 +							<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.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>
     2.1 --- a/Makefile	Mon Sep 19 08:11:08 2011 +0200
     2.2 +++ b/Makefile	Tue Sep 20 15:19:28 2011 +0200
     2.3 @@ -1,23 +1,32 @@
     2.4  CC = gcc
     2.5  BUILDDIR = build/
     2.6 -OBJ = $(shell ls | grep \.c | sed 's/^\([^.]*\)\.c/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ')
     2.7 +OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ')
     2.8  BIN = ${BUILDDIR}cline
     2.9  
    2.10 -all: addrnum ${OBJ} remrnum
    2.11 +.PHONY: setup run-compile teardown
    2.12 +
    2.13 +all: setup run-compile teardown
    2.14 +	
    2.15 +	
    2.16 +run-compile:
    2.17 +	-${MAKE} compile
    2.18 +
    2.19 +compile: ${OBJ}
    2.20  	${CC} -o ${BIN} ${OBJ}
    2.21  
    2.22 -addrnum:
    2.23 -	rm build/cline.o
    2.24 +setup:
    2.25 +	mkdir -p ${BUILDDIR}
    2.26 +	rm -f build/cline.o
    2.27  	mv cline.h cline.src
    2.28 -	cat cline.src | sed "s/VERSION.*/VERSION=\"$(shell hg identify -i)\";/g" > cline.h
    2.29 +	cat cline.src | sed "s/VERSION.*/VERSION=\"$(shell hg identify -n) ($(shell hg identify -i))\";/g" > cline.h
    2.30  	
    2.31 -remrnum:
    2.32 -	rm cline.h
    2.33 +teardown:
    2.34 +	rm -f cline.h
    2.35  	mv cline.src cline.h
    2.36  
    2.37  ${BUILDDIR}%.o: %.c
    2.38 -	mkdir -p ${BUILDDIR}
    2.39  	${CC} -c -std=c99 -o ${BUILDDIR}$*.o $<
    2.40  
    2.41  clean:
    2.42 -	rm build/*
    2.43 +	rm -f build/*
    2.44 +	
    2.45 \ No newline at end of file
     3.1 --- a/cline.c	Mon Sep 19 08:11:08 2011 +0200
     3.2 +++ b/cline.c	Tue Sep 20 15:19:28 2011 +0200
     3.3 @@ -9,6 +9,7 @@
     3.4  #include "scanner.h"
     3.5  #include "settings.h"
     3.6  #include "arguments.h"
     3.7 +#include "stream.h"
     3.8  
     3.9  void printHelpText() {
    3.10    const char* helpText = 
    3.11 @@ -26,6 +27,7 @@
    3.12      "\n                        (separated by commas)"
    3.13      "\n  -r, -R              - includes subdirectories"
    3.14      "\n  -v, --version       - print out version information"
    3.15 +    "\n  -V                  - turn verbose output off, print the result only"
    3.16      "\n\n"
    3.17      "The default call without any options is:"    
    3.18      "\n  cline ./\n"
    3.19 @@ -37,7 +39,7 @@
    3.20  }
    3.21  
    3.22  int exit_with_version(settings_t* settings) {
    3.23 -  printf("cline - Version: %s", VERSION);
    3.24 +  printf("cline - Revision: %s", VERSION);
    3.25    destroy_settings_t(settings);
    3.26    return 0;
    3.27  }
    3.28 @@ -64,7 +66,7 @@
    3.29  
    3.30    for (int t = 1 ; t < argc ; t++) {
    3.31  
    3.32 -    int argflags = checkArgument(argv[t], "hsSrRmv");
    3.33 +    int argflags = checkArgument(argv[t], "hsSrRmvV");
    3.34  
    3.35      // s, S
    3.36      if ((argflags & 6) > 0) {
    3.37 @@ -100,6 +102,13 @@
    3.38      if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
    3.39        return exit_with_version(settings);
    3.40      }
    3.41 +    // V
    3.42 +    if ((argflags & 128) > 0) {
    3.43 +      if (registerArgument(&checked, 128)) {
    3.44 +        return exit_with_help(settings, 1);
    3.45 +      }
    3.46 +      settings->verbose = false;
    3.47 +    }
    3.48      // Path
    3.49      if (argflags == 0) {
    3.50        if (registerArgument(&checked, 1024)) {
    3.51 @@ -109,6 +118,11 @@
    3.52      }
    3.53    }
    3.54  
    3.55 +  // Configure output
    3.56 +  if (!settings->verbose) {
    3.57 +    close_stdout();
    3.58 +  }
    3.59 +
    3.60    // Find tokens
    3.61    char* finder = strtok(suffix, ",");
    3.62    while (finder != NULL) {
    3.63 @@ -130,20 +144,26 @@
    3.64    destroy_settings_t(settings);
    3.65  
    3.66    // Print double line and line count
    3.67 -  #ifdef _WIN32
    3.68 +#ifdef _WIN32
    3.69      const int columns = 79;
    3.70 -  #else
    3.71 +#else
    3.72      const int columns = 80;
    3.73 -  #endif /* _WIN32 */
    3.74 +#endif /* _WIN32 */
    3.75  
    3.76    for (int t = 0 ; t < columns ; t++) {
    3.77      printf("=");
    3.78    }
    3.79 -  #ifdef _WIN32
    3.80 +#ifdef _WIN32
    3.81      printf("\n%73d lines\n", lines);
    3.82 -  #else
    3.83 +#else
    3.84      printf("\n%74d lines\n", lines);
    3.85 -  #endif /* _WIN32 */
    3.86 +#endif /* _WIN32 */
    3.87  
    3.88 +  if (!settings->verbose) {
    3.89 +    reopen_stdout();
    3.90 +    printf("%d", lines);
    3.91 +  }
    3.92 +
    3.93 +  fflush(stdout);
    3.94    return 0;
    3.95  }
     4.1 --- a/scanner.c	Mon Sep 19 08:11:08 2011 +0200
     4.2 +++ b/scanner.c	Tue Sep 20 15:19:28 2011 +0200
     4.3 @@ -44,10 +44,6 @@
     4.4  
     4.5        // Count lines
     4.6        lines = 0;
     4.7 -      /* char filename[(1+strlen(currdir)+strlen(entry->d_name))];
     4.8 -      strcpy(filename, currdir);
     4.9 -      strncat(filename, &settings->fileSeparator, 1);
    4.10 -      strcat(filename, entry->d_name); */
    4.11        if (testSuffix(filename, settings)) {
    4.12          FILE *file = fopen(filename, "r");
    4.13          if (file == NULL) {
    4.14 @@ -72,8 +68,7 @@
    4.15          #endif /* _WIN32 */
    4.16  
    4.17          lineSum += lines;
    4.18 -      }
    4.19 -      else {
    4.20 +      } else {
    4.21          if (!settings->matchesOnly) {
    4.22            // Print hint
    4.23            #ifdef _WIN32
     5.1 --- a/settings.c	Mon Sep 19 08:11:08 2011 +0200
     5.2 +++ b/settings.c	Tue Sep 20 15:19:28 2011 +0200
     5.3 @@ -19,6 +19,7 @@
     5.4      settings->includeSuffixes    = false;
     5.5      settings->matchesOnly        = false;
     5.6      settings->suffixList         = new_suffix_list_t();
     5.7 +    settings->verbose            = true;
     5.8    }
     5.9  
    5.10    return settings;
     6.1 --- a/settings.h	Mon Sep 19 08:11:08 2011 +0200
     6.2 +++ b/settings.h	Tue Sep 20 15:19:28 2011 +0200
     6.3 @@ -17,6 +17,7 @@
     6.4    bool recursive;
     6.5    bool includeSuffixes;
     6.6    bool matchesOnly;
     6.7 +  bool verbose;
     6.8  } settings_t;
     6.9  
    6.10  #ifdef _cplusplus
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/stream.c	Tue Sep 20 15:19:28 2011 +0200
     7.3 @@ -0,0 +1,25 @@
     7.4 +/*
     7.5 + * stream.c
     7.6 + *
     7.7 + *  Created on: 20.09.2011
     7.8 + *      Author: beckermi
     7.9 + */
    7.10 +
    7.11 +#include "stream.h"
    7.12 +
    7.13 +void close_stdout() {
    7.14 +#ifdef _WIN32
    7.15 +  _STREAM_STDOUT = dup(STDOUT_FILENO);
    7.16 +#endif
    7.17 +  freopen("/dev/null", "w", stdout);
    7.18 +}
    7.19 +
    7.20 +void reopen_stdout() {
    7.21 +#ifdef _WIN32
    7.22 +  close(STDOUT_FILENO);
    7.23 +  fdopen(dup(_STREAM_STDOUT), "wa");
    7.24 +  close(_STREAM_STDOUT);
    7.25 +#else
    7.26 +  freopen("/dev/stdout", "w", stdout);
    7.27 +#endif
    7.28 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/stream.h	Tue Sep 20 15:19:28 2011 +0200
     8.3 @@ -0,0 +1,28 @@
     8.4 +/*
     8.5 + * stream.h
     8.6 + *
     8.7 + *  Created on: 20.09.2011
     8.8 + *      Author: beckermi
     8.9 + */
    8.10 +
    8.11 +#ifndef STREAM_H_
    8.12 +#define STREAM_H_
    8.13 +
    8.14 +#include "stdinc.h"
    8.15 +
    8.16 +#ifdef _WIN32
    8.17 +int _STREAM_STDOUT;
    8.18 +#endif
    8.19 +
    8.20 +#ifdef _cplusplus
    8.21 +extern "C" {
    8.22 +#endif
    8.23 +
    8.24 +void close_stdout();
    8.25 +void reopen_stdout();
    8.26 +
    8.27 +#ifdef _cplusplus
    8.28 +extern "C" }
    8.29 +#endif
    8.30 +
    8.31 +#endif /* STREAM_H_ */

mercurial