Hi,

Whenever I use cu(1) I miss the possibility to configure the escape
character. Both ssh(1) and telnet(1) provide the option -e to change the
escape character.

So I created the diffs below to implement it. I think it's time to share
it with you and ask for your feedback.

Cheers,
Bruno

Index: usr.bin/cu/command.c
===================================================================
RCS file: /cvs/src/usr.bin/cu/command.c,v
retrieving revision 1.15
diff -u -p -r1.15 command.c
--- usr.bin/cu/command.c        5 Oct 2015 23:15:31 -0000       1.15
+++ usr.bin/cu/command.c        3 Aug 2017 08:54:38 -0000
@@ -219,9 +219,29 @@ start_record(void)
                cu_warnx("%s", file);
 }
 
+struct escape_help_text {
+       const char *cmd;
+       const char *text;
+};
+
+static struct escape_help_text esc_txt[] = {
+       {".", "drop connection and exit"},
+       {"#", "send break"},
+       {"$", "pipe local command to remote host"},
+       {">", "send file to remote host"},
+       {"C", "connect program to remote host"},
+       {"D", "de-assert DTR line briefly"},
+       {"R", "start recording to file"},
+       {"S", "set speed"},
+       {"X", "send file with XMODEM"},
+       {"?", "get this summary"},
+};
+
 void
 do_command(char c)
 {
+       int i;
+
        switch (c) {
        case '.':
        case '\004': /* ^D */
@@ -260,21 +280,15 @@ do_command(char c)
                sleep(1);
                ioctl(line_fd, TIOCCBRK, NULL);
                break;
-       case '~':
-               bufferevent_write(line_ev, "~", 1);
-               break;
        case '?':
-               printf("\r\n"
-                   "~#      send break\r\n"
-                   "~$      pipe local command to remote host\r\n"
-                   "~>      send file to remote host\r\n"
-                   "~C      connect program to remote host\r\n"
-                   "~D      de-assert DTR line briefly\r\n"
-                   "~R      start recording to file\r\n"
-                   "~S      set speed\r\n"
-                   "~X      send file with XMODEM\r\n"
-                   "~?      get this summary\r\n"
-               );
+               for (i = 0; i < sizeof(esc_txt) / sizeof(esc_txt[0]); i++) {
+                       if (escape < 32)
+                               printf("\r\n^%c", escape + 64);
+                       else
+                               printf("\r\n%c", escape);
+                       printf("%s\t%s", esc_txt[i].cmd, esc_txt[i].text);
+               }
+               printf("\r\n");
                break;
        }
 }

Index: usr.bin/cu/cu.1
===================================================================
RCS file: /cvs/src/usr.bin/cu/cu.1,v
retrieving revision 1.15
diff -u -p -r1.15 cu.1
--- usr.bin/cu/cu.1     18 May 2015 09:35:05 -0000      1.15
+++ usr.bin/cu/cu.1     11 May 2017 11:56:15 -0000
@@ -36,6 +36,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl d
+.Op Fl e Ar char
 .Op Fl l Ar line
 .Op Fl s Ar speed | Fl Ar speed
 .Nm
@@ -55,6 +56,13 @@ The options are as follows:
 Specify that the line is directly connected and
 .Nm
 should not allow the driver to block waiting for a carrier to be detected.
+.It Fl e Ar char
+Sets the
+.Nm
+escape character to
+.Pa char .
+If not specified the escape character is the tilde
+.Pq Ql ~ .
 .It Fl l Ar line
 Specify the line to use.
 Either of the forms like
@@ -95,8 +103,8 @@ utility ignores other capabilities found
 .Pp
 Typed characters are normally transmitted directly to the remote
 machine (which does the echoing as well).
-A tilde
-.Pq Ql ~
+The escape character (by default:
+.Ql ~ )
 appearing as the first character of a line is an escape signal; the
 following are recognized:
 .Bl -tag -offset indent -width Fl

Index: usr.bin/cu/cu.c
===================================================================
RCS file: /cvs/src/usr.bin/cu/cu.c,v
retrieving revision 1.24
diff -u -p -r1.24 cu.c
--- usr.bin/cu/cu.c     16 Oct 2015 07:01:53 -0000      1.24
+++ usr.bin/cu/cu.c     3 Aug 2017 08:54:54 -0000
@@ -41,6 +41,7 @@ FILE                  *record_file;
 struct termios          saved_tio;
 struct bufferevent     *input_ev;
 struct bufferevent     *output_ev;
+u_char                  escape = '~';
 int                     is_direct = -1;
 const char             *line_path = NULL;
 int                     line_speed = -1;
@@ -52,7 +53,7 @@ struct event           sighup_ev;
 enum {
        STATE_NONE,
        STATE_NEWLINE,
-       STATE_TILDE
+       STATE_ESCAPE
 } last_state = STATE_NEWLINE;
 
 __dead void    usage(void);
@@ -66,7 +67,7 @@ void          try_remote(const char *, const cha
 __dead void
 usage(void)
 {
-       fprintf(stderr, "usage: %s [-d] [-l line] [-s speed | -speed]\n",
+       fprintf(stderr, "usage: %s [-d] [-e char] [-l line] [-s speed | 
-speed]\n",
            __progname);
        fprintf(stderr, "       %s [host]\n", __progname);
        exit(1);
@@ -100,11 +101,21 @@ main(int argc, char **argv)
                        errx(1, "speed asprintf");
        }
 
-       while ((opt = getopt(argc, argv, "dl:s:")) != -1) {
+       while ((opt = getopt(argc, argv, "de:l:s:")) != -1) {
                switch (opt) {
                case 'd':
                        is_direct = 1;
                        break;
+               case 'e':
+                       if (optarg[0] == '^' && optarg[2] == 0 &&
+                           (u_char) optarg[1] >= 64 &&
+                           (u_char) optarg[1] < 128)
+                               escape = (u_char) optarg[1] & 31;
+                       else if (strlen(optarg) == 1)
+                               escape = (u_char) optarg[0];
+                       else
+                               errx(1, "bad escape character '%s'", optarg);
+                       break;
                case 'l':
                        line_path = optarg;
                        break;
@@ -298,15 +309,18 @@ stream_read(struct bufferevent *bufev, v
                                last_state = STATE_NEWLINE;
                        break;
                case STATE_NEWLINE:
-                       if (state_change && *ptr == '~') {
-                               last_state = STATE_TILDE;
+                       if (state_change && *ptr == escape) {
+                               last_state = STATE_ESCAPE;
                                continue;
                        }
                        if (*ptr != '\r')
                                last_state = STATE_NONE;
                        break;
-               case STATE_TILDE:
-                       do_command(*ptr);
+               case STATE_ESCAPE:
+                       if (*ptr == escape)
+                               bufferevent_write(line_ev, ptr, 1);
+                       else
+                               do_command(*ptr);
                        last_state = STATE_NEWLINE;
                        continue;
                }

Index: usr.bin/cu/cu.h
===================================================================
RCS file: /cvs/src/usr.bin/cu/cu.h,v
retrieving revision 1.7
diff -u -p -r1.7 cu.h
--- usr.bin/cu/cu.h     5 Oct 2015 23:15:31 -0000       1.7
+++ usr.bin/cu/cu.h     11 May 2017 11:18:45 -0000
@@ -25,6 +25,7 @@ void                           do_command(char);
 /* cu.c */
 extern FILE                    *record_file;
 extern struct termios           saved_tio;
+extern char                     escape;
 extern int                      line_fd;
 extern struct bufferevent      *line_ev;
 void                            set_blocking(int, int);

Reply via email to