Debian Codegraph @

ax25/axgetput/axgetput.c

raw
1 /*
2 * This is axgetput
3 *
4 * This shell utility is for up/downloading files via your ax25 unix login
5 * session which is managed by axspawn.
6 * you need a //BIN compatible axspawn, which has the functionality of making
7 * the stream 8bit compatible (normally the EOL-conversion <LF> <-> <CR>
8 * prevents this). see:
9 * http://x-berg.in-berlin.de/cgi-bin/viewcvs.cgi/ampr/patches/linux-ax25/
10 *
11 * (c) 2002 Thomas Osterried DL9SAU <[email protected]>
12 * License: GPL. See http://www.fsf.org/
13 * Sources: http://x-berg.in-berlin.de/cgi-bin/viewcvs.cgi/ampr/axgetput/
14 *
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20 #include <scm-version.h>
21
22 #include "includes.h"
23
24 #include "axgetput.h"
25 #include "util.h"
26 #include "proto_bin.h"
27
28 int fdin;
29 int fdout = 1;
30 int fderr = 2;
31 int fdout_is_pipe;
32 int fdin_is_pipe;
33
34 char myname[PATH_MAX+1];
35 char filename[PATH_MAX+1];
36 char err_msg[2048];
37
38 int is_stream;
39 int mode;
40 int do_crc_only;
41
42 char c_eol = '\n';
43
44 unsigned int BLOCKSIZ = BLOCKSIZ_DEFAULT;
45
46 char *send_on_signal;
47
48 static struct termios prev_termios;
49 static int prev_termios_stored;
50 static mode_t mode_tty;
51
52 #ifndef MYNAME
53 #define MYNAME "axgetput"
54 #endif
55
56 /*---------------------------------------------------------------------------*/
57
58 static void set_tty_flags(void)
59 {
60 struct termios termios;
61 struct stat statbuf;
62
63 if (fdin_is_pipe)
64 return;
65 /* mesg no */
66 if (!fstat(fdin, &statbuf)) {
67 /* save old mode */
68 mode_tty = statbuf.st_mode;
69 fchmod(fdin, 0600);
70 }
71 /* make tty 8bit clean */
72 if (tcgetattr(fdin, &prev_termios) != -1)
73 prev_termios_stored = 1;
74 memset(&termios, 0, sizeof(termios));
75 termios.c_iflag = IGNBRK | IGNPAR;
76 termios.c_oflag = 0;
77 termios.c_cflag = CBAUD | CS8 | CREAD | CLOCAL;
78 termios.c_cflag = ~(CSTOPB|PARENB|PARODD|HUPCL);
79 termios.c_lflag = 0;
80 termios.c_cc[VMIN] = 1;
81 termios.c_cc[VTIME] = 0;
82 termios.c_cc[VSTART] = -1;
83 termios.c_cc[VSTOP] = -1;
84 tcsetattr(fdin, TCSANOW, &termios);
85 }
86
87 /*---------------------------------------------------------------------------*/
88
89 static void restore_tty_flags(void)
90 {
91 if (fdin_is_pipe)
92 return;
93 if (prev_termios_stored)
94 tcsetattr(fdin, TCSANOW, &prev_termios);
95 if (mode_tty)
96 fchmod(fdin, mode_tty);
97 }
98
99 /*---------------------------------------------------------------------------*/
100
101 static void eol_convention(int state_should)
102 {
103 /* need patched axspawn */
104 #define BIN_ON "//BIN ON\r"
105 #define BIN_OFF "//BIN OFF\r"
106 static int state_is;
107
108 /* already in correct state? */
109 if ((state_is && state_should) || (!state_is && !state_should))
110 return;
111
112 sleep(1);
113
114 if (state_should) {
115 write(fderr, BIN_ON, strlen(BIN_ON));
116 c_eol = '\r';
117 } else {
118 write(fderr, BIN_OFF, strlen(BIN_OFF));
119 c_eol = '\n';
120 }
121 state_is = state_should;
122
123 sleep(1);
124 }
125
126 /*---------------------------------------------------------------------------*/
127
128 static void restore_defaults(void)
129 {
130 eol_convention(0);
131 restore_tty_flags();
132 }
133
134 /*---------------------------------------------------------------------------*/
135
136 static void signal_handler(int sig)
137 {
138 if (send_on_signal)
139 secure_write(fdout, send_on_signal, strlen(send_on_signal));
140 restore_defaults();
141 if (*err_msg) {
142 fputs(err_msg, stderr);
143 }
144 fprintf(stderr, "Died by signal %d.\n", sig);
145 exit(sig);
146 }
147
148 /*---------------------------------------------------------------------------*/
149
150 static void do_version(void)
151 {
152 fprintf(stderr, MYNAME " " FULL_VER "\n");
153 fprintf(stderr, " (c) 2002 Thomas Osterried <[email protected]>\n");
154 fprintf(stderr, " License: GPL. See http://www.fsf.org/\n");
155 fprintf(stderr, " Sources: http://x-berg.in-berlin.de/cgi-bin/viewcvs.cgi/ampr/axgetput/\n");
156 }
157
158 /*---------------------------------------------------------------------------*/
159
160 static void usage(int all) {
161 fprintf(stderr, "usage: %s ", myname);
162 if (mode % 2) {
163 fprintf(stderr, "[-ivh] [filename]\n");
164 } else {
165 fprintf(stderr, "[-isvh] [-b <blocksize>] [filename]\n");
166 }
167 fprintf(stderr, " -h prints detailed help\n");
168 fprintf(stderr, " -i computes checksum only\n");
169 fprintf(stderr, " -v prints version and exits\n");
170 if (!all)
171 return;
172 if (mode % 2) {
173 fprintf(stderr, " filename is usually got from the remote side by the protocol\n");
174 fprintf(stderr, " but can be forced if you like to ignore this.\n");
175 fprintf(stderr, " filename should be omitted if output is sent to a pipe\n.");
176 } else {
177 fprintf(stderr, " -b value is the blocksize (framelen) of the transmitted data\n");
178 fprintf(stderr, " default %d, which is a useful choice for ampr ax25.\n", BLOCKSIZ_DEFAULT);
179 fprintf(stderr, " -s indicates a stream with unknown size.\n");
180 fprintf(stderr, " otherwise, the data will be read to memory until EOF.\n");
181 fprintf(stderr, " -s is only available if stdin is a pipe\n");
182 fprintf(stderr, " if filename specified in filter, the given name will be suggested instead.\n");
183 fprintf(stderr, " filename may be omitted if used as filter.\n");
184 }
185 fputc('\n', stderr);
186 fprintf(stderr, "Tips: - compressed download:\n");
187 fprintf(stderr, " gzip -c foo.txt | bget foo.txt.gz\n");
188 fprintf(stderr, " tar cjf - ~/foo ~/bar/ | bget my_data.tar.bz2\n");
189 fputc('\n', stderr);
190 fprintf(stderr, "Other protocols:\n");
191 fprintf(stderr, " bget / bput: receive / send with #BIN# protocol\n");
192 fprintf(stderr, " yget / yput: receive / send with yapp\n");
193 fprintf(stderr, " rget / rput: receive / send with didadit\n");
194 fprintf(stderr, "These are (sym)links to one program: " MYNAME "\n");
195 }
196
197
198 /*---------------------------------------------------------------------------*/
199
200 /* not implemented */
201 static int yput(void) { strcpy(err_msg, "yapp: not implementet yet\n"); return 1; }
202 static int yget(void) { strcpy(err_msg, "yapp: not implementet yet\n"); return 1; }
203 static int rget(void) { strcpy(err_msg, "yapp: not implementet yet\n"); return 1; }
204 static int rput(void) { strcpy(err_msg, "yapp: not implementet yet\n"); return 1; }
205
206 /*---------------------------------------------------------------------------*/
207
208 int main(int argc, char *argv[])
209 {
210
211 int len;
212 int ret = 0;
213 int c;
214 char *p;
215
216 /* determine what to so in the way how we are called */
217 if ((p = strrchr(argv[0], '/')))
218 p++; /* skip '/' */
219 else
220 p = argv[0];
221 len = strlen(p);
222 if (len > sizeof(myname)-1)
223 len = sizeof(myname)-1;
224 strncpy(myname, p, len);
225 myname[len] = 0;
226 strlwc(myname);
227
228 fdin_is_pipe = (isatty(fdin) ? 0 : 1);
229 fdout_is_pipe = (isatty(fdout) ? 0 : 1);
230
231 if (fdin_is_pipe && fdout_is_pipe) {
232 fprintf(stderr, "error: cannot work in between two pipes\n");
233 exit(1);
234 }
235
236 *filename = 0;
237 *err_msg = 0;
238
239 if (!strcmp(myname, "bput"))
240 mode = RECV_BIN;
241 else if (!strcmp(myname, "bget"))
242 mode = SEND_BIN;
243 else if (!strcmp(myname, "yput"))
244 mode = RECV_YAPP;
245 else if (!strcmp(myname, "yget"))
246 mode = SEND_YAPP;
247 else if (!strcmp(myname, "rput"))
248 mode = RECV_DIDADIT;
249 else if (!strcmp(myname, "rget"))
250 mode = SEND_DIDADIT;
251
252 if (mode % 2) {
253 if (fdin_is_pipe) {
254 fprintf(stderr, "error: error: stdin must be a tty\n");
255 exit(1);
256 }
257 } else {
258 if (fdout_is_pipe) {
259 fprintf(stderr, "error: stdout must be a tty\n");
260 exit(1);
261 }
262 }
263
264 signal(SIGHUP, signal_handler);
265 signal(SIGTERM, signal_handler);
266 signal(SIGINT, signal_handler);
267
268 /* for difference betreen "bput -f file" and "bget file" */
269 #define get_filename(f) { \
270 if (!strcmp(f, "-")) { \
271 if (mode % 2) \
272 fdin_is_pipe = 1; \
273 else \
274 fdout_is_pipe = 1; \
275 } else { \
276 strncpy(filename, f, sizeof(filename)-1); \
277 filename[sizeof(filename)-1] = 0; \
278 if (mode % 2) { \
279 if (fdin_is_pipe) \
280 fdin_is_pipe = 0; \
281 } else { \
282 if (fdout_is_pipe) \
283 fdout_is_pipe = 0; \
284 } \
285 } \
286 }
287
288 while ((c = getopt(argc, argv, (mode % 2) ? "ivh?" : "b:isvh?")) != EOF) {
289 switch (c) {
290 case 'b':
291 if (((BLOCKSIZ = (unsigned ) atoi(optarg)) < BLOCKSIZ_MIN) || BLOCKSIZ > BLOCKSIZ_MAX) {
292 fprintf(stderr, "error: invalid blocksize: %d\n", BLOCKSIZ);
293 fprintf(stderr, " blocksize must be in the range %d <= x <= %d. a value\n", BLOCKSIZ_MIN, BLOCKSIZ_MAX);
294 fprintf(stderr, " of %d (default) is suggested, because it fits in an ax25 frame.\n", BLOCKSIZ_DEFAULT);
295 exit(1);
296 }
297 break;
298 case 'i':
299 do_crc_only = 1;
300 break;
301 case 's':
302 is_stream = 1;
303 break;
304 case 'v':
305 do_version();
306 exit(0);
307 break;
308 case 'h':
309 case '?':
310 usage((c == 'h'));
311 exit(0);
312 break;
313 }
314 }
315
316 if (mode == 0) {
317 usage(1);
318 exit(0);
319 }
320
321 if (optind < argc) {
322 get_filename(argv[optind]);
323 optind++;
324 }
325 if (optind < argc) {
326 usage(0);
327 exit(1);
328 }
329
330 if (is_stream && !fdin_is_pipe) {
331 fprintf(stderr, "error: -s is only available in a pipe\n");
332 exit(1);
333 }
334
335 if (do_crc_only)
336 goto skiped_crc_only_tag_1;
337
338 if (mode % 2) {
339 if (fdin_is_pipe) {
340 fprintf(stderr, "error: error: stdin must be a tty.\n");
341 exit(1);
342 }
343 if (fdout_is_pipe && *filename) {
344 fprintf(stderr, "error: filename in a pipe does not make sense.\n");
345 exit(1);
346 }
347 } else {
348 if (fdout_is_pipe) {
349 fprintf(stderr, "error: stdout must be a tty.\n");
350 exit(1);
351 }
352 if (!fdin_is_pipe && !*filename) {
353 fprintf(stderr, "error: no file to send.\n");
354 exit(1);
355 }
356 }
357
358 signal(SIGQUIT, signal_handler);
359
360 set_tty_flags();
361 eol_convention(1);
362
363 skiped_crc_only_tag_1:
364
365 switch (mode) {
366 case RECV_BIN:
367 if (do_crc_only)
368 ret = bget();
369 else
370 ret = bput();
371 break;
372 case SEND_BIN:
373 ret = bget();
374 break;
375 case RECV_YAPP:
376 ret = yput();
377 break;
378 case SEND_YAPP:
379 ret = yget();
380 break;
381 case RECV_DIDADIT:
382 ret = rput();
383 break;
384 case SEND_DIDADIT:
385 ret = rget();
386 break;
387 }
388
389 restore_defaults();
390 if (*err_msg) {
391 fputs(err_msg, stderr);
392 }
393 exit(ret);
394
395 return 0;
396
397 }