]> git.sur5r.net Git - tio/blobdiff - src/options.c
Imported Upstream version 1.18
[tio] / src / options.c
index 7eb964bd7fb274da7bf505a1b3e5eda298b5dcc2..e280fffd56e3defd339f2758e51e327f4b14dbc2 100644 (file)
@@ -19,6 +19,7 @@
  * 02110-1301, USA.
  */
 
+#include "config.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <string.h>
 #include <getopt.h>
 #include <termios.h>
 #include <limits.h>
-#include "config.h"
 #include "tio/options.h"
 #include "tio/error.h"
 
+/* Default options */
 struct option_t option =
 {
     "",       // Device name
-    false,    // No log
-    "",       // Log filename
-    false,    // No autoconnect
-    0,        // No output delay
-    {},
     115200,   // Baudrate
     8,        // Databits
     "none",   // Flow
     1,        // Stopbits
-    "none"    // Parity
+    "none",   // Parity
+    0,        // No output delay
+    false,    // No autoconnect
+    false,    // No log
+    ""        // Log filename
 };
 
-void print_options_help(char *argv[])
+void print_help(char *argv[])
 {
     printf("Usage: %s [<options>] <tty device>\n", argv[0]);
     printf("\n");
@@ -63,29 +63,36 @@ void print_options_help(char *argv[])
     printf("  -v, --version               Display version\n");
     printf("  -h, --help                  Display help\n");
     printf("\n");
-    printf("In session, press ctrl-t q to quit.\n");
+    printf("In session, press ctrl-t q to quit.\n");
     printf("\n");
 }
 
