2 * tio - a simple TTY terminal I/O tool
4 * Copyright (c) 2014-2022 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>
37 struct option_t option =
46 .no_autoconnect = false,
50 .list_devices = false,
56 void print_help(char *argv[])
58 printf("Usage: %s [<options>] <tty-device>\n", argv[0]);
61 printf(" -b, --baudrate <bps> Baud rate (default: 115200)\n");
62 printf(" -d, --databits 5|6|7|8 Data bits (default: 8)\n");
63 printf(" -f, --flow hard|soft|none Flow control (default: none)\n");
64 printf(" -s, --stopbits 1|2 Stop bits (default: 1)\n");
65 printf(" -p, --parity odd|even|none Parity (default: none)\n");
66 printf(" -o, --output-delay <ms> Output delay (default: 0)\n");
67 printf(" -n, --no-autoconnect Disable automatic connect\n");
68 printf(" -e, --local-echo Enable local echo\n");
69 printf(" -t, --timestamp Enable line timestamp\n");
70 printf(" -L, --list-devices List available serial devices\n");
71 printf(" -l, --log <filename> Log to file\n");
72 printf(" -m, --map <flags> Map special characters\n");
73 printf(" -c, --color <0..255> Colorize tio text\n");
74 printf(" -v, --version Display version\n");
75 printf(" -h, --help Display help\n");
77 printf("See the man page for more details.\n");
79 printf("In session, press ctrl-t q to quit.\n");
83 long string_to_long(char *string)
89 result = strtol(string, &end_token, 10);
90 if ((errno != 0) || (*end_token != 0))
92 printf("Error: Invalid digit\n");
99 void parse_options(int argc, char *argv[])
111 static struct option long_options[] =
113 {"baudrate", required_argument, 0, 'b'},
114 {"databits", required_argument, 0, 'd'},
115 {"flow", required_argument, 0, 'f'},
116 {"stopbits", required_argument, 0, 's'},
117 {"parity", required_argument, 0, 'p'},
118 {"output-delay", required_argument, 0, 'o'},
119 {"no-autoconnect", no_argument, 0, 'n'},
120 {"local-echo", no_argument, 0, 'e'},
121 {"timestamp", no_argument, 0, 't'},
122 {"list-devices", no_argument, 0, 'L'},
123 {"log", required_argument, 0, 'l'},
124 {"map", required_argument, 0, 'm'},
125 {"color", required_argument, 0, 'c'},
126 {"version", no_argument, 0, 'v'},
127 {"help", no_argument, 0, 'h'},
131 /* getopt_long stores the option index here */
132 int option_index = 0;
134 /* Parse argument using getopt_long */
135 c = getopt_long(argc, argv, "b:d:f:s:p:o:netLl:m:c:vh", long_options, &option_index);
137 /* Detect the end of the options */
144 /* If this option sets a flag, do nothing else now */
145 if (long_options[option_index].flag != 0)
147 printf("option %s", long_options[option_index].name);
149 printf(" with arg %s", optarg);
154 option.baudrate = string_to_long(optarg);
158 option.databits = string_to_long(optarg);
162 option.flow = optarg;
166 option.stopbits = string_to_long(optarg);
170 option.parity = optarg;
174 option.output_delay = string_to_long(optarg);
178 option.no_autoconnect = true;
182 option.local_echo = true;
186 option.timestamp = true;
190 option.list_devices = true;
195 option.log_filename = optarg;
203 option.color = string_to_long(optarg);
204 if (option.color > 255)
206 printf("Error: Invalid color code\n");
209 if (option.color < 0)
211 // Print available color codes
212 printf("Available color codes:\n");
213 for (int i=0; i<=255; i++)
215 printf(" \e[1;38;5;%dmThis is color code %d\e[0m\n", i, i);
222 printf("tio v%s\n", VERSION);
223 printf("Copyright (c) 2014-2022 Martin Lund\n");
225 printf("License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
226 printf("This is free software: you are free to change and redistribute it.\n");
227 printf("There is NO WARRANTY, to the extent permitted by law.\n");
237 /* getopt_long already printed an error message */
246 if (option.list_devices)
251 /* Assume first non-option is the tty device name */
253 option.tty_device = argv[optind++];
255 if (strlen(option.tty_device) == 0)
257 printf("Error: Missing device name\n");
261 /* Print any remaining command line arguments (unknown options) */
264 printf("Error: unknown arguments: ");
265 while (optind < argc)
266 printf("%s ", argv[optind++]);