chiark / gitweb /
f78746d9ede360c9961110b8b2f87470aa9c2eab
[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 /*----- Header files ------------------------------------------------------*/
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #endif
34
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
50 #include "au.h"
51 #include "err.h"
52 #include "jog.h"
53 #include "rxglue.h"
54 #include "txport.h"
55 #include "tx-serial-unix.h"
56
57 /*----- Shutdown stuff ----------------------------------------------------*/
58
59 static int sigtab[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, -1 };
60
61 static void tidy(void)
62 {
63   txsu_shutdown();
64   au_shutdown();
65 }
66
67 static void sigtidy(int sig)
68 {
69   tidy();
70   signal(sig, SIG_DFL);
71   raise(sig);
72 }
73
74 /*----- Help functions ----------------------------------------------------*/
75
76 static void usage(FILE *fp)
77 {
78   pquis(fp, "\
79 Usage: $ [-t TRANSPORT] [-f FILE] [-c CONFIG] [-a AUDIR] [-x SIZE]\n\
80         SCRIPT ARG...\n\
81 ");
82 }
83
84 static void version(FILE *fp)
85 {
86   pquis(fp, "$, version " VERSION "\n");
87 }
88
89 static void help(FILE *fp)
90 {
91   version(fp);
92   fputc('\n', fp);
93   usage(fp);
94   fputs("\n\
95 Options 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\
104 \n\
105 -a, --audio=DIR         Set directory containing audio samples.\n\
106 -x, --cache-max=SIZE    Maximum size for audio sample cache.\n\
107 ",
108         fp);
109 }
110
111 /*----- Main code ---------------------------------------------------------*/
112
113 int main(int argc, char *argv[])
114 {
115   unsigned f = 0;
116   int rc = 0;
117   int i;
118   const char *audir = 0;
119   size_t aumax = AU_CACHEMAX;
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();
130   T( trace_on(stderr, 0u); )
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
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
168       /* --- Debugging stuff --- */
169
170 #ifndef NTRACE
171       { "trace",        OPTF_ARGREQ,    0,      'T' },
172 #endif
173
174       /* --- End marker --- */
175
176       { 0,              0,              0,      0 }
177     };
178
179 #ifndef NTRACE
180     static const trace_opt tropt[] = {
181       { 'x',    T_TX,           "transport layer" },
182       { 's',    T_TXSYS,        "low-level transport" },
183       { 'a',    T_AU,           "audio subsystem" },
184       { 'y',    T_AUSYS,        "system-specific audio" },
185       { 'A',    T_ALL,          "all of the above" },
186       { 0,      0,              0 }
187     };
188 #endif
189
190     i = mdwopt(argc, argv, "hvu" "t:c:f:" "a:x:" T("T:"), opt, 0, 0, 0);
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
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
229       /* --- Tracing --- */
230
231 #ifndef NTRACE
232       case 'T':
233         trace_level(traceopt(tropt, optarg, tracing(), 0));
234         break;
235 #endif
236
237       /* --- Errors --- */
238
239       default:
240         f |= f_bogus;
241         break;
242     }
243   }
244
245   if ((f & f_bogus) || optind > argc - 1) {
246     usage(stderr);
247     exit(EXIT_FAILURE);
248   }
249
250   au_init(audir, aumax);
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 -------------------------------------------------*/