universe@0: /* universe@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@0: * universe@55: * Copyright 2016 Mike Becker. All rights reserved. universe@0: * universe@0: * Redistribution and use in source and binary forms, with or without universe@0: * modification, are permitted provided that the following conditions are met: universe@0: * universe@0: * 1. Redistributions of source code must retain the above copyright universe@0: * notice, this list of conditions and the following disclaimer. universe@0: * universe@0: * 2. Redistributions in binary form must reproduce the above copyright universe@0: * notice, this list of conditions and the following disclaimer in the universe@0: * documentation and/or other materials provided with the distribution. universe@0: * universe@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@0: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@0: * POSSIBILITY OF SUCH DAMAGE. universe@0: * universe@0: */ universe@0: universe@1: #include "terminal-chess.h" universe@7: #include "game.h" universe@7: #include "input.h" universe@35: #include "colors.h" universe@1: #include universe@2: #include universe@7: #include universe@69: #include universe@1: universe@1: int get_settings(int argc, char **argv, Settings *settings) { universe@1: char *valid; universe@2: unsigned long int time, port; universe@2: uint8_t timeunit = 60; universe@2: size_t len; universe@1: universe@69: for (int opt ; (opt = getopt(argc, argv, "a:bc:hp:rsS:t:Uv")) != -1 ;) { universe@1: switch (opt) { universe@47: case 'c': universe@47: settings->continuepgn = optarg; universe@47: break; universe@2: case 'b': universe@2: settings->gameinfo.servercolor = BLACK; universe@2: break; universe@2: case 'r': universe@7: settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK; universe@2: break; universe@26: case 's': universe@26: settings->singlemachine = 1; universe@26: break; universe@50: case 'S': universe@50: settings->analyzepgn = optarg; universe@50: break; universe@69: case 'U': universe@69: settings->unicode = 0; universe@69: break; universe@2: case 't': universe@2: case 'a': universe@2: len = strlen(optarg); universe@2: if (optarg[len-1] == 's') { universe@2: optarg[len-1] = '\0'; universe@2: timeunit = 1; universe@2: } universe@2: universe@30: if ((time = strtoul(optarg, &valid, 10))*timeunit > UINT16_MAX universe@2: || *valid != '\0') { universe@30: fprintf(stderr, "Specified time is invalid (%s)" universe@30: "- Maximum: 65535 seconds (1092 minutes)\n", optarg); universe@2: return 1; universe@2: } else { universe@30: settings->gameinfo.timecontrol = 1; universe@2: if (opt=='t') { universe@2: settings->gameinfo.time = timeunit * time; universe@1: } else { universe@2: settings->gameinfo.addtime = time; universe@1: } universe@2: } universe@2: break; universe@2: case 'p': universe@2: port = strtol(optarg, &valid, 10); universe@2: if (port < 1025 || port > 65535 || *valid != '\0') { universe@2: fprintf(stderr, universe@2: "Invalid port number (%s) - choose a number between " universe@2: "1025 and 65535\n", universe@2: optarg); universe@2: return 1; universe@2: } else { universe@2: settings->port = optarg; universe@2: } universe@2: break; universe@52: case 'v': universe@52: printf("terminal-chess : Version %s (Netcode Version %d)\n", universe@52: PROGRAM_VERSION, NETCODE_VERSION); universe@52: exit(0); universe@2: case 'h': universe@2: case '?': universe@52: printf( universe@52: "Usage: terminal-chess [OPTION]... [HOST]\n" universe@52: "Starts/joins a network chess game\n" universe@52: "\nGeneral options\n" universe@52: " -c Continue the specified game\n" universe@52: " -h This help page\n" universe@52: " -p TCP port to use (default: 27015)\n" universe@52: // TODO: implement and activate feature universe@52: //" -S Compute and print statistics for the specified game\n" universe@52: " -v Print version information and exists\n" universe@52: "\nServer options\n" universe@52: " -a