initial commit
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 16 Dec 2024 09:59:04 +0000 (10:59 +0100)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 16 Dec 2024 09:59:04 +0000 (10:59 +0100)
.gitignore [new file with mode: 0644]
debug.c [new file with mode: 0644]
debug.h [new file with mode: 0644]
makefile [new file with mode: 0644]
sokoban.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..68f55a9
--- /dev/null
@@ -0,0 +1,8 @@
+*.d
+*.exe
+*.gcda
+*.gcno
+*.gcov
+*.ld
+*.o
+*.log
diff --git a/debug.c b/debug.c
new file mode 100644 (file)
index 0000000..b9eba44
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,5 @@
+#include "debug.h"
+
+int verbose = 0;
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/debug.h b/debug.h
new file mode 100644 (file)
index 0000000..32dc0ca
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,21 @@
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+/* constants */
+
+#define DEBUG 3
+#define INFO 2
+#define WARNING 1
+#define ERROR 0
+
+/* macros */
+
+#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
+
+/* gobal variables */
+
+extern int verbose;
+
+#endif /* __DEBUG_H__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/makefile b/makefile
new file mode 100644 (file)
index 0000000..55371bd
--- /dev/null
+++ b/makefile
@@ -0,0 +1,190 @@
+# Default flags
+
+CC = gcc
+
+#INCLUDES = -I../debug -D__MEMORY_ALLOCATION__
+INCLUDES =
+OFLAGS  = -O4 -Os
+#OFLAGS  = -O4 -ffast-math -finline-functions
+#OFLAGS  = -O4 -finline-functions
+#OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow
+#OFLAGS += -minline-all-stringops -fsingle-precision-constant
+#OFLAGS += -malign-double
+CFLAGS += -W -Wall -Wextra -g
+#CFLAGS += -std=c99 -D_XOPEN_SOURCE=500
+CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS)
+LDFLAGS += -g $(LDOPTS) $(OPTIONS)
+
+LDOPT = linker
+MV = mv
+ifneq (, $(findstring linux, $(MAKE_HOST)))
+# Linux
+else ifneq (, $(findstring mingw, $(MAKE_HOST)))
+# Windows MinGw
+#LDLIBS += -lws2_32
+LDOPT = winlnk
+else ifneq (, $(findstring cygwin, $(MAKE_HOST)))
+# Windows CygWin
+LDOPT = winlnk
+else ifneq (, $(findstring msdos, $(MAKE_HOST)))
+# MSDOS
+LDOPT = doslnk
+MV = move
+endif
+
+# Targets
+
+ALLEXE  =
+ALLEXE += sokoban
+
+SHELL = bash
+
+#MAKE = mingw32-make
+MAKEFLAGS += -s
+include $(wildcard .makefile)
+
+# Functions
+
+TITLE = echo -en "\033[0;1m$(strip $(1))\033[0;0m:\t"
+PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m"
+WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m"
+FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m"
+
+MKDIR = mkdir -p $(1) && chmod a+rx,go-w $(1)
+
+INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1) $(2) && chmod a+rX,go-w $(2)
+
+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)
+
+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 _)
+       $(call PASS, SUCCESS)
+
+depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE))
+
+gcovs:
+       $(MAKE) $(addprefix gcov_,$(ALLEXE))
+
+gprofs:
+       $(MAKE) $(addprefix gprof_,$(ALLEXE))
+
+purge: clean
+       $(call TITLE, "Purging")
+       touch purge
+       rm -f purge $(ALLEXE:%=%.exe)
+       $(call PASS, SUCCESS)
+
+valgrinds:
+       $(MAKE) all
+       $(MAKE) $(addprefix valgrind_,$(ALLEXE))
+
+wipe: purge
+       $(call TITLE, "Wiping")
+       touch wipe
+       rm -f wipe $(wildcard *.gcda *.gcno *.gcov *.glog)
+       $(call PASS, SUCCESS)
+
+tests:
+       -rm -f $(ALLEXE)
+       $(MAKE) all
+       $(MAKE) $(addprefix test_,$(ALLEXE))
+
+## Main rules
+
+include $(wildcard *.d)
+include $(wildcard *.ld)
+
+gcov_%:
+       $(MAKE) purge
+       $(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
+
+gprof_%:
+       $(MAKE) purge
+       $(MAKE) depends
+       OPTIONS="-pg" $(MAKE) ${@:gprof_%=%}.exe
+       $(MAKE) ${@:gprof_%=%}.test
+       IFS=$$'\n'; id=1; \
+               for test in `cat ${@:gprof_%=%}.test | sed 's,${@:gprof_%=%}.exe,./${@:gprof_%=%}.exe,g'`; do \
+               log=${@:gprof_%=%}.prof-$$id.glog; \
+               $(call TITLE, test: $$test); \
+               echo $$test > $$log; \
+               eval $$test >> $$log; \
+               [ $$? -eq 0 ] \
+                       && echo -e "\033[1;32mSUCCESS\033[0;0m" \
+                       || echo -e "\033[1;31mFAILED\033[0;0m"; \
+               [ -f gmon.out ] && { gprof ${@:gprof_%=%}.exe gmon.out >> $$log; rm gmon.out; }; \
+               let id++; \
+       done;
+       $(MAKE) purge
+
+%.test: %.c
+       $(call TITLE, "Building $@")
+       $(call GETCOMMENTS,test, $<) > $@
+       $(call PASS, SUCCESS)
+       -rm -f _
+
+test_%: %.test %.exe
+       IFS=$$'\n'; RC=0; \
+       for test in `cat $< | sed 's,${<:.test=.exe},$(VALGRIND) ./${<:.test=.exe},g'`; do \
+         echo "=== $$test ==="; \
+         eval $$test; \
+         [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \
+                       || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \
+       done; \
+       test "$$RC" -ne 1
+
+valgrind_%: %.exe
+       VALGRIND="valgrind -v --leak-check=full --log-fd=3"; \
+       export VALGRIND; \
+       $(MAKE) $(@:valgrind_%=test_%) 3>$@.log
+
+%.d: %.c
+       $(call TITLE, "Building $@")
+       $(CC) $(INCLUDES) -MM $< -o $@~
+       echo ${<:.c=.o}: $(shell $(call GETCOMMENTS,depends, $<)) >> $@~
+       $(MV) $@~ $@
+       $(call PASS, SUCCESS)
+
+%.ld: %.c
+       $(call TITLE, "Building $@")
+       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 $(call GETCOMMENTS,cflags, $<)) -c $< -o $@
+       $(call PASS, SUCCESS)
+
+
+%.exe: %.o %.d
+       $(call TITLE, "Building $@")
+       $(CC) $(LDFLAGS) $< $(shell $(call GETCOMMENTS,$(LDOPT), ${<:.o=.c})) $(LDLIBS) -o $@
+       $(call PASS, SUCCESS)
+
+## Phony
+
+.PHONY: all clean count depends gcovs purge tests
+
+## Precious
+
+.PRECIOUS: %.d %.o
diff --git a/sokoban.c b/sokoban.c
new file mode 100644 (file)
index 0000000..5756498
--- /dev/null
+++ b/sokoban.c
@@ -0,0 +1,114 @@
+/* depend: */
+/* cflags: */
+/* linker: block.o color.c constant.o debug.o display.o function.o time.o -lcurses */
+/* doslnk: block.o color.c constant.o debug.o display.o function.o time.o -lpdc~1 */
+/* winlnk: block.o color.c constant.o debug.o display.o function.o time.o -lpdcurses */
+
+#include <curses.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+//#include "constant.h"
+#include "debug.h"
+//#include "time.h"
+
+/* static variables */
+char *progname = NULL;
+char *version = "0.1";
+
+char *filename = NULL;
+char *levels = "level.skb";
+int scale = 1;
+int wide = 0;
+
+int usage (int ret)
+{
+    FILE *fd = ret ? stderr : stdout;
+    fprintf (fd, "usage: %s [-c] [-f file] [-h] [-l file] [-v int]\n", progname);
+    fprintf (fd, " -f: file name (%s)\n", (filename) ? filename : "none");
+    fprintf (fd, " -h: help message\n");
+    fprintf (fd, " -l: level library (%s)\n", levels);
+    fprintf (fd, " -s: scale [0..3] (%d)\n", scale);
+    fprintf (fd, " -v: verbose level (%d)\n", verbose);
+    fprintf (fd, " -w: wide board (%d)\n", wide);
+    fprintf (fd, "%s version %s\n", progname, version);
+
+    return ret;
+}
+
+/* main function */
+int main (int argc, char *argv[])
+{
+
+    /* get basename */
+    char *pt = progname = argv[0];
+    while (*pt) {
+        if ((*pt == '/') || (*pt == '\\')) {
+           progname = pt + 1;
+        }
+        pt++;
+    }
+
+    /* process argument */
+    while (argc-- > 1) {
+        char *arg = *(++argv);
+        if (arg[0] != '-') {
+            VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- %s\n", progname, arg));
+            return usage (1);
+        }
+        char c = arg[1];
+        switch (c) {
+        case 'f':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: no file specified\n", progname));
+                return usage (1);
+            }
+            filename = arg;
+            break;
+        case 'l':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: no file specified\n", progname));
+                return usage (1);
+            }
+            levels = arg;
+            break;
+        case 'v':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname));
+                return usage (1);
+            }
+            verbose = atoi (arg);
+            break;
+        case 'w':
+            wide = 1;
+            break;
+        case 'h':
+        default:
+            return usage (c != 'h');
+        }
+    }
+
+    /* check */
+    if ((scale < 0) || (scale > 3)) {
+        VERBOSE (ERROR, fprintf (stderr, "incorrect scale (%d)\n", scale));
+        return 1;
+    }
+
+    int ret = -1;
+
+    return ret;
+}
+
+/* test: sokoban.exe -f 2>&1 | grep 'no file' */
+/* test: sokoban.exe -f nofile.sok 2>&1 | grep "can't read file" */
+/* test: sokoban.exe -f bogus.sok 2>&1 | grep 'incorrect file' */
+/* test: sokoban.exe -h | grep usage */
+/* test: sokoban.exe -l nofile.skb 2>&1 | grep "can't read library" */
+/* test: sokoban.exe -l bogus.skb 2>&1 | grep 'incorrect library' */
+/* test: sokoban.exe -v 2>&1 | grep missing */
+/* test: sokoban.exe _ 2>&1 | grep invalid */
+
+/* vim: set ts=4 sw=4 et: */