# HG changeset patch # User John Tsiombikas # Date 1491597786 -10800 # Node ID ad098a33fd3483981c7c0d5cc3e302bc073d7dba initial commit of my fork of dterm diff -r 000000000000 -r ad098a33fd34 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Apr 07 23:43:06 2017 +0300 @@ -0,0 +1,4 @@ +\.o$ +\.d$ +\.swp$ +^dterm$ diff -r 000000000000 -r ad098a33fd34 LICENCE.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LICENCE.txt Fri Apr 07 23:43:06 2017 +0300 @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff -r 000000000000 -r ad098a33fd34 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Apr 07 23:43:06 2017 +0300 @@ -0,0 +1,18 @@ +COPT = -O3 -Wall -Werror +BIN = /usr/local/bin +VERSION = 0.3 + +dterm: dterm.c + gcc ${COPT} -o dterm dterm.c -DVERSION='"$(VERSION)"' + +install: + install dterm ${BIN} + +clean: + rm -f dterm *.o *.bak + +package: dterm.c LICENCE.txt README.txt Makefile + mkdir -p dterm-$(VERSION) + cp dterm.c LICENCE.txt README.txt Makefile dterm-$(VERSION) + tar cfvz dterm-$(VERSION).tgz dterm-$(VERSION) + rm -rf dterm-$(VERSION) diff -r 000000000000 -r ad098a33fd34 README.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.txt Fri Apr 07 23:43:06 2017 +0300 @@ -0,0 +1,129 @@ +dterm: A simple terminal program + + +dterm is a simple terminal emulator, which doesn't actually emulate +any particular terminal. Mainly, it is designed for use with xterm +and friends, which already do a perfectly good emulation, and therefore +don't need any special help; dterm simply provides a means by which +keystrokes are forwarded to the serial line, and data forwarded from +the serial line appears on the terminal. + + +Running dterm + +dterm is invoked thusly: + + dterm [options|device ...] + +dterm attempts to read the file ~/.dtermrc for options; if this doesn't +exist, it tries /etc/dtermrc. Then it parses the options passed on the +command line. + +The options read should include a device name, e.g "ttyS0" or "ttyd0" +for the first serial port on a Linux or FreeBSD system respectively. If +no device is specified, dterm tries /dev/ttyS0, /dev/ttyd0, /dev/ttyUSB0 +and /dev/ttyU0. + +Once started, dterm can be got into command mode using Ctrl/]. Press +enter once from command mode to get back into conversational mode. (The +command character can be changed with the esc= option, e.g. esc=p to +use Ctrl/P instead of Ctrl/].) + + +Options + +The following options can be used from command mode + +- 300, 1200, 9600 etc: Set speed, default 9600. +- 5, 6, 7, 8: Set bits per character, default 8. +- 1, 2: Set number of stop bits, default 1. +- e, o, n, m, s: Set parity to even, odd, none, mark or space, default none. +- cts, nocts: Enable / disable CTS flow control, default nocts. +- xon, noxon: Enable / disable XON/XOFF flow control, default noxon. +- modem: Enable / disable modem control (hang up modem on exit, exit if +modem hangs up), default nomodem. +- bs, nobs: Enable / disable mapping of Delete to Backspace, default nobs. +- del, nodel: Enable / disable mapping of Backspace to Delete, default nodel. +- maplf, nomaplf: Enable / disable mapping of LF to CR, default nomaplf. +- igncr, noigncr: Ignore / output carriage returns, default noigncr. +- crlf, nocrlf: Enable / disable sending LF after each CR, default nocrlf. +- ctrl, noctrl: Enable / disable control character display mode. In this +mode, non-printable characters are displayed as ^c for the codes 0-31 (except +CR, LF & TAB), [DEL] for 127, or [xx] for non-printing characters >= 128. +- hex, nohex: As for ctrl, but prints [xx] for all characters except 7-bit +printable ASCII, CR and LF. +- b: Send a 500 ms break. +- dtr, nodtr: Raise / lower DTR, default dtr. +- rts, norts: Raise / lower RTS, default rts. +- d, r: Toggle DTR / RTS. +- esc=: Set command mode character to Ctrl/ (default ']') +- @: Read and process configuration from . +- !: Execute shell command +- sx : Send a file using XMODEM. +- rx : Receive a file using XMODEM. +- sz : Send a file using ZMODEM. +- rz: Receive file(s) using ZMODEM. +- show: Display current configuration and modem status. +- help, h, ?: Display a summary of commands. +- version: Display version, copyright and warranty information. +- quit, q: Exit + + +File Transfer + +If the rzsz package (or lrzsz) package is installed, the sx, sz, rx & rz +commands can be used to initiate file transfers using the reliable +XMODEM and ZMODEM file transfer protocols. Note that the program files +for rzsz must be in /usr/bin or /usr/local/bin for dterm to find them. + +Note that rx, sx and rz require that the transfer be initiated at the +remote end before escaping back to the dterm command prompt. sz will send +an "rz" command down the serial link in start-up to initiate the transfer. + + +Examples + +Connect via ttyS1 to a system running at 2400 bps, 7 bits even parity: + + dterm ttyS1 2400 7 e + +Send a break in a running session: + + ^] + dterm> b + dterm> + +Transmit a file using ZMODEM: + + ^] + dterm> sz file.txt + rz waiting to receive.Sending: file.txt + Bytes Sent: 22943 BPS:645 + Transfer complete + dterm> + + +Copyright + +dterm is Copyright 2007 Knossos Networks Ltd. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License version 2 +as published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +A copy of the GNU General Public License version 2 is available at +http://www.knossos.net.nz/gpl.html or can be obtained from the Free +Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + + +Source Code + +dterm source code is located at +http://www.knossos.net.nz/downloads/dterm-0.3.tgz + diff -r 000000000000 -r ad098a33fd34 dterm.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dterm.c Fri Apr 07 23:43:06 2017 +0300 @@ -0,0 +1,1045 @@ +/* dterm.c Dumb terminal program 19/11/2004/dcs + */ + +/* + * Copyright 2007 Knossos Networks Ltd. + * Copyright 2017 John Tsiombikas + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef VERSION +#define VERSION "unknown" +#endif +#define COPYRIGHT \ +"dterm version " VERSION " Copyright 2007 Knossos Networks Ltd.\n" \ +" Copyright 2017 John Tsiombikas \n" \ +"\n" \ +"This program is free software; you can redistribute it and/or\n" \ +"modify it under the terms of the GNU General Public License version 2\n" \ +"as published by the Free Software Foundation.\n" \ +"\n" \ +"This program is distributed in the hope that it will be useful,\n" \ +"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \ +"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" \ +"GNU General Public License for more details.\n" \ +"\n" \ +"You should have received a copy of the GNU General Public License\n" \ +"along with this program; if not, write to the Free Software\n" \ +"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n" \ +"02110-1301, USA.\n" + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FIXTTY if(istty) tcsetattr(0, TCSADRAIN, &savetio) +#define DIEP(s) { FIXTTY; perror(s); exit(1); } +#define DIE(s,m) { FIXTTY; fprintf(stderr, "%s: %s\n", s, m); exit(1); } + +/* + Sigh + */ +#ifndef CCTS_OFLOW +#ifdef CRTSCTS +#define CCTS_OFLOW CRTSCTS +#endif /* CRTSCTS */ +#endif /* ! CCTS_OFLOW */ + + +/* + Valid speed settings. + */ +static struct { int s, c; } speeds[] = { + { 50, B50 }, + { 75, B75 }, + { 110, B110 }, + { 134, B134 }, + { 150, B150 }, + { 200, B200 }, + { 300, B300 }, + { 600, B600 }, + { 1200, B1200 }, + { 1800, B1800 }, + { 2400, B2400 }, + { 4800, B4800 }, + { 9600, B9600 }, + { 19200, B19200 }, + { 38400, B38400 }, +#ifdef B7200 + { 7200, B7200 }, +#endif +#ifdef B14400 + { 14400, B14400 }, +#endif +#ifdef B28800 + { 28800, B28800 }, +#endif +#ifdef B57600 + { 57600, B57600 }, +#endif +#ifdef B76800 + { 76800, B76800 }, +#endif +#ifdef B115200 + { 115200, B115200 }, +#endif +#ifdef B230400 + { 230400, B230400 }, +#endif +#ifdef B460800 + { 460800, B460800 }, +#endif +#ifdef B921600 + { 921600, B921600 }, +#endif + { 0, 0 }}; + + +/* + Save the terminal settings here + */ +static struct termios intio, savetio; + +static int fd = -1; /* Channel to port */ +static int cmdchar = 035; /* Command character, default ^] */ +static char *device = 0; /* Device, no default */ +static int ispeed = B9600; /* Input speed, default 9600 bps */ +static int ospeed = B9600; /* Output speed, default 9600 bps */ +static int twostop = 0; /* Stop bits, if set to CSTOPB, use two */ +static int parity = 0; /* Parity, -1 = mark, -2 = space 0=none */ + /* PARENB enables parity, PARODD = odd */ +static int bits = CS8; /* CS5-CS8 for 5-8 bits per character */ +static int ctsflow = 0; /* CCTS_OFLOW enables CTS flow control */ +static int xonflow = 0; /* IXON | IXOFF for in & out soft F/c */ +static int modemcontrol = 0; /* Set to HUPCLto enable modem control */ +static int markparity = 0; /* Mark parity -- set high bit on out */ +static int backspace = 0; /* Backspace char, send on BS or DEL */ +static int maplf = 0; /* If set, map LF to CR */ +static int ignorecr = 0; /* Ignore carriage returns */ +static int crlf = 0; /* Send CRLF on CR */ +static int istty; /* Result of isatty() */ +static char *connname = 0; /* Connection name found in config */ +static int sendbreak = 0; /* Break requested */ +static int modem = 0; /* Current modem status */ +static int setmodem = 0; /* Set modem status to this */ +static int delay = 0; /* Millisecond delay after each key */ +static int linedelay = 0; /* Millisecond delay after each CR */ +static int showspecial = 0; /* Show special chars as [xx] */ + +static void +help() { + fprintf(stderr, +"dterm commands:\n" +"^%c Enter command mode quit, q Exit\n" +"show Display configuration help, h, ? Display this message\n" +"! Execute shell command version Display version\n" +"@ Get configuration from \n" +" Connect to 300,1200,9600 Set speed\n" +"5, 6, 7, 8 Set bits per character 1, 2 Set stop bits\n" +"e, o, n, m, s Set parity" +#ifdef CCTS_OFLOW +" cts,nocts Enable / disable CTS\n" +#else +"\n" +#endif +"dtr, nodtr Raise / lower DTR b Send a 500ms break\n" +"rts, norts Raise / lower RTS d, r Toggle DTR, RTS\n" +"rx Receive file (XMODEM) sx Send file (XMODEM)\n" +"rz Receive file (ZMODEM) sz Send file (ZMODEM)\n" +"xon,noxon Enable / disable XON/XOFF flow control\n" +"modem Hang up modem on exit, exit if modem hangs up\n" +"nomodem Do not do modem control\n" +"bs, nobs Enable / disable mapping of Delete to Backspace\n" +"del, nodel Enable / disable mapping of Backspace to Delete\n" +"maplf, nomaplf Enable / disable mapping of LF to CR\n" +"igncr, noigncr Ignore / output carriage returns\n" +"crlf, nocrlf Enable / disable sending LF after each CR\n" +"delay= Add delay of ms after each charachter sent\n" +"crwait= Add delay of ms after each line sent\n" +"esc= Set command mode character to Ctrl/\n" +"ctrl,hex,noctrl Show control characters as ^n (except tab, CR, LF)\n" + + + + , cmdchar + '@'); +} + + + + +/* + Show current setup + */ +static void +showsetup() { + int i; + fprintf(stderr, "Port: %s\n", device); + fprintf(stderr, "Communications parameters: "); + for(i = 0; speeds[i].s && speeds[i].c != ispeed; ) + i++; + fprintf(stderr, "%d", speeds[i].s); + if(ospeed != ispeed) { + for(i = 0; speeds[i].s && speeds[i].c != ospeed; ) + i++; + fprintf(stderr, "/%d", speeds[i].s); + } + if(bits == CS5) fprintf(stderr, " 5"); + if(bits == CS6) fprintf(stderr, " 6"); + if(bits == CS7) fprintf(stderr, " 7"); + if(bits == CS8) fprintf(stderr, " 8"); + if(parity == 0) fprintf(stderr, " n"); + if(parity == PARENB) fprintf(stderr, " e"); + if(parity == (PARENB|PARODD)) fprintf(stderr, " o"); + if(parity == -1) fprintf(stderr, " m"); + if(parity == -2) fprintf(stderr, " s"); + if(twostop) fprintf(stderr, " 2\n"); + else fprintf(stderr, " 1\n"); + fprintf(stderr, "Flow/modem control:"); + if(xonflow) fprintf(stderr, " xon"); + if(ctsflow) fprintf(stderr, " cts"); + if(modemcontrol)fprintf(stderr, " modem"); + putc('\n', stderr); + fprintf(stderr, "Character mappings:"); + if(backspace == 8) fprintf(stderr, " bs"); + if(backspace == 127) fprintf(stderr, " del"); + if(maplf) fprintf(stderr, " maplf"); + if(ignorecr) fprintf(stderr, " igncr"); + if(crlf) fprintf(stderr, " crlf"); + if(showspecial == 1) fprintf(stderr, " ctrl"); + if(showspecial == 2) fprintf(stderr, " hex"); + putc('\n', stderr); + printf("Modem control: DTR: %s RTS: %s CTS: %s DSR: %s DCD: %s\n", + setmodem & TIOCM_DTR ? "on" : "off", + setmodem & TIOCM_RTS ? "on" : "off", + modem & TIOCM_CTS ? "on" : "off", + modem & TIOCM_DSR ? "on" : "off", + modem & TIOCM_CD ? "on" : "off"); + fprintf(stderr, "Escape character: ^%c\n", cmdchar + '@'); + fflush(stderr); +} + + +/* + Use the rzsz or lrzsz package to do file transfer + Mode should be "rx", "rb" or "rz" to receive a file, "sx", "sb" or "sz" + to send. "rz" & "rb" don't require a file name; the others do. + */ +static int +rzsz(char *mode, char *file) { + static char *loc[] = { "/usr/bin/", "/usr/local/bin/l", + "/usr/bin/l", "/usr/local/bin/", 0 }; + char path[128]; + char *cmd; + int i; + struct stat sb; + pid_t pid; + + /* + Check file name + */ + if(*file == 0 && (mode[0] != 'r' || mode[1] == 'x')) { + fprintf(stderr, "File name required for %c%c\n", + mode[0], mode[1]); + return -1; + } + + /* + Find the appropriate command + */ + for(i = 0; loc[i]; i++) { + if(mode[1] == 'a') { + sprintf(path, "%sascii-xfr", loc[i]); + } else { + sprintf(path, "%s%c%c", loc[i], mode[0], mode[1]); + } + if(!stat(path, &sb) && (sb.st_mode & S_IXUSR)) + break; + } + if(!loc[i]) { + fprintf(stderr, "RZ/SZ/ascii-xfr not installed\n"); + return -1; + } + + /* + Get the command name + */ + cmd = strrchr(path, '/'); + if(cmd) cmd++; + else cmd = path; + + /* + Fork subprocess. Set stdin & stdout to the serial line + stderr remains on the console for progress messages + */ + pid = fork(); + if(pid == 0) { + dup2(fd, 0); + dup2(fd, 1); + if(mode[1] == 'a') { + char delay_str[64]; + sprintf(delay_str, "%d", linedelay); + if(*file) execl(path, cmd, "-s", "-l", delay_str, file, NULL); + else execl(path, cmd, "-r", NULL); + } else { + if(*file) execl(path, cmd, file, NULL); + else execl(path, cmd, NULL); + } + _exit(127); + } + else if(pid == -1) { + perror("fork"); + return -1; + } + + /* + Wait for the process to complete and give a meaningful + response + */ + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + signal(SIGTERM, SIG_IGN); + while(waitpid(pid, &i, 0) != pid) {} + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGTERM, SIG_DFL); + + if(WIFSIGNALED(i)) { + fprintf(stderr, "Terminated on signal %d%s\n", WTERMSIG(i), + WCOREDUMP(i) ? " (core dumped)" : ""); + return -1; + } + if(WTERMSIG(i)) + return -1; + return 0; +} + + + +static int readconfig(char *, char *); + +/* + Process commands passed as strings, may be several commands on a line + Allow ugliness like 8n1 -- treat as 8, n, 1 + */ +static int +setup(char *s, char *cffile, int cfline) { + char *t, *d; + int j,k; + int ret = 0; + struct stat sb; + char ttybuf[128]; + int justospeed = 0; + + while(*s) { + /* + Skip whitespace, commas + */ + while(isspace(*s) || *s == ',') + s++; + if(!*s) break; + + /* + '?' = help + */ + if(*s == '?') { + help(); + return -4; + } + + /* + '!' = shell command + */ + if(*s == '!') { + if((t = strchr(s, '\n'))) + *t = 0; + system(++s); + break; + } + + /* + File transfer (sx, sz, rx, rz, sa, ra) + Run rzsz or ascii-xfr to perform the actual file transfer + Allow "sxfilename", "sx filename" or just "rz" + */ + if((s[0] == 's' || s[0] == 'r') && + (s[1] == 'x' || s[1] == 'z' || s[1] == 'a')) { + if((t = strchr(s, '\n'))) + *t = 0; + for(t = s + 2; isspace(*t); ) t++; + return rzsz(s, t); + } + + /* + If a number, process it. + If it's a valid speed, use that -- if nnn/mmm, set both to nnn + for the first time, and flag things so mmm just sets ospeed + 5-8 = data bits, 1,2 sets stops + Anything else is error + */ + if(isdigit(*s)) { + j = strtol(s, &s, 10); + for(k = 1; speeds[k].s; k++) + if(speeds[k].s == j) + break; + if(speeds[k].s) { + ospeed = speeds[k].c; + if(!justospeed) + ispeed = ospeed; + if(*s == '/') { + s++; + justospeed = 1; + } + else justospeed = 0; + } + else if(j == 5) bits = CS5; + else if(j == 6) bits = CS6; + else if(j == 7) bits = CS7; + else if(j == 8) bits = CS8; + else if(j == 1) twostop = 0; + else if(j == 2) twostop = CSTOPB; + else { + if(cffile) + fprintf(stderr, "in %s:%d: ", + cffile, cfline); + fprintf(stderr, "%d: invalid speed/bits\n", j); + ret = -1; + } + } + + /* + Otherwise, get the alpha-only keyword, see if it matches anything + useful. + */ + else { + t = s; + while(isalpha(*t)) t++; + j = *t; + *t = 0; + if( !strcasecmp(s, "xon")) + xonflow = IXON | IXOFF; + else if(!strcasecmp(s, "noxon")) + xonflow = 0; + +#ifdef CCTS_OFLOW + else if(!strcasecmp(s, "cts")) + ctsflow = CCTS_OFLOW; + else if(!strcasecmp(s, "nocts")) + ctsflow = 0; +#endif + else if(!strcasecmp(s, "modem")) + modemcontrol = HUPCL; + else if(!strcasecmp(s, "nomodem")) + modemcontrol = 0; + else if(!strcasecmp(s, "E")) + parity = PARENB; + else if(!strcasecmp(s, "O")) + parity = PARENB | PARODD; + else if(!strcasecmp(s, "M")) + parity = -1; + else if(!strcasecmp(s, "S")) + parity = -2; + else if(!strcasecmp(s, "N")) + parity = 0; + else if(!strcasecmp(s, "q") || !strcasecmp(s, "quit")) + ret = -3; + else if(!strcasecmp(s, "h") || !strcasecmp(s, "help")) + help(); + else if(!strcasecmp(s, "del")) + backspace = 127; + else if(!strcasecmp(s, "bs")) + backspace = 8; + else if(!strcasecmp(s,"nobs") || !strcasecmp(s,"nodel")) + backspace = 0; + else if(!strcasecmp(s, "noesc")) + cmdchar = 0; + else if(!strcasecmp(s, "maplf")) + maplf = 1; + else if(!strcasecmp(s, "nomaplf")) + maplf = 0; + else if(!strcasecmp(s, "igncr")) + ignorecr = 1; + else if(!strcasecmp(s, "noigncr")) + ignorecr = 0; + else if(!strcasecmp(s, "crlf")) + crlf = 1; + else if(!strcasecmp(s, "nocrlf")) + crlf = 0; + else if(!strcasecmp(s, "b")) + sendbreak = 1; + else if(!strcasecmp(s, "d")) + setmodem ^= TIOCM_DTR; + else if(!strcasecmp(s, "r")) + setmodem ^= TIOCM_RTS; + else if(!strcasecmp(s, "dtr")) + setmodem |= TIOCM_DTR; + else if(!strcasecmp(s, "rts")) + setmodem |= TIOCM_RTS; + else if(!strcasecmp(s, "nodtr")) + setmodem &= ~TIOCM_DTR; + else if(!strcasecmp(s, "norts")) + setmodem &= ~TIOCM_RTS; + else if(!strcasecmp(s, "show")) + showsetup(); + else if(!strcasecmp(s, "version")) + fputs(COPYRIGHT, stderr); + else if(!strcasecmp(s, "ctrl")) + showspecial = 1; + else if(!strcasecmp(s, "hex")) + showspecial = 2; + else if(!strcasecmp(s, "noctrl") || + !strcasecmp(s, "noctrl")) + showspecial = 0; + + /* + No? + @ includes a file + !cmd Run a command + esc=c sets escape char + select device + */ + else { + *t = j; + while(*t && !isspace(*t) && *t != ',') + t++; + j = *t; + *t = 0; + if(*s == '@') { + k = readconfig(++s, 0); + if(k == -2) { + if(cffile) + fprintf(stderr, + "in %s:%d: ", + cffile, cfline); + fprintf(stderr, "%s: %s\n", + s, strerror(errno)); + ret = -1; + } + goto next; + } + if(!strncasecmp(s, "esc=", 4)) { + cmdchar = s[4] & 0x1f; + goto next; + } + else if(!strncasecmp(s, "delay=", 6)) { + delay = atoi(s+6); + goto next; + } + else if(!strncasecmp(s, "crwait=", 7)) { + linedelay = atoi(s+7); + goto next; + } + d = s; + k = stat(d, &sb); + if(k && *d != '/') { + sprintf(ttybuf, "/dev/%.100s", d); + d = ttybuf; + k = stat(d, &sb); + } + if(!k) { + if((sb.st_mode & S_IFMT) == S_IFCHR) { + ret = 1; + device = strdup(d); + goto next; + } + } + if(cffile) + fprintf(stderr, "in %s:%d: ", + cffile, cfline); + fprintf(stderr, + "%s: unrecognised keyword/device\n", s); + ret = -1; + } + next: *t = j; + s = t; + } + } + return ret; +} + + +/* + Read a config file + Input lines can be lists of config words, or in the form + name: words + If name: form, only lines matching passed name are used + */ +static int +readconfig(char *file, char *name) { + char buf[1024]; + FILE *f; + char *s, *t; + int lineno = 0; + int ret = 0, r; + + /* + ~/file = get file from $HOME/file + */ + if(*file == '~' && file[1] == '/' && (s = getenv("HOME"))) { + snprintf(buf, sizeof(buf), "%s/%s", s, file+2); + file = buf; + } + + /* + Return -2 if can't open + */ + f = fopen(file, "r"); + if(!f) return -2; + + /* + Read input, strip # commends + Count lines + Keep track of return code + */ + while(fgets(buf, sizeof(buf), f)) { + lineno++; + if((s = strchr(buf, '#'))) + *s = 0; + for(s = buf; isspace(*s);) + s++; + if(!*s) continue; + for(t = s; *t && *t != ':' && *t != ',' && !isspace(*t); ) + t++; + if(*t == ':') { + *t++ = 0; + if(!name) + continue; + if(strcmp(name, s)) + continue; + s = t; + connname = name; + } + + r = setup(s, file, lineno); + if(r < 0) + ret = r; + if(r > 0 && !ret) + ret = 1; + } + + fclose(f); + return ret; +} + + +/* + Sleep n milliseconds + */ +static void +millisleep(int n) { + struct timespec t; + t.tv_sec = n / 1000; + t.tv_nsec = (n % 1000) * 1000000; + nanosleep(&t, 0); +} + + +/* + Set up the port + */ +static int +setupport(int fd) { + int parityflags; + int modemflags; + int spaceparity = 0; + struct termios tio; + int m; + + /* + See what to do with parity + */ + markparity = 0; + parityflags = parity; + if(parity == -1) { + parityflags = 0; + markparity = ISTRIP; + } + else if(parity == -2) { + parityflags = 0; + spaceparity = ISTRIP; + } + + /* + If no modem control, use local mode + */ + modemflags = ctsflow | modemcontrol; + if(!modemflags) + modemflags = CLOCAL; + + /* + Set the speed and params + */ + tcgetattr(fd, &tio); + tio.c_iflag = IGNBRK | IGNPAR | xonflow | spaceparity | markparity; + tio.c_cflag = CREAD | HUPCL | modemflags | bits | parityflags; + tio.c_oflag = 0; + tio.c_lflag = 0; + cfsetispeed(&tio, ispeed); + cfsetospeed(&tio, ospeed); + if(tcsetattr(fd, TCSAFLUSH, &tio) == -1) + return -1; + + /* + Set up the modem lines + */ + ioctl(fd, TIOCMGET, &modem); + if((modem & (TIOCM_RTS | TIOCM_DTR)) != (setmodem & (TIOCM_RTS | + TIOCM_DTR))) { + m = setmodem & (TIOCM_RTS | TIOCM_DTR); + ioctl(fd, TIOCMBIS, &m); + m = (~setmodem) & (TIOCM_RTS | TIOCM_DTR); + ioctl(fd, TIOCMBIC, &m); + } + + /* + Send a break if requested + */ + if(sendbreak) { + ioctl(fd, TIOCSBRK, 0); + millisleep(500); + ioctl(fd, TIOCCBRK, 0); + sendbreak = 0; + } + + /* + Get the current modem status + */ + ioctl(fd, TIOCMGET, &modem); + setmodem = modem; + + return 0; +} + + +/* + Set up the modem. This is a little tricky due to the need to + ensure the modem does not block before we set it up. + */ +static int +openport(char *device) { + int fd; + + if((fd = open(device, O_RDWR|O_NONBLOCK, 0)) < 0) + DIEP(device) + if(setupport(fd) < 0) + DIEP(device) + millisleep(10); + fcntl(fd, F_SETFL, 0); + + ioctl(fd, TIOCMGET, &modem); + setmodem = modem; + + setenv("DTERM_PORT", device, 1); + return fd; +} + + +/* + Usage + */ +static void +usage(char *this) { + fprintf(stderr, "Usage: %s port/setup\n" + " '%s help' for help\n", this, this); + exit(1); +} + + +int +main(int argc, char **argv) { + int nfd; + int i, j, c; + unsigned char inbuf; + char buf[256]; + char cbuf[8]; + char *s; + int done; + fd_set fds; + struct timeval delay_tv, *readdelay; + struct stat sb; + + /* + Do the right things depending on whether stdin & stdout are TTYs + If the input is a TTY, we need to put it into non-canonical mode + (which we'll actually do later); otherwise, default to turning + LFs from a file into CRs. (See [no]maplf setup command.) + If the output is a TTY, default to passing CR transparently; + otherwise default to ignoring CR so that output is logged to a + files etc nicely. (See [no]igncr.) + */ + istty = isatty(0); + if(istty) { + tcgetattr(0, &savetio); + maplf = 0; + } + else maplf = 1; + if(isatty(1)) + ignorecr = 0; + else ignorecr = 1; + + /* + Read default config, if no private .dtermrc, use /etc/dtermrc + */ + setmodem = modem = TIOCM_DTR | TIOCM_RTS; + if(readconfig("~/.dtermrc", argv[1]) == -2) + readconfig("/etc/dtermrc", argv[1]); + + /* + Parse args + If only arg is "help", exit + */ + i = 1; + if(connname) i = 2; + for(; i < argc; i++) + if(setup(argv[i], 0, 0) < 0) + usage(argv[0]); + if(argc == 2 && !strcasecmp(argv[1], "help")) + exit(0); + + /* + If no device specified, have a crack at finding a default device + Look for the first on-board device first; failing that look for + the first USB device. For Linux & BSD these are: + OS Onboard USB + Linux ttyS0 ttyUSB0 + BSD ttyd0 ttyU0 + Note that this rather assumes that the existence of a device + name in /dev indicates the actual existence of a device. Mostly + this is the case on modern systems. Mostly. + */ + if(!device) { + if( !stat("/dev/ttyS0", &sb)) device = "/dev/ttyS0"; + else if(!stat("/dev/ttyd0", &sb)) device = "/dev/ttyd0"; + else if(!stat("/dev/ttyUSB0", &sb)) device = "/dev/ttyUSB0"; + else if(!stat("/dev/ttyU0", &sb)) device = "/dev/ttyU0"; + else { + fprintf(stderr, "Could not find default device\n"); + usage(argv[0]); + } + } + + /* + If the controlling TTY is in fact a TTY, set it up + */ + if(istty) { + intio = savetio; + intio.c_oflag = 0; + intio.c_lflag = 0; + intio.c_iflag = savetio.c_iflag & ~(INLCR|IGNCR|ICRNL); + + intio.c_cc[VEOF] = _POSIX_VDISABLE; + intio.c_cc[VEOL] = _POSIX_VDISABLE; + intio.c_cc[VEOL2] = _POSIX_VDISABLE; + intio.c_cc[VERASE] = _POSIX_VDISABLE; + intio.c_cc[VWERASE] = _POSIX_VDISABLE; + intio.c_cc[VKILL] = _POSIX_VDISABLE; + intio.c_cc[VREPRINT] = _POSIX_VDISABLE; + intio.c_cc[VINTR] = _POSIX_VDISABLE; + intio.c_cc[VQUIT] = _POSIX_VDISABLE; + intio.c_cc[VSUSP] = _POSIX_VDISABLE; +#ifdef VDSUSP + intio.c_cc[VDSUSP] = _POSIX_VDISABLE; +#endif + intio.c_cc[VLNEXT] = _POSIX_VDISABLE; + intio.c_cc[VDISCARD] = _POSIX_VDISABLE; +#ifdef VSTATUS + intio.c_cc[VSTATUS] = _POSIX_VDISABLE; +#endif + tcsetattr(0, TCSADRAIN, &intio); + } + + /* + Connect to serial port + */ + fd = openport(device); + if(fd < 0) exit(1); + + /* + Main loop + */ + readdelay = 0; + done = 0; + while(!done) { + + /* + Set up the select() call + If readdelay is not 0, we're waiting for things to go quiet so we + can exit. + Errors kill us, execpt for interrupted calls + 0 return only happens if readdelay is set, so we exit then + */ + FD_ZERO(&fds); + if(!readdelay) + FD_SET(0, &fds); + FD_SET(fd, &fds); + i = select(fd + 1, &fds, 0,0, readdelay); + if(i == -1 && errno != EINTR) + DIEP("select"); + if(i == 0 && readdelay) + break; + + /* + If input from line, read a full buffer in + IgnoreCR means sucking CRs out of the buffer (yuck) + If EOF (e.g. hangup), exit nicely. + */ + if(FD_ISSET(fd, &fds)) { + i = read(fd, buf, sizeof(buf)); + if(i < 0) + DIEP(device); + if(!i) break; + if(showspecial) { + s = buf; + do { + c = 0; + for(j = 0; j < i; j++) { + c = (unsigned char) s[j]; + if(showspecial == 2 && ( + c == '\t' || c > '~')) + break; + if(c == '\r' && ignorecr) + break; + if(( c < ' ' && c != '\t' + && c != '\r' && c != '\n') + || (c > '~' && c < 160)) + break; + } + if(j) write(1, s, j); + if(j >= i) + break; + if(c == '\r' && ignorecr) { + /* Do nothing */ + } + else if(c < 32 && showspecial != 2) { + cbuf[0] = '^'; + cbuf[1] = c + '@'; + write(1, cbuf, 2); + } + else if(c == 127 && showspecial != 2) + write(1, "[DEL]", 5); + else { + sprintf(cbuf, "[%02x]", c); + write(1, cbuf, 4); + } + j++; + s += j; + i -= j; + } while(i > 0); + } + else { + if(ignorecr) { + j = 0; + for(s = buf; s < buf + i; s++) + if(*s != '\r') + buf[j++] = *s; + i = j; + } + write(1, buf, i); + } + } + + /* + Input on stdin + Read a character + If EOF, set readdelay to 1 second + */ + if(FD_ISSET(0, &fds)) { + if(read(0, &inbuf, 1) < 1) { + delay_tv.tv_sec = 1; + delay_tv.tv_usec = 0; + readdelay = &delay_tv; + continue; + } + /* + If command character received, read commands + */ + if(inbuf == cmdchar && istty) { + FIXTTY; + putchar('\n'); + for(;;) { + fprintf(stderr, "dterm> "); + if(!fgets(buf, sizeof(buf), stdin)) + return 0; + if((s = strchr(buf, '#'))) + *s = 0; + for(s = buf; *s; s++) + if(!isspace(*s)) break; + if(!*s) break; + ioctl(fd, TIOCMGET, &modem); + i = setup(buf, 0, 0); + if(i == -3) + return 0; + if(i == 1) { + nfd = openport(device); + if(nfd >= 0) { + close(fd); + fd = nfd; + } + } + else if(setupport(fd)) + fprintf(stderr, + "invalid parameters\n"); + } + if(istty) tcsetattr(0, TCSADRAIN, &intio); + } + /* + Otherwise do any processing on the character + Add dread high bit disease if mark parity + BS <-> DEL mapping + LF -> CR mapping for files + CR -> CRLF mapping for TTYs + */ + else { + if(markparity) + inbuf |= 0x80; + if(backspace && (inbuf == 8 || inbuf == 127)) + inbuf = backspace; + if(maplf && inbuf == '\n') + inbuf = '\r'; + write(fd, &inbuf, 1); + if(crlf && inbuf == '\r') { + inbuf = '\n'; + write(fd, &inbuf, 1); + } + if(linedelay && inbuf == '\r') + millisleep(linedelay); + else if(delay) + millisleep(delay); + } + } + + } + + /* + Fall out the bottom, cleaning up + */ + FIXTTY; + return 0; +}