- // Create a copy of our string, so we can edit it.
- char* copy = (char*) xmalloc(strlen(msg) + 1);
- strncpy(copy, msg, strlen(msg) + 1);
-
- // Staging area for stringified parameters.
- char parameters[5][100]; // FIXME: to be replaced with dynamic allocation
-
- // Handle format specifiers (including the custom %C, %L, %S) by adjusting the parameter accordingly, and replacing the specifier with %s.
- int pi = 0; // parameter index
- for (int i = 0; i < strlen(msg); ++i)
- {
- if (msg[i] == '%')
- {
- ++pi;
-
- // Integer specifier. In order to accommodate the fact that PARMS can have both legitimate integers *and* packed tokens, stringify everything. Future work may eliminate the need for this.
- if (msg[i + 1] == 'd')
- {
- copy[i + 1] = 's';
- sprintf(parameters[pi], "%d", PARMS[pi]);
- }
-
- // Unmodified string specifier.
- if (msg[i + 1] == 's')
- {
- packed_to_token(PARMS[pi], parameters[pi]);
- }
-
- // Singular/plural specifier.
- if (msg[i + 1] == 'S')
- {
- copy[i + 1] = 's';
- if (PARMS[pi - 1] > 1) // look at the *previous* parameter (which by necessity must be numeric)
- {
- sprintf(parameters[pi], "%s", "s");
- }
- else
- {
- sprintf(parameters[pi], "%s", "");
- }
- }
-
- // All-lowercase specifier.
- if (msg[i + 1] == 'L')
- {
- copy[i + 1] = 's';
- packed_to_token(PARMS[pi], parameters[pi]);
- for (int i = 0; i < strlen(parameters[pi]); ++i)
- {
- parameters[pi][i] = tolower(parameters[pi][i]);
- }
- }
-
- // First char uppercase, rest lowercase.
- if (msg[i + 1] == 'C')
- {
- copy[i + 1] = 's';
- packed_to_token(PARMS[pi], parameters[pi]);
- for (int i = 0; i < strlen(parameters[pi]); ++i)
- {
- parameters[pi][i] = tolower(parameters[pi][i]);
- }
- parameters[pi][0] = toupper(parameters[pi][0]);
- }
- }
+ // break the words into up to 4 5-char substrings
+ sscanf(words[0], "%5s%5s", chunk_data[0], chunk_data[1]);
+ if (word_count == 2)
+ sscanf(words[1], "%5s%5s", chunk_data[2], chunk_data[3]);
+ free(words[0]);
+ free(words[1]);
+
+ // uppercase all the substrings
+ for (int i = 0; i < 4; ++i)
+ for (unsigned int j = 0; j < strlen(chunk_data[i]); ++j)
+ chunk_data[i][j] = (char) toupper(chunk_data[i][j]);
+
+ // pack the substrings
+ for (int i = 0; i < 4; ++i)
+ tokens[i] = token_to_packed(chunk_data[i]);
+}
+
+/* Hide the fact that wods are corrently packed longs */
+
+bool wordeq(token_t a, token_t b)
+{
+ return a == b;
+}
+
+bool wordempty(token_t a)
+{
+ return a == 0;
+}
+
+void wordclear(token_t *v)
+{
+ *v = 0;
+}
+
+/* I/O routines (speak, pspeak, rspeak, get_input, yes) */
+
+void vspeak(const char* msg, bool blank, va_list ap)
+{
+ // Do nothing if we got a null pointer.
+ if (msg == NULL)
+ return;
+
+ // Do nothing if we got an empty string.
+ if (strlen(msg) == 0)
+ return;
+
+ if (blank == true)
+ printf("\n");
+
+ int msglen = strlen(msg);
+
+ // Rendered string
+ ssize_t size = 2000; /* msglen > 50 ? msglen*2 : 100; */
+ char* rendered = xmalloc(size);
+ char* renderp = rendered;
+
+ // Handle format specifiers (including the custom %C, %L, %S) by
+ // adjusting the parameter accordingly, and replacing the
+ // specifier with %s.
+ long previous_arg = 0;
+ for (int i = 0; i < msglen; i++) {
+ if (msg[i] != '%') {
+ *renderp++ = msg[i];
+ size--;
+ } else {
+ long arg = va_arg(ap, long);
+ if (arg == -1)
+ arg = 0; // LCOV_EXCL_LINE - don't think we can get here.
+ i++;
+ // Integer specifier. In order to accommodate the fact
+ // that PARMS can have both legitimate integers *and*
+ // packed tokens, stringify everything. Future work may
+ // eliminate the need for this.
+ if (msg[i] == 'd') {
+ int ret = snprintf(renderp, size, "%ld", arg);
+ if (ret < size) {
+ renderp += ret;
+ size -= ret;
+ }
+ }
+
+ // Unmodified string specifier.
+ if (msg[i] == 's') {
+ packed_to_token(arg, renderp); /* unpack directly to destination */
+ size_t len = strlen(renderp);
+ renderp += len;
+ size -= len;
+ }
+
+ // Singular/plural specifier.
+ if (msg[i] == 'S') {
+ if (previous_arg > 1) { // look at the *previous* parameter (which by necessity must be numeric)
+ *renderp++ = 's';
+ size--;
+ }
+ }
+
+ /* Version specifier */
+ if (msg[i] == 'V') {
+ strcpy(renderp, VERSION);
+ size_t len = strlen(VERSION);
+ renderp += len;
+ size -= len;
+ }
+
+ // All-lowercase specifier.
+ if (msg[i] == 'L' || msg[i] == 'C') {
+ packed_to_token(arg, renderp); /* unpack directly to destination */
+ int len = strlen(renderp);
+ for (int j = 0; j < len; ++j) {
+ renderp[j] = tolower(renderp[j]);
+ }
+ if (msg[i] == 'C') // First char uppercase, rest lowercase.
+ renderp[0] = toupper(renderp[0]);
+ renderp += len;
+ size -= len;
+ }
+
+ previous_arg = arg;
+ }
+ }
+ *renderp = 0;
+
+ // Print the message.
+ printf("%s\n", rendered);
+
+ free(rendered);
+}
+
+void speak(const char* msg, ...)
+{
+ va_list ap;
+ va_start(ap, msg);
+ vspeak(msg, true, ap);
+ va_end(ap);
+}
+
+void pspeak(vocab_t msg, enum speaktype mode, int skip, bool blank, ...)
+/* Find the skip+1st message from msg and print it. Modes are:
+ * feel = for inventory, what you can touch
+ * look = the long description for the state the object is in
+ * listen = the sound for the state the object is in
+ * study = text on the object. */
+{
+ va_list ap;
+ va_start(ap, blank);
+ switch (mode) {
+ case touch:
+ vspeak(objects[msg].inventory, blank, ap);
+ break;
+ case look:
+ vspeak(objects[msg].descriptions[skip], blank, ap);
+ break;
+ case hear:
+ vspeak(objects[msg].sounds[skip], blank, ap);
+ break;
+ case study:
+ vspeak(objects[msg].texts[skip], blank, ap);
+ break;
+ case change:
+ vspeak(objects[msg].changes[skip], blank, ap);
+ break;