]> git.sur5r.net Git - tio/blobdiff - src/options.c
New upstream version 1.34
[tio] / src / options.c
index c88582a788a34d1113d8876ce4044ff631ed4ef2..160faeabb4fbcbb7ccbbc35ff66ba4870983065c 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * tio - a simple TTY terminal I/O application
+ * tio - a simple TTY terminal I/O tool
  *
- * Copyright (c) 2014-2018  Martin Lund
+ * Copyright (c) 2014-2022  Martin Lund
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include <getopt.h>
 #include <termios.h>
 #include <limits.h>
-#include "tio/options.h"
-#include "tio/error.h"
+#include "options.h"
+#include "error.h"
 
 /* Default options */
 struct option_t option =
 {
-    "",       // Device name
-    115200,   // Baudrate
-    8,        // Databits
-    "none",   // Flow
-    1,        // Stopbits
-    "none",   // Parity
-    0,        // No output delay
-    false,    // No autoconnect
-    false,    // No log
-    false,    // No local echo
-    false,    // No timestamp
-    "",       // Log filename
-    ""        // Map string
+    .tty_device = "",
+    .baudrate = 115200,
+    .databits = 8,
+    .flow = "none",
+    .stopbits = 1,
+    .parity = "none",
+    .output_delay = 0,
+    .no_autoconnect = false,
+    .log = false,
+    .local_echo = false,
+    .timestamp = false,
+    .list_devices = false,
+    .log_filename = "",
+    .map = "",
+    .color = -1,
 };
 
 void print_help(char *argv[])
@@ -63,14 +65,16 @@ void print_help(char *argv[])
     printf("  -p, --parity odd|even|none  Parity (default: none)\n");
     printf("  -o, --output-delay <ms>     Output delay (default: 0)\n");
     printf("  -n, --no-autoconnect        Disable automatic connect\n");
-    printf("  -e, --local-echo            Do local echo\n");
-    printf("  -t, --timestamp             Prefix each new line with a timestamp\n");
+    printf("  -e, --local-echo            Enable local echo\n");
+    printf("  -t, --timestamp             Enable line timestamp\n");
+    printf("  -L, --list-devices          List available serial devices\n");
     printf("  -l, --log <filename>        Log to file\n");
     printf("  -m, --map <flags>           Map special characters\n");
+    printf("  -c, --color <0..255>        Colorize tio text\n");
     printf("  -v, --version               Display version\n");
     printf("  -h, --help                  Display help\n");
     printf("\n");
-    printf("See the man page for list of supported mapping flags.\n");
+    printf("See the man page for more details.\n");
     printf("\n");
     printf("In session, press ctrl-t q to quit.\n");
     printf("\n");
@@ -85,7 +89,7 @@ long string_to_long(char *string)
     result = strtol(string, &end_token, 10);
     if ((errno != 0) || (*end_token != 0))
     {
-        error_printf("Invalid digit");
+        printf("Error: Invalid digit\n");
         exit(EXIT_FAILURE);
     }
 
@@ -115,8 +119,10 @@ void parse_options(int argc, char *argv[])
             {"no-autoconnect", no_argument,       0, 'n'},
             {"local-echo",     no_argument,       0, 'e'},
             {"timestamp",      no_argument,       0, 't'},
+            {"list-devices",   no_argument,       0, 'L'},
             {"log",            required_argument, 0, 'l'},
             {"map",            required_argument, 0, 'm'},
+            {"color",          required_argument, 0, 'c'},
             {"version",        no_argument,       0, 'v'},
             {"help",           no_argument,       0, 'h'},
             {0,                0,                 0,  0 }
@@ -126,7 +132,7 @@ void parse_options(int argc, char *argv[])
         int option_index = 0;
 
         /* Parse argument using getopt_long */
-        c = getopt_long(argc, argv, "b:d:f:s:p:o:netl:m:vh", long_options, &option_index);
+        c = getopt_long(argc, argv, "b:d:f:s:p:o:netLl:m:c:vh", long_options, &option_index);
 
         /* Detect the end of the options */
         if (c == -1)
@@ -180,6 +186,10 @@ void parse_options(int argc, char *argv[])
                 option.timestamp = true;
                 break;
 
+            case 'L':
+                option.list_devices = true;
+                break;
+
             case 'l':
                 option.log = true;
                 option.log_filename = optarg;
@@ -189,9 +199,28 @@ void parse_options(int argc, char *argv[])
                 option.map = optarg;
                 break;
 
+            case 'c':
+                option.color = string_to_long(optarg);
+                if (option.color > 255)
+                {
+                    printf("Error: Invalid color code\n");
+                    exit(EXIT_FAILURE);
+                }
+                if (option.color < 0)
+                {
+                    // Print available color codes
+                    printf("Available color codes:\n");
+                    for (int i=0; i<=255; i++)
+                    {
+                        printf(" \e[1;38;5;%dmThis is color code %d\e[0m\n", i, i);
+                    }
+                    exit(EXIT_SUCCESS);
+                }
+                break;
+
             case 'v':
                 printf("tio v%s\n", VERSION);
-                printf("Copyright (c) 2014-2018 Martin Lund\n");
+                printf("Copyright (c) 2014-2022 Martin Lund\n");
                 printf("\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");
@@ -214,13 +243,18 @@ void parse_options(int argc, char *argv[])
         }
     }
 
+    if (option.list_devices)
+    {
+        return;
+    }
+
     /* Assume first non-option is the tty device name */
     if (optind < argc)
         option.tty_device = argv[optind++];
 
     if (strlen(option.tty_device) == 0)
     {
-        error_printf("Missing device name");
+        printf("Error: Missing device name\n");
         exit(EXIT_FAILURE);
     }