2 * tio - a simple TTY terminal I/O application
4 * Copyright (c) 2014-2016 Martin Lund
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <sys/types.h>
32 #include "tio/options.h"
33 #include "tio/error.h"
35 struct option_t option =
40 false, // No autoconnect
50 void print_options_help(char *argv[])
52 printf("Usage: %s [<options>] <tty device>\n", argv[0]);
55 printf(" -b, --baudrate <bps> Baud rate (default: 115200)\n");
56 printf(" -d, --databits 5|6|7|8 Data bits (default: 8)\n");
57 printf(" -f, --flow hard|soft|none Flow control (default: none)\n");
58 printf(" -s, --stopbits 1|2 Stop bits (default: 1)\n");
59 printf(" -p, --parity odd|even|none Parity (default: none)\n");
60 printf(" -o, --output-delay <ms> Output delay (default: 0)\n");
61 printf(" -n, --no-autoconnect Disable automatic connect\n");
62 printf(" -l, --log <filename> Log to file\n");
63 printf(" -v, --version Display version\n");
64 printf(" -h, --help Display help\n");
66 printf("In session, press ctrl-t + q to quit.\n");
70 void parse_options(int argc, char *argv[])
77 print_options_help(argv);
81 /* Set default termios settings:
82 * (115200 baud, 8 data bits, no flow control, 1 stop bit, no parity) */
83 bzero(&option.tio, sizeof(option.tio));
84 option.tio.c_cflag = B115200 | CS8;
86 /* Set default output delay */
87 option.output_delay = 0;
91 static struct option long_options[] =
93 {"baudrate", required_argument, 0, 'b'},
94 {"databits", required_argument, 0, 'd'},
95 {"flow", required_argument, 0, 'f'},
96 {"stopbits", required_argument, 0, 's'},
97 {"parity", required_argument, 0, 'p'},
98 {"output-delay", required_argument, 0, 'o'},
99 {"no-autoconnect", no_argument, 0, 'n'},
100 {"log", required_argument, 0, 'l'},
101 {"version", no_argument, 0, 'v'},
102 {"help", no_argument, 0, 'h'},
106 /* getopt_long stores the option index here */
107 int option_index = 0;
109 /* Parse argument using getopt_long */
110 c = getopt_long(argc, argv, "b:d:f:s:p:o:nl:vh", long_options, &option_index);
112 /* Detect the end of the options */
119 /* If this option sets a flag, do nothing else now */
120 if (long_options[option_index].flag != 0)
122 printf("option %s", long_options[option_index].name);
124 printf(" with arg %s", optarg);
129 option.baudrate = baudrate = atoi(optarg);
220 error_printf("Invalid baud rate");
224 cfsetispeed(&option.tio, baudrate);
225 cfsetospeed(&option.tio, baudrate);
230 option.databits = atoi(optarg);
231 option.tio.c_cflag &= ~CSIZE;
232 switch (option.databits)
235 option.tio.c_cflag |= CS5;
238 option.tio.c_cflag |= CS6;
241 option.tio.c_cflag |= CS7;
244 option.tio.c_cflag |= CS8;
247 error_printf("Invalid data bits");
253 option.flow = optarg;
255 if (strcmp("hard", optarg) == 0)
257 option.tio.c_cflag |= CRTSCTS;
258 option.tio.c_iflag &= ~(IXON | IXOFF | IXANY);
260 else if (strcmp("soft", optarg) == 0)
262 option.tio.c_cflag &= ~CRTSCTS;
263 option.tio.c_iflag |= IXON | IXOFF;
265 else if (strcmp("none", optarg) == 0)
267 option.tio.c_cflag &= ~CRTSCTS;
268 option.tio.c_iflag &= ~(IXON | IXOFF | IXANY);
272 error_printf("Invalid flow control");
278 option.stopbits = atoi(optarg);
279 switch (option.stopbits)
282 option.tio.c_cflag &= ~CSTOPB;
285 option.tio.c_cflag |= CSTOPB;
288 error_printf("Invalid stop bits");
294 option.parity = optarg;
296 if (strcmp("odd", optarg) == 0)
298 option.tio.c_cflag |= PARENB;
299 option.tio.c_cflag |= PARODD;
301 else if (strcmp("even", optarg) == 0)
303 option.tio.c_cflag |= PARENB;
304 option.tio.c_cflag &= ~PARODD;
306 else if (strcmp("none", optarg) == 0)
307 option.tio.c_cflag &= ~PARENB;
310 error_printf("Invalid parity");
316 option.output_delay = atoi(optarg);
320 option.no_autoconnect = true;
325 option.log_filename = optarg;
329 printf("tio v%s\n", VERSION);
330 printf("Copyright (c) 2014-2016 Martin Lund\n");
332 printf("License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
333 printf("This is free software: you are free to change and redistribute it.\n");
334 printf("There is NO WARRANTY, to the extent permitted by law.\n");
339 print_options_help(argv);
344 /* getopt_long already printed an error message */
353 /* Assume first non-option is the tty device name */
355 option.tty_device = argv[optind++];
357 if (strlen(option.tty_device) == 0)
359 error_printf("Missing device name");
363 /* Print any remaining command line arguments (unknown options) */
366 printf("Error: unknown arguments: ");
367 while (optind < argc)
368 printf("%s ", argv[optind++]);