From 8332044369786552c9863574d789c4c75f28d481 Mon Sep 17 00:00:00 2001 From: espie Date: Wed, 1 Dec 1999 16:22:01 +0000 Subject: [PATCH] Restore TERM capability. Code scavenged from tracker, since it's known to work... removed all special cases, since it's needed only for OpenBSD. Tweaks after some ho@'s comments. --- audio/mpg123/Makefile | 4 +- audio/mpg123/files/term.c | 159 ++++++++++++++++++++++++++++++++++ audio/mpg123/patches/patch-aa | 4 +- 3 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 audio/mpg123/files/term.c diff --git a/audio/mpg123/Makefile b/audio/mpg123/Makefile index cd599b2b469..d30f0fadc7c 100644 --- a/audio/mpg123/Makefile +++ b/audio/mpg123/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.8 1999/12/01 15:26:59 ho Exp $ +# $OpenBSD: Makefile,v 1.9 1999/12/01 16:22:01 espie Exp $ DISTNAME= mpg123-0.59q CATEGORIES= audio @@ -18,6 +18,6 @@ ALL_TARGET= openbsd-i386 .endif post-patch: - @${CP} ${FILESDIR}/audio_openbsd.c ${WRKSRC} + @cp ${FILESDIR}/{audio_openbsd.c,term.c} ${WRKSRC} .include diff --git a/audio/mpg123/files/term.c b/audio/mpg123/files/term.c new file mode 100644 index 00000000000..71a40e0dd15 --- /dev/null +++ b/audio/mpg123/files/term.c @@ -0,0 +1,159 @@ +/* $OpenBSD: term.c,v 1.1 1999/12/01 16:22:01 espie Exp $ */ +/*- + * Copyright (c) 1999 Marc Espie. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Marc Espie for the OpenBSD + * Project. + * + * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT 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 OPENBSD + * PROJECT 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. + */ + +/* Set terminal discipline to non blocking io and such. */ + +#include +#include +#include +#include + +#include +#include +#include + +#include "mpg123.h" + +#ifdef TERM_CONTROL + +static void sane_tty(void); +static void suspend(int sig); +static int run_in_fg(void); +static void if_fg_sane_tty(void); +static int may_getchar(void); +static void set_raw(int); + +static struct termios sanity; +static struct termios *psanity = NULL; + +static int is_fg; + +#ifdef SIGTSTP +static void suspend(int sig) +{ + sane_tty(); + signal(SIGTSTP, SIG_DFL); + kill(0, SIGTSTP); +} +#endif + +static int run_in_fg(void) +{ + pid_t val; + + /* this should work on every unix */ + if (!isatty(fileno(stdin)) || !isatty(fileno(stdout))) + return 0; + + val = tcgetpgrp(fileno(stdin)); + if (val == -1) { + if (errno == ENOTTY) + return 0; + else + return 1; + } + if (val == getpgrp()) + return 1; + else + return 0; +} + +/* if_fg_sane_tty(): + * restore tty modes, _only_ if running in foreground + */ +static void if_fg_sane_tty(void) +{ + if (run_in_fg()) + sane_tty(); +} + + +static void set_raw(int sig) +{ + struct termios zap; + +#ifdef SIGTSTP + signal(SIGTSTP, suspend); +#endif +#ifdef SIGCONT + signal(SIGCONT, set_raw); +#endif + if (run_in_fg()) { + tcgetattr(fileno(stdin), &zap); + zap.c_cc[VMIN] = 0; /* can't work with old */ + zap.c_cc[VTIME] = 0; /* FreeBSD versions */ + zap.c_lflag &= ~(ICANON|ECHO|ECHONL); + tcsetattr(fileno(stdin), TCSADRAIN, &zap); + is_fg = TRUE; + } else + is_fg = FALSE; +} + +/* nonblocking_io(): + * try to setup the keyboard to non blocking io + */ +void term_init(void) +{ + if (!psanity) { + psanity = &sanity; + tcgetattr(fileno(stdin), psanity); + } + set_raw(0); + atexit(if_fg_sane_tty); +} + + +/* sane_tty(): + * restores everything to a sane state before returning to shell */ +static void sane_tty(void) +{ + tcsetattr(fileno(stdin), TCSADRAIN, psanity); +} + +static int may_getchar(void) +{ + char buffer; + + if (run_in_fg() && !is_fg) + set_raw(0); + if (run_in_fg() && read(fileno(stdin), &buffer, 1)) + return buffer; + return EOF; +} + +void term_control(void) +{ + int val = may_getchar(); + + if (val == 'b' || val == 'B') + rd->rewind(rd); +} + +#endif diff --git a/audio/mpg123/patches/patch-aa b/audio/mpg123/patches/patch-aa index ef957cff190..a2557bb10a9 100644 --- a/audio/mpg123/patches/patch-aa +++ b/audio/mpg123/patches/patch-aa @@ -18,14 +18,14 @@ + $(MAKE) CC=cc LDFLAGS= \ + OBJECTS='decode.o dct64.o audio_openbsd.o term.o' \ + CFLAGS='-Wall -ansi -pedantic -O3 -fomit-frame-pointer \ -+ -funroll-all-loops -ffast-math \ ++ -funroll-all-loops -ffast-math -DTERM_CONTROL \ + -DREAL_IS_FLOAT -DUSE_MMAP -DOPENBSD' mpg123-make + +openbsd-i386: + $(MAKE) CC=cc LDFLAGS= \ + OBJECTS='decode_i386.o dct64_i386.o getbits_.o audio_openbsd.o term.o' \ + CFLAGS='-Wall -ansi -pedantic -O4 -m486 -fomit-frame-pointer \ -+ -funroll-all-loops -ffast-math -DROT_I386 \ ++ -funroll-all-loops -ffast-math -DROT_I386 -DTERM_CONTROL \ + -DI386_ASSEM -DREAL_IS_FLOAT -DUSE_MMAP -DOPENBSD' \ mpg123-make