From: Sascha Wildner Date: Sat, 12 Sep 2009 15:37:36 +0000 (+0200) Subject: backgammon(6)/hack(6)/larn(6): replace sgtty with termios X-Git-Tag: v2.4.0~26^2~1 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/a579280ade25ef68ce67cd8432a625d6bce8d3bb backgammon(6)/hack(6)/larn(6): replace sgtty with termios Courtesy of FreeBSD ports. Also does some additional cleanup. Submitted-by: Ulrich Spoerlein Dragonfly-bugs: --- diff --git a/games/backgammon/backgammon/Makefile b/games/backgammon/backgammon/Makefile index 033527ba1c..9500749d0b 100644 --- a/games/backgammon/backgammon/Makefile +++ b/games/backgammon/backgammon/Makefile @@ -3,7 +3,7 @@ # $DragonFly: src/games/backgammon/backgammon/Makefile,v 1.4 2006/10/08 16:22:35 pavalos Exp $ PROG= backgammon -CFLAGS+=-DV7 -I${.CURDIR}/../common_source +CFLAGS+=-I${.CURDIR}/../common_source SRCS= allow.c board.c check.c extra.c fancy.c init.c main.c move.c \ odds.c one.c save.c subs.c table.c text.c version.c MAN= backgammon.6 diff --git a/games/backgammon/backgammon/main.c b/games/backgammon/backgammon/main.c index 4791faf660..3ae7c540e0 100644 --- a/games/backgammon/backgammon/main.c +++ b/games/backgammon/backgammon/main.c @@ -37,7 +37,10 @@ */ #include +#include +#include #include +#include #include #include "back.h" @@ -61,7 +64,7 @@ const char *contin[] = { /* pause message */ }; static const char rules[] = "\nDo you want the rules of the game?"; -static const char noteach[] = "Teachgammon not available!\n\007"; +static const char noteach[] = "Teachgammon not available!\n\a"; static const char need[] = "Do you need instructions for this program?"; static const char askcol[] = "Enter 'r' to play red, 'w' to play white, 'b' to play both:"; @@ -95,14 +98,11 @@ main(int argc, char **argv) bflag = 2; /* default no board */ acnt = 1; /* Nuber of args */ signal (SIGINT,(sig_t)getout); /* trap interrupts */ - if (ioctl(0, TIOCGETP, &tty) == -1) /* get old tty mode */ - errexit ("backgammon(gtty)"); - old = tty.sg_flags; -#ifdef V7 - raw = ((noech = old & ~ECHO) | CBREAK); /* set up modes */ -#else - raw = ((noech = old & ~ECHO) | RAW); /* set up modes */ -#endif + if (tcgetattr (0,&tty) == -1) /* get old tty mode */ + errexit ("backgammon(tcgetattr)"); + old = tty.c_lflag; + raw = ((noech = old & ~ECHO) & ~ICANON); /* set up modes */ + ospeed = cfgetospeed(&tty); /* for termlib */ /* get terminal * capabilities, and @@ -118,8 +118,8 @@ main(int argc, char **argv) getarg (argc, argv); args[acnt] = NULL; if (tflag) { /* clear screen */ - noech &= ~(CRMOD|XTABS); - raw &= ~(CRMOD|XTABS); + noech &= ~(ICRNL|OXTABS); + raw &= ~(ICRNL|OXTABS); clear(); } fixtty (raw); /* go into raw mode */ diff --git a/games/backgammon/common_source/back.h b/games/backgammon/common_source/back.h index 7b946be9b7..4469531965 100644 --- a/games/backgammon/common_source/back.h +++ b/games/backgammon/common_source/back.h @@ -34,7 +34,7 @@ * $DragonFly: src/games/backgammon/common_source/back.h,v 1.2 2006/08/08 16:36:11 pavalos Exp $ */ -#include +#include #include #include @@ -117,7 +117,7 @@ extern const char *const *colorptr; /* color of current player */ extern const char *const *Colorptr; /* color of current player, capitalized */ extern int colen; /* length of color of current player */ -extern struct sgttyb tty; /* tty information buffer */ +extern struct termios tty; /* tty information buffer */ extern int old; /* original tty status */ extern int noech; /* original tty status without echo */ extern int raw; /* raw tty status, no echo */ diff --git a/games/backgammon/common_source/save.c b/games/backgammon/common_source/save.c index 367454877a..3250c7ddc7 100644 --- a/games/backgammon/common_source/save.c +++ b/games/backgammon/common_source/save.c @@ -74,7 +74,7 @@ save(int n) writel (prompt); fs = fname; while ((*fs = readc()) != '\n') { - if (*fs == tty.sg_erase) { + if (*fs == tty.c_cc[2]) { if (fs > fname) { fs--; if (tflag) diff --git a/games/backgammon/common_source/subs.c b/games/backgammon/common_source/subs.c index 5906aa9256..77676bfd16 100644 --- a/games/backgammon/common_source/subs.c +++ b/games/backgammon/common_source/subs.c @@ -36,6 +36,7 @@ */ #include +#include #include #include "back.h" @@ -422,8 +423,8 @@ fixtty(int mode) if (tflag) newpos(); buflush(); - tty.sg_flags = mode; - if (ioctl(0, TIOCSETP, &tty) < 0) + tty.c_lflag = mode; + if (tcsetattr (0,TCSANOW,&tty) < 0) errexit("fixtty"); } diff --git a/games/backgammon/common_source/table.c b/games/backgammon/common_source/table.c index dce7806c4b..6c3944ed2d 100644 --- a/games/backgammon/common_source/table.c +++ b/games/backgammon/common_source/table.c @@ -112,11 +112,11 @@ dochar: } } - if (c == tty.sg_erase && ncin > 0) { + if (c == tty.c_cc[2] && ncin > 0) { if (tflag) curmove (curr,curc-1); else { - if (tty.sg_erase == '\010') + if (tty.c_cc[2] == '\010') writel ("\010 \010"); else writec (cin[ncin-1]); @@ -133,13 +133,13 @@ dochar: goto domove; } - if (c == tty.sg_kill && ncin > 0) { + if (c == tty.c_cc[3] && ncin > 0) { if (tflag) { refresh(); curmove (curr,39); ist = -1; goto domove; - } else if (tty.sg_erase == '\010') { + } else if (tty.c_cc[2] == '\010') { for (j = 0; j < ncin; j++) writel ("\010 \010"); ist = -1; diff --git a/games/backgammon/teachgammon/Makefile b/games/backgammon/teachgammon/Makefile index 19315b670c..41a358fd18 100644 --- a/games/backgammon/teachgammon/Makefile +++ b/games/backgammon/teachgammon/Makefile @@ -2,7 +2,7 @@ # $DragonFly: src/games/backgammon/teachgammon/Makefile,v 1.3 2006/10/08 16:22:35 pavalos Exp $ PROG= teachgammon -CFLAGS+=-DTEACHGAMMON_TEXT -DV7 -I${.CURDIR}/../common_source +CFLAGS+=-DTEACHGAMMON_TEXT -I${.CURDIR}/../common_source SRCS= allow.c board.c check.c data.c fancy.c init.c odds.c one.c save.c \ subs.c table.c teach.c ttext1.c ttext2.c tutor.c DPADD= ${LIBTERMCAP} diff --git a/games/backgammon/teachgammon/teach.c b/games/backgammon/teachgammon/teach.c index 1b2acbaffd..0bb3fc6a8f 100644 --- a/games/backgammon/teachgammon/teach.c +++ b/games/backgammon/teachgammon/teach.c @@ -38,6 +38,8 @@ #include #include +#include +#include #include #include "back.h" #include "tutor.h" @@ -65,19 +67,16 @@ main(int argc, char **argv) acnt = 1; signal (SIGINT,(sig_t)getout); - if (ioctl(0,TIOCGETP,&tty) == -1) /* get old tty mode */ - errexit ("teachgammon(gtty)"); - old = tty.sg_flags; -#ifdef V7 - raw = ((noech = old & ~ECHO) | CBREAK); /* set up modes */ -#else - raw = ((noech = old & ~ECHO) | RAW); /* set up modes */ -#endif + if (tcgetattr (0,&tty) == -1) /* get old tty mode */ + errexit ("teachgammon(tcgetattr)"); + old = tty.c_lflag; + raw = ((noech = old & ~ECHO) & ~ICANON); /* set up modes */ + ospeed = cfgetospeed(&tty); /* for termlib */ tflag = getcaps (getenv ("TERM")); getarg (argc, argv); if (tflag) { - noech &= ~(CRMOD|XTABS); - raw &= ~(CRMOD|XTABS); + noech &= ~(ICRNL|OXTABS); + raw &= ~(ICRNL|OXTABS); clear(); } text (hello); diff --git a/games/hack/config.h b/games/hack/config.h index c11fe3498a..28f589d742 100644 --- a/games/hack/config.h +++ b/games/hack/config.h @@ -11,20 +11,6 @@ #define UNIX /* delete if no fork(), exec() available */ #define CHDIR /* delete if no chdir() available */ -/* - * Some include files are in a different place under SYSV - * BSD SYSV - * - * - * - * Some routines are called differently - * index strchr - * rindex strrchr - * Also, the code for suspend and various ioctls is only given for BSD4.2 - * (I do not have access to a SYSV system.) - */ -#define BSD /* delete this line on System V */ - /* #define STUPID */ /* avoid some complicated expressions if your C compiler chokes on them */ /* #define PYRAMID_BUG */ /* avoid a bug on the Pyramid */ @@ -63,10 +49,7 @@ #define SHELL /* do not delete the '!' command */ - -#ifdef BSD #define SUSPEND /* let ^Z suspend the game */ -#endif /* BSD */ #endif /* UNIX */ #ifdef CHDIR diff --git a/games/hack/hack.h b/games/hack/hack.h index 27cd816424..df9f582c79 100644 --- a/games/hack/hack.h +++ b/games/hack/hack.h @@ -12,10 +12,9 @@ #include #include -#ifndef BSD +/* FIXME */ #define index strchr #define rindex strrchr -#endif /* BSD */ #include "def.objclass.h" diff --git a/games/hack/hack.ioctl.c b/games/hack/hack.ioctl.c index e63465c6a2..7c9aa90ade 100644 --- a/games/hack/hack.ioctl.c +++ b/games/hack/hack.ioctl.c @@ -7,36 +7,22 @@ systems (e.g. MUNIX) the include files and define the same constants, and the C preprocessor complains. */ #include "hack.h" -#ifdef BSD -#include -struct ltchars ltchars, ltchars0; -#else -#include /* also includes part of */ -struct termio termio; -#endif /* BSD */ +#include +struct termios termio; void getioctls(void) { -#ifdef BSD - ioctl(fileno(stdin), (int) TIOCGLTC, (char *) <chars); - ioctl(fileno(stdin), (int) TIOCSLTC, (char *) <chars0); -#else - ioctl(fileno(stdin), (int) TCGETA, &termio); -#endif /* BSD */ + tcgetattr(fileno(stdin), &termio); } void setioctls(void) { -#ifdef BSD - ioctl(fileno(stdin), (int) TIOCSLTC, (char *) <chars); -#else - ioctl(fileno(stdin), (int) TCSETA, &termio); -#endif /* BSD */ + tcsetattr(fileno(stdin), TCSANOW, &termio); } -#ifdef SUSPEND /* implies BSD */ +#ifdef SUSPEND #include int dosuspend(void) diff --git a/games/hack/hack.pager.c b/games/hack/hack.pager.c index bb0eccb96e..b478bbd5f2 100644 --- a/games/hack/hack.pager.c +++ b/games/hack/hack.pager.c @@ -370,12 +370,7 @@ union wait { /* used only for the cast (union wait *) 0 */ }; #else - -#ifdef BSD #include -#else -#include -#endif /* BSD */ #endif /* NOWAITINCLUDE */ bool diff --git a/games/hack/hack.termcap.c b/games/hack/hack.termcap.c index ca06296c49..baf06a5bfa 100644 --- a/games/hack/hack.termcap.c +++ b/games/hack/hack.termcap.c @@ -38,7 +38,8 @@ startup(void) flags.nonull = 1; if((pc = tgetstr(__DECONST(char *, "pc"), &tbufptr))) tcPC = *pc; - if(!(tcBC = tgetstr(__DECONST(char *, "bc"), &tbufptr))) { + if(!(tcBC = tgetstr(__DECONST(char *, "bc"), &tbufptr)) + &&!(tcBC = tgetstr(__DECONST(char *, "le"), &tbufptr))) { if(!tgetflag(__DECONST(char *, "bs"))) error("Terminal must backspace."); tcBC = tbufptr; diff --git a/games/hack/hack.tty.c b/games/hack/hack.tty.c index a1ced1e449..60202ee888 100644 --- a/games/hack/hack.tty.c +++ b/games/hack/hack.tty.c @@ -40,64 +40,19 @@ /* With thanks to the people who sent code for SYSV - hpscdi!jon, arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others. */ -#include "hack.h" - -/* - * The distinctions here are not BSD - rest but rather USG - rest, as - * BSD still has the old sgttyb structure, but SYSV has termio. Thus: - */ -#ifdef BSD -#define V7 -#else -#define USG -#endif /* BSD */ +#include +#include "hack.h" /* * Some systems may have getchar() return EOF for various reasons, and * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs. + * FIXME: is this still valid nowadays? */ -#ifndef BSD #define NR_OF_EOFS 20 -#endif /* BSD */ - - -#ifdef USG - -#include -#define termstruct termio -#define kill_sym c_cc[VKILL] -#define erase_sym c_cc[VERASE] -#define EXTABS TAB3 -#define tabflgs c_oflag -#define echoflgs c_lflag -#define cbrkflgs c_lflag -#define CBRKMASK ICANON -#define CBRKON ! /* reverse condition */ -#define OSPEED(x) ((x).c_cflag & CBAUD) -#define GTTY(x) (ioctl(0, TCGETA, x)) -#define STTY(x) (ioctl(0, TCSETA, x)) /* TCSETAF? TCSETAW? */ - -#else /* V7 */ - -#include -#define termstruct sgttyb -#define kill_sym sg_kill -#define erase_sym sg_erase -#define EXTABS XTABS -#define tabflgs sg_flags -#define echoflgs sg_flags -#define cbrkflgs sg_flags -#define CBRKMASK CBREAK -#define CBRKON /* empty */ -#define OSPEED(x) (x).sg_ospeed -#define GTTY(x) (ioctl(0, TIOCGETP, x)) -#define STTY(x) (ioctl(0, TIOCSETP, x)) - -#endif /* USG */ static char erase_char, kill_char; static boolean settty_needed = FALSE; -struct termstruct inittyb, curttyb; +struct termios inittyb, curttyb; static void setctty(void); @@ -109,16 +64,16 @@ static void setctty(void); void gettty(void) { - if(GTTY(&inittyb) < 0) + if(tcgetattr(fileno(stdin),&inittyb) < 0) perror("Hack (gettty)"); curttyb = inittyb; - erase_char = inittyb.erase_sym; - kill_char = inittyb.kill_sym; + erase_char = inittyb.c_cc[VERASE]; + kill_char = inittyb.c_cc[VKILL]; getioctls(); /* do not expand tabs - they might be needed inside a cm sequence */ - if(curttyb.tabflgs & EXTABS) { - curttyb.tabflgs &= ~EXTABS; + if(curttyb.c_oflag & OXTABS) { + curttyb.c_oflag &= ~OXTABS; setctty(); } settty_needed = TRUE; @@ -132,41 +87,39 @@ settty(const char *s) end_screen(); if(s) printf("%s", s); fflush(stdout); - if(STTY(&inittyb) < 0) + if(tcsetattr(fileno(stdin),TCSANOW,&inittyb) < 0) perror("Hack (settty)"); - flags.echo = (inittyb.echoflgs & ECHO) ? ON : OFF; - flags.cbreak = (CBRKON(inittyb.cbrkflgs & CBRKMASK)) ? ON : OFF; + flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF; + flags.cbreak = (!(inittyb.c_lflag & ICANON)) ? ON : OFF; setioctls(); } static void setctty(void) { - if(STTY(&curttyb) < 0) + if(tcsetattr(fileno(stdin),TCSANOW,&curttyb) < 0) perror("Hack (setctty)"); } void setftty(void) { -int ef = 0; /* desired value of flags & ECHO */ -int cf = CBRKON(CBRKMASK); /* desired value of flags & CBREAK */ +u_long ef = 0; /* desired value of flags & ECHO */ +u_long cf = !(ICANON); /* desired value of flags & CBREAK */ int change = 0; flags.cbreak = ON; flags.echo = OFF; /* Should use (ECHO|CRMOD) here instead of ECHO */ - if((curttyb.echoflgs & ECHO) != ef){ - curttyb.echoflgs &= ~ECHO; + if((curttyb.c_lflag & ECHO) != ef){ + curttyb.c_lflag &= ~ECHO; change++; } - if((curttyb.cbrkflgs & CBRKMASK) != cf){ - curttyb.cbrkflgs &= ~CBRKMASK; - curttyb.cbrkflgs |= cf; -#ifdef USG + if((curttyb.c_lflag & ICANON) != cf){ + curttyb.c_lflag &= ~ICANON; + curttyb.c_lflag |= cf; /* be satisfied with one character; no timeout */ curttyb.c_cc[VMIN] = 1; /* was VEOF */ curttyb.c_cc[VTIME] = 0; /* was VEOL */ -#endif /* USG */ change++; } if(change){ diff --git a/games/larn/Makefile b/games/larn/Makefile index 0c8e4138b2..cb708c77d0 100644 --- a/games/larn/Makefile +++ b/games/larn/Makefile @@ -4,13 +4,6 @@ # EXTRA # Incorporates code to gather additional performance statistics -# SYSV -# Use system III/V (instead of V7) type ioctl calls -# BSD -# Use BSD specific features (mostly timer and signal stuff) -# BSD4.1 -# Use BSD4.1 to avoid some 4.2 dependencies (must be used with -# BSD above; do not mix with SYSV) # HIDEBYLINK # If defined, the program attempts to hide from ps # DOCHECKPOINTS @@ -27,9 +20,6 @@ # This is the revision of the software, example: 1 # FLUSHNO=# # Set the input queue excess flushing threshold (default 5) -# NOVARARGS -# Define for systems that don't have varargs (a default varargs will -# be used). # MACRORND # Define to use macro version of rnd() and rund() (fast and big) # UIDSCORE @@ -55,7 +45,7 @@ PROG= larn MAN= larn.6 -CFLAGS+=-DBSD -DVER=12 -DSUBVER=0 -DNONAP -DUIDSCORE +CFLAGS+=-DVER=12 -DSUBVER=0 -DNONAP -DUIDSCORE SRCS= main.c object.c create.c tok.c display.c global.c data.c io.c \ monster.c store.c diag.c help.c config.c nap.c bill.c scores.c \ signal.c moreobj.c movem.c regen.c fortune.c savelev.c diff --git a/games/larn/io.c b/games/larn/io.c index 457ca522c9..7a21f84bf9 100644 --- a/games/larn/io.c +++ b/games/larn/io.c @@ -57,46 +57,17 @@ * Note: ** entries are available only in termcap mode. */ +#include +#include #include "header.h" -#ifdef SYSV /* system III or system V */ -#include -#define sgttyb termio -#define stty(_a,_b) ioctl(_a,TCSETA,_b) -#define gtty(_a,_b) ioctl(_a,TCGETA,_b) static int rawflg = 0; static char saveeof,saveeol; -#define doraw(_a) if(!rawflg){++rawflg;saveeof=_a.c_cc[VMIN];saveeol=_a.c_cc[VTIME];}\ - _a.c_cc[VMIN]=1;_a.c_cc[VTIME]=1;_a.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL) -#define unraw(_a) _a.c_cc[VMIN]=saveeof;_a.c_cc[VTIME]=saveeol;_a.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL - -#else /* not SYSV */ - -#ifndef BSD -#define CBREAK RAW /* V7 has no CBREAK */ -#endif - -#define doraw(_a) (_a.sg_flags |= CBREAK,_a.sg_flags &= ~ECHO) -#define unraw(_a) (_a.sg_flags &= ~CBREAK,_a.sg_flags |= ECHO) -#include -#define stty(_a,_b) ioctl(_a,TIOCSETP,_b) -#define gtty(_a,_b) ioctl(_a,TIOCGETP,_b) -#endif /* not SYSV */ - -#ifndef NOVARARGS /* if we have varargs */ -#include -#else /* NOVARARGS */ /* if we don't have varargs */ -typedef char *va_list; -#define va_dcl int va_alist; -#define va_start(plist) plist = (char *) &va_alist -#define va_end(plist) -#define va_arg(plist,mode) ((mode *)(plist += sizeof(mode)))[-1] -#endif /* NOVARARGS */ #define LINBUFSIZE 128 /* size of the lgetw() and lgetl() buffer */ int lfd; /* output file numbers */ int fd; /* input file numbers */ -static struct sgttyb ttx; /* storage for the tty modes */ +static struct termios ttx; /* storage for the tty modes */ static int ipoint=MAXIBUF,iepoint=MAXIBUF; /* input buffering pointers */ static char lgetwbuf[LINBUFSIZE]; /* get line (word) buffer */ @@ -148,7 +119,17 @@ getchr(void) void scbr(void) { - gtty(0,&ttx); doraw(ttx); stty(0,&ttx); + tcgetattr(0,&ttx); + /* doraw */ + if (!rawflg) { + ++rawflg; + saveeof=ttx.c_cc[VMIN]; + saveeol=ttx.c_cc[VTIME]; + } + ttx.c_cc[VMIN]=1; + ttx.c_cc[VTIME]=1; + ttx.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL); + tcsetattr(0,TCSANOW,&ttx); } /* @@ -159,7 +140,12 @@ scbr(void) void sncbr(void) { - gtty(0,&ttx); unraw(ttx); stty(0,&ttx); + tcgetattr(0,&ttx); + /* unraw */ + ttx.c_cc[VMIN]=saveeof; + ttx.c_cc[VTIME]=saveeol; + ttx.c_lflag |= ICANON|ECHO|ECHOE|ECHOK|ECHONL; + tcsetattr(0,TCSANOW,&ttx); } /* diff --git a/games/larn/signal.c b/games/larn/signal.c index a69f986b2b..56a39b5d8d 100644 --- a/games/larn/signal.c +++ b/games/larn/signal.c @@ -126,8 +126,6 @@ sigsetup(void) #endif /* SIGTSTP */ } -#ifdef BSD /* for BSD UNIX? */ - static const char *signame[NSIG] = { "", "SIGHUP", /* 1 hangup */ "SIGINT", /* 2 interrupt */ @@ -156,33 +154,11 @@ static const char *signame[NSIG] = { "", "SIGXFSZ", /* 25 exceeded file size limit */ "SIGVTALRM",/* 26 virtual time alarm */ "SIGPROF", /* 27 profiling time alarm */ -"","","","" }; - -#else /* for system V? */ - -static const char *signame[NSIG] = { "", -"SIGHUP", /* 1 hangup */ -"SIGINT", /* 2 interrupt */ -"SIGQUIT", /* 3 quit */ -"SIGILL", /* 4 illegal instruction (not reset when caught) */ -"SIGTRAP", /* 5 trace trap (not reset when caught) */ -"SIGIOT", /* 6 IOT instruction */ -"SIGEMT", /* 7 EMT instruction */ -"SIGFPE", /* 8 floating point exception */ -"SIGKILL", /* 9 kill (cannot be caught or ignored) */ -"SIGBUS", /* 10 bus error */ -"SIGSEGV", /* 11 segmentation violation */ -"SIGSYS", /* 12 bad argument to system call */ -"SIGPIPE", /* 13 write on a pipe with no one to read it */ -"SIGALRM", /* 14 alarm clock */ -"SIGTERM", /* 15 software termination signal from kill */ -"SIGUSR1", /* 16 user defines signal 1 */ -"SIGUSR2", /* 17 user defines signal 2 */ -"SIGCLD", /* 18 child death */ -"SIGPWR", /* 19 power fail */ -"","","","","","","","","","","","" }; - -#endif /* BSD */ +"SIGWINCH",/* 28 window size changes */ +"SIGINFO",/* 29 information request */ +"SIGUSR1",/* 30 user defined signal 1 */ +"SIGUSR2",/* 31 user defined signal 2 */ +}; /* * routine to process a fatal error signal