From 7fa5833fb6d4c7085b68090999c1fb10a3d0b08c Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Thu, 30 Jun 2011 15:25:30 +1000 Subject: [PATCH] Complete the command line argument processing routines --- src/Makefile.am | 1 - src/system.h | 23 +++++++++++++++ src/trader.c | 28 ++++++++++++++----- src/utils.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 11 ++++++++ 5 files changed, 129 insertions(+), 8 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 05b93c4..5395ca6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -34,4 +34,3 @@ trader_SOURCES = \ globals.c globals.h \ utils.c utils.h \ system.h -trader_LDADD = @CURSES_LIB@ diff --git a/src/system.h b/src/system.h index 1757c48..d5d6d4e 100644 --- a/src/system.h +++ b/src/system.h @@ -48,6 +48,29 @@ #include #include #include +#include + + +/************************************************************************ +* 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 */ diff --git a/src/trader.c b/src/trader.c index 0e07d57..78137db 100644 --- a/src/trader.c +++ b/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(_("\ diff --git a/src/utils.c b/src/utils.c index 84db493..77d1437 100644 --- a/src/utils.c +++ b/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; + } +} diff --git a/src/utils.h b/src/utils.h index 489c99e..b71e12a 100644 --- a/src/utils.h +++ b/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 */