mirror of
https://git.zap.org.au/git/trader.git
synced 2025-01-03 14:57:41 -05:00
Complete the command line argument processing routines
This commit is contained in:
parent
255dc9aa19
commit
7fa5833fb6
@ -34,4 +34,3 @@ trader_SOURCES = \
|
||||
globals.c globals.h \
|
||||
utils.c utils.h \
|
||||
system.h
|
||||
trader_LDADD = @CURSES_LIB@
|
||||
|
23
src/system.h
23
src/system.h
@ -48,6 +48,29 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
|
||||
/************************************************************************
|
||||
* Miscellaneous definitions *
|
||||
************************************************************************/
|
||||
|
||||
// For future use in internationalisation
|
||||
#ifndef _
|
||||
# define _(x) (x)
|
||||
#endif
|
||||
#ifndef N_
|
||||
# define N_(x) (x)
|
||||
#endif
|
||||
|
||||
|
||||
// Boolean values
|
||||
#ifndef FALSE
|
||||
# define FALSE (0)
|
||||
#endif
|
||||
#ifndef TRUE
|
||||
# define TRUE (1)
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* included_SYSTEM_H */
|
||||
|
28
src/trader.c
28
src/trader.c
@ -60,6 +60,7 @@ int main (int argc, char *argv[])
|
||||
printf(_("Program name: %s\n"), program_name());
|
||||
printf(_("Home directory: %s\n"), home_directory());
|
||||
printf(_("Data directory: %s\n"), data_directory());
|
||||
printf(_("Game filename: %s\n"), game_filename);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@ -71,9 +72,9 @@ int main (int argc, char *argv[])
|
||||
|
||||
/* Constants for command line options */
|
||||
|
||||
static const char options_short[] = "hV?";
|
||||
/* -h, -? --help
|
||||
-V --version
|
||||
static const char options_short[] = "hV";
|
||||
/* -h --help
|
||||
-V --version
|
||||
*/
|
||||
|
||||
static struct option const options_long[] = {
|
||||
@ -106,8 +107,7 @@ static void process_cmdline (int argc, char *argv[])
|
||||
|
||||
switch (c) {
|
||||
case 'h':
|
||||
case '?':
|
||||
/* -h, -?, --help: show help */
|
||||
/* -h, --help: show help */
|
||||
show_usage(EXIT_SUCCESS);
|
||||
|
||||
case 'V':
|
||||
@ -122,6 +122,20 @@ static void process_cmdline (int argc, char *argv[])
|
||||
// Process remaining arguments
|
||||
|
||||
if ((optind < argc) && (argv[optind] != NULL)) {
|
||||
if (argv[optind][0] == '-') {
|
||||
fprintf(stderr, _("%s: invalid operand `%s'\n"), program_name(),
|
||||
argv[optind]);
|
||||
show_usage(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
game_filename = strto_game_filename(argv[optind]);
|
||||
optind++;
|
||||
}
|
||||
|
||||
if ((optind < argc) && (argv[optind] != NULL)) {
|
||||
fprintf(stderr, _("%s: extra operand `%s'\n"), program_name(),
|
||||
argv[optind]);
|
||||
show_usage(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,8 +181,8 @@ NO WARRANTY, to the extent permitted by law; see the License for details.\n\
|
||||
static void show_usage (int status)
|
||||
{
|
||||
if (status != EXIT_SUCCESS) {
|
||||
fprintf(stderr, _("Try `%s --help' for more information.\n"),
|
||||
program_name());
|
||||
fprintf(stderr, _("%s: Try `%s --help' for more information.\n"),
|
||||
program_name(), program_name());
|
||||
} else {
|
||||
printf(_("Usage: %s [OPTION ...] [GAME]\n"), program_name());
|
||||
printf(_("\
|
||||
|
74
src/utils.c
74
src/utils.c
@ -148,3 +148,77 @@ const char *data_directory (void)
|
||||
|
||||
return data_directory_str;
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------
|
||||
Function: strto_game_filename - Convert a string to a game filename
|
||||
Arguments: game_num - Game number (1-9) as a string
|
||||
Returns: char * - Pointer to game filename string
|
||||
|
||||
This function returns the full game filename as a malloc()ed string.
|
||||
If game_num is a string between "1" and "9" inclusive, the string
|
||||
returned is in the form data_directory() + "/" + GAME_FILENAME(game_num),
|
||||
eg, "/home/test/.trader/game7". If game_num is any other string, that
|
||||
string is returned as the game filename. If game_num is NULL, NULL is
|
||||
returned.
|
||||
*/
|
||||
|
||||
char *strto_game_filename (const char *game_num)
|
||||
{
|
||||
if (game_num == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((strlen(game_num) == 1) &&
|
||||
(game_num[0] >= '1') && (game_num[0] <= '9')) {
|
||||
return intto_game_filename(game_num[0] - '0');
|
||||
} else {
|
||||
char *p = malloc(strlen(game_num) + 1);
|
||||
if (p != NULL) {
|
||||
strcpy(p, game_num);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------
|
||||
Function: intto_game_filename - Convert an integer to a game filename
|
||||
Arguments: game_num - Game number (1-9) as an integer
|
||||
Returns: char * - Pointer to game filename string
|
||||
|
||||
This function returns the full game filename as a malloc()ed string.
|
||||
If game_num is between 1 and 9 inclusive, the string returned is in the
|
||||
form data_directory() + "/" + GAME_FILENAME(game_num). If game_num is
|
||||
any other integer, NULL is returned.
|
||||
*/
|
||||
|
||||
char *intto_game_filename (const int game_num)
|
||||
{
|
||||
char buf[GAME_FILENAME_BUFSIZE]; // Buffer for part of filename
|
||||
const char *dd; // Data directory
|
||||
|
||||
|
||||
if ((game_num < 1) || (game_num > 9)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dd = data_directory();
|
||||
snprintf(buf, GAME_FILENAME_BUFSIZE, GAME_FILENAME_PROTO, game_num);
|
||||
|
||||
if (dd == NULL) {
|
||||
char *p = malloc(strlen(buf) + 1);
|
||||
if (p != NULL) {
|
||||
strcpy(p, buf);
|
||||
}
|
||||
return p;
|
||||
} else {
|
||||
char *p = malloc(strlen(dd) + strlen(buf) + 2);
|
||||
if (p != NULL) {
|
||||
strcpy(p, dd);
|
||||
strcat(p, "/");
|
||||
strcat(p, buf);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
11
src/utils.h
11
src/utils.h
@ -32,6 +32,14 @@
|
||||
#define included_UTILS_H 1
|
||||
|
||||
|
||||
/************************************************************************
|
||||
* Constants and type declarations *
|
||||
************************************************************************/
|
||||
|
||||
#define GAME_FILENAME_PROTO "game%d"
|
||||
#define GAME_FILENAME_BUFSIZE (16)
|
||||
|
||||
|
||||
/************************************************************************
|
||||
* Utility function declarations *
|
||||
************************************************************************/
|
||||
@ -42,5 +50,8 @@ extern const char *program_name (void);
|
||||
extern const char *home_directory (void);
|
||||
extern const char *data_directory (void);
|
||||
|
||||
extern char *strto_game_filename (const char *game_num);
|
||||
extern char *intto_game_filename (const int game_num);
|
||||
|
||||
|
||||
#endif /* included_UTILS_H */
|
||||
|
Loading…
Reference in New Issue
Block a user