summaryrefslogtreecommitdiffstats
path: root/battlestar/getcom.c
diff options
context:
space:
mode:
authorjsm <jsm@NetBSD.org>2000-09-21 17:44:34 +0000
committerjsm <jsm@NetBSD.org>2000-09-21 17:44:34 +0000
commit0ba05dd5a679e7723ea4b9f1426175b1651926cf (patch)
tree8beede32304395cb7418af5573f1e3b042030ca8 /battlestar/getcom.c
parent66bc28438e19fafc7abecfa76bce72d10174c9be (diff)
downloadbsdgames-darwin-0ba05dd5a679e7723ea4b9f1426175b1651926cf.tar.gz
bsdgames-darwin-0ba05dd5a679e7723ea4b9f1426175b1651926cf.tar.zst
bsdgames-darwin-0ba05dd5a679e7723ea4b9f1426175b1651926cf.zip
Various improvements to parsing in battlestar, mostly from OpenBSD.
Define a constant WORDLEN. Always use this constant and NWORD where appropriate. Use NWORD - 1 in battlestar.c to avoid off-by-one error. Increment wordnumber after the INVEN verb to allow it to be followed by a comma and other actions. Avoid overflowing elements of the words array if input words are too long. Parse "," as AND except when followed by a verb, to allow such constructions as "take foo, bar, and baz". Trim AND AND which may occur from the ", and" in such a list. Avoid crashes from EVERYTHING in the wrong place by moving it to the start of OBJECT AND EVERYTHING and NOUNS AND EVERYTHING sequences, and trimming EVERYTHING AND EVERYTHING.
Diffstat (limited to 'battlestar/getcom.c')
-rw-r--r--battlestar/getcom.c34
1 files changed, 24 insertions, 10 deletions
diff --git a/battlestar/getcom.c b/battlestar/getcom.c
index 4c7e2935..5d68738c 100644
--- a/battlestar/getcom.c
+++ b/battlestar/getcom.c
@@ -1,4 +1,4 @@
-/* $NetBSD: getcom.c,v 1.8 2000/09/21 09:49:03 jsm Exp $ */
+/* $NetBSD: getcom.c,v 1.9 2000/09/21 17:44:34 jsm Exp $ */
/*
* Copyright (c) 1983, 1993
@@ -38,7 +38,7 @@
#if 0
static char sccsid[] = "@(#)getcom.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: getcom.c,v 1.8 2000/09/21 09:49:03 jsm Exp $");
+__RCSID("$NetBSD: getcom.c,v 1.9 2000/09/21 17:44:34 jsm Exp $");
#endif
#endif /* not lint */
@@ -78,6 +78,9 @@ getword(buf1, buf2, flag)
char *buf1, *buf2;
int flag;
{
+ int cnt;
+
+ cnt = 1;
while (isspace(*buf1))
buf1++;
if (*buf1 != ',') {
@@ -85,23 +88,34 @@ getword(buf1, buf2, flag)
*buf2 = 0;
return (0);
}
- while (*buf1 && !isspace(*buf1) && *buf1 != ',')
+ while (cnt < WORDLEN && *buf1 && !isspace(*buf1) && *buf1 != ',')
if (flag < 0) {
- if (isupper(*buf1))
+ if (isupper(*buf1)) {
*buf2++ = tolower(*buf1++);
- else
+ cnt++;
+ } else {
*buf2++ = *buf1++;
+ cnt++;
+ }
} else if (flag > 0) {
- if (islower(*buf1))
+ if (islower(*buf1)) {
*buf2++ = toupper(*buf1++);
- else
+ cnt++;
+ } else {
*buf2++ = *buf1++;
- } else
+ cnt++;
+ }
+ } else {
*buf2++ = *buf1++;
+ cnt++;
+ }
+ if (cnt == WORDLEN)
+ while (*buf1 && !isspace(*buf1))
+ buf1++;
} else
*buf2++ = *buf1++;
- *buf2 = 0;
+ *buf2 = '\0';
while (isspace(*buf1))
buf1++;
- return (*buf1 ? buf1 : 0);
+ return (*buf1 ? buf1 : NULL);
}