src/main.c

Tue, 08 Apr 2014 21:13:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 08 Apr 2014 21:13:28 +0200
changeset 31
ed440bcd9740
parent 30
a285ee393860
child 33
866025982aa9
permissions
-rw-r--r--

fixed some type bugs, uninitialized memory and async input function

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2014 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    30 #include "terminal-chess.h"
    31 #include "game.h"
    32 #include "input.h"
    33 #include <string.h>
    34 #include <time.h>
    35 #include <getopt.h>
    37 int get_settings(int argc, char **argv, Settings *settings) {
    38     char *valid;
    39     unsigned long int time, port;
    40     uint8_t timeunit = 60;
    41     size_t len;
    43     for (int opt ; (opt = getopt(argc, argv, "a:bhp:rst:")) != -1 ;) {
    44         switch (opt) {
    45         case 'b':
    46             settings->gameinfo.servercolor = BLACK;
    47             break;
    48         case 'r':
    49             settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
    50             break;
    51         case 's':
    52             settings->singlemachine = 1;
    53             break;
    54         case 't':
    55         case 'a':
    56             len = strlen(optarg);
    57             if (optarg[len-1] == 's') {
    58                 optarg[len-1] = '\0';
    59                 timeunit = 1;
    60             }
    62             if ((time = strtoul(optarg, &valid, 10))*timeunit > UINT16_MAX
    63                 || *valid != '\0') {
    64                 fprintf(stderr, "Specified time is invalid (%s)"
    65                     "- Maximum: 65535 seconds (1092 minutes)\n", optarg);
    66                 return 1;
    67             } else {
    68                 settings->gameinfo.timecontrol = 1;
    69                 if (opt=='t') {
    70                     settings->gameinfo.time = timeunit * time;
    71                 } else {
    72                     settings->gameinfo.addtime = time;
    73                 }
    74             }
    75             break;
    76         case 'p':
    77             port = strtol(optarg, &valid, 10);
    78             if (port < 1025 || port > 65535 || *valid != '\0') {
    79                 fprintf(stderr,
    80                     "Invalid port number (%s) - choose a number between "
    81                     "1025 and 65535\n",
    82                     optarg);
    83                 return 1;
    84             } else {
    85                 settings->port = optarg;
    86             }
    87             break;
    88         case 'h':
    89         case '?':
    90             settings->printhelp = 1;
    91             break;
    92         }
    93     }
    95     if (optind == argc - 1) {
    96         settings->serverhost = argv[optind];
    97     } else if (optind < argc - 1) {
    98         fprintf(stderr, "Too many arguments\n");
    99         return 1;
   100     }
   102     return 0;
   103 }
   105 Settings default_settings() {
   106     Settings settings;
   107     memset(&settings, 0, sizeof(Settings));
   108     settings.gameinfo.servercolor = WHITE;
   109     settings.port = "27015";
   110     return settings;
   111 }
   113 void dump_gameinfo(GameInfo *gameinfo) {
   114     int serverwhite = gameinfo->servercolor == WHITE;
   115     attron(A_UNDERLINE);
   116     printw("Game details\n");
   117     attroff(A_UNDERLINE);
   118     printw("  Server:     %s\n  Client:     %s\n",
   119         serverwhite?"white":"black", serverwhite?"black":"White"
   120     );
   121     if (gameinfo->timecontrol) {
   122         if (gameinfo->time % 60) {
   123             printw("  Time limit: %ds + %ds\n",
   124                 gameinfo->time, gameinfo->addtime);
   125         } else {
   126             printw("  Time limit: %dm + %ds\n",
   127                 gameinfo->time/60, gameinfo->addtime);
   128         }
   129     } else {
   130         printw("  No time limit\n");
   131     }
   132     refresh();
   133 }
   135 void leavescr() {
   136     endwin();
   137 }
   139 int main(int argc, char **argv) {
   140     srand(time(NULL));
   142     Settings settings = default_settings();
   143     if (get_settings(argc, argv, &settings)) {
   144         return 1;
   145     }
   147     if (settings.printhelp) {
   148         printf(
   149             "Usage: terminal-chess [OPTION]... [HOST]\n"
   150             "Starts/joins a network chess game\n"
   151             "\nGeneral options\n"
   152             "  -h            This help page\n"
   153             "  -p            TCP port to use (default: 27015)\n"
   154             "\nServer options\n"
   155             "  -a <time>     Specifies the time to add after each move\n"
   156             "  -b            Server plays black pieces (default: white)\n"
   157             "  -r            Distribute color randomly\n"
   158             "  -s            Single machine mode\n"
   159             "  -t <time>     Specifies time limit (default: no limit)\n"
   160             "\nNotes\n"
   161             "The time unit for -a is seconds and for -t minutes by default. To "
   162             "specify\nseconds for the -t option, use the s suffix.\n"
   163             "Example: -t 150s\n"
   164         );
   165         return EXIT_SUCCESS;
   166     }    
   167     initscr();
   168     halfdelay(10);
   169     keypad(stdscr, TRUE);
   170     if (has_colors()) {
   171         start_color();
   172         init_colorpairs();
   173         bkgd(COLOR_PAIR(COL_YB));
   174     } else {
   175         fprintf(stderr, "Non-colored terminals are not supported yet.");
   176         endwin();
   177         return EXIT_FAILURE;
   178     }
   179     atexit(leavescr);
   181     if (settings.singlemachine) {
   182         game_start_singlemachine(&settings);
   183     } else {
   184         return is_server(&settings) ?
   185             server_run(&settings) : client_run(&settings);
   186     }
   187 }

mercurial