src/main.c

changeset 1
e5fbb8f9edbe
parent 0
98034084033f
child 2
0a08f79c320d
     1.1 --- a/src/main.c	Thu Jan 23 14:45:34 2014 +0100
     1.2 +++ b/src/main.c	Wed Feb 05 14:07:43 2014 +0100
     1.3 @@ -27,9 +27,98 @@
     1.4   *
     1.5   */
     1.6  
     1.7 -#include <stdlib.h>
     1.8 +#include "terminal-chess.h"
     1.9 +#include <string.h>
    1.10 +
    1.11 +int get_settings(int argc, char **argv, Settings *settings) {
    1.12 +    char *valid;
    1.13 +    
    1.14 +    for (char opt ; (opt = getopt(argc, argv, "hp:")) != -1 ;) {
    1.15 +        switch (opt) {
    1.16 +            case 'p':
    1.17 +                if (strtol(optarg, &valid, 10) < 1025 || *valid != '\0') {
    1.18 +                    fprintf(stderr,
    1.19 +                        "Invalid port number (%s) - choose a number > 1024\n",
    1.20 +                        optarg);
    1.21 +                    return 1;
    1.22 +                } else {
    1.23 +                    settings->port = optarg;
    1.24 +                }
    1.25 +                break;
    1.26 +            case 'h':
    1.27 +            case '?':
    1.28 +                settings->printhelp = 1;
    1.29 +                break;
    1.30 +        }
    1.31 +    }
    1.32 +    
    1.33 +    if (optind == argc - 1) {
    1.34 +        settings->serverhost = argv[optind];
    1.35 +    } else if (optind < argc - 1) {
    1.36 +        fprintf(stderr, "Too many arguments\n");
    1.37 +        return 1;
    1.38 +    }
    1.39 +    
    1.40 +    return 0;
    1.41 +}
    1.42 +
    1.43 +Settings default_settings() {
    1.44 +    Settings settings;
    1.45 +    memset(&settings, 0, sizeof(Settings));
    1.46 +    settings.port = "27015";
    1.47 +    return settings;
    1.48 +}
    1.49 +
    1.50 +int cleanup(Settings *settings, int exitcode) {
    1.51 +    if (settings->server) {
    1.52 +        if (net_destroy(settings->server)) {
    1.53 +            perror("Server shutdown failed");
    1.54 +        }
    1.55 +    }
    1.56 +    
    1.57 +    return exitcode;
    1.58 +}
    1.59  
    1.60  int main(int argc, char **argv) {
    1.61 -    return EXIT_SUCCESS;
    1.62 +    
    1.63 +    Settings settings = default_settings();
    1.64 +    get_settings(argc, argv, &settings);
    1.65 +    
    1.66 +    if (settings.printhelp) {
    1.67 +        printf(
    1.68 +            "Usage: %s [OPTION]... [HOST]\n"
    1.69 +            "Starts/joins a network chess game\n\n"
    1.70 +            "  -h            This help page\n"
    1.71 +            "  -p            TCP port to use (default: 27015)\n",
    1.72 +            argv[0]);
    1.73 +        return EXIT_SUCCESS;
    1.74 +    }
    1.75 +    
    1.76 +    Server server;
    1.77 +    settings.server = &server;
    1.78 +    
    1.79 +    if (is_server(&settings)) {
    1.80 +        if (net_find(&server, settings.serverhost, settings.port)) {
    1.81 +            perror("Can't find server");
    1.82 +            return cleanup(&settings, EXIT_FAILURE);
    1.83 +        }
    1.84 +        
    1.85 +        if (net_connect(&server)) {
    1.86 +            perror("Can't connect to server");
    1.87 +            return cleanup(&settings, EXIT_FAILURE);
    1.88 +        }
    1.89 +    } else {
    1.90 +        if (net_create(&server, settings.port)) {
    1.91 +            perror("Server creation failed");
    1.92 +            return cleanup(&settings, EXIT_FAILURE);
    1.93 +        }
    1.94 +        
    1.95 +        if (net_listen(&server)) {
    1.96 +            perror("Listening for client failed");
    1.97 +            return cleanup(&settings, EXIT_FAILURE);
    1.98 +        }
    1.99 +    }
   1.100 +    
   1.101 +    return cleanup(&settings, EXIT_SUCCESS);
   1.102  }
   1.103  

mercurial