]> rtime.felk.cvut.cz Git - sojka/sterm.git/blobdiff - sterm.c
Merge pull request #5 from pr2502/add-fish-completion
[sojka/sterm.git] / sterm.c
diff --git a/sterm.c b/sterm.c
index 6d38cd80166f162e19e81fb4a99518d5671bd2be..13701e7a777acc6c59f49e5c51de29ea943f3819 100644 (file)
--- a/sterm.c
+++ b/sterm.c
@@ -1,7 +1,7 @@
 /*
  * Simple serial terminal
  *
- * Copyright 2014, 2015, 2016, 2017, 2019, 2020 Michal Sojka <sojkam1@fel.cvut.cz>
+ * Copyright 2014, 2015, 2016, 2017, 2019, 2020, 2021 Michal Sojka <sojkam1@fel.cvut.cz>
  *
  * This program is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -114,12 +114,35 @@ int dtr_rts_arg(const char option, const char *optarg)
        return val;
 }
 
+// See DSR and CPR at
+// https://en.wikipedia.org/wiki/ANSI_escape_code#Terminal_output_sequences
+bool is_cpr_control_seq(char c)
+{
+       static enum state { CSI_ESC, CSI_BRACKET, PAR_N, PAR_M, FIN_R } state;
+
+       switch (state) {
+       case CSI_ESC:      state = (c == 0x1b) ? CSI_BRACKET : CSI_ESC; break;
+       case CSI_BRACKET:  state = (c == '[')  ? PAR_N : CSI_ESC; break;
+       case PAR_N:        state = (c == ';')  ? PAR_M : (c >= '0' && c <= '9' ? PAR_N : CSI_ESC); break;
+       case PAR_M:        state = (c == 'R')  ? FIN_R : (c >= '0' && c <= '9' ? PAR_M : CSI_ESC); break;
+       case FIN_R: break;
+       }
+       if (state == FIN_R) {
+               state = CSI_ESC;
+               return true;
+
+       } else
+               return state != CSI_ESC;
+}
+
 void exit_on_escapeseq(const char *buf, int len)
 {
        static const char escseq[] = "\r~.";
        static const char *state = escseq+1;
        int i;
        for (i = 0; i < len; i++) {
+               if (is_cpr_control_seq(buf[i]))
+                       continue;
                if (buf[i] == *state) {
                        state++;
                        if (*state == 0)
@@ -132,26 +155,29 @@ void exit_on_escapeseq(const char *buf, int len)
        }
 }
 
-void usage(const char* argv0)
+void print_usage_and_exit(const char* argv0, int exit_code)
 {
-       fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
-       fprintf(stderr,
+       fprintf(exit_code == 0 ? stdout : stderr, "Usage: %s [options] <device>\n", argv0);
+       fprintf(exit_code == 0 ? stdout : stderr,
                "Options:\n"
                "  -b <duration> send break signal\n"
                "  -c        enter command mode\n"
                "  -d[PULSE] make pulse on DTR\n"
                "  -e        ignore '~.' escape sequence\n"
+               "  -h, --help  print help and exit\n"
                "  -n        do not switch stdin TTY to raw mode\n"
                "  -r[PULSE] make pulse on RTS\n"
                "  -s <baudrate>\n"
                "  -t <ms>   minimum delay between two transmitted characters\n"
                "  -v        verbose mode\n"
+               "  -V, --version  print version and exit\n"
                "\n"
                "PULSE is a number specifying the pulse. Absolute value defines the\n"
                "length of the pulse in milliseconds, sign determines the polarity of\n"
                "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
-               "corresponds to +1 or -1.\n"
+               "corresponds to '+1' or '-1'.\n"
                );
+       exit(exit_code);
 }
 
 void pulse(int fd, int dtr, int rts)
@@ -232,7 +258,14 @@ int main(int argc, char *argv[])
                atexit(restore_stdin_term);
        }
 
-       while ((opt = getopt(argc, argv, "b:cnd::er::s:vt:")) != -1) {
+       static struct option long_options[] = {
+               {"help",    no_argument,       0,  'h' },
+               {"version", no_argument,       0,  'V' },
+               {0,         0,                 0,  0   }
+       };
+
+       while ((opt = getopt_long(argc, argv, "b:cnd::er::s:vVt:h",
+                                 long_options, NULL)) != -1) {
                switch (opt) {
                case 'b': break_dur = atoi(optarg); break;
                case 'c': cmd = true; break;
@@ -288,9 +321,13 @@ int main(int argc, char *argv[])
                case 'v':
                        verbose = true;
                        break;
+               case 'V':
+                       printf("sterm version %s\n", strlen(STERM_VERSION) > 0 ? STERM_VERSION : "unknown");
+                       return 0;
+               case 'h':
+                       /* fallthrough */
                default: /* '?' */
-                       usage(argv[0]);
-                       exit(1);
+                       print_usage_and_exit(argv[0], opt == 'h' ? 0 : 1);
                }
        }
 
@@ -299,8 +336,7 @@ int main(int argc, char *argv[])
 
        if (!dev) {
                fprintf(stderr, "No device specified\n");
-               usage(argv[0]);
-               exit(1);
+               print_usage_and_exit(argv[0], 1);
        }
 
        signal(SIGINT, sighandler);