gnulib: update
[bison.git] / src / glyphs.c
blobd93fd8e951df935ebf29bc37a692e5bc3ba1fac5
1 /* Graphical symbols.
3 Copyright (C) 2020-2021 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
20 #include <config.h>
22 #include "glyphs.h"
24 #include <assert.h>
25 #include <attribute.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <mbswidth.h>
29 #include <unicodeio.h>
32 glyph_buffer_t arrow;
33 int arrow_width;
35 glyph_buffer_t down_arrow;
36 int down_arrow_width;
38 glyph_buffer_t dot;
39 int dot_width;
41 glyph_buffer_t empty;
42 int empty_width;
44 const char *derivation_separator = " ";
45 int derivation_separator_width = 1;
47 typedef struct
49 glyph_buffer_t *pbuf;
50 const char *fallback;
51 } callback_arg_t;
54 static long
55 on_success (const char *buf, size_t buflen, void *callback_arg)
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;
63 static long
64 on_failure (unsigned code MAYBE_UNUSED, const char *msg MAYBE_UNUSED,
65 void *callback_arg)
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;
73 static bool
74 glyph_set (glyph_buffer_t *glyph, int *width,
75 unsigned code, const char *fallback)
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;
83 void
84 glyphs_init (void)
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");
91 strncat (down_arrow, " ", sizeof down_arrow - strlen (down_arrow) - 1);
92 down_arrow_width += 1;