diff --git a/CHANGES.old b/CHANGES.old index 08a8e40..e676740 100644 --- a/CHANGES.old +++ b/CHANGES.old @@ -52,7 +52,7 @@ Changed paths: Added support for UUCP-style locks. Lock handling is compiled-in if the macro UUCP_LOCK_DIR is defined; if it is, it must contain the name of the lock directory. Locking can be disabled at runtime using the -"--noock" option. +"--noock" option. UUCP-locks support is based on a patch submitted by Julius P. Malkiewicz diff --git a/CONTRIBUTORS b/CONTRIBUTORS index b3efbef..79c24e5 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -56,4 +56,4 @@ comments: - Joe Merten (https://github.com/Joe-Merten) contributed the --lower-rts and --lower-dtr options, custom baudrate support for - OSX, the --logfile option implementation, and several bug-fixes. + OSX, the --logfile option implementation, and several bug-fixes. diff --git a/LICENSE.txt b/LICENSE.txt index 5b6e7c6..fa6cc9f 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,12 +1,12 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - Preamble + Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public @@ -56,7 +56,7 @@ 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 + GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains @@ -255,7 +255,7 @@ 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 + 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 @@ -277,9 +277,9 @@ 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 + END OF TERMS AND CONDITIONS - How to Apply These Terms to Your New Programs + 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 diff --git a/README.md b/README.md index 592830c..f95913d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The latest release can be downloaded from: As its name suggests, *picocom* is a minimal dumb-terminal emulation program. It is, in principle, very much like minicom, only it's "pico" -instead of "mini"! +instead of "mini"! It was designed to serve as a simple, manual, modem configuration, testing, and debugging tool. It has also served (quite well) as a @@ -57,11 +57,11 @@ man-page, to wherever you put your binaries and man-pages. For example: - cp picocom ~/bin - cp picocom.1 ~/man/man1 + cp picocom ~/bin + cp picocom.1 ~/man/man1 Again, this is not strictly necessary. You can run picocom and read -its man-page directly from the source directory. +its man-page directly from the source directory. If something goes wrong and picocom can't compile cleanly, or if it's lacking a feature you need, take a look at the included Makefile. It's @@ -71,15 +71,15 @@ by commenting in or out the respective lines. Once you edit the Makefile, to recompile say: - make clean - make + make clean + make If your system's default make(1) command is not GNU Make (or compatible enough), find out how you can run GNU Make on your system. For example: - gmake clean - gmake + gmake clean + gmake Alternatively, you might have to make some trivial edits to the Makefile for it to work with your system's make(1) command. @@ -102,22 +102,22 @@ through a device node named `/dev/ttyS0`. You can start picocom with its default option values (default serial port settings) like this: - picocom /dev/ttyS0 + picocom /dev/ttyS0 If you have not installed the picocom binary to a suitable place, then you can run it directly from the source distribution directory like this: - ./picocom /dev/ttyS0 + ./picocom /dev/ttyS0 If this fails with a message like: - FATAL: cannot open /dev/ttyS0: Permission denied + FATAL: cannot open /dev/ttyS0: Permission denied This means that you do not have permissions to access the serial port's device node. To overcome this you can run picocom as root: - sudo picocom /dev/ttyS0 + sudo picocom /dev/ttyS0 Alternatively, and preferably, you can add yourself to the user-group that your system has for allowing access to serial ports. For most @@ -125,7 +125,7 @@ Unix-like systems this group is called "dialout". Consult you system's documentation to find out how you can do this (as it differs form system to system). On most Linux systems you can do it like this: - sudo usermod -a -G dialout username + sudo usermod -a -G dialout username You will need to log-out and then log-in back again for this change to take effect. @@ -135,20 +135,20 @@ desired values using picocom's command line options. For example, to set the baud-rate to 115200bps (the default is 9600bps), and enable hardware flow-control (RTS/CTS handshake) you can say: - picocom -b 115200 -f h /dev/ttyS0 + picocom -b 115200 -f h /dev/ttyS0 or: - picocom --baud 115200 --flow h /dev/ttyS0 + picocom --baud 115200 --flow h /dev/ttyS0 To see all available options run picocom like this: - picocom --help + picocom --help Once picocom starts, it initializes the serial port and prints the message: - Terminal is ready + Terminal is ready From now on, every character you type is sent to the serial port, and every character received from the serial port is sent ro your @@ -160,7 +160,7 @@ terminal. This is normal. To exit picocom you have to type: - C-a, C-x + C-a, C-x Which means you have to type [Control-A] followed by [Control-X]. You can do this by pressing and holding down the [Control] key, then @@ -177,11 +177,11 @@ Next you should take a look at the very detailed picocom manual page. It can be accessed like this (assuming you are inside the picocom distribution source directory): - man ./picocom.1 + man ./picocom.1 or (assuming you have installed the manual page to a suitable place): - man picocom + man picocom Thanks for using picocom @@ -198,7 +198,7 @@ access these console ports remotely. If you provide shell-access to termbox for your users, then it's as simple as having the users say (from their remote workstations): - $ ssh -t user@termbox picocom -b 115200 /dev/ttyS0 + $ ssh -t user@termbox picocom -b 115200 /dev/ttyS0 Or make a convenient script/alias for this. Remember the `-t` switch which instructs ssh to create a pseudo-tty, otherwise picocom won't @@ -213,7 +213,7 @@ go from Linux to other Unix-like systems. Login to termbox and create a user called _termbox_: - $ sudo useradd -r -m termbox + $ sudo useradd -r -m termbox The `-r` means "system account", and the `-m` means *do* make the home-directory. Mostly we need this account's home-directory as a @@ -223,48 +223,48 @@ password. Switch to the _termbox_ account and create a `bin` directory in its home-dir. - $ sudo su termbox - $ cd ~ - $ mkdir bin + $ sudo su termbox + $ cd ~ + $ mkdir bin Copy the picocom binary in `~termbox/bin` (if you don't have it globally installed): - $ cp /path/to/picocom ./bin + $ cp /path/to/picocom ./bin For every serial port you want to provide access to, create a file named after the port and put it in `~termbox/bin`. It should look like this: - $ cat ./bin/ttyS0 - #!/bin/sh - exec /home/termbox/bin/picocom \ - --send-cmd '' \ - --receive-cmd '' \ - -b 115200 \ - /dev/ttyS0 + $ cat ./bin/ttyS0 + #!/bin/sh + exec /home/termbox/bin/picocom \ + --send-cmd '' \ + --receive-cmd '' \ + -b 115200 \ + /dev/ttyS0 And make it executable: - $ chmod +x ./bin/ttyS0 + $ chmod +x ./bin/ttyS0 Repeat accordingly for every other port. Now the contents of `~termbox/bin` should look like this: - $ ls -l ./bin - -rwxrwxr-x 1 termbox termbox 102128 Aug 29 13:56 picocom* - -rwxrwxr-x 1 termbox termbox 108 Aug 29 14:07 ttyS0* - -rwxrwxr-x 1 termbox termbox 108 Aug 29 14:07 ttyS1* - ... and so on ... + $ ls -l ./bin + -rwxrwxr-x 1 termbox termbox 102128 Aug 29 13:56 picocom* + -rwxrwxr-x 1 termbox termbox 108 Aug 29 14:07 ttyS0* + -rwxrwxr-x 1 termbox termbox 108 Aug 29 14:07 ttyS1* + ... and so on ... Exit the _termbox_ account: - $ exit + $ exit Now, for every serial port, create a user account named after the port, like this: - $ sudo useradd -r -g dialout -d ~termbox -M -s ~termbox/bin/ttyS0 ttyS0 + $ sudo useradd -r -g dialout -d ~termbox -M -s ~termbox/bin/ttyS0 ttyS0 Observe that we make `dialout` the default group for this account, so the account has access to the serial ports. Also observe that we make @@ -280,9 +280,9 @@ useradd create the usual `/home/ttyS0`. Then set a password for the newly created account: - $ sudo passwd ttyS0 - Enter new UNIX password: ****** - Retype new UNIX password: ****** + $ sudo passwd ttyS0 + Enter new UNIX password: ****** + Retype new UNIX password: ****** Repeat (create user account, set password) for every port you want to give access to. @@ -290,7 +290,7 @@ give access to. You 're set. All a user has to do to remotely access the console connected to termbox's `/dev/ttyS0` port, is: - ssh ttyS0@termbox + ssh ttyS0@termbox Some interesting points: diff --git a/TODO b/TODO index d3f5a12..8b13789 100644 --- a/TODO +++ b/TODO @@ -1 +1 @@ - + diff --git a/fdio.c b/fdio.c index aaecc85..f8d1499 100644 --- a/fdio.c +++ b/fdio.c @@ -19,7 +19,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #include @@ -34,7 +34,7 @@ ssize_t writen_ni(int fd, const void *buff, size_t n) { - size_t nl; + size_t nl; ssize_t nw; const char *p; @@ -48,7 +48,7 @@ writen_ni(int fd, const void *buff, size_t n) nl -= nw; p += nw; } - + return n - nl; } @@ -58,12 +58,12 @@ fd_printf (int fd, const char *format, ...) char buf[256]; va_list args; int len; - + va_start(args, format); len = vsnprintf(buf, sizeof(buf), format, args); buf[sizeof(buf) - 1] = '\0'; va_end(args); - + return writen_ni(fd, buf, len); } @@ -71,30 +71,30 @@ fd_printf (int fd, const char *format, ...) #ifndef LINENOISE -static int -cput(int fd, char c) -{ - return write(fd, &c, 1); +static int +cput(int fd, char c) +{ + return write(fd, &c, 1); } -static int +static int cdel (int fd) { const char del[] = "\b \b"; return write(fd, del, sizeof(del) - 1); } -static int +static int xput (int fd, unsigned char c) { - const char hex[] = "0123456789abcdef"; + const char hex[] = "0123456789abcdef"; char b[4]; b[0] = '\\'; b[1] = 'x'; b[2] = hex[c >> 4]; b[3] = hex[c & 0x0f]; return write(fd, b, sizeof(b)); } -static int +static int xdel (int fd) { const char del[] = "\b\b\b\b \b\b\b\b"; @@ -107,7 +107,7 @@ fd_readline (int fdi, int fdo, char *b, int bsz) int r; unsigned char c; unsigned char *bp, *bpe; - + bp = (unsigned char *)b; bpe = (unsigned char *)b + bsz - 1; @@ -118,11 +118,11 @@ fd_readline (int fdi, int fdo, char *b, int bsz) switch (c) { case '\b': case '\x7f': - if ( bp > (unsigned char *)b ) { + if ( bp > (unsigned char *)b ) { bp--; - if ( isprint(*bp) ) + if ( isprint(*bp) ) cdel(fdo); - else + else xdel(fdo); } else { cput(fdo, '\x07'); @@ -134,17 +134,17 @@ fd_readline (int fdi, int fdo, char *b, int bsz) goto out; case '\r': *bp = '\0'; - r = bp - (unsigned char *)b; + r = bp - (unsigned char *)b; goto out; default: - if ( bp < bpe ) { + if ( bp < bpe ) { *bp++ = c; - if ( isprint(c) ) - cput(fdo, c); - else + if ( isprint(c) ) + cput(fdo, c); + else xput(fdo, c); - } else { - cput(fdo, '\x07'); + } else { + cput(fdo, '\x07'); } break; } diff --git a/fdio.h b/fdio.h index 4c30d1b..ad5dd87 100644 --- a/fdio.h +++ b/fdio.h @@ -19,7 +19,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #ifndef FDIO_H diff --git a/linenoise-1.0/README.markdown b/linenoise-1.0/README.markdown index c845673..66ffd6f 100644 --- a/linenoise-1.0/README.markdown +++ b/linenoise-1.0/README.markdown @@ -17,7 +17,7 @@ So what usually happens is either: * Large programs with configure scripts disabling line editing if readline is not present in the system, or not supporting it at all since readline is GPL licensed and libedit (the BSD clone) is not as known and available as readline is (Real world example of this problem: Tclsh). * Smaller programs not using a configure script not supporting line editing at all (A problem we had with Redis-cli for instance). - + The result is a pollution of binaries without line editing support. So I spent more or less two hours doing a reality check resulting in this little library: is it *really* needed for a line editing library to be 20k lines of code? Apparently not, it is possibe to get a very small, zero configuration, trivial to embed library, that solves the problem. Smaller programs will just include this, supporing line editing out of the box. Larger programs may use this little library or just checking with configure if readline/libedit is available and resorting to linenoise if not. diff --git a/linenoise-1.0/linenoise.c b/linenoise-1.0/linenoise.c index 02ed35b..c4e23bc 100644 --- a/linenoise-1.0/linenoise.c +++ b/linenoise-1.0/linenoise.c @@ -150,25 +150,25 @@ struct linenoiseState { }; enum KEY_ACTION{ - KEY_NULL = 0, /* NULL */ - CTRL_A = 1, /* Ctrl+a */ - CTRL_B = 2, /* Ctrl-b */ - CTRL_C = 3, /* Ctrl-c */ - CTRL_D = 4, /* Ctrl-d */ - CTRL_E = 5, /* Ctrl-e */ - CTRL_F = 6, /* Ctrl-f */ - CTRL_H = 8, /* Ctrl-h */ - TAB = 9, /* Tab */ - CTRL_K = 11, /* Ctrl+k */ - CTRL_L = 12, /* Ctrl+l */ - ENTER = 13, /* Enter */ - CTRL_N = 14, /* Ctrl-n */ - CTRL_P = 16, /* Ctrl-p */ - CTRL_T = 20, /* Ctrl-t */ - CTRL_U = 21, /* Ctrl+u */ - CTRL_W = 23, /* Ctrl+w */ - ESC = 27, /* Escape */ - BACKSPACE = 127 /* Backspace */ + KEY_NULL = 0, /* NULL */ + CTRL_A = 1, /* Ctrl+a */ + CTRL_B = 2, /* Ctrl-b */ + CTRL_C = 3, /* Ctrl-c */ + CTRL_D = 4, /* Ctrl-d */ + CTRL_E = 5, /* Ctrl-e */ + CTRL_F = 6, /* Ctrl-f */ + CTRL_H = 8, /* Ctrl-h */ + TAB = 9, /* Tab */ + CTRL_K = 11, /* Ctrl+k */ + CTRL_L = 12, /* Ctrl+l */ + ENTER = 13, /* Enter */ + CTRL_N = 14, /* Ctrl-n */ + CTRL_P = 16, /* Ctrl-p */ + CTRL_T = 20, /* Ctrl-t */ + CTRL_U = 21, /* Ctrl+u */ + CTRL_W = 23, /* Ctrl+w */ + ESC = 27, /* Escape */ + BACKSPACE = 127 /* Backspace */ }; static void linenoiseAtExit(void); diff --git a/picocom.1 b/picocom.1 index 0eebc2e..2446439 100644 --- a/picocom.1 +++ b/picocom.1 @@ -387,7 +387,7 @@ Example: .IP .nf \f[C] -***\ baud:\ 115200\ (9600)\ +***\ baud:\ 115200\ (9600)\ \f[] .fi .PP diff --git a/picocom.1.md b/picocom.1.md index f1bde20..fe5a4a4 100644 --- a/picocom.1.md +++ b/picocom.1.md @@ -9,7 +9,7 @@ header: User Commands picocom - minimal dumb-terminal emulation program # SYNOPSIS - + **picocom** [ _options_ ] _device_ # DESCRIPTION @@ -46,7 +46,7 @@ Commands are given to picocom by first keying the *espace character* which by default is **C-a** (see **[OPTIONS]** below for how to change it), and then keying one of the function (command) characters shown here. - + *escape character* : Send the escape character to the serial port and return to @@ -146,7 +146,7 @@ here. : Send (upload) a file. See **[SENDING AND RECEIVING FILES]** below. - + **C-r** : Receive (download) a file. See **[SENDING AND RECEIVING FILES]** @@ -174,7 +174,7 @@ Picocom accepts the following command-line options. one of: **x** for xon/xoff (software) mode, **h** for hardware flow control (RTS/CTS), **n** for no flow control. (Default: **n**) - + **--parity** | **-y** : Defines the parity mode to set the serial-port to. Must be one @@ -190,13 +190,13 @@ Picocom accepts the following command-line options. : Defines the number of stop bits in every character. Must be one of: **1**, or **2**. (Default: **1**) - + **--escape** | **-e** : Defines the character that will make picocom enter command-mode (see description above). If **x** is given, then **C-x** will make picocom enter command mode. (Default: **a**) - + **--echo** | **-c** : Enable local echo. Every character being read from the terminal @@ -213,7 +213,7 @@ Picocom accepts the following command-line options. connection, or altering the settings. If required, serial port parameters can then be adjusted at run-time by commands. (Default: Disabled) - + **--noreset** | **-r** : If given, picocom will not reset the serial port when exiting. It @@ -224,7 +224,7 @@ Picocom accepts the following command-line options. "Exit"), which never resets the serial port. If **--noreset** is given then "Quit" and "Exit" behave essentially the same. (Default: Disabled) - + **--nolock** | **-l** : If given, picocom will _not_ attempt to lock the serial port @@ -236,14 +236,14 @@ Picocom accepts the following command-line options. is possible that your picocom binary is compiled without support for locking. In this case the **--nolock** option is accepted, but has no effect. (Default: Disabled) - + **--send-cmd** | **-s** : Specifies the external program (and any arguments to it) that will be used for transmitting files. If the argument to **--send-cmd** is the empty string (''), the send-file command is disabled. See **[SENDING AND RECEIVING FILES]**. (Default: **sz -vv**) - + **--receive-cmd** | **-v** : Specifies the external program (and any arguments to it) that will @@ -251,19 +251,19 @@ Picocom accepts the following command-line options. is the empty string (''), the receive-file command is disabled. See **[SENDING AND RECEIVING FILES]**. (Default: **rz -vv**) - + **--imap** : Specifies the input character map (i.e. special characters to be replaced when read from the serial port). See **[INPUT, OUTPUT, AND ECHO MAPPING]**. (Defaul: Empty) - + **--omap** : Specifies the output character map (i.e. special characters to be replaced before being written to serial port). See **[INPUT, OUTPUT, AND ECHO MAPPING]**. (Defaul: Empty) - + **--emap** : Specifies the local-echo character map (i.e. special characters to @@ -302,7 +302,7 @@ Picocom accepts the following command-line options. features are also shown. -# DISPLAY OF OPTIONS AND PORT SETTINGS +# DISPLAY OF OPTIONS AND PORT SETTINGS The "show program options" command (**C-v**), as well as the commands that change program options (**C-b**, **C-u**, **C-d**, **C-f**, etc) @@ -313,7 +313,7 @@ respective option (for whatever reason), then the value of the option is shown followed by the value of the actual serial-port setting in parenthesis. Example: - *** baud: 115200 (9600) + *** baud: 115200 (9600) This means that a baud rate of 115200bps has been selected (from the command line, or using commands that change the baudrate) but the @@ -343,7 +343,7 @@ programs for this purpose are: - **sb(1)** - send using the Y-MODEM protocol - **sz(1)** - send using the Z-MODEM protocol - **ascii-xfr(1)** - receive or transmit ASCII files - + The name of, and the command-line options to, the program to be used for transmitting files are given by the **--send-cmd** option. Similarly the program to receive files, and its arguments, are @@ -397,20 +397,20 @@ the terminal (standard output) if local echo is enabled (with **--emap**). These mapping options take, each, a single argument which is a comma-separated list of one or more of the following identifiers: -- **crlf** (map CR to LF), -- **crcrlf** (map CR to CR + LF), -- **igncr** (ignore CR), +- **crlf** (map CR to LF), +- **crcrlf** (map CR to CR + LF), +- **igncr** (ignore CR), - **lfcr** (map LF to CR), -- **lfcrlf** (map LF to CR + LF), -- **ignlf** (ignore LF), -- **bsdel** (map BS to DEL), +- **lfcrlf** (map LF to CR + LF), +- **ignlf** (ignore LF), +- **bsdel** (map BS to DEL), - **delbs** (map DEL to BS) For example the command: picocom --omap crlf,delbs --imap ignlf,bsdel --emap crcrlf ... -will: +will: - Replace every CR (carriage return, 0x0d) character with LF (line feed, 0x0a) and every DEL (delete, 0x7f) character with BS diff --git a/picocom.c b/picocom.c index e3a5c72..62ddd31 100644 --- a/picocom.c +++ b/picocom.c @@ -20,7 +20,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #include @@ -59,19 +59,19 @@ /* parity modes names */ const char *parity_str[] = { - [P_NONE] = "none", - [P_EVEN] = "even", - [P_ODD] = "odd", - [P_MARK] = "mark", - [P_SPACE] = "space", + [P_NONE] = "none", + [P_EVEN] = "even", + [P_ODD] = "odd", + [P_MARK] = "mark", + [P_SPACE] = "space", }; /* flow control modes names */ const char *flow_str[] = { - [FC_NONE] = "none", - [FC_RTSCTS] = "RTS/CTS", - [FC_XONXOFF] = "xon/xoff", - [FC_OTHER] = "other", + [FC_NONE] = "none", + [FC_RTSCTS] = "RTS/CTS", + [FC_XONXOFF] = "xon/xoff", + [FC_OTHER] = "other", }; /**********************************************************************/ @@ -88,12 +88,12 @@ const char *flow_str[] = { #define KEY_TOG_RTS CKEY('g') /* toggle RTS */ #define KEY_BAUD CKEY('b') /* set baudrate */ #define KEY_BAUD_UP CKEY('u') /* increase baudrate (up) */ -#define KEY_BAUD_DN CKEY('d') /* decrase baudrate (down) */ -#define KEY_FLOW CKEY('f') /* change flowcntrl mode */ -#define KEY_PARITY CKEY('y') /* change parity mode */ -#define KEY_BITS CKEY('i') /* change number of databits */ -#define KEY_STOP CKEY('j') /* change number of stopbits */ -#define KEY_LECHO CKEY('c') /* toggle local echo */ +#define KEY_BAUD_DN CKEY('d') /* decrase baudrate (down) */ +#define KEY_FLOW CKEY('f') /* change flowcntrl mode */ +#define KEY_PARITY CKEY('y') /* change parity mode */ +#define KEY_BITS CKEY('i') /* change number of databits */ +#define KEY_STOP CKEY('j') /* change number of stopbits */ +#define KEY_LECHO CKEY('c') /* toggle local echo */ #define KEY_STATUS CKEY('v') /* show program options */ #define KEY_HELP CKEY('h') /* show help (same as [C-k]) */ #define KEY_KEYS CKEY('k') /* show available command keys */ @@ -121,100 +121,100 @@ const char *flow_str[] = { /* character mapping names */ struct map_names_s { - char *name; - int flag; + char *name; + int flag; } map_names[] = { - { "crlf", M_CRLF }, - { "crcrlf", M_CRCRLF }, - { "igncr", M_IGNCR }, + { "crlf", M_CRLF }, + { "crcrlf", M_CRCRLF }, + { "igncr", M_IGNCR }, { "lfcr", M_LFCR }, - { "lfcrlf", M_LFCRLF }, - { "ignlf", M_IGNLF }, - { "delbs", M_DELBS }, - { "bsdel", M_BSDEL }, - /* Sentinel */ - { NULL, 0 } + { "lfcrlf", M_LFCRLF }, + { "ignlf", M_IGNLF }, + { "delbs", M_DELBS }, + { "bsdel", M_BSDEL }, + /* Sentinel */ + { NULL, 0 } }; int parse_map (char *s) { - char *m, *t; - int f, flags, i; + char *m, *t; + int f, flags, i; - flags = 0; - while ( (t = strtok(s, ", \t")) ) { - for (i=0; (m = map_names[i].name); i++) { - if ( ! strcmp(t, m) ) { - f = map_names[i].flag; - break; - } - } - if ( m ) flags |= f; - else { flags = -1; break; } - s = NULL; - } + flags = 0; + while ( (t = strtok(s, ", \t")) ) { + for (i=0; (m = map_names[i].name); i++) { + if ( ! strcmp(t, m) ) { + f = map_names[i].flag; + break; + } + } + if ( m ) flags |= f; + else { flags = -1; break; } + s = NULL; + } - return flags; + return flags; } void print_map (int flags) { - int i; + int i; - for (i = 0; i < M_NFLAGS; i++) - if ( flags & (1 << i) ) - printf("%s,", map_names[i].name); - printf("\n"); + for (i = 0; i < M_NFLAGS; i++) + if ( flags & (1 << i) ) + printf("%s,", map_names[i].name); + printf("\n"); } /**********************************************************************/ struct { - char *port; - int baud; - enum flowcntrl_e flow; - enum parity_e parity; - int databits; - int stopbits; - int lecho; - int noinit; - int noreset; + char *port; + int baud; + enum flowcntrl_e flow; + enum parity_e parity; + int databits; + int stopbits; + int lecho; + int noinit; + int noreset; #if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK) - int nolock; + int nolock; #endif - unsigned char escape; - char send_cmd[128]; - char receive_cmd[128]; - int imap; - int omap; - int emap; - char *log_filename; - int lower_rts; - int lower_dtr; + unsigned char escape; + char send_cmd[128]; + char receive_cmd[128]; + int imap; + int omap; + int emap; + char *log_filename; + int lower_rts; + int lower_dtr; } opts = { - .port = NULL, - .baud = 9600, - .flow = FC_NONE, - .parity = P_NONE, - .databits = 8, - .stopbits = 1, - .lecho = 0, - .noinit = 0, - .noreset = 0, + .port = NULL, + .baud = 9600, + .flow = FC_NONE, + .parity = P_NONE, + .databits = 8, + .stopbits = 1, + .lecho = 0, + .noinit = 0, + .noreset = 0, #if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK) - .nolock = 0, + .nolock = 0, #endif - .escape = CKEY('a'), - .send_cmd = "sz -vv", - .receive_cmd = "rz -vv -E", - .imap = M_I_DFL, - .omap = M_O_DFL, - .emap = M_E_DFL, - .log_filename = NULL, - .lower_rts = 0, - .lower_dtr = 0 + .escape = CKEY('a'), + .send_cmd = "sz -vv", + .receive_cmd = "rz -vv -E", + .imap = M_I_DFL, + .omap = M_O_DFL, + .emap = M_E_DFL, + .log_filename = NULL, + .lower_rts = 0, + .lower_dtr = 0 }; int sig_exit = 0; @@ -230,8 +230,8 @@ int log_fd = -1; #endif struct tty_q { - int len; - unsigned char buff[TTY_Q_SZ]; + int len; + unsigned char buff[TTY_Q_SZ]; } tty_q; #define TTY_RD_SZ 128 @@ -241,11 +241,11 @@ int tty_write_sz; #define TTY_WRITE_SZ_DIV 10 #define TTY_WRITE_SZ_MIN 8 -#define set_tty_write_sz(baud) \ - do { \ - tty_write_sz = (baud) / TTY_WRITE_SZ_DIV; \ - if ( tty_write_sz < TTY_WRITE_SZ_MIN ) \ - tty_write_sz = TTY_WRITE_SZ_MIN; \ +#define set_tty_write_sz(baud) \ + do { \ + tty_write_sz = (baud) / TTY_WRITE_SZ_DIV; \ + if ( tty_write_sz < TTY_WRITE_SZ_MIN ) \ + tty_write_sz = TTY_WRITE_SZ_MIN; \ } while (0) /**********************************************************************/ @@ -261,71 +261,71 @@ char lockname[_POSIX_PATH_MAX] = ""; int uucp_lockname(const char *dir, const char *file) { - char *p, *cp; - struct stat sb; + char *p, *cp; + struct stat sb; - if ( ! dir || *dir == '\0' || stat(dir, &sb) != 0 ) - return -1; + if ( ! dir || *dir == '\0' || stat(dir, &sb) != 0 ) + return -1; - /* cut-off initial "/dev/" from file-name */ - p = strchr(file + 1, '/'); - p = p ? p + 1 : (char *)file; - /* replace '/'s with '_'s in what remains (after making a copy) */ - p = cp = strdup(p); - do { if ( *p == '/' ) *p = '_'; } while(*p++); - /* build lockname */ - snprintf(lockname, sizeof(lockname), "%s/LCK..%s", dir, cp); - /* destroy the copy */ - free(cp); + /* cut-off initial "/dev/" from file-name */ + p = strchr(file + 1, '/'); + p = p ? p + 1 : (char *)file; + /* replace '/'s with '_'s in what remains (after making a copy) */ + p = cp = strdup(p); + do { if ( *p == '/' ) *p = '_'; } while(*p++); + /* build lockname */ + snprintf(lockname, sizeof(lockname), "%s/LCK..%s", dir, cp); + /* destroy the copy */ + free(cp); - return 0; + return 0; } int uucp_lock(void) { - int r, fd, pid; - char buf[16]; - mode_t m; + int r, fd, pid; + char buf[16]; + mode_t m; - if ( lockname[0] == '\0' ) return 0; + if ( lockname[0] == '\0' ) return 0; - fd = open(lockname, O_RDONLY); - if ( fd >= 0 ) { - r = read(fd, buf, sizeof(buf)); - close(fd); - /* if r == 4, lock file is binary (old-style) */ - pid = (r == 4) ? *(int *)buf : strtol(buf, NULL, 10); - if ( pid > 0 - && kill((pid_t)pid, 0) < 0 - && errno == ESRCH ) { - /* stale lock file */ - printf("Removing stale lock: %s\n", lockname); - sleep(1); - unlink(lockname); - } else { - lockname[0] = '\0'; - errno = EEXIST; - return -1; - } - } - /* lock it */ - m = umask(022); - fd = open(lockname, O_WRONLY|O_CREAT|O_EXCL, 0666); - if ( fd < 0 ) { lockname[0] = '\0'; return -1; } - umask(m); - snprintf(buf, sizeof(buf), "%04d\n", getpid()); - write(fd, buf, strlen(buf)); - close(fd); + fd = open(lockname, O_RDONLY); + if ( fd >= 0 ) { + r = read(fd, buf, sizeof(buf)); + close(fd); + /* if r == 4, lock file is binary (old-style) */ + pid = (r == 4) ? *(int *)buf : strtol(buf, NULL, 10); + if ( pid > 0 + && kill((pid_t)pid, 0) < 0 + && errno == ESRCH ) { + /* stale lock file */ + printf("Removing stale lock: %s\n", lockname); + sleep(1); + unlink(lockname); + } else { + lockname[0] = '\0'; + errno = EEXIST; + return -1; + } + } + /* lock it */ + m = umask(022); + fd = open(lockname, O_WRONLY|O_CREAT|O_EXCL, 0666); + if ( fd < 0 ) { lockname[0] = '\0'; return -1; } + umask(m); + snprintf(buf, sizeof(buf), "%04d\n", getpid()); + write(fd, buf, strlen(buf)); + close(fd); - return 0; + return 0; } int uucp_unlock(void) { - if ( lockname[0] ) unlink(lockname); - return 0; + if ( lockname[0] ) unlink(lockname); + return 0; } #endif /* of UUCP_LOCK_DIR */ @@ -335,38 +335,38 @@ uucp_unlock(void) void fatal (const char *format, ...) { - char *s, buf[256]; - va_list args; - int len; + char *s, buf[256]; + va_list args; + int len; - term_reset(STO); - term_reset(STI); - - va_start(args, format); - len = vsnprintf(buf, sizeof(buf), format, args); - buf[sizeof(buf) - 1] = '\0'; - va_end(args); - - s = "\r\nFATAL: "; - writen_ni(STO, s, strlen(s)); - writen_ni(STO, buf, len); - s = "\r\n"; - writen_ni(STO, s, strlen(s)); + term_reset(STO); + term_reset(STI); - /* wait a bit for output to drain */ - sleep(1); + va_start(args, format); + len = vsnprintf(buf, sizeof(buf), format, args); + buf[sizeof(buf) - 1] = '\0'; + va_end(args); + + s = "\r\nFATAL: "; + writen_ni(STO, s, strlen(s)); + writen_ni(STO, buf, len); + s = "\r\n"; + writen_ni(STO, s, strlen(s)); + + /* wait a bit for output to drain */ + sleep(1); #ifdef UUCP_LOCK_DIR - uucp_unlock(); + uucp_unlock(); #endif - - free(opts.port); - if (opts.log_filename) { - free(opts.log_filename); - close(log_fd); - } - exit(EXIT_FAILURE); + free(opts.port); + if (opts.log_filename) { + free(opts.log_filename); + close(log_fd); + } + + exit(EXIT_FAILURE); } /**********************************************************************/ @@ -376,158 +376,158 @@ fatal (const char *format, ...) char * read_filename (void) { - char fname[_POSIX_PATH_MAX]; - int r; + char fname[_POSIX_PATH_MAX]; + int r; - fd_printf(STO, "\r\n*** file: "); - r = fd_readline(STI, STO, fname, sizeof(fname)); - fd_printf(STO, "\r\n"); - if ( r < 0 ) - return NULL; - else - return strdup(fname); + fd_printf(STO, "\r\n*** file: "); + r = fd_readline(STI, STO, fname, sizeof(fname)); + fd_printf(STO, "\r\n"); + if ( r < 0 ) + return NULL; + else + return strdup(fname); } int read_baud (void) { - char baudstr[9], *ep; - int baud = -1, r; + char baudstr[9], *ep; + int baud = -1, r; - do { - fd_printf(STO, "\r\n*** baud: "); - r = fd_readline(STI, STO, baudstr, sizeof(baudstr)); - fd_printf(STO, "\r\n"); - if ( r < 0 ) - break; - baud = strtol(baudstr, &ep, 0); - if ( ! ep || *ep != '\0' || ! term_baud_ok(baud) || baud == 0 ) { - fd_printf(STO, "*** Invalid baudrate!"); - baud = -1; - } - } while (baud < 0); + do { + fd_printf(STO, "\r\n*** baud: "); + r = fd_readline(STI, STO, baudstr, sizeof(baudstr)); + fd_printf(STO, "\r\n"); + if ( r < 0 ) + break; + baud = strtol(baudstr, &ep, 0); + if ( ! ep || *ep != '\0' || ! term_baud_ok(baud) || baud == 0 ) { + fd_printf(STO, "*** Invalid baudrate!"); + baud = -1; + } + } while (baud < 0); - return baud; + return baud; } #else /* LINENOISE defined */ -void -file_completion_cb (const char *buf, linenoiseCompletions *lc) +void +file_completion_cb (const char *buf, linenoiseCompletions *lc) { - DIR *dirp; - struct dirent *dp; - char *basec, *basen, *dirc, *dirn; - int baselen, dirlen; - char *fullpath; - struct stat filestat; + DIR *dirp; + struct dirent *dp; + char *basec, *basen, *dirc, *dirn; + int baselen, dirlen; + char *fullpath; + struct stat filestat; - basec = strdup(buf); - dirc = strdup(buf); - dirn = dirname(dirc); - dirlen = strlen(dirn); - basen = basename(basec); - baselen = strlen(basen); - dirp = opendir(dirn); + basec = strdup(buf); + dirc = strdup(buf); + dirn = dirname(dirc); + dirlen = strlen(dirn); + basen = basename(basec); + baselen = strlen(basen); + dirp = opendir(dirn); - if (dirp) { - while ((dp = readdir(dirp)) != NULL) { - if (strncmp(basen, dp->d_name, baselen) == 0) { - /* add 2 extra bytes for possible / in middle & at end */ - fullpath = (char *) malloc(strlen(dp->d_name) + dirlen + 3); - strcpy(fullpath, dirn); - if (fullpath[dirlen-1] != '/') - strcat(fullpath, "/"); - strcat(fullpath, dp->d_name); - if (stat(fullpath, &filestat) == 0) { - if (S_ISDIR(filestat.st_mode)) { - strcat(fullpath, "/"); - } - linenoiseAddCompletion(lc,fullpath); - } - free(fullpath); - } - } + if (dirp) { + while ((dp = readdir(dirp)) != NULL) { + if (strncmp(basen, dp->d_name, baselen) == 0) { + /* add 2 extra bytes for possible / in middle & at end */ + fullpath = (char *) malloc(strlen(dp->d_name) + dirlen + 3); + strcpy(fullpath, dirn); + if (fullpath[dirlen-1] != '/') + strcat(fullpath, "/"); + strcat(fullpath, dp->d_name); + if (stat(fullpath, &filestat) == 0) { + if (S_ISDIR(filestat.st_mode)) { + strcat(fullpath, "/"); + } + linenoiseAddCompletion(lc,fullpath); + } + free(fullpath); + } + } - closedir(dirp); - } - free(basec); - free(dirc); + closedir(dirp); + } + free(basec); + free(dirc); } static char *history_file_path = NULL; -void +void init_history (void) { - char *home_directory; + char *home_directory; - home_directory = getenv("HOME"); - if (home_directory) { - history_file_path = malloc(strlen(home_directory) + 2 + - strlen(HISTFILE)); - strcpy(history_file_path, home_directory); - if (home_directory[strlen(home_directory)-1] != '/') { - strcat(history_file_path, "/"); - } - strcat(history_file_path, HISTFILE); - linenoiseHistoryLoad(history_file_path); - } + home_directory = getenv("HOME"); + if (home_directory) { + history_file_path = malloc(strlen(home_directory) + 2 + + strlen(HISTFILE)); + strcpy(history_file_path, home_directory); + if (home_directory[strlen(home_directory)-1] != '/') { + strcat(history_file_path, "/"); + } + strcat(history_file_path, HISTFILE); + linenoiseHistoryLoad(history_file_path); + } } -void +void cleanup_history (void) { - if (history_file_path) - free(history_file_path); + if (history_file_path) + free(history_file_path); } -void +void add_history (char *fname) { - linenoiseHistoryAdd(fname); - if (history_file_path) - linenoiseHistorySave(history_file_path); + linenoiseHistoryAdd(fname); + if (history_file_path) + linenoiseHistorySave(history_file_path); } char * read_filename (void) { - char *fname; - linenoiseSetCompletionCallback(file_completion_cb); - fd_printf(STO, "\r\n"); - fname = linenoise("*** file: "); - fd_printf(STO, "\r"); - linenoiseSetCompletionCallback(NULL); - if (fname != NULL) - add_history(fname); - return fname; + char *fname; + linenoiseSetCompletionCallback(file_completion_cb); + fd_printf(STO, "\r\n"); + fname = linenoise("*** file: "); + fd_printf(STO, "\r"); + linenoiseSetCompletionCallback(NULL); + if (fname != NULL) + add_history(fname); + return fname; } int read_baud (void) { - char *baudstr, *ep; - int baud = -1; + char *baudstr, *ep; + int baud = -1; - do { - fd_printf(STO, "\r\n"); - baudstr = linenoise("*** baud: "); - fd_printf(STO, "\r"); - if ( baudstr == NULL ) - break; - baud = strtol(baudstr, &ep, 0); - if ( ! ep || *ep != '\0' || ! term_baud_ok(baud) || baud == 0 ) { - fd_printf(STO, "*** Invalid baudrate!"); - baud = -1; - } - free(baudstr); - } while (baud < 0); + do { + fd_printf(STO, "\r\n"); + baudstr = linenoise("*** baud: "); + fd_printf(STO, "\r"); + if ( baudstr == NULL ) + break; + baud = strtol(baudstr, &ep, 0); + if ( ! ep || *ep != '\0' || ! term_baud_ok(baud) || baud == 0 ) { + fd_printf(STO, "*** Invalid baudrate!"); + baud = -1; + } + free(baudstr); + } while (baud < 0); - if (baudstr != NULL) - add_history(baudstr); + if (baudstr != NULL) + add_history(baudstr); - return baud; + return baud; } #endif /* of ifndef LINENOISE */ @@ -541,67 +541,67 @@ read_baud (void) int do_map (char *b, int map, char c) { - int n; + int n; - switch (c) { - case '\x7f': - /* DEL mapings */ - if ( map & M_DELBS ) { - b[0] = '\x08'; n = 1; - } else { - b[0] = c; n = 1; - } - break; - case '\x08': - /* BS mapings */ - if ( map & M_BSDEL ) { - b[0] = '\x7f'; n = 1; - } else { - b[0] = c; n = 1; - } - break; - case '\x0d': - /* CR mappings */ - if ( map & M_CRLF ) { - b[0] = '\x0a'; n = 1; - } else if ( map & M_CRCRLF ) { - b[0] = '\x0d'; b[1] = '\x0a'; n = 2; - } else if ( map & M_IGNCR ) { - n = 0; - } else { - b[0] = c; n = 1; - } - break; - case '\x0a': - /* LF mappings */ - if ( map & M_LFCR ) { - b[0] = '\x0d'; n = 1; - } else if ( map & M_LFCRLF ) { - b[0] = '\x0d'; b[1] = '\x0a'; n = 2; - } else if ( map & M_IGNLF ) { - n = 0; - } else { - b[0] = c; n = 1; - } - break; - default: - b[0] = c; n = 1; - break; - } + switch (c) { + case '\x7f': + /* DEL mapings */ + if ( map & M_DELBS ) { + b[0] = '\x08'; n = 1; + } else { + b[0] = c; n = 1; + } + break; + case '\x08': + /* BS mapings */ + if ( map & M_BSDEL ) { + b[0] = '\x7f'; n = 1; + } else { + b[0] = c; n = 1; + } + break; + case '\x0d': + /* CR mappings */ + if ( map & M_CRLF ) { + b[0] = '\x0a'; n = 1; + } else if ( map & M_CRCRLF ) { + b[0] = '\x0d'; b[1] = '\x0a'; n = 2; + } else if ( map & M_IGNCR ) { + n = 0; + } else { + b[0] = c; n = 1; + } + break; + case '\x0a': + /* LF mappings */ + if ( map & M_LFCR ) { + b[0] = '\x0d'; n = 1; + } else if ( map & M_LFCRLF ) { + b[0] = '\x0d'; b[1] = '\x0a'; n = 2; + } else if ( map & M_IGNLF ) { + n = 0; + } else { + b[0] = c; n = 1; + } + break; + default: + b[0] = c; n = 1; + break; + } - return n; + return n; } -void +void map_and_write (int fd, int map, char c) { - char b[M_MAXMAP]; - int n; - - n = do_map(b, map, c); - if ( n ) - if ( writen_ni(fd, b, n) < n ) - fatal("write to stdout failed: %s", strerror(errno)); + char b[M_MAXMAP]; + int n; + + n = do_map(b, map, c); + if ( n ) + if ( writen_ni(fd, b, n) < n ) + fatal("write to stdout failed: %s", strerror(errno)); } /**********************************************************************/ @@ -609,199 +609,199 @@ map_and_write (int fd, int map, char c) int baud_up (int baud) { - return term_baud_up(baud); + return term_baud_up(baud); } int baud_down (int baud) { - int nb; - nb = term_baud_down(baud); - if (nb == 0) - nb = baud; - return nb; + int nb; + nb = term_baud_down(baud); + if (nb == 0) + nb = baud; + return nb; } int flow_next (int flow) { - switch(flow) { - case FC_NONE: - flow = FC_RTSCTS; - break; - case FC_RTSCTS: - flow = FC_XONXOFF; - break; - case FC_XONXOFF: - flow = FC_NONE; - break; - default: - flow = FC_NONE; - break; - } + switch(flow) { + case FC_NONE: + flow = FC_RTSCTS; + break; + case FC_RTSCTS: + flow = FC_XONXOFF; + break; + case FC_XONXOFF: + flow = FC_NONE; + break; + default: + flow = FC_NONE; + break; + } - return flow; + return flow; } int parity_next (int parity) { - switch(parity) { - case P_NONE: - parity = P_EVEN; - break; - case P_EVEN: - parity = P_ODD; - break; - case P_ODD: - parity = P_NONE; - break; - default: - parity = P_NONE; - break; - } + switch(parity) { + case P_NONE: + parity = P_EVEN; + break; + case P_EVEN: + parity = P_ODD; + break; + case P_ODD: + parity = P_NONE; + break; + default: + parity = P_NONE; + break; + } - return parity; + return parity; } int bits_next (int bits) { - bits++; - if (bits > 8) bits = 5; + bits++; + if (bits > 8) bits = 5; - return bits; + return bits; } int stopbits_next (int bits) { - bits++; - if (bits > 2) bits = 1; + bits++; + if (bits > 2) bits = 1; - return bits; + return bits; } void -show_status (int dtr_up, int rts_up) +show_status (int dtr_up, int rts_up) { - int baud, bits, stopbits, mctl; - enum flowcntrl_e flow; - enum parity_e parity; + int baud, bits, stopbits, mctl; + enum flowcntrl_e flow; + enum parity_e parity; - term_refresh(tty_fd); + term_refresh(tty_fd); - baud = term_get_baudrate(tty_fd, NULL); - flow = term_get_flowcntrl(tty_fd); - parity = term_get_parity(tty_fd); - bits = term_get_databits(tty_fd); - stopbits = term_get_stopbits(tty_fd); - - fd_printf(STO, "\r\n"); - - if ( baud != opts.baud ) { - fd_printf(STO, "*** baud: %d (%d)\r\n", opts.baud, baud); - } else { - fd_printf(STO, "*** baud: %d\r\n", opts.baud); - } - if ( flow != opts.flow ) { - fd_printf(STO, "*** flow: %s (%s)\r\n", - flow_str[opts.flow], flow_str[flow]); - } else { - fd_printf(STO, "*** flow: %s\r\n", flow_str[opts.flow]); - } - if ( parity != opts.parity ) { - fd_printf(STO, "*** parity: %s (%s)\r\n", - parity_str[opts.parity], parity_str[parity]); - } else { - fd_printf(STO, "*** parity: %s\r\n", parity_str[opts.parity]); - } - if ( bits != opts.databits ) { - fd_printf(STO, "*** databits: %d (%d)\r\n", opts.databits, bits); - } else { - fd_printf(STO, "*** databits: %d\r\n", opts.databits); - } - if ( stopbits != opts.stopbits ) { - fd_printf(STO, "*** stopbits: %d (%d)\r\n", opts.stopbits, stopbits); - } else { - fd_printf(STO, "*** stopbits: %d\r\n", opts.stopbits); - } + baud = term_get_baudrate(tty_fd, NULL); + flow = term_get_flowcntrl(tty_fd); + parity = term_get_parity(tty_fd); + bits = term_get_databits(tty_fd); + stopbits = term_get_stopbits(tty_fd); - mctl = term_get_mctl(tty_fd); - if (mctl >= 0 && mctl != MCTL_UNAVAIL) { - if ( ((mctl & MCTL_DTR) ? 1 : 0) == dtr_up ) - fd_printf(STO, "*** dtr: %s\r\n", dtr_up ? "up" : "down"); - else - fd_printf(STO, "*** dtr: %s (%s)\r\n", - dtr_up ? "up" : "down", - (mctl & MCTL_DTR) ? "up" : "down"); - if ( ((mctl & MCTL_RTS) ? 1 : 0) == rts_up ) - fd_printf(STO, "*** rts: %s\r\n", rts_up ? "up" : "down"); - else - fd_printf(STO, "*** rts: %s (%s)\r\n", - rts_up ? "up" : "down", - (mctl & MCTL_RTS) ? "up" : "down"); - fd_printf(STO, "*** mctl: "); - fd_printf(STO, "DTR:%c DSR:%c DCD:%c RTS:%c CTS:%c RI:%c\r\n", - (mctl & MCTL_DTR) ? '1' : '0', - (mctl & MCTL_DSR) ? '1' : '0', - (mctl & MCTL_DCD) ? '1' : '0', - (mctl & MCTL_RTS) ? '1' : '0', - (mctl & MCTL_CTS) ? '1' : '0', - (mctl & MCTL_RI) ? '1' : '0'); - } else { - fd_printf(STO, "*** dtr: %s\r\n", dtr_up ? "up" : "down"); - fd_printf(STO, "*** rts: %s\r\n", rts_up ? "up" : "down"); - } + fd_printf(STO, "\r\n"); + + if ( baud != opts.baud ) { + fd_printf(STO, "*** baud: %d (%d)\r\n", opts.baud, baud); + } else { + fd_printf(STO, "*** baud: %d\r\n", opts.baud); + } + if ( flow != opts.flow ) { + fd_printf(STO, "*** flow: %s (%s)\r\n", + flow_str[opts.flow], flow_str[flow]); + } else { + fd_printf(STO, "*** flow: %s\r\n", flow_str[opts.flow]); + } + if ( parity != opts.parity ) { + fd_printf(STO, "*** parity: %s (%s)\r\n", + parity_str[opts.parity], parity_str[parity]); + } else { + fd_printf(STO, "*** parity: %s\r\n", parity_str[opts.parity]); + } + if ( bits != opts.databits ) { + fd_printf(STO, "*** databits: %d (%d)\r\n", opts.databits, bits); + } else { + fd_printf(STO, "*** databits: %d\r\n", opts.databits); + } + if ( stopbits != opts.stopbits ) { + fd_printf(STO, "*** stopbits: %d (%d)\r\n", opts.stopbits, stopbits); + } else { + fd_printf(STO, "*** stopbits: %d\r\n", opts.stopbits); + } + + mctl = term_get_mctl(tty_fd); + if (mctl >= 0 && mctl != MCTL_UNAVAIL) { + if ( ((mctl & MCTL_DTR) ? 1 : 0) == dtr_up ) + fd_printf(STO, "*** dtr: %s\r\n", dtr_up ? "up" : "down"); + else + fd_printf(STO, "*** dtr: %s (%s)\r\n", + dtr_up ? "up" : "down", + (mctl & MCTL_DTR) ? "up" : "down"); + if ( ((mctl & MCTL_RTS) ? 1 : 0) == rts_up ) + fd_printf(STO, "*** rts: %s\r\n", rts_up ? "up" : "down"); + else + fd_printf(STO, "*** rts: %s (%s)\r\n", + rts_up ? "up" : "down", + (mctl & MCTL_RTS) ? "up" : "down"); + fd_printf(STO, "*** mctl: "); + fd_printf(STO, "DTR:%c DSR:%c DCD:%c RTS:%c CTS:%c RI:%c\r\n", + (mctl & MCTL_DTR) ? '1' : '0', + (mctl & MCTL_DSR) ? '1' : '0', + (mctl & MCTL_DCD) ? '1' : '0', + (mctl & MCTL_RTS) ? '1' : '0', + (mctl & MCTL_CTS) ? '1' : '0', + (mctl & MCTL_RI) ? '1' : '0'); + } else { + fd_printf(STO, "*** dtr: %s\r\n", dtr_up ? "up" : "down"); + fd_printf(STO, "*** rts: %s\r\n", rts_up ? "up" : "down"); + } } void show_keys() { #ifndef NO_HELP - fd_printf(STO, "\r\n"); - fd_printf(STO, "*** Picocom commands (all prefixed by [C-%c])\r\n", - KEYC(opts.escape)); - fd_printf(STO, "\r\n"); - fd_printf(STO, "*** [C-%c] : Exit picocom\r\n", - KEYC(KEY_EXIT)); - fd_printf(STO, "*** [C-%c] : Exit without reseting serial port\r\n", - KEYC(KEY_QUIT)); - fd_printf(STO, "*** [C-%c] : Set baudrate\r\n", - KEYC(KEY_BAUD)); - fd_printf(STO, "*** [C-%c] : Increase baudrate (baud-up)\r\n", - KEYC(KEY_BAUD_UP)); - fd_printf(STO, "*** [C-%c] : Decrease baudrate (baud-down)\r\n", - KEYC(KEY_BAUD_DN));; - fd_printf(STO, "*** [C-%c] : Change number of databits\r\n", - KEYC(KEY_BITS)); - fd_printf(STO, "*** [C-%c] : Change number of stopbits\r\n", - KEYC(KEY_STOP)); - fd_printf(STO, "*** [C-%c] : Change flow-control mode\r\n", - KEYC(KEY_FLOW)); - fd_printf(STO, "*** [C-%c] : Change parity mode\r\n", - KEYC(KEY_PARITY)); - fd_printf(STO, "*** [C-%c] : Pulse DTR\r\n", - KEYC(KEY_PULSE)); - fd_printf(STO, "*** [C-%c] : Toggle DTR\r\n", - KEYC(KEY_TOG_DTR)); - fd_printf(STO, "*** [C-%c] : Toggle RTS\r\n", - KEYC(KEY_TOG_RTS)); - fd_printf(STO, "*** [C-%c] : Send break\r\n", - KEYC(KEY_BREAK)); - fd_printf(STO, "*** [C-%c] : Toggle local echo\r\n", - KEYC(KEY_LECHO)); - fd_printf(STO, "*** [C-%c] : Send file\r\n", - KEYC(KEY_SEND)); - fd_printf(STO, "*** [C-%c] : Receive file\r\n", - KEYC(KEY_RECEIVE)); - fd_printf(STO, "*** [C-%c] : Show port settings\r\n", - KEYC(KEY_STATUS)); - fd_printf(STO, "*** [C-%c] : Show this message\r\n", - KEYC(KEY_HELP)); - fd_printf(STO, "\r\n"); + fd_printf(STO, "\r\n"); + fd_printf(STO, "*** Picocom commands (all prefixed by [C-%c])\r\n", + KEYC(opts.escape)); + fd_printf(STO, "\r\n"); + fd_printf(STO, "*** [C-%c] : Exit picocom\r\n", + KEYC(KEY_EXIT)); + fd_printf(STO, "*** [C-%c] : Exit without reseting serial port\r\n", + KEYC(KEY_QUIT)); + fd_printf(STO, "*** [C-%c] : Set baudrate\r\n", + KEYC(KEY_BAUD)); + fd_printf(STO, "*** [C-%c] : Increase baudrate (baud-up)\r\n", + KEYC(KEY_BAUD_UP)); + fd_printf(STO, "*** [C-%c] : Decrease baudrate (baud-down)\r\n", + KEYC(KEY_BAUD_DN));; + fd_printf(STO, "*** [C-%c] : Change number of databits\r\n", + KEYC(KEY_BITS)); + fd_printf(STO, "*** [C-%c] : Change number of stopbits\r\n", + KEYC(KEY_STOP)); + fd_printf(STO, "*** [C-%c] : Change flow-control mode\r\n", + KEYC(KEY_FLOW)); + fd_printf(STO, "*** [C-%c] : Change parity mode\r\n", + KEYC(KEY_PARITY)); + fd_printf(STO, "*** [C-%c] : Pulse DTR\r\n", + KEYC(KEY_PULSE)); + fd_printf(STO, "*** [C-%c] : Toggle DTR\r\n", + KEYC(KEY_TOG_DTR)); + fd_printf(STO, "*** [C-%c] : Toggle RTS\r\n", + KEYC(KEY_TOG_RTS)); + fd_printf(STO, "*** [C-%c] : Send break\r\n", + KEYC(KEY_BREAK)); + fd_printf(STO, "*** [C-%c] : Toggle local echo\r\n", + KEYC(KEY_LECHO)); + fd_printf(STO, "*** [C-%c] : Send file\r\n", + KEYC(KEY_SEND)); + fd_printf(STO, "*** [C-%c] : Receive file\r\n", + KEYC(KEY_RECEIVE)); + fd_printf(STO, "*** [C-%c] : Show port settings\r\n", + KEYC(KEY_STATUS)); + fd_printf(STO, "*** [C-%c] : Show this message\r\n", + KEYC(KEY_HELP)); + fd_printf(STO, "\r\n"); #else /* defined NO_HELP */ - fd_printf(STO, "*** Help is disabled.\r\n"); + fd_printf(STO, "*** Help is disabled.\r\n"); #endif /* of NO_HELP */ } @@ -813,106 +813,106 @@ show_keys() void establish_child_signal_handlers (void) { - struct sigaction dfl_action; + struct sigaction dfl_action; - /* Set up the structure to specify the default action. */ - dfl_action.sa_handler = SIG_DFL; - sigemptyset (&dfl_action.sa_mask); - dfl_action.sa_flags = 0; - - sigaction (SIGINT, &dfl_action, NULL); - sigaction (SIGTERM, &dfl_action, NULL); + /* Set up the structure to specify the default action. */ + dfl_action.sa_handler = SIG_DFL; + sigemptyset (&dfl_action.sa_mask); + dfl_action.sa_flags = 0; + + sigaction (SIGINT, &dfl_action, NULL); + sigaction (SIGTERM, &dfl_action, NULL); } int run_cmd(int fd, const char *cmd, const char *args_extra) { - pid_t pid; - sigset_t sigm, sigm_old; + pid_t pid; + sigset_t sigm, sigm_old; - /* block signals, let child establish its own handlers */ - sigemptyset(&sigm); - sigaddset(&sigm, SIGTERM); - sigprocmask(SIG_BLOCK, &sigm, &sigm_old); + /* block signals, let child establish its own handlers */ + sigemptyset(&sigm); + sigaddset(&sigm, SIGTERM); + sigprocmask(SIG_BLOCK, &sigm, &sigm_old); - pid = fork(); - if ( pid < 0 ) { - sigprocmask(SIG_SETMASK, &sigm_old, NULL); - fd_printf(STO, "*** cannot fork: %s ***\r\n", strerror(errno)); - return -1; - } else if ( pid ) { - /* father: picocom */ - int status, r; + pid = fork(); + if ( pid < 0 ) { + sigprocmask(SIG_SETMASK, &sigm_old, NULL); + fd_printf(STO, "*** cannot fork: %s ***\r\n", strerror(errno)); + return -1; + } else if ( pid ) { + /* father: picocom */ + int status, r; - /* reset the mask */ - sigprocmask(SIG_SETMASK, &sigm_old, NULL); - /* wait for child to finish */ - do { - r = waitpid(pid, &status, 0); - } while ( r < 0 && errno == EINTR ); - /* reset terminal (back to raw mode) */ - term_apply(STI, 0); - /* check and report child return status */ - if ( WIFEXITED(status) ) { - fd_printf(STO, "\r\n*** exit status: %d ***\r\n", - WEXITSTATUS(status)); - return WEXITSTATUS(status); - } else if ( WIFSIGNALED(status) ) { - fd_printf(STO, "\r\n*** killed by signal: %d ***\r\n", - WTERMSIG(status)); - return -1; - } else { - fd_printf(STO, "\r\n*** abnormal termination: 0x%x ***\r\n", r); - return -1; - } - } else { - /* child: external program */ - long fl; - int argc; - char *argv[RUNCMD_ARGS_MAX + 1]; - int r; - - /* unmanage terminal, and reset it to canonical mode */ - term_remove(STI); - /* unmanage serial port fd, without reset */ - term_erase(fd); - /* set serial port fd to blocking mode */ - fl = fcntl(fd, F_GETFL); - fl &= ~O_NONBLOCK; - fcntl(fd, F_SETFL, fl); - /* connect stdin and stdout to serial port */ - close(STI); - close(STO); - dup2(fd, STI); - dup2(fd, STO); - - /* build command arguments vector */ - argc = 0; - r = split_quoted(cmd, &argc, argv, RUNCMD_ARGS_MAX); - if ( r < 0 ) { - fd_printf(STDERR_FILENO, "Cannot parse command\n"); - exit(RUNCMD_EXEC_FAIL); - } - r = split_quoted(args_extra, &argc, argv, RUNCMD_ARGS_MAX); - if ( r < 0 ) { - fd_printf(STDERR_FILENO, "Cannot parse extra args\n"); - exit(RUNCMD_EXEC_FAIL); - } - if ( argc < 1 ) { - fd_printf(STDERR_FILENO, "No command given\n"); - exit(RUNCMD_EXEC_FAIL); - } - argv[argc] = NULL; - - /* run extenral command */ - fd_printf(STDERR_FILENO, "$ %s %s\n", cmd, args_extra); - establish_child_signal_handlers(); - sigprocmask(SIG_SETMASK, &sigm_old, NULL); - execvp(argv[0], argv); + /* reset the mask */ + sigprocmask(SIG_SETMASK, &sigm_old, NULL); + /* wait for child to finish */ + do { + r = waitpid(pid, &status, 0); + } while ( r < 0 && errno == EINTR ); + /* reset terminal (back to raw mode) */ + term_apply(STI, 0); + /* check and report child return status */ + if ( WIFEXITED(status) ) { + fd_printf(STO, "\r\n*** exit status: %d ***\r\n", + WEXITSTATUS(status)); + return WEXITSTATUS(status); + } else if ( WIFSIGNALED(status) ) { + fd_printf(STO, "\r\n*** killed by signal: %d ***\r\n", + WTERMSIG(status)); + return -1; + } else { + fd_printf(STO, "\r\n*** abnormal termination: 0x%x ***\r\n", r); + return -1; + } + } else { + /* child: external program */ + long fl; + int argc; + char *argv[RUNCMD_ARGS_MAX + 1]; + int r; - fd_printf(STDERR_FILENO, "exec: %s\n", strerror(errno)); - exit(RUNCMD_EXEC_FAIL); - } + /* unmanage terminal, and reset it to canonical mode */ + term_remove(STI); + /* unmanage serial port fd, without reset */ + term_erase(fd); + /* set serial port fd to blocking mode */ + fl = fcntl(fd, F_GETFL); + fl &= ~O_NONBLOCK; + fcntl(fd, F_SETFL, fl); + /* connect stdin and stdout to serial port */ + close(STI); + close(STO); + dup2(fd, STI); + dup2(fd, STO); + + /* build command arguments vector */ + argc = 0; + r = split_quoted(cmd, &argc, argv, RUNCMD_ARGS_MAX); + if ( r < 0 ) { + fd_printf(STDERR_FILENO, "Cannot parse command\n"); + exit(RUNCMD_EXEC_FAIL); + } + r = split_quoted(args_extra, &argc, argv, RUNCMD_ARGS_MAX); + if ( r < 0 ) { + fd_printf(STDERR_FILENO, "Cannot parse extra args\n"); + exit(RUNCMD_EXEC_FAIL); + } + if ( argc < 1 ) { + fd_printf(STDERR_FILENO, "No command given\n"); + exit(RUNCMD_EXEC_FAIL); + } + argv[argc] = NULL; + + /* run extenral command */ + fd_printf(STDERR_FILENO, "$ %s %s\n", cmd, args_extra); + establish_child_signal_handlers(); + sigprocmask(SIG_SETMASK, &sigm_old, NULL); + execvp(argv[0], argv); + + fd_printf(STDERR_FILENO, "exec: %s\n", strerror(errno)); + exit(RUNCMD_EXEC_FAIL); + } } /**********************************************************************/ @@ -922,165 +922,165 @@ run_cmd(int fd, const char *cmd, const char *args_extra) int do_command (unsigned char c) { - static int dtr_up = 0; - static int rts_up = 0; - int newbaud, newflow, newparity, newbits, newstopbits; - const char *xfr_cmd; - char *fname; - int r; + static int dtr_up = 0; + static int rts_up = 0; + int newbaud, newflow, newparity, newbits, newstopbits; + const char *xfr_cmd; + char *fname; + int r; - switch (c) { - case KEY_EXIT: - return 1; - case KEY_QUIT: - term_set_hupcl(tty_fd, 0); - term_flush(tty_fd); - term_apply(tty_fd, 1); - term_erase(tty_fd); - return 1; - case KEY_STATUS: - show_status(dtr_up, rts_up); - break; - case KEY_HELP: - case KEY_KEYS: - show_keys(); - break; - case KEY_PULSE: - fd_printf(STO, "\r\n*** pulse DTR ***\r\n"); - if ( term_pulse_dtr(tty_fd) < 0 ) - fd_printf(STO, "*** FAILED\r\n"); - break; - case KEY_TOG_DTR: - if ( dtr_up ) - r = term_lower_dtr(tty_fd); - else - r = term_raise_dtr(tty_fd); - if ( r >= 0 ) dtr_up = ! dtr_up; - fd_printf(STO, "\r\n*** DTR: %s ***\r\n", - dtr_up ? "up" : "down"); - break; - case KEY_TOG_RTS: - if ( rts_up ) - r = term_lower_rts(tty_fd); - else - r = term_raise_rts(tty_fd); - if ( r >= 0 ) rts_up = ! rts_up; - fd_printf(STO, "\r\n*** RTS: %s ***\r\n", - rts_up ? "up" : "down"); - break; - case KEY_BAUD: - case KEY_BAUD_UP: - case KEY_BAUD_DN: - if ( c== KEY_BAUD) { - newbaud = read_baud(); - if ( newbaud < 0 ) { - fd_printf(STO, "*** cannot read baudrate ***\r\n"); - break; - } - opts.baud = newbaud; - } else if (c == KEY_BAUD_UP) { - opts.baud = baud_up(opts.baud); - } else { - opts.baud = baud_down(opts.baud); - } - term_set_baudrate(tty_fd, opts.baud); - tty_q.len = 0; term_flush(tty_fd); - term_apply(tty_fd, 1); - newbaud = term_get_baudrate(tty_fd, NULL); - if ( opts.baud != newbaud ) { - fd_printf(STO, "\r\n*** baud: %d (%d) ***\r\n", - opts.baud, newbaud); - } else { - fd_printf(STO, "\r\n*** baud: %d ***\r\n", opts.baud); - } - set_tty_write_sz(newbaud); - break; - case KEY_FLOW: - opts.flow = flow_next(opts.flow); - term_set_flowcntrl(tty_fd, opts.flow); - tty_q.len = 0; term_flush(tty_fd); - term_apply(tty_fd, 1); - newflow = term_get_flowcntrl(tty_fd); - if ( opts.flow != newflow ) { - fd_printf(STO, "\r\n*** flow: %s (%s) ***\r\n", - flow_str[opts.flow], flow_str[newflow]); - } else { - fd_printf(STO, "\r\n*** flow: %s ***\r\n", - flow_str[opts.flow]); - } - break; - case KEY_PARITY: - opts.parity = parity_next(opts.parity); - term_set_parity(tty_fd, opts.parity); - tty_q.len = 0; term_flush(tty_fd); - term_apply(tty_fd, 1); - newparity = term_get_parity(tty_fd); - if (opts.parity != newparity ) { - fd_printf(STO, "\r\n*** parity: %s (%s) ***\r\n", - parity_str[opts.parity], - parity_str[newparity]); - } else { - fd_printf(STO, "\r\n*** parity: %s ***\r\n", - parity_str[opts.parity]); - } - break; - case KEY_BITS: - opts.databits = bits_next(opts.databits); - term_set_databits(tty_fd, opts.databits); - tty_q.len = 0; term_flush(tty_fd); - term_apply(tty_fd, 1); - newbits = term_get_databits(tty_fd); - if (opts.databits != newbits ) { - fd_printf(STO, "\r\n*** databits: %d (%d) ***\r\n", - opts.databits, newbits); - } else { - fd_printf(STO, "\r\n*** databits: %d ***\r\n", - opts.databits); - } - break; - case KEY_STOP: - opts.stopbits = stopbits_next(opts.stopbits); - term_set_stopbits(tty_fd, opts.stopbits); - tty_q.len = 0; term_flush(tty_fd); - term_apply(tty_fd, 1); - newstopbits = term_get_stopbits(tty_fd); - if (opts.stopbits != newstopbits ) { - fd_printf(STO, "\r\n*** stopbits: %d (%d) ***\r\n", - opts.stopbits, newstopbits); - } else { - fd_printf(STO, "\r\n*** stopbits: %d ***\r\n", - opts.stopbits); - } - break; - case KEY_LECHO: - opts.lecho = ! opts.lecho; - fd_printf(STO, "\r\n*** local echo: %s ***\r\n", - opts.lecho ? "yes" : "no"); - break; - case KEY_SEND: - case KEY_RECEIVE: - xfr_cmd = (c == KEY_SEND) ? opts.send_cmd : opts.receive_cmd; - if ( xfr_cmd[0] == '\0' ) { - fd_printf(STO, "\r\n*** command disabled ***\r\n"); - break; - } - fname = read_filename(); - if (fname == NULL) { - fd_printf(STO, "*** cannot read filename ***\r\n"); - break; - } - run_cmd(tty_fd, xfr_cmd, fname); - free(fname); - break; - case KEY_BREAK: - term_break(tty_fd); - fd_printf(STO, "\r\n*** break sent ***\r\n"); - break; - default: - break; - } + switch (c) { + case KEY_EXIT: + return 1; + case KEY_QUIT: + term_set_hupcl(tty_fd, 0); + term_flush(tty_fd); + term_apply(tty_fd, 1); + term_erase(tty_fd); + return 1; + case KEY_STATUS: + show_status(dtr_up, rts_up); + break; + case KEY_HELP: + case KEY_KEYS: + show_keys(); + break; + case KEY_PULSE: + fd_printf(STO, "\r\n*** pulse DTR ***\r\n"); + if ( term_pulse_dtr(tty_fd) < 0 ) + fd_printf(STO, "*** FAILED\r\n"); + break; + case KEY_TOG_DTR: + if ( dtr_up ) + r = term_lower_dtr(tty_fd); + else + r = term_raise_dtr(tty_fd); + if ( r >= 0 ) dtr_up = ! dtr_up; + fd_printf(STO, "\r\n*** DTR: %s ***\r\n", + dtr_up ? "up" : "down"); + break; + case KEY_TOG_RTS: + if ( rts_up ) + r = term_lower_rts(tty_fd); + else + r = term_raise_rts(tty_fd); + if ( r >= 0 ) rts_up = ! rts_up; + fd_printf(STO, "\r\n*** RTS: %s ***\r\n", + rts_up ? "up" : "down"); + break; + case KEY_BAUD: + case KEY_BAUD_UP: + case KEY_BAUD_DN: + if ( c== KEY_BAUD) { + newbaud = read_baud(); + if ( newbaud < 0 ) { + fd_printf(STO, "*** cannot read baudrate ***\r\n"); + break; + } + opts.baud = newbaud; + } else if (c == KEY_BAUD_UP) { + opts.baud = baud_up(opts.baud); + } else { + opts.baud = baud_down(opts.baud); + } + term_set_baudrate(tty_fd, opts.baud); + tty_q.len = 0; term_flush(tty_fd); + term_apply(tty_fd, 1); + newbaud = term_get_baudrate(tty_fd, NULL); + if ( opts.baud != newbaud ) { + fd_printf(STO, "\r\n*** baud: %d (%d) ***\r\n", + opts.baud, newbaud); + } else { + fd_printf(STO, "\r\n*** baud: %d ***\r\n", opts.baud); + } + set_tty_write_sz(newbaud); + break; + case KEY_FLOW: + opts.flow = flow_next(opts.flow); + term_set_flowcntrl(tty_fd, opts.flow); + tty_q.len = 0; term_flush(tty_fd); + term_apply(tty_fd, 1); + newflow = term_get_flowcntrl(tty_fd); + if ( opts.flow != newflow ) { + fd_printf(STO, "\r\n*** flow: %s (%s) ***\r\n", + flow_str[opts.flow], flow_str[newflow]); + } else { + fd_printf(STO, "\r\n*** flow: %s ***\r\n", + flow_str[opts.flow]); + } + break; + case KEY_PARITY: + opts.parity = parity_next(opts.parity); + term_set_parity(tty_fd, opts.parity); + tty_q.len = 0; term_flush(tty_fd); + term_apply(tty_fd, 1); + newparity = term_get_parity(tty_fd); + if (opts.parity != newparity ) { + fd_printf(STO, "\r\n*** parity: %s (%s) ***\r\n", + parity_str[opts.parity], + parity_str[newparity]); + } else { + fd_printf(STO, "\r\n*** parity: %s ***\r\n", + parity_str[opts.parity]); + } + break; + case KEY_BITS: + opts.databits = bits_next(opts.databits); + term_set_databits(tty_fd, opts.databits); + tty_q.len = 0; term_flush(tty_fd); + term_apply(tty_fd, 1); + newbits = term_get_databits(tty_fd); + if (opts.databits != newbits ) { + fd_printf(STO, "\r\n*** databits: %d (%d) ***\r\n", + opts.databits, newbits); + } else { + fd_printf(STO, "\r\n*** databits: %d ***\r\n", + opts.databits); + } + break; + case KEY_STOP: + opts.stopbits = stopbits_next(opts.stopbits); + term_set_stopbits(tty_fd, opts.stopbits); + tty_q.len = 0; term_flush(tty_fd); + term_apply(tty_fd, 1); + newstopbits = term_get_stopbits(tty_fd); + if (opts.stopbits != newstopbits ) { + fd_printf(STO, "\r\n*** stopbits: %d (%d) ***\r\n", + opts.stopbits, newstopbits); + } else { + fd_printf(STO, "\r\n*** stopbits: %d ***\r\n", + opts.stopbits); + } + break; + case KEY_LECHO: + opts.lecho = ! opts.lecho; + fd_printf(STO, "\r\n*** local echo: %s ***\r\n", + opts.lecho ? "yes" : "no"); + break; + case KEY_SEND: + case KEY_RECEIVE: + xfr_cmd = (c == KEY_SEND) ? opts.send_cmd : opts.receive_cmd; + if ( xfr_cmd[0] == '\0' ) { + fd_printf(STO, "\r\n*** command disabled ***\r\n"); + break; + } + fname = read_filename(); + if (fname == NULL) { + fd_printf(STO, "*** cannot read filename ***\r\n"); + break; + } + run_cmd(tty_fd, xfr_cmd, fname); + free(fname); + break; + case KEY_BREAK: + term_break(tty_fd); + fd_printf(STO, "\r\n*** break sent ***\r\n"); + break; + default: + break; + } - return 0; + return 0; } /**********************************************************************/ @@ -1088,139 +1088,139 @@ do_command (unsigned char c) void loop(void) { - enum { - ST_COMMAND, - ST_TRANSPARENT - } state; - fd_set rdset, wrset; - int r, n; - unsigned char c; + enum { + ST_COMMAND, + ST_TRANSPARENT + } state; + fd_set rdset, wrset; + int r, n; + unsigned char c; - tty_q.len = 0; - state = ST_TRANSPARENT; + tty_q.len = 0; + state = ST_TRANSPARENT; - while ( ! sig_exit ) { - FD_ZERO(&rdset); - FD_ZERO(&wrset); - FD_SET(STI, &rdset); - FD_SET(tty_fd, &rdset); - if ( tty_q.len ) FD_SET(tty_fd, &wrset); + while ( ! sig_exit ) { + FD_ZERO(&rdset); + FD_ZERO(&wrset); + FD_SET(STI, &rdset); + FD_SET(tty_fd, &rdset); + if ( tty_q.len ) FD_SET(tty_fd, &wrset); - r = select(tty_fd + 1, &rdset, &wrset, NULL, NULL); - if ( r < 0 ) { - if ( errno == EINTR ) - continue; - else - fatal("select failed: %d : %s", errno, strerror(errno)); - } + r = select(tty_fd + 1, &rdset, &wrset, NULL, NULL); + if ( r < 0 ) { + if ( errno == EINTR ) + continue; + else + fatal("select failed: %d : %s", errno, strerror(errno)); + } - if ( FD_ISSET(STI, &rdset) ) { + if ( FD_ISSET(STI, &rdset) ) { - /* read from terminal */ + /* read from terminal */ - do { - n = read(STI, &c, 1); - } while (n < 0 && errno == EINTR); - if (n == 0) { - fatal("stdin closed"); - } else if (n < 0) { - /* is this really necessary? better safe than sory! */ - if ( errno != EAGAIN && errno != EWOULDBLOCK ) - fatal("read from stdin failed: %s", strerror(errno)); - else - goto skip_proc_STI; - } + do { + n = read(STI, &c, 1); + } while (n < 0 && errno == EINTR); + if (n == 0) { + fatal("stdin closed"); + } else if (n < 0) { + /* is this really necessary? better safe than sory! */ + if ( errno != EAGAIN && errno != EWOULDBLOCK ) + fatal("read from stdin failed: %s", strerror(errno)); + else + goto skip_proc_STI; + } - switch (state) { - case ST_COMMAND: - if ( c == opts.escape ) { - /* pass the escape character down */ - if (tty_q.len + M_MAXMAP <= TTY_Q_SZ) { - n = do_map((char *)tty_q.buff + tty_q.len, - opts.omap, c); - tty_q.len += n; - if ( opts.lecho ) - map_and_write(STO, opts.emap, c); - } else { - fd_printf(STO, "\x07"); - } - } else { - /* process command key */ - if ( do_command(c) ) - /* picocom exit */ - return; - } - state = ST_TRANSPARENT; - break; - case ST_TRANSPARENT: - if ( c == opts.escape ) { - state = ST_COMMAND; - } else { - if (tty_q.len + M_MAXMAP <= TTY_Q_SZ) { - n = do_map((char *)tty_q.buff + tty_q.len, - opts.omap, c); - tty_q.len += n; - if ( opts.lecho ) - map_and_write(STO, opts.emap, c); - } else - fd_printf(STO, "\x07"); - } - break; - default: - assert(0); - break; - } - } - skip_proc_STI: + switch (state) { + case ST_COMMAND: + if ( c == opts.escape ) { + /* pass the escape character down */ + if (tty_q.len + M_MAXMAP <= TTY_Q_SZ) { + n = do_map((char *)tty_q.buff + tty_q.len, + opts.omap, c); + tty_q.len += n; + if ( opts.lecho ) + map_and_write(STO, opts.emap, c); + } else { + fd_printf(STO, "\x07"); + } + } else { + /* process command key */ + if ( do_command(c) ) + /* picocom exit */ + return; + } + state = ST_TRANSPARENT; + break; + case ST_TRANSPARENT: + if ( c == opts.escape ) { + state = ST_COMMAND; + } else { + if (tty_q.len + M_MAXMAP <= TTY_Q_SZ) { + n = do_map((char *)tty_q.buff + tty_q.len, + opts.omap, c); + tty_q.len += n; + if ( opts.lecho ) + map_and_write(STO, opts.emap, c); + } else + fd_printf(STO, "\x07"); + } + break; + default: + assert(0); + break; + } + } + skip_proc_STI: - if ( FD_ISSET(tty_fd, &rdset) ) { + if ( FD_ISSET(tty_fd, &rdset) ) { - char buff_rd[TTY_RD_SZ]; - char buff_map[TTY_RD_SZ * M_MAXMAP]; + char buff_rd[TTY_RD_SZ]; + char buff_map[TTY_RD_SZ * M_MAXMAP]; - /* read from port */ + /* read from port */ - do { - n = read(tty_fd, &buff_rd, sizeof(buff_rd)); - } while (n < 0 && errno == EINTR); - if (n == 0) { - fatal("term closed"); - } else if ( n < 0 ) { - if ( errno != EAGAIN && errno != EWOULDBLOCK ) - fatal("read from term failed: %s", strerror(errno)); - } else { - int i; - char *bmp = &buff_map[0]; - if ( opts.log_filename ) - if ( writen_ni(log_fd, buff_rd, n) < n ) - fatal("write to logfile failed: %s", strerror(errno)); - for (i = 0; i < n; i++) { - bmp += do_map(bmp, opts.imap, buff_rd[i]); - } - n = bmp - buff_map; - if ( writen_ni(STO, buff_map, n) < n ) - fatal("write to stdout failed: %s", strerror(errno)); - } - } + do { + n = read(tty_fd, &buff_rd, sizeof(buff_rd)); + } while (n < 0 && errno == EINTR); + if (n == 0) { + fatal("term closed"); + } else if ( n < 0 ) { + if ( errno != EAGAIN && errno != EWOULDBLOCK ) + fatal("read from term failed: %s", strerror(errno)); + } else { + int i; + char *bmp = &buff_map[0]; + if ( opts.log_filename ) + if ( writen_ni(log_fd, buff_rd, n) < n ) + fatal("write to logfile failed: %s", strerror(errno)); + for (i = 0; i < n; i++) { + bmp += do_map(bmp, opts.imap, buff_rd[i]); + } + n = bmp - buff_map; + if ( writen_ni(STO, buff_map, n) < n ) + fatal("write to stdout failed: %s", strerror(errno)); + } + } - if ( FD_ISSET(tty_fd, &wrset) ) { + if ( FD_ISSET(tty_fd, &wrset) ) { - /* write to port */ + /* write to port */ - int sz; - sz = (tty_q.len < tty_write_sz) ? tty_q.len : tty_write_sz; - do { - n = write(tty_fd, tty_q.buff, sz); - } while ( n < 0 && errno == EINTR ); - if ( n <= 0 ) - fatal("write to term failed: %s", strerror(errno)); - if ( opts.lecho && opts.log_filename ) - if ( writen_ni(log_fd, tty_q.buff, n) < n ) - fatal("write to logfile failed: %s", strerror(errno)); - memmove(tty_q.buff, tty_q.buff + n, tty_q.len - n); - tty_q.len -= n; - } - } + int sz; + sz = (tty_q.len < tty_write_sz) ? tty_q.len : tty_write_sz; + do { + n = write(tty_fd, tty_q.buff, sz); + } while ( n < 0 && errno == EINTR ); + if ( n <= 0 ) + fatal("write to term failed: %s", strerror(errno)); + if ( opts.lecho && opts.log_filename ) + if ( writen_ni(log_fd, tty_q.buff, n) < n ) + fatal("write to logfile failed: %s", strerror(errno)); + memmove(tty_q.buff, tty_q.buff + n, tty_q.len - n); + tty_q.len -= n; + } + } } /**********************************************************************/ @@ -1228,10 +1228,10 @@ loop(void) void deadly_handler(int signum) { - if ( ! sig_exit ) { - sig_exit = 1; - kill(0, SIGTERM); - } + if ( ! sig_exit ) { + sig_exit = 1; + kill(0, SIGTERM); + } } void @@ -1251,9 +1251,9 @@ establish_signal_handlers (void) sigaction (SIGTERM, &exit_action, NULL); - sigaction (SIGINT, &ign_action, NULL); + sigaction (SIGINT, &ign_action, NULL); sigaction (SIGHUP, &ign_action, NULL); - sigaction (SIGQUIT, &ign_action, NULL); + sigaction (SIGQUIT, &ign_action, NULL); sigaction (SIGALRM, &ign_action, NULL); sigaction (SIGUSR1, &ign_action, NULL); sigaction (SIGUSR2, &ign_action, NULL); @@ -1266,66 +1266,66 @@ void show_usage(char *name) { #ifndef NO_HELP - char *s; + char *s; - s = strrchr(name, '/'); - s = s ? s+1 : name; + s = strrchr(name, '/'); + s = s ? s+1 : name; - printf("picocom v%s\n", VERSION_STR); + printf("picocom v%s\n", VERSION_STR); - printf("\nCompiled-in options:\n"); - printf(" TTY_Q_SZ is %d\n", TTY_Q_SZ); + printf("\nCompiled-in options:\n"); + printf(" TTY_Q_SZ is %d\n", TTY_Q_SZ); #ifdef HIGH_BAUD - printf(" HIGH_BAUD is enabled\n"); + printf(" HIGH_BAUD is enabled\n"); #endif #ifdef USE_FLOCK - printf(" USE_FLOCK is enabled\n"); + printf(" USE_FLOCK is enabled\n"); #endif #ifdef UUCP_LOCK_DIR - printf(" UUCP_LOCK_DIR is: %s\n", UUCP_LOCK_DIR); + printf(" UUCP_LOCK_DIR is: %s\n", UUCP_LOCK_DIR); #endif #ifdef LINENOISE - printf(" LINENOISE is enabled\n"); - printf(" HISTFILE is: %s\n", HISTFILE); + printf(" LINENOISE is enabled\n"); + printf(" HISTFILE is: %s\n", HISTFILE); #endif #ifdef USE_CUSTOM_BAUD - printf(" USE_CUSTOM_BAUD is enabled\n"); + printf(" USE_CUSTOM_BAUD is enabled\n"); #endif - - printf("\nUsage is: %s [options] \n", s); - printf("Options are:\n"); - printf(" --aud \n"); - printf(" --low x (=soft,xon/xoff) | h (=hard) | n (=none)\n"); - printf(" --parit o (=odd) | e (=even) | n (=none)\n"); - printf(" --atabits 5 | 6 | 7 | 8\n"); - printf(" --sto

bits 1 | 2\n"); - printf(" --scape \n"); - printf(" --eho\n"); - printf(" --nonit\n"); - printf(" --noeset\n"); - printf(" --noock\n"); - printf(" --end-cmd \n"); - printf(" --receie-cmd \n"); - printf(" --imap (input mappings)\n"); - printf(" --omap (output mappings)\n"); - printf(" --emap (local-echo mappings)\n"); - printf(" --lofile \n"); - printf(" --lower-rts\n"); - printf(" --lower-dtr\n"); - printf(" --elp\n"); - printf(" is a comma-separated list of one or more of:\n"); - printf(" crlf : map CR --> LF\n"); - printf(" crcrlf : map CR --> CR + LF\n"); - printf(" igncr : ignore CR\n"); - printf(" lfcr : map LF --> CR\n"); - printf(" lfcrlf : map LF --> CR + LF\n"); - printf(" ignlf : ignore LF\n"); - printf(" bsdel : map BS --> DEL\n"); - printf(" delbs : map DEL --> BS\n"); - printf(" indicates the equivalent short option.\n"); - printf("Short options are prefixed by \"-\" instead of by \"--\".\n"); + + printf("\nUsage is: %s [options] \n", s); + printf("Options are:\n"); + printf(" --aud \n"); + printf(" --low x (=soft,xon/xoff) | h (=hard) | n (=none)\n"); + printf(" --parit o (=odd) | e (=even) | n (=none)\n"); + printf(" --atabits 5 | 6 | 7 | 8\n"); + printf(" --sto

bits 1 | 2\n"); + printf(" --scape \n"); + printf(" --eho\n"); + printf(" --nonit\n"); + printf(" --noeset\n"); + printf(" --noock\n"); + printf(" --end-cmd \n"); + printf(" --receie-cmd \n"); + printf(" --imap (input mappings)\n"); + printf(" --omap (output mappings)\n"); + printf(" --emap (local-echo mappings)\n"); + printf(" --lofile \n"); + printf(" --lower-rts\n"); + printf(" --lower-dtr\n"); + printf(" --elp\n"); + printf(" is a comma-separated list of one or more of:\n"); + printf(" crlf : map CR --> LF\n"); + printf(" crcrlf : map CR --> CR + LF\n"); + printf(" igncr : ignore CR\n"); + printf(" lfcr : map LF --> CR\n"); + printf(" lfcrlf : map LF --> CR + LF\n"); + printf(" ignlf : ignore LF\n"); + printf(" bsdel : map BS --> DEL\n"); + printf(" delbs : map DEL --> BS\n"); + printf(" indicates the equivalent short option.\n"); + printf("Short options are prefixed by \"-\" instead of by \"--\".\n"); #else /* defined NO_HELP */ - printf("Help disabled.\n"); + printf("Help disabled.\n"); #endif /* of NO_HELP */ } @@ -1334,234 +1334,234 @@ show_usage(char *name) void parse_args(int argc, char *argv[]) { - int r; + int r; - static struct option longOptions[] = - { - {"receive-cmd", required_argument, 0, 'v'}, - {"send-cmd", required_argument, 0, 's'}, + static struct option longOptions[] = + { + {"receive-cmd", required_argument, 0, 'v'}, + {"send-cmd", required_argument, 0, 's'}, {"imap", required_argument, 0, 'I' }, {"omap", required_argument, 0, 'O' }, {"emap", required_argument, 0, 'E' }, - {"escape", required_argument, 0, 'e'}, - {"echo", no_argument, 0, 'c'}, - {"noinit", no_argument, 0, 'i'}, - {"noreset", no_argument, 0, 'r'}, - {"nolock", no_argument, 0, 'l'}, - {"flow", required_argument, 0, 'f'}, - {"baud", required_argument, 0, 'b'}, - {"parity", required_argument, 0, 'y'}, - {"databits", required_argument, 0, 'd'}, - {"stopbits", required_argument, 0, 'p'}, - {"logfile", required_argument, 0, 'g'}, - {"lower-rts", no_argument, 0, 'R'}, - {"lower-dtr", no_argument, 0, 'D'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0} - }; + {"escape", required_argument, 0, 'e'}, + {"echo", no_argument, 0, 'c'}, + {"noinit", no_argument, 0, 'i'}, + {"noreset", no_argument, 0, 'r'}, + {"nolock", no_argument, 0, 'l'}, + {"flow", required_argument, 0, 'f'}, + {"baud", required_argument, 0, 'b'}, + {"parity", required_argument, 0, 'y'}, + {"databits", required_argument, 0, 'd'}, + {"stopbits", required_argument, 0, 'p'}, + {"logfile", required_argument, 0, 'g'}, + {"lower-rts", no_argument, 0, 'R'}, + {"lower-dtr", no_argument, 0, 'D'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0} + }; - r = 0; - while (1) { - int optionIndex = 0; - int c; - int map; + r = 0; + while (1) { + int optionIndex = 0; + int c; + int map; - /* no default error messages printed. */ - opterr = 0; + /* no default error messages printed. */ + opterr = 0; - c = getopt_long(argc, argv, "hirlcv:s:r:e:f:b:y:d:p:g:", - longOptions, &optionIndex); + c = getopt_long(argc, argv, "hirlcv:s:r:e:f:b:y:d:p:g:", + longOptions, &optionIndex); - if (c < 0) - break; + if (c < 0) + break; - switch (c) { - case 's': - strncpy(opts.send_cmd, optarg, sizeof(opts.send_cmd)); - opts.send_cmd[sizeof(opts.send_cmd) - 1] = '\0'; - break; - case 'v': - strncpy(opts.receive_cmd, optarg, sizeof(opts.receive_cmd)); - opts.receive_cmd[sizeof(opts.receive_cmd) - 1] = '\0'; - break; - case 'I': - map = parse_map(optarg); - if (map >= 0) opts.imap = map; - else { fprintf(stderr, "Invalid --imap\n"); r = -1; } - break; - case 'O': - map = parse_map(optarg); - if (map >= 0) opts.omap = map; - else { fprintf(stderr, "Invalid --omap\n"); r = -1; } - break; - case 'E': - map = parse_map(optarg); - if (map >= 0) opts.emap = map; - else { fprintf(stderr, "Invalid --emap\n"); r = -1; } - break; - case 'c': - opts.lecho = 1; - break; - case 'i': - opts.noinit = 1; - break; - case 'r': - opts.noreset = 1; - break; - case 'l': + switch (c) { + case 's': + strncpy(opts.send_cmd, optarg, sizeof(opts.send_cmd)); + opts.send_cmd[sizeof(opts.send_cmd) - 1] = '\0'; + break; + case 'v': + strncpy(opts.receive_cmd, optarg, sizeof(opts.receive_cmd)); + opts.receive_cmd[sizeof(opts.receive_cmd) - 1] = '\0'; + break; + case 'I': + map = parse_map(optarg); + if (map >= 0) opts.imap = map; + else { fprintf(stderr, "Invalid --imap\n"); r = -1; } + break; + case 'O': + map = parse_map(optarg); + if (map >= 0) opts.omap = map; + else { fprintf(stderr, "Invalid --omap\n"); r = -1; } + break; + case 'E': + map = parse_map(optarg); + if (map >= 0) opts.emap = map; + else { fprintf(stderr, "Invalid --emap\n"); r = -1; } + break; + case 'c': + opts.lecho = 1; + break; + case 'i': + opts.noinit = 1; + break; + case 'r': + opts.noreset = 1; + break; + case 'l': #if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK) - opts.nolock = 1; + opts.nolock = 1; #endif - break; - case 'e': - opts.escape = CKEY(optarg[0]); - break; - case 'f': - switch (optarg[0]) { - case 'X': - case 'x': - opts.flow = FC_XONXOFF; - break; - case 'H': - case 'h': - opts.flow = FC_RTSCTS; - break; - case 'N': - case 'n': - opts.flow = FC_NONE; - break; - default: - fprintf(stderr, "Invalid --flow: %c\n", optarg[0]); - r = -1; - break; - } - break; - case 'b': - opts.baud = atoi(optarg); - if ( opts.baud == 0 || ! term_baud_ok(opts.baud) ) { - fprintf(stderr, "Invalid --baud: %d\n", opts.baud); - r = -1; - } - break; - case 'y': - switch (optarg[0]) { - case 'e': - opts.parity = P_EVEN; - break; - case 'o': - opts.parity = P_ODD; - break; - case 'n': - opts.parity = P_NONE; - break; - default: - fprintf(stderr, "Invalid --parity: %c\n", optarg[0]); - r = -1; - break; - } - break; - case 'd': - switch (optarg[0]) { - case '5': - opts.databits = 5; - break; - case '6': - opts.databits = 6; - break; - case '7': - opts.databits = 7; - break; - case '8': - opts.databits = 8; - break; - default: - fprintf(stderr, "Invalid --databits: %c\n", optarg[0]); - r = -1; - break; - } - break; - case 'p': - switch (optarg[0]) { - case '1': - opts.stopbits = 1; - break; - case '2': - opts.stopbits = 2; - break; - default: - fprintf(stderr, "Invalid --stopbits: %c\n", optarg[0]); - r = -1; - break; - } - break; - case 'g': - opts.log_filename = strdup(optarg); - break; - case 'R': - opts.lower_rts = 1; - break; - case 'D': - opts.lower_dtr = 1; - break; - case 'h': - show_usage(argv[0]); - exit(EXIT_SUCCESS); - case '?': - default: - fprintf(stderr, "Unrecognized option(s)\n"); - r = -1; - break; - } - if ( r < 0 ) { - fprintf(stderr, "Run with '--help'.\n"); - exit(EXIT_FAILURE); - } - } /* while */ + break; + case 'e': + opts.escape = CKEY(optarg[0]); + break; + case 'f': + switch (optarg[0]) { + case 'X': + case 'x': + opts.flow = FC_XONXOFF; + break; + case 'H': + case 'h': + opts.flow = FC_RTSCTS; + break; + case 'N': + case 'n': + opts.flow = FC_NONE; + break; + default: + fprintf(stderr, "Invalid --flow: %c\n", optarg[0]); + r = -1; + break; + } + break; + case 'b': + opts.baud = atoi(optarg); + if ( opts.baud == 0 || ! term_baud_ok(opts.baud) ) { + fprintf(stderr, "Invalid --baud: %d\n", opts.baud); + r = -1; + } + break; + case 'y': + switch (optarg[0]) { + case 'e': + opts.parity = P_EVEN; + break; + case 'o': + opts.parity = P_ODD; + break; + case 'n': + opts.parity = P_NONE; + break; + default: + fprintf(stderr, "Invalid --parity: %c\n", optarg[0]); + r = -1; + break; + } + break; + case 'd': + switch (optarg[0]) { + case '5': + opts.databits = 5; + break; + case '6': + opts.databits = 6; + break; + case '7': + opts.databits = 7; + break; + case '8': + opts.databits = 8; + break; + default: + fprintf(stderr, "Invalid --databits: %c\n", optarg[0]); + r = -1; + break; + } + break; + case 'p': + switch (optarg[0]) { + case '1': + opts.stopbits = 1; + break; + case '2': + opts.stopbits = 2; + break; + default: + fprintf(stderr, "Invalid --stopbits: %c\n", optarg[0]); + r = -1; + break; + } + break; + case 'g': + opts.log_filename = strdup(optarg); + break; + case 'R': + opts.lower_rts = 1; + break; + case 'D': + opts.lower_dtr = 1; + break; + case 'h': + show_usage(argv[0]); + exit(EXIT_SUCCESS); + case '?': + default: + fprintf(stderr, "Unrecognized option(s)\n"); + r = -1; + break; + } + if ( r < 0 ) { + fprintf(stderr, "Run with '--help'.\n"); + exit(EXIT_FAILURE); + } + } /* while */ - if ( (argc - optind) < 1) { - fprintf(stderr, "No port given\n"); - fprintf(stderr, "Run with '--help'.\n"); - exit(EXIT_FAILURE); - } - opts.port = strdup(argv[optind++]); - if ( ! opts.port ) { - fprintf(stderr, "Out of memory\n"); - exit(EXIT_FAILURE); - } + if ( (argc - optind) < 1) { + fprintf(stderr, "No port given\n"); + fprintf(stderr, "Run with '--help'.\n"); + exit(EXIT_FAILURE); + } + opts.port = strdup(argv[optind++]); + if ( ! opts.port ) { + fprintf(stderr, "Out of memory\n"); + exit(EXIT_FAILURE); + } - if ( argc != optind ) { - fprintf(stderr, "Unexpected non-option arguments: "); - while (argc != optind) - fprintf(stderr, "%s ", argv[optind++]); - fprintf(stderr, "\n"); - exit(EXIT_FAILURE); - } + if ( argc != optind ) { + fprintf(stderr, "Unexpected non-option arguments: "); + while (argc != optind) + fprintf(stderr, "%s ", argv[optind++]); + fprintf(stderr, "\n"); + exit(EXIT_FAILURE); + } #ifndef NO_HELP - printf("picocom v%s\n", VERSION_STR); - printf("\n"); - printf("port is : %s\n", opts.port); - printf("flowcontrol : %s\n", flow_str[opts.flow]); - printf("baudrate is : %d\n", opts.baud); - printf("parity is : %s\n", parity_str[opts.parity]); - printf("databits are : %d\n", opts.databits); - printf("stopbits are : %d\n", opts.stopbits); - printf("escape is : C-%c\n", KEYC(opts.escape)); - printf("local echo is : %s\n", opts.lecho ? "yes" : "no"); - printf("noinit is : %s\n", opts.noinit ? "yes" : "no"); - printf("noreset is : %s\n", opts.noreset ? "yes" : "no"); + printf("picocom v%s\n", VERSION_STR); + printf("\n"); + printf("port is : %s\n", opts.port); + printf("flowcontrol : %s\n", flow_str[opts.flow]); + printf("baudrate is : %d\n", opts.baud); + printf("parity is : %s\n", parity_str[opts.parity]); + printf("databits are : %d\n", opts.databits); + printf("stopbits are : %d\n", opts.stopbits); + printf("escape is : C-%c\n", KEYC(opts.escape)); + printf("local echo is : %s\n", opts.lecho ? "yes" : "no"); + printf("noinit is : %s\n", opts.noinit ? "yes" : "no"); + printf("noreset is : %s\n", opts.noreset ? "yes" : "no"); #if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK) - printf("nolock is : %s\n", opts.nolock ? "yes" : "no"); + printf("nolock is : %s\n", opts.nolock ? "yes" : "no"); #endif - printf("send_cmd is : %s\n", - (opts.send_cmd[0] == '\0') ? "disabled" : opts.send_cmd); - printf("receive_cmd is : %s\n", - (opts.receive_cmd[0] == '\0') ? "disabled" : opts.receive_cmd); - printf("imap is : "); print_map(opts.imap); - printf("omap is : "); print_map(opts.omap); - printf("emap is : "); print_map(opts.emap); - printf("logfile is : %s\n", opts.log_filename ? opts.log_filename : "none"); - printf("\n"); + printf("send_cmd is : %s\n", + (opts.send_cmd[0] == '\0') ? "disabled" : opts.send_cmd); + printf("receive_cmd is : %s\n", + (opts.receive_cmd[0] == '\0') ? "disabled" : opts.receive_cmd); + printf("imap is : "); print_map(opts.imap); + printf("omap is : "); print_map(opts.omap); + printf("emap is : "); print_map(opts.emap); + printf("logfile is : %s\n", opts.log_filename ? opts.log_filename : "none"); + printf("\n"); #endif /* of NO_HELP */ } @@ -1571,124 +1571,124 @@ parse_args(int argc, char *argv[]) int main(int argc, char *argv[]) { - int r; + int r; - parse_args(argc, argv); + parse_args(argc, argv); - establish_signal_handlers(); + establish_signal_handlers(); - r = term_lib_init(); - if ( r < 0 ) - fatal("term_init failed: %s", term_strerror(term_errno, errno)); + r = term_lib_init(); + if ( r < 0 ) + fatal("term_init failed: %s", term_strerror(term_errno, errno)); #ifdef UUCP_LOCK_DIR - if ( ! opts.nolock ) uucp_lockname(UUCP_LOCK_DIR, opts.port); - if ( uucp_lock() < 0 ) - fatal("cannot lock %s: %s", opts.port, strerror(errno)); + if ( ! opts.nolock ) uucp_lockname(UUCP_LOCK_DIR, opts.port); + if ( uucp_lock() < 0 ) + fatal("cannot lock %s: %s", opts.port, strerror(errno)); #endif - if (opts.log_filename) { - log_fd = open(opts.log_filename, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); - if (log_fd < 0) - fatal("cannot open %s: %s", opts.log_filename, strerror(errno)); - } + if (opts.log_filename) { + log_fd = open(opts.log_filename, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + if (log_fd < 0) + fatal("cannot open %s: %s", opts.log_filename, strerror(errno)); + } - tty_fd = open(opts.port, O_RDWR | O_NONBLOCK | O_NOCTTY); - if (tty_fd < 0) - fatal("cannot open %s: %s", opts.port, strerror(errno)); + tty_fd = open(opts.port, O_RDWR | O_NONBLOCK | O_NOCTTY); + if (tty_fd < 0) + fatal("cannot open %s: %s", opts.port, strerror(errno)); #ifdef USE_FLOCK - if ( ! opts.nolock ) { - r = flock(tty_fd, LOCK_EX | LOCK_NB); - if ( r < 0 ) - fatal("cannot lock %s: %s", opts.port, strerror(errno)); - } + if ( ! opts.nolock ) { + r = flock(tty_fd, LOCK_EX | LOCK_NB); + if ( r < 0 ) + fatal("cannot lock %s: %s", opts.port, strerror(errno)); + } #endif - if ( opts.noinit ) { - r = term_add(tty_fd); - } else { - r = term_set(tty_fd, - 1, /* raw mode. */ - opts.baud, /* baud rate. */ - opts.parity, /* parity. */ - opts.databits, /* data bits. */ - opts.stopbits, /* stop bits. */ - opts.flow, /* flow control. */ - 1, /* local or modem */ - !opts.noreset); /* hup-on-close. */ - } - if ( r < 0 ) - fatal("failed to add device %s: %s", - opts.port, term_strerror(term_errno, errno)); + if ( opts.noinit ) { + r = term_add(tty_fd); + } else { + r = term_set(tty_fd, + 1, /* raw mode. */ + opts.baud, /* baud rate. */ + opts.parity, /* parity. */ + opts.databits, /* data bits. */ + opts.stopbits, /* stop bits. */ + opts.flow, /* flow control. */ + 1, /* local or modem */ + !opts.noreset); /* hup-on-close. */ + } + if ( r < 0 ) + fatal("failed to add device %s: %s", + opts.port, term_strerror(term_errno, errno)); - if ( opts.lower_rts ) { - r = term_lower_rts(tty_fd); - if ( r < 0 ) - fatal("failed to lower RTS of device %s: %s", opts.port, term_strerror(term_errno, errno)); - } - if ( opts.lower_dtr ) { - r = term_lower_dtr(tty_fd); - if ( r < 0 ) - fatal("failed to lower DTR of device %s: %s", opts.port, term_strerror(term_errno, errno)); - } + if ( opts.lower_rts ) { + r = term_lower_rts(tty_fd); + if ( r < 0 ) + fatal("failed to lower RTS of device %s: %s", opts.port, term_strerror(term_errno, errno)); + } + if ( opts.lower_dtr ) { + r = term_lower_dtr(tty_fd); + if ( r < 0 ) + fatal("failed to lower DTR of device %s: %s", opts.port, term_strerror(term_errno, errno)); + } - r = term_apply(tty_fd, 0); - if ( r < 0 ) - fatal("failed to config device %s: %s", - opts.port, term_strerror(term_errno, errno)); + r = term_apply(tty_fd, 0); + if ( r < 0 ) + fatal("failed to config device %s: %s", + opts.port, term_strerror(term_errno, errno)); - set_tty_write_sz(term_get_baudrate(tty_fd, NULL)); - - r = term_add(STI); - if ( r < 0 ) - fatal("failed to add I/O device: %s", - term_strerror(term_errno, errno)); - term_set_raw(STI); - r = term_apply(STI, 0); - if ( r < 0 ) - fatal("failed to set I/O device to raw mode: %s", - term_strerror(term_errno, errno)); + set_tty_write_sz(term_get_baudrate(tty_fd, NULL)); + + r = term_add(STI); + if ( r < 0 ) + fatal("failed to add I/O device: %s", + term_strerror(term_errno, errno)); + term_set_raw(STI); + r = term_apply(STI, 0); + if ( r < 0 ) + fatal("failed to set I/O device to raw mode: %s", + term_strerror(term_errno, errno)); #ifdef LINENOISE - init_history(); + init_history(); #endif #ifndef NO_HELP - fd_printf(STO, "Type [C-%c] [C-%c] to see available commands\r\n\r\n", - KEYC(opts.escape), KEYC(KEY_HELP)); + fd_printf(STO, "Type [C-%c] [C-%c] to see available commands\r\n\r\n", + KEYC(opts.escape), KEYC(KEY_HELP)); #endif - fd_printf(STO, "Terminal ready\r\n"); - loop(); + fd_printf(STO, "Terminal ready\r\n"); + loop(); #ifdef LINENOISE - cleanup_history(); + cleanup_history(); #endif - fd_printf(STO, "\r\n"); - if ( opts.noreset ) { - fd_printf(STO, "Skipping tty reset...\r\n"); - term_erase(tty_fd); - } + fd_printf(STO, "\r\n"); + if ( opts.noreset ) { + fd_printf(STO, "Skipping tty reset...\r\n"); + term_erase(tty_fd); + } - if ( sig_exit ) - fd_printf(STO, "Picocom was killed\r\n"); - else - fd_printf(STO, "Thanks for using picocom\r\n"); - /* wait a bit for output to drain */ - sleep(1); + if ( sig_exit ) + fd_printf(STO, "Picocom was killed\r\n"); + else + fd_printf(STO, "Thanks for using picocom\r\n"); + /* wait a bit for output to drain */ + sleep(1); #ifdef UUCP_LOCK_DIR - uucp_unlock(); + uucp_unlock(); #endif - free(opts.port); - if (opts.log_filename) { - free(opts.log_filename); - close(log_fd); - } + free(opts.port); + if (opts.log_filename) { + free(opts.log_filename); + close(log_fd); + } - return EXIT_SUCCESS; + return EXIT_SUCCESS; } /**********************************************************************/ diff --git a/split.c b/split.c index 2d25893..8f97c16 100644 --- a/split.c +++ b/split.c @@ -19,7 +19,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #include @@ -81,7 +81,7 @@ enum states { } else { \ flags |= SPLIT_TRUNC; \ } \ - } while (0) + } while (0) int split_quoted (const char *s, int *argc, char *argv[], int argv_sz) @@ -107,7 +107,7 @@ split_quoted (const char *s, int *argc, char *argv[], int argv_sz) case ST_DELIM: while ( is_delim(*c) ) c++; if ( *c == SQ || *c == DQ ) { - qc = *c; c++; state = ST_QUOTE; + qc = *c; c++; state = ST_QUOTE; break; } if ( *c == EOS ) { @@ -187,7 +187,7 @@ split_quoted (const char *s, int *argc, char *argv[], int argv_sz) assert(0); } } - + return ( err != ERR_OK ) ? -1 : flags; } @@ -205,7 +205,7 @@ main (int argc, char *argv[]) printf("Usage is: %s: \n", argv[0]); exit(EXIT_FAILURE); } - + printf("String to split is: [%s]\n", argv[1]); r = split_quoted(argv[1], &my_argc, my_argv, 12); if ( r < 0 ) { diff --git a/split.h b/split.h index b450be8..d554e01 100644 --- a/split.h +++ b/split.h @@ -19,7 +19,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #ifndef SPLIT_H @@ -88,7 +88,7 @@ * 'a "b"' c d --> [a "b"] [c] [d] * "a 'b'" c d --> [a 'b'] [c] [d] * a"b c" d --> [ab c] [d] - * a\ b c d --> [a b] [c] [d] + * a\ b c d --> [a b] [c] [d] * \a\b c d --> [ab] [c] [d] * \a\\b \\ c d --> [a\b] [\] [c] [d] * "a\$\b" c d --> [a$\b] [c] [d] diff --git a/term.c b/term.c index 52aa3b7..18324a4 100644 --- a/term.c +++ b/term.c @@ -7,7 +7,7 @@ * Nick Patavalis (npat@inaccessnetworks.com) * * originaly by Pantelis Antoniou (panto@intranet.gr), Nick Patavalis - * + * * Documentation can be found in the header file "term.h". * * This program is free software; you can redistribute it and/or @@ -23,7 +23,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA * * $Id$ */ @@ -66,11 +66,11 @@ /***************************************************************************/ static struct term_s { - int init; - int fd[MAX_TERMS]; - struct termios origtermios[MAX_TERMS]; - struct termios currtermios[MAX_TERMS]; - struct termios nexttermios[MAX_TERMS]; + int init; + int fd[MAX_TERMS]; + struct termios origtermios[MAX_TERMS]; + struct termios currtermios[MAX_TERMS]; + struct termios nexttermios[MAX_TERMS]; } term; /***************************************************************************/ @@ -78,31 +78,31 @@ static struct term_s { int term_errno; static const char * const term_err_str[] = { - [TERM_EOK] = "No error", - [TERM_ENOINIT] = "Framework is uninitialized", - [TERM_EFULL] = "Framework is full", + [TERM_EOK] = "No error", + [TERM_ENOINIT] = "Framework is uninitialized", + [TERM_EFULL] = "Framework is full", [TERM_ENOTFOUND] = "Filedes not in the framework", [TERM_EEXISTS] = "Filedes already in the framework", [TERM_EATEXIT] = "Cannot install atexit handler", [TERM_EISATTY] = "Filedes is not a tty", [TERM_EFLUSH] = "Cannot flush the device", - [TERM_EGETATTR] = "Cannot get the device attributes", - [TERM_ESETATTR] = "Cannot set the device attributes", - [TERM_EBAUD] = "Invalid baud rate", - [TERM_ESETOSPEED] = "Cannot set the output speed", - [TERM_ESETISPEED] = "Cannot set the input speed", - [TERM_EGETSPEED] = "Cannot decode speed", - [TERM_EPARITY] = "Invalid parity mode", - [TERM_EDATABITS] = "Invalid number of databits", - [TERM_ESTOPBITS] = "Invalid number of stopbits", - [TERM_EFLOW] = "Invalid flowcontrol mode", + [TERM_EGETATTR] = "Cannot get the device attributes", + [TERM_ESETATTR] = "Cannot set the device attributes", + [TERM_EBAUD] = "Invalid baud rate", + [TERM_ESETOSPEED] = "Cannot set the output speed", + [TERM_ESETISPEED] = "Cannot set the input speed", + [TERM_EGETSPEED] = "Cannot decode speed", + [TERM_EPARITY] = "Invalid parity mode", + [TERM_EDATABITS] = "Invalid number of databits", + [TERM_ESTOPBITS] = "Invalid number of stopbits", + [TERM_EFLOW] = "Invalid flowcontrol mode", [TERM_EDTRDOWN] = "Cannot lower DTR", [TERM_EDTRUP] = "Cannot raise DTR", - [TERM_EMCTL] = "Cannot get mctl status", - [TERM_EDRAIN] = "Cannot drain the device", - [TERM_EBREAK] = "Cannot send break sequence", - [TERM_ERTSDOWN] = "Cannot lower RTS", - [TERM_ERTSUP] = "Cannot raise RTS" + [TERM_EMCTL] = "Cannot get mctl status", + [TERM_EDRAIN] = "Cannot drain the device", + [TERM_EBREAK] = "Cannot send break sequence", + [TERM_ERTSDOWN] = "Cannot lower RTS", + [TERM_ERTSUP] = "Cannot raise RTS" }; static char term_err_buff[1024]; @@ -110,54 +110,54 @@ static char term_err_buff[1024]; const char * term_strerror (int terrnum, int errnum) { - const char *rval; + const char *rval; - switch(terrnum) { - case TERM_EFLUSH: - case TERM_EGETATTR: - case TERM_ESETATTR: - case TERM_ESETOSPEED: - case TERM_ESETISPEED: - case TERM_EDRAIN: - case TERM_EBREAK: - snprintf(term_err_buff, sizeof(term_err_buff), - "%s: %s", term_err_str[terrnum], strerror(errnum)); - rval = term_err_buff; - break; - case TERM_EOK: - case TERM_ENOINIT: - case TERM_EFULL: - case TERM_ENOTFOUND: - case TERM_EEXISTS: - case TERM_EATEXIT: - case TERM_EISATTY: - case TERM_EBAUD: - case TERM_EPARITY: - case TERM_EDATABITS: - case TERM_ESTOPBITS: - case TERM_EFLOW: - case TERM_EDTRDOWN: - case TERM_EDTRUP: - case TERM_EMCTL: - case TERM_ERTSDOWN: - case TERM_ERTSUP: - snprintf(term_err_buff, sizeof(term_err_buff), - "%s", term_err_str[terrnum]); - rval = term_err_buff; - break; - default: - rval = NULL; - break; - } + switch(terrnum) { + case TERM_EFLUSH: + case TERM_EGETATTR: + case TERM_ESETATTR: + case TERM_ESETOSPEED: + case TERM_ESETISPEED: + case TERM_EDRAIN: + case TERM_EBREAK: + snprintf(term_err_buff, sizeof(term_err_buff), + "%s: %s", term_err_str[terrnum], strerror(errnum)); + rval = term_err_buff; + break; + case TERM_EOK: + case TERM_ENOINIT: + case TERM_EFULL: + case TERM_ENOTFOUND: + case TERM_EEXISTS: + case TERM_EATEXIT: + case TERM_EISATTY: + case TERM_EBAUD: + case TERM_EPARITY: + case TERM_EDATABITS: + case TERM_ESTOPBITS: + case TERM_EFLOW: + case TERM_EDTRDOWN: + case TERM_EDTRUP: + case TERM_EMCTL: + case TERM_ERTSDOWN: + case TERM_ERTSUP: + snprintf(term_err_buff, sizeof(term_err_buff), + "%s", term_err_str[terrnum]); + rval = term_err_buff; + break; + default: + rval = NULL; + break; + } - return rval; + return rval; } int term_perror (const char *prefix) { - return fprintf(stderr, "%s %s\n", - prefix, term_strerror(term_errno, errno)); + return fprintf(stderr, "%s %s\n", + prefix, term_strerror(term_errno, errno)); } /***************************************************************************/ @@ -213,19 +213,19 @@ term_perror (const char *prefix) #if defined(__APPLE__) && defined (USE_CUSTOM_BAUD) int cfsetospeed_custom(struct termios *tiop, int speed) { - return cfsetospeed(tiop, speed); + return cfsetospeed(tiop, speed); } int cfsetispeed_custom(struct termios *tiop, int speed) { - return cfsetispeed(tiop, speed); + return cfsetispeed(tiop, speed); } int cfgetospeed_custom(struct termios *tiop) { - return cfgetospeed(tiop); + return cfgetospeed(tiop); } int cfgetispeed_custom(struct termios *tiop) { - return cfgetispeed(tiop); + return cfgetispeed(tiop); } #endif /* __APPLE__ && USE_CUSTOM_BAUD */ @@ -235,66 +235,66 @@ int cfgetispeed_custom(struct termios *tiop) { #define BNONE 0xFFFFFFFF struct baud_codes { - int speed; - speed_t code; + int speed; + speed_t code; } baud_table[] = { - { 0, B0 }, - { 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 }, - { 57600, B57600 }, - { 115200, B115200 }, + { 0, B0 }, + { 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 }, + { 57600, B57600 }, + { 115200, B115200 }, #ifdef HIGH_BAUD #ifdef B230400 - { 230400, B230400 }, + { 230400, B230400 }, #endif #ifdef B460800 - { 460800, B460800 }, + { 460800, B460800 }, #endif #ifdef B500000 - { 500000, B500000 }, + { 500000, B500000 }, #endif #ifdef B576000 - { 576000, B576000 }, + { 576000, B576000 }, #endif #ifdef B921600 - { 921600, B921600 }, + { 921600, B921600 }, #endif #ifdef B1000000 - { 1000000, B1000000 }, + { 1000000, B1000000 }, #endif #ifdef B1152000 - { 1152000, B1152000 }, + { 1152000, B1152000 }, #endif #ifdef B1500000 - { 1500000, B1500000 }, + { 1500000, B1500000 }, #endif #ifdef B2000000 - { 2000000, B2000000 }, + { 2000000, B2000000 }, #endif #ifdef B2500000 - { 2500000, B2500000 }, + { 2500000, B2500000 }, #endif #ifdef B3000000 - { 3000000, B3000000 }, + { 3000000, B3000000 }, #endif #ifdef B3500000 - { 3500000, B3500000 }, + { 3500000, B3500000 }, #endif #ifdef B4000000 - { 4000000, B4000000 }, + { 4000000, B4000000 }, #endif #endif /* of HIGH_BAUD */ }; @@ -304,73 +304,73 @@ struct baud_codes { int term_baud_up (int baud) { - int i; + int i; - for (i = 0; i < BAUD_TABLE_SZ; i++) { - if ( baud >= baud_table[i].speed ) - continue; - else { - baud = baud_table[i].speed; - break; - } - } + for (i = 0; i < BAUD_TABLE_SZ; i++) { + if ( baud >= baud_table[i].speed ) + continue; + else { + baud = baud_table[i].speed; + break; + } + } - return baud; + return baud; } int term_baud_down (int baud) { - int i; + int i; - for (i = BAUD_TABLE_SZ - 1; i >= 0; i--) { - if ( baud <= baud_table[i].speed ) - continue; - else { - baud = baud_table[i].speed; - break; - } - } + for (i = BAUD_TABLE_SZ - 1; i >= 0; i--) { + if ( baud <= baud_table[i].speed ) + continue; + else { + baud = baud_table[i].speed; + break; + } + } - return baud; + return baud; } static speed_t Bcode(int speed) { - speed_t code = BNONE; - int i; + speed_t code = BNONE; + int i; - for (i = 0; i < BAUD_TABLE_SZ; i++) { - if ( baud_table[i].speed == speed ) { - code = baud_table[i].code; - break; - } - } - return code; + for (i = 0; i < BAUD_TABLE_SZ; i++) { + if ( baud_table[i].speed == speed ) { + code = baud_table[i].code; + break; + } + } + return code; } static int Bspeed(speed_t code) { - int speed = -1, i; + int speed = -1, i; - for (i = 0; i < BAUD_TABLE_SZ; i++) { - if ( baud_table[i].code == code ) { - speed = baud_table[i].speed; - break; - } - } - return speed; + for (i = 0; i < BAUD_TABLE_SZ; i++) { + if ( baud_table[i].code == code ) { + speed = baud_table[i].speed; + break; + } + } + return speed; } int term_baud_ok(int baud) { #ifndef USE_CUSTOM_BAUD - return (Bcode(baud) != BNONE) ? 1 : 0; + return (Bcode(baud) != BNONE) ? 1 : 0; #else - return (baud >= 0); + return (baud >= 0); #endif } @@ -379,28 +379,28 @@ term_baud_ok(int baud) static int term_find_next_free (void) { - int rval, i; + int rval, i; - do { /* dummy */ - if ( ! term.init ) { - term_errno = TERM_ENOINIT; - rval = -1; - break; - } + do { /* dummy */ + if ( ! term.init ) { + term_errno = TERM_ENOINIT; + rval = -1; + break; + } - for (i = 0; i < MAX_TERMS; i++) - if ( term.fd[i] == -1 ) break; + for (i = 0; i < MAX_TERMS; i++) + if ( term.fd[i] == -1 ) break; - if ( i == MAX_TERMS ) { - term_errno = TERM_EFULL; - rval = -1; - break; - } + if ( i == MAX_TERMS ) { + term_errno = TERM_EFULL; + rval = -1; + break; + } - rval = i; - } while (0); + rval = i; + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -408,28 +408,28 @@ term_find_next_free (void) static int term_find (int fd) { - int rval, i; + int rval, i; - do { /* dummy */ - if ( ! term.init ) { - term_errno = TERM_ENOINIT; - rval = -1; - break; - } + do { /* dummy */ + if ( ! term.init ) { + term_errno = TERM_ENOINIT; + rval = -1; + break; + } - for (i = 0; i < MAX_TERMS; i++) - if (term.fd[i] == fd) break; + for (i = 0; i < MAX_TERMS; i++) + if (term.fd[i] == fd) break; - if ( i == MAX_TERMS ) { - term_errno = TERM_ENOTFOUND; - rval = -1; - break; - } + if ( i == MAX_TERMS ) { + term_errno = TERM_ENOTFOUND; + rval = -1; + break; + } - rval = i; - } while (0); + rval = i; + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -437,30 +437,30 @@ term_find (int fd) static void term_exitfunc (void) { - int r, i; + int r, i; - do { /* dummy */ - if ( ! term.init ) - break; + do { /* dummy */ + if ( ! term.init ) + break; - for (i = 0; i < MAX_TERMS; i++) { - if (term.fd[i] == -1) - continue; - tcflush(term.fd[i], TCIOFLUSH); - do { - r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); - } while ( r < 0 && errno == EINTR ); - if ( r < 0 ) { - char *tname; + for (i = 0; i < MAX_TERMS; i++) { + if (term.fd[i] == -1) + continue; + tcflush(term.fd[i], TCIOFLUSH); + do { + r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); + } while ( r < 0 && errno == EINTR ); + if ( r < 0 ) { + char *tname; - tname = ttyname(term.fd[i]); - if ( ! tname ) tname = "UNKNOWN"; - fprintf(stderr, "%s: reset failed for dev %s: %s\n", - __FUNCTION__, tname, strerror(errno)); - } - term.fd[i] = -1; - } - } while (0); + tname = ttyname(term.fd[i]); + if ( ! tname ) tname = "UNKNOWN"; + fprintf(stderr, "%s: reset failed for dev %s: %s\n", + __FUNCTION__, tname, strerror(errno)); + } + term.fd[i] = -1; + } + } while (0); } /***************************************************************************/ @@ -468,45 +468,45 @@ term_exitfunc (void) int term_lib_init (void) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ - if ( term.init ) { - /* reset all terms back to their original settings */ - for (i = 0; i < MAX_TERMS; i++) { - if (term.fd[i] == -1) - continue; - tcflush(term.fd[i], TCIOFLUSH); - do { - r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); - } while ( r < 0 && errno == EINTR ); - if ( r < 0 ) { - char *tname; - - tname = ttyname(term.fd[i]); - if ( ! tname ) tname = "UNKNOWN"; - fprintf(stderr, "%s: reset failed for dev %s: %s\n", - __FUNCTION__, tname, strerror(errno)); - } - term.fd[i] = -1; - } - } else { - /* initialize term structure. */ - for (i = 0; i < MAX_TERMS; i++) - term.fd[i] = -1; - if ( atexit(term_exitfunc) != 0 ) { - term_errno = TERM_EATEXIT; - rval = -1; - break; - } - /* ok. term struct is now initialized. */ - term.init = 1; - } - } while(0); + do { /* dummy */ + if ( term.init ) { + /* reset all terms back to their original settings */ + for (i = 0; i < MAX_TERMS; i++) { + if (term.fd[i] == -1) + continue; + tcflush(term.fd[i], TCIOFLUSH); + do { + r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); + } while ( r < 0 && errno == EINTR ); + if ( r < 0 ) { + char *tname; - return rval; + tname = ttyname(term.fd[i]); + if ( ! tname ) tname = "UNKNOWN"; + fprintf(stderr, "%s: reset failed for dev %s: %s\n", + __FUNCTION__, tname, strerror(errno)); + } + term.fd[i] = -1; + } + } else { + /* initialize term structure. */ + for (i = 0; i < MAX_TERMS; i++) + term.fd[i] = -1; + if ( atexit(term_exitfunc) != 0 ) { + term_errno = TERM_EATEXIT; + rval = -1; + break; + } + /* ok. term struct is now initialized. */ + term.init = 1; + } + } while(0); + + return rval; } /***************************************************************************/ @@ -514,43 +514,43 @@ term_lib_init (void) int term_add (int fd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ - i = term_find(fd); - if ( i >= 0 ) { - term_errno = TERM_EEXISTS; - rval = -1; - break; - } + do { /* dummy */ + i = term_find(fd); + if ( i >= 0 ) { + term_errno = TERM_EEXISTS; + rval = -1; + break; + } - if ( ! isatty(fd) ) { - term_errno = TERM_EISATTY; - rval = -1; - break; - } + if ( ! isatty(fd) ) { + term_errno = TERM_EISATTY; + rval = -1; + break; + } - i = term_find_next_free(); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find_next_free(); + if ( i < 0 ) { + rval = -1; + break; + } - r = tcgetattr(fd, &term.origtermios[i]); - if ( r < 0 ) { - term_errno = TERM_EGETATTR; - rval = -1; - break; - } + r = tcgetattr(fd, &term.origtermios[i]); + if ( r < 0 ) { + term_errno = TERM_EGETATTR; + rval = -1; + break; + } - term.currtermios[i] = term.origtermios[i]; - term.nexttermios[i] = term.origtermios[i]; - term.fd[i] = fd; - } while (0); + term.currtermios[i] = term.origtermios[i]; + term.nexttermios[i] = term.origtermios[i]; + term.fd[i] = fd; + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -558,36 +558,36 @@ term_add (int fd) int term_remove(int fd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } - - do { /* dummy */ - r = tcflush(term.fd[i], TCIOFLUSH); - if ( r < 0 ) { - term_errno = TERM_EFLUSH; - rval = -1; - break; - } - r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); - if ( r < 0 ) { - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - } while (0); - - term.fd[i] = -1; - } while (0); + do { /* dummy */ + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - return rval; + do { /* dummy */ + r = tcflush(term.fd[i], TCIOFLUSH); + if ( r < 0 ) { + term_errno = TERM_EFLUSH; + rval = -1; + break; + } + r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); + if ( r < 0 ) { + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + } while (0); + + term.fd[i] = -1; + } while (0); + + return rval; } /***************************************************************************/ @@ -595,21 +595,21 @@ term_remove(int fd) int term_erase(int fd) { - int rval, i; + int rval, i; - rval = 0; + rval = 0; - do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } - - term.fd[i] = -1; - } while (0); + do { /* dummy */ + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - return rval; + term.fd[i] = -1; + } while (0); + + return rval; } /***************************************************************************/ @@ -617,36 +617,36 @@ term_erase(int fd) int term_replace (int oldfd, int newfd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(oldfd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(oldfd); + if ( i < 0 ) { + rval = -1; + break; + } - r = tcsetattr(newfd, TCSANOW, &term.currtermios[i]); - if ( r < 0 ) { - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - r = tcgetattr(newfd, &term.currtermios[i]); - if ( r < 0 ) { - term_errno = TERM_EGETATTR; - rval = -1; - break; - } + r = tcsetattr(newfd, TCSANOW, &term.currtermios[i]); + if ( r < 0 ) { + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + r = tcgetattr(newfd, &term.currtermios[i]); + if ( r < 0 ) { + term_errno = TERM_EGETATTR; + rval = -1; + break; + } - term.fd[i] = newfd; + term.fd[i] = newfd; - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -654,41 +654,41 @@ term_replace (int oldfd, int newfd) int term_reset (int fd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - r = tcflush(term.fd[i], TCIOFLUSH); - if ( r < 0 ) { - term_errno = TERM_EFLUSH; - rval = -1; - break; - } - r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); - if ( r < 0 ) { - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - r = tcgetattr(term.fd[i], &term.currtermios[i]); - if ( r < 0 ) { - term_errno = TERM_EGETATTR; - rval = -1; - break; - } + r = tcflush(term.fd[i], TCIOFLUSH); + if ( r < 0 ) { + term_errno = TERM_EFLUSH; + rval = -1; + break; + } + r = tcsetattr(term.fd[i], TCSANOW, &term.origtermios[i]); + if ( r < 0 ) { + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + r = tcgetattr(term.fd[i], &term.currtermios[i]); + if ( r < 0 ) { + term_errno = TERM_EGETATTR; + rval = -1; + break; + } - term.nexttermios[i] = term.currtermios[i]; - } while (0); + term.nexttermios[i] = term.currtermios[i]; + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -696,23 +696,23 @@ term_reset (int fd) int term_revert (int fd) { - int rval, i; + int rval, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - term.nexttermios[i] = term.currtermios[i]; + term.nexttermios[i] = term.currtermios[i]; - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -720,28 +720,28 @@ term_revert (int fd) int term_refresh (int fd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - r = tcgetattr(fd, &term.currtermios[i]); - if ( r < 0 ) { - term_errno = TERM_EGETATTR; - rval = -1; - break; - } + r = tcgetattr(fd, &term.currtermios[i]); + if ( r < 0 ) { + term_errno = TERM_EGETATTR; + rval = -1; + break; + } - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -749,38 +749,38 @@ term_refresh (int fd) int term_apply (int fd, int now) { - int when, rval, r, i; + int when, rval, r, i; - when = now ? TCSANOW : TCSAFLUSH; + when = now ? TCSANOW : TCSAFLUSH; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } - - r = tcsetattr(term.fd[i], when, &term.nexttermios[i]); - if ( r < 0 ) { - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - r = tcgetattr(term.fd[i], &term.nexttermios[i]); - if ( r < 0 ) { - term_errno = TERM_EGETATTR; - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - term.currtermios[i] = term.nexttermios[i]; + r = tcsetattr(term.fd[i], when, &term.nexttermios[i]); + if ( r < 0 ) { + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + r = tcgetattr(term.fd[i], &term.nexttermios[i]); + if ( r < 0 ) { + term_errno = TERM_EGETATTR; + rval = -1; + break; + } - } while (0); + term.currtermios[i] = term.nexttermios[i]; - return rval; + } while (0); + + return rval; } /***************************************************************************/ @@ -788,27 +788,27 @@ term_apply (int fd, int now) int term_set_raw (int fd) { - int rval, i; + int rval, i; - rval = 0; + rval = 0; - do { /* dummy */ - - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + do { /* dummy */ - /* BSD raw mode */ - cfmakeraw(&term.nexttermios[i]); - /* one byte at a time, no timer */ - term.nexttermios[i].c_cc[VMIN] = 1; - term.nexttermios[i].c_cc[VTIME] = 0; + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - } while (0); - - return rval; + /* BSD raw mode */ + cfmakeraw(&term.nexttermios[i]); + /* one byte at a time, no timer */ + term.nexttermios[i].c_cc[VMIN] = 1; + term.nexttermios[i].c_cc[VTIME] = 0; + + } while (0); + + return rval; } /***************************************************************************/ @@ -816,171 +816,171 @@ term_set_raw (int fd) int term_set_baudrate (int fd, int baudrate) { - int rval, r, i; - speed_t spd; - struct termios tio; + int rval, r, i; + speed_t spd; + struct termios tio; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - tio = term.nexttermios[i]; - spd = Bcode(baudrate); - if ( spd != BNONE ) { - r = cfsetospeed(&tio, spd); - if ( r < 0 ) { - term_errno = TERM_ESETOSPEED; - rval = -1; - break; - } - cfsetispeed(&tio, B0); - } else { + tio = term.nexttermios[i]; + spd = Bcode(baudrate); + if ( spd != BNONE ) { + r = cfsetospeed(&tio, spd); + if ( r < 0 ) { + term_errno = TERM_ESETOSPEED; + rval = -1; + break; + } + cfsetispeed(&tio, B0); + } else { #ifdef USE_CUSTOM_BAUD - r = cfsetospeed_custom(&tio, baudrate); - if ( r < 0 ) { - term_errno = TERM_ESETOSPEED; - rval = -1; - break; - } - cfsetispeed(&tio, B0); + r = cfsetospeed_custom(&tio, baudrate); + if ( r < 0 ) { + term_errno = TERM_ESETOSPEED; + rval = -1; + break; + } + cfsetispeed(&tio, B0); #else /* ! defined USE_CUSTOM_BAUD */ - term_errno = TERM_EBAUD; - rval = -1; - break; + term_errno = TERM_EBAUD; + rval = -1; + break; #endif /* of USE_CUSTOM_BAUD */ - } + } - term.nexttermios[i] = tio; + term.nexttermios[i] = tio; - } while (0); + } while (0); - return rval; + return rval; } -int +int term_get_baudrate (int fd, int *ispeed) { - speed_t code; - int i, ospeed; + speed_t code; + int i, ospeed; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - ospeed = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + ospeed = -1; + break; + } - if ( ispeed ) { - code = cfgetispeed(&term.currtermios[i]); - *ispeed = Bspeed(code); + if ( ispeed ) { + code = cfgetispeed(&term.currtermios[i]); + *ispeed = Bspeed(code); #ifdef USE_CUSTOM_BAUD - if ( *ispeed < 0 ) { - *ispeed = cfgetispeed_custom(&term.currtermios[i]); - } + if ( *ispeed < 0 ) { + *ispeed = cfgetispeed_custom(&term.currtermios[i]); + } #endif - } - code = cfgetospeed(&term.currtermios[i]); - ospeed = Bspeed(code); - if ( ospeed < 0 ) { + } + code = cfgetospeed(&term.currtermios[i]); + ospeed = Bspeed(code); + if ( ospeed < 0 ) { #ifdef USE_CUSTOM_BAUD - ospeed = cfgetospeed_custom(&term.currtermios[i]); - if ( ospeed < 0 ) { - term_errno = TERM_EGETSPEED; - } + ospeed = cfgetospeed_custom(&term.currtermios[i]); + if ( ospeed < 0 ) { + term_errno = TERM_EGETSPEED; + } #else - term_errno = TERM_EGETSPEED; + term_errno = TERM_EGETSPEED; #endif - } + } - } while (0); + } while (0); - return ospeed; + return ospeed; } /***************************************************************************/ int -term_set_parity (int fd, enum parity_e parity) +term_set_parity (int fd, enum parity_e parity) { - int rval, i; - struct termios *tiop; + int rval, i; + struct termios *tiop; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - tiop = &term.nexttermios[i]; + tiop = &term.nexttermios[i]; - switch (parity) { - case P_EVEN: - tiop->c_cflag &= ~(PARODD | CMSPAR); - tiop->c_cflag |= PARENB; - break; - case P_ODD: - tiop->c_cflag &= ~CMSPAR; - tiop->c_cflag |= PARENB | PARODD; - break; - case P_MARK: - tiop->c_cflag |= PARENB | PARODD | CMSPAR; - break; - case P_SPACE: - tiop->c_cflag &= ~PARODD; - tiop->c_cflag |= PARENB | CMSPAR; - break; - case P_NONE: - tiop->c_cflag &= ~(PARENB | PARODD | CMSPAR); - break; - default: - term_errno = TERM_EPARITY; - rval = -1; - break; - } - if ( rval < 0 ) break; + switch (parity) { + case P_EVEN: + tiop->c_cflag &= ~(PARODD | CMSPAR); + tiop->c_cflag |= PARENB; + break; + case P_ODD: + tiop->c_cflag &= ~CMSPAR; + tiop->c_cflag |= PARENB | PARODD; + break; + case P_MARK: + tiop->c_cflag |= PARENB | PARODD | CMSPAR; + break; + case P_SPACE: + tiop->c_cflag &= ~PARODD; + tiop->c_cflag |= PARENB | CMSPAR; + break; + case P_NONE: + tiop->c_cflag &= ~(PARENB | PARODD | CMSPAR); + break; + default: + term_errno = TERM_EPARITY; + rval = -1; + break; + } + if ( rval < 0 ) break; - } while (0); + } while (0); - return rval; + return rval; } enum parity_e term_get_parity (int fd) { - tcflag_t flg; - int i, parity; + tcflag_t flg; + int i, parity; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - parity = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + parity = -1; + break; + } - flg = term.currtermios[i].c_cflag; - if ( ! (flg & PARENB) ) { - parity = P_NONE; - } else if ( flg & CMSPAR ) { - parity = (flg & PARODD) ? P_MARK : P_SPACE; - } else { - parity = (flg & PARODD) ? P_ODD : P_EVEN; - } + flg = term.currtermios[i].c_cflag; + if ( ! (flg & PARENB) ) { + parity = P_NONE; + } else if ( flg & CMSPAR ) { + parity = (flg & PARODD) ? P_MARK : P_SPACE; + } else { + parity = (flg & PARODD) ? P_ODD : P_EVEN; + } - } while (0); - - return parity; + } while (0); + + return parity; } /***************************************************************************/ @@ -988,80 +988,80 @@ term_get_parity (int fd) int term_set_databits (int fd, int databits) { - int rval, i; - struct termios *tiop; + int rval, i; + struct termios *tiop; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - tiop = &term.nexttermios[i]; - - switch (databits) { - case 5: - tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS5; - break; - case 6: - tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS6; - break; - case 7: - tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS7; - break; - case 8: - tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS8; - break; - default: - term_errno = TERM_EDATABITS; - rval = -1; - break; - } - if ( rval < 0 ) break; + tiop = &term.nexttermios[i]; - } while (0); + switch (databits) { + case 5: + tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS5; + break; + case 6: + tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS6; + break; + case 7: + tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS7; + break; + case 8: + tiop->c_cflag = (tiop->c_cflag & ~CSIZE) | CS8; + break; + default: + term_errno = TERM_EDATABITS; + rval = -1; + break; + } + if ( rval < 0 ) break; - return rval; + } while (0); + + return rval; } int term_get_databits (int fd) { - tcflag_t flg; - int i, bits; + tcflag_t flg; + int i, bits; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - bits = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + bits = -1; + break; + } - flg = term.currtermios[i].c_cflag & CSIZE; - switch (flg) { - case CS5: - bits = 5; - break; - case CS6: - bits = 6; - break; - case CS7: - bits = 7; - break; - case CS8: - default: - bits = 8; - break; - } + flg = term.currtermios[i].c_cflag & CSIZE; + switch (flg) { + case CS5: + bits = 5; + break; + case CS6: + bits = 6; + break; + case CS7: + bits = 7; + break; + case CS8: + default: + bits = 8; + break; + } - } while (0); + } while (0); - return bits; + return bits; } /***************************************************************************/ @@ -1069,58 +1069,58 @@ term_get_databits (int fd) int term_set_stopbits (int fd, int stopbits) { - int rval, i; - struct termios *tiop; + int rval, i; + struct termios *tiop; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - tiop = &term.nexttermios[i]; - - switch (stopbits) { - case 1: - tiop->c_cflag &= ~CSTOPB; - break; - case 2: - tiop->c_cflag |= CSTOPB; - break; - default: - term_errno = TERM_ESTOPBITS; - rval = -1; - break; - } - if ( rval < 0 ) break; + tiop = &term.nexttermios[i]; - } while (0); + switch (stopbits) { + case 1: + tiop->c_cflag &= ~CSTOPB; + break; + case 2: + tiop->c_cflag |= CSTOPB; + break; + default: + term_errno = TERM_ESTOPBITS; + rval = -1; + break; + } + if ( rval < 0 ) break; - return rval; + } while (0); + + return rval; } int term_get_stopbits (int fd) { - int i, bits; + int i, bits; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - bits = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + bits = -1; + break; + } - bits = (term.currtermios[i].c_cflag & CSTOPB) ? 2 : 1; + bits = (term.currtermios[i].c_cflag & CSTOPB) ? 2 : 1; - } while (0); + } while (0); - return bits; + return bits; } /***************************************************************************/ @@ -1128,77 +1128,77 @@ term_get_stopbits (int fd) int term_set_flowcntrl (int fd, enum flowcntrl_e flowcntl) { - int rval, i; - struct termios *tiop; + int rval, i; + struct termios *tiop; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } - - tiop = &term.nexttermios[i]; + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - switch (flowcntl) { - case FC_RTSCTS: - tiop->c_cflag |= CRTSCTS; - tiop->c_iflag &= ~(IXON | IXOFF | IXANY); - break; - case FC_XONXOFF: - tiop->c_cflag &= ~(CRTSCTS); - tiop->c_iflag |= IXON | IXOFF; - break; - case FC_NONE: - tiop->c_cflag &= ~(CRTSCTS); - tiop->c_iflag &= ~(IXON | IXOFF | IXANY); - break; - default: - term_errno = TERM_EFLOW; - rval = -1; - break; - } - if ( rval < 0 ) break; + tiop = &term.nexttermios[i]; - } while (0); + switch (flowcntl) { + case FC_RTSCTS: + tiop->c_cflag |= CRTSCTS; + tiop->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + case FC_XONXOFF: + tiop->c_cflag &= ~(CRTSCTS); + tiop->c_iflag |= IXON | IXOFF; + break; + case FC_NONE: + tiop->c_cflag &= ~(CRTSCTS); + tiop->c_iflag &= ~(IXON | IXOFF | IXANY); + break; + default: + term_errno = TERM_EFLOW; + rval = -1; + break; + } + if ( rval < 0 ) break; - return rval; + } while (0); + + return rval; } enum flowcntrl_e term_get_flowcntrl (int fd) { - int i, flow; - int rtscts, xoff, xon; + int i, flow; + int rtscts, xoff, xon; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - flow = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + flow = -1; + break; + } - rtscts = (term.currtermios[i].c_cflag & CRTSCTS) ? 1 : 0; - xoff = (term.currtermios[i].c_iflag & IXOFF) ? 1 : 0; - xon = (term.currtermios[i].c_iflag & (IXON | IXANY)) ? 1 : 0; + rtscts = (term.currtermios[i].c_cflag & CRTSCTS) ? 1 : 0; + xoff = (term.currtermios[i].c_iflag & IXOFF) ? 1 : 0; + xon = (term.currtermios[i].c_iflag & (IXON | IXANY)) ? 1 : 0; - if ( rtscts && ! xoff && ! xon ) { - flow = FC_RTSCTS; - } else if ( ! rtscts && xoff && xon ) { - flow = FC_XONXOFF; - } else if ( ! rtscts && ! xoff && ! xon ) { - flow = FC_NONE; - } else { - flow = FC_OTHER; - } + if ( rtscts && ! xoff && ! xon ) { + flow = FC_RTSCTS; + } else if ( ! rtscts && xoff && xon ) { + flow = FC_XONXOFF; + } else if ( ! rtscts && ! xoff && ! xon ) { + flow = FC_NONE; + } else { + flow = FC_OTHER; + } - } while (0); - - return flow; + } while (0); + + return flow; } /***************************************************************************/ @@ -1206,29 +1206,29 @@ term_get_flowcntrl (int fd) int term_set_local(int fd, int local) { - int rval, i; - struct termios *tiop; + int rval, i; + struct termios *tiop; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - tiop = &term.nexttermios[i]; + tiop = &term.nexttermios[i]; - if ( local ) - tiop->c_cflag |= CLOCAL; - else - tiop->c_cflag &= ~CLOCAL; + if ( local ) + tiop->c_cflag |= CLOCAL; + else + tiop->c_cflag &= ~CLOCAL; - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -1236,104 +1236,104 @@ term_set_local(int fd, int local) int term_set_hupcl (int fd, int on) { - int rval, i; - struct termios *tiop; + int rval, i; + struct termios *tiop; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } - tiop = &term.nexttermios[i]; + tiop = &term.nexttermios[i]; - if ( on ) - tiop->c_cflag |= HUPCL; - else - tiop->c_cflag &= ~HUPCL; + if ( on ) + tiop->c_cflag |= HUPCL; + else + tiop->c_cflag &= ~HUPCL; - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ int term_set(int fd, - int raw, - int baud, - enum parity_e parity, - int databits, int stopbits, - enum flowcntrl_e fc, - int local, int hup_close) + int raw, + int baud, + enum parity_e parity, + int databits, int stopbits, + enum flowcntrl_e fc, + int local, int hup_close) { - int rval, r, i, ni; - struct termios tio; + int rval, r, i, ni; + struct termios tio; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - ni = term_add(fd); - if ( ni < 0 ) { - rval = -1; - break; - } - } else { - ni = i; - } + i = term_find(fd); + if ( i < 0 ) { + ni = term_add(fd); + if ( ni < 0 ) { + rval = -1; + break; + } + } else { + ni = i; + } - tio = term.nexttermios[ni]; + tio = term.nexttermios[ni]; - do { /* dummy */ + do { /* dummy */ - if (raw) { - r = term_set_raw(fd); - if ( r < 0 ) { rval = -1; break; } - } - - r = term_set_baudrate(fd, baud); - if ( r < 0 ) { rval = -1; break; } - - r = term_set_parity(fd, parity); - if ( r < 0 ) { rval = -1; break; } - - r = term_set_databits(fd, databits); - if ( r < 0 ) { rval = -1; break; } + if (raw) { + r = term_set_raw(fd); + if ( r < 0 ) { rval = -1; break; } + } - r = term_set_stopbits(fd, stopbits); - if ( r < 0 ) { rval = -1; break; } - - r = term_set_flowcntrl(fd, fc); - if ( r < 0 ) { rval = -1; break; } - - r = term_set_local(fd, local); - if ( r < 0 ) { rval = -1; break; } - - r = term_set_hupcl(fd, hup_close); - if ( r < 0 ) { rval = -1; break; } - - } while (0); + r = term_set_baudrate(fd, baud); + if ( r < 0 ) { rval = -1; break; } - if ( rval < 0 ) { - if ( i < 0 ) - /* new addition. must be removed */ - term.fd[ni] = -1; - else - /* just revert to previous settings */ - term.nexttermios[ni] = tio; - } + r = term_set_parity(fd, parity); + if ( r < 0 ) { rval = -1; break; } - } while (0); + r = term_set_databits(fd, databits); + if ( r < 0 ) { rval = -1; break; } - return rval; + r = term_set_stopbits(fd, stopbits); + if ( r < 0 ) { rval = -1; break; } + + r = term_set_flowcntrl(fd, fc); + if ( r < 0 ) { rval = -1; break; } + + r = term_set_local(fd, local); + if ( r < 0 ) { rval = -1; break; } + + r = term_set_hupcl(fd, hup_close); + if ( r < 0 ) { rval = -1; break; } + + } while (0); + + if ( rval < 0 ) { + if ( i < 0 ) + /* new addition. must be removed */ + term.fd[ni] = -1; + else + /* just revert to previous settings */ + term.nexttermios[ni] = tio; + } + + } while (0); + + return rval; } /***************************************************************************/ @@ -1341,75 +1341,75 @@ term_set(int fd, int term_pulse_dtr (int fd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } #if defined(__linux__) || defined(__APPLE__) - { - int opins = TIOCM_DTR; + { + int opins = TIOCM_DTR; - r = ioctl(fd, TIOCMBIC, &opins); - if ( r < 0 ) { - term_errno = TERM_EDTRDOWN; - rval = -1; - break; - } + r = ioctl(fd, TIOCMBIC, &opins); + if ( r < 0 ) { + term_errno = TERM_EDTRDOWN; + rval = -1; + break; + } - sleep(1); + sleep(1); - r = ioctl(fd, TIOCMBIS, &opins); - if ( r < 0 ) { - term_errno = TERM_EDTRUP; - rval = -1; - break; - } - } + r = ioctl(fd, TIOCMBIS, &opins); + if ( r < 0 ) { + term_errno = TERM_EDTRUP; + rval = -1; + break; + } + } #else - { - struct termios tio, tioold; + { + struct termios tio, tioold; - r = tcgetattr(fd, &tio); - if ( r < 0 ) { - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - - tioold = tio; - - cfsetospeed(&tio, B0); - cfsetispeed(&tio, B0); - r = tcsetattr(fd, TCSANOW, &tio); - if ( r < 0 ) { - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - - sleep(1); - - r = tcsetattr(fd, TCSANOW, &tioold); - if ( r < 0 ) { - term.currtermios[i] = tio; - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - } + r = tcgetattr(fd, &tio); + if ( r < 0 ) { + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + + tioold = tio; + + cfsetospeed(&tio, B0); + cfsetispeed(&tio, B0); + r = tcsetattr(fd, TCSANOW, &tio); + if ( r < 0 ) { + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + + sleep(1); + + r = tcsetattr(fd, TCSANOW, &tioold); + if ( r < 0 ) { + term.currtermios[i] = tio; + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + } #endif /* of __linux__ or __APPLE__ */ - - } while (0); - return rval; + } while (0); + + return rval; } /***************************************************************************/ @@ -1417,41 +1417,41 @@ term_pulse_dtr (int fd) int term_raise_dtr(int fd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } #if defined(__linux__) || defined(__APPLE__) - { - int opins = TIOCM_DTR; + { + int opins = TIOCM_DTR; - r = ioctl(fd, TIOCMBIS, &opins); - if ( r < 0 ) { - term_errno = TERM_EDTRUP; - rval = -1; - break; - } - } + r = ioctl(fd, TIOCMBIS, &opins); + if ( r < 0 ) { + term_errno = TERM_EDTRUP; + rval = -1; + break; + } + } #else - r = tcsetattr(fd, TCSANOW, &term.currtermios[i]); - if ( r < 0 ) { - /* FIXME: perhaps try to update currtermios */ - term_errno = TERM_ESETATTR; - rval = -1; - break; - } + r = tcsetattr(fd, TCSANOW, &term.currtermios[i]); + if ( r < 0 ) { + /* FIXME: perhaps try to update currtermios */ + term_errno = TERM_ESETATTR; + rval = -1; + break; + } #endif /* of __linux__ or __APPLE__ */ - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -1460,55 +1460,55 @@ term_raise_dtr(int fd) int term_lower_dtr(int fd) { - int rval, r, i; + int rval, r, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } #if defined(__linux__) || defined(__APPLE__) - { - int opins = TIOCM_DTR; + { + int opins = TIOCM_DTR; - r = ioctl(fd, TIOCMBIC, &opins); - if ( r < 0 ) { - term_errno = TERM_EDTRDOWN; - rval = -1; - break; - } - } + r = ioctl(fd, TIOCMBIC, &opins); + if ( r < 0 ) { + term_errno = TERM_EDTRDOWN; + rval = -1; + break; + } + } #else - { - struct termios tio; + { + struct termios tio; - r = tcgetattr(fd, &tio); - if ( r < 0 ) { - term_errno = TERM_EGETATTR; - rval = -1; - break; - } - term.currtermios[i] = tio; - - cfsetospeed(&tio, B0); - cfsetispeed(&tio, B0); - - r = tcsetattr(fd, TCSANOW, &tio); - if ( r < 0 ) { - term_errno = TERM_ESETATTR; - rval = -1; - break; - } - } + r = tcgetattr(fd, &tio); + if ( r < 0 ) { + term_errno = TERM_EGETATTR; + rval = -1; + break; + } + term.currtermios[i] = tio; + + cfsetospeed(&tio, B0); + cfsetispeed(&tio, B0); + + r = tcsetattr(fd, TCSANOW, &tio); + if ( r < 0 ) { + term_errno = TERM_ESETATTR; + rval = -1; + break; + } + } #endif /* of __linux__ or __APPLE__ */ - } while (0); - - return rval; + } while (0); + + return rval; } /***************************************************************************/ @@ -1516,37 +1516,37 @@ term_lower_dtr(int fd) int term_raise_rts(int fd) { - int rval, i; + int rval, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } #if defined(__linux__) || defined(__APPLE__) - { - int r; - int opins = TIOCM_RTS; + { + int r; + int opins = TIOCM_RTS; - r = ioctl(fd, TIOCMBIS, &opins); - if ( r < 0 ) { - term_errno = TERM_ERTSUP; - rval = -1; - break; - } - } + r = ioctl(fd, TIOCMBIS, &opins); + if ( r < 0 ) { + term_errno = TERM_ERTSUP; + rval = -1; + break; + } + } #else - term_errno = TERM_ERTSUP; - rval = -1; + term_errno = TERM_ERTSUP; + rval = -1; #endif /* of __linux__ or __APPLE__ */ - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -1554,37 +1554,37 @@ term_raise_rts(int fd) int term_lower_rts(int fd) { - int rval, i; + int rval, i; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - rval = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + rval = -1; + break; + } #if defined(__linux__) || defined(__APPLE__) - { - int r; - int opins = TIOCM_RTS; + { + int r; + int opins = TIOCM_RTS; - r = ioctl(fd, TIOCMBIC, &opins); - if ( r < 0 ) { - term_errno = TERM_ERTSDOWN; - rval = -1; - break; - } - } + r = ioctl(fd, TIOCMBIC, &opins); + if ( r < 0 ) { + term_errno = TERM_ERTSDOWN; + rval = -1; + break; + } + } #else - term_errno = TERM_ERTSDOWN; - rval = -1; + term_errno = TERM_ERTSDOWN; + rval = -1; #endif /* of __linux__ or __APPLE__ */ - } while (0); - - return rval; + } while (0); + + return rval; } @@ -1593,73 +1593,73 @@ term_lower_rts(int fd) int term_get_mctl (int fd) { - int mctl, i; + int mctl, i; - do { /* dummy */ + do { /* dummy */ - i = term_find(fd); - if ( i < 0 ) { - mctl = -1; - break; - } + i = term_find(fd); + if ( i < 0 ) { + mctl = -1; + break; + } #if defined(__linux__) || defined(__APPLE__) - { - int r, pmctl; - - r = ioctl(fd, TIOCMGET, &pmctl); - if (r < 0) { - mctl = -1; - break; - } - mctl = 0; - if (pmctl & TIOCM_DTR) mctl |= MCTL_DTR; - if (pmctl & TIOCM_DSR) mctl |= MCTL_DSR; - if (pmctl & TIOCM_CD) mctl |= MCTL_DCD; - if (pmctl & TIOCM_RTS) mctl |= MCTL_RTS; - if (pmctl & TIOCM_CTS) mctl |= MCTL_CTS; - if (pmctl & TIOCM_RI) mctl |= MCTL_RI; - } -#else - mctl = MCTL_UNAVAIL; -#endif /* of __linux__ or __APPLE__ */ - } while(0); + { + int r, pmctl; - return mctl; + r = ioctl(fd, TIOCMGET, &pmctl); + if (r < 0) { + mctl = -1; + break; + } + mctl = 0; + if (pmctl & TIOCM_DTR) mctl |= MCTL_DTR; + if (pmctl & TIOCM_DSR) mctl |= MCTL_DSR; + if (pmctl & TIOCM_CD) mctl |= MCTL_DCD; + if (pmctl & TIOCM_RTS) mctl |= MCTL_RTS; + if (pmctl & TIOCM_CTS) mctl |= MCTL_CTS; + if (pmctl & TIOCM_RI) mctl |= MCTL_RI; + } +#else + mctl = MCTL_UNAVAIL; +#endif /* of __linux__ or __APPLE__ */ + } while(0); + + return mctl; } int term_drain(int fd) { - int rval, r; + int rval, r; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - r = term_find(fd); - if ( r < 0 ) { - rval = -1; - break; - } + r = term_find(fd); + if ( r < 0 ) { + rval = -1; + break; + } - do { + do { #ifdef __BIONIC__ - /* See: http://dan.drown.org/android/src/gdb/no-tcdrain */ - r = ioctl(fd, TCSBRK, 1); + /* See: http://dan.drown.org/android/src/gdb/no-tcdrain */ + r = ioctl(fd, TCSBRK, 1); #else - r = tcdrain(fd); + r = tcdrain(fd); #endif - } while ( r < 0 && errno == EINTR); - if ( r < 0 ) { - term_errno = TERM_EDRAIN; - rval = -1; - break; - } + } while ( r < 0 && errno == EINTR); + if ( r < 0 ) { + term_errno = TERM_EDRAIN; + rval = -1; + break; + } - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -1667,27 +1667,27 @@ term_drain(int fd) int term_flush(int fd) { - int rval, r; + int rval, r; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - r = term_find(fd); - if ( r < 0 ) { - rval = -1; - break; - } + r = term_find(fd); + if ( r < 0 ) { + rval = -1; + break; + } - r = tcflush(fd, TCIOFLUSH); - if ( r < 0 ) { - rval = -1; - break; - } + r = tcflush(fd, TCIOFLUSH); + if ( r < 0 ) { + rval = -1; + break; + } - } while (0); + } while (0); - return rval; + return rval; } /***************************************************************************/ @@ -1695,28 +1695,28 @@ term_flush(int fd) int term_break(int fd) { - int rval, r; + int rval, r; - rval = 0; + rval = 0; - do { /* dummy */ + do { /* dummy */ - r = term_find(fd); - if ( r < 0 ) { - rval = -1; - break; - } - - r = tcsendbreak(fd, 0); - if ( r < 0 ) { - term_errno = TERM_EBREAK; - rval = -1; - break; - } + r = term_find(fd); + if ( r < 0 ) { + rval = -1; + break; + } - } while (0); + r = tcsendbreak(fd, 0); + if ( r < 0 ) { + term_errno = TERM_EBREAK; + rval = -1; + break; + } - return rval; + } while (0); + + return rval; } /**************************************************************************/ diff --git a/term.h b/term.h index ee6bdfa..91f8a22 100644 --- a/term.h +++ b/term.h @@ -94,7 +94,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA * * $Id: term.h,v 1.1 2003/05/07 18:00:05 npat Exp $ */ @@ -122,31 +122,31 @@ * See the error strings in "term.c" for a description of each. */ enum term_errno_e { - TERM_EOK = 0, - TERM_ENOINIT, - TERM_EFULL, + TERM_EOK = 0, + TERM_ENOINIT, + TERM_EFULL, TERM_ENOTFOUND, TERM_EEXISTS, TERM_EATEXIT, TERM_EISATTY, TERM_EFLUSH, /* see errno */ - TERM_EGETATTR, /* see errno */ - TERM_ESETATTR, /* see errno */ - TERM_EBAUD, - TERM_ESETOSPEED, - TERM_ESETISPEED, - TERM_EGETSPEED, - TERM_EPARITY, - TERM_EDATABITS, - TERM_ESTOPBITS, - TERM_EFLOW, - TERM_EDTRDOWN, - TERM_EDTRUP, - TERM_EMCTL, - TERM_EDRAIN, /* see errno */ - TERM_EBREAK, - TERM_ERTSDOWN, - TERM_ERTSUP + TERM_EGETATTR, /* see errno */ + TERM_ESETATTR, /* see errno */ + TERM_EBAUD, + TERM_ESETOSPEED, + TERM_ESETISPEED, + TERM_EGETSPEED, + TERM_EPARITY, + TERM_EDATABITS, + TERM_ESTOPBITS, + TERM_EFLOW, + TERM_EDTRDOWN, + TERM_EDTRUP, + TERM_EMCTL, + TERM_EDRAIN, /* see errno */ + TERM_EBREAK, + TERM_ERTSDOWN, + TERM_ERTSUP }; /* E parity_e @@ -160,32 +160,32 @@ enum term_errno_e { * P_SPACE - space parity (parity bit always 0) */ enum parity_e { - P_NONE = 0, - P_EVEN, - P_ODD, - P_MARK, - P_SPACE + P_NONE = 0, + P_EVEN, + P_ODD, + P_MARK, + P_SPACE }; -/* +/* * E flowcntrl_e * * Flow control modes, supported by the library. * * FC_NONE - no flow control - * FC_RTSCTS - RTS/CTS handshaking, also known as hardware + * FC_RTSCTS - RTS/CTS handshaking, also known as hardware * flow-control. - * FC_XONXOFF - xon/xoff flow control. + * FC_XONXOFF - xon/xoff flow control. */ enum flowcntrl_e { - FC_NONE = 0, - FC_RTSCTS, - FC_XONXOFF, - FC_OTHER + FC_NONE = 0, + FC_RTSCTS, + FC_XONXOFF, + FC_OTHER }; -/* - * C MCTL_xxx +/* + * C MCTL_xxx * * Modem control line bits. Used against the return value of * term_get_mctl(). @@ -229,7 +229,7 @@ const char *term_strerror (int terrnum, int errnum); * Emit a description of the current library (and possibly system) * error condition to the standard-error stream. The description is * prefixed by a user-supplied string. What is actually emmited is: - * + * * \n * * The description emitted is the string returned by term_strerror(). @@ -319,7 +319,7 @@ int term_erase (int fd); int term_replace (int oldfd, int newfd); /* - * F term_apply + * F term_apply * * Applies the settings stored in the "nexttermios" structure * associated with the managed filedes "fd", to the respective @@ -338,7 +338,7 @@ int term_replace (int oldfd, int newfd); int term_apply (int fd, int now); /* - * F term_revert + * F term_revert * * Discards all the changes made to the nexttermios structure * associated with the managed filedes "fd" that have not been applied @@ -365,7 +365,7 @@ int term_revert (int fd); int term_reset (int fd); /* - * F term_refresh + * F term_refresh * * Updates the contents of the currtermios structure associated with * the managed filedes "fd", by reading the settings from the @@ -377,11 +377,11 @@ int term_reset (int fd); int term_refresh (int fd); /* F term_set_raw - * + * * Sets the "nexttermios" structure associated with the managed * filedes "fd" to raw mode. The effective settings of the device are * not affected by this function. - * + * * Returns negative on failure, non-negative on success. Returns * failure only to indicate invalid arguments, so the return value can * be safely ignored. @@ -392,7 +392,7 @@ int term_refresh (int fd); * terminal settings as indicated: * * -ignbrk -brkint -parmrk -istrip -inlcr -igncr -icrnl -ixon - * -opost -echo -echonl -icannon -isig -iexten -csize -parenb + * -opost -echo -echonl -icannon -isig -iexten -csize -parenb * cs8 min=1 time=0 */ int term_set_raw (int fd); @@ -427,7 +427,7 @@ int term_set_baudrate (int fd, int baudrate); int term_set_parity (int fd, enum parity_e parity); /* F term_set_databits - * + * * Sets the databits number in the "nexttermios" structure associated * with the managed filedes "fd" to "databits". The effective settings * of the device are not affected by this function. @@ -441,7 +441,7 @@ int term_set_parity (int fd, enum parity_e parity); int term_set_databits (int fd, int databits); /* F term_set_stopbits - * + * * Sets the stopbits number in the "nexttermios" structure associated * with the managed filedes "fd" to "stopbits". The effective settings * of the device are not affected by this function. @@ -459,7 +459,7 @@ int term_set_stopbits (int fd, int stopbits); * Sets the folwcontrol mode in the "nexttermios" structure associated * with the managed filedes "fd" to "flowcntl". The effective settings * of the device are not affected by this function. - * + * * The following flow control modes are supportd by the library: * FC_NONE, FC_RTSCTS, FC_XONXOFF. * @@ -478,7 +478,7 @@ int term_set_flowcntrl (int fd, enum flowcntrl_e flowcntl); * * Returns negative on failure, non negative on success. Returns * failure only to indicate invalid arguments, so the return value can - * be safely ignored. + * be safely ignored. */ int term_set_hupcl (int fd, int on); @@ -491,7 +491,7 @@ int term_set_hupcl (int fd, int on); * * Returns negative on failure, non negative on success. Returns * failure only to indicate invalid arguments, so the return value can - * be safely ignored. + * be safely ignored. */ int term_set_local (int fd, int local); @@ -501,11 +501,11 @@ int term_set_local (int fd, int local); * associated with the managed filedes "fd". Actually sets the * following: * - * Raw mode if "raw" is nonzero. - * Baudrate to "baud". - * Parity mode to "parity". - * Flow control mode to "fc". - * Enables local mode if "local" is nonzero, dis. otherwise. + * Raw mode if "raw" is nonzero. + * Baudrate to "baud". + * Parity mode to "parity". + * Flow control mode to "fc". + * Enables local mode if "local" is nonzero, dis. otherwise. * Enables HUP-on-close if "hupcl" is nonzero, dis. otherwise * * The effective settings of the device are not affected by this @@ -520,13 +520,13 @@ int term_set_local (int fd, int local); * filedes to the framework, and following this it fails, then it will * remove the filedes before returning. */ -int term_set (int fd, - int raw, - int baud, - enum parity_e parity, - int databits, int stopbits, - enum flowcntrl_e fc, - int local, int hupcl); +int term_set (int fd, + int raw, + int baud, + enum parity_e parity, + int databits, int stopbits, + enum flowcntrl_e fc, + int local, int hupcl); /* F term_get_baudrate * @@ -638,7 +638,7 @@ int term_raise_rts (int fd); */ int term_get_mctl (int fd); -/* F term_drain +/* F term_drain * * Drains (flushes) the output queue of the device associated with the * managed filedes "fd". This functions blocks until all the contents @@ -649,7 +649,7 @@ int term_get_mctl (int fd); int term_drain (int fd); /* F term_flush - * + * * Discards all the contents of the input AND output queues of the * device associated with the managed filedes "fd". Although it is * called flush this functions does NOT FLUSHES the terminal diff --git a/termbits2.h b/termbits2.h index 0096793..55cc8f8 100644 --- a/termbits2.h +++ b/termbits2.h @@ -6,7 +6,7 @@ * * by Nick Patavalis (npat@efault.net) * - * ATTENTION: Linux-specific kludge! + * ATTENTION: Linux-specific kludge! * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -21,7 +21,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #ifndef TERMBITS2_H @@ -56,7 +56,7 @@ cannot get the definion of "struct termios2" from the above header files, since this would also bring-in the clashing definition of the kernel version of "struct termios". If you have an idea for a better - way out of this mess, I would REALLY like to hear it. + way out of this mess, I would REALLY like to hear it. I hope that soon GLIBC will pick-up termios2 and all these will be useless. Until then ... diff --git a/termios2.c b/termios2.c index 15e3412..185fd8a 100644 --- a/termios2.c +++ b/termios2.c @@ -5,7 +5,7 @@ * * by Nick Patavalis (npat@efault.net) * - * ATTENTION: Linux-specific kludge! + * ATTENTION: Linux-specific kludge! * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,7 +20,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #if defined(__linux__) && defined(USE_CUSTOM_BAUD) @@ -45,68 +45,68 @@ /* GLIBC termios use an (otherwise unused) bit in c_iflags to internally record the fact that ispeed was set to zero (which is special behavior and means "same as ospeed". We want to clear this - bit before passing c_iflags back to the kernel. See: - - /sysdeps/unix/sysv/linux/speed.c + bit before passing c_iflags back to the kernel. See: + + /sysdeps/unix/sysv/linux/speed.c */ #define IBAUD0 020000000000 int tc2setattr(int fd, int optional_actions, const struct termios *tios) { - struct termios2 t2; - int cmd; + struct termios2 t2; + int cmd; - switch (optional_actions) { - case TCSANOW: - cmd = IOCTL_SETS; - break; - case TCSADRAIN: - cmd = IOCTL_SETSW; - break; - case TCSAFLUSH: - cmd = IOCTL_SETSF; - break; - default: - errno = EINVAL; - return -1; - } + switch (optional_actions) { + case TCSANOW: + cmd = IOCTL_SETS; + break; + case TCSADRAIN: + cmd = IOCTL_SETSW; + break; + case TCSAFLUSH: + cmd = IOCTL_SETSF; + break; + default: + errno = EINVAL; + return -1; + } - t2.c_iflag = tios->c_iflag & ~IBAUD0; - t2.c_oflag = tios->c_oflag; - t2.c_cflag = tios->c_cflag; - t2.c_lflag = tios->c_lflag; - t2.c_line = tios->c_line; - t2.c_ispeed = tios->c_ispeed; - t2.c_ospeed = tios->c_ospeed; - memcpy(&t2.c_cc[0], &tios->c_cc[0], K_NCCS * sizeof (cc_t)); - - return ioctl(fd, cmd, &t2); + t2.c_iflag = tios->c_iflag & ~IBAUD0; + t2.c_oflag = tios->c_oflag; + t2.c_cflag = tios->c_cflag; + t2.c_lflag = tios->c_lflag; + t2.c_line = tios->c_line; + t2.c_ispeed = tios->c_ispeed; + t2.c_ospeed = tios->c_ospeed; + memcpy(&t2.c_cc[0], &tios->c_cc[0], K_NCCS * sizeof (cc_t)); + + return ioctl(fd, cmd, &t2); } int tc2getattr(int fd, struct termios *tios) { - struct termios2 t2; - size_t i; - int r; + struct termios2 t2; + size_t i; + int r; - r = ioctl(fd, IOCTL_GETS, &t2); - if (r < 0) return r; + r = ioctl(fd, IOCTL_GETS, &t2); + if (r < 0) return r; - tios->c_iflag = t2.c_iflag; - tios->c_oflag = t2.c_oflag; - tios->c_cflag = t2.c_cflag; - tios->c_lflag = t2.c_lflag; - tios->c_line = t2.c_line; - tios->c_ispeed = t2.c_ispeed; - tios->c_ospeed = t2.c_ospeed; - memcpy(&tios->c_cc[0], &t2.c_cc[0], K_NCCS * sizeof (cc_t)); - - for (i = K_NCCS; i < NCCS; i++) - tios->c_cc[i] = _POSIX_VDISABLE; + tios->c_iflag = t2.c_iflag; + tios->c_oflag = t2.c_oflag; + tios->c_cflag = t2.c_cflag; + tios->c_lflag = t2.c_lflag; + tios->c_line = t2.c_line; + tios->c_ispeed = t2.c_ispeed; + tios->c_ospeed = t2.c_ospeed; + memcpy(&tios->c_cc[0], &t2.c_cc[0], K_NCCS * sizeof (cc_t)); - return 0; + for (i = K_NCCS; i < NCCS; i++) + tios->c_cc[i] = _POSIX_VDISABLE; + + return 0; } /* The termios2 interface supports separate input and output @@ -124,22 +124,22 @@ tc2getattr(int fd, struct termios *tios) int cf2setispeed(struct termios *tios, speed_t speed) { - if ( (speed & ~CBAUD) != 0 - && (speed < B57600 || speed > __MAX_BAUD) ) { - errno = EINVAL; - return -1; - } - tios->c_ispeed = speed; - tios->c_cflag &= ~((CBAUD | CBAUDEX) << IBSHIFT); - tios->c_cflag |= (speed << IBSHIFT); + if ( (speed & ~CBAUD) != 0 + && (speed < B57600 || speed > __MAX_BAUD) ) { + errno = EINVAL; + return -1; + } + tios->c_ispeed = speed; + tios->c_cflag &= ~((CBAUD | CBAUDEX) << IBSHIFT); + tios->c_cflag |= (speed << IBSHIFT); - return 0; + return 0; } speed_t cf2getispeed(struct termios *tios) { - return (tios->c_cflag >> IBSHIFT) & (CBAUD | CBAUDEX); + return (tios->c_cflag >> IBSHIFT) & (CBAUD | CBAUDEX); } /* Use these to set custom input or output speeds (i.e. speeds that do @@ -148,37 +148,37 @@ cf2getispeed(struct termios *tios) int cf2setospeed_custom(struct termios *tios, int speed) { - if ( speed <= 0 ) { - errno = EINVAL; - return -1; - } - tios->c_cflag &= ~(CBAUD | CBAUDEX); - tios->c_cflag |= BOTHER; - tios->c_ospeed = speed; + if ( speed <= 0 ) { + errno = EINVAL; + return -1; + } + tios->c_cflag &= ~(CBAUD | CBAUDEX); + tios->c_cflag |= BOTHER; + tios->c_ospeed = speed; - return 0; + return 0; } int cf2setispeed_custom(struct termios *tios, int speed) { - if ( speed < 0 ) { - errno = EINVAL; - return -1; - } - if ( speed == 0 ) { - /* Special case: ispeed == 0 means "same as ospeed". Kernel - does this if it sees B0 in the "CIBAUD" field (i.e. in - CBAUD << IBSHIFT) */ - tios->c_cflag &= ~((CBAUD | CBAUDEX) << IBSHIFT); - tios->c_cflag |= (B0 << IBSHIFT); - } else { - tios->c_cflag &= ~((CBAUD | CBAUDEX) << IBSHIFT); - tios->c_cflag |= (BOTHER << IBSHIFT); - tios->c_ispeed = speed; - } + if ( speed < 0 ) { + errno = EINVAL; + return -1; + } + if ( speed == 0 ) { + /* Special case: ispeed == 0 means "same as ospeed". Kernel + does this if it sees B0 in the "CIBAUD" field (i.e. in + CBAUD << IBSHIFT) */ + tios->c_cflag &= ~((CBAUD | CBAUDEX) << IBSHIFT); + tios->c_cflag |= (B0 << IBSHIFT); + } else { + tios->c_cflag &= ~((CBAUD | CBAUDEX) << IBSHIFT); + tios->c_cflag |= (BOTHER << IBSHIFT); + tios->c_ispeed = speed; + } - return 0; + return 0; } /***************************************************************************/ diff --git a/termios2.h b/termios2.h index e20013d..e13b0e3 100644 --- a/termios2.h +++ b/termios2.h @@ -5,7 +5,7 @@ * * by Nick Patavalis (npat@efault.net) * - * ATTENTION: Linux-specific kludge! + * ATTENTION: Linux-specific kludge! * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,7 +20,7 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * USA */ #ifndef TERMIOS2_H diff --git a/termios2.txt b/termios2.txt index f7e0e24..124673b 100644 --- a/termios2.txt +++ b/termios2.txt @@ -7,7 +7,7 @@ through the "new" "termios2" terminal-attributes structure, and the respective ioctls: TCSETS2, TCSETSW2, TCSETSF2, and TCGETS2. The "termios2" structure is defined in: - + /arch//include/uapi/asm/termbits.h or /include/uapi/asm-generic/termbits.h