]> git.cworth.org Git - ttt/blob - src/ttt-args.c
2005-11-24 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-args.c
1 /* ttt-args.c - Parse command-line arguments for ttt using getopt
2  *
3  * Copyright © 2005 Carl Worth
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Author: Carl Worth <cworth@cworth.org>
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <libgen.h>
26 #include <getopt.h>
27
28 #include "ttt-args.h"
29
30 static const char TTT_ARGS_PROGRAM_VERSION[] = VERSION;
31 static const char TTT_ARGS_PROGRAM_DESCRIPTION[] = "client-server tic-tac-toe game";
32 static const char TTT_ARGS_PROGRAM_BUG_ADDRESS[] = "<carl@theworths.org>";
33
34 static char TTT_ARGS_PROGRAM_ARGDOC[] = "";
35
36 /* XXX: getopt is rather annoying in that you must maintain a
37  * string-encoding of the options in addition to the table. For
38  * example, to enable the sample display option below, one would have
39  * to add "d:" to the optstring.
40  *
41  * A useful exercise would be to write a function to generate the
42  * optstring from the table.
43  *
44  * The other annoying thing is that the table does not include help
45  * strings, so the args_help function below must also be maintained
46  * manually.
47  */
48
49 enum {
50     TTT_ARGS_VAL_LOG_FILE = 256,
51     TTT_ARGS_VAL_HELP,
52     TTT_ARGS_VAL_VERSION
53 };
54
55 static char ttt_args_optstring[] = "h:p:";
56 static struct option ttt_args_options[] = {
57     /* name,            has_arg,        flag,   val */
58     {"host",            1,              0,      'h'},
59     {"port",            1,              0,      'p'},
60     {"log-file",        1,              0,      TTT_ARGS_VAL_LOG_FILE},
61     {"help",            0,              0,      TTT_ARGS_VAL_HELP},
62     {"version",         0,              0,      TTT_ARGS_VAL_VERSION},
63     { 0 }
64 };
65
66 static void
67 ttt_args_help (const char *argv0)
68 {
69     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
70     printf ("%s - %s\n", argv0, TTT_ARGS_PROGRAM_DESCRIPTION);
71     puts ("");
72     printf ("Options that are common to both client and server:\n");
73     puts ("");
74     printf ("  -h HOST, --host=HOST\tHost to connect/bind to\n");
75     printf ("  -p PORT, --port=PORT\tPort to connect/bind to\n");
76     printf ("           --help\tGive this help list\n");
77     printf ("           --version\tPrint program version\n");
78     puts ("");
79     printf ("Options that are specific to the server:\n");
80     puts ("");
81     printf ("           --log-file=FILE\tFile to use for logging\n");
82 }
83
84 #if 0
85 static void
86 ttt_args_usage (const char *argv0)
87 {
88     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
89     printf ("Try `%s --help' for more information.\n", argv0);
90 }
91 #endif
92
93 int
94 ttt_args_parse(ttt_args_t *args, int argc, char *argv[])
95 {
96     char *argv0_copy = strdup (argv[0]);
97     char *argv0 = basename (argv0_copy);
98
99     int c;
100
101     args->host = TTT_ARGS_HOST_DEFAULT;
102     args->port = TTT_ARGS_PORT_DEFAULT;
103     args->log_file = TTT_ARGS_LOG_FILE_DEFAULT;
104
105     while (1) {
106         c = getopt_long (argc, argv, ttt_args_optstring, ttt_args_options, NULL);
107         if (c == -1)
108             break;
109
110         switch (c) {
111         case 'h':
112             args->host = optarg;
113             break;
114         case 'p':
115             args->port = optarg;
116             break;
117         case TTT_ARGS_VAL_LOG_FILE:
118             args->log_file = optarg;
119             break;
120         case TTT_ARGS_VAL_VERSION:
121             printf ("%s\n", VERSION);
122             exit (0);
123             break;
124
125         case TTT_ARGS_VAL_HELP:
126             ttt_args_help (argv0);
127             exit (0);
128             break;
129
130         case '?':
131             ttt_args_help (argv0);
132             exit (1);
133             break;
134
135         default:
136             fprintf (stderr, "Unhandled option `%c'\n", c);
137             break;
138         }
139     }
140
141     free (argv0_copy);
142
143     return 0;
144 }