(root)/
bison-3.8.2/
src/
glyphs.c
       1  /* Graphical symbols.
       2  
       3     Copyright (C) 2020-2021 Free Software Foundation, Inc.
       4  
       5     This file is part of Bison, the GNU Compiler Compiler.
       6  
       7     This program is free software: you can redistribute it and/or modify
       8     it under the terms of the GNU General Public License as published by
       9     the Free Software Foundation, either version 3 of the License, or
      10     (at your option) any later version.
      11  
      12     This program is distributed in the hope that it will be useful,
      13     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15     GNU General Public License for more details.
      16  
      17     You should have received a copy of the GNU General Public License
      18     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      19  
      20  #include <config.h>
      21  
      22  #include "glyphs.h"
      23  
      24  #include <assert.h>
      25  #include <attribute.h>
      26  #include <stdbool.h>
      27  #include <string.h>
      28  #include <mbswidth.h>
      29  #include <unicodeio.h>
      30  
      31  
      32  glyph_buffer_t arrow;
      33  int arrow_width;
      34  
      35  glyph_buffer_t down_arrow;
      36  int down_arrow_width;
      37  
      38  glyph_buffer_t dot;
      39  int dot_width;
      40  
      41  glyph_buffer_t empty;
      42  int empty_width;
      43  
      44  const char *derivation_separator = " ";
      45  int derivation_separator_width = 1;
      46  
      47  typedef struct
      48  {
      49    glyph_buffer_t *pbuf;
      50    const char *fallback;
      51  } callback_arg_t;
      52  
      53  
      54  static long
      55  on_success (const char *buf, size_t buflen, void *callback_arg)
      56  {
      57    callback_arg_t *arg = (callback_arg_t *) callback_arg;
      58    assert (buflen + 1 < sizeof *arg->pbuf);
      59    *stpncpy (*arg->pbuf, buf, buflen) = '\0';
      60    return 1;
      61  }
      62  
      63  static long
      64  on_failure (unsigned code MAYBE_UNUSED, const char *msg MAYBE_UNUSED,
      65              void *callback_arg)
      66  {
      67    callback_arg_t *arg = (callback_arg_t *) callback_arg;
      68    assert (strlen (arg->fallback) + 1 < sizeof *arg->pbuf);
      69    strcpy (*arg->pbuf, arg->fallback);
      70    return 0;
      71  }
      72  
      73  static bool
      74  glyph_set (glyph_buffer_t *glyph, int *width,
      75             unsigned code, const char *fallback)
      76  {
      77    callback_arg_t arg = { glyph, fallback };
      78    int res = unicode_to_mb (code, on_success, on_failure, &arg);
      79    *width = mbswidth (*glyph, 0);
      80    return res;
      81  }
      82  
      83  void
      84  glyphs_init (void)
      85  {
      86    glyph_set (&arrow,      &arrow_width,      0x2192, "->");
      87    glyph_set (&dot,        &dot_width,        0x2022, ".");
      88    glyph_set (&down_arrow, &down_arrow_width, 0x21b3, "`->");
      89    glyph_set (&empty,      &empty_width,      0x03b5, "%empty");
      90  
      91    strncat (down_arrow, " ", sizeof down_arrow - strlen (down_arrow) - 1);
      92    down_arrow_width += 1;
      93  }