[rtems-libbsd commit] Adding stty command files from FREEBSD tree

Sebastian Huber sebh at rtems.org
Thu May 11 09:23:18 UTC 2017


Module:    rtems-libbsd
Branch:    master
Commit:    67cd18e85ab06ecbefcb08185ec90c0bebe54d6a
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=67cd18e85ab06ecbefcb08185ec90c0bebe54d6a

Author:    Kevin Kirspel <kevin-kirspel at idexx.com>
Date:      Thu May  4 08:27:55 2017 -0400

Adding stty command files from FREEBSD tree

---

 freebsd/bin/stty/cchar.c  | 142 ++++++++++++++++++++++
 freebsd/bin/stty/extern.h |  45 +++++++
 freebsd/bin/stty/gfmt.c   | 129 ++++++++++++++++++++
 freebsd/bin/stty/key.c    | 296 ++++++++++++++++++++++++++++++++++++++++++++++
 freebsd/bin/stty/modes.c  | 248 ++++++++++++++++++++++++++++++++++++++
 freebsd/bin/stty/print.c  | 283 ++++++++++++++++++++++++++++++++++++++++++++
 freebsd/bin/stty/stty.c   | 169 ++++++++++++++++++++++++++
 freebsd/bin/stty/stty.h   |  55 +++++++++
 freebsd/bin/stty/util.c   |  66 +++++++++++
 9 files changed, 1433 insertions(+)

