Tue, 08 Sep 2026 22:22:01 +0200
some cleanup and simplifications
| src/Makefile | file | annotate | diff | comparison | revisions | |
| src/chess/fen.c | file | annotate | diff | comparison | revisions | |
| src/colors.c | file | annotate | diff | comparison | revisions | |
| src/colors.h | file | annotate | diff | comparison | revisions | |
| src/input.c | file | annotate | diff | comparison | revisions | |
| src/input.h | file | annotate | diff | comparison | revisions | |
| src/main.c | file | annotate | diff | comparison | revisions |
--- a/src/Makefile Tue Sep 08 14:34:48 2026 +0200 +++ b/src/Makefile Tue Sep 08 22:22:01 2026 +0200 @@ -28,7 +28,7 @@ include ../config.mk -SRC = main.c colors.c network.c input.c +SRC = main.c network.c OBJ = $(SRC:%.c=$(BUILDDIR)/%.o) all: $(BUILDDIR)/terminal-chess FORCE @@ -41,16 +41,8 @@ FORCE: -$(BUILDDIR)/colors.o: colors.c colors.h - @echo "Compiling $<" - $(CC) -o $@ $(CFLAGS) -c $< - -$(BUILDDIR)/input.o: input.c input.h - @echo "Compiling $<" - $(CC) -o $@ $(CFLAGS) -c $< - $(BUILDDIR)/main.o: main.c chess/rules.h chess/pgn.h chess/rules.h \ - input.h network.h colors.h + network.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $<
--- a/src/chess/fen.c Tue Sep 08 14:34:48 2026 +0200 +++ b/src/chess/fen.c Tue Sep 08 22:22:01 2026 +0200 @@ -57,6 +57,7 @@ case BKNIGHT: str[i++] = 'n'; break; case BROOK: str[i++] = 'r'; break; case BPAWN: str[i++] = 'p'; break; + case NOPIECE: /* unreachable */ break; } } else { skip++;
--- a/src/colors.c Tue Sep 08 14:34:48 2026 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2016 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include "colors.h" -#include <curses.h> - -void init_colorpairs(void) { -#ifndef USE_POSIX_CURSES - use_default_colors(); - init_pair(COL_APP, -1, -1); -#endif - init_pair(COL_BW, COLOR_BLACK, COLOR_CYAN); - init_pair(COL_BB, COLOR_BLACK, COLOR_BLUE); - init_pair(COL_WB, COLOR_WHITE, COLOR_BLUE); - init_pair(COL_WW, COLOR_WHITE, COLOR_CYAN); -} -
--- a/src/colors.h Tue Sep 08 14:34:48 2026 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2016 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - -#ifndef COLORS_H -#define COLORS_H - -#ifdef USE_POSIX_CURSES -#define COL_APP 0 -#else -#define COL_APP 1 -#endif -#define COL_BW 2 -#define COL_WB 3 -#define COL_BB 4 -#define COL_WW 5 - -void init_colorpairs(void); - -#endif /* COLORS_H */ -
--- a/src/input.c Tue Sep 08 14:34:48 2026 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2016 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include "input.h" -#include <curses.h> -#include <ctype.h> - -int prompt_yesno(const char *msg) { - addstr(msg); - addstr(" (y/n)? "); - clrtoeol(); - refresh(); - noecho(); - int ch; - do { - ch = getch(); - } while (ch != 'y' && ch != 'n'); - echo(); - - return ch == 'y'; -} - -int asyncgetnstr(char *str, size_t *pos, size_t len) { - WINDOW* w = stdscr; - int y, x; - getyx(w, y, x); - mvwaddnstr(w, y, x, str, *pos); - wrefresh(w); - int c = wgetch(w); - - if (c != ERR) { - switch (c) { - case KEY_DOWN: - case '\n': - str[*pos] = '\0'; - *pos = 0; - waddch(w,'\n'); - return 1; - case 127: /* ASCII DEL (important for macOS) */ - case KEY_DC: /* DEL key (for other OS) */ - case KEY_BACKSPACE: - case KEY_LEFT: - if ((*pos) > 0) { - str[--(*pos)] = '\0'; - } - break; - default: - if (isprint(c) && *pos < len-1) { - str[(*pos)++] = (char) c; - } - } - } - - return 0; -} - -int input_getmaxy(void) { - /* POSIX-curses-compliant wrapper for getmaxy() */ - int x, y; - getmaxyx(stdscr, y, x); - (void)x; - return y; -}
--- a/src/input.h Tue Sep 08 14:34:48 2026 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2016 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - -#ifndef INPUT_H -#define INPUT_H - -#include <stdlib.h> - -int prompt_yesno(const char *msg); - -/** - * Asynchronous variant of getnstr(). - * - * Needs halfdelay mode enabled! - * - * @param str the buffer for the read string - * @param pos a pointer to the object containing the current buffer position - * @param len the length of the buffer - * @return 0 if reading is in progress and 1 when a complete line is read - */ -int asyncgetnstr(char *str, size_t *pos, size_t len); - -int input_getmaxy(void); - -#endif /* INPUT_H */ -
--- a/src/main.c Tue Sep 08 14:34:48 2026 +0200 +++ b/src/main.c Tue Sep 08 22:22:01 2026 +0200 @@ -33,6 +33,7 @@ #include <string.h> #include <time.h> #include <locale.h> +#include <ctype.h> #include <sys/stat.h> #include <signal.h> #include <errno.h> @@ -41,9 +42,7 @@ #include "chess/rules.h" #include "chess/pgn.h" -#include "input.h" #include "network.h" -#include "colors.h" typedef struct { /** @@ -250,6 +249,84 @@ return 0; } +#ifdef USE_POSIX_CURSES +#define COL_APP 0 +#else +#define COL_APP 1 +#endif +#define COL_BW 2 +#define COL_WB 3 +#define COL_BB 4 +#define COL_WW 5 + +static void init_colorpairs(void) { +#ifndef USE_POSIX_CURSES + use_default_colors(); + init_pair(COL_APP, -1, -1); +#endif + init_pair(COL_BW, COLOR_BLACK, COLOR_CYAN); + init_pair(COL_BB, COLOR_BLACK, COLOR_BLUE); + init_pair(COL_WB, COLOR_WHITE, COLOR_BLUE); + init_pair(COL_WW, COLOR_WHITE, COLOR_CYAN); +} + +static int prompt_yesno(const char *msg) { + addstr(msg); + addstr(" (y/n)? "); + clrtoeol(); + refresh(); + noecho(); + int ch; + do { + ch = getch(); + } while (ch != 'y' && ch != 'n'); + echo(); + + return ch == 'y'; +} + +static int asyncgetnstr(char *str, size_t *pos, size_t len) { + WINDOW* w = stdscr; + int y, x; + getyx(w, y, x); + mvwaddnstr(w, y, x, str, *pos); + wrefresh(w); + int c = wgetch(w); + + if (c != ERR) { + switch (c) { + case KEY_DOWN: + case '\n': + str[*pos] = '\0'; + *pos = 0; + waddch(w,'\n'); + return 1; + case 127: /* ASCII DEL (important for macOS) */ + case KEY_DC: /* DEL key (for other OS) */ + case KEY_BACKSPACE: + case KEY_LEFT: + if ((*pos) > 0) { + str[--(*pos)] = '\0'; + } + break; + default: + if (isprint(c) && *pos < len-1) { + str[(*pos)++] = (char) c; + } + } + } + + return 0; +} + +static int input_getmaxy(void) { + /* POSIX-curses-compliant wrapper for getmaxy() */ + int x, y; + getmaxyx(stdscr, y, x); + (void)x; + return y; +} + static const uint8_t boardx = 4, boardy = 10; static int inputy = 21; /* should be overridden on game startup */