diff --git a/connect4.c b/connect4.c index e0b459f..a43e347 100644 --- a/connect4.c +++ b/connect4.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ #include +#include #include #include "macros.h" #include "config.h" @@ -24,25 +25,50 @@ #include "types.h" #include "ui.h" -#define VERSION 0.0.4 +#define VERSION 0.0.5 + +#define VERSIONSTRING \ + "AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n" + +// The LISENCE file +// Not null-terminated so need pointers for both start and end +extern char _binary_LICENSE_start[]; +extern char _binary_LICENSE_end[]; int main( int argc, char *argv[] ){ // Parse options for(;;){ + // Allowed options static struct option long_options[] = { { "help", no_argument, NULL, 'h' }, { "license", no_argument, NULL, 'l' }, { "version", no_argument, NULL, '\0' }, { 0 } }; + // Index of current option int option_index = 0; + // Get next option char c = getopt_long( argc, argv, "hl", long_options, &option_index ); - if( c == -1 ) break; - + // Handle option switch( c ){ - case 'h': - printf( "Usage: connect4 [OPTIONS]\n\n" ); + case '\0': // Any option without shorthand + switch( option_index ){ + case 2: // --version + printf( VERSIONSTRING ); + return 0; + default: + fprintf( + stderr, + "%s: unhandled option \'--%s\' \n", + argv[0], + long_options[ option_index ].name + ); + break; + } + break; + case 'h': // --help + printf( "Usage: %s [OPTIONS]\n\n", argv[0] ); for( int i = 0; long_options[i].name != NULL; i++ ){ if( long_options[i].val != '\0' ) printf( " -%c --", long_options[i].val ); @@ -51,13 +77,30 @@ int main( int argc, char *argv[] ){ putchar( '\n' ); } return 0; + case 'l': // --license + for( + char *p = _binary_LICENSE_start; + p != _binary_LICENSE_end; + p++ + ){ + putchar( *p ); + } + return 0; } } + // Check for unhandled options + if( optind < argc ) { + fprintf( stderr, "non-option ARGV-elements: " ); + while( optind < argc ) + printf( "%s ", argv[optind++] ); + putchar( '\n' ); + } + // Start the actual program printf( - "AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n" + VERSIONSTRING "AnnaConnect comes with ABSOLUTELY NO WARRANTY\n" "This is free software, and you are welcome to redistribute it\n" - "under certain condtions\n" + "under certain condtions. See `%s --license`\n", argv[0] ); // board, innit? initBoard(); @@ -84,6 +127,7 @@ int main( int argc, char *argv[] ){ .win0 = {0}, .win1 = {0}, }; + // Begin loopin for(;; playboard.player = !playboard.player ){ move_t move = askMove(); int column = askColumn( playboard, move ); diff --git a/makefile b/makefile index 8ea8522..dc4c6bc 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,6 @@ #!/usr/bin/make -f -FLAGS = -O3 +FLAGS = -std=gnu23 +FLAGS += -O3 FLAGS += -pedantic FLAGS += -Wall FLAGS += -Wextra @@ -8,8 +9,6 @@ FLAGS += -Werror OBJDIR = obj OUTDIR = out -OBJ = $(SRC:%.c=$(OBJDIR)/%.o) - #UI_TARGET = vt100 UI_TARGET = ncurses @@ -31,16 +30,21 @@ run: $(OUTDIR)/connect4_$(UI_TARGET).elf run_%: $(OUTDIR)/connect4_%.elf ./$< -$(OUTDIR)/connect4_ncurses.elf: $(addprefix $(OBJDIR)/,ui_ncurses.o logic.o connect4.o) +$(OUTDIR)/connect4_ncurses.elf: $(addprefix $(OBJDIR)/,ui_ncurses.o logic.o connect4.o LICENSE.o) @echo $(MSG_LINKING) $@ @mkdir -p $(@D) $(CC) $(FLAGS) -lncursesw -o $@ $^ -$(OUTDIR)/connect4_%.elf: $(addprefix $(OBJDIR)/,ui_%.o logic.o connect4.o) +$(OUTDIR)/connect4_%.elf: $(addprefix $(OBJDIR)/,ui_%.o logic.o connect4.o LICENSE.o) @echo $(MSG_LINKING) $@ @mkdir -p $(@D) $(CC) $(FLAGS) -o $@ $^ +$(OBJDIR)/LICENSE.o: LICENSE + @echo $(MSG_LINKING) $@ + @mkdir -p $(@D) + ld -r -b binary -o $@ $^ + $(OBJDIR)/%.o: %.c @echo $(MSG_COMPILING) $^ @mkdir -p $(@D) diff --git a/test b/test new file mode 100644 index 0000000..0198e15 --- /dev/null +++ b/test @@ -0,0 +1,16 @@ +AnnaConnect version 0.0.4, Copyright (C) Anna Snoeijs +AnnaConnect comes with ABSOLUTELY NO WARRANTY +This is free software, and you are welcome to redistribute it +under certain condtions. See `./out/connect4_vt100.elf --license` + + + 1 2 3 4 5 6 7 + 6 [ ][ ][ ][ ][ ][ ][ ] 6 + 5 [ ][ ][ ][ ][ ][ ][ ] 5 + 4 [ ][ ][ ][ ][ ][ ][ ] 4 + 3 [ ][ ][ ][ ][ ][ ][ ] 3 + 2 [ ][ ][ ][ ][ ][ ][ ] 2 + 1 [ ][ ][ ][ ][ ][ ][ ] 1 + 1 2 3 4 5 6 7 ┌───────────────┬────┬────┐│ player │ 0 │ 1 │├───────────────┼────┼────┤│ vertical │ │ ││ horizontal │ │ ││ diagonal up │ │ ││ diagonal down │ │ │├───────────────┼────┼────┤│ total │ │ │└───────────────┴────┴────┘ + +Wher player 0 put?  \ No newline at end of file