infra: Clean up project setup
[jog] / main.c
CommitLineData
2ec1e693 1/* -*-c-*-
2 *
e9060e7e 3 * $Id: main.c,v 1.3 2002/02/02 19:21:53 mdw Exp $
2ec1e693 4 *
5 * Main program
6 *
7 * (c) 2001 Mark Wooding
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Jog: Programming for a jogging machine.
13 *
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
2ec1e693 29/*----- Header files ------------------------------------------------------*/
30
e9060e7e 31#ifdef HAVE_CONFIG_H
32# include "config.h"
33#endif
34
2ec1e693 35#include <signal.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <time.h>
40
41#include <sys/time.h>
42#include <unistd.h>
43
44#include <mLib/alloc.h>
45#include <mLib/mdwopt.h>
46#include <mLib/quis.h>
47#include <mLib/report.h>
48#include <mLib/trace.h>
49
e9060e7e 50#include "au.h"
2ec1e693 51#include "err.h"
61158d20 52#include "jog.h"
2ec1e693 53#include "rxglue.h"
54#include "txport.h"
55#include "tx-serial-unix.h"
56
57/*----- Shutdown stuff ----------------------------------------------------*/
58
59static int sigtab[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, -1 };
60
61static void tidy(void)
62{
63 txsu_shutdown();
e9060e7e 64 au_shutdown();
2ec1e693 65}
66
67static void sigtidy(int sig)
68{
69 tidy();
70 signal(sig, SIG_DFL);
71 raise(sig);
72}
73
74/*----- Help functions ----------------------------------------------------*/
75
76static void usage(FILE *fp)
77{
61158d20 78 pquis(fp, "\
e9060e7e 79Usage: $ [-t TRANSPORT] [-f FILE] [-c CONFIG] [-a AUDIR] [-x SIZE]\n\
80 SCRIPT ARG...\n\
61158d20 81");
2ec1e693 82}
83
84static void version(FILE *fp)
85{
86 pquis(fp, "$, version " VERSION "\n");
87}
88
89static void help(FILE *fp)
90{
91 version(fp);
92 fputc('\n', fp);
93 usage(fp);
94 fputs("\n\
95Options provided:\n\
96\n\
97-h, --help Print this help message.\n\
98-v, --version Show the version number.\n\
99-u, --usage Show terse usage summary.\n\
100\n\
101-t, --transport=NAME Use transport type NAME.\n\
102-f, --tx-file=FILE Communicate using the named FILE.\n\
103-c, --tx-config=CONFIG Use CONFIG as transport configuration.\n\
e9060e7e 104\n\
105-a, --audio=DIR Set directory containing audio samples.\n\
106-x, --cache-max=SIZE Maximum size for audio sample cache.\n\
2ec1e693 107",
108 fp);
109}
110
111/*----- Main code ---------------------------------------------------------*/
112
113int main(int argc, char *argv[])
114{
115 unsigned f = 0;
116 int rc = 0;
117 int i;
e9060e7e 118 const char *audir = 0;
119 size_t aumax = AU_CACHEMAX;
2ec1e693 120
121#define f_bogus 1u
122
123 ego(argv[0]);
124 atexit(tidy);
125 for (i = 0; sigtab[i] >= 0; i++)
126 signal(sigtab[i], sigtidy);
127
128 err_init();
129 rx_init();
61158d20 130 T( trace_on(stderr, 0u); )
2ec1e693 131 if ((txname = getenv("JOGTX")) != 0)
132 ;
133 else
134 txname = txlist->name;
135
136 for (;;) {
137 static const struct option opt[] = {
138
139 /* --- Standard help options --- */
140
141 { "help", 0, 0, 'h' },
142 { "version", 0, 0, 'v' },
143 { "usage", 0, 0, 'u' },
144
145 /* --- Transport configuration stuff --- */
146
147 { "transport", OPTF_ARGREQ, 0, 't' },
148 { "transport-config",
149 OPTF_ARGREQ, 0, 'c' },
150 { "tx-config", OPTF_ARGREQ, 0, 'c' },
151 { "txconfig", OPTF_ARGREQ, 0, 'c' },
152 { "config", OPTF_ARGREQ, 0, 'c' },
153 { "transport-file",
154 OPTF_ARGREQ, 0, 'f' },
155 { "tx-file", OPTF_ARGREQ, 0, 'f' },
156 { "txfile", OPTF_ARGREQ, 0, 'f' },
157 { "file", OPTF_ARGREQ, 0, 'f' },
158
e9060e7e 159 /* --- Audio configuration stuff --- */
160
161 { "audio-directory",
162 OPTF_ARGREQ, 0, 'a' },
163 { "audio-cache-max",
164 OPTF_ARGREQ, 0, 'x' },
165 { "cache-max",
166 OPTF_ARGREQ, 0, 'x' },
167
61158d20 168 /* --- Debugging stuff --- */
169
170#ifndef NTRACE
171 { "trace", OPTF_ARGREQ, 0, 'T' },
172#endif
173
2ec1e693 174 /* --- End marker --- */
175
176 { 0, 0, 0, 0 }
177 };
178
61158d20 179#ifndef NTRACE
180 static const trace_opt tropt[] = {
181 { 'x', T_TX, "transport layer" },
182 { 's', T_TXSYS, "low-level transport" },
e9060e7e 183 { 'a', T_AU, "audio subsystem" },
184 { 'y', T_AUSYS, "system-specific audio" },
61158d20 185 { 'A', T_ALL, "all of the above" },
186 { 0, 0, 0 }
187 };
188#endif
189
e9060e7e 190 i = mdwopt(argc, argv, "hvu" "t:c:f:" "a:x:" T("T:"), opt, 0, 0, 0);
2ec1e693 191 if (i < 0)
192 break;
193
194 switch (i) {
195
196 /* --- Standard help options --- */
197
198 case 'h':
199 help(stdout);
200 exit(0);
201 case 'v':
202 version(stdout);
203 exit(0);
204 case 'u':
205 usage(stdout);
206 exit(0);
207
208 /* --- Transport configuration stuff --- */
209
210 case 't':
211 txname = optarg;
212 break;
213 case 'c':
214 txconf = optarg;
215 break;
216 case 'f':
217 txfile = optarg;
218 break;
219
e9060e7e 220 /* --- Audio configuration stuff --- */
221
222 case 'a':
223 audir = optarg;
224 break;
225 case 'x':
226 aumax = strtoul(optarg, 0, 0);
227 break;
228
61158d20 229 /* --- Tracing --- */
230
231#ifndef NTRACE
232 case 'T':
233 trace_level(traceopt(tropt, optarg, tracing(), 0));
234 break;
235#endif
236
2ec1e693 237 /* --- Errors --- */
238
239 default:
240 f |= f_bogus;
241 break;
242 }
243 }
244
61158d20 245 if ((f & f_bogus) || optind > argc - 1) {
2ec1e693 246 usage(stderr);
247 exit(EXIT_FAILURE);
248 }
249
e9060e7e 250 au_init(audir, aumax);
2ec1e693 251 rc = rx_runfile(argv[optind],
252 argc - optind - 1, (const char *const *)argv + optind + 1);
253 return (rc ? EXIT_FAILURE : 0);
254}
255
256/*----- That's all, folks -------------------------------------------------*/