From: Laurent Mazet Date: Tue, 15 Apr 2025 22:31:05 +0000 (+0200) Subject: full adaptation to c99 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=8cd1f7d5440314459a03c7b0a260fd8e9a3852a7;p=cmore.git full adaptation to c99 --- diff --git a/cmd.c b/cmd.c index c2dd0e9..39921c6 100644 --- a/cmd.c +++ b/cmd.c @@ -35,14 +35,17 @@ char *exec_cmd (char *cmd) VERBOSE (DEBUG, fprintf (stdout, "exec command: %s\n", cmd)); int saved_stderr = dup (STDERR_FILENO); - close (STDERR_FILENO); - FILE *fp = popen (cmd, "r"); - if (fp != NULL) { - buffer = _read_stream (fp); - status = pclose(fp); + if (saved_stderr == -1) { + VERBOSE (ERROR, fprintf (stderr, "error on dup\n")); + } else { + close (STDERR_FILENO); + FILE *fp = popen (cmd, "r"); + if (fp != NULL) { + buffer = _read_stream (fp); + status = pclose(fp); + } + dup2 (saved_stderr, STDERR_FILENO); } - dup2 (saved_stderr, STDERR_FILENO); - if ((status == -1) || (buffer == NULL)) { free (buffer); buffer = NULL; @@ -84,11 +87,9 @@ char *read_stdin (void) char **split_lines (char *buffer, int max) { int n = 0; - char **lines = NULL; - int i, j; char *line = (char *) calloc (max + 1, sizeof (char)); - for (i = j = 0; buffer[i] != '\0'; i++) { + for (int i = 0, j = 0; buffer[i] != '\0'; i++) { if (buffer[i] >= ' ') { line[j++] = buffer[i]; } diff --git a/cmore.c b/cmore.c index a8ac2bf..3e4472f 100644 --- a/cmore.c +++ b/cmore.c @@ -146,7 +146,8 @@ void process (void) free_lines (lines); lines = split_lines (buffer, xmax); - free (buffer); + if (buffer != (char *)-1) + free (buffer); display (lines, xmax, ymax, 0); if (second > 0) { @@ -177,10 +178,9 @@ void about (void) wmove (stdscr, ymax * (1 - ratio) / 2, xmax * (1 - ratio) / 2); WINDOW *whelp = winputbox (stdscr, ymax * ratio, xmax * ratio); - int i, j; - for (i = 1; i < ymax * ratio - 1; i++) { + for (int i = 1; i < ymax * ratio - 1; i++) { char c[2] = {0}; - for (j = 1; j < xmax * ratio - 1; j++) { + for (int j = 1; j < xmax * ratio - 1; j++) { *c = *message++; if ((*c == '\n') && (j == 1)) { *c = *message++; diff --git a/getcomments.pl b/getcomments.pl deleted file mode 100644 index 47edf67..0000000 --- a/getcomments.pl +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/perl - -use strict; - -# default value -my $format = "%"; -my $pattern = ""; - -# help message -sub usage() { - - print <) { - 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"; - } - } -} diff --git a/makefile b/makefile index 1624fa5..09ecb8b 100644 --- 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,19 +14,30 @@ OFLAGS = -O4 -Os CFLAGS += -W -Wall -Wextra -g #CFLAGS += -std=c99 -D_XOPEN_SOURCE=500 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS) -LDFLAGS += -g $(OPTIONS) +LDFLAGS += -g $(LDOPTS) $(OPTIONS) LDOPT = linker -ifeq ($(OS),Windows_NT) +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 += cmore +ALLEXE = $(shell for f in *.c; do grep -q '/\*\slinker:' $$f && echo $${f/.c}; done) SHELL = bash @@ -46,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)) @@ -74,7 +94,8 @@ purge: clean rm -f purge $(ALLEXE:%=%.exe) $(call PASS, SUCCESS) -valgrinds: all +valgrinds: + $(MAKE) all $(MAKE) $(addprefix valgrind_,$(ALLEXE)) wipe: purge @@ -83,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 @@ -93,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 @@ -122,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; \ @@ -133,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 @@ -144,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='$(LDOPT):\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='$(LDOPT):\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 diff --git a/tui.c b/tui.c index 795da65..367187f 100644 --- a/tui.c +++ b/tui.c @@ -80,14 +80,12 @@ static char *padstr(char *s, int length) static char *prepad(char *s, int length) { - int i; char *p = s; - if (length > 0) - { + if (length > 0) { memmove((void *)(s + length), (const void *)s, strlen(s) + 1); - for (i = 0; i < length; i++) + for (int i = 0; i < length; i++) *p++ = ' '; } @@ -184,10 +182,13 @@ static void idle(void) static void menudim(menu *mp, int *lines, int *columns) { - int n, l, mmax = 0; + int n, mmax = 0; - for (n = 0; mp->func; n++, mp++) - if ((l = strlen(mp->name)) > mmax) mmax = l; + for (n = 0; mp->func; n++, mp++) { + int l; + if ((l = strlen(mp->name)) > mmax) + mmax = l; + } *lines = n; *columns = mmax + 2; @@ -218,10 +219,9 @@ static int hotkey(const char *s) static void repaintmenu(WINDOW *wmenu, menu *mp) { - int i; menu *p = mp; - for (i = 0; p->func; i++, p++) + for (int i = 0; p->func; i++, p++) mvwaddstr(wmenu, i + 1, 2, p->name); touchwin(wmenu);