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
44 void print_options_help(char *argv[])
46 printf("Usage: %s [<options>] <tty device>\n", argv[0]);
49 printf(" -b, --baudrate <bps> Baud rate (default: 115200)\n");
50 printf(" -d, --databits 5|6|7|8 Data bits (default: 8)\n");
51 printf(" -f, --flow hard|soft|none Flow control (default: none)\n");
52 printf(" -s, --stopbits 1|2 Stop bits (default: 1)\n");
53 printf(" -p, --parity odd|even|none Parity (default: none)\n");
54 printf(" -o, --output-delay <ms> Output delay (default: 0)\n");
55 printf(" -n, --no-autoconnect Disable automatic connect\n");
56 printf(" -l, --log <filename> Log to file\n");
57 printf(" -v, --version Display version\n");
58 printf(" -h, --help Display help\n");
60 printf("In session, press ctrl-t + q to quit.\n");
64 void parse_options(int argc, char *argv[])
73 print_options_help(argv);
77 /* Set default termios settings:
78 * (115200 baud, 8 data bits, no flow control, 1 stop bit, no parity) */
79 bzero(&option.tio, sizeof(option.tio));
80 option.tio.c_cflag = B115200 | CS8;
82 /* Set default output delay */
83 option.output_delay = 0;
87 static struct option long_options[] =
89 {"baudrate", required_argument, 0, 'b'},
90 {"databits", required_argument, 0, 'd'},
91 {"flow", required_argument, 0, 'f'},
92 {"stopbits", required_argument, 0, 's'},
93 {"parity", required_argument, 0, 'p'},
94 {"output-delay", required_argument, 0, 'o'},
95 {"no-autoconnect", no_argument, 0, 'n'},
96 {"log", required_argument, 0, 'l'},
97 {"version", no_argument, 0, 'v'},
98 {"help", no_argument, 0, 'h'},
102 /* getopt_long stores the option index here */
103 int option_index = 0;
105 /* Parse argument using getopt_long */
106 c = getopt_long(argc, argv, "b:d:f:s:p:o:nl:vh", long_options, &option_index);
108 /* Detect the end of the options */
115 /* If this option sets a flag, do nothing else now */
116 if (long_options[option_index].flag != 0)
118 printf("option %s", long_options[option_index].name);
120 printf(" with arg %s", optarg);
125 baudrate = atoi(optarg);
216 error_printf("Invalid baud rate");
220 cfsetispeed(&option.tio, baudrate);
221 cfsetospeed(&option.tio, baudrate);
226 databits = atoi(optarg);
227 option.tio.c_cflag &= ~CSIZE;
231 option.tio.c_cflag |= CS5;
234 option.tio.c_cflag |= CS6;
237 option.tio.c_cflag |= CS7;
240 option.tio.c_cflag |= CS8;
243 error_printf("Invalid data bits");
249 if (strcmp("hard", optarg) == 0)
251 option.tio.c_cflag |= CRTSCTS;
252 option.tio.c_iflag &= ~(IXON | IXOFF | IXANY);
254 else if (strcmp("soft", optarg) == 0)
256 option.tio.c_cflag &= ~CRTSCTS;
257 option.tio.c_iflag |= IXON | IXOFF;
259 else if (strcmp("none", optarg) == 0)
261 option.tio.c_cflag &= ~CRTSCTS;
262 option.tio.c_iflag &= ~(IXON | IXOFF | IXANY);
266 error_printf("Invalid flow control");
272 stopbits = atoi(optarg);
276 option.tio.c_cflag &= ~CSTOPB;
279 option.tio.c_cflag |= CSTOPB;
282 error_printf("Invalid stop bits");
288 if (strcmp("odd", optarg) == 0)
290 option.tio.c_cflag |= PARENB;
291 option.tio.c_cflag |= PARODD;
293 else if (strcmp("even", optarg) == 0)
295 option.tio.c_cflag |= PARENB;
296 option.tio.c_cflag &= ~PARODD;
298 else if (strcmp("none", optarg) == 0)
299 option.tio.c_cflag &= ~PARENB;
302 error_printf("Invalid parity");
308 option.output_delay = atoi(optarg);
312 option.no_autoconnect = true;
317 option.log_filename = optarg;
321 printf("tio v%s\n", VERSION);
322 printf("Copyright (c) 2014-2016 Martin Lund\n");
324 printf("License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
325 printf("This is free software: you are free to change and redistribute it.\n");
326 printf("There is NO WARRANTY, to the extent permitted by law.\n");
331 print_options_help(argv);
336 /* getopt_long already printed an error message */
345 /* Assume first non-option is the tty device name */
347 option.tty_device = argv[optind++];
349 if (strlen(option.tty_device) == 0)
351 error_printf("Missing device name");
355 /* Print any remaining command line arguments (unknown options) */
358 printf("Error: unknown arguments: ");
359 while (optind < argc)
360 printf("%s ", argv[optind++]);