summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 27e750e727bb86fe94329e89300559530d0ce85b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*****************************************************************************
 *                          ___ __  __ ___ _ __                              *
 *                         / _ \\ \/ // _ \ '_ \                             *
 *                        | (_) |>  <|  __/ | | |                            *
 *                         \___//_/\_\\___|_| |_|                            *
 *                                                                           *
 *                             The card game                                 *
 *                                                                           *
 *  Copyright (C) 2011, Reiner Herrmann <reiner@reiner-h.de>                 *
 *                      Mario Kilies <MarioKilies@GMX.net>                   *
 *                                                                           *
 *  This program is free software; you can redistribute it and/or modify     *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation; either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
 *                                                                           *
 *  This program is distributed in the hope that it will be useful,          *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
 *  GNU General Public License for more details.                             *
 *                                                                           *
 *  You should have received a copy of the GNU General Public License        *
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.     *
 *                                                                           *
 *****************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include "data_store.h"
#include "game.h"
#include "manual.h"

#define DEFAULT_PORT "12345"

#define OXEN_MAJOR	0
#define OXEN_MINOR	1
#define OXEN_PATCH	0

/**
 * Prints version number.
 */
static void print_version(void)
{
	printf("You are running oxen %d.%d.%d\n", OXEN_MAJOR, OXEN_MINOR, OXEN_PATCH);
}

/**
 * Print usage information.
 */
static void print_usage(void)
{
	const char* usage = "Usage: oxen [-u username] [-s address] [-l] [-p port]\n"
		            "            [-n num_players] [-m] [-v]\n"
	                    "  -s address       client: hostname/address to connect to (required)\n"
			    "                   server: address to listen on (optional)\n"
			    "  -l               start server\n"
			    "  -n num_players   number of players; only on server (default: 2, max: 10)\n"
			    "  -u username      your nickname in the player list (default: $USER)\n"
			    "  -p port          port to use for connecting/listening (default: %s)\n"
			    "  -m               display the manual\n"
			    "  -v               print version number\n";

	print_version();
	fprintf(stderr, usage, DEFAULT_PORT);
	exit(EXIT_FAILURE);
}

/**
 * The application's entry point.
 * @param[in] argc The number of arguments passed
 * @param[in] argv The array of passed arguments
*/
int main(int argc, char *argv[])
{
	int opt;
	uint8_t num_players = 2;
	char* port = NULL;
	char* addr = NULL;
	bool servermode = false;
	data_store_t *ds = data_store();

	const char* accepted = "u:s:p:n:hlmv";
	while((opt = getopt(argc, argv, accepted)) != -1)
	{
		switch(opt)
		{
			case 'u': // nickname
				strncpy(ds->nickname, optarg, MAX_PLAYER_NAME_LENGTH);
				ds->nickname[MAX_PLAYER_NAME_LENGTH] = '\0';
				break;
			case 'p': // port
				port = optarg;
				break;
			case 's': // hostname
				addr = optarg;
				break;
			case 'n': // number of users
				num_players = atoi(optarg);
				break;
			case 'l':
				servermode = true;
				break;
			case 'm':
				print_manual();
				break;
			case 'v':
				print_version();
				exit(EXIT_SUCCESS);
				break;
			case 'h': // help
			default:
				print_usage();
				break;
		}
	}

	if(!servermode && addr == NULL)
		print_usage();

	if(port == NULL)
		port = DEFAULT_PORT;

	if(strlen(ds->nickname) == 0)
	{
		const char* env_nick = getenv("USER");
		strncpy(ds->nickname, (env_nick!=NULL)?env_nick:"hornoxe", MAX_PLAYER_NAME_LENGTH);
		ds->nickname[MAX_PLAYER_NAME_LENGTH] = '\0';
	}

	start_game(servermode, addr, port, num_players);

	exit(EXIT_SUCCESS);
}