full adaptation to c99 master
authorLaurent Mazet <mazet@softndesign.org>
Tue, 15 Apr 2025 11:21:05 +0000 (13:21 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 15 Apr 2025 11:21:05 +0000 (13:21 +0200)
getcomments.pl [deleted file]
indent.c
makefile

diff --git a/getcomments.pl b/getcomments.pl
deleted file mode 100644 (file)
index 47edf67..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-
-# default value
-my $format = "%";
-my $pattern = "";
-
-# help message
-sub usage() {
-
-  print <<EOF;
-usage: getcomments [-f string] [-h] [-p regex] file...
- -f|--format string: format string for output printing [%]
- -h|--help: help message
- -p|--pattern regex: pattern matching on block []
-
- Extract C/C++ block of comments
-
-Example: getcomments.pl -p='test:\\s' -f='./%' random.c
-EOF
-
-  exit 1;
-}
-
-usage() if ($#ARGV < 0);
-
-# process argument
-foreach my $arg (@ARGV) {
-  use vars qw/$caif $caip $naif $naip/;
-
-  # analyse format argument
-  ($caif, $_) = ($arg =~ /^(-f|--format)=(.*)/);
-  ($caif, $_) = (1, $arg) if ($naif);
-  next if ($naif = ($arg =~ /^(-f|--format)$/));
-  if ($caif) { $format = $_; next }
-
-  # check for help message
-  usage() if ($arg =~ /^(-h|--help)$/);
-
-  # analyse pattern argument
-  ($caip, $_) = ($arg =~ /^(-p|--pattern)=(.*)/);
-  ($caip, $_) = (1, $arg) if ($naip);
-  next if ($naip = ($arg =~ /^(-p|--pattern)$/));
-  if ($caip) { $pattern = $_; next }
-
-  # no more argument, only file
-  my $filename = $arg;
-
-  # open file
-  if (!open (IN, "<", $filename)) {
-    print "Can not open $filename\n";
-  }
-
-  # init table of comments
-  my @comments;
-  $#comments = -1;
-
-  # read all the file
-  while ($_ .= <IN>) {
-    my $cmt;
-
-    # process c++ comments
-    ($cmt, $_) = m{//\s*(.*?)\s*$()} if (m{//} && !m{/\*.*//});
-
-    # process standard c comments
-    ($cmt, $_) = m{^.*?/\*\s*(.*?)\s*\*/(.*)}s if (m{/\*.*\*/}s);
-
-    push(@comments, $cmt) if ($cmt);
-
-    # empty buffer if no comment is present
-    undef($_) if (!m{/[/*]});
-  }
-
-  # close file
-  close (IN);
-
-  # display comment blocks
-  foreach my $block (@comments) {
-    if (($block) = ($block =~ /$pattern(.*)/s)) {
-      ($_ = $format) =~ s/%/$block/gs;
-      print "$_\n";
-    }
-  }
-}
index 26b19288163877e594201fe048c3e0d0273f2fcb..11fb0ecd00017eac011549c9a1113f8fbce85265 100644 (file)
--- a/indent.c
+++ b/indent.c
@@ -51,11 +51,11 @@ void usage (int ret)
 
 /* indent function */
 
-int indent (FILE *fin, FILE *fout, cmode_t cmode) {
+int indent (FILE *fin, FILE *fout, cmode_t __attribute__((unused)) cmode) {
     char bufin[BUFFERSIZE + 1] = {0};
     char bufout[BUFFERSIZE * TABSIZE + 1] = {0};
-    size_t i, nb;
-    size_t nbindent = 0;
+    int nb;
+    int nbindent = 0;
     int begin = 1;
     int parent = 0;
     int comment = 0;
@@ -72,7 +72,7 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) {
         /* read file */
         nb = fread (bufin, 1, BUFFERSIZE, fin);
         VERBOSE (DEBUG, fprintf (stdout, "buffer in: %d\n", nb));
-        if (errno != 0) {
+        if (ferror (fin)) {
             VERBOSE (ERROR, fprintf (stderr, "can't read file (%d)\n", errno));
             exit (1);
         }
@@ -99,7 +99,7 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) {
                 case '/':
                     comment = (ptin[1] == '/') ? 1 : (ptin[1] == '*') ? 2 : 0;
                     if (begin) {
-                        for (i = 0; i < nbindent * TABSIZE; i++) {
+                        for (int i = 0; i < nbindent * TABSIZE; i++) {
                             *ptout++ = ' ';
                         }
                         begin = 0;
@@ -116,7 +116,7 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) {
                     break;
                 case '{':
                     *ptout++ = '\n';
-                    for (i = 0; i < nbindent * TABSIZE; i++) {
+                    for (int i = 0; i < nbindent * TABSIZE; i++) {
                         *ptout++ = ' ';
                     }
                     *ptout++ = *ptin;
@@ -128,7 +128,7 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) {
                 case '}':
                     *ptout++ = '\n';
                     nbindent--;
-                    for (i = 0; i < nbindent * TABSIZE; i++) {
+                    for (int i = 0; i < nbindent * TABSIZE; i++) {
                         *ptout++ = ' ';
                     }
                     *ptout++ = *ptin;
@@ -165,7 +165,7 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) {
                         character ^= 1;
                     }
                     if (begin) {
-                        for (i = 0; i < nbindent * TABSIZE; i++) {
+                        for (int i = 0; i < nbindent * TABSIZE; i++) {
                             *ptout++ = ' ';
                         }
                         begin = 0;
@@ -180,11 +180,11 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) {
         ptout = '\0';
 
         /* write file */
-        VERBOSE (DEBUG, fprintf (stdout, "buffer out: %d\n", strlen (bufout)));
+        VERBOSE (DEBUG, fprintf (stdout, "buffer out: %d\n", (int)strlen (bufout)));
         ptout = bufout;
-        while ((nb = fwrite (ptout, 1, strlen (ptout), fout)) != strlen (ptout)) {
-            VERBOSE (DEBUG, fprintf (stdout, "buffer out: %d/%d\n", nb, strlen (ptout)));
-            if (errno != 0) {
+        while ((nb = fwrite (ptout, 1, strlen (ptout), fout)) != (int)strlen (ptout)) {
+            VERBOSE (DEBUG, fprintf (stdout, "buffer out: %d/%d\n", nb, (int)strlen (ptout)));
+            if (ferror (fout)) {
                 VERBOSE (ERROR, fprintf (stderr, "can't write file (%d)\n", errno));
                 exit (1);
             }
@@ -243,14 +243,9 @@ int main (int argc, char *argv[])
     }
 
     /* check input */
-    FILE *fin = NULL;
-    if (input) {
-        fin = fopen (input, "rb");
-        if (!fin) {
-            VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
-        }
-    } else {
-        fin = stdin;
+    FILE *fin = input ? fopen (input, "rb") : stdin;
+    if (!fin) {
+        VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
     }
 
     /* check output */
@@ -259,7 +254,9 @@ int main (int argc, char *argv[])
         fout = fopen (output, "wb");
         if (!fout) {
             VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
-            fclose (fin);
+            if ((fin != stdin) && (fin)) {
+                fclose (fin);
+            }
         }
     } else {
         fout = stdout;
index 2fa923fb527569493ad27fe47e63ae3b015af0a9..09ecb8bc309abe5c9bba31812b7dfecc3743bcb9 100644 (file)
--- a/makefile
+++ b/makefile
@@ -5,6 +5,7 @@ CC = gcc
 #INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
 INCLUDES =
 OFLAGS  = -O4 -Os
+#OFLAGS  = -O0
 #OFLAGS  = -O4 -ffast-math -finline-functions
 #OFLAGS  = -O4 -finline-functions
 #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
@@ -13,16 +14,30 @@ OFLAGS  = -O4 -Os
 CFLAGS += -W -Wall -Wextra -g
 #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
-LDFLAGS += -g $(OPTIONS)
-
-ifeq ($(OS),Windows_NT)
+LDFLAGS += -g $(LDOPTS) $(OPTIONS)
+
+LDOPT = linker
+MV = mv
+ifneq (, $(findstring linux, $(MAKE_HOST)))
+# Linux
+else ifneq (, $(findstring mingw, $(MAKE_HOST)))
+# Windows MinGw
+CFLAGS += -DWIN32
 #LDLIBS += -lws2_32
+LDOPT = winlnk
+else ifneq (, $(findstring cygwin, $(MAKE_HOST)))
+CFLAGS += -DWIN32
+# Windows CygWin
+LDOPT = winlnk
+else ifneq (, $(findstring msdos, $(MAKE_HOST)))
+# MSDOS
+LDOPT = doslnk
+MV = move
 endif
 
 # Targets
 
-ALLEXE  =
-ALLEXE += indent
+ALLEXE  = $(shell for f in *.c; do grep -q '/\*\slinker:' $$f && echo $${f/.c}; done)
 
 SHELL = bash
 
@@ -43,18 +58,26 @@ INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1)
 
 VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; }
 
+GETCOMMENTS = awk '/\/\*\s*$(1):/,/\*\// { sub(/.*\/\*\s*$(1):/, ""); sub (/\s*\*\/.*/, ""); print } /\/\/\s*$(1):/ {sub (/.*\/\/\s*$(1):/, ""); print }' $(2)
+#GETCOMMENTS = perl -- getcomments.pl -p='$(1):\s' -f='%' $(2)
+
 ## Generic rules
 
 all: depends
        $(MAKE) $(ALLEXE:%=%.exe)
 
+analyze:
+       make purge
+       scan-build make
+       #scan-build -stats make
+
 count:
        wc $(wildcard *.c *.h) $(MAKEFILE_LIST)
 
 clean:
        $(call TITLE, "Cleaning")
        touch clean
-       rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_* gmon.out)
+       rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_* gmon.out _)
        $(call PASS, SUCCESS)
 
 depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE))
@@ -71,7 +94,8 @@ purge: clean
        rm -f purge $(ALLEXE:%=%.exe)
        $(call PASS, SUCCESS)
 
-valgrinds: all
+valgrinds:
+       $(MAKE) all
        $(MAKE) $(addprefix valgrind_,$(ALLEXE))
 
 wipe: purge
@@ -80,7 +104,9 @@ wipe: purge
        rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.glog)
        $(call PASS, SUCCESS)
 
-tests: all
+tests:
+       -rm -f $(ALLEXE)
+       $(MAKE) all
        $(MAKE) $(addprefix test_,$(ALLEXE))
 
 ## Main rules
@@ -90,13 +116,14 @@ include $(wildcard *.ld)
 
 gcov_%:
        $(MAKE) purge
-       OPTIONS="-coverage -O0" $(MAKE)
+       $(MAKE) depends
+       OPTIONS="-coverage -O0" $(MAKE) ${@:gcov_%=%}.exe
        $(MAKE) test_$(@:gcov_%=%)
        gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)`
        touch gcov
        rm -f gcov $(wildcard *.gcda *.gcno)
        $(MAKE) purge
-       grep '#####' *.c.gcov || true
+       grep '^ *#####' *.c.gcov || true
 
 gprof_%:
        $(MAKE) purge
@@ -119,9 +146,9 @@ gprof_%:
 
 %.test: %.c
        $(call TITLE, "Building $@")
-#      awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@
-       perl -- getcomments.pl -p='test:\s' -f='%' $< > $@
+       $(call GETCOMMENTS,test, $<) > $@
        $(call PASS, SUCCESS)
+       -rm -f _
 
 test_%: %.test %.exe
        IFS=$$'\n'; RC=0; \
@@ -130,6 +157,7 @@ test_%: %.test %.exe
          eval $$test; \
          [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
                        || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
+         test "$$RC" = 1 -a "$(STOP)" = 1 && break; \
        done; \
        test "$$RC" -ne 1
 
@@ -141,29 +169,29 @@ valgrind_%: %.exe
 %.d: %.c
        $(call TITLE, "Building $@")
        $(CC) $(INCLUDES) -MM $< -o $@~
-       echo ${<:.c=.o}: $(shell perl -- getcomments.pl -p='depend:\s' -f='%' $<) >> $@~
-       mv $@~ $@
+       echo ${<:.c=.o}: $(shell $(call GETCOMMENTS,depends, $<)) >> $@~
+       $(MV) $@~ $@
        $(call PASS, SUCCESS)
 
 %.ld: %.c
        $(call TITLE, "Building $@")
-       echo ${<:.c=.exe}: $(shell perl -- getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@
+       echo ${<:.c=.exe}: $(shell $(call GETCOMMENTS,$(LDOPT), $<) | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@
        $(call PASS, SUCCESS)
 
 %.o: %.c
        $(call TITLE, "Building $@")
-       $(CC) $(CFLAGS) $(INCLUDES) $(shell perl -- getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@
+       $(CC) $(CFLAGS) $(INCLUDES) $(shell $(call GETCOMMENTS,cflags, $<)) -c $< -o $@
        $(call PASS, SUCCESS)
 
 
 %.exe: %.o %.d
        $(call TITLE, "Building $@")
-       $(CC) $(LDFLAGS) $< $(shell perl -- getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $(LDLIBS) -o $@
+       $(CC) $(LDFLAGS) $< $(shell $(call GETCOMMENTS,$(LDOPT), ${<:.o=.c})) $(LDLIBS) -o $@
        $(call PASS, SUCCESS)
 
 ## Phony
 
-.PHONY: all clean count depends gcovs purge tests
+.PHONY: all analyze clean count depends gcovs purge tests
 
 ## Precious