From ae0fde830ad286d1c12fadbea7399e33077f4d15 Mon Sep 17 00:00:00 2001 From: Rainer Poisel Date: Fri, 13 Sep 2024 20:27:32 +0200 Subject: [PATCH] microcom: allow for specifying the escape character Signed-off-by: Rainer Poisel --- microcom.1 | 5 +++++ microcom.c | 18 ++++++++++++++---- microcom.h | 2 ++ mux.c | 2 +- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/microcom.1 b/microcom.1 index 04eac82..8bce123 100644 --- a/microcom.1 +++ b/microcom.1 @@ -14,6 +14,8 @@ microcom \- A minimalistic terminal program .IR host : port ] .RB [\| \-s .IR speed \|] +.RB [\| \-e +.IR escape-char \|] .br .B microcom -c .IB interface : rx_id : tx_id @@ -65,6 +67,9 @@ work in telnet (rfc2217) mode. .BI \-c\ interface\fB:\fIrx_id\fB:\fItx_id\fR,\ \fI \-\-can= interface\fB:\fIrx_id\fB:\fItx_id work in CAN mode (default: \fBcan0:200:200\fR) .TP +.BI \-e\ escape-character \fR,\ \fB\-\-escape-char= char +use specified escape charater with Ctrl (default \fB\\\fR). +.TP .BR -h ", " \-\-help Show help. diff --git a/microcom.c b/microcom.c index 2b3d484..9f2cfc2 100644 --- a/microcom.c +++ b/microcom.c @@ -99,12 +99,14 @@ void main_usage(int exitcode, char *str, char *dev) " -l, --logfile= log output to \n" " -o, --listenonly Do not modify local terminal, do not send input\n" " from stdin\n" - " -a, --answerback= specify the answerback string sent as response to\n" + " -a, --answerback= specify the answerback string sent as response to\n" " an ENQ (ASCII 0x05) Character\n" + " -e, --escape-char= escape charater to use with Ctrl (%c)\n" " -v, --version print version string\n" " -h, --help This help\n", DEFAULT_DEVICE, DEFAULT_BAUDRATE, - DEFAULT_CAN_INTERFACE, DEFAULT_CAN_ID, DEFAULT_CAN_ID); + DEFAULT_CAN_INTERFACE, DEFAULT_CAN_ID, DEFAULT_CAN_ID, + DEFAULT_ESCAPE_CHAR); fprintf(stderr, "Exitcode %d - %s %s\n\n", exitcode, str, dev); exit(exitcode); } @@ -113,6 +115,7 @@ int opt_force = 0; unsigned long current_speed = DEFAULT_BAUDRATE; int current_flow = FLOW_NONE; int listenonly = 0; +char escape_char = DEFAULT_ESCAPE_CHAR; int main(int argc, char *argv[]) { @@ -139,7 +142,7 @@ int main(int argc, char *argv[]) { }, }; - while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:v", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:e:v", long_options, NULL)) != -1) { switch (opt) { case '?': main_usage(1, "", ""); @@ -180,6 +183,13 @@ int main(int argc, char *argv[]) case 'a': answerback = optarg; break; + case 'e': + if (strlen(optarg) != 1) { + fprintf(stderr, "Option -e requires a single character argument.\n"); + exit(EXIT_FAILURE); + } + escape_char = *optarg; + break; } } @@ -224,7 +234,7 @@ int main(int argc, char *argv[]) ios->set_flow(ios, current_flow); if (!listenonly) { - printf("Escape character: Ctrl-\\\n"); + printf("Escape character: Ctrl-%c\n", escape_char); printf("Type the escape character to get to the prompt.\n"); /* Now deal with the local terminal side */ diff --git a/microcom.h b/microcom.h index d1c7035..1f19ea8 100644 --- a/microcom.h +++ b/microcom.h @@ -37,6 +37,7 @@ #define DEFAULT_DEVICE "/dev/ttyS0" #define DEFAULT_CAN_INTERFACE "can0" #define DEFAULT_CAN_ID (0x200) +#define DEFAULT_ESCAPE_CHAR ('\\') struct ios_ops { ssize_t (*write)(struct ios_ops *, const void *buf, size_t count); @@ -73,6 +74,7 @@ extern int debug; extern int opt_force; extern int listenonly; extern char *answerback; +extern char escape_char; struct cmd { char *name; diff --git a/mux.c b/mux.c index 44ba163..d15012b 100644 --- a/mux.c +++ b/mux.c @@ -73,7 +73,7 @@ static void cook_buf(struct ios_ops *ios, unsigned char *buf, int num) while (current < num) { /* big while loop, to process all the charactes in buffer */ /* look for the next escape character (Ctrl-\) */ - while ((current < num) && (buf[current] != 28)) + while ((current < num) && (buf[current] != CTRL(escape_char))) current++; /* and write the sequence before esc char to the comm port */ if (current)