There's no point in pulling in cfs-posix-dir.c if the POSIX functions it depends...
[contiki-2.x.git] / tools / scat.c
blob41234ac81e731f84b0acdb914841a3ce207450e3
1 /*
2 * Copyright (c) 2006, Swedish Institute of Computer Science
3 * All rights reserved.
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 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
15 * written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $Id: scat.c,v 1.6 2007/05/21 15:22:45 bg- Exp $
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <sys/types.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <termios.h>
45 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
51 #include <err.h>
53 #define SLIP_END 0300
54 #define SLIP_ESC 0333
55 #define SLIP_ESC_END 0334
56 #define SLIP_ESC_ESC 0335
58 #ifndef BAUDRATE
59 #define BAUDRATE B115200
60 #endif
61 speed_t b_rate = BAUDRATE;
63 void
64 stty_telos(int fd)
66 struct termios tty;
67 int i;
69 if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
71 if(tcgetattr(fd, &tty) == -1) err(1, "tcgetattr");
73 cfmakeraw(&tty);
75 /* Blocking read. */
76 tty.c_cc[VTIME] = 0;
77 tty.c_cc[VMIN] = 1;
78 tty.c_cflag &= ~CRTSCTS;
79 tty.c_cflag &= ~HUPCL;
80 tty.c_cflag &= ~CLOCAL;
82 cfsetispeed(&tty, b_rate);
83 cfsetospeed(&tty, b_rate);
85 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
87 tty.c_cflag |= CLOCAL;
88 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
90 i = TIOCM_DTR;
91 if(ioctl(fd, TIOCMBIS, &i) == -1) err(1, "ioctl");
93 usleep(10*1000); /* Wait for hardware 10ms. */
95 /* Flush input and output buffers. */
96 if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
99 int
100 main(int argc, char **argv)
102 int c;
103 int slipfd;
104 FILE *inslip;
105 const char *siodev;
106 int baudrate = -2;
108 while ((c = getopt(argc, argv, "B:")) != -1) {
109 switch (c) {
110 case 'B':
111 baudrate = atoi(optarg);
112 break;
114 case '?':
115 case 'h':
116 default:
117 err(1, "usage: scat [-B baudrate] device-file");
118 break;
121 argc -= (optind - 1);
122 argv += (optind - 1);
124 switch (baudrate) {
125 case -2:
126 break; /* Use default. */
127 case 9600:
128 b_rate = B9600;
129 break;
130 case 19200:
131 b_rate = B19200;
132 break;
133 case 38400:
134 b_rate = B38400;
135 break;
136 case 57600:
137 b_rate = B57600;
138 break;
139 case 115200:
140 b_rate = B115200;
141 break;
142 default:
143 err(1, "unknown baudrate %d", baudrate);
144 break;
147 if (argc != 2)
148 err(1, "usage: scat device-file");
149 siodev = argv[1];
151 setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
153 slipfd = open(siodev, O_RDWR);
154 if (slipfd == -1) err(1, "can't open '%s'", siodev);
155 stty_telos(slipfd);
156 inslip = fdopen(slipfd, "r");
157 if(inslip == NULL) err(1, "main: fdopen");
159 while (1) {
160 int c = getc(inslip);
161 while (c == SLIP_END)
162 c = getc(inslip);
163 do {
164 if (c == SLIP_ESC) {
165 c = getc(inslip);
166 if (c == SLIP_ESC_ESC)
167 c = SLIP_ESC;
168 else if (c == SLIP_ESC_END)
169 c = SLIP_END;
171 switch (c) {
172 case EOF:
173 err(1, "getc(inslip)");
174 break;
176 case '\007':
177 case '\b':
178 case '\f':
179 case '\n':
180 case '\r':
181 case '\t':
182 case '\v':
183 putchar(c);
184 break;
186 default:
187 if (isprint(c))
188 putchar(c);
189 else
190 printf("%02x ", c);
191 break;
193 c = getc(inslip);
194 } while (c != SLIP_END);
197 return 0;