/***************************************************************************** * ___ __ __ ___ _ __ * * / _ \\ \/ // _ \ '_ \ * * | (_) |> <| __/ | | | * * \___//_/\_\\___|_| |_| * * * * The card game * * * * Copyright (C) 2011, Reiner Herrmann * * Mario Kilies * * * * 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 . * * * *****************************************************************************/ #include #include #include #include #include #include "data_store.h" #include "game.h" #include "manual.h" #define DEFAULT_PORT "12345" /** * Print usage information * @param[in] name The name of the program (normally argv[0]) */ static void print_usage(const char* name) { const char* usage = "Usage: %s [-u username] [-s address] [-n num_players] [-l] [-p port] [-m]\n" "\t-s address\t\thostname/address to connect to (client, required) or listen on (server, optional)\n" "\t-n num_players\t\tnumber of players; only on server (default: 2, max: 10)\n" "\t-l\t\t\tstart server\n" "\t-u username\t\tyour nickname in the player list (default: $USER)\n" "\t-p port\t\t\tport to use for connecting/listening (default: %s)\n" "\t-m\t\t\tdisplay the manual\n"; fprintf(stderr, usage, name, 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:hlm"; 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 'h': // help default: print_usage(argv[0]); break; } } if(!servermode && addr == NULL) print_usage(argv[0]); 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); }