Skip to content

Commit 84d6c97

Browse files
authored
Merge pull request #46 from honeytreelabs/dev-escape-char
microcom: allow for specifying the escape character
2 parents 4100b42 + 8e6af30 commit 84d6c97

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

commands.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ static int cmd_quit(int argc, char *argv[])
155155

156156
static int cmd_sendescape(int argc, char *argv[])
157157
{
158-
ios->write(ios, "\x1c", 1);
158+
unsigned char tmp = CTRL(escape_char);
159+
ios->write(ios, &tmp, 1);
159160
return 0;
160161
}
161162

@@ -234,7 +235,7 @@ static struct cmd cmds[] = {
234235
}, {
235236
.name = "sendescape",
236237
.fn = cmd_sendescape,
237-
.info = "send a Ctrl-\\",
238+
.info = "send the escape sequence",
238239
}, {
239240
.name = "quit",
240241
.fn = cmd_quit,

microcom.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ microcom \- A minimalistic terminal program
1414
.IR host : port ]
1515
.RB [\| \-s
1616
.IR speed \|]
17+
.RB [\| \-e
18+
.IR escape-char \|]
1719
.br
1820
.B microcom -c
1921
.IB interface : rx_id : tx_id
@@ -65,6 +67,9 @@ work in telnet (rfc2217) mode.
6567
.BI \-c\ interface\fB:\fIrx_id\fB:\fItx_id\fR,\ \fI \-\-can= interface\fB:\fIrx_id\fB:\fItx_id
6668
work in CAN mode (default: \fBcan0:200:200\fR)
6769
.TP
70+
.BI \-e\ escape-character \fR,\ \fB\-\-escape-char= char
71+
use specified escape charater with Ctrl (default \fB\\\fR).
72+
.TP
6873
.BR -h ", " \-\-help
6974
Show help.
7075

microcom.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ void main_usage(int exitcode, char *str, char *dev)
101101
" -l, --logfile=<logfile> log output to <logfile>\n"
102102
" -o, --listenonly Do not modify local terminal, do not send input\n"
103103
" from stdin\n"
104-
" -a, --answerback=<str> specify the answerback string sent as response to\n"
104+
" -a, --answerback=<str> specify the answerback string sent as response to\n"
105105
" an ENQ (ASCII 0x05) Character\n"
106+
" -e, --escape-char=<chr> escape charater to use with Ctrl (%c)\n"
106107
" -v, --version print version string\n"
107108
" -h, --help This help\n",
108109
DEFAULT_DEVICE, DEFAULT_BAUDRATE,
109-
DEFAULT_CAN_INTERFACE, DEFAULT_CAN_ID, DEFAULT_CAN_ID);
110+
DEFAULT_CAN_INTERFACE, DEFAULT_CAN_ID, DEFAULT_CAN_ID,
111+
DEFAULT_ESCAPE_CHAR);
110112
fprintf(stderr, "Exitcode %d - %s %s\n\n", exitcode, str, dev);
111113
exit(exitcode);
112114
}
@@ -115,6 +117,7 @@ int opt_force = 0;
115117
unsigned long current_speed = DEFAULT_BAUDRATE;
116118
int current_flow = FLOW_NONE;
117119
int listenonly = 0;
120+
char escape_char = DEFAULT_ESCAPE_CHAR;
118121

119122
int main(int argc, char *argv[])
120123
{
@@ -141,7 +144,7 @@ int main(int argc, char *argv[])
141144
{ },
142145
};
143146

144-
while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:v", long_options, NULL)) != -1) {
147+
while ((opt = getopt_long(argc, argv, "hp:s:t:c:dfl:oi:a:e:v", long_options, NULL)) != -1) {
145148
switch (opt) {
146149
case '?':
147150
main_usage(1, "", "");
@@ -182,6 +185,13 @@ int main(int argc, char *argv[])
182185
case 'a':
183186
answerback = optarg;
184187
break;
188+
case 'e':
189+
if (strlen(optarg) != 1) {
190+
fprintf(stderr, "Option -e requires a single character argument.\n");
191+
exit(EXIT_FAILURE);
192+
}
193+
escape_char = *optarg;
194+
break;
185195
}
186196
}
187197

@@ -229,7 +239,7 @@ int main(int argc, char *argv[])
229239
ios->set_flow(ios, current_flow);
230240

231241
if (!listenonly) {
232-
printf("Escape character: Ctrl-\\\n");
242+
printf("Escape character: Ctrl-%c\n", escape_char);
233243
printf("Type the escape character to get to the prompt.\n");
234244

235245
/* Now deal with the local terminal side */

microcom.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <stdbool.h>
3030
#include <signal.h>
3131
#include <string.h>
32+
#include <sys/ttydefaults.h>
3233
#include <termios.h>
3334
#include <unistd.h>
3435
#include <assert.h>
@@ -37,6 +38,7 @@
3738
#define DEFAULT_DEVICE "/dev/ttyS0"
3839
#define DEFAULT_CAN_INTERFACE "can0"
3940
#define DEFAULT_CAN_ID (0x200)
41+
#define DEFAULT_ESCAPE_CHAR ('\\')
4042

4143
struct ios_ops {
4244
ssize_t (*write)(struct ios_ops *, const void *buf, size_t count);
@@ -73,6 +75,7 @@ extern int debug;
7375
extern int opt_force;
7476
extern int listenonly;
7577
extern char *answerback;
78+
extern char escape_char;
7679

7780
struct cmd {
7881
char *name;

mux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void cook_buf(struct ios_ops *ios, unsigned char *buf, int num)
7373
while (current < num) { /* big while loop, to process all the charactes in buffer */
7474

7575
/* look for the next escape character (Ctrl-\) */
76-
while ((current < num) && (buf[current] != 28))
76+
while ((current < num) && (buf[current] != CTRL(escape_char)))
7777
current++;
7878
/* and write the sequence before esc char to the comm port */
7979
if (current)

0 commit comments

Comments
 (0)