summaryrefslogtreecommitdiffstats
path: root/modules/menu/src/parser.c
blob: 2a20afbe6ae5e218bfc9d13266f8a75db0331164 (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/******************************************************************************
 *	Copyright (C) 2015	Alejandro Colomar Andrés		      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
/*	*	*	*	*	*	*	*	*	*
 *	*	* Standard	*	*	*	*	*	*
 *	*	*	*	*	*	*	*	*	*/
	#include <getopt.h>
		/* FILE & fopen() & snprintf() & FILENAME_MAX */
	#include <stdio.h>
		/* exit() */
	#include <stdlib.h>

/*	*	*	*	*	*	*	*	*	*
 *	*	* Other	*	*	*	*	*	*	*
 *	*	*	*	*	*	*	*	*	*/
	#include "alx_seed.h"

	#include "about.h"
	#include "game_iface.h"
	#include "player_iface.h"
	#include "save.h"
	#include "start.h"

	#include "menu_iface.h"

	#include "parser.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
	# define	OPT_LIST	"xhLuv""a:b:f:i:p:r:s:"


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	parse_rows		(char* argument);
static	void	parse_columns		(char* argument);
static	void	parse_file		(char* argument);
static	void	parse_iface		(char* argument);
static	void	parse_proportion	(char* argument);
static	void	parse_rand_seed		(char* argument);
static	void	parse_start		(char* argument);


/******************************************************************************
 ******* main *****************************************************************
 ******************************************************************************/
void	parser	(int argc, char *argv[])
{
	int	opt		= 0;
	int	opt_index	= 0;

	struct option	long_options[]	= {
		/* Standard */
		{"exit",		no_argument,		0,	'x'},
		{"help",		no_argument,		0,	'h'},
		{"license",		no_argument,		0,	'L'},
		{"usage",		no_argument,		0,	'u'},
		{"version",		no_argument,		0,	'v'},
		/* Non-standard */
		{"rows",		required_argument,	0,	'a'},
		{"columns",		required_argument,	0,	'b'},
		{"file",		required_argument,	0,	'f'},
		{"iface",		required_argument,	0,	'i'},
		{"proportion",		required_argument,	0,	'p'},
//		{"rand-seed",		required_argument,	0,	'r'},
		{"start",		required_argument,	0,	's'},
		/* Null */
		{0,			0,			0,	0}
	};

	while ((opt = getopt_long(argc, argv, OPT_LIST, long_options,
						&opt_index)) != -1) {

		switch (opt) {
		/* Standard */
		case 'x':
			flag_exit	= true;
			break;

		case 'h':
			print_share_file(SHARE_HELP);
			exit(EXIT_SUCCESS);

		case 'L':
			print_share_file(SHARE_LICENSE);
			exit(EXIT_SUCCESS);

		case 'u':
			print_share_file(SHARE_USAGE);
			exit(EXIT_SUCCESS);

		case 'v':
			print_version();
			exit(EXIT_SUCCESS);

		/* Non-standard */
		case 'a':
			parse_rows(optarg);
			break;

		case 'b':
			parse_columns(optarg);
			break;

		case 'f':
			parse_file(optarg);
			break;

		case 'i':
			parse_iface(optarg);
			break;

		case 'p':
			parse_proportion(optarg);
			break;
/* Only for DEVEL
		case 'r':
			parse_rand_seed(optarg);
			break;
*/
		case 's':
			parse_start(optarg);
			break;

		case '?':
			/* getopt_long already printed an error message. */

		default:
			print_share_file(SHARE_USAGE);
			exit(EXIT_FAILURE);
		}
	}
}


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	parse_rows		(char* argument)
{
	menu_iface_variables.rows	= atoi(argument);
	if (menu_iface_variables.rows < 2 || menu_iface_variables.rows > ROWS_MAX) {
		printf("--rows argument not valid\n");
		printf("It must be an integer [%i U %i]\n", 2, ROWS_MAX);
		exit(EXIT_FAILURE);
	}
}

static	void	parse_columns		(char* argument)
{
	menu_iface_variables.cols	= atoi(argument);
	if (menu_iface_variables.cols < 2 || menu_iface_variables.cols > COLS_MAX) {
		printf("--columns argument not valid\n");
		printf("It must be an integer [%i U %i]\n", 2, COLS_MAX);
		exit(EXIT_FAILURE);
	}
}

static	void	parse_file		(char* argument)
{
	// FIXME
	FILE	*fp;
	fp	= fopen(argument, "r");
	if (!fp) {
		printf("--file argument not valid\n");
		printf("It must be a valid file name (relative to saved dir)\n");
		exit(EXIT_FAILURE);
	} else {
		fclose(fp);

		sprintf(saved_path, "");
		snprintf(saved_name, FILENAME_MAX, argument);
	}
}

static	void	parse_iface		(char* argument)
{
	menu_iface_mode		= atoi(argument);
	player_iface_mode	= menu_iface_mode;
	if (menu_iface_mode < MENU_IFACE_CLUI || menu_iface_mode > MENU_IFACE_GUI) {
		printf("--iface argument not valid\n");
		printf("It must be an integer [%i U %i]\n", MENU_IFACE_CLUI, MENU_IFACE_GUI);
		exit(EXIT_FAILURE);
	}
}

static	void	parse_proportion	(char* argument)
{
	menu_iface_variables.p	= atof(argument);
	if (menu_iface_variables.p < 0 || menu_iface_variables.p > 1) {
		printf("--proportion argument not valid\n");
		printf("It must be a real [0 U 1]\n");
		exit(EXIT_FAILURE);
	}
}

static	void	parse_rand_seed		(char* argument)
{
	int	seed;
	seed	= atof(argument);
	srand(seed);
}

static	void	parse_start		(char* argument)
{
	start_mode	= atoi(argument);
	if (start_mode < START_FOO || start_mode > START_LOAD) {
		printf("--start argument not valid\n");
		printf("It must be an integer [%i U %i]\n", START_FOO, START_LOAD);
		exit(EXIT_FAILURE);
	}
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/