summaryrefslogtreecommitdiffstats
path: root/src/menu/parser.c
blob: b59403210cd02640e74d81dc639f96c9e3956c6d (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/******************************************************************************
 *	Copyright (C) 2015	Alejandro Colomar Andrés		      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "mine-sweeper/menu/parser.h"

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

#define ALX_NO_PREFIX
#include <libalx/base/stdio.h>
#include <libalx/base/stdlib.h>

#include "mine-sweeper/about/about.h"
#include "mine-sweeper/ctrl/start.h"
#include "mine-sweeper/game/iface.h"
#include "mine-sweeper/menu/iface.h"
#include "mine-sweeper/player/iface.h"
#include "mine-sweeper/save/save.h"


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


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	parse_rows		(const char *argument);
static	void	parse_columns		(const char *argument);
static	void	parse_file		(const char *argument);
static	void	parse_iface		(const char *argument);
static	void	parse_proportion	(const char *argument);
#if 0
static	void	parse_rand_seed		(const char *argument);
#endif
static	void	parse_start		(const char *argument);


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

	const 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;
# if 0
// for DEVEL
		case 'r':
			parse_rand_seed(optarg);
			break;
# endif
		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		(const char *argument)
{

	if (strtoi64_s(&menu_iface_variables.rows, argument, 0, NULL))
		goto err;
	if ((menu_iface_variables.rows < 2)  ||
			(menu_iface_variables.rows > ROWS_MAX))
		goto err;
	return;
err:
	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		(const char *argument)
{

	if (strtoi64_s(&menu_iface_variables.cols, argument, 0, NULL))
		goto err;
	if ((menu_iface_variables.cols < 2)  ||
			(menu_iface_variables.cols > COLS_MAX))
		goto err;
	return;
err:
	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		(const char *argument)
{
	FILE	*fp;

	// FIXME
	fp	= fopen(argument, "r");
	if (!fp)
		goto err;
	fclose(fp);

	saved_path[0]	= '\0';
	if (sbprintf(saved_name, NULL, "%s", argument))
		goto err;

	return;

err:
	printf("--file argument not valid\n");
	printf("It must be a valid file name (relative to saved dir)\n");
	exit(EXIT_FAILURE);
}

static	void	parse_iface		(const char *argument)
{

	if (strtoi32_s(&menu_iface_mode, argument, 0, NULL))
		goto err;
	player_iface_mode	= menu_iface_mode;
	if ((menu_iface_mode < MENU_IFACE_CLUI)  ||
					(menu_iface_mode > MENU_IFACE_GUI))
		goto err;
	return;
err:
	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	(const char *argument)
{

	if (strtod_s(&menu_iface_variables.p, argument, NULL))
		goto err;
	if ((menu_iface_variables.p < 0)  ||  (menu_iface_variables.p > 1))
		goto err;
	return;
err:
	printf("--proportion argument not valid\n");
	printf("It must be a real [0 U 1]\n");
	exit(EXIT_FAILURE);
}
#if 0
static	void	parse_rand_seed		(const char *argument)
{
	int	seed;

	seed	= atof(argument);
	srand(seed);
}
#endif
static	void	parse_start		(const char *argument)
{

	if (strtoi32_s(&start_mode, argument, 0, NULL))
		goto err;
	if ((start_mode < START_FOO)  ||  (start_mode > START_LOAD))
		goto err;
	return;
err:
	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 **********************************************************
 ******************************************************************************/