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