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