Source:NetHack 3.6.1/src/pline.c

From NetHackWiki
(Redirected from Source:Ref/verbalize)
Jump to: navigation, search

Below is the full text to pline.c from the source code of NetHack 3.6.1. To link to a particular line, write [[Source:NetHack 3.6.1/src/pline.c#line123]], for example.

Top of file

  1.  /* NetHack 3.6	pline.c	$NHDT-Date: 1520964541 2018/03/13 18:09:01 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.66 $ */
  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. */

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.  
  2.  #define NEED_VARARGS /* Uses ... */ /* comment line for pre-compiled headers \
  3.                                         */
  4.  #include "hack.h"
  5.  
  6.  static unsigned pline_flags = 0;
  7.  static char prevmsg[BUFSZ];
  8.  
  9.  static char *FDECL(You_buf, (int));
  10.  #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__))
  11.  static void FDECL(execplinehandler, (const char *));
  12.  #endif
  13.  
  14.  #ifdef DUMPLOG
  15.  /* also used in end.c */
  16.  unsigned saved_pline_index = 0; /* slot in saved_plines[] to use next */
  17.  char *saved_plines[DUMPLOG_MSG_COUNT] = { (char *) 0 };
  18.  

dumplogmsg

  1.  /* keep the most recent DUMPLOG_MSG_COUNT messages */
  2.  void
  3.  dumplogmsg(line)
  4.  const char *line;
  5.  {
  6.      /*
  7.       * TODO:
  8.       *  This essentially duplicates message history, which is
  9.       *  currently implemented in an interface-specific manner.
  10.       *  The core should take responsibility for that and have
  11.       *  this share it.
  12.       */
  13.      unsigned indx = saved_pline_index; /* next slot to use */
  14.      char *oldest = saved_plines[indx]; /* current content of that slot */
  15.  
  16.      if (oldest && strlen(oldest) >= strlen(line)) {
  17.          /* this buffer will gradually shrink until the 'else' is needed;
  18.             there's no pressing need to track allocation size instead */
  19.          Strcpy(oldest, line);
  20.      } else {
  21.          if (oldest)
  22.              free((genericptr_t) oldest);
  23.          saved_plines[indx] = dupstr(line);
  24.      }
  25.      saved_pline_index = (indx + 1) % DUMPLOG_MSG_COUNT;
  26.  }
  27.  

dumplogfreemessages

  1.  /* called during save (unlike the interface-specific message history,
  2.     this data isn't saved and restored); end-of-game releases saved_pline[]
  3.     while writing its contents to the final dump log */
  4.  void
  5.  dumplogfreemessages()
  6.  {
  7.      unsigned indx;
  8.  
  9.      for (indx = 0; indx < DUMPLOG_MSG_COUNT; ++indx)
  10.          if (saved_plines[indx])
  11.              free((genericptr_t) saved_plines[indx]), saved_plines[indx] = 0;
  12.      saved_pline_index = 0;
  13.  }
  14.  #endif
  15.  
  16.  /*VARARGS1*/

pline

  1.  /* Note that these declarations rely on knowledge of the internals
  2.   * of the variable argument handling stuff in "tradstdc.h"
  3.   */
  4.  
  5.  #if defined(USE_STDARG) || defined(USE_VARARGS)
  6.  static void FDECL(vpline, (const char *, va_list));
  7.  
  8.  void pline
  9.  VA_DECL(const char *, line)
  10.  {
  11.      VA_START(line);
  12.      VA_INIT(line, char *);
  13.      vpline(line, VA_ARGS);
  14.      VA_END();
  15.  }
  16.  
  17.  # ifdef USE_STDARG
  18.  static void
  19.  vpline(const char *line, va_list the_args)
  20.  # else
  21.  static void
  22.  vpline(line, the_args)
  23.  const char *line;
  24.  va_list the_args;
  25.  # endif
  26.  
  27.  #else /* USE_STDARG | USE_VARARG */
  28.  
  29.  # define vpline pline
  30.  
  31.  void pline
  32.  VA_DECL(const char *, line)
  33.  #endif /* USE_STDARG | USE_VARARG */
  34.  {       /* start of vpline() or of nested block in USE_OLDARG's pline() */
  35.      static int in_pline = 0;
  36.      char pbuf[3 * BUFSZ];
  37.      int ln;
  38.      int msgtyp;
  39.      boolean no_repeat;
  40.      /* Do NOT use VA_START and VA_END in here... see above */
  41.  
  42.      if (!line || !*line)
  43.          return;
  44.  #ifdef HANGUPHANDLING
  45.      if (program_state.done_hup)
  46.          return;
  47.  #endif
  48.      if (program_state.wizkit_wishing)
  49.          return;
  50.  
  51.      if (index(line, '%')) {
  52.          Vsprintf(pbuf, line, VA_ARGS);
  53.          line = pbuf;
  54.      }
  55.      if ((ln = (int) strlen(line)) > BUFSZ - 1) {
  56.          if (line != pbuf)                          /* no '%' was present */
  57.              (void) strncpy(pbuf, line, BUFSZ - 1); /* caveat: unterminated */
  58.          /* truncate, preserving the final 3 characters:
  59.             "___ extremely long text" -> "___ extremely l...ext"
  60.             (this may be suboptimal if overflow is less than 3) */
  61.          (void) strncpy(pbuf + BUFSZ - 1 - 6, "...", 3);
  62.          /* avoid strncpy; buffers could overlap if excess is small */
  63.          pbuf[BUFSZ - 1 - 3] = line[ln - 3];
  64.          pbuf[BUFSZ - 1 - 2] = line[ln - 2];
  65.          pbuf[BUFSZ - 1 - 1] = line[ln - 1];
  66.          pbuf[BUFSZ - 1] = '\0';
  67.          line = pbuf;
  68.      }
  69.  
  70.  #ifdef DUMPLOG
  71.      /* We hook here early to have options-agnostic output.
  72.       * Unfortunately, that means Norep() isn't honored (general issue) and
  73.       * that short lines aren't combined into one longer one (tty behavior).
  74.       */
  75.      if ((pline_flags & SUPPRESS_HISTORY) == 0)
  76.          dumplogmsg(line);
  77.  #endif
  78.      /* use raw_print() if we're called too early (or perhaps too late
  79.         during shutdown) or if we're being called recursively (probably
  80.         via debugpline() in the interface code) */
  81.      if (in_pline++ || !iflags.window_inited) {
  82.          /* [we should probably be using raw_printf("\n%s", line) here] */
  83.          raw_print(line);
  84.          iflags.last_msg = PLNMSG_UNKNOWN;
  85.          goto pline_done;
  86.      }
  87.  
  88.      msgtyp = MSGTYP_NORMAL;
  89.      no_repeat = (pline_flags & PLINE_NOREPEAT) ? TRUE : FALSE;
  90.      if ((pline_flags & OVERRIDE_MSGTYPE) == 0) {
  91.          msgtyp = msgtype_type(line, no_repeat);
  92.          if (msgtyp == MSGTYP_NOSHOW
  93.              || (msgtyp == MSGTYP_NOREP && !strcmp(line, prevmsg)))
  94.              /* FIXME: we need a way to tell our caller that this message
  95.               * was suppressed so that caller doesn't set iflags.last_msg
  96.               * for something that hasn't been shown, otherwise a subsequent
  97.               * message which uses alternate wording based on that would be
  98.               * doing so out of context and probably end up seeming silly.
  99.               * (Not an issue for no-repeat but matters for no-show.)
  100.               */
  101.              goto pline_done;
  102.      }
  103.  
  104.      if (vision_full_recalc)
  105.          vision_recalc(0);
  106.      if (u.ux)
  107.          flush_screen(1); /* %% */
  108.  
  109.      putstr(WIN_MESSAGE, 0, line);
  110.  
  111.  #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__))
  112.      execplinehandler(line);
  113.  #endif
  114.  
  115.      /* this gets cleared after every pline message */
  116.      iflags.last_msg = PLNMSG_UNKNOWN;
  117.      (void) strncpy(prevmsg, line, BUFSZ), prevmsg[BUFSZ - 1] = '\0';
  118.      if (msgtyp == MSGTYP_STOP)
  119.          display_nhwindow(WIN_MESSAGE, TRUE); /* --more-- */
  120.  
  121.   pline_done:
  122.      --in_pline;
  123.      return;
  124.  
  125.  #if !(defined(USE_STDARG) || defined(USE_VARARGS))
  126.      /* provide closing brace for the nested block
  127.         which immediately follows USE_OLDARGS's VA_DECL() */
  128.      VA_END();
  129.  #endif
  130.  }
  131.  

custompline

  1.  /* pline() variant which can override MSGTYPE handling or suppress
  2.     message history (tty interface uses pline() to issue prompts and
  3.     they shouldn't be blockable via MSGTYPE=hide) */
  4.  /*VARARGS2*/
  5.  void custompline
  6.  VA_DECL2(unsigned, pflags, const char *, line)
  7.  {
  8.      VA_START(line);
  9.      VA_INIT(line, const char *);
  10.      pline_flags = pflags;
  11.      vpline(line, VA_ARGS);
  12.      pline_flags = 0;
  13.      VA_END();
  14.      return;
  15.  }
  16.  
  17.  /*VARARGS1*/

Norep

  1.  void Norep
  2.  VA_DECL(const char *, line)
  3.  {
  4.      VA_START(line);
  5.      VA_INIT(line, const char *);
  6.      pline_flags = PLINE_NOREPEAT;
  7.      vpline(line, VA_ARGS);
  8.      pline_flags = 0;
  9.      VA_END();
  10.      return;
  11.  }
  12.  
  13.  /* work buffer for You(), &c and verbalize() */
  14.  static char *you_buf = 0;
  15.  static int you_buf_siz = 0;
  16.  

You_buf

  1.  static char *
  2.  You_buf(siz)
  3.  int siz;
  4.  {
  5.      if (siz > you_buf_siz) {
  6.          if (you_buf)
  7.              free((genericptr_t) you_buf);
  8.          you_buf_siz = siz + 10;
  9.          you_buf = (char *) alloc((unsigned) you_buf_siz);
  10.      }
  11.      return you_buf;
  12.  }
  13.  

free_youbuf

  1.  void
  2.  free_youbuf()
  3.  {
  4.      if (you_buf)
  5.          free((genericptr_t) you_buf), you_buf = (char *) 0;
  6.      you_buf_siz = 0;
  7.  }
  8.  
  9.  /* `prefix' must be a string literal, not a pointer */
  10.  #define YouPrefix(pointer, prefix, text) \
  11.      Strcpy((pointer = You_buf((int) (strlen(text) + sizeof prefix))), prefix)
  12.  
  13.  #define YouMessage(pointer, prefix, text) \
  14.      strcat((YouPrefix(pointer, prefix, text), pointer), text)
  15.  
  16.  /*VARARGS1*/

You

  1.  void You
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.      VA_START(line);
  6.      VA_INIT(line, const char *);
  7.      vpline(YouMessage(tmp, "You ", line), VA_ARGS);
  8.      VA_END();
  9.  }
  10.  
  11.  /*VARARGS1*/

Your

  1.  void Your
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.      VA_START(line);
  6.      VA_INIT(line, const char *);
  7.      vpline(YouMessage(tmp, "Your ", line), VA_ARGS);
  8.      VA_END();
  9.  }
  10.  
  11.  /*VARARGS1*/

