]> git.sur5r.net Git - tio/blob - src/options.c
Imported Upstream version 1.18
[tio] / src / options.c
1 /*
2  * tio - a simple TTY terminal I/O application
3  *
4  * Copyright (c) 2014-2016  Martin Lund
5  *
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.
10  *
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.
15  *
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
19  * 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <termios.h>
31 #include <limits.h>
32 #include "tio/options.h"
33 #include "tio/error.h"
34
35 /* Default options */
36 struct option_t option =
37 {
38     "",       // Device name
39     115200,   // Baudrate
40     8,        // Databits
41     "none",   // Flow
42     1,        // Stopbits
43     "none",   // Parity
44     0,        // No output delay
45     false,    // No autoconnect
46     false,    // No log
47     ""        // Log filename
48 };
49
50 void print_help(char *argv[])
51 {
52     printf("Usage: %s [<options>] <tty device>\n", argv[0]);
53     printf("\n");
54     printf("Options:\n");
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");
65     printf("\n");
66     printf("In session, press ctrl-t q to quit.\n");
67     printf("\n");
68 }
69
70 long string_to_long(char *string)
71 {
72     long result;
73     char *end_token;
74
75     errno = 0;
76     result = strtol(string, &end_token, 10);
77     if ((errno != 0) || (*end_token != 0))
78     {
79         error_printf("Invalid digit");
80         exit(EXIT_FAILURE);
81     }
82
83     return result;
84 }
85
86 void parse_options(int argc, char *argv[])
87 {
88     int c;
89
90     if (argc == 1)
91     {
92         print_help(argv);
93         exit(EXIT_SUCCESS);
94     }
95
96     while (1)
97     {
98         static struct option long_options[] =
99         {
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'},
110             {0,                0,                 0,  0 }
111         };
112
113         /* getopt_long stores the option index here */
114         int option_index = 0;
115
116         /* Parse argument using getopt_long */
117         c = getopt_long(argc, argv, "b:d:f:s:p:o:nl:vh", long_options, &option_index);
118
119         /* Detect the end of the options */
120         if (c == -1)
121             break;
122
123         switch (c)
124         {
125             case 0:
126                 /* If this option sets a flag, do nothing else now */
127                 if (long_options[option_index].flag != 0)
128                     break;
129                 printf("option %s", long_options[option_index].name);
130                 if (optarg)
131                     printf(" with arg %s", optarg);
132                 printf("\n");
133                 break;
134
135             case 'b':
136                 option.baudrate = string_to_long(optarg);
137                 break;
138
139             case 'd':
140                 option.databits = string_to_long(optarg);
141                 break;
142
143             case 'f':
144                 option.flow = optarg;
145                 break;
146
147             case 's':
148                 option.stopbits = string_to_long(optarg);
149                 break;
150
151             case 'p':
152                 option.parity = optarg;
153                 break;
154
155             case 'o':
156                 option.output_delay = string_to_long(optarg);
157                 break;
158
159             case 'n':
160                 option.no_autoconnect = true;
161                 break;
162
163             case 'l':
164                 option.log = true;
165                 option.log_filename = optarg;
166                 break;
167
168             case 'v':
169                 printf("tio v%s\n", VERSION);
170                 printf("Copyright (c) 2014-2016 Martin Lund\n");
171                 printf("\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");
175                 exit(EXIT_SUCCESS);
176                 break;
177
178             case 'h':
179                 print_help(argv);
180                 exit(EXIT_SUCCESS);
181                 break;
182
183             case '?':
184                 /* getopt_long already printed an error message */
185                 exit(EXIT_FAILURE);
186                 break;
187
188             default:
189                 exit(EXIT_FAILURE);
190         }
191     }
192
193     /* Assume first non-option is the tty device name */
194     if (optind < argc)
195         option.tty_device = argv[optind++];
196
197     if (strlen(option.tty_device) == 0)
198     {
199         error_printf("Missing device name");
200         exit(EXIT_FAILURE);
201     }
202
203     /* Print any remaining command line arguments (unknown options) */
204     if (optind < argc)
205     {
206         printf("Error: unknown arguments: ");
207         while (optind < argc)
208             printf("%s ", argv[optind++]);
209         printf("\n");
210         exit(EXIT_FAILURE);
211     }
212 }