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
23 #include <sys/types.h>
32 #include "tio/options.h"
33 #include "tio/error.h"
36 struct option_t option =
45 false, // No autoconnect
50 void print_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 long string_to_long(char *string)
76 result = strtol(string, &end_token, 10);
77 if ((errno != 0) || (*end_token != 0))
79 error_printf("Invalid digit");
86 void parse_options(int argc, char *argv[])
98 static struct option long_options[] =
100 {"baudrate", required_argument, 0, 'b'},
101 {"databits", required_argument, 0, 'd'},
102 {"flow", required_argument, 0, 'f'},
103 {"stopbits", required_argument, 0, 's'},
104 {"parity", required_argument, 0, 'p'},
105 {"output-delay", required_argument, 0, 'o'},
106 {"no-autoconnect", no_argument, 0, 'n'},
107 {"log", required_argument, 0, 'l'},
108 {"version", no_argument, 0, 'v'},
109 {"help", no_argument, 0, 'h'},
113 /* getopt_long stores the option index here */
114 int option_index = 0;
116 /* Parse argument using getopt_long */
117 c = getopt_long(argc, argv, "b:d:f:s:p:o:nl:vh", long_options, &option_index);
119 /* Detect the end of the options */
126 /* If this option sets a flag, do nothing else now */
127 if (long_options[option_index].flag != 0)
129 printf("option %s", long_options[option_index].name);
131 printf(" with arg %s", optarg);
136 option.baudrate = string_to_long(optarg);
140 option.databits = string_to_long(optarg);
144 option.flow = optarg;
148 option.stopbits = string_to_long(optarg);
152 option.parity = optarg;
156 option.output_delay = string_to_long(optarg);
160 option.no_autoconnect = true;
165 option.log_filename = optarg;
169 printf("tio v%s\n", VERSION);
170 printf("Copyright (c) 2014-2016 Martin Lund\n");
172 printf("License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
173 printf("This is free software: you are free to change and redistribute it.\n");
174 printf("There is NO WARRANTY, to the extent permitted by law.\n");
184 /* getopt_long already printed an error message */
193 /* Assume first non-option is the tty device name */
195 option.tty_device = argv[optind++];
197 if (strlen(option.tty_device) == 0)
199 error_printf("Missing device name");
203 /* Print any remaining command line arguments (unknown options) */
206 printf("Error: unknown arguments: ");
207 while (optind < argc)
208 printf("%s ", argv[optind++]);