You_feel

  1.  void You_feel
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.      VA_START(line);
  6.      VA_INIT(line, const char *);
  7.      if (Unaware)
  8.          YouPrefix(tmp, "You dream that you feel ", line);
  9.      else
  10.          YouPrefix(tmp, "You feel ", line);
  11.      vpline(strcat(tmp, line), VA_ARGS);
  12.      VA_END();
  13.  }
  14.  
  15.  /*VARARGS1*/

You_cant

  1.  void You_cant
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.      VA_START(line);
  6.      VA_INIT(line, const char *);
  7.      vpline(YouMessage(tmp, "You can't ", line), VA_ARGS);
  8.      VA_END();
  9.  }
  10.  
  11.  /*VARARGS1*/

pline_The

  1.  void pline_The
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.      VA_START(line);
  6.      VA_INIT(line, const char *);
  7.      vpline(YouMessage(tmp, "The ", line), VA_ARGS);
  8.      VA_END();
  9.  }
  10.  
  11.  /*VARARGS1*/

There

  1.  void There
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.      VA_START(line);
  6.      VA_INIT(line, const char *);
  7.      vpline(YouMessage(tmp, "There ", line), VA_ARGS);
  8.      VA_END();
  9.  }
  10.  
  11.  /*VARARGS1*/

