-
- if(FIRST >= 25)BUG(29);
- PARMS[FIRST]=P1;
- {long x = FIRST+1; PARMS[x]=P2;}
- return;
-}
-
-
-
-#define SETPRM(FIRST,P1,P2) fSETPRM(FIRST,P1,P2)
-#undef GETIN
-#define WORD1 (*wORD1)
-#define WORD1X (*wORD1X)
-#define WORD2 (*wORD2)
-#define WORD2X (*wORD2X)
-void fGETIN(FILE *input, long *wORD1, long *wORD1X, long *wORD2, long *wORD2X) {
-long JUNK;
-
-/* Get a command from the adventurer. Snarf out the first word, pad it with
- * blanks, and return it in WORD1. Chars 6 thru 10 are returned in WORD1X, in
- * case we need to print out the whole word in an error message. Any number of
- * blanks may follow the word. If a second word appears, it is returned in
- * WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
-
-
-L10: if(BLKLIN)TYPE0();
- MAPLIN(input);
- if(input == stdin && feof(stdin)) score(1);
- WORD1=GETTXT(true,true,true,0);
- if(BLKLIN && WORD1 < 0) goto L10;
- WORD1X=GETTXT(false,true,true,0);
-L12: JUNK=GETTXT(false,true,true,0);
- if(JUNK > 0) goto L12;
- WORD2=GETTXT(true,true,true,0);
- WORD2X=GETTXT(false,true,true,0);
-L22: JUNK=GETTXT(false,true,true,0);
- if(JUNK > 0) goto L22;
- if(GETTXT(true,true,true,0) <= 0)return;
- RSPEAK(53);
- goto L10;
-}
-
-
-
-#undef WORD1
-#undef WORD1X
-#undef WORD2
-#undef WORD2X
-#define GETIN(SRC,WORD1,WORD1X,WORD2,WORD2X) fGETIN(SRC,&WORD1,&WORD1X,&WORD2,&WORD2X)
-#undef YES
-long fYES(FILE *input, long X, long Y, long Z) {
-
-long YES, REPLY, JUNK1, JUNK2, JUNK3;
-
-/* Print message X, wait for yes/no answer. If yes, print Y and return true;
- * if no, print Z and return false. */
-
-L1: RSPEAK(X);
- GETIN(input, REPLY,JUNK1,JUNK2,JUNK3);
- if(REPLY == MAKEWD(250519) || REPLY == MAKEWD(25)) goto L10;
- if(REPLY == MAKEWD(1415) || REPLY == MAKEWD(14)) goto L20;
- RSPEAK(185);
- goto L1;
-L10: YES=true;
- RSPEAK(Y);
- return(YES);
-L20: YES=false;
- RSPEAK(Z);
- return(YES);
-}
-
-
-
-
-
-/* Line-parsing routines (GETNUM, GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0)
- */
-
-/* The routines on this page handle all the stuff that would normally be
- * taken care of by format statements. We do it this way instead so that
- * we can handle textual data in a machine independent fashion. All the
- * machine dependent i/o stuff is on the following page. See that page
- * for a description of MAPCOM's inline array. */
-
-#define YES(X,Y,Z) fYES(X,Y,Z)
-#undef GETNUM
-long fGETNUM(FILE *source) {
-long DIGIT, GETNUM, SIGN;
-
-/* Obtain the next integer from an input line. If K>0, we first read a
- * new input line from a file; if K<0, we read a line from the keyboard;
- * if K=0 we use a line that has already been read (and perhaps partially
- * scanned). If we're at the end of the line or encounter an illegal
- * character (not a digit, hyphen, or blank), we return 0. */
-
-
- if(source != NULL)MAPLIN(source);
- GETNUM=0;
-L10: if(LNPOSN > LNLENG)return(GETNUM);
- if(INLINE[LNPOSN] != 0) goto L20;
- LNPOSN=LNPOSN+1;
- goto L10;
-
-L20: SIGN=1;
- if(INLINE[LNPOSN] != 9) goto L32;
- SIGN= -1;
-L30: LNPOSN=LNPOSN+1;
-L32: if(LNPOSN > LNLENG || INLINE[LNPOSN] == 0) goto L42;
- DIGIT=INLINE[LNPOSN]-64;
- if(DIGIT < 0 || DIGIT > 9) goto L40;
- GETNUM=GETNUM*10+DIGIT;
- goto L30;
-
-L40: GETNUM=0;
-L42: GETNUM=GETNUM*SIGN;
- LNPOSN=LNPOSN+1;
- return(GETNUM);
-}
-
-
-
-#define GETNUM(K) fGETNUM(K)
-#undef GETTXT
-long fGETTXT(long SKIP,long ONEWRD, long UPPER, long HASH) {
-long CHAR, GETTXT, I; static long SPLITTING = -1;
-
-/* Take characters from an input line and pack them into 30-bit words.
- * Skip says to skip leading blanks. ONEWRD says stop if we come to a
- * blank. UPPER says to map all letters to uppercase. HASH may be used
- * as a parameter for encrypting the text if desired; however, a hash of 0
- * should result in unmodified bytes being packed. If we reach the
- * end of the line, the word is filled up with blanks (which encode as 0's).
- * If we're already at end of line when GETTXT is called, we return -1. */
-
- if(LNPOSN != SPLITTING)SPLITTING = -1;
- GETTXT= -1;
-L10: if(LNPOSN > LNLENG)return(GETTXT);
- if((!SKIP) || INLINE[LNPOSN] != 0) goto L11;
- LNPOSN=LNPOSN+1;
- goto L10;
-
-L11: GETTXT=0;
- /* 15 */ for (I=1; I<=5; I++) {
- GETTXT=GETTXT*64;
- if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0)) goto L15;
- CHAR=INLINE[LNPOSN];
- if(CHAR >= 63) goto L12;
- SPLITTING = -1;
- if(UPPER && CHAR >= 37)CHAR=CHAR-26;
- GETTXT=GETTXT+CHAR;
- goto L14;
-
-L12: if(SPLITTING == LNPOSN) goto L13;
- GETTXT=GETTXT+63;
- SPLITTING = LNPOSN;
- goto L15;
-
-L13: GETTXT=GETTXT+CHAR-63;
- SPLITTING = -1;
-L14: LNPOSN=LNPOSN+1;
-L15: /*etc*/ ;
- } /* end loop */
-
- if(HASH)GETTXT=GETTXT+MOD(HASH*13579L+5432L,97531L)*12345L+HASH;
- return(GETTXT);
-}
-
-
-
-#define GETTXT(SKIP,ONEWRD,UPPER,HASH) fGETTXT(SKIP,ONEWRD,UPPER,HASH)
-#undef MAKEWD
-long fMAKEWD(long LETTRS) {
-long I, L, MAKEWD;
-
-/* Combine five uppercase letters (represented by pairs of decimal digits
- * in lettrs) to form a 30-bit value matching the one that GETTXT would
- * return given those characters plus trailing blanks and HASH=0. Caution:
- * lettrs will overflow 31 bits if 5-letter word starts with V-Z. As a
- * kludgey workaround, you can increment a letter by 5 by adding 50 to
- * the next pair of digits. */
-
-
- MAKEWD=0;
- I=1;
- L=LETTRS;
-L10: MAKEWD=MAKEWD+I*(MOD(L,50)+10);
- I=I*64;
- if(MOD(L,100) > 50)MAKEWD=MAKEWD+I*5;
- L=L/100;
- if(L != 0) goto L10;
- I=64L*64L*64L*64L*64L/I;
- MAKEWD=MAKEWD*I;
- return(MAKEWD);
+static void* xmalloc(size_t size)
+{
+ void* ptr = malloc(size);
+ if (ptr == NULL) {
+ // LCOV_EXCL_START
+ // exclude from coverage analysis because we can't simulate an out of memory error in testing
+ fprintf(stderr, "Out of memory!\n");
+ exit(EXIT_FAILURE);
+ // LCOV_EXCL_STOP
+ }
+ return (ptr);
+}
+
+void packed_to_token(long packed, char token[TOKLEN + 1])
+{
+ // The advent->ascii mapping.
+ const char advent_to_ascii[] = {
+ ' ', '!', '"', '#', '$', '%', '&', '\'',
+ '(', ')', '*', '+', ',', '-', '.', '/',
+ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', ':', ';', '<', '=', '>', '?',
+ '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
+ 'X', 'Y', 'Z', '\0', '\0', '\0', '\0', '\0',
+ };
+
+ // Unpack and map back to ASCII.
+ for (int i = 0; i < 5; ++i) {
+ char advent = (packed >> i * 6) & 63;
+ token[i] = advent_to_ascii[(int) advent];
+ }
+
+ // Ensure the last character is \0.
+ token[5] = '\0';
+
+ // Replace trailing whitespace with \0.
+ for (int i = 4; i >= 0; --i) {
+ if (token[i] == ' ' ||
+ token[i] == '\t')
+ token[i] = '\0';
+ else
+ break;
+ }
+}
+
+long token_to_packed(const char token[])
+{
+ const char ascii_to_advent[] = {
+ 63, 63, 63, 63, 63, 63, 63, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63,
+
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 16, 17, 18, 19, 20, 21, 22, 23,
+ 24, 25, 26, 27, 28, 29, 30, 31,
+ 32, 33, 34, 35, 36, 37, 38, 39,
+ 40, 41, 42, 43, 44, 45, 46, 47,
+ 48, 49, 50, 51, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61, 62, 63,
+
+ 63, 63, 63, 63, 63, 63, 63, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63,
+ 63, 63, 63, 63, 63, 63, 63, 63,
+ };
+
+ size_t t_len = strlen(token);
+ if (t_len > TOKLEN)
+ t_len = TOKLEN;
+ long packed = 0;
+ for (size_t i = 0; i < t_len; ++i) {
+ char mapped = ascii_to_advent[(int) toupper(token[i])];
+ packed |= (mapped << (6 * i));
+ }
+ return (packed);
+}
+
+void tokenize(char* raw, struct command_t *cmd)
+{
+ memset(cmd, '\0', sizeof(struct command_t));
+
+ /* Bound prefix on the %s would be needed to prevent buffer
+ * overflow. but we shortstop this more simply by making each
+ * raw-input buffer as long as the enrire inout buffer. */
+ sscanf(raw, "%s%s", cmd->raw1, cmd->raw2);
+
+ // pack the substrings
+ cmd->wd1 = token_to_packed(cmd->raw1);
+ cmd->wd2 = token_to_packed(cmd->raw2);
+
+ /* (ESR) In oldstyle mode, simulate the uppercasing and truncating
+ * effect on raw tokens of packing them into sixbit characters, 5
+ * to a 32-bit word. This is something the FORTRAN version did
+ * becuse archaic FORTRAN had no string types. Don Wood's
+ * mechanical translation of 2.5 to C retained the packing and
+ * thus this misfeature.
+ *
+ * It's philosophically questionable whether this is the right
+ * thing to do even in oldstyle mode. On one hand, the text
+ * mangling was not authorial intent, but a result of limitations
+ * in their tools. On the other, not simulating this misbehavior
+ * goes against the goal of making oldstyle as accurate as
+ * possible an emulation of the original UI.
+ */
+ if (settings.oldstyle) {
+ cmd->raw1[TOKLEN + TOKLEN] = cmd->raw2[TOKLEN + TOKLEN] = '\0';
+ for (size_t i = 0; i < strlen(cmd->raw1); i++)
+ cmd->raw1[i] = toupper(cmd->raw1[i]);
+ for (size_t i = 0; i < strlen(cmd->raw2); i++)
+ cmd->raw2[i] = toupper(cmd->raw2[i]);
+ }
+}
+
+/* Hide the fact that wods are corrently packed longs */
+
+void wordclear(token_t *v)
+{
+ *v = 0;