]> git.cworth.org Git - ttt/blob - src/ttt-args.c
2005-12-09 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_PID_FILE,
52     TTT_ARGS_VAL_HELP,
53     TTT_ARGS_VAL_VERSION,
54 };
55
56 static char ttt_args_optstring[] = "dh:p:u:";
57 static struct option ttt_args_options[] = {
58     /* name,            has_arg,        flag,   val */
59     {"host",            1,              0,      'h'},
60     {"port",            1,              0,      'p'},
61     {"user",            1,              0,      'u'},
62     {"detach",          0,              0,      'd'},
63     {"log-file",        1,              0,      TTT_ARGS_VAL_LOG_FILE},
64     {"pid-file",        1,              0,      TTT_ARGS_VAL_PID_FILE},
65     {"help",            0,              0,      TTT_ARGS_VAL_HELP},
66     {"version",         0,              0,      TTT_ARGS_VAL_VERSION},
67     { 0 }
68 };
69
70 static void
71 ttt_args_help (const char *argv0)
72 {
73     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
74     printf ("%s - %s\n", argv0, TTT_ARGS_PROGRAM_DESCRIPTION);
75     puts ("");
76     printf ("Options that are common to both client and server:\n");
77     puts ("");
78     printf ("  -h HOST, --host=HOST\tHost to connect/bind to [%s]\n",
79             TTT_ARGS_HOST_DEFAULT);
80     printf ("  -p PORT, --port=PORT\tPort to connect/bind to [%s]\n",
81             TTT_ARGS_PORT_DEFAULT);
82     printf ("  -u USER, --user=USER\tUser the server should run as.\n");
83     printf ("           --help\tGive this help list\n");
84     printf ("           --version\tPrint program version\n");
85     puts ("");
86     printf ("Options that are specific to the server:\n");
87     puts ("");
88     printf ("           --log-file=FILE\tFile to use for logging [stderr]\n");
89     printf ("       -d, --detach\tDetach and daemonize\n");
90     printf ("           --pid-file=FILE\tFile in which to save PID (if -d given)\n"
91             "                          \t[%s]\n",
92             TTT_ARGS_PID_FILE_DEFAULT);
93 }
94
95 #if 0
96 static void
97 ttt_args_usage (const char *argv0)
98 {
99     printf ("Usage: %s [OPTION] %s\n", argv0, TTT_ARGS_PROGRAM_ARGDOC);
100     printf ("Try `%s --help' for more information.\n", argv0);
101 }
102 #endif
103
104 int
105 ttt_args_parse(ttt_args_t *args, int argc, char *argv[])
106 {
107     char *argv0_copy = strdup (argv[0]);
108     char *argv0 = basename (argv0_copy);
109
110     int c;
111
112     args->host = TTT_ARGS_HOST_DEFAULT;
113     args->port = TTT_ARGS_PORT_DEFAULT;
114     args->user = NULL;
115     args->log_file = TTT_ARGS_LOG_FILE_DEFAULT;
116     args->detach = FALSE;
117     args->pid_file = TTT_ARGS_PID_FILE_DEFAULT;
118
119     while (1) {
120         c = getopt_long (argc, argv, ttt_args_optstring, ttt_args_options, NULL);
121         if (c == -1)
122             break;
123
124         switch (c) {
125         case 'h':
126             args->host = optarg;
127             break;
128         case 'p':
129             args->port = optarg;
130             break;
131         case 'u':
132             args->user = optarg;
133             break;
134         case TTT_ARGS_VAL_LOG_FILE:
135             args->log_file = optarg;
136             break;
137         case 'd':
138             args->detach = TRUE;
139             break;
140         case TTT_ARGS_VAL_PID_FILE:
141             args->pid_file = optarg;
142             break;
143         case TTT_ARGS_VAL_VERSION:
144             printf ("%s\n", VERSION);
145             exit (0);
146             break;
147
148         case TTT_ARGS_VAL_HELP:
149             ttt_args_help (argv0);
150             exit (0);
151             break;
152
153         case '?':
154             ttt_args_help (argv0);
155             exit (1);
156             break;
157
158         default:
159             fprintf (stderr, "Unhandled option `%c'\n", c);
160             break;
161         }
162     }
163
164     free (argv0_copy);
165
166     return 0;
167 }