Debian Codegraph @

ax25/axwrapper.c

raw
1 /*
2 * axwrapper.c - Convert end-of-line sequences for non-AX.25 aware programs -
3 * version 1.1
4 *
5 * Copyright (C) 1996-2001 Tomi Manninen, OH2BNS, <[email protected]>.
6 *
7 * Compile with: gcc -Wall -O6 -o axwrapper axwrapper.c
8 *
9 * Usage: axwrapper [-p <paclen>] <filename> <argv[0]> ...
10 *
11 * Axwrapper first creates a pipe and then forks and execs the program
12 * <filename> with arguments given at the axwrapper command line.
13 * The parent process then sits and waits for any I/O to and from the
14 * user and converts any <CR>'s from user to <NL>'s and any <NL>'s from
15 * the program to <CR>'s. This is useful when starting non-AX.25 aware
16 * programs from ax25d.
17 *
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
32 * 02110-1301, USA.
33 *
34 */
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <fcntl.h>
41 #include <sys/time.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #define FLUSHTIMEOUT 500000 /* 0.5 sec */
46
47 #define PERROR(s) fprintf(stderr, "*** %s: %s\r", (s), strerror(errno))
48 #define USAGE() fputs("Usage: axwrapper [-p <paclen>] <filename> <argv[0]> ...\r", stderr)
49
50 static void convert_cr_lf(unsigned char *buf, int len)
51 {
52 while (len-- > 0) {
53 if (*buf == '\r') *buf = '\n';
54 buf++;
55 }
56 }
57
58 static void convert_lf_cr(unsigned char *buf, int len)
59 {
60 while (len-- > 0) {
61 if (*buf == '\n') *buf = '\r';
62 buf++;
63 }
64 }
65
66 int main(int argc, char **argv)
67 {
68 unsigned char buf[4096];
69 char *stdoutbuf;
70 int pipe_in[2];
71 int pipe_out[2];
72 int pipe_err[2];
73 int len;
74 int pid;
75 int paclen = 256;
76 fd_set fdset;
77 struct timeval tv;
78
79 while ((len = getopt(argc, argv, "p:")) != -1) {
80 switch (len) {
81 case 'p':
82 paclen = atoi(optarg);
83 break;
84 case ':':
85 case '?':
86 USAGE();
87 exit(1);
88 }
89 }
90
91 if (argc - optind < 2) {
92 USAGE();
93 exit(1);
94 }
95
96 stdoutbuf = malloc(paclen);
97 if (stdoutbuf == NULL) {
98 PERROR("axwrapper: malloc");
99 exit(1);
100 }
101
102 setvbuf(stdout, stdoutbuf, _IOFBF, paclen);
103
104 if (pipe(pipe_in) == -1) {
105 PERROR("axwrapper: pipe");
106 exit(1);
107 }
108 if (pipe(pipe_out) == -1) {
109 PERROR("axwrapper: pipe");
110 exit(1);
111 }
112 if (pipe(pipe_err) == -1) {
113 PERROR("axwrapper: pipe");
114 exit(1);
115 }
116
117 signal(SIGCHLD, SIG_IGN);
118
119 pid = fork();
120
121 if (pid == -1) {
122 /* fork error */
123 PERROR("axwrapper: fork");
124 exit(1);
125 }
126
127 if (pid == 0) {
128 /* child */
129 dup2(pipe_in[0], STDIN_FILENO);
130 close(pipe_in[1]);
131
132 dup2(pipe_out[1], STDOUT_FILENO);
133 close(pipe_out[0]);
134
135 dup2(pipe_err[1], STDERR_FILENO);
136 close(pipe_err[0]);
137
138 execve(argv[optind], argv + optind + 1, NULL);
139
140 /* execve() should not return */
141 perror("axwrapper: execve");
142 exit(1);
143 }
144
145 /* parent */
146
147 close(pipe_in[0]);
148 close(pipe_out[1]);
149 close(pipe_err[1]);
150
151 if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1) {
152 perror("axwrapper: fcntl");
153 exit(1);
154 }
155 if (fcntl(pipe_out[0], F_SETFL, O_NONBLOCK) == -1) {
156 perror("axwrapper: fcntl");
157 exit(1);
158 }
159 if (fcntl(pipe_err[0], F_SETFL, O_NONBLOCK) == -1) {
160 perror("axwrapper: fcntl");
161 exit(1);
162 }
163
164 while (1) {
165 FD_ZERO(&fdset);
166 int maxfd = -1;
167
168 FD_SET(STDIN_FILENO, &fdset);
169 if (STDIN_FILENO > maxfd)
170 maxfd = STDIN_FILENO + 1;
171 FD_SET(pipe_out[0], &fdset);
172 if (pipe_out[0] > maxfd)
173 maxfd = pipe_out[0] + 1;
174 FD_SET(pipe_err[0], &fdset);
175 if (pipe_err[0] > maxfd)
176 maxfd = pipe_err[0] + 1;
177
178 tv.tv_sec = 0;
179 tv.tv_usec = FLUSHTIMEOUT;
180
181 len = select(maxfd, &fdset, NULL, NULL, &tv);
182 if (len == -1) {
183 if (errno == EINTR)
184 continue;
185
186 perror("axwrapper: select");
187 exit(1);
188 }
189 if (len == 0) {
190 fflush(stdout);
191 }
192
193 if (FD_ISSET(STDIN_FILENO, &fdset)) {
194 len = read(STDIN_FILENO, buf, sizeof(buf));
195 if (len < 0 && errno != EAGAIN) {
196 perror("axwrapper: read");
197 break;
198 }
199 if (len == 0)
200 break;
201 convert_cr_lf(buf, len);
202 write(pipe_in[1], buf, len);
203 }
204 if (FD_ISSET(pipe_out[0], &fdset)) {
205 len = read(pipe_out[0], buf, sizeof(buf));
206 if (len < 0 && errno != EAGAIN) {
207 perror("axwrapper: read");
208 break;
209 }
210 if (len == 0)
211 break;
212 convert_lf_cr(buf, len);
213 fwrite(buf, 1, len, stdout);
214 }
215 if (FD_ISSET(pipe_err[0], &fdset)) {
216 len = read(pipe_err[0], buf, sizeof(buf));
217 if (len < 0 && errno != EAGAIN) {
218 perror("axwrapper: read");
219 break;
220 }
221 if (len == 0)
222 break;
223 convert_lf_cr(buf, len);
224 fwrite(buf, 1, len, stderr);
225 }
226 }
227
228 kill(pid, SIGTERM);
229 close(pipe_in[1]);
230 close(pipe_out[0]);
231 close(pipe_err[0]);
232 return 0;
233 }