/*
- * Simple terminal
- *
- * This is a minimalist terminal program like minicom or cu. The only
- * thing it does is creating a bidirectional connection between
- * stdin/stdout and a device (e.g. serial terminal). It can also set
- * serial line baudrate and manipulate DTR/RTS modem lines.
+ * Simple serial terminal
*
* Copyright 2014 Michal Sojka <sojkam1@fel.cvut.cz>
*
* <http://www.gnu.org/licenses/>.
*/
+/*
+ * This is a minimalist terminal program like minicom or cu. The only
+ * thing it does is creating a bidirectional connection between
+ * stdin/stdout and a device (e.g. serial terminal). It can also set
+ * serial line baudrate and manipulate DTR/RTS modem lines.
+ *
+ * The -d and -r option create short pulse on DTR/RTS. The lines are
+ * always raised when the device is opened and those options lower the
+ * lines immediately after opening.
+ */
+
#define _BSD_SOURCE
#include <sys/ioctl.h>
#include <unistd.h>
#define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, format, ##__VA_ARGS__); } while (0)
bool verbose = false;
+bool exit_on_escape = true;
char template[] = "/var/lock/TMPXXXXXX";
char lockfile[100];
void sighandler(int arg)
{
- exit(0);
+ exit(0); /* Invoke exit handlers */
+}
+
+int dtr_rts_arg(const char option)
+{
+ int val = -1;
+
+ if (optarg) {
+ switch (optarg[0]) {
+ case '+': val = +1; break;
+ case '-': val = -1; break;
+ default:
+ fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
+ exit(1);
+ }
+ }
+ return val;
+}
+
+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 (buf[i] == *state) {
+ state++;
+ if (*state == 0)
+ exit(0);
+ } else
+ state = escseq;
+ }
}
char *dev = NULL;
int opt;
speed_t speed = 0;
- int ret;
int dtr = 0, rts = 0;
struct termios tio;
bool stdin_tty;
atexit(restore_stdin_term);
}
- while ((opt = getopt(argc, argv, "ndrs:v")) != -1) {
+ while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
switch (opt) {
- case 'd':
- dtr = 1;
- break;
- case 'n':
- raw = false;
- case 'r':
- rts = 1;
- break;
+ case 'd': dtr = dtr_rts_arg(opt); break;
+ case 'e': exit_on_escape = false; break;
+ case 'n': raw = false; break;
+ case 'r': rts = dtr_rts_arg(opt); break;
case 's': {
int s = atoi(optarg);
switch (s) {
CHECK(write(tmp, pid, strlen(pid)));
close(tmp);
snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
+ retry:
if (link(template, lockfile) == -1) {
- perror(lockfile);
- exit(1);
+ tmp = CHECK(open(lockfile, O_RDONLY));
+ CHECK(read(tmp, pid, sizeof(pid)));
+ close(tmp);
+ int p = atoi(pid);
+ char proc[50];
+ snprintf(proc, sizeof(proc), "/proc/%d", p);
+ if (access(proc, F_OK) == 0) {
+ fprintf(stderr, "%s is used by PID %d\n", dev, p);
+ exit(1);
+ }
+ fprintf(stderr, "Stale lock file %s (PID %d) - removing it!\n", lockfile, p);
+ CHECK(unlink(lockfile));
+ goto retry;
}
rm_file(0, template);
on_exit(rm_file, lockfile);
if (dtr || rts) {
int status;
- tio.c_cflag &= ~HUPCL;
+ /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
CHECK(ioctl(fd, TIOCMGET, &status));
- if (dtr == +1) status &= ~TIOCM_DTR;
- if (dtr == -1) status |= TIOCM_DTR;
- if (rts == +1) status &= ~TIOCM_RTS;
- if (rts == -1) status |= TIOCM_RTS;
+ if (dtr == -1) status &= ~TIOCM_DTR;
+ if (dtr == +1) status |= TIOCM_DTR;
+ if (rts == -1) status &= ~TIOCM_RTS;
+ if (rts == +1) status |= TIOCM_RTS;
CHECK(ioctl(fd, TIOCMSET, &status));
}
VERBOSE("Connected.\n");
while (1) {
int r1, r2;
- ret = CHECK(poll(fds, 2, -1));
+ CHECK(poll(fds, 2, -1));
if (fds[0].revents & POLLIN) {
r1 = CHECK(read(0, buf, sizeof(buf)));
if (r1 == 0) {
VERBOSE("EOF on stdin\n");
break;
}
+ if (exit_on_escape)
+ exit_on_escapeseq(buf, r1);
r2 = CHECK(write(fd, buf, r1));
if (r1 != r2) {
fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);