Parse tracing options on the command-line.
[jog] / main.c
CommitLineData
2ec1e693 1/* -*-c-*-
2 *
61158d20 3 * $Id: main.c,v 1.2 2002/01/30 09:27:55 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
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: main.c,v $
61158d20 32 * Revision 1.2 2002/01/30 09:27:55 mdw
33 * Parse tracing options on the command-line.
34 *
2ec1e693 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"
61158d20 58#include "jog.h"
2ec1e693 59#include "rxglue.h"
60#include "txport.h"
61#include "tx-serial-unix.h"
62
63/*----- Shutdown stuff ----------------------------------------------------*/
64
65static int sigtab[] = { SIGINT, SIGQUIT, SIGTERM, SIGHUP, -1 };
66
67static void tidy(void)
68{
69 txsu_shutdown();
70}
71
72static void sigtidy(int sig)
73{
74 tidy();
75 signal(sig, SIG_DFL);
76 raise(sig);
77}
78
79/*----- Help functions ----------------------------------------------------*/
80
81static void usage(FILE *fp)
82{
61158d20 83 pquis(fp, "\
84Usage: $ [-t TRANSPORT] [-f FILE] [-c CONFIG] SCRIPT ARG...\n\
85");
2ec1e693 86}
87
88static void version(FILE *fp)
89{
90 pquis(fp, "$, version " VERSION "\n");
91}
92
93static void help(FILE *fp)
94{
95 version(fp);
96 fputc('\n', fp);
97 usage(fp);
98 fputs("\n\
99Options 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
114int 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();
61158d20 129 T( trace_on(stderr, 0u); )
2ec1e693 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
61158d20 158 /* --- Debugging stuff --- */
159
160#ifndef NTRACE
161 { "trace", OPTF_ARGREQ, 0, 'T' },
162#endif
163
2ec1e693 164 /* --- End marker --- */
165
166 { 0, 0, 0, 0 }
167 };
168
61158d20 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);
2ec1e693 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
61158d20 208 /* --- Tracing --- */
209
210#ifndef NTRACE
211 case 'T':
212 trace_level(traceopt(tropt, optarg, tracing(), 0));
213 break;
214#endif
215
2ec1e693 216 /* --- Errors --- */
217
218 default:
219 f |= f_bogus;
220 break;
221 }
222 }
223
61158d20 224 if ((f & f_bogus) || optind > argc - 1) {
2ec1e693 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 -------------------------------------------------*/