nuclear@0: /* dterm.c Dumb terminal program 19/11/2004/dcs nuclear@0: */ nuclear@0: nuclear@0: /* nuclear@0: * Copyright 2007 Knossos Networks Ltd. nuclear@0: * Copyright 2017 John Tsiombikas nuclear@0: * nuclear@0: * This program is free software; you can redistribute it and/or nuclear@0: * modify it under the terms of the GNU General Public License version 2 nuclear@0: * as published by the Free Software Foundation. nuclear@0: * nuclear@0: * This program is distributed in the hope that it will be useful, nuclear@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@0: * GNU General Public License nuclear@0: * nuclear@0: * You should have received a copy of the GNU General Public License nuclear@0: * along with this program; if not, write to the Free Software nuclear@0: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA nuclear@0: * 02110-1301, USA. nuclear@0: */ nuclear@0: nuclear@0: #ifndef VERSION nuclear@0: #define VERSION "unknown" nuclear@0: #endif nuclear@0: #define COPYRIGHT \ nuclear@0: "dterm version " VERSION " Copyright 2007 Knossos Networks Ltd.\n" \ nuclear@0: " Copyright 2017 John Tsiombikas \n" \ nuclear@0: "\n" \ nuclear@0: "This program is free software; you can redistribute it and/or\n" \ nuclear@0: "modify it under the terms of the GNU General Public License version 2\n" \ nuclear@0: "as published by the Free Software Foundation.\n" \ nuclear@0: "\n" \ nuclear@0: "This program is distributed in the hope that it will be useful,\n" \ nuclear@0: "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \ nuclear@0: "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \ nuclear@0: "GNU General Public License for more details.\n" \ nuclear@0: "\n" \ nuclear@0: "You should have received a copy of the GNU General Public License\n" \ nuclear@0: "along with this program; if not, write to the Free Software\n" \ nuclear@0: "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n" \ nuclear@0: "02110-1301, USA.\n" nuclear@0: nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: nuclear@0: #define FIXTTY if(istty) tcsetattr(0, TCSADRAIN, &savetio) nuclear@0: #define DIEP(s) { FIXTTY; perror(s); exit(1); } nuclear@0: #define DIE(s,m) { FIXTTY; fprintf(stderr, "%s: %s\n", s, m); exit(1); } nuclear@0: nuclear@0: /* nuclear@0: Sigh nuclear@0: */ nuclear@0: #ifndef CCTS_OFLOW nuclear@0: #ifdef CRTSCTS nuclear@0: #define CCTS_OFLOW CRTSCTS nuclear@0: #endif /* CRTSCTS */ nuclear@0: #endif /* ! CCTS_OFLOW */ nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Valid speed settings. nuclear@0: */ nuclear@0: static struct { int s, c; } speeds[] = { nuclear@0: { 50, B50 }, nuclear@0: { 75, B75 }, nuclear@0: { 110, B110 }, nuclear@0: { 134, B134 }, nuclear@0: { 150, B150 }, nuclear@0: { 200, B200 }, nuclear@0: { 300, B300 }, nuclear@0: { 600, B600 }, nuclear@0: { 1200, B1200 }, nuclear@0: { 1800, B1800 }, nuclear@0: { 2400, B2400 }, nuclear@0: { 4800, B4800 }, nuclear@0: { 9600, B9600 }, nuclear@0: { 19200, B19200 }, nuclear@0: { 38400, B38400 }, nuclear@0: #ifdef B7200 nuclear@0: { 7200, B7200 }, nuclear@0: #endif nuclear@0: #ifdef B14400 nuclear@0: { 14400, B14400 }, nuclear@0: #endif nuclear@0: #ifdef B28800 nuclear@0: { 28800, B28800 }, nuclear@0: #endif nuclear@0: #ifdef B57600 nuclear@0: { 57600, B57600 }, nuclear@0: #endif nuclear@0: #ifdef B76800 nuclear@0: { 76800, B76800 }, nuclear@0: #endif nuclear@0: #ifdef B115200 nuclear@0: { 115200, B115200 }, nuclear@0: #endif nuclear@0: #ifdef B230400 nuclear@0: { 230400, B230400 }, nuclear@0: #endif nuclear@0: #ifdef B460800 nuclear@0: { 460800, B460800 }, nuclear@0: #endif nuclear@0: #ifdef B921600 nuclear@0: { 921600, B921600 }, nuclear@0: #endif nuclear@0: { 0, 0 }}; nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Save the terminal settings here nuclear@0: */ nuclear@0: static struct termios intio, savetio; nuclear@0: nuclear@0: static int fd = -1; /* Channel to port */ nuclear@0: static int cmdchar = 035; /* Command character, default ^] */ nuclear@0: static char *device = 0; /* Device, no default */ nuclear@0: static int ispeed = B9600; /* Input speed, default 9600 bps */ nuclear@0: static int ospeed = B9600; /* Output speed, default 9600 bps */ nuclear@0: static int twostop = 0; /* Stop bits, if set to CSTOPB, use two */ nuclear@0: static int parity = 0; /* Parity, -1 = mark, -2 = space 0=none */ nuclear@0: /* PARENB enables parity, PARODD = odd */ nuclear@0: static int bits = CS8; /* CS5-CS8 for 5-8 bits per character */ nuclear@0: static int ctsflow = 0; /* CCTS_OFLOW enables CTS flow control */ nuclear@0: static int xonflow = 0; /* IXON | IXOFF for in & out soft F/c */ nuclear@0: static int modemcontrol = 0; /* Set to HUPCLto enable modem control */ nuclear@0: static int markparity = 0; /* Mark parity -- set high bit on out */ nuclear@0: static int backspace = 0; /* Backspace char, send on BS or DEL */ nuclear@0: static int maplf = 0; /* If set, map LF to CR */ nuclear@0: static int ignorecr = 0; /* Ignore carriage returns */ nuclear@0: static int crlf = 0; /* Send CRLF on CR */ nuclear@0: static int istty; /* Result of isatty() */ nuclear@0: static char *connname = 0; /* Connection name found in config */ nuclear@0: static int sendbreak = 0; /* Break requested */ nuclear@0: static int modem = 0; /* Current modem status */ nuclear@0: static int setmodem = 0; /* Set modem status to this */ nuclear@0: static int delay = 0; /* Millisecond delay after each key */ nuclear@0: static int linedelay = 0; /* Millisecond delay after each CR */ nuclear@0: static int showspecial = 0; /* Show special chars as [xx] */ nuclear@0: nuclear@0: static void nuclear@0: help() { nuclear@0: fprintf(stderr, nuclear@0: "dterm commands:\n" nuclear@0: "^%c Enter command mode quit, q Exit\n" nuclear@0: "show Display configuration help, h, ? Display this message\n" nuclear@0: "! Execute shell command version Display version\n" nuclear@0: "@ Get configuration from \n" nuclear@0: " Connect to 300,1200,9600 Set speed\n" nuclear@0: "5, 6, 7, 8 Set bits per character 1, 2 Set stop bits\n" nuclear@0: "e, o, n, m, s Set parity" nuclear@0: #ifdef CCTS_OFLOW nuclear@0: " cts,nocts Enable / disable CTS\n" nuclear@0: #else nuclear@0: "\n" nuclear@0: #endif nuclear@0: "dtr, nodtr Raise / lower DTR b Send a 500ms break\n" nuclear@0: "rts, norts Raise / lower RTS d, r Toggle DTR, RTS\n" nuclear@0: "rx Receive file (XMODEM) sx Send file (XMODEM)\n" nuclear@0: "rz Receive file (ZMODEM) sz Send file (ZMODEM)\n" nuclear@0: "xon,noxon Enable / disable XON/XOFF flow control\n" nuclear@0: "modem Hang up modem on exit, exit if modem hangs up\n" nuclear@0: "nomodem Do not do modem control\n" nuclear@0: "bs, nobs Enable / disable mapping of Delete to Backspace\n" nuclear@0: "del, nodel Enable / disable mapping of Backspace to Delete\n" nuclear@0: "maplf, nomaplf Enable / disable mapping of LF to CR\n" nuclear@0: "igncr, noigncr Ignore / output carriage returns\n" nuclear@0: "crlf, nocrlf Enable / disable sending LF after each CR\n" nuclear@0: "delay= Add delay of ms after each charachter sent\n" nuclear@0: "crwait= Add delay of ms after each line sent\n" nuclear@0: "esc= Set command mode character to Ctrl/\n" nuclear@0: "ctrl,hex,noctrl Show control characters as ^n (except tab, CR, LF)\n" nuclear@0: nuclear@0: nuclear@0: nuclear@0: , cmdchar + '@'); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Show current setup nuclear@0: */ nuclear@0: static void nuclear@0: showsetup() { nuclear@0: int i; nuclear@0: fprintf(stderr, "Port: %s\n", device); nuclear@0: fprintf(stderr, "Communications parameters: "); nuclear@0: for(i = 0; speeds[i].s && speeds[i].c != ispeed; ) nuclear@0: i++; nuclear@0: fprintf(stderr, "%d", speeds[i].s); nuclear@0: if(ospeed != ispeed) { nuclear@0: for(i = 0; speeds[i].s && speeds[i].c != ospeed; ) nuclear@0: i++; nuclear@0: fprintf(stderr, "/%d", speeds[i].s); nuclear@0: } nuclear@0: if(bits == CS5) fprintf(stderr, " 5"); nuclear@0: if(bits == CS6) fprintf(stderr, " 6"); nuclear@0: if(bits == CS7) fprintf(stderr, " 7"); nuclear@0: if(bits == CS8) fprintf(stderr, " 8"); nuclear@0: if(parity == 0) fprintf(stderr, " n"); nuclear@0: if(parity == PARENB) fprintf(stderr, " e"); nuclear@0: if(parity == (PARENB|PARODD)) fprintf(stderr, " o"); nuclear@0: if(parity == -1) fprintf(stderr, " m"); nuclear@0: if(parity == -2) fprintf(stderr, " s"); nuclear@0: if(twostop) fprintf(stderr, " 2\n"); nuclear@0: else fprintf(stderr, " 1\n"); nuclear@0: fprintf(stderr, "Flow/modem control:"); nuclear@0: if(xonflow) fprintf(stderr, " xon"); nuclear@0: if(ctsflow) fprintf(stderr, " cts"); nuclear@0: if(modemcontrol)fprintf(stderr, " modem"); nuclear@0: putc('\n', stderr); nuclear@0: fprintf(stderr, "Character mappings:"); nuclear@0: if(backspace == 8) fprintf(stderr, " bs"); nuclear@0: if(backspace == 127) fprintf(stderr, " del"); nuclear@0: if(maplf) fprintf(stderr, " maplf"); nuclear@0: if(ignorecr) fprintf(stderr, " igncr"); nuclear@0: if(crlf) fprintf(stderr, " crlf"); nuclear@0: if(showspecial == 1) fprintf(stderr, " ctrl"); nuclear@0: if(showspecial == 2) fprintf(stderr, " hex"); nuclear@0: putc('\n', stderr); nuclear@0: printf("Modem control: DTR: %s RTS: %s CTS: %s DSR: %s DCD: %s\n", nuclear@0: setmodem & TIOCM_DTR ? "on" : "off", nuclear@0: setmodem & TIOCM_RTS ? "on" : "off", nuclear@0: modem & TIOCM_CTS ? "on" : "off", nuclear@0: modem & TIOCM_DSR ? "on" : "off", nuclear@0: modem & TIOCM_CD ? "on" : "off"); nuclear@0: fprintf(stderr, "Escape character: ^%c\n", cmdchar + '@'); nuclear@0: fflush(stderr); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Use the rzsz or lrzsz package to do file transfer nuclear@0: Mode should be "rx", "rb" or "rz" to receive a file, "sx", "sb" or "sz" nuclear@0: to send. "rz" & "rb" don't require a file name; the others do. nuclear@0: */ nuclear@0: static int nuclear@0: rzsz(char *mode, char *file) { nuclear@0: static char *loc[] = { "/usr/bin/", "/usr/local/bin/l", nuclear@0: "/usr/bin/l", "/usr/local/bin/", 0 }; nuclear@0: char path[128]; nuclear@0: char *cmd; nuclear@0: int i; nuclear@0: struct stat sb; nuclear@0: pid_t pid; nuclear@0: nuclear@0: /* nuclear@0: Check file name nuclear@0: */ nuclear@0: if(*file == 0 && (mode[0] != 'r' || mode[1] == 'x')) { nuclear@0: fprintf(stderr, "File name required for %c%c\n", nuclear@0: mode[0], mode[1]); nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Find the appropriate command nuclear@0: */ nuclear@0: for(i = 0; loc[i]; i++) { nuclear@0: if(mode[1] == 'a') { nuclear@0: sprintf(path, "%sascii-xfr", loc[i]); nuclear@0: } else { nuclear@0: sprintf(path, "%s%c%c", loc[i], mode[0], mode[1]); nuclear@0: } nuclear@0: if(!stat(path, &sb) && (sb.st_mode & S_IXUSR)) nuclear@0: break; nuclear@0: } nuclear@0: if(!loc[i]) { nuclear@0: fprintf(stderr, "RZ/SZ/ascii-xfr not installed\n"); nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Get the command name nuclear@0: */ nuclear@0: cmd = strrchr(path, '/'); nuclear@0: if(cmd) cmd++; nuclear@0: else cmd = path; nuclear@0: nuclear@0: /* nuclear@0: Fork subprocess. Set stdin & stdout to the serial line nuclear@0: stderr remains on the console for progress messages nuclear@0: */ nuclear@0: pid = fork(); nuclear@0: if(pid == 0) { nuclear@0: dup2(fd, 0); nuclear@0: dup2(fd, 1); nuclear@0: if(mode[1] == 'a') { nuclear@0: char delay_str[64]; nuclear@0: sprintf(delay_str, "%d", linedelay); nuclear@0: if(*file) execl(path, cmd, "-s", "-l", delay_str, file, NULL); nuclear@0: else execl(path, cmd, "-r", NULL); nuclear@0: } else { nuclear@0: if(*file) execl(path, cmd, file, NULL); nuclear@0: else execl(path, cmd, NULL); nuclear@0: } nuclear@0: _exit(127); nuclear@0: } nuclear@0: else if(pid == -1) { nuclear@0: perror("fork"); nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Wait for the process to complete and give a meaningful nuclear@0: response nuclear@0: */ nuclear@0: signal(SIGINT, SIG_IGN); nuclear@0: signal(SIGQUIT, SIG_IGN); nuclear@0: signal(SIGTERM, SIG_IGN); nuclear@0: while(waitpid(pid, &i, 0) != pid) {} nuclear@0: signal(SIGINT, SIG_DFL); nuclear@0: signal(SIGQUIT, SIG_DFL); nuclear@0: signal(SIGTERM, SIG_DFL); nuclear@0: nuclear@0: if(WIFSIGNALED(i)) { nuclear@0: fprintf(stderr, "Terminated on signal %d%s\n", WTERMSIG(i), nuclear@0: WCOREDUMP(i) ? " (core dumped)" : ""); nuclear@0: return -1; nuclear@0: } nuclear@0: if(WTERMSIG(i)) nuclear@0: return -1; nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: nuclear@0: static int readconfig(char *, char *); nuclear@0: nuclear@0: /* nuclear@0: Process commands passed as strings, may be several commands on a line nuclear@0: Allow ugliness like 8n1 -- treat as 8, n, 1 nuclear@0: */ nuclear@0: static int nuclear@0: setup(char *s, char *cffile, int cfline) { nuclear@0: char *t, *d; nuclear@0: int j,k; nuclear@0: int ret = 0; nuclear@0: struct stat sb; nuclear@0: char ttybuf[128]; nuclear@0: int justospeed = 0; nuclear@0: nuclear@0: while(*s) { nuclear@0: /* nuclear@0: Skip whitespace, commas nuclear@0: */ nuclear@0: while(isspace(*s) || *s == ',') nuclear@0: s++; nuclear@0: if(!*s) break; nuclear@0: nuclear@0: /* nuclear@0: '?' = help nuclear@0: */ nuclear@0: if(*s == '?') { nuclear@0: help(); nuclear@0: return -4; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: '!' = shell command nuclear@0: */ nuclear@0: if(*s == '!') { nuclear@0: if((t = strchr(s, '\n'))) nuclear@0: *t = 0; nuclear@0: system(++s); nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: File transfer (sx, sz, rx, rz, sa, ra) nuclear@0: Run rzsz or ascii-xfr to perform the actual file transfer nuclear@0: Allow "sxfilename", "sx filename" or just "rz" nuclear@0: */ nuclear@0: if((s[0] == 's' || s[0] == 'r') && nuclear@0: (s[1] == 'x' || s[1] == 'z' || s[1] == 'a')) { nuclear@0: if((t = strchr(s, '\n'))) nuclear@0: *t = 0; nuclear@0: for(t = s + 2; isspace(*t); ) t++; nuclear@0: return rzsz(s, t); nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: If a number, process it. nuclear@0: If it's a valid speed, use that -- if nnn/mmm, set both to nnn nuclear@0: for the first time, and flag things so mmm just sets ospeed nuclear@0: 5-8 = data bits, 1,2 sets stops nuclear@0: Anything else is error nuclear@0: */ nuclear@0: if(isdigit(*s)) { nuclear@0: j = strtol(s, &s, 10); nuclear@0: for(k = 1; speeds[k].s; k++) nuclear@0: if(speeds[k].s == j) nuclear@0: break; nuclear@0: if(speeds[k].s) { nuclear@0: ospeed = speeds[k].c; nuclear@0: if(!justospeed) nuclear@0: ispeed = ospeed; nuclear@0: if(*s == '/') { nuclear@0: s++; nuclear@0: justospeed = 1; nuclear@0: } nuclear@0: else justospeed = 0; nuclear@0: } nuclear@0: else if(j == 5) bits = CS5; nuclear@0: else if(j == 6) bits = CS6; nuclear@0: else if(j == 7) bits = CS7; nuclear@0: else if(j == 8) bits = CS8; nuclear@0: else if(j == 1) twostop = 0; nuclear@0: else if(j == 2) twostop = CSTOPB; nuclear@0: else { nuclear@0: if(cffile) nuclear@0: fprintf(stderr, "in %s:%d: ", nuclear@0: cffile, cfline); nuclear@0: fprintf(stderr, "%d: invalid speed/bits\n", j); nuclear@0: ret = -1; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Otherwise, get the alpha-only keyword, see if it matches anything nuclear@0: useful. nuclear@0: */ nuclear@0: else { nuclear@0: t = s; nuclear@0: while(isalpha(*t)) t++; nuclear@0: j = *t; nuclear@0: *t = 0; nuclear@0: if( !strcasecmp(s, "xon")) nuclear@0: xonflow = IXON | IXOFF; nuclear@0: else if(!strcasecmp(s, "noxon")) nuclear@0: xonflow = 0; nuclear@0: nuclear@0: #ifdef CCTS_OFLOW nuclear@0: else if(!strcasecmp(s, "cts")) nuclear@0: ctsflow = CCTS_OFLOW; nuclear@0: else if(!strcasecmp(s, "nocts")) nuclear@0: ctsflow = 0; nuclear@0: #endif nuclear@0: else if(!strcasecmp(s, "modem")) nuclear@0: modemcontrol = HUPCL; nuclear@0: else if(!strcasecmp(s, "nomodem")) nuclear@0: modemcontrol = 0; nuclear@0: else if(!strcasecmp(s, "E")) nuclear@0: parity = PARENB; nuclear@0: else if(!strcasecmp(s, "O")) nuclear@0: parity = PARENB | PARODD; nuclear@0: else if(!strcasecmp(s, "M")) nuclear@0: parity = -1; nuclear@0: else if(!strcasecmp(s, "S")) nuclear@0: parity = -2; nuclear@0: else if(!strcasecmp(s, "N")) nuclear@0: parity = 0; nuclear@0: else if(!strcasecmp(s, "q") || !strcasecmp(s, "quit")) nuclear@0: ret = -3; nuclear@0: else if(!strcasecmp(s, "h") || !strcasecmp(s, "help")) nuclear@0: help(); nuclear@0: else if(!strcasecmp(s, "del")) nuclear@0: backspace = 127; nuclear@0: else if(!strcasecmp(s, "bs")) nuclear@0: backspace = 8; nuclear@0: else if(!strcasecmp(s,"nobs") || !strcasecmp(s,"nodel")) nuclear@0: backspace = 0; nuclear@0: else if(!strcasecmp(s, "noesc")) nuclear@0: cmdchar = 0; nuclear@0: else if(!strcasecmp(s, "maplf")) nuclear@0: maplf = 1; nuclear@0: else if(!strcasecmp(s, "nomaplf")) nuclear@0: maplf = 0; nuclear@0: else if(!strcasecmp(s, "igncr")) nuclear@0: ignorecr = 1; nuclear@0: else if(!strcasecmp(s, "noigncr")) nuclear@0: ignorecr = 0; nuclear@0: else if(!strcasecmp(s, "crlf")) nuclear@0: crlf = 1; nuclear@0: else if(!strcasecmp(s, "nocrlf")) nuclear@0: crlf = 0; nuclear@0: else if(!strcasecmp(s, "b")) nuclear@0: sendbreak = 1; nuclear@0: else if(!strcasecmp(s, "d")) nuclear@0: setmodem ^= TIOCM_DTR; nuclear@0: else if(!strcasecmp(s, "r")) nuclear@0: setmodem ^= TIOCM_RTS; nuclear@0: else if(!strcasecmp(s, "dtr")) nuclear@0: setmodem |= TIOCM_DTR; nuclear@0: else if(!strcasecmp(s, "rts")) nuclear@0: setmodem |= TIOCM_RTS; nuclear@0: else if(!strcasecmp(s, "nodtr")) nuclear@0: setmodem &= ~TIOCM_DTR; nuclear@0: else if(!strcasecmp(s, "norts")) nuclear@0: setmodem &= ~TIOCM_RTS; nuclear@0: else if(!strcasecmp(s, "show")) nuclear@0: showsetup(); nuclear@0: else if(!strcasecmp(s, "version")) nuclear@0: fputs(COPYRIGHT, stderr); nuclear@0: else if(!strcasecmp(s, "ctrl")) nuclear@0: showspecial = 1; nuclear@0: else if(!strcasecmp(s, "hex")) nuclear@0: showspecial = 2; nuclear@0: else if(!strcasecmp(s, "noctrl") || nuclear@0: !strcasecmp(s, "noctrl")) nuclear@0: showspecial = 0; nuclear@0: nuclear@0: /* nuclear@0: No? nuclear@0: @ includes a file nuclear@0: !cmd Run a command nuclear@0: esc=c sets escape char nuclear@0: select device nuclear@0: */ nuclear@0: else { nuclear@0: *t = j; nuclear@0: while(*t && !isspace(*t) && *t != ',') nuclear@0: t++; nuclear@0: j = *t; nuclear@0: *t = 0; nuclear@0: if(*s == '@') { nuclear@0: k = readconfig(++s, 0); nuclear@0: if(k == -2) { nuclear@0: if(cffile) nuclear@0: fprintf(stderr, nuclear@0: "in %s:%d: ", nuclear@0: cffile, cfline); nuclear@0: fprintf(stderr, "%s: %s\n", nuclear@0: s, strerror(errno)); nuclear@0: ret = -1; nuclear@0: } nuclear@0: goto next; nuclear@0: } nuclear@0: if(!strncasecmp(s, "esc=", 4)) { nuclear@0: cmdchar = s[4] & 0x1f; nuclear@0: goto next; nuclear@0: } nuclear@0: else if(!strncasecmp(s, "delay=", 6)) { nuclear@0: delay = atoi(s+6); nuclear@0: goto next; nuclear@0: } nuclear@0: else if(!strncasecmp(s, "crwait=", 7)) { nuclear@0: linedelay = atoi(s+7); nuclear@0: goto next; nuclear@0: } nuclear@0: d = s; nuclear@0: k = stat(d, &sb); nuclear@0: if(k && *d != '/') { nuclear@0: sprintf(ttybuf, "/dev/%.100s", d); nuclear@0: d = ttybuf; nuclear@0: k = stat(d, &sb); nuclear@0: } nuclear@0: if(!k) { nuclear@0: if((sb.st_mode & S_IFMT) == S_IFCHR) { nuclear@0: ret = 1; nuclear@0: device = strdup(d); nuclear@0: goto next; nuclear@0: } nuclear@0: } nuclear@0: if(cffile) nuclear@0: fprintf(stderr, "in %s:%d: ", nuclear@0: cffile, cfline); nuclear@0: fprintf(stderr, nuclear@0: "%s: unrecognised keyword/device\n", s); nuclear@0: ret = -1; nuclear@0: } nuclear@0: next: *t = j; nuclear@0: s = t; nuclear@0: } nuclear@0: } nuclear@0: return ret; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Read a config file nuclear@0: Input lines can be lists of config words, or in the form nuclear@0: name: words nuclear@0: If name: form, only lines matching passed name are used nuclear@0: */ nuclear@0: static int nuclear@0: readconfig(char *file, char *name) { nuclear@0: char buf[1024]; nuclear@0: FILE *f; nuclear@0: char *s, *t; nuclear@0: int lineno = 0; nuclear@0: int ret = 0, r; nuclear@0: nuclear@0: /* nuclear@0: ~/file = get file from $HOME/file nuclear@0: */ nuclear@0: if(*file == '~' && file[1] == '/' && (s = getenv("HOME"))) { nuclear@0: snprintf(buf, sizeof(buf), "%s/%s", s, file+2); nuclear@0: file = buf; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Return -2 if can't open nuclear@0: */ nuclear@0: f = fopen(file, "r"); nuclear@0: if(!f) return -2; nuclear@0: nuclear@0: /* nuclear@0: Read input, strip # commends nuclear@0: Count lines nuclear@0: Keep track of return code nuclear@0: */ nuclear@0: while(fgets(buf, sizeof(buf), f)) { nuclear@0: lineno++; nuclear@0: if((s = strchr(buf, '#'))) nuclear@0: *s = 0; nuclear@0: for(s = buf; isspace(*s);) nuclear@0: s++; nuclear@0: if(!*s) continue; nuclear@0: for(t = s; *t && *t != ':' && *t != ',' && !isspace(*t); ) nuclear@0: t++; nuclear@0: if(*t == ':') { nuclear@0: *t++ = 0; nuclear@0: if(!name) nuclear@0: continue; nuclear@0: if(strcmp(name, s)) nuclear@0: continue; nuclear@0: s = t; nuclear@0: connname = name; nuclear@0: } nuclear@0: nuclear@0: r = setup(s, file, lineno); nuclear@0: if(r < 0) nuclear@0: ret = r; nuclear@0: if(r > 0 && !ret) nuclear@0: ret = 1; nuclear@0: } nuclear@0: nuclear@0: fclose(f); nuclear@0: return ret; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Sleep n milliseconds nuclear@0: */ nuclear@0: static void nuclear@0: millisleep(int n) { nuclear@0: struct timespec t; nuclear@0: t.tv_sec = n / 1000; nuclear@0: t.tv_nsec = (n % 1000) * 1000000; nuclear@0: nanosleep(&t, 0); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Set up the port nuclear@0: */ nuclear@0: static int nuclear@0: setupport(int fd) { nuclear@0: int parityflags; nuclear@0: int modemflags; nuclear@0: int spaceparity = 0; nuclear@0: struct termios tio; nuclear@0: int m; nuclear@0: nuclear@0: /* nuclear@0: See what to do with parity nuclear@0: */ nuclear@0: markparity = 0; nuclear@0: parityflags = parity; nuclear@0: if(parity == -1) { nuclear@0: parityflags = 0; nuclear@0: markparity = ISTRIP; nuclear@0: } nuclear@0: else if(parity == -2) { nuclear@0: parityflags = 0; nuclear@0: spaceparity = ISTRIP; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: If no modem control, use local mode nuclear@0: */ nuclear@0: modemflags = ctsflow | modemcontrol; nuclear@0: if(!modemflags) nuclear@0: modemflags = CLOCAL; nuclear@0: nuclear@0: /* nuclear@0: Set the speed and params nuclear@0: */ nuclear@0: tcgetattr(fd, &tio); nuclear@0: tio.c_iflag = IGNBRK | IGNPAR | xonflow | spaceparity | markparity; nuclear@0: tio.c_cflag = CREAD | HUPCL | modemflags | bits | parityflags; nuclear@0: tio.c_oflag = 0; nuclear@0: tio.c_lflag = 0; nuclear@0: cfsetispeed(&tio, ispeed); nuclear@0: cfsetospeed(&tio, ospeed); nuclear@0: if(tcsetattr(fd, TCSAFLUSH, &tio) == -1) nuclear@0: return -1; nuclear@0: nuclear@0: /* nuclear@0: Set up the modem lines nuclear@0: */ nuclear@0: ioctl(fd, TIOCMGET, &modem); nuclear@0: if((modem & (TIOCM_RTS | TIOCM_DTR)) != (setmodem & (TIOCM_RTS | nuclear@0: TIOCM_DTR))) { nuclear@0: m = setmodem & (TIOCM_RTS | TIOCM_DTR); nuclear@0: ioctl(fd, TIOCMBIS, &m); nuclear@0: m = (~setmodem) & (TIOCM_RTS | TIOCM_DTR); nuclear@0: ioctl(fd, TIOCMBIC, &m); nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Send a break if requested nuclear@0: */ nuclear@0: if(sendbreak) { nuclear@0: ioctl(fd, TIOCSBRK, 0); nuclear@0: millisleep(500); nuclear@0: ioctl(fd, TIOCCBRK, 0); nuclear@0: sendbreak = 0; nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Get the current modem status nuclear@0: */ nuclear@0: ioctl(fd, TIOCMGET, &modem); nuclear@0: setmodem = modem; nuclear@0: nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Set up the modem. This is a little tricky due to the need to nuclear@0: ensure the modem does not block before we set it up. nuclear@0: */ nuclear@0: static int nuclear@0: openport(char *device) { nuclear@0: int fd; nuclear@0: nuclear@0: if((fd = open(device, O_RDWR|O_NONBLOCK, 0)) < 0) nuclear@0: DIEP(device) nuclear@0: if(setupport(fd) < 0) nuclear@0: DIEP(device) nuclear@0: millisleep(10); nuclear@0: fcntl(fd, F_SETFL, 0); nuclear@0: nuclear@0: ioctl(fd, TIOCMGET, &modem); nuclear@0: setmodem = modem; nuclear@0: nuclear@0: setenv("DTERM_PORT", device, 1); nuclear@0: return fd; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /* nuclear@0: Usage nuclear@0: */ nuclear@0: static void nuclear@0: usage(char *this) { nuclear@0: fprintf(stderr, "Usage: %s port/setup\n" nuclear@0: " '%s help' for help\n", this, this); nuclear@0: exit(1); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: int nuclear@0: main(int argc, char **argv) { nuclear@0: int nfd; nuclear@0: int i, j, c; nuclear@0: unsigned char inbuf; nuclear@0: char buf[256]; nuclear@0: char cbuf[8]; nuclear@0: char *s; nuclear@0: int done; nuclear@0: fd_set fds; nuclear@0: struct timeval delay_tv, *readdelay; nuclear@0: struct stat sb; nuclear@0: nuclear@0: /* nuclear@0: Do the right things depending on whether stdin & stdout are TTYs nuclear@0: If the input is a TTY, we need to put it into non-canonical mode nuclear@0: (which we'll actually do later); otherwise, default to turning nuclear@0: LFs from a file into CRs. (See [no]maplf setup command.) nuclear@0: If the output is a TTY, default to passing CR transparently; nuclear@0: otherwise default to ignoring CR so that output is logged to a nuclear@0: files etc nicely. (See [no]igncr.) nuclear@0: */ nuclear@0: istty = isatty(0); nuclear@0: if(istty) { nuclear@0: tcgetattr(0, &savetio); nuclear@0: maplf = 0; nuclear@0: } nuclear@0: else maplf = 1; nuclear@0: if(isatty(1)) nuclear@0: ignorecr = 0; nuclear@0: else ignorecr = 1; nuclear@0: nuclear@0: /* nuclear@0: Read default config, if no private .dtermrc, use /etc/dtermrc nuclear@0: */ nuclear@0: setmodem = modem = TIOCM_DTR | TIOCM_RTS; nuclear@0: if(readconfig("~/.dtermrc", argv[1]) == -2) nuclear@0: readconfig("/etc/dtermrc", argv[1]); nuclear@0: nuclear@0: /* nuclear@0: Parse args nuclear@0: If only arg is "help", exit nuclear@0: */ nuclear@0: i = 1; nuclear@0: if(connname) i = 2; nuclear@0: for(; i < argc; i++) nuclear@0: if(setup(argv[i], 0, 0) < 0) nuclear@0: usage(argv[0]); nuclear@0: if(argc == 2 && !strcasecmp(argv[1], "help")) nuclear@0: exit(0); nuclear@0: nuclear@0: /* nuclear@0: If no device specified, have a crack at finding a default device nuclear@0: Look for the first on-board device first; failing that look for nuclear@0: the first USB device. For Linux & BSD these are: nuclear@0: OS Onboard USB nuclear@0: Linux ttyS0 ttyUSB0 nuclear@0: BSD ttyd0 ttyU0 nuclear@0: Note that this rather assumes that the existence of a device nuclear@0: name in /dev indicates the actual existence of a device. Mostly nuclear@0: this is the case on modern systems. Mostly. nuclear@0: */ nuclear@0: if(!device) { nuclear@0: if( !stat("/dev/ttyS0", &sb)) device = "/dev/ttyS0"; nuclear@0: else if(!stat("/dev/ttyd0", &sb)) device = "/dev/ttyd0"; nuclear@0: else if(!stat("/dev/ttyUSB0", &sb)) device = "/dev/ttyUSB0"; nuclear@0: else if(!stat("/dev/ttyU0", &sb)) device = "/dev/ttyU0"; nuclear@0: else { nuclear@0: fprintf(stderr, "Could not find default device\n"); nuclear@0: usage(argv[0]); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: If the controlling TTY is in fact a TTY, set it up nuclear@0: */ nuclear@0: if(istty) { nuclear@0: intio = savetio; nuclear@0: intio.c_oflag = 0; nuclear@0: intio.c_lflag = 0; nuclear@0: intio.c_iflag = savetio.c_iflag & ~(INLCR|IGNCR|ICRNL); nuclear@0: nuclear@0: intio.c_cc[VEOF] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VEOL] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VEOL2] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VERASE] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VWERASE] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VKILL] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VREPRINT] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VINTR] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VQUIT] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VSUSP] = _POSIX_VDISABLE; nuclear@0: #ifdef VDSUSP nuclear@0: intio.c_cc[VDSUSP] = _POSIX_VDISABLE; nuclear@0: #endif nuclear@0: intio.c_cc[VLNEXT] = _POSIX_VDISABLE; nuclear@0: intio.c_cc[VDISCARD] = _POSIX_VDISABLE; nuclear@0: #ifdef VSTATUS nuclear@0: intio.c_cc[VSTATUS] = _POSIX_VDISABLE; nuclear@0: #endif nuclear@0: tcsetattr(0, TCSADRAIN, &intio); nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Connect to serial port nuclear@0: */ nuclear@0: fd = openport(device); nuclear@0: if(fd < 0) exit(1); nuclear@0: nuclear@0: /* nuclear@0: Main loop nuclear@0: */ nuclear@0: readdelay = 0; nuclear@0: done = 0; nuclear@0: while(!done) { nuclear@0: nuclear@0: /* nuclear@0: Set up the select() call nuclear@0: If readdelay is not 0, we're waiting for things to go quiet so we nuclear@0: can exit. nuclear@0: Errors kill us, execpt for interrupted calls nuclear@0: 0 return only happens if readdelay is set, so we exit then nuclear@0: */ nuclear@0: FD_ZERO(&fds); nuclear@0: if(!readdelay) nuclear@0: FD_SET(0, &fds); nuclear@0: FD_SET(fd, &fds); nuclear@0: i = select(fd + 1, &fds, 0,0, readdelay); nuclear@0: if(i == -1 && errno != EINTR) nuclear@0: DIEP("select"); nuclear@0: if(i == 0 && readdelay) nuclear@0: break; nuclear@0: nuclear@0: /* nuclear@0: If input from line, read a full buffer in nuclear@0: IgnoreCR means sucking CRs out of the buffer (yuck) nuclear@0: If EOF (e.g. hangup), exit nicely. nuclear@0: */ nuclear@0: if(FD_ISSET(fd, &fds)) { nuclear@0: i = read(fd, buf, sizeof(buf)); nuclear@0: if(i < 0) nuclear@0: DIEP(device); nuclear@0: if(!i) break; nuclear@0: if(showspecial) { nuclear@0: s = buf; nuclear@0: do { nuclear@0: c = 0; nuclear@0: for(j = 0; j < i; j++) { nuclear@0: c = (unsigned char) s[j]; nuclear@0: if(showspecial == 2 && ( nuclear@0: c == '\t' || c > '~')) nuclear@0: break; nuclear@0: if(c == '\r' && ignorecr) nuclear@0: break; nuclear@0: if(( c < ' ' && c != '\t' nuclear@0: && c != '\r' && c != '\n') nuclear@0: || (c > '~' && c < 160)) nuclear@0: break; nuclear@0: } nuclear@0: if(j) write(1, s, j); nuclear@0: if(j >= i) nuclear@0: break; nuclear@0: if(c == '\r' && ignorecr) { nuclear@0: /* Do nothing */ nuclear@0: } nuclear@0: else if(c < 32 && showspecial != 2) { nuclear@0: cbuf[0] = '^'; nuclear@0: cbuf[1] = c + '@'; nuclear@0: write(1, cbuf, 2); nuclear@0: } nuclear@0: else if(c == 127 && showspecial != 2) nuclear@0: write(1, "[DEL]", 5); nuclear@0: else { nuclear@0: sprintf(cbuf, "[%02x]", c); nuclear@0: write(1, cbuf, 4); nuclear@0: } nuclear@0: j++; nuclear@0: s += j; nuclear@0: i -= j; nuclear@0: } while(i > 0); nuclear@0: } nuclear@0: else { nuclear@0: if(ignorecr) { nuclear@0: j = 0; nuclear@0: for(s = buf; s < buf + i; s++) nuclear@0: if(*s != '\r') nuclear@0: buf[j++] = *s; nuclear@0: i = j; nuclear@0: } nuclear@0: write(1, buf, i); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Input on stdin nuclear@0: Read a character nuclear@0: If EOF, set readdelay to 1 second nuclear@0: */ nuclear@0: if(FD_ISSET(0, &fds)) { nuclear@0: if(read(0, &inbuf, 1) < 1) { nuclear@0: delay_tv.tv_sec = 1; nuclear@0: delay_tv.tv_usec = 0; nuclear@0: readdelay = &delay_tv; nuclear@0: continue; nuclear@0: } nuclear@0: /* nuclear@0: If command character received, read commands nuclear@0: */ nuclear@0: if(inbuf == cmdchar && istty) { nuclear@0: FIXTTY; nuclear@0: putchar('\n'); nuclear@0: for(;;) { nuclear@0: fprintf(stderr, "dterm> "); nuclear@0: if(!fgets(buf, sizeof(buf), stdin)) nuclear@0: return 0; nuclear@0: if((s = strchr(buf, '#'))) nuclear@0: *s = 0; nuclear@0: for(s = buf; *s; s++) nuclear@0: if(!isspace(*s)) break; nuclear@0: if(!*s) break; nuclear@0: ioctl(fd, TIOCMGET, &modem); nuclear@0: i = setup(buf, 0, 0); nuclear@0: if(i == -3) nuclear@0: return 0; nuclear@0: if(i == 1) { nuclear@0: nfd = openport(device); nuclear@0: if(nfd >= 0) { nuclear@0: close(fd); nuclear@0: fd = nfd; nuclear@0: } nuclear@0: } nuclear@0: else if(setupport(fd)) nuclear@0: fprintf(stderr, nuclear@0: "invalid parameters\n"); nuclear@0: } nuclear@0: if(istty) tcsetattr(0, TCSADRAIN, &intio); nuclear@0: } nuclear@0: /* nuclear@0: Otherwise do any processing on the character nuclear@0: Add dread high bit disease if mark parity nuclear@0: BS <-> DEL mapping nuclear@0: LF -> CR mapping for files nuclear@0: CR -> CRLF mapping for TTYs nuclear@0: */ nuclear@0: else { nuclear@0: if(markparity) nuclear@0: inbuf |= 0x80; nuclear@0: if(backspace && (inbuf == 8 || inbuf == 127)) nuclear@0: inbuf = backspace; nuclear@0: if(maplf && inbuf == '\n') nuclear@0: inbuf = '\r'; nuclear@0: write(fd, &inbuf, 1); nuclear@0: if(crlf && inbuf == '\r') { nuclear@0: inbuf = '\n'; nuclear@0: write(fd, &inbuf, 1); nuclear@0: } nuclear@0: if(linedelay && inbuf == '\r') nuclear@0: millisleep(linedelay); nuclear@0: else if(delay) nuclear@0: millisleep(delay); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: } nuclear@0: nuclear@0: /* nuclear@0: Fall out the bottom, cleaning up nuclear@0: */ nuclear@0: FIXTTY; nuclear@0: return 0; nuclear@0: }