You_hear

  1.  void You_hear
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.  
  6.      if (Deaf || !flags.acoustics)
  7.          return;
  8.      VA_START(line);
  9.      VA_INIT(line, const char *);
  10.      if (Underwater)
  11.          YouPrefix(tmp, "You barely hear ", line);
  12.      else if (Unaware)
  13.          YouPrefix(tmp, "You dream that you hear ", line);
  14.      else
  15.          YouPrefix(tmp, "You hear ", line);
  16.      vpline(strcat(tmp, line), VA_ARGS);
  17.      VA_END();
  18.  }
  19.  
  20.  /*VARARGS1*/

You_see

  1.  void You_see
  2.  VA_DECL(const char *, line)
  3.  {
  4.      char *tmp;
  5.  
  6.      VA_START(line);
  7.      VA_INIT(line, const char *);
  8.      if (Unaware)
  9.          YouPrefix(tmp, "You dream that you see ", line);
  10.      else if (Blind) /* caller should have caught this... */
  11.          YouPrefix(tmp, "You sense ", line);
  12.      else
  13.          YouPrefix(tmp, "You see ", line);
  14.      vpline(strcat(tmp, line), VA_ARGS);
  15.      VA_END();
  16.  }
  17.  

verbalize

  1.  /* Print a message inside double-quotes.
  2.   * The caller is responsible for checking deafness.
  3.   * Gods can speak directly to you in spite of deafness.
  4.   */
  5.  /*VARARGS1*/
  6.  void verbalize
  7.  VA_DECL(const char *, line)
  8.  {
  9.      char *tmp;
  10.  
  11.      VA_START(line);
  12.      VA_INIT(line, const char *);
  13.      tmp = You_buf((int) strlen(line) + sizeof "\"\"");
  14.      Strcpy(tmp, "\"");
  15.      Strcat(tmp, line);
  16.      Strcat(tmp, "\"");
  17.      vpline(tmp, VA_ARGS);
  18.      VA_END();
  19.  }
  20.  
  21.  /*VARARGS1*/

