-/* $NetBSD: io.c,v 1.16 2003/08/07 09:37:10 agc Exp $ */
+/* $NetBSD: io.c,v 1.27 2012/10/13 20:36:06 dholland Exp $ */
/*-
* Copyright (c) 1980, 1993
#if 0
static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: io.c,v 1.16 2003/08/07 09:37:10 agc Exp $");
+__RCSID("$NetBSD: io.c,v 1.27 2012/10/13 20:36:06 dholland Exp $");
#endif
#endif /* not lint */
#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);
-const char *const rankname[RANKS] = {
+static char linebuf[LINESIZE];
+
+static const char *const rankname[RANKS] = {
"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"
};
-const char *const rankchar[RANKS] = {
+static const char *const rankchar[RANKS] = {
"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"
};
-const char *const suitname[SUITS] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
+static const char *const suitname[SUITS] = {
+ "SPADES", "HEARTS", "DIAMONDS", "CLUBS"
+};
-const char *const 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;
- const 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)
- const CARD h[];
- int n;
- WINDOW *win;
- BOOLEAN blank;
+prhand(const CARD h[], unsigned n, WINDOW *win, BOOLEAN blank)
{
- 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)
- const CARD hand[];
- int n;
- const char *prompt;
+infrom(const CARD hand[], int n, const char *prompt)
{
int i, j;
CARD crd;
exit(74);
}
for (;;) {
- msg(prompt);
+ msg("%s", prompt);
if (incard(&crd)) { /* if card is full card */
if (!is_one(crd, hand, n))
msg("That's not in your hand");
* 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)
{
int i;
int rnk, sut;
retval = FALSE;
rnk = sut = EMPTY;
- if (!(line = getline()))
+ if (!(line = get_line()))
goto gotit;
p = p1 = line;
while (*p1 != ' ' && *p1 != '\0')
* Reads and converts to upper case
*/
int
-getuchar()
+getuchar(void)
{
int c;
* "hi" inclusive.
*/
int
-number(lo, hi, prompt)
- int lo, hi;
- const char *prompt;
+number(int lo, int hi, const char *prompt)
{
char *p;
int sum;
for (sum = 0;;) {
- msg(prompt);
- if (!(p = getline()) || *p == '\0') {
+ 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;
}
* 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
va_list ap;
va_start(ap, fmt);
- (void)vsprintf(&Msgbuf[Newpos], fmt, ap);
+ (void)vsnprintf(&Msgbuf[Newpos], sizeof(Msgbuf)-Newpos, fmt, ap);
Newpos = strlen(Msgbuf);
va_end(ap);
endmsg();
va_list ap;
va_start(ap, fmt);
- (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;
int len;
/* 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 const char prompt[] = {'-', '-', 'M', 'o', 'r', 'e', '-', '-', '\0'};
* wait_for
* Sit around until the guy types the right key
*/
-void
-wait_for(ch)
- int ch;
+static void
+wait_for(int ch)
{
- 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)
{
int cnt;
- char c;
+ 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)
{
- char *sp;
+ size_t pos;
int c, oy, ox;
WINDOW *oscr;
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) {
+ 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 __attribute__((__unused__));
+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);