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