raw_printf

  1.  /* Note that these declarations rely on knowledge of the internals
  2.   * of the variable argument handling stuff in "tradstdc.h"
  3.   */
  4.  
  5.  #if defined(USE_STDARG) || defined(USE_VARARGS)
  6.  static void FDECL(vraw_printf, (const char *, va_list));
  7.  
  8.  void raw_printf
  9.  VA_DECL(const char *, line)
  10.  {
  11.      VA_START(line);
  12.      VA_INIT(line, char *);
  13.      vraw_printf(line, VA_ARGS);
  14.      VA_END();
  15.  }
  16.  
  17.  # ifdef USE_STDARG
  18.  static void
  19.  vraw_printf(const char *line, va_list the_args)
  20.  # else
  21.  static void
  22.  vraw_printf(line, the_args)
  23.  const char *line;
  24.  va_list the_args;
  25.  # endif
  26.  
  27.  #else /* USE_STDARG | USE_VARARG */
  28.  
  29.  void raw_printf
  30.  VA_DECL(const char *, line)
  31.  #endif
  32.  {
  33.      char pbuf[3 * BUFSZ];
  34.      int ln;
  35.      /* Do NOT use VA_START and VA_END in here... see above */
  36.  
  37.      if (index(line, '%')) {
  38.          Vsprintf(pbuf, line, VA_ARGS);
  39.          line = pbuf;
  40.      }
  41.      if ((ln = (int) strlen(line)) > BUFSZ - 1) {
  42.          if (line != pbuf)
  43.              line = strncpy(pbuf, line, BUFSZ - 1);
  44.          /* unlike pline, we don't futz around to keep last few chars */
  45.          pbuf[BUFSZ - 1] = '\0'; /* terminate strncpy or truncate vsprintf */
  46.      }
  47.      raw_print(line);
  48.  #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__))
  49.      execplinehandler(line);
  50.  #endif
  51.  #if !(defined(USE_STDARG) || defined(USE_VARARGS))
  52.      VA_END(); /* (see vpline) */
  53.  #endif
  54.  }
  55.  
  56.  /*VARARGS1*/