+long string_to_long(char *string)
+{
+    long result;
+    char *end_token;
+
+    errno = 0;
+    result = strtol(string, &end_token, 10);
+    if ((errno != 0) || (*end_token != 0))
+    {
+        error_printf("Invalid digit");
+        exit(EXIT_FAILURE);
+    }
+
+    return result;
+}
+
 void parse_options(int argc, char *argv[])
 {
     int c;
-    int baudrate;
 
     if (argc == 1)
     {
-        print_options_help(argv);
+        print_help(argv);
         exit(EXIT_SUCCESS);
     }
 
-    /* Set default termios settings:
-     * (115200 baud, 8 data bits, no flow control, 1 stop bit, no parity) */
-    bzero(&option.tio, sizeof(option.tio));
-    option.tio.c_cflag = B115200 | CS8;
-
-    /* Set default output delay */
-    option.output_delay = 0;
-
     while (1)
     {
         static struct option long_options[] =
@@ -126,194 +133,27 @@ void parse_options(int argc, char *argv[])
                 break;
 
             case 'b':
-                option.baudrate = baudrate = atoi(optarg);
-                switch (baudrate)
-                {
-                    case 0:
-                        baudrate = B0;
-                        break;
-                    case 50:
-                        baudrate = B50;
-                        break;
-                    case 75:
-                        baudrate = B75;
-                        break;
-                    case 110:
-                        baudrate = B110;
-                        break;
-                    case 134:
-                        baudrate = B134;
-                        break;
-                    case 150:
-                        baudrate = B150;
-                        break;
-                    case 300:
-                        baudrate = B300;
-                        break;
-                    case 600:
-                        baudrate = B600;
-                        break;
-                    case 1200:
-                        baudrate = B1200;
-                        break;
-                    case 2400:
-                        baudrate = B2400;
-                        break;
-                    case 4800:
-                        baudrate = B4800;
-                        break;
-                    case 9600:
-                        baudrate = B9600;
-                        break;
-                    case 19200:
-                        baudrate = B19200;
-                        break;
-                    case 38400:
-                        baudrate = B38400;
-                        break;
-                    case 57600:
-                        baudrate = B57600;
-                        break;
-                    case 115200:
-                        baudrate = B115200;
-                        break;
-                    case 230400:
-                        baudrate = B230400;
-                        break;
-                    case 460800:
-                        baudrate = B460800;
-                        break;
-                    case 500000:
-                        baudrate = B500000;
-                        break;
-                    case 576000:
-                        baudrate = B576000;
-                        break;
-                    case 921600:
-                        baudrate = B921600;
-                        break;
-                    case 1000000:
-                        baudrate = B1000000;
-                        break;
-                    case 1152000:
-                        baudrate = B1152000;
-                        break;
-                    case 1500000:
-                        baudrate = B1500000;
-                        break;
-                    case 2000000:
-                        baudrate = B2000000;
-                        break;
-                    case 2500000:
-                        baudrate = B2500000;
-                        break;
-                    case 3000000:
-                        baudrate = B3000000;
-                        break;
-                    case 3500000:
-                        baudrate = B3500000;
-                        break;
-                    case 4000000:
-                        baudrate = B4000000;
-                        break;
-                    default:
-                        error_printf("Invalid baud rate");
-                        exit(EXIT_FAILURE);
-                }
-
-                cfsetispeed(&option.tio, baudrate);
-                cfsetospeed(&option.tio, baudrate);
-
+                option.baudrate = string_to_long(optarg);
                 break;
 
             case 'd':
-                option.databits = atoi(optarg);
-                option.tio.c_cflag &= ~CSIZE;
-                switch (option.databits)
-                {
-                    case 5:
-                        option.tio.c_cflag |= CS5;
-                        break;
-                    case 6:
-                        option.tio.c_cflag |= CS6;
-                        break;
-                    case 7:
-                        option.tio.c_cflag |= CS7;
-                        break;
-                    case 8:
-                        option.tio.c_cflag |= CS8;
-                        break;
-                    default:
-                        error_printf("Invalid data bits");
-                        exit(EXIT_FAILURE);
-                }
+                option.databits = string_to_long(optarg);
                 break;
 
             case 'f':
                 option.flow = optarg;
-
-                if (strcmp("hard", optarg) == 0)
-                {
-                    option.tio.c_cflag |= CRTSCTS;
-                    option.tio.c_iflag &= ~(IXON | IXOFF | IXANY);
-                }
-                else if (strcmp("soft", optarg) == 0)
-                {
-                    option.tio.c_cflag &= ~CRTSCTS;
-                    option.tio.c_iflag |= IXON | IXOFF;
-                }
-                else if (strcmp("none", optarg) == 0)
-                {
-                    option.tio.c_cflag &= ~CRTSCTS;
-                    option.tio.c_iflag &= ~(IXON | IXOFF | IXANY);
-                }
-                else
-                {
-                    error_printf("Invalid flow control");
-                    exit(EXIT_FAILURE);
-                }
                 break;
 
             case 's':
-                option.stopbits = atoi(optarg);
-                switch (option.stopbits)
-                {
-                    case 1:
-                        option.tio.c_cflag &= ~CSTOPB;
-                        break;
-                    case 2:
-                        option.tio.c_cflag |= CSTOPB;
-                        break;
-                    default:
-                        error_printf("Invalid stop bits");
-                        exit(EXIT_FAILURE);
-                }
+                option.stopbits = string_to_long(optarg);
                 break;
 
             case 'p':
                 option.parity = optarg;
-
-                if (strcmp("odd", optarg) == 0)
-                {
-                    option.tio.c_cflag |= PARENB;
-                    option.tio.c_cflag |= PARODD;
-                }
-                else if (strcmp("even", optarg) == 0)
-                {
-                    option.tio.c_cflag |= PARENB;
-                    option.tio.c_cflag &= ~PARODD;
-                }
-                else if (strcmp("none", optarg) == 0)
-                    option.tio.c_cflag &= ~PARENB;
-                else
-                {
-                    error_printf("Invalid parity");
-                    exit(EXIT_FAILURE);
-                }
                 break;
 
             case 'o':
-                option.output_delay = atoi(optarg);
+                option.output_delay = string_to_long(optarg);
                 break;
 
             case 'n':
@@ -329,14 +169,14 @@ void parse_options(int argc, char *argv[])
                 printf("tio v%s\n", VERSION);
                 printf("Copyright (c) 2014-2016 Martin Lund\n");
                 printf("\n");
-                printf("License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
+                printf("License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
                 printf("This is free software: you are free to change and redistribute it.\n");
                 printf("There is NO WARRANTY, to the extent permitted by law.\n");
                 exit(EXIT_SUCCESS);
                 break;
 
             case 'h':
-                print_options_help(argv);
+                print_help(argv);
                 exit(EXIT_SUCCESS);
                 break;