+static void* xcalloc(size_t size)
+{
+ void* ptr = calloc(size, 1);
+ 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);
+}
+
+/* I/O routines (speak, pspeak, rspeak, sspeak, get_input, yes) */
+
+static 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 = xcalloc(size);
+ char* renderp = rendered;
+
+ // Handle format specifiers (including the custom %S) by
+ // adjusting the parameter accordingly, and replacing the
+ // specifier with %s.
+ bool pluralize = false;
+ for (int i = 0; i < msglen; i++) {
+ if (msg[i] != '%') {
+ /* Ugh. Least obtrusive way to deal with artifacts "on the floor"
+ * being dropped outside of both cave and building. */
+ if (strncmp(msg + i, "floor", 5) == 0 && strchr(" .", msg[i + 5]) && !INSIDE(game.loc)) {
+ strcpy(renderp, "ground");
+ renderp += 6;
+ i += 4;
+ size -= 5;
+ } else {
+ *renderp++ = msg[i];
+ size--;
+ }
+ } else {
+ i++;
+ // Integer specifier.
+ if (msg[i] == 'd') {
+ long arg = va_arg(ap, long);
+ int ret = snprintf(renderp, size, "%ld", arg);
+ if (ret < size) {
+ renderp += ret;
+ size -= ret;
+ }
+ pluralize = (arg != 1);
+ }
+
+ // Unmodified string specifier.
+ if (msg[i] == 's') {
+ char *arg = va_arg(ap, char *);
+ strncat(renderp, arg, size - 1);
+ size_t len = strlen(renderp);
+ renderp += len;
+ size -= len;
+ }
+
+ // Singular/plural specifier.
+ if (msg[i] == 'S') {
+ // look at the *previous* numeric parameter
+ if (pluralize) {
+ *renderp++ = 's';
+ size--;
+ }
+ }
+
+ // LCOV_EXCL_START - doesn't occur in test suite.
+ /* Version specifier */
+ if (msg[i] == 'V') {
+ strcpy(renderp, VERSION);
+ size_t len = strlen(VERSION);
+ renderp += len;
+ size -= len;
+ }
+ // LCOV_EXCL_STOP
+ }
+ }
+ *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 sspeak(const int msg, ...)
+{
+ va_list ap;
+ va_start(ap, msg);
+ fputc('\n', stdout);
+ vprintf(arbitrary_messages[msg], ap);
+ fputc('\n', stdout);
+ 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 full 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;
+ }
+ va_end(ap);
+}