diff --git a/freebsd/bin/stty/cchar.c b/freebsd/bin/stty/cchar.c
new file mode 100644
index 0000000..4a925da
--- /dev/null
+++ b/freebsd/bin/stty/cchar.c
@@ -0,0 +1,142 @@
+#include <machine/rtems-bsd-user-space.h>
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)cchar.c	8.5 (Berkeley) 4/2/94";
+#endif
+#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "stty.h"
+#include "extern.h"
+
+static int c_cchar(const void *, const void *);
+
+/*
+ * Special control characters.
+ *
+ * Cchars1 are the standard names, cchars2 are the old aliases.
+ * The first are displayed, but both are recognized on the
+ * command line.
+ */
+struct cchar cchars1[] = {
+	{ "discard",	VDISCARD, 	CDISCARD },
+	{ "dsusp", 	VDSUSP,		CDSUSP },
+	{ "eof",	VEOF,		CEOF },
+	{ "eol",	VEOL,		CEOL },
+	{ "eol2",	VEOL2,		CEOL },
+	{ "erase",	VERASE,		CERASE },
+	{ "erase2",	VERASE2,	CERASE2 },
+	{ "intr",	VINTR,		CINTR },
+	{ "kill",	VKILL,		CKILL },
+	{ "lnext",	VLNEXT,		CLNEXT },
+	{ "min",	VMIN,		CMIN },
+	{ "quit",	VQUIT,		CQUIT },
+	{ "reprint",	VREPRINT, 	CREPRINT },
+	{ "start",	VSTART,		CSTART },
+	{ "status",	VSTATUS, 	CSTATUS },
+	{ "stop",	VSTOP,		CSTOP },
+	{ "susp",	VSUSP,		CSUSP },
+	{ "time",	VTIME,		CTIME },
+	{ "werase",	VWERASE,	CWERASE },
+	{ NULL,		0,		0},
+};
+
+struct cchar cchars2[] = {
+	{ "brk",	VEOL,		CEOL },
+	{ "flush",	VDISCARD, 	CDISCARD },
+	{ "rprnt",	VREPRINT, 	CREPRINT },
+	{ NULL,		0,		0 },
+};
+
+static int
+c_cchar(const void *a, const void *b)
+{
+
+        return (strcmp(((const struct cchar *)a)->name, ((const struct cchar *)b)->name));
+}
+
+int
+csearch(char ***argvp, struct info *ip)
+{
+	struct cchar *cp, tmp;
+	long val;
+	char *arg, *ep, *name;
+
+	name = **argvp;
+
+	tmp.name = name;
+	if (!(cp = (struct cchar *)bsearch(&tmp, cchars1,
+	    sizeof(cchars1)/sizeof(struct cchar) - 1, sizeof(struct cchar),
+	    c_cchar)) && !(cp = (struct cchar *)bsearch(&tmp, cchars2,
+	    sizeof(cchars2)/sizeof(struct cchar) - 1, sizeof(struct cchar),
+	    c_cchar)))
+		return (0);
+
+	arg = *++*argvp;
+	if (!arg) {
+		warnx("option requires an argument -- %s", name);
+		usage();
+	}
+
+#define CHK(s)  (*arg == s[0] && !strcmp(arg, s))
+	if (CHK("undef") || CHK("<undef>"))
+		ip->t.c_cc[cp->sub] = _POSIX_VDISABLE;
+	else if (cp->sub == VMIN || cp->sub == VTIME) {
+		val = strtol(arg, &ep, 10);
+		if (val > UCHAR_MAX) {
+			warnx("maximum option value is %d -- %s",
+			    UCHAR_MAX, name);
+			usage();
+		}
+		if (*ep != '\0') {
+			warnx("option requires a numeric argument -- %s", name);
+			usage();
+		}
+		ip->t.c_cc[cp->sub] = val;
+	} else if (arg[0] == '^')
+		ip->t.c_cc[cp->sub] = (arg[1] == '?') ? 0177 :
+		    (arg[1] == '-') ? _POSIX_VDISABLE : arg[1] & 037;
+	else
+		ip->t.c_cc[cp->sub] = arg[0];
+	ip->set = 1;
+	return (1);
+}
diff --git a/freebsd/bin/stty/extern.h b/freebsd/bin/stty/extern.h
new file mode 100644
index 0000000..88b5889
--- /dev/null
+++ b/freebsd/bin/stty/extern.h
@@ -0,0 +1,45 @@
+/*-
+ * Copyright (c) 1991, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)extern.h	8.1 (Berkeley) 5/31/93
+ * $FreeBSD$
+ */
+
+int	c_cchars(const void *, const void *);
+int	c_modes(const void *, const void *);
+int	csearch(char ***, struct info *);
+void	checkredirect(void);
+void	gprint(struct termios *, struct winsize *, int);
+void	gread(struct termios *, char *);
+int	ksearch(char ***, struct info *);
+int	msearch(char ***, struct info *);
+void	optlist(void);
+void	print(struct termios *, struct winsize *, int, enum FMT);
+void	usage(void) __dead2;
+
+extern struct cchar cchars1[], cchars2[];
diff --git a/freebsd/bin/stty/gfmt.c b/freebsd/bin/stty/gfmt.c
new file mode 100644
index 0000000..57adc1e
--- /dev/null
+++ b/freebsd/bin/stty/gfmt.c
@@ -0,0 +1,129 @@
+#include <machine/rtems-bsd-user-space.h>
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)gfmt.c	8.6 (Berkeley) 4/2/94";
+#endif
+#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "stty.h"
+#include "extern.h"
+
+static void gerr(const char *s) __dead2;
+
+static void
+gerr(const char *s)
+{
+	if (s)
+		errx(1, "illegal gfmt1 option -- %s", s);
+	else
+		errx(1, "illegal gfmt1 option");
+}
+
+void
+gprint(struct termios *tp, struct winsize *wp __unused, int ldisc __unused)
+{
+	struct cchar *cp;
+
+	(void)printf("gfmt1:cflag=%lx:iflag=%lx:lflag=%lx:oflag=%lx:",
+	    (u_long)tp->c_cflag, (u_long)tp->c_iflag, (u_long)tp->c_lflag,
+	    (u_long)tp->c_oflag);
+	for (cp = cchars1; cp->name; ++cp)
+		(void)printf("%s=%x:", cp->name, tp->c_cc[cp->sub]);
+	(void)printf("ispeed=%lu:ospeed=%lu\n",
+	    (u_long)cfgetispeed(tp), (u_long)cfgetospeed(tp));
+}
+
+void
+gread(struct termios *tp, char *s)
+{
+	struct cchar *cp;
+	char *ep, *p;
+	long tmp;
+
+	if ((s = strchr(s, ':')) == NULL)
+		gerr(NULL);
+	for (++s; s != NULL;) {
+		p = strsep(&s, ":\0");
+		if (!p || !*p)
+			break;
+		if (!(ep = strchr(p, '=')))
+			gerr(p);
+		*ep++ = '\0';
+		(void)sscanf(ep, "%lx", (u_long *)&tmp);
+
+#define	CHK(s)	(*p == s[0] && !strcmp(p, s))
+		if (CHK("cflag")) {
+			tp->c_cflag = tmp;
+			continue;
+		}
+		if (CHK("iflag")) {
+			tp->c_iflag = tmp;
+			continue;
+		}
+		if (CHK("ispeed")) {
+			(void)sscanf(ep, "%ld", &tmp);
+			tp->c_ispeed = tmp;
+			continue;
+		}
+		if (CHK("lflag")) {
+			tp->c_lflag = tmp;
+			continue;
+		}
+		if (CHK("oflag")) {
+			tp->c_oflag = tmp;
+			continue;
+		}
+		if (CHK("ospeed")) {
+			(void)sscanf(ep, "%ld", &tmp);
+			tp->c_ospeed = tmp;
+			continue;
+		}
+		for (cp = cchars1; cp->name != NULL; ++cp)
+			if (CHK(cp->name)) {
+				if (cp->sub == VMIN || cp->sub == VTIME)
+					(void)sscanf(ep, "%ld", &tmp);
+				tp->c_cc[cp->sub] = tmp;
+				break;
+			}
+		if (cp->name == NULL)
+			gerr(p);
+	}
+}
diff --git a/freebsd/bin/stty/key.c b/freebsd/bin/stty/key.c
new file mode 100644
index 0000000..1023fa6
--- /dev/null
+++ b/freebsd/bin/stty/key.c
@@ -0,0 +1,296 @@
+#include <machine/rtems-bsd-user-space.h>
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)key.c	8.3 (Berkeley) 4/2/94";
+#endif
+#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "stty.h"
+#include "extern.h"
+
+__BEGIN_DECLS
+static int c_key(const void *, const void *);
+void	f_all(struct info *);
+void	f_cbreak(struct info *);
+void	f_columns(struct info *);
+void	f_dec(struct info *);
+void	f_ek(struct info *);
+void	f_everything(struct info *);
+void	f_extproc(struct info *);
+void	f_ispeed(struct info *);
+void	f_nl(struct info *);
+void	f_ospeed(struct info *);
+void	f_raw(struct info *);
+void	f_rows(struct info *);
+void	f_sane(struct info *);
+void	f_size(struct info *);
+void	f_speed(struct info *);
+void	f_tty(struct info *);
+__END_DECLS
+
+static struct key {
+	const char *name;			/* name */
+	void (*f)(struct info *);		/* function */
+#define	F_NEEDARG	0x01			/* needs an argument */
+#define	F_OFFOK		0x02			/* can turn off */
+	int flags;
+} keys[] = {
+	{ "all",	f_all,		0 },
+	{ "cbreak",	f_cbreak,	F_OFFOK },
+	{ "cols",	f_columns,	F_NEEDARG },
+	{ "columns",	f_columns,	F_NEEDARG },
+	{ "cooked", 	f_sane,		0 },
+	{ "dec",	f_dec,		0 },
+	{ "ek",		f_ek,		0 },
+	{ "everything",	f_everything,	0 },
+	{ "extproc",	f_extproc,	F_OFFOK },
+	{ "ispeed",	f_ispeed,	F_NEEDARG },
+	{ "new",	f_tty,		0 },
+	{ "nl",		f_nl,		F_OFFOK },
+	{ "old",	f_tty,		0 },
+	{ "ospeed",	f_ospeed,	F_NEEDARG },
+	{ "raw",	f_raw,		F_OFFOK },
+	{ "rows",	f_rows,		F_NEEDARG },
+	{ "sane",	f_sane,		0 },
+	{ "size",	f_size,		0 },
+	{ "speed",	f_speed,	0 },
+	{ "tty",	f_tty,		0 },
+};
+
+static int
+c_key(const void *a, const void *b)
+{
+
+        return (strcmp(((const struct key *)a)->name, ((const struct key *)b)->name));
+}
+
+int
+ksearch(char ***argvp, struct info *ip)
+{
+	char *name;
+	struct key *kp, tmp;
+
+	name = **argvp;
+	if (*name == '-') {
+		ip->off = 1;
+		++name;
+	} else
+		ip->off = 0;
+
+	tmp.name = name;
+	if (!(kp = (struct key *)bsearch(&tmp, keys,
+	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
+		return (0);
+	if (!(kp->flags & F_OFFOK) && ip->off) {
+		warnx("illegal option -- -%s", name);
+		usage();
+	}
+	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
+		warnx("option requires an argument -- %s", name);
+		usage();
+	}
+	kp->f(ip);
+	return (1);
+}
+
+void
+f_all(struct info *ip)
+{
+	print(&ip->t, &ip->win, ip->ldisc, BSD);
+}
+
+void
+f_cbreak(struct info *ip)
+{
+
+	if (ip->off)
+		f_sane(ip);
+	else {
+		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
+		ip->t.c_oflag |= OPOST;
+		ip->t.c_lflag |= ISIG|IEXTEN;
+		ip->t.c_lflag &= ~ICANON;
+		ip->set = 1;
+	}
+}
+
+void
+f_columns(struct info *ip)
+{
+
+	ip->win.ws_col = atoi(ip->arg);
+	ip->wset = 1;
+}
+
+void
+f_dec(struct info *ip)
+{
+
+	ip->t.c_cc[VERASE] = (u_char)0177;
+	ip->t.c_cc[VKILL] = CTRL('u');
+	ip->t.c_cc[VINTR] = CTRL('c');
+	ip->t.c_lflag &= ~ECHOPRT;
+	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
+	ip->t.c_iflag &= ~IXANY;
+	ip->set = 1;
+}
+
+void
+f_ek(struct info *ip)
+{
+
+	ip->t.c_cc[VERASE] = CERASE;
+	ip->t.c_cc[VKILL] = CKILL;
+	ip->set = 1;
+}
+
+void
+f_everything(struct info *ip)
+{
+
+	print(&ip->t, &ip->win, ip->ldisc, BSD);
+}
+
+void
+f_extproc(struct info *ip)
+{
+
+	if (ip->off) {
+		int tmp = 0;
+		(void)ioctl(ip->fd, TIOCEXT, &tmp);
+	} else {
+		int tmp = 1;
+		(void)ioctl(ip->fd, TIOCEXT, &tmp);
+	}
+}
+
+void
+f_ispeed(struct info *ip)
+{
+
+	cfsetispeed(&ip->t, (speed_t)atoi(ip->arg));
+	ip->set = 1;
+}
+
+void
+f_nl(struct info *ip)
+{
+
+	if (ip->off) {
+		ip->t.c_iflag |= ICRNL;
+		ip->t.c_oflag |= ONLCR;
+	} else {
+		ip->t.c_iflag &= ~ICRNL;
+		ip->t.c_oflag &= ~ONLCR;
+	}
+	ip->set = 1;
+}
+
+void
+f_ospeed(struct info *ip)
+{
+
+	cfsetospeed(&ip->t, (speed_t)atoi(ip->arg));
+	ip->set = 1;
+}
+
+void
+f_raw(struct info *ip)
+{
+
+	if (ip->off)
+		f_sane(ip);
+	else {
+		cfmakeraw(&ip->t);
+		ip->t.c_cflag &= ~(CSIZE|PARENB);
+		ip->t.c_cflag |= CS8;
+		ip->set = 1;
+	}
+}
+
+void
+f_rows(struct info *ip)
+{
+
+	ip->win.ws_row = atoi(ip->arg);
+	ip->wset = 1;
+}
+
+void
+f_sane(struct info *ip)
+{
+	struct termios def;
+
+	cfmakesane(&def);
+	ip->t.c_cflag = def.c_cflag | (ip->t.c_cflag & CLOCAL);
+	ip->t.c_iflag = def.c_iflag;
+	/* preserve user-preference flags in lflag */
+#define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
+	ip->t.c_lflag = def.c_lflag | (ip->t.c_lflag & LKEEP);
+	ip->t.c_oflag = def.c_oflag;
+	ip->set = 1;
+}
+
+void
+f_size(struct info *ip)
+{
+
+	(void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
+}
+
+void
+f_speed(struct info *ip)
+{
+
+	(void)printf("%lu\n", (u_long)cfgetospeed(&ip->t));
+}
+
+void
+f_tty(struct info *ip)
+{
+	int tmp;
+
+	tmp = TTYDISC;
+	if (ioctl(ip->fd, TIOCSETD, &tmp) < 0)
+		err(1, "TIOCSETD");
+}
diff --git a/freebsd/bin/stty/modes.c b/freebsd/bin/stty/modes.c
new file mode 100644
index 0000000..79a0ed8
--- /dev/null
+++ b/freebsd/bin/stty/modes.c
@@ -0,0 +1,248 @@
+#include <machine/rtems-bsd-user-space.h>
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)modes.c	8.3 (Berkeley) 4/2/94";
+#endif
+#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <stddef.h>
+#include <string.h>
+#include "stty.h"
+
+int msearch(char ***, struct info *);
+
+struct modes {
+	const char *name;
+	long set;
+	long unset;
+};
+
+/*
+ * The code in optlist() depends on minus options following regular
+ * options, i.e. "foo" must immediately precede "-foo".
+ */
+static const struct modes cmodes[] = {
+	{ "cs5",	CS5, CSIZE },
+	{ "cs6",	CS6, CSIZE },
+	{ "cs7",	CS7, CSIZE },
+	{ "cs8",	CS8, CSIZE },
+	{ "cstopb",	CSTOPB, 0 },
+	{ "-cstopb",	0, CSTOPB },
+	{ "cread",	CREAD, 0 },
+	{ "-cread",	0, CREAD },
+	{ "parenb",	PARENB, 0 },
+	{ "-parenb",	0, PARENB },
+	{ "parodd",	PARODD, 0 },
+	{ "-parodd",	0, PARODD },
+	{ "parity",	PARENB | CS7, PARODD | CSIZE },
+	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
+	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
+	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
+	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
+	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
+	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
+	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
+	{ "hupcl",	HUPCL, 0 },
+	{ "-hupcl",	0, HUPCL },
+	{ "hup",	HUPCL, 0 },
+	{ "-hup",	0, HUPCL },
+	{ "clocal",	CLOCAL, 0 },
+	{ "-clocal",	0, CLOCAL },
+	{ "crtscts",	CRTSCTS, 0 },
+	{ "-crtscts",	0, CRTSCTS },
+	{ "ctsflow",	CCTS_OFLOW, 0 },
+	{ "-ctsflow",	0, CCTS_OFLOW },
+	{ "dsrflow",	CDSR_OFLOW, 0 },
+	{ "-dsrflow",	0, CDSR_OFLOW },
+	{ "dtrflow",	CDTR_IFLOW, 0 },
+	{ "-dtrflow",	0, CDTR_IFLOW },
+	{ "rtsflow",	CRTS_IFLOW, 0 },
+	{ "-rtsflow",	0, CRTS_IFLOW },
+	{ "mdmbuf",	MDMBUF, 0 },
+	{ "-mdmbuf",	0, MDMBUF },
+	{ NULL,		0, 0 },
+};
+
+static const struct modes imodes[] = {
+	{ "ignbrk",	IGNBRK, 0 },
+	{ "-ignbrk",	0, IGNBRK },
+	{ "brkint",	BRKINT, 0 },
+	{ "-brkint",	0, BRKINT },
+	{ "ignpar",	IGNPAR, 0 },
+	{ "-ignpar",	0, IGNPAR },
+	{ "parmrk",	PARMRK, 0 },
+	{ "-parmrk",	0, PARMRK },
+	{ "inpck",	INPCK, 0 },
+	{ "-inpck",	0, INPCK },
+	{ "istrip",	ISTRIP, 0 },
+	{ "-istrip",	0, ISTRIP },
+	{ "inlcr",	INLCR, 0 },
+	{ "-inlcr",	0, INLCR },
+	{ "igncr",	IGNCR, 0 },
+	{ "-igncr",	0, IGNCR },
+	{ "icrnl",	ICRNL, 0 },
+	{ "-icrnl",	0, ICRNL },
+	{ "ixon",	IXON, 0 },
+	{ "-ixon",	0, IXON },
+	{ "flow",	IXON, 0 },
+	{ "-flow",	0, IXON },
+	{ "ixoff",	IXOFF, 0 },
+	{ "-ixoff",	0, IXOFF },
+	{ "tandem",	IXOFF, 0 },
+	{ "-tandem",	0, IXOFF },
+	{ "ixany",	IXANY, 0 },
+	{ "-ixany",	0, IXANY },
+	{ "decctlq",	0, IXANY },
+	{ "-decctlq",	IXANY, 0 },
+	{ "imaxbel",	IMAXBEL, 0 },
+	{ "-imaxbel",	0, IMAXBEL },
+	{ NULL,		0, 0 },
+};
+
+static const struct modes lmodes[] = {
+	{ "echo",	ECHO, 0 },
+	{ "-echo",	0, ECHO },
+	{ "echoe",	ECHOE, 0 },
+	{ "-echoe",	0, ECHOE },
+	{ "crterase",	ECHOE, 0 },
+	{ "-crterase",	0, ECHOE },
+	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
+	{ "-crtbs",	0, ECHOE },
+	{ "echok",	ECHOK, 0 },
+	{ "-echok",	0, ECHOK },
+	{ "echoke",	ECHOKE, 0 },
+	{ "-echoke",	0, ECHOKE },
+	{ "crtkill",	ECHOKE, 0 },
+	{ "-crtkill",	0, ECHOKE },
+	{ "altwerase",	ALTWERASE, 0 },
+	{ "-altwerase",	0, ALTWERASE },
+	{ "iexten",	IEXTEN, 0 },
+	{ "-iexten",	0, IEXTEN },
+	{ "echonl",	ECHONL, 0 },
+	{ "-echonl",	0, ECHONL },
+	{ "echoctl",	ECHOCTL, 0 },
+	{ "-echoctl",	0, ECHOCTL },
+	{ "ctlecho",	ECHOCTL, 0 },
+	{ "-ctlecho",	0, ECHOCTL },
+	{ "echoprt",	ECHOPRT, 0 },
+	{ "-echoprt",	0, ECHOPRT },
+	{ "prterase",	ECHOPRT, 0 },
+	{ "-prterase",	0, ECHOPRT },
+	{ "isig",	ISIG, 0 },
+	{ "-isig",	0, ISIG },
+	{ "icanon",	ICANON, 0 },
+	{ "-icanon",	0, ICANON },
+	{ "noflsh",	NOFLSH, 0 },
+	{ "-noflsh",	0, NOFLSH },
+	{ "tostop",	TOSTOP, 0 },
+	{ "-tostop",	0, TOSTOP },
+	{ "flusho",	FLUSHO, 0 },
+	{ "-flusho",	0, FLUSHO },
+	{ "pendin",	PENDIN, 0 },
+	{ "-pendin",	0, PENDIN },
+	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
+	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
+	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
+	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
+	{ "nokerninfo",	NOKERNINFO, 0 },
+	{ "-nokerninfo",0, NOKERNINFO },
+	{ "kerninfo",	0, NOKERNINFO },
+	{ "-kerninfo",	NOKERNINFO, 0 },
+	{ NULL,		0, 0 },
+};
+
+static const struct modes omodes[] = {
+	{ "opost",	OPOST, 0 },
+	{ "-opost",	0, OPOST },
+	{ "litout",	0, OPOST },
+	{ "-litout",	OPOST, 0 },
+	{ "onlcr",	ONLCR, 0 },
+	{ "-onlcr",	0, ONLCR },
+	{ "ocrnl",	OCRNL, 0 },
+	{ "-ocrnl",	0, OCRNL },
+	{ "tabs",	TAB0, TABDLY },		/* "preserve" tabs */
+	{ "-tabs",	TAB3, TABDLY },
+	{ "oxtabs",	TAB3, TABDLY },
+	{ "-oxtabs",	TAB0, TABDLY },
+	{ "tab0",	TAB0, TABDLY },
+	{ "tab3",	TAB3, TABDLY },
+	{ "onocr",	ONOCR, 0 },
+	{ "-onocr",	0, ONOCR },
+	{ "onlret",	ONLRET, 0 },
+	{ "-onlret",	0, ONLRET },
+	{ NULL,		0, 0 },
+};
+
+#define	CHK(s)	(*name == s[0] && !strcmp(name, s))
+
+int
+msearch(char ***argvp, struct info *ip)
+{
+	const struct modes *mp;
+	char *name;
+
+	name = **argvp;
+
+	for (mp = cmodes; mp->name; ++mp)
+		if (CHK(mp->name)) {
+			ip->t.c_cflag &= ~mp->unset;
+			ip->t.c_cflag |= mp->set;
+			ip->set = 1;
+			return (1);
+		}
+	for (mp = imodes; mp->name; ++mp)
+		if (CHK(mp->name)) {
+			ip->t.c_iflag &= ~mp->unset;
+			ip->t.c_iflag |= mp->set;
+			ip->set = 1;
+			return (1);
+		}
+	for (mp = lmodes; mp->name; ++mp)
+		if (CHK(mp->name)) {
+			ip->t.c_lflag &= ~mp->unset;
+			ip->t.c_lflag |= mp->set;
+			ip->set = 1;
+			return (1);
+		}
+	for (mp = omodes; mp->name; ++mp)
+		if (CHK(mp->name)) {
+			ip->t.c_oflag &= ~mp->unset;
+			ip->t.c_oflag |= mp->set;
+			ip->set = 1;
+			return (1);
+		}
+	return (0);
+}
diff --git a/freebsd/bin/stty/print.c b/freebsd/bin/stty/print.c
new file mode 100644
index 0000000..770c5b5
--- /dev/null
+++ b/freebsd/bin/stty/print.c
@@ -0,0 +1,283 @@
+#include <machine/rtems-bsd-user-space.h>
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
+#endif
+#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "stty.h"
+#include "extern.h"
+
+static void  binit(const char *);
+static void  bput(const char *);
+static const char *ccval(struct cchar *, int);
+
+void
+print(struct termios *tp, struct winsize *wp, int ldisc, enum FMT fmt)
+{
+	struct cchar *p;
+	long tmp;
+	u_char *cc;
+	int cnt, ispeed, ospeed;
+	char buf1[100], buf2[100];
+
+	cnt = 0;
+
+	/* Line discipline. */
+	if (ldisc != TTYDISC) {
+		switch(ldisc) {
+		case SLIPDISC:
+			cnt += printf("slip disc; ");
+			break;
+		case PPPDISC:
+			cnt += printf("ppp disc; ");
+			break;
+		default:
+			cnt += printf("#%d disc; ", ldisc);
+			break;
+		}
+	}
+
+	/* Line speed. */
+	ispeed = cfgetispeed(tp);
+	ospeed = cfgetospeed(tp);
+	if (ispeed != ospeed)
+		cnt +=
+		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
+	else
+		cnt += printf("speed %d baud;", ispeed);
+	if (fmt >= BSD)
+		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
+	if (cnt)
+		(void)printf("\n");
+
+#define	on(f)	((tmp & (f)) != 0)
+#define put(n, f, d) \
+	if (fmt >= BSD || on(f) != (d)) \
+		bput((n) + on(f));
+
+	/* "local" flags */
+	tmp = tp->c_lflag;
+	binit("lflags");
+	put("-icanon", ICANON, 1);
+	put("-isig", ISIG, 1);
+	put("-iexten", IEXTEN, 1);
+	put("-echo", ECHO, 1);
+	put("-echoe", ECHOE, 0);
+	put("-echok", ECHOK, 0);
+	put("-echoke", ECHOKE, 0);
+	put("-echonl", ECHONL, 0);
+	put("-echoctl", ECHOCTL, 0);
+	put("-echoprt", ECHOPRT, 0);
+	put("-altwerase", ALTWERASE, 0);
+	put("-noflsh", NOFLSH, 0);
+	put("-tostop", TOSTOP, 0);
+	put("-flusho", FLUSHO, 0);
+	put("-pendin", PENDIN, 0);
+	put("-nokerninfo", NOKERNINFO, 0);
+	put("-extproc", EXTPROC, 0);
+
+	/* input flags */
+	tmp = tp->c_iflag;
+	binit("iflags");
+	put("-istrip", ISTRIP, 0);
+	put("-icrnl", ICRNL, 1);
+	put("-inlcr", INLCR, 0);
+	put("-igncr", IGNCR, 0);
+	put("-ixon", IXON, 1);
+	put("-ixoff", IXOFF, 0);
+	put("-ixany", IXANY, 1);
+	put("-imaxbel", IMAXBEL, 1);
+	put("-ignbrk", IGNBRK, 0);
+	put("-brkint", BRKINT, 1);
+	put("-inpck", INPCK, 0);
+	put("-ignpar", IGNPAR, 0);
+	put("-parmrk", PARMRK, 0);
+
+	/* output flags */
+	tmp = tp->c_oflag;
+	binit("oflags");
+	put("-opost", OPOST, 1);
+	put("-onlcr", ONLCR, 1);
+	put("-ocrnl", OCRNL, 0);
+	switch(tmp&TABDLY) {
+	case TAB0:
+		bput("tab0");
+		break;
+	case TAB3:
+		bput("tab3");
+		break;
+	}
+	put("-onocr", ONOCR, 0);
+	put("-onlret", ONLRET, 0);
+
+	/* control flags (hardware state) */
+	tmp = tp->c_cflag;
+	binit("cflags");
+	put("-cread", CREAD, 1);
+	switch(tmp&CSIZE) {
+	case CS5:
+		bput("cs5");
+		break;
+	case CS6:
+		bput("cs6");
+		break;
+	case CS7:
+		bput("cs7");
+		break;
+	case CS8:
+		bput("cs8");
+		break;
+	}
+	bput("-parenb" + on(PARENB));
+	put("-parodd", PARODD, 0);
+	put("-hupcl", HUPCL, 1);
+	put("-clocal", CLOCAL, 0);
+	put("-cstopb", CSTOPB, 0);
+	switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) {
+	case CCTS_OFLOW:
+		bput("ctsflow");
+		break;
+	case CRTS_IFLOW:
+		bput("rtsflow");
+		break;
+	default:
+		put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0);
+		break;
+	}
+	put("-dsrflow", CDSR_OFLOW, 0);
+	put("-dtrflow", CDTR_IFLOW, 0);
+	put("-mdmbuf", MDMBUF, 0);	/* XXX mdmbuf ==  dtrflow */
+
+	/* special control characters */
+	cc = tp->c_cc;
+	if (fmt == POSIX) {
+		binit("cchars");
+		for (p = cchars1; p->name; ++p) {
+			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
+			    p->name, ccval(p, cc[p->sub]));
+			bput(buf1);
+		}
+		binit(NULL);
+	} else {
+		binit(NULL);
+		for (p = cchars1, cnt = 0; p->name; ++p) {
+			if (fmt != BSD && cc[p->sub] == p->def)
+				continue;
+#define	WD	"%-8s"
+			(void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8,
+			    WD, p->name);
+			(void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8,
+			    WD, ccval(p, cc[p->sub]));
+			if (++cnt == LINELENGTH / 8) {
+				cnt = 0;
+				(void)printf("%s\n", buf1);
+				(void)printf("%s\n", buf2);
+			}
+		}
+		if (cnt) {
+			(void)printf("%s\n", buf1);
+			(void)printf("%s\n", buf2);
+		}
+	}
+}
+
+static int col;
+static const char *label;
+
+static void
+binit(const char *lb)
+{
+
+	if (col) {
+		(void)printf("\n");
+		col = 0;
+	}
+	label = lb;
+}
+
+static void
+bput(const char *s)
+{
+
+	if (col == 0) {
+		col = printf("%s: %s", label, s);
+		return;
+	}
+	if ((col + strlen(s)) > LINELENGTH) {
+		(void)printf("\n\t");
+		col = printf("%s", s) + 8;
+		return;
+	}
+	col += printf(" %s", s);
+}
+
+static const char *
+ccval(struct cchar *p, int c)
+{
+	static char buf[5];
+	char *bp;
+
+	if (p->sub == VMIN || p->sub == VTIME) {
+		(void)snprintf(buf, sizeof(buf), "%d", c);
+		return (buf);
+	}
+	if (c == _POSIX_VDISABLE)
+		return ("<undef>");
+	bp = buf;
+	if (c & 0200) {
+		*bp++ = 'M';
+		*bp++ = '-';
+		c &= 0177;
+	}
+	if (c == 0177) {
+		*bp++ = '^';
+		*bp++ = '?';
+	}
+	else if (c < 040) {
+		*bp++ = '^';
+		*bp++ = c + '@';
+	}
+	else
+		*bp++ = c;
+	*bp = '\0';
+	return (buf);
+}
diff --git a/freebsd/bin/stty/stty.c b/freebsd/bin/stty/stty.c
new file mode 100644
index 0000000..f7b9ab5
--- /dev/null
+++ b/freebsd/bin/stty/stty.c
@@ -0,0 +1,169 @@
+#include <machine/rtems-bsd-user-space.h>
+
+/*-
+ * Copyright (c) 1989, 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if 0
+#ifndef lint
+static char const copyright[] =
+"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
+	The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
+#endif /* not lint */
+#endif
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stty.h"
+#include "extern.h"
+
+int
+main(int argc, char *argv[])
+{
+	struct info i;
+	enum FMT fmt;
+	int ch;
+	const char *file, *errstr = NULL;
+
+	fmt = NOTSET;
+	i.fd = STDIN_FILENO;
+	file = "stdin";
+
+	opterr = 0;
+	while (optind < argc &&
+	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
+	    (ch = getopt(argc, argv, "aef:g")) != -1)
+		switch(ch) {
+		case 'a':		/* undocumented: POSIX compatibility */
+			fmt = POSIX;
+			break;
+		case 'e':
+			fmt = BSD;
+			break;
+		case 'f':
+			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
+				err(1, "%s", optarg);
+			file = optarg;
+			break;
+		case 'g':
+			fmt = GFLAG;
+			break;
+		case '?':
+		default:
+			goto args;
+		}
+
+args:	argc -= optind;
+	argv += optind;
+
+	if (tcgetattr(i.fd, &i.t) < 0)
+		errx(1, "%s isn't a terminal", file);
+	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
+		err(1, "TIOCGETD");
+	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
+		warn("TIOCGWINSZ");
+
+	checkredirect();			/* conversion aid */
+
+	switch(fmt) {
+	case NOTSET:
+		if (*argv)
+			break;
+		/* FALLTHROUGH */
+	case BSD:
+	case POSIX:
+		print(&i.t, &i.win, i.ldisc, fmt);
+		break;
+	case GFLAG:
+		gprint(&i.t, &i.win, i.ldisc);
+		break;
+	}
+
+	for (i.set = i.wset = 0; *argv; ++argv) {
+		if (ksearch(&argv, &i))
+			continue;
+
+		if (csearch(&argv, &i))
+			continue;
+
+		if (msearch(&argv, &i))
+			continue;
+
+		if (isdigit(**argv)) {
+			speed_t speed;
+
+			speed = strtonum(*argv, 0, UINT_MAX, &errstr);
+			if (errstr)
+				err(1, "speed");
+			cfsetospeed(&i.t, speed);
+			cfsetispeed(&i.t, speed);
+			i.set = 1;
+			continue;
+		}
+
+		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
+			gread(&i.t, *argv + sizeof("gfmt1") - 1);
+			i.set = 1;
+			continue;
+		}
+
+		warnx("illegal option -- %s", *argv);
+		usage();
+	}
+
+	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
+		err(1, "tcsetattr");
+	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
+		warn("TIOCSWINSZ");
+	exit(0);
+}
+
+void
+usage(void)
+{
+
+	(void)fprintf(stderr,
+	    "usage: stty [-a | -e | -g] [-f file] [arguments]\n");
+	exit (1);
+}
diff --git a/freebsd/bin/stty/stty.h b/freebsd/bin/stty/stty.h
new file mode 100644
index 0000000..d48ccaa
--- /dev/null
+++ b/freebsd/bin/stty/stty.h
@@ -0,0 +1,55 @@
+/*-
+ * Copyright (c) 1991, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)stty.h	8.1 (Berkeley) 5/31/93
+ * $FreeBSD$
+ */
+
+#include <sys/ioctl.h>
+#include <termios.h>
+
+struct info {
+	int fd;					/* file descriptor */
+	int ldisc;				/* line discipline */
+	int off;				/* turn off */
+	int set;				/* need set */
+	int wset;				/* need window set */
+	const char *arg;			/* argument */
+	struct termios t;			/* terminal info */
+	struct winsize win;			/* window info */
+};
+
+struct cchar {
+	const char *name;
+	int sub;
+	u_char def;
+};
+
+enum FMT { NOTSET, GFLAG, BSD, POSIX };
+
+#define	LINELENGTH	72
diff --git a/freebsd/bin/stty/util.c b/freebsd/bin/stty/util.c
new file mode 100644
index 0000000..2cdc7d9
--- /dev/null
+++ b/freebsd/bin/stty/util.c
@@ -0,0 +1,66 @@
+#include <machine/rtems-bsd-user-space.h>
+
+/*-
+ * Copyright (c) 1991, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/2/94";
+#endif
+#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "stty.h"
+#include "extern.h"
+
+/*
+ * Gross, but since we're changing the control descriptor from 1 to 0, most
+ * users will be probably be doing "stty > /dev/sometty" by accident.  If 1
+ * and 2 are both ttys, but not the same, assume that 1 was incorrectly
+ * redirected.
+ */
+void
+checkredirect(void)
+{
+	struct stat sb1, sb2;
+
+	if (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) &&
+	    !fstat(STDOUT_FILENO, &sb1) && !fstat(STDERR_FILENO, &sb2) &&
+	    (sb1.st_rdev != sb2.st_rdev))
+warnx("stdout appears redirected, but stdin is the control descriptor");
+}




More information about the vc mailing list