New audio subsystem.
[jog] / main.c
1 /* -*-c-*-
2 *
3 * $Id: main.c,v 1.3 2002/02/02 19:21:53 mdw Exp $
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
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: main.c,v $
32 * Revision 1.3 2002/02/02 19:21:53 mdw
33 * New audio subsystem.
34 *
35 * Revision 1.2 2002/01/30 09:27:55 mdw
36 * Parse tracing options on the command-line.
37 *
38 * Revision 1.1 2002/01/25 19:34:45 mdw
39 * Initial revision
40 *
41 */
42
43 /*----- Header files ------------------------------------------------------*/
44
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54
55 #include <sys/time.h>
56 #include <unistd.h>
57
58 #include <mLib/alloc.h>
59 #include <mLib/mdwopt.h>
60 #include <mLib/quis.h>
61 #include <mLib/report.h>
62 #include <mLib/trace.h>
63
64 #include "au.h"
65 #include "err.h"
66 #include "jog.h"
67 #include "rxglue.h"
68 #include "txport.h"
69 #include "tx-serial-unix.h"
70
71 /*----- Shutdown stuff ----------------------------------------------------*/
72
73 static int sigtab[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, -1 };
74
75 static void tidy(void)
76 {
77 txsu_shutdown();
78 au_shutdown();
79 }
80
81 static void sigtidy(int sig)
82 {
83 tidy();
84 signal(sig, SIG_DFL);
85 raise(sig);
86 }
87
88 /*----- Help functions ----------------------------------------------------*/
89
90 static void usage(FILE *fp)
91 {
92 pquis(fp, "\
93 Usage: $ [-t TRANSPORT] [-f FILE] [-c CONFIG] [-a AUDIR] [-x SIZE]\n\
94 SCRIPT ARG...\n\
95 ");
96 }
97
98 static void version(FILE *fp)
99 {
100 pquis(fp, "$, version " VERSION "\n");
101 }
102
103 static void help(FILE *fp)
104 {
105 version(fp);
106 fputc('\n', fp);
107 usage(fp);
108 fputs("\n\
109 Options provided:\n\
110 \n\
111 -h, --help Print this help message.\n\
112 -v, --version Show the version number.\n\
113 -u, --usage Show terse usage summary.\n\
114 \n\
115 -t, --transport=NAME Use transport type NAME.\n\
116 -f, --tx-file=FILE Communicate using the named FILE.\n\
117 -c, --tx-config=CONFIG Use CONFIG as transport configuration.\n\
118 \n\
119 -a, --audio=DIR Set directory containing audio samples.\n\
120 -x, --cache-max=SIZE Maximum size for audio sample cache.\n\
121 ",
122 fp);
123 }
124
125 /*----- Main code ---------------------------------------------------------*/
126
127 int main(int argc, char *argv[])
128 {
129 unsigned f = 0;
130 int rc = 0;
131 int i;
132 const char *audir = 0;
133 size_t aumax = AU_CACHEMAX;
134
135 #define f_bogus 1u
136
137 ego(argv[0]);
138 atexit(tidy);
139 for (i = 0; sigtab[i] >= 0; i++)
140 signal(sigtab[i], sigtidy);
141
142 err_init();
143 rx_init();
144 T( trace_on(stderr, 0u); )
145 if ((txname = getenv("JOGTX")) != 0)
146 ;
147 else
148 txname = txlist->name;
149
150 for (;;) {
151 static const struct option opt[] = {
152
153 /* --- Standard help options --- */
154
155 { "help", 0, 0, 'h' },
156 { "version", 0, 0, 'v' },
157 { "usage", 0, 0, 'u' },
158
159 /* --- Transport configuration stuff --- */
160
161 { "transport", OPTF_ARGREQ, 0, 't' },
162 { "transport-config",
163 OPTF_ARGREQ, 0, 'c' },
164 { "tx-config", OPTF_ARGREQ, 0, 'c' },
165 { "txconfig", OPTF_ARGREQ, 0, 'c' },
166 { "config", OPTF_ARGREQ, 0, 'c' },
167 { "transport-file",
168 OPTF_ARGREQ, 0, 'f' },
169 { "tx-file", OPTF_ARGREQ, 0, 'f' },
170 { "txfile", OPTF_ARGREQ, 0, 'f' },
171 { "file", OPTF_ARGREQ, 0, 'f' },
172
173 /* --- Audio configuration stuff --- */
174
175 { "audio-directory",
176 OPTF_ARGREQ, 0, 'a' },
177 { "audio-cache-max",
178 OPTF_ARGREQ, 0, 'x' },
179 { "cache-max",
180 OPTF_ARGREQ, 0, 'x' },
181
182 /* --- Debugging stuff --- */
183
184 #ifndef NTRACE
185 { "trace", OPTF_ARGREQ, 0, 'T' },
186 #endif
187
188 /* --- End marker --- */
189
190 { 0, 0, 0, 0 }
191 };
192
193 #ifndef NTRACE
194 static const trace_opt tropt[] = {
195 { 'x', T_TX, "transport layer" },
196 { 's', T_TXSYS, "low-level transport" },
197 { 'a', T_AU, "audio subsystem" },
198 { 'y', T_AUSYS, "system-specific audio" },
199 { 'A', T_ALL, "all of the above" },
200 { 0, 0, 0 }
201 };
202 #endif
203
204 i = mdwopt(argc, argv, "hvu" "t:c:f:" "a:x:" T("T:"), opt, 0, 0, 0);
205 if (i < 0)
206 break;
207
208 switch (i) {
209
210 /* --- Standard help options --- */
211
212 case 'h':
213 help(stdout);
214 exit(0);
215 case 'v':
216 version(stdout);
217 exit(0);
218 case 'u':
219 usage(stdout);
220 exit(0);
221
222 /* --- Transport configuration stuff --- */
223
224 case 't':
225 txname = optarg;
226 break;
227 case 'c':
228 txconf = optarg;
229 break;
230 case 'f':
231 txfile = optarg;
232 break;
233
234 /* --- Audio configuration stuff --- */
235
236 case 'a':
237 audir = optarg;
238 break;
239 case 'x':
240 aumax = strtoul(optarg, 0, 0);
241 break;
242
243 /* --- Tracing --- */
244
245 #ifndef NTRACE
246 case 'T':
247 trace_level(traceopt(tropt, optarg, tracing(), 0));
248 break;
249 #endif
250
251 /* --- Errors --- */
252
253 default:
254 f |= f_bogus;
255 break;
256 }
257 }
258
259 if ((f & f_bogus) || optind > argc - 1) {
260 usage(stderr);
261 exit(EXIT_FAILURE);
262 }
263
264 au_init(audir, aumax);
265 rc = rx_runfile(argv[optind],
266 argc - optind - 1, (const char *const *)argv + optind + 1);
267 return (rc ? EXIT_FAILURE : 0);
268 }
269
270 /*----- That's all, folks -------------------------------------------------*/