impossible

  1.  void impossible
  2.  VA_DECL(const char *, s)
  3.  {
  4.      char pbuf[2 * BUFSZ];
  5.      VA_START(s);
  6.      VA_INIT(s, const char *);
  7.      if (program_state.in_impossible)
  8.          panic("impossible called impossible");
  9.  
  10.      program_state.in_impossible = 1;
  11.      Vsprintf(pbuf, s, VA_ARGS);
  12.      pbuf[BUFSZ - 1] = '\0'; /* sanity */
  13.      paniclog("impossible", pbuf);
  14.      pline("%s", VA_PASS1(pbuf));
  15.      pline(VA_PASS1(
  16.         "Program in disorder!  (Saving and reloading may fix this problem.)"));
  17.      program_state.in_impossible = 0;
  18.      VA_END();
  19.  }
  20.  

execplinehandler

  1.  #if defined(MSGHANDLER) && (defined(POSIX_TYPES) || defined(__GNUC__))
  2.  static boolean use_pline_handler = TRUE;
  3.  static void
  4.  execplinehandler(line)
  5.  const char *line;
  6.  {
  7.      int f;
  8.      const char *args[3];
  9.      char *env;
  10.  
  11.      if (!use_pline_handler)
  12.          return;
  13.  
  14.      if (!(env = nh_getenv("NETHACK_MSGHANDLER"))) {
  15.          use_pline_handler = FALSE;
  16.          return;
  17.      }
  18.  
  19.      f = fork();
  20.      if (f == 0) { /* child */
  21.          args[0] = env;
  22.          args[1] = line;
  23.          args[2] = NULL;
  24.          (void) setgid(getgid());
  25.          (void) setuid(getuid());
  26.          (void) execv(args[0], (char *const *) args);
  27.          perror((char *) 0);
  28.          (void) fprintf(stderr, "Exec to message handler %s failed.\n", env);
  29.          nh_terminate(EXIT_FAILURE);
  30.      } else if (f > 0) {
  31.          int status;
  32.  
  33.          waitpid(f, &status, 0);
  34.      } else if (f == -1) {
  35.          perror((char *) 0);
  36.          use_pline_handler = FALSE;
  37.          pline("%s", VA_PASS1("Fork to message handler failed."));
  38.      }
  39.  }
  40.  #endif /* defined(POSIX_TYPES) || defined(__GNUC__) */
  41.  
  42.  /*pline.c*/