Implemented basic command-line options
This commit is contained in:
parent
197445de70
commit
92f4057583
3 changed files with 76 additions and 12 deletions
58
connect4.c
58
connect4.c
|
|
@ -17,6 +17,7 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
@ -24,25 +25,50 @@
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "ui.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[] ){
|
int main( int argc, char *argv[] ){
|
||||||
// Parse options
|
// Parse options
|
||||||
for(;;){
|
for(;;){
|
||||||
|
// Allowed options
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
{ "help", no_argument, NULL, 'h' },
|
{ "help", no_argument, NULL, 'h' },
|
||||||
{ "license", no_argument, NULL, 'l' },
|
{ "license", no_argument, NULL, 'l' },
|
||||||
{ "version", no_argument, NULL, '\0' },
|
{ "version", no_argument, NULL, '\0' },
|
||||||
{ 0 }
|
{ 0 }
|
||||||
};
|
};
|
||||||
|
// Index of current option
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
// Get next option
|
||||||
char c = getopt_long( argc, argv, "hl", long_options, &option_index );
|
char c = getopt_long( argc, argv, "hl", long_options, &option_index );
|
||||||
|
|
||||||
if( c == -1 ) break;
|
if( c == -1 ) break;
|
||||||
|
// Handle option
|
||||||
switch( c ){
|
switch( c ){
|
||||||
case 'h':
|
case '\0': // Any option without shorthand
|
||||||
printf( "Usage: connect4 [OPTIONS]\n\n" );
|
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++ ){
|
for( int i = 0; long_options[i].name != NULL; i++ ){
|
||||||
if( long_options[i].val != '\0' )
|
if( long_options[i].val != '\0' )
|
||||||
printf( " -%c --", long_options[i].val );
|
printf( " -%c --", long_options[i].val );
|
||||||
|
|
@ -51,13 +77,30 @@ int main( int argc, char *argv[] ){
|
||||||
putchar( '\n' );
|
putchar( '\n' );
|
||||||
}
|
}
|
||||||
return 0;
|
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(
|
printf(
|
||||||
"AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n"
|
VERSIONSTRING
|
||||||
"AnnaConnect comes with ABSOLUTELY NO WARRANTY\n"
|
"AnnaConnect comes with ABSOLUTELY NO WARRANTY\n"
|
||||||
"This is free software, and you are welcome to redistribute it\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?
|
// board, innit?
|
||||||
initBoard();
|
initBoard();
|
||||||
|
|
@ -84,6 +127,7 @@ int main( int argc, char *argv[] ){
|
||||||
.win0 = {0},
|
.win0 = {0},
|
||||||
.win1 = {0},
|
.win1 = {0},
|
||||||
};
|
};
|
||||||
|
// Begin loopin
|
||||||
for(;; playboard.player = !playboard.player ){
|
for(;; playboard.player = !playboard.player ){
|
||||||
move_t move = askMove();
|
move_t move = askMove();
|
||||||
int column = askColumn( playboard, move );
|
int column = askColumn( playboard, move );
|
||||||
|
|
|
||||||
14
makefile
14
makefile
|
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/make -f
|
#!/usr/bin/make -f
|
||||||
FLAGS = -O3
|
FLAGS = -std=gnu23
|
||||||
|
FLAGS += -O3
|
||||||
FLAGS += -pedantic
|
FLAGS += -pedantic
|
||||||
FLAGS += -Wall
|
FLAGS += -Wall
|
||||||
FLAGS += -Wextra
|
FLAGS += -Wextra
|
||||||
|
|
@ -8,8 +9,6 @@ FLAGS += -Werror
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
OUTDIR = out
|
OUTDIR = out
|
||||||
|
|
||||||
OBJ = $(SRC:%.c=$(OBJDIR)/%.o)
|
|
||||||
|
|
||||||
#UI_TARGET = vt100
|
#UI_TARGET = vt100
|
||||||
UI_TARGET = ncurses
|
UI_TARGET = ncurses
|
||||||
|
|
||||||
|
|
@ -31,16 +30,21 @@ run: $(OUTDIR)/connect4_$(UI_TARGET).elf
|
||||||
run_%: $(OUTDIR)/connect4_%.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) $@
|
@echo $(MSG_LINKING) $@
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
$(CC) $(FLAGS) -lncursesw -o $@ $^
|
$(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) $@
|
@echo $(MSG_LINKING) $@
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
$(CC) $(FLAGS) -o $@ $^
|
$(CC) $(FLAGS) -o $@ $^
|
||||||
|
|
||||||
|
$(OBJDIR)/LICENSE.o: LICENSE
|
||||||
|
@echo $(MSG_LINKING) $@
|
||||||
|
@mkdir -p $(@D)
|
||||||
|
ld -r -b binary -o $@ $^
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.c
|
$(OBJDIR)/%.o: %.c
|
||||||
@echo $(MSG_COMPILING) $^
|
@echo $(MSG_COMPILING) $^
|
||||||
@mkdir -p $(@D)
|
@mkdir -p $(@D)
|
||||||
|
|
|
||||||
16
test
Normal file
16
test
Normal file
|
|
@ -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 [5C[9A┌───────────────┬────┬────┐[B[27D│ player │ 0 │ 1 │[B[27D├───────────────┼────┼────┤[B[27D│ vertical │ │ │[B[27D│ horizontal │ │ │[B[27D│ diagonal up │ │ │[B[27D│ diagonal down │ │ │[B[27D├───────────────┼────┼────┤[B[27D│ total │ │ │[B[27D└───────────────┴────┴────┘[B[27D
|
||||||
|
|
||||||
|
Wher player 0 put? [K
|
||||||
Loading…
Add table
Add a link
Reference in a new issue