Source:NetHack 3.6.5/src/pline.c
Jump to navigation
Jump to search
Below is the full text to pline.c from the source code of NetHack 3.6.5. To link to a particular line, write [[Source:NetHack 3.6.5/src/pline.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 3.6 pline.c $NHDT-Date: 1549327495 2019/02/05 00:44:55 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.73 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2018. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #define NEED_VARARGS /* Uses ... */ /* comment line for pre-compiled headers */ 7. #include "hack.h" 8. 9. #define BIGBUFSZ (5 * BUFSZ) /* big enough to format a 4*BUFSZ string (from 10. * config file parsing) with modest decoration; 11. * result will then be truncated to BUFSZ-1 */ 12. 13. static unsigned pline_flags = 0; 14. static char prevmsg[BUFSZ]; 15. 16. static void FDECL(putmesg, (const char *)); 17. static char *FDECL(You_buf, (int)); 18. #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__)) 19. static void FDECL(execplinehandler, (const char *)); 20. #endif 21. 22. #ifdef DUMPLOG 23. /* also used in end.c */ 24. unsigned saved_pline_index = 0; /* slot in saved_plines[] to use next */ 25. char *saved_plines[DUMPLOG_MSG_COUNT] = { (char *) 0 }; 26. 27. /* keep the most recent DUMPLOG_MSG_COUNT messages */ 28. void 29. dumplogmsg(line) 30. const char *line; 31. { 32. /* 33. * TODO: 34. * This essentially duplicates message history, which is 35. * currently implemented in an interface-specific manner. 36. * The core should take responsibility for that and have 37. * this share it. 38. */ 39. unsigned indx = saved_pline_index; /* next slot to use */ 40. char *oldest = saved_plines[indx]; /* current content of that slot */ 41. 42. if (oldest && strlen(oldest) >= strlen(line)) { 43. /* this buffer will gradually shrink until the 'else' is needed; 44. there's no pressing need to track allocation size instead */ 45. Strcpy(oldest, line); 46. } else { 47. if (oldest) 48. free((genericptr_t) oldest); 49. saved_plines[indx] = dupstr(line); 50. } 51. saved_pline_index = (indx + 1) % DUMPLOG_MSG_COUNT; 52. } 53. 54. /* called during save (unlike the interface-specific message history, 55. this data isn't saved and restored); end-of-game releases saved_pline[] 56. while writing its contents to the final dump log */ 57. void 58. dumplogfreemessages() 59. { 60. unsigned indx; 61. 62. for (indx = 0; indx < DUMPLOG_MSG_COUNT; ++indx) 63. if (saved_plines[indx]) 64. free((genericptr_t) saved_plines[indx]), saved_plines[indx] = 0; 65. saved_pline_index = 0; 66. } 67. #endif 68. 69. /* keeps windowprocs usage out of pline() */ 70. static void 71. putmesg(line) 72. const char *line; 73. { 74. int attr = ATR_NONE; 75. 76. if ((pline_flags & URGENT_MESSAGE) != 0 77. && (windowprocs.wincap2 & WC2_URGENT_MESG) != 0) 78. attr |= ATR_URGENT; 79. if ((pline_flags & SUPPRESS_HISTORY) != 0 80. && (windowprocs.wincap2 & WC2_SUPPRESS_HIST) != 0) 81. attr |= ATR_NOHISTORY; 82. 83. putstr(WIN_MESSAGE, attr, line); 84. } 85. 86. /* Note that these declarations rely on knowledge of the internals 87. * of the variable argument handling stuff in "tradstdc.h" 88. */ 89. 90. #if defined(USE_STDARG) || defined(USE_VARARGS) 91. static void FDECL(vpline, (const char *, va_list)); 92. 93. /*VARARGS1*/ 94. void 95. pline 96. VA_DECL(const char *, line) 97. { 98. VA_START(line); 99. VA_INIT(line, char *); 100. vpline(line, VA_ARGS); 101. VA_END(); 102. } 103. 104. # ifdef USE_STDARG 105. static void 106. vpline(const char *line, va_list the_args) 107. # else 108. static void 109. vpline(line, the_args) 110. const char *line; 111. va_list the_args; 112. # endif 113. 114. #else /* USE_STDARG | USE_VARARG */ 115. 116. # define vpline pline 117. 118. /*VARARGS1*/ 119. void 120. pline 121. VA_DECL(const char *, line) 122. #endif /* USE_STDARG | USE_VARARG */ 123. { /* start of vpline() or of nested block in USE_OLDARG's pline() */ 124. static int in_pline = 0; 125. char pbuf[BIGBUFSZ]; /* will get chopped down to BUFSZ-1 if longer */ 126. int ln; 127. int msgtyp; 128. #if !defined(NO_VSNPRINTF) 129. int vlen = 0; 130. #endif 131. boolean no_repeat; 132. /* Do NOT use VA_START and VA_END in here... see above */ 133. 134. if (!line || !*line) 135. return; 136. #ifdef HANGUPHANDLING 137. if (program_state.done_hup) 138. return; 139. #endif 140. if (program_state.wizkit_wishing) 141. return; 142. 143. if (index(line, '%')) { 144. #if !defined(NO_VSNPRINTF) 145. vlen = vsnprintf(pbuf, sizeof pbuf, line, VA_ARGS); 146. #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) && defined(DEBUG) 147. if (vlen >= (int) sizeof pbuf) 148. panic("%s: truncation of buffer at %zu of %d bytes", 149. "pline", sizeof pbuf, vlen); 150. #endif 151. #else 152. Vsprintf(pbuf, line, VA_ARGS); 153. #endif 154. line = pbuf; 155. } 156. if ((ln = (int) strlen(line)) > BUFSZ - 1) { 157. if (line != pbuf) /* no '%' was present */ 158. (void) strncpy(pbuf, line, BUFSZ - 1); /* caveat: unterminated */ 159. /* truncate, preserving the final 3 characters: 160. "___ extremely long text" -> "___ extremely l...ext" 161. (this may be suboptimal if overflow is less than 3) */ 162. (void) strncpy(pbuf + BUFSZ - 1 - 6, "...", 3); 163. /* avoid strncpy; buffers could overlap if excess is small */ 164. pbuf[BUFSZ - 1 - 3] = line[ln - 3]; 165. pbuf[BUFSZ - 1 - 2] = line[ln - 2]; 166. pbuf[BUFSZ - 1 - 1] = line[ln - 1]; 167. pbuf[BUFSZ - 1] = '\0'; 168. line = pbuf; 169. } 170. 171. #ifdef DUMPLOG 172. /* We hook here early to have options-agnostic output. 173. * Unfortunately, that means Norep() isn't honored (general issue) and 174. * that short lines aren't combined into one longer one (tty behavior). 175. */ 176. if ((pline_flags & SUPPRESS_HISTORY) == 0) 177. dumplogmsg(line); 178. #endif 179. /* use raw_print() if we're called too early (or perhaps too late 180. during shutdown) or if we're being called recursively (probably 181. via debugpline() in the interface code) */ 182. if (in_pline++ || !iflags.window_inited) { 183. /* [we should probably be using raw_printf("\n%s", line) here] */ 184. raw_print(line); 185. iflags.last_msg = PLNMSG_UNKNOWN; 186. goto pline_done; 187. } 188. 189. msgtyp = MSGTYP_NORMAL; 190. no_repeat = (pline_flags & PLINE_NOREPEAT) ? TRUE : FALSE; 191. if ((pline_flags & OVERRIDE_MSGTYPE) == 0) { 192. msgtyp = msgtype_type(line, no_repeat); 193. if ((pline_flags & URGENT_MESSAGE) == 0 194. && (msgtyp == MSGTYP_NOSHOW 195. || (msgtyp == MSGTYP_NOREP && !strcmp(line, prevmsg)))) 196. /* FIXME: we need a way to tell our caller that this message 197. * was suppressed so that caller doesn't set iflags.last_msg 198. * for something that hasn't been shown, otherwise a subsequent 199. * message which uses alternate wording based on that would be 200. * doing so out of context and probably end up seeming silly. 201. * (Not an issue for no-repeat but matters for no-show.) 202. */ 203. goto pline_done; 204. } 205. 206. if (vision_full_recalc) 207. vision_recalc(0); 208. if (u.ux) 209. flush_screen(1); /* %% */ 210. 211. putmesg(line); 212. 213. #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__)) 214. execplinehandler(line); 215. #endif 216. 217. /* this gets cleared after every pline message */ 218. iflags.last_msg = PLNMSG_UNKNOWN; 219. (void) strncpy(prevmsg, line, BUFSZ), prevmsg[BUFSZ - 1] = '\0'; 220. if (msgtyp == MSGTYP_STOP) 221. display_nhwindow(WIN_MESSAGE, TRUE); /* --more-- */ 222. 223. pline_done: 224. --in_pline; 225. return; 226. 227. #if !(defined(USE_STDARG) || defined(USE_VARARGS)) 228. /* provide closing brace for the nested block 229. which immediately follows USE_OLDARGS's VA_DECL() */ 230. VA_END(); 231. #endif 232. } 233. 234. /* pline() variant which can override MSGTYPE handling or suppress 235. message history (tty interface uses pline() to issue prompts and 236. they shouldn't be blockable via MSGTYPE=hide) */ 237. /*VARARGS2*/ 238. void custompline 239. VA_DECL2(unsigned, pflags, const char *, line) 240. { 241. VA_START(line); 242. VA_INIT(line, const char *); 243. pline_flags = pflags; 244. vpline(line, VA_ARGS); 245. pline_flags = 0; 246. VA_END(); 247. return; 248. } 249. 250. /*VARARGS1*/ 251. void Norep 252. VA_DECL(const char *, line) 253. { 254. VA_START(line); 255. VA_INIT(line, const char *); 256. pline_flags = PLINE_NOREPEAT; 257. vpline(line, VA_ARGS); 258. pline_flags = 0; 259. VA_END(); 260. return; 261. } 262. 263. /* work buffer for You(), &c and verbalize() */ 264. static char *you_buf = 0; 265. static int you_buf_siz = 0; 266. 267. static char * 268. You_buf(siz) 269. int siz; 270. { 271. if (siz > you_buf_siz) { 272. if (you_buf) 273. free((genericptr_t) you_buf); 274. you_buf_siz = siz + 10; 275. you_buf = (char *) alloc((unsigned) you_buf_siz); 276. } 277. return you_buf; 278. } 279. 280. void 281. free_youbuf() 282. { 283. if (you_buf) 284. free((genericptr_t) you_buf), you_buf = (char *) 0; 285. you_buf_siz = 0; 286. } 287. 288. /* `prefix' must be a string literal, not a pointer */ 289. #define YouPrefix(pointer, prefix, text) \ 290. Strcpy((pointer = You_buf((int) (strlen(text) + sizeof prefix))), prefix) 291. 292. #define YouMessage(pointer, prefix, text) \ 293. strcat((YouPrefix(pointer, prefix, text), pointer), text) 294. 295. /*VARARGS1*/ 296. void You 297. VA_DECL(const char *, line) 298. { 299. char *tmp; 300. 301. VA_START(line); 302. VA_INIT(line, const char *); 303. vpline(YouMessage(tmp, "You ", line), VA_ARGS); 304. VA_END(); 305. } 306. 307. /*VARARGS1*/ 308. void Your 309. VA_DECL(const char *, line) 310. { 311. char *tmp; 312. 313. VA_START(line); 314. VA_INIT(line, const char *); 315. vpline(YouMessage(tmp, "Your ", line), VA_ARGS); 316. VA_END(); 317. } 318. 319. /*VARARGS1*/ 320. void You_feel 321. VA_DECL(const char *, line) 322. { 323. char *tmp; 324. 325. VA_START(line); 326. VA_INIT(line, const char *); 327. if (Unaware) 328. YouPrefix(tmp, "You dream that you feel ", line); 329. else 330. YouPrefix(tmp, "You feel ", line); 331. vpline(strcat(tmp, line), VA_ARGS); 332. VA_END(); 333. } 334. 335. /*VARARGS1*/ 336. void You_cant 337. VA_DECL(const char *, line) 338. { 339. char *tmp; 340. 341. VA_START(line); 342. VA_INIT(line, const char *); 343. vpline(YouMessage(tmp, "You can't ", line), VA_ARGS); 344. VA_END(); 345. } 346. 347. /*VARARGS1*/ 348. void pline_The 349. VA_DECL(const char *, line) 350. { 351. char *tmp; 352. 353. VA_START(line); 354. VA_INIT(line, const char *); 355. vpline(YouMessage(tmp, "The ", line), VA_ARGS); 356. VA_END(); 357. } 358. 359. /*VARARGS1*/ 360. void There 361. VA_DECL(const char *, line) 362. { 363. char *tmp; 364. 365. VA_START(line); 366. VA_INIT(line, const char *); 367. vpline(YouMessage(tmp, "There ", line), VA_ARGS); 368. VA_END(); 369. } 370. 371. /*VARARGS1*/ 372. void You_hear 373. VA_DECL(const char *, line) 374. { 375. char *tmp; 376. 377. if (Deaf || !flags.acoustics) 378. return; 379. VA_START(line); 380. VA_INIT(line, const char *); 381. if (Underwater) 382. YouPrefix(tmp, "You barely hear ", line); 383. else if (Unaware) 384. YouPrefix(tmp, "You dream that you hear ", line); 385. else 386. YouPrefix(tmp, "You hear ", line); /* Deaf-aware */ 387. vpline(strcat(tmp, line), VA_ARGS); 388. VA_END(); 389. } 390. 391. /*VARARGS1*/ 392. void You_see 393. VA_DECL(const char *, line) 394. { 395. char *tmp; 396. 397. VA_START(line); 398. VA_INIT(line, const char *); 399. if (Unaware) 400. YouPrefix(tmp, "You dream that you see ", line); 401. else if (Blind) /* caller should have caught this... */ 402. YouPrefix(tmp, "You sense ", line); 403. else 404. YouPrefix(tmp, "You see ", line); 405. vpline(strcat(tmp, line), VA_ARGS); 406. VA_END(); 407. } 408. 409. /* Print a message inside double-quotes. 410. * The caller is responsible for checking deafness. 411. * Gods can speak directly to you in spite of deafness. 412. */ 413. /*VARARGS1*/ 414. void verbalize 415. VA_DECL(const char *, line) 416. { 417. char *tmp; 418. 419. VA_START(line); 420. VA_INIT(line, const char *); 421. tmp = You_buf((int) strlen(line) + sizeof "\"\""); 422. Strcpy(tmp, "\""); 423. Strcat(tmp, line); 424. Strcat(tmp, "\""); 425. vpline(tmp, VA_ARGS); 426. VA_END(); 427. } 428. 429. /*VARARGS1*/ 430. /* Note that these declarations rely on knowledge of the internals 431. * of the variable argument handling stuff in "tradstdc.h" 432. */ 433. 434. #if defined(USE_STDARG) || defined(USE_VARARGS) 435. static void FDECL(vraw_printf, (const char *, va_list)); 436. 437. void raw_printf 438. VA_DECL(const char *, line) 439. { 440. VA_START(line); 441. VA_INIT(line, char *); 442. vraw_printf(line, VA_ARGS); 443. VA_END(); 444. } 445. 446. # ifdef USE_STDARG 447. static void 448. vraw_printf(const char *line, va_list the_args) 449. # else 450. static void 451. vraw_printf(line, the_args) 452. const char *line; 453. va_list the_args; 454. # endif 455. 456. #else /* USE_STDARG | USE_VARARG */ 457. 458. void raw_printf 459. VA_DECL(const char *, line) 460. #endif 461. { 462. char pbuf[BIGBUFSZ]; /* will be chopped down to BUFSZ-1 if longer */ 463. /* Do NOT use VA_START and VA_END in here... see above */ 464. 465. if (index(line, '%')) { 466. #if !defined(NO_VSNPRINTF) 467. (void) vsnprintf(pbuf, sizeof pbuf, line, VA_ARGS); 468. #else 469. Vsprintf(pbuf, line, VA_ARGS); 470. #endif 471. line = pbuf; 472. } 473. if ((int) strlen(line) > BUFSZ - 1) { 474. if (line != pbuf) 475. line = strncpy(pbuf, line, BUFSZ - 1); 476. /* unlike pline, we don't futz around to keep last few chars */ 477. pbuf[BUFSZ - 1] = '\0'; /* terminate strncpy or truncate vsprintf */ 478. } 479. raw_print(line); 480. #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__)) 481. execplinehandler(line); 482. #endif 483. #if !(defined(USE_STDARG) || defined(USE_VARARGS)) 484. VA_END(); /* (see vpline) */ 485. #endif 486. } 487. 488. /*VARARGS1*/ 489. void impossible 490. VA_DECL(const char *, s) 491. { 492. char pbuf[BIGBUFSZ]; /* will be chopped down to BUFSZ-1 if longer */ 493. 494. VA_START(s); 495. VA_INIT(s, const char *); 496. if (program_state.in_impossible) 497. panic("impossible called impossible"); 498. 499. program_state.in_impossible = 1; 500. #if !defined(NO_VSNPRINTF) 501. (void) vsnprintf(pbuf, sizeof pbuf, s, VA_ARGS); 502. #else 503. Vsprintf(pbuf, s, VA_ARGS); 504. #endif 505. pbuf[BUFSZ - 1] = '\0'; /* sanity */ 506. paniclog("impossible", pbuf); 507. if (iflags.debug_fuzzer) 508. panic("%s", pbuf); 509. pline("%s", VA_PASS1(pbuf)); 510. /* reuse pbuf[] */ 511. Strcpy(pbuf, "Program in disorder!"); 512. if (program_state.something_worth_saving) 513. Strcat(pbuf, " (Saving and reloading may fix this problem.)"); 514. pline("%s", VA_PASS1(pbuf)); 515. 516. program_state.in_impossible = 0; 517. VA_END(); 518. } 519. 520. #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__)) 521. static boolean use_pline_handler = TRUE; 522. 523. static void 524. execplinehandler(line) 525. const char *line; 526. { 527. int f; 528. const char *args[3]; 529. char *env; 530. 531. if (!use_pline_handler) 532. return; 533. 534. if (!(env = nh_getenv("NETHACK_MSGHANDLER"))) { 535. use_pline_handler = FALSE; 536. return; 537. } 538. 539. f = fork(); 540. if (f == 0) { /* child */ 541. args[0] = env; 542. args[1] = line; 543. args[2] = NULL; 544. (void) setgid(getgid()); 545. (void) setuid(getuid()); 546. (void) execv(args[0], (char *const *) args); 547. perror((char *) 0); 548. (void) fprintf(stderr, "Exec to message handler %s failed.\n", env); 549. nh_terminate(EXIT_FAILURE); 550. } else if (f > 0) { 551. int status; 552. 553. waitpid(f, &status, 0); 554. } else if (f == -1) { 555. perror((char *) 0); 556. use_pline_handler = FALSE; 557. pline("%s", VA_PASS1("Fork to message handler failed.")); 558. } 559. } 560. #endif /* MSGHANDLER && (POSIX_TYPES || __GNUC__) */ 561. 562. /* 563. * varargs handling for files.c 564. */ 565. #if defined(USE_STDARG) || defined(USE_VARARGS) 566. static void FDECL(vconfig_error_add, (const char *, va_list)); 567. 568. /*VARARGS1*/ 569. void 570. config_error_add 571. VA_DECL(const char *, str) 572. { 573. VA_START(str); 574. VA_INIT(str, char *); 575. vconfig_error_add(str, VA_ARGS); 576. VA_END(); 577. } 578. 579. # ifdef USE_STDARG 580. static void 581. vconfig_error_add(const char *str, va_list the_args) 582. # else 583. static void 584. vconfig_error_add(str, the_args) 585. const char *str; 586. va_list the_args; 587. # endif 588. 589. #else /* !(USE_STDARG || USE_VARARG) => USE_OLDARGS */ 590. 591. /*VARARGS1*/ 592. void 593. config_error_add 594. VA_DECL(const char *, str) 595. #endif /* ?(USE_STDARG || USE_VARARG) */ 596. { /* start of vconf...() or of nested block in USE_OLDARG's conf...() */ 597. #if !defined(NO_VSNPRINTF) 598. int vlen = 0; 599. #endif 600. char buf[BIGBUFSZ]; /* will be chopped down to BUFSZ-1 if longer */ 601. 602. #if !defined(NO_VSNPRINTF) 603. vlen = vsnprintf(buf, sizeof buf, str, VA_ARGS); 604. #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) && defined(DEBUG) 605. if (vlen >= (int) sizeof buf) 606. panic("%s: truncation of buffer at %zu of %d bytes", 607. "config_error_add", sizeof buf, vlen); 608. #endif 609. #else 610. Vsprintf(buf, str, VA_ARGS); 611. #endif 612. buf[BUFSZ - 1] = '\0'; 613. config_erradd(buf); 614. 615. #if !(defined(USE_STDARG) || defined(USE_VARARGS)) 616. VA_END(); /* (see pline/vpline -- ends nested block for USE_OLDARGS) */ 617. #endif 618. } 619. 620. /*pline.c*/