/*
* Simple serial terminal
*
- * Copyright 2014 Michal Sojka <sojkam1@fel.cvut.cz>
+ * Copyright 2014, 2015 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
#define _BSD_SOURCE
#include <sys/ioctl.h>
+#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
+#include <lockdev.h>
#define STRINGIFY(val) #val
#define TOSTRING(val) STRINGIFY(val)
#define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
#define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
-#define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, format, ##__VA_ARGS__); } while (0)
+#define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
bool verbose = false;
+bool exit_on_escape = true;
-char template[] = "/var/lock/TMPXXXXXX";
-char lockfile[100];
struct termios stdin_tio_backup;
+char *dev = NULL;
void rm_file(int status, void *arg)
{
tcsetattr(0, TCSANOW, &stdin_tio_backup);
}
+void unlock()
+{
+ dev_unlock(dev, getpid());
+}
+
void sighandler(int arg)
{
exit(0); /* Invoke exit handlers */
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;
+ if (buf[i] == *state)
+ state++;
+ }
+ }
+}
+
+void usage(const char* argv0)
+{
+ fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
+ fprintf(stderr, "Options:\n");
+ fprintf(stderr, " -d [+|-] create short positive/negative pulse on DTR\n");
+ fprintf(stderr, " -e ignore '~.' escape sequence\n");
+ fprintf(stderr, " -n do not switch the device to raw mode\n");
+ fprintf(stderr, " -r [+|-] create short positive/negative pulse on RTS\n");
+ fprintf(stderr, " -s <baudrate>\n");
+ fprintf(stderr, " -v verbose\n");
+}
int main(int argc, char *argv[])
{
int fd;
- char *dev = NULL;
int opt;
speed_t speed = 0;
int dtr = 0, rts = 0;
atexit(restore_stdin_term);
}
- while ((opt = getopt(argc, argv, "nd::r::s:v")) != -1) {
+ while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
switch (opt) {
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': {
verbose = true;
break;
default: /* '?' */
- fprintf(stderr, "Usage: %s [-s baudrate] [-v] <device>\n", argv[0]);
+ usage(argv[0]);
exit(1);
}
}
if (!dev) {
fprintf(stderr, "No device specified\n");
+ usage(argv[0]);
exit(1);
}
signal(SIGTERM, sighandler);
signal(SIGHUP, sighandler);
- if (strncmp(dev, "/dev/", 5) == 0 &&
- strrchr(dev, '/') == dev + 4 &&
- dev[5] != 0)
- { /* Create lock file (to be inter-operable with other programs) */
- /* This is racy, but what we can do - see also comments in uucp / cu */
- int tmp = CHECK(mkstemp(template));
- on_exit(rm_file, template);
- char pid[20];
- snprintf(pid, sizeof(pid), "%u", getpid());
- CHECK(write(tmp, pid, strlen(pid)));
- close(tmp);
- snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
- if (link(template, lockfile) == -1) {
- perror(lockfile);
- exit(1);
- }
- rm_file(0, template);
- on_exit(rm_file, lockfile);
+ pid_t pid = dev_lock(dev);
+ if (pid > 0) {
+ fprintf(stderr, "%s is used by PID %d\n", dev, pid);
+ exit(1);
+ } else if (pid < 0) {
+ perror("dev_lock()");
+ exit(1);
}
+ atexit(unlock);
- if ((fd = open(dev, O_RDWR)) < 0) {
+ /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
+ if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
perror(dev);
exit(1);
}
+ /* Cancel the efect of O_NONBLOCK flag. */
+ int n = fcntl(fd, F_GETFL, 0);
+ fcntl(fd, F_SETFL, n & ~O_NDELAY);
if (isatty(fd)) {
CHECK(ioctl(fd, TIOCEXCL, NULL));
CHECK(tcsetattr(0, TCSANOW, &tio));
}
- VERBOSE("Connected.\n");
+ VERBOSE("Connected.\r\n");
+ if (exit_on_escape)
+ VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
+
while (1) {
int r1, r2;
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");
+ VERBOSE("EOF on stdin\r\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);
if (fds[1].revents & POLLIN) {
r1 = CHECK(read(fd, buf, sizeof(buf)));
if (r1 == 0) {
- VERBOSE("EOF on %s\n", dev);
+ VERBOSE("EOF on %s\r\n", dev);
break;
}
r2 = CHECK(write(1, buf, r1));