-/* $NetBSD: io.c,v 1.7 1995/03/21 15:08:53 cgd Exp $ */
+/* $NetBSD: io.c,v 1.27 2012/10/13 20:36:06 dholland Exp $ */
/*-
* Copyright (c) 1980, 1993
* 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 the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 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.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$NetBSD: io.c,v 1.7 1995/03/21 15:08:53 cgd Exp $";
+__RCSID("$NetBSD: io.c,v 1.27 2012/10/13 20:36:06 dholland Exp $");
#endif
#endif /* not lint */
#include <ctype.h>
#include <curses.h>
#include <signal.h>
+#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
-#if __STDC__
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
-
#include "deck.h"
#include "cribbage.h"
#include "cribcur.h"
#endif
#define CTRL(X) (X - 'A' + 1)
-char linebuf[LINESIZE];
+static int msgcrd(CARD, BOOLEAN, const char *, BOOLEAN);
+static void printcard(WINDOW *, unsigned, CARD, BOOLEAN);
+static int incard(CARD *);
+static void wait_for(int);
+static int readchar(void);
+
+static char linebuf[LINESIZE];
-char *rankname[RANKS] = {
+static const char *const rankname[RANKS] = {
"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
};
-char *rankchar[RANKS] = {
+static const char *const rankchar[RANKS] = {
"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
};
-char *suitname[SUITS] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
+static const char *const suitname[SUITS] = {
+ "SPADES", "HEARTS", "DIAMONDS", "CLUBS"
+};
-char *suitchar[SUITS] = {"S", "H", "D", "C"};
+static const char *const suitchar[SUITS] = {"S", "H", "D", "C"};
/*
* msgcard:
* Call msgcrd in one of two forms
*/
int
-msgcard(c, brief)
- CARD c;
- BOOLEAN brief;
+msgcard(CARD c, BOOLEAN brief)
{
if (brief)
return (msgcrd(c, TRUE, NULL, TRUE));
* msgcrd:
* Print the value of a card in ascii
*/
-int
-msgcrd(c, brfrank, mid, brfsuit)
- CARD c;
- BOOLEAN brfrank, brfsuit;
- char *mid;
+static int
+msgcrd(CARD c, BOOLEAN brfrank, const char *mid, BOOLEAN brfsuit)
{
if (c.rank == EMPTY || c.suit == EMPTY)
return (FALSE);
if (brfrank)
addmsg("%1.1s", rankchar[c.rank]);
else
- addmsg(rankname[c.rank]);
+ addmsg("%s", rankname[c.rank]);
if (mid != NULL)
- addmsg(mid);
+ addmsg("%s", mid);
if (brfsuit)
addmsg("%1.1s", suitchar[c.suit]);
else
- addmsg(suitname[c.suit]);
+ addmsg("%s", suitname[c.suit]);
return (TRUE);
}
* printcard:
* Print out a card.
*/
-void
-printcard(win, cardno, c, blank)
- WINDOW *win;
- int cardno;
- CARD c;
- BOOLEAN blank;
+static void
+printcard(WINDOW *win, unsigned cardno, CARD c, BOOLEAN blank)
{
prcard(win, cardno * 2, cardno, c, blank);
}
* Print out a card on the window at the specified location
*/
void
-prcard(win, y, x, c, blank)
- WINDOW *win;
- int y, x;
- CARD c;
- BOOLEAN blank;
+prcard(WINDOW *win, int y, int x, CARD c, BOOLEAN blank)
{
if (c.rank == EMPTY)
return;
* Print a hand of n cards
*/
void
-prhand(h, n, win, blank)
- CARD h[];
- int n;
- WINDOW *win;
- BOOLEAN blank;
+prhand(const CARD h[], unsigned n, WINDOW *win, BOOLEAN blank)
{
- register int i;
+ unsigned i;
werase(win);
for (i = 0; i < n; i++)
* input, returns the index of the card found...
*/
int
-infrom(hand, n, prompt)
- CARD hand[];
- int n;
- char *prompt;
+infrom(const CARD hand[], int n, const char *prompt)
{
- register int i, j;
+ int i, j;
CARD crd;
if (n < 1) {
exit(74);
}
for (;;) {
- msg(prompt);
+ msg("%s", prompt);
if (incard(&crd)) { /* if card is full card */
- if (!isone(crd, hand, n))
+ if (!is_one(crd, hand, n))
msg("That's not in your hand");
else {
for (i = 0; i < n; i++)
hand[i].suit == crd.suit)
break;
if (i >= n) {
- printf("\nINFROM: isone or something messed up\n");
+ printf("\nINFROM: is_one or something messed up\n");
exit(77);
}
return (i);
* Inputs a card in any format. It reads a line ending with a CR
* and then parses it.
*/
-int
-incard(crd)
- CARD *crd;
+static int
+incard(CARD *crd)
{
- register int i;
+ int i;
int rnk, sut;
char *line, *p, *p1;
BOOLEAN retval;
retval = FALSE;
rnk = sut = EMPTY;
- if (!(line = getline()))
+ if (!(line = get_line()))
goto gotit;
p = p1 = line;
- while (*p1 != ' ' && *p1 != NULL)
+ while (*p1 != ' ' && *p1 != '\0')
++p1;
- *p1++ = NULL;
- if (*p == NULL)
+ *p1++ = '\0';
+ if (*p == '\0')
goto gotit;
/* IMPORTANT: no real card has 2 char first name */
if (rnk == EMPTY)
goto gotit;
p = p1;
- while (*p1 != ' ' && *p1 != NULL)
+ while (*p1 != ' ' && *p1 != '\0')
++p1;
- *p1++ = NULL;
- if (*p == NULL)
+ *p1++ = '\0';
+ if (*p == '\0')
goto gotit;
if (!strcmp("OF", p)) {
p = p1;
- while (*p1 != ' ' && *p1 != NULL)
+ while (*p1 != ' ' && *p1 != '\0')
++p1;
- *p1++ = NULL;
- if (*p == NULL)
+ *p1++ = '\0';
+ if (*p == '\0')
goto gotit;
}
sut = EMPTY;
* Reads and converts to upper case
*/
int
-getuchar()
+getuchar(void)
{
- register int c;
+ int c;
c = readchar();
if (islower(c))
* "hi" inclusive.
*/
int
-number(lo, hi, prompt)
- int lo, hi;
- char *prompt;
+number(int lo, int hi, const char *prompt)
{
- register char *p;
- register int sum;
+ char *p;
+ int sum;
for (sum = 0;;) {
- msg(prompt);
- if (!(p = getline()) || *p == NULL) {
+ msg("%s", prompt);
+ if (!(p = get_line()) || *p == '\0') {
msg(quiet ? "Not a number" :
"That doesn't look like a number");
continue;
}
sum = 0;
- if (!isdigit(*p))
+ if (!isdigit((unsigned char)*p))
sum = lo - 1;
else
- while (isdigit(*p)) {
+ while (isdigit((unsigned char)*p)) {
sum = 10 * sum + (*p - '0');
++p;
}
- if (*p != ' ' && *p != '\t' && *p != NULL)
+ if (*p != ' ' && *p != '\t' && *p != '\0')
sum = lo - 1;
if (sum >= lo && sum <= hi)
break;
* msg:
* Display a message at the top of the screen.
*/
-char Msgbuf[BUFSIZ] = {'\0'};
-int Mpos = 0;
+static char Msgbuf[BUFSIZ] = {'\0'};
+static int Mpos = 0;
static int Newpos = 0;
void
-#if __STDC__
msg(const char *fmt, ...)
-#else
-msg(fmt, va_alist)
- char *fmt;
- va_dcl
-#endif
{
va_list ap;
-#if __STDC__
va_start(ap, fmt);
-#else
- va_start(ap);
-#endif
- (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
+ (void)vsnprintf(&Msgbuf[Newpos], sizeof(Msgbuf)-Newpos, fmt, ap);
+ Newpos = strlen(Msgbuf);
va_end(ap);
endmsg();
}
* Add things to the current message
*/
void
-#if __STDC__
addmsg(const char *fmt, ...)
-#else
-addmsg(fmt, va_alist)
- char *fmt;
- va_dcl
-#endif
{
va_list ap;
-#if __STDC__
va_start(ap, fmt);
-#else
- va_start(ap);
-#endif
- (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
+ (void)vsnprintf(&Msgbuf[Newpos], sizeof(Msgbuf)-Newpos, fmt, ap);
+ Newpos = strlen(Msgbuf);
va_end(ap);
}
* endmsg:
* Display a new msg.
*/
-int Lineno = 0;
+static int Lineno = 0;
void
-endmsg()
+endmsg(void)
{
static int lastline = 0;
- register int len;
- register char *mp, *omp;
+ int len;
+ char *mp, *omp;
/* All messages should start with uppercase */
mvaddch(lastline + Y_MSG_START, SCORE_X, ' ');
- if (islower(Msgbuf[0]) && Msgbuf[1] != ')')
- Msgbuf[0] = toupper(Msgbuf[0]);
+ if (islower((unsigned char)Msgbuf[0]) && Msgbuf[1] != ')')
+ Msgbuf[0] = toupper((unsigned char)Msgbuf[0]);
mp = Msgbuf;
len = strlen(mp);
if (len / MSG_X + Lineno >= MSG_Y) {
* Wait for the user to type ' ' before doing anything else
*/
void
-do_wait()
+do_wait(void)
{
- static char prompt[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
+ static const char prompt[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
- if (Mpos + sizeof prompt < MSG_X)
+ if ((int)(Mpos + sizeof prompt) < MSG_X)
wmove(Msgwin, Lineno > 0 ? Lineno - 1 : MSG_Y - 1, Mpos);
else {
mvwaddch(Msgwin, Lineno, 0, ' ');
* wait_for
* Sit around until the guy types the right key
*/
-void
-wait_for(ch)
- register int ch;
+static void
+wait_for(int ch)
{
- register char c;
+ int c;
if (ch == '\n')
while ((c = readchar()) != '\n')
* readchar:
* Reads and returns a character, checking for gross input errors
*/
-int
-readchar()
+static int
+readchar(void)
{
- register int cnt;
- char c;
+ int cnt;
+ unsigned char c;
over:
cnt = 0;
- while (read(STDIN_FILENO, &c, sizeof(char)) <= 0)
+ while (read(STDIN_FILENO, &c, sizeof(unsigned char)) <= 0)
if (cnt++ > 100) { /* if we are getting infinite EOFs */
bye(); /* quit the game */
exit(1);
}
/*
- * getline:
+ * get_line:
* Reads the next line up to '\n' or EOF. Multiple spaces are
* compressed to one space; a space is inserted before a ','
*/
char *
-getline()
+get_line(void)
{
- register char *sp;
- register int c, oy, ox;
- register WINDOW *oscr;
+ size_t pos;
+ int c, oy, ox;
+ WINDOW *oscr;
oscr = stdscr;
stdscr = Msgwin;
getyx(stdscr, oy, ox);
refresh();
/* loop reading in the string, and put it in a temporary buffer */
- for (sp = linebuf; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
- if (c == -1)
- continue;
- else
+ for (pos = 0; (c = readchar()) != '\n'; clrtoeol(), refresh()) {
if (c == erasechar()) { /* process erase character */
- if (sp > linebuf) {
- register int i;
+ if (pos > 0) {
+ int i;
- sp--;
- for (i = strlen(unctrl(*sp)); i; i--)
+ pos--;
+ for (i = strlen(unctrl(linebuf[pos])); i; i--)
addch('\b');
}
continue;
} else
if (c == killchar()) { /* process kill
* character */
- sp = linebuf;
+ pos = 0;
move(oy, ox);
continue;
} else
- if (sp == linebuf && c == ' ')
+ if (pos == 0 && c == ' ')
continue;
- if (sp >= &linebuf[LINESIZE - 1] || !(isprint(c) || c == ' '))
+ if (pos >= LINESIZE - 1 || !(isprint(c) || c == ' '))
putchar(CTRL('G'));
else {
if (islower(c))
c = toupper(c);
- *sp++ = c;
+ linebuf[pos++] = c;
addstr(unctrl(c));
Mpos++;
}
}
- *sp = '\0';
+ linebuf[pos] = '\0';
stdscr = oscr;
return (linebuf);
}
void
-rint(signo)
- int signo;
+receive_intr(int signo __unused)
{
bye();
exit(1);
* Leave the program, cleaning things up as we go.
*/
void
-bye()
+bye(void)
{
signal(SIGINT, SIG_IGN);
mvcur(0, COLS - 1, LINES - 1, 0);