| 1 |
#include "output.h" |
| 2 |
|
| 3 |
|
| 4 |
#include "options.h" |
| 5 |
#include "memory.h" |
| 6 |
#include "word.h" |
| 7 |
#include "lists.h" |
| 8 |
#include "term.h" |
| 9 |
#include "syserr.h" |
| 10 |
#include "dictionary.h" |
| 11 |
#include "current.h" |
| 12 |
#include "msg.h" |
| 13 |
#include "readline.h" |
| 14 |
#include "instance.h" |
| 15 |
#include "converter.h" |
| 16 |
|
| 17 |
|
| 18 |
#ifdef HAVE_GLK |
| 19 |
#include "glkio.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
|
| 23 |
bool anyOutput = false; |
| 24 |
bool capitalize = false; |
| 25 |
bool needSpace = false; |
| 26 |
bool skipSpace = false; |
| 27 |
|
| 28 |
|
| 29 |
int col, lin; |
| 30 |
int pageLength, pageWidth; |
| 31 |
|
| 32 |
|
| 33 |
#ifdef HAVE_GLK |
| 34 |
strid_t commandLogFile; |
| 35 |
strid_t transcriptFile; |
| 36 |
#else |
| 37 |
FILE *commandLogFile; |
| 38 |
FILE *transcriptFile; |
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#if defined(HAVE_GLK) || defined(RUNNING_UNITTESTS) |
| 46 |
|
| 47 |
static int updateColumn(int currentColumn, char *string) { |
| 48 |
char *newlinePosition = strrchr(string, '\n'); |
| 49 |
if (newlinePosition != NULL) |
| 50 |
return &string[strlen(string)] - newlinePosition; |
| 51 |
else |
| 52 |
return currentColumn + strlen(string); |
| 53 |
} |
| 54 |
#endif |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
void setSubHeaderStyle(void) { |
| 59 |
#ifdef HAVE_GLK |
| 60 |
glk_set_style(style_Subheader); |
| 61 |
#endif |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
void setNormalStyle(void) { |
| 67 |
#ifdef HAVE_GLK |
| 68 |
glk_set_style(style_Normal); |
| 69 |
#endif |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
void newline(void) |
| 74 |
{ |
| 75 |
#ifndef HAVE_GLK |
| 76 |
char buf[256]; |
| 77 |
|
| 78 |
printAndLog("\n"); |
| 79 |
if ((!nopagingOption && !regressionTestOption) && lin == pageLength - 1) { |
| 80 |
needSpace = false; |
| 81 |
col = 0; |
| 82 |
lin = 0; |
| 83 |
printMessage(M_MORE); |
| 84 |
statusline(); |
| 85 |
fflush(stdout); |
| 86 |
if (fgets(buf, 256, stdin) == 0) |
| 87 |
; |
| 88 |
getPageSize(); |
| 89 |
} |
| 90 |
|
| 91 |
lin++; |
| 92 |
#else |
| 93 |
printAndLog("\n"); |
| 94 |
#endif |
| 95 |
col = 1; |
| 96 |
needSpace = false; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
void para(void) |
| 102 |
{ |
| 103 |
|
| 104 |
|
| 105 |
#ifdef HAVE_GLK |
| 106 |
if (glk_gestalt(gestalt_Graphics, 0) == 1) |
| 107 |
glk_window_flow_break(glkMainWin); |
| 108 |
#endif |
| 109 |
if (col != 1) |
| 110 |
newline(); |
| 111 |
newline(); |
| 112 |
capitalize = true; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
void clear(void) |
| 118 |
{ |
| 119 |
#ifdef HAVE_GLK |
| 120 |
glk_window_clear(glkMainWin); |
| 121 |
#else |
| 122 |
#ifdef HAVE_ANSI_CONTROL |
| 123 |
if (!statusLineOption) return; |
| 124 |
printf("\x1b[2J"); |
| 125 |
printf("\x1b[%d;1H", pageLength); |
| 126 |
#endif |
| 127 |
#endif |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
static void capitalizeFirst(char *str) { |
| 133 |
int i = 0; |
| 134 |
|
| 135 |
|
| 136 |
while (i < strlen(str) && isSpace(str[i])) i++; |
| 137 |
if (i < strlen(str)) { |
| 138 |
str[i] = toUpper(str[i]); |
| 139 |
capitalize = false; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
void printAndLog(char string[]) |
| 147 |
{ |
| 148 |
|
| 149 |
#ifdef HAVE_GLK |
| 150 |
printf("%s", string); |
| 151 |
#else |
| 152 |
char *outputString = ensureExternalEncoding(string); |
| 153 |
printf("%s", outputString); |
| 154 |
#endif |
| 155 |
|
| 156 |
#ifdef HAVE_GLK |
| 157 |
if (!onStatusLine && transcriptOption) { |
| 158 |
static int column = 0; |
| 159 |
char *stringCopy; |
| 160 |
char *stringPart; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
stringCopy = strdup(string); |
| 168 |
stringPart = stringCopy; |
| 169 |
while (strlen(stringPart) > 70-column) { |
| 170 |
int p; |
| 171 |
for (p = 70-column; p>0 && !isspace((int)stringPart[p]); p--); |
| 172 |
stringPart[p] = '\0'; |
| 173 |
char *converted = ensureExternalEncoding(stringPart); |
| 174 |
glk_put_string_stream(transcriptFile, converted); |
| 175 |
free(converted); |
| 176 |
glk_put_string_stream(transcriptFile, "\n"); |
| 177 |
column = 0; |
| 178 |
stringPart = &stringPart[p+1]; |
| 179 |
} |
| 180 |
char *converted = ensureExternalEncoding(stringPart); |
| 181 |
glk_put_string_stream(transcriptFile, converted); |
| 182 |
free(converted); |
| 183 |
column = updateColumn(column, stringPart); |
| 184 |
free(stringCopy); |
| 185 |
} |
| 186 |
#else |
| 187 |
if (!onStatusLine && transcriptOption) { |
| 188 |
fprintf(transcriptFile, "%s", outputString); |
| 189 |
fflush(transcriptFile); |
| 190 |
} |
| 191 |
free(outputString); |
| 192 |
#endif |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static void justify(char str[]) |
| 198 |
{ |
| 199 |
if (capitalize) |
| 200 |
capitalizeFirst(str); |
| 201 |
|
| 202 |
#ifdef HAVE_GLK |
| 203 |
printAndLog(str); |
| 204 |
#else |
| 205 |
int i; |
| 206 |
char ch; |
| 207 |
|
| 208 |
if (col >= pageWidth && !skipSpace) |
| 209 |
newline(); |
| 210 |
|
| 211 |
while (strlen(str) > pageWidth - col) { |
| 212 |
i = pageWidth - col - 1; |
| 213 |
while (!isSpace(str[i]) && i > 0) |
| 214 |
i--; |
| 215 |
if (i == 0 && col == 1) |
| 216 |
|
| 217 |
while (!isSpace(str[i]) && str[i] != '\0') |
| 218 |
i++; |
| 219 |
if (i > 0) { |
| 220 |
ch = str[i]; |
| 221 |
str[i] = '\0'; |
| 222 |
printAndLog(str); |
| 223 |
skipSpace = false; |
| 224 |
str[i] = ch; |
| 225 |
|
| 226 |
for (str = &str[i]; isSpace(str[0]) && str[0] != '\0'; str++); |
| 227 |
} |
| 228 |
newline(); |
| 229 |
while(isSpace(str[0])) str++; |
| 230 |
} |
| 231 |
printAndLog(str); |
| 232 |
#endif |
| 233 |
col = col + strlen(str); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static void space(void) |
| 239 |
{ |
| 240 |
if (skipSpace) |
| 241 |
skipSpace = false; |
| 242 |
else { |
| 243 |
if (needSpace) { |
| 244 |
printAndLog(" "); |
| 245 |
col++; |
| 246 |
} |
| 247 |
} |
| 248 |
needSpace = false; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static void sayPlayerWordsForParameter(int p) { |
| 254 |
int i; |
| 255 |
|
| 256 |
for (i = globalParameters[p].firstWord; i <= globalParameters[p].lastWord; i++) { |
| 257 |
justify((char *)pointerTo(dictionary[playerWords[i].code].string)); |
| 258 |
if (i < globalParameters[p].lastWord) |
| 259 |
justify(" "); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static void sayParameter(int p, int form) |
| 266 |
{ |
| 267 |
int i; |
| 268 |
|
| 269 |
for (i = 0; i <= p; i++) |
| 270 |
if (isEndOfArray(&globalParameters[i])) |
| 271 |
apperr("Nonexistent parameter referenced."); |
| 272 |
|
| 273 |
#ifdef ALWAYS_SAY_PARAMETERS_USING_PLAYER_WORDS |
| 274 |
if (params[p].firstWord != EOF) |
| 275 |
|
| 276 |
sayPlayerWordsForParameter(p); |
| 277 |
else |
| 278 |
sayForm(params[p].code, form); |
| 279 |
#else |
| 280 |
if (globalParameters[p].useWords) { |
| 281 |
|
| 282 |
sayPlayerWordsForParameter(p); |
| 283 |
} else |
| 284 |
sayForm(globalParameters[p].instance, form); |
| 285 |
#endif |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
static char *printSymbol(char str[]) |
| 309 |
{ |
| 310 |
int advance = 2; |
| 311 |
|
| 312 |
if (*str == '\0') printAndLog("$"); |
| 313 |
else switch (toLower(str[1])) { |
| 314 |
case 'n': |
| 315 |
newline(); |
| 316 |
needSpace = false; |
| 317 |
break; |
| 318 |
case 'i': |
| 319 |
newline(); |
| 320 |
printAndLog(" "); |
| 321 |
col = 5; |
| 322 |
needSpace = false; |
| 323 |
break; |
| 324 |
case 'o': |
| 325 |
space(); |
| 326 |
sayParameter(0, 0); |
| 327 |
needSpace = true; |
| 328 |
break; |
| 329 |
case '+': |
| 330 |
case '0': |
| 331 |
case '-': |
| 332 |
case '!': |
| 333 |
space(); |
| 334 |
if (isdigit((int)str[2])) { |
| 335 |
int form; |
| 336 |
switch (str[1]) { |
| 337 |
case '+': form = SAY_DEFINITE; break; |
| 338 |
case '0': form = SAY_INDEFINITE; break; |
| 339 |
case '-': form = SAY_NEGATIVE; break; |
| 340 |
case '!': form = SAY_PRONOUN; break; |
| 341 |
default: form = SAY_SIMPLE; break; |
| 342 |
} |
| 343 |
sayParameter(str[2]-'1', form); |
| 344 |
needSpace = true; |
| 345 |
} |
| 346 |
advance = 3; |
| 347 |
break; |
| 348 |
case '1': |
| 349 |
case '2': |
| 350 |
case '3': |
| 351 |
case '4': |
| 352 |
case '5': |
| 353 |
case '6': |
| 354 |
case '7': |
| 355 |
case '8': |
| 356 |
case '9': |
| 357 |
space(); |
| 358 |
sayParameter(str[1]-'1', SAY_SIMPLE); |
| 359 |
needSpace = true; |
| 360 |
break; |
| 361 |
case 'l': |
| 362 |
space(); |
| 363 |
say(current.location); |
| 364 |
needSpace = true; |
| 365 |
break; |
| 366 |
case 'a': |
| 367 |
space(); |
| 368 |
say(current.actor); |
| 369 |
needSpace = true; |
| 370 |
break; |
| 371 |
case 'v': |
| 372 |
space(); |
| 373 |
justify((char *)pointerTo(dictionary[verbWord].string)); |
| 374 |
needSpace = true; |
| 375 |
break; |
| 376 |
case 'p': |
| 377 |
para(); |
| 378 |
needSpace = false; |
| 379 |
break; |
| 380 |
case 't': { |
| 381 |
int i; |
| 382 |
int spaces = 4-(col-1)%4; |
| 383 |
|
| 384 |
for (i = 0; i<spaces; i++) printAndLog(" "); |
| 385 |
col = col + spaces; |
| 386 |
needSpace = false; |
| 387 |
break; |
| 388 |
} |
| 389 |
case '$': |
| 390 |
skipSpace = true; |
| 391 |
capitalize = false; |
| 392 |
break; |
| 393 |
case '_': |
| 394 |
advance = 2; |
| 395 |
printAndLog("$"); |
| 396 |
break; |
| 397 |
default: |
| 398 |
advance = 1; |
| 399 |
printAndLog("$"); |
| 400 |
break; |
| 401 |
} |
| 402 |
|
| 403 |
return &str[advance]; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
static bool inhibitSpace(char *str) { |
| 409 |
return str[0] != '\0' && str[0] == '$' && str[1] == '$'; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
static bool isSpaceEquivalent(char str[]) { |
| 415 |
if (str[0] == ' ') |
| 416 |
return true; |
| 417 |
else |
| 418 |
return strncmp(str, "$p", 2) == 0 |
| 419 |
|| strncmp(str, "$n", 2) == 0 |
| 420 |
|| strncmp(str, "$i", 2) == 0 |
| 421 |
|| strncmp(str, "$t", 2) == 0; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
static bool punctuationNext(char *str) { |
| 427 |
char *punctuation = strchr(".,!?", str[0]); |
| 428 |
bool end = str[1] == '\0'; |
| 429 |
bool space = isSpaceEquivalent(&str[1]); |
| 430 |
return (punctuation != NULL && (end || space)); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static char lastCharOf(char *str) { |
| 436 |
return str[strlen(str)-1]; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
void output(char original[]) |
| 442 |
{ |
| 443 |
char ch; |
| 444 |
char *str, *copy; |
| 445 |
char *symptr; |
| 446 |
|
| 447 |
if (strlen(original) == 0) |
| 448 |
return; |
| 449 |
|
| 450 |
copy = strdup(original); |
| 451 |
str = copy; |
| 452 |
|
| 453 |
if (inhibitSpace(str) || punctuationNext(str)) |
| 454 |
needSpace = false; |
| 455 |
else |
| 456 |
space(); |
| 457 |
|
| 458 |
|
| 459 |
while ((symptr = strchr(str, '$')) != (char *) NULL) { |
| 460 |
ch = *symptr; |
| 461 |
*symptr = '\0'; |
| 462 |
if (strlen(str) > 0) { |
| 463 |
skipSpace = false; |
| 464 |
|
| 465 |
if (lastCharOf(str) == ' ') { |
| 466 |
str[strlen(str)-1] = '\0'; |
| 467 |
justify(str); |
| 468 |
needSpace = true; |
| 469 |
} else { |
| 470 |
justify(str); |
| 471 |
needSpace = false; |
| 472 |
} |
| 473 |
} |
| 474 |
*symptr = ch; |
| 475 |
str = printSymbol(symptr); |
| 476 |
} |
| 477 |
|
| 478 |
if (str[0] != 0) { |
| 479 |
justify(str); |
| 480 |
skipSpace = false; |
| 481 |
if (lastCharOf(str) != ' ') |
| 482 |
needSpace = true; |
| 483 |
} |
| 484 |
if (needSpace) |
| 485 |
capitalize = strchr("!?.", str[strlen(str)-1]) != 0; |
| 486 |
anyOutput = true; |
| 487 |
free(copy); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
bool confirm(MsgKind msgno) |
| 493 |
{ |
| 494 |
char buf[80]; |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
printMessage(msgno); |
| 499 |
|
| 500 |
#ifdef USE_READLINE |
| 501 |
if (!readline(buf)) return true; |
| 502 |
#else |
| 503 |
if (gets(buf) == NULL) return true; |
| 504 |
#endif |
| 505 |
col = 1; |
| 506 |
|
| 507 |
return (buf[0] == '\0'); |
| 508 |
} |