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