Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / examples / isdn / contrib / isdntelmux.c
1 /*
2  * Copyright (c) 1999 Michael Reifenberger (Michael@Reifenberger.com). 
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *---------------------------------------------------------------------------
27  *
28  *      i4btemux - record while playing
29  *      ===============================
30  *
31  * $FreeBSD: src/share/examples/isdn/contrib/isdntelmux.c,v 1.1.2.1 2001/08/10 14:59:48 obrien Exp $
32  *
33  *----------------------------------------------------------------------------*/
34
35 #include<stdio.h>
36 #include<stdarg.h>
37 #include<signal.h>
38 #include<errno.h>
39 #include<string.h>
40 #include<stdlib.h>
41 #include<unistd.h>
42 #include<fcntl.h>
43 #include<ctype.h>
44 #include<sys/stat.h>
45 #include<sys/wait.h>
46 #include<sys/ioctl.h>
47 #include<sys/types.h>
48 #include<sys/time.h>
49 #include<sys/param.h>
50 #include<machine/i4b_tel_ioctl.h>
51
52 // DECL DEFS
53 #define BUFLEN 2048
54 #define MAXBLOCKS_DEFAULT 23
55
56 // DECL VARS
57 int ibytes = 0;
58 int obytes = 0;
59 int maxbytes = (BUFLEN * MAXBLOCKS_DEFAULT);
60
61 int xfd = -1, xact = 0;
62 int ifd = -1, iact = 0;
63 int ofd = -1;
64 FILE *dfp = NULL;
65 int opt_dbg = 0;
66 int maxfd = 0;
67 fd_set set;
68 struct timeval timeout;
69 char nambuf[PATH_MAX];
70 int ch;
71
72 // DECL FUNC
73 void ifd_hdlr( void);
74 void xfd_hdlr( void);
75 void usage( void);
76 void dbg( char *fmt, ... );
77
78 // DEF FUNC
79 int main (int argc, char **argv) {
80   int dummy;
81   int x = -1;
82
83   dfp = stderr;
84   while( ( ch = getopt( argc, argv, "x:i:o:b:D:")) != -1 ){
85     switch(ch){
86       case 'b':
87         x = atoi(optarg);
88         maxbytes = x * BUFLEN;
89         break;
90       case 'i':
91         ifd = open( optarg, O_RDONLY );
92         iact = 1;
93         break;
94       case 'o':
95         ofd = open( optarg, O_WRONLY|O_TRUNC|O_CREAT );
96         break;
97       case 'x':
98         xfd = open( optarg, O_RDWR );
99         xact = 1;
100         break;
101       case 'D':
102         opt_dbg = 1;
103         if( (dfp = fopen( optarg, "w" )) < 0) {
104           dfp = stderr;
105           dbg("Err for opening %s\n", optarg);
106           exit(1);
107         }
108         break;
109       case '?':
110       default:
111         usage();
112         break;
113     }
114   }
115   if( ( xfd < 0 ) || ( ifd < 0 ) || ( ofd < 0 ) ) {
116     dbg("Err opening one ore more Files.\n");
117     dbg("xfd: %d, ifd: %d, ofd: %d\n", xfd, ifd, ofd );
118     usage();
119   }
120
121   if((x = ioctl(xfd, I4B_TEL_EMPTYINPUTQUEUE, &dummy)) < 0){
122     dbg("Err I4B_TEL_EMPTYINPUTQUEUE\n");
123   }
124
125   while( (iact == 1) || ( (obytes < maxbytes) && (xact == 1) ) ){
126     FD_ZERO( &set);
127     if( iact == 1){
128       FD_SET( ifd, &set);
129       if( ifd > maxfd)
130         maxfd = ifd; 
131       dbg("FSET ifd\n");
132     }
133     if( xact == 1){
134       FD_SET( xfd, &set);
135       if( xfd > maxfd)
136         maxfd = xfd; 
137       dbg("FSET xfd\n");
138     }
139     x=select( maxfd+1, &set, NULL, NULL, NULL);
140     if( x > 0){
141       if( (iact == 1) && FD_ISSET( ifd, &set) ){
142         ifd_hdlr();
143       }
144       if( (xact == 1) && FD_ISSET( xfd, &set) ){
145         xfd_hdlr();
146       }
147     }
148   }
149   dbg("exit0\n");
150   return(0);
151 }
152
153 void ifd_hdlr( void) {
154   int x;
155   unsigned char buf[BUFLEN];
156
157   x = read( ifd, buf, BUFLEN); 
158   dbg("ifd read %d bytes\n", x);
159   if( x > 0 ){
160     write( xfd, buf, x);
161     ibytes += x;
162     dbg("xfd %d bytes written to %d\n", x, ibytes);
163   } else {
164     iact = 0;
165   }
166 }
167
168 void xfd_hdlr( void) {
169   int x;
170   unsigned char buf[BUFLEN];
171
172   x = read( xfd, buf, BUFLEN);
173   dbg("xfd read %d bytes\n", x);
174   if( x > 0){
175     write( ofd, buf, x);
176     obytes += x;
177     dbg("ofd %d bytes written to %d\n", x, obytes);
178   } else {
179     xact = 0;
180   }
181 }
182
183 void usage( void) {
184   fprintf(dfp, "isdntelmux V.1\n");
185   fprintf(dfp, "usage: isdntelmux -x device -i ifile -o ofile [-b blocks]\n");
186   exit(1);
187 }
188
189 void dbg( char *fmt, ... ) {
190   va_list ap;
191
192   if( opt_dbg == 0 )
193     return;
194   va_start( ap, fmt );
195   vfprintf( dfp, fmt, ap);
196   va_end(ap);
197 }