summaryrefslogtreecommitdiff
path: root/src/glyphs.c
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2020-07-19 16:05:15 +0200
committerAkim Demaille <akim.demaille@gmail.com>2020-07-19 17:09:01 +0200
commit744da03955fc60a6d2055cc4213d0853f587cafc (patch)
tree66eac26e3b1dd5e89fb694c0cce4860aafa235c6 /src/glyphs.c
parentb28d67b6b02d921a8d42427304afe1e2ae93b39c (diff)
downloadbison-744da03955fc60a6d2055cc4213d0853f587cafc.tar.gz
glyphs: fix types
The code was written on top of buffers of `char[26]`, and then was changed to use `char *`, yet was still using `sizeof buf`, which became `sizeof (char *)` instead of `sizeof (char[26])`. Reported by Dagobert Michelsen. https://lists.gnu.org/r/bug-bison/2020-07/msg00023.html * src/glyphs.h, src/glyphs.c: Get rid of uses of `char *`, use only glyph_buffer_t.
Diffstat (limited to 'src/glyphs.c')
-rw-r--r--src/glyphs.c42
1 files changed, 16 insertions, 26 deletions
diff --git a/src/glyphs.c b/src/glyphs.c
index b69659d8..dcd7f0c7 100644
--- a/src/glyphs.c
+++ b/src/glyphs.c
@@ -28,24 +28,17 @@
#include <mbswidth.h>
#include <unicodeio.h>
-// In gnulib/lib/unicodeio.h unicode_to_mb uses a buffer of 25 bytes.
-typedef char glyph_buffer_t[26];
-
-static glyph_buffer_t arrow_buf;
-const char *arrow;
+glyph_buffer_t arrow;
int arrow_width;
-static glyph_buffer_t down_arrow_buf;
-const char *down_arrow;
+glyph_buffer_t down_arrow;
int down_arrow_width;
-static glyph_buffer_t dot_buf;
-const char *dot;
+glyph_buffer_t dot;
int dot_width;
-static glyph_buffer_t empty_buf;
-const char *empty;
+glyph_buffer_t empty;
int empty_width;
const char *derivation_separator = " ";
@@ -53,8 +46,7 @@ int derivation_separator_width = 1;
typedef struct
{
- const char **glyph;
- char *buf;
+ glyph_buffer_t *pbuf;
const char *fallback;
} callback_arg_t;
@@ -63,8 +55,8 @@ static long
on_success (const char *buf, size_t buflen, void *callback_arg)
{
callback_arg_t *arg = (callback_arg_t *) callback_arg;
- assert (buflen + 1 < sizeof arg->buf);
- *stpncpy (arg->buf, buf, buflen) = '\0';
+ assert (buflen + 1 < sizeof *arg->pbuf);
+ *stpncpy (*arg->pbuf, buf, buflen) = '\0';
return 1;
}
@@ -73,19 +65,17 @@ on_failure (unsigned code MAYBE_UNUSED, const char *msg MAYBE_UNUSED,
void *callback_arg)
{
callback_arg_t *arg = (callback_arg_t *) callback_arg;
- assert (strlen (arg->fallback) + 1 < sizeof arg->buf);
- strcpy (arg->buf, arg->fallback);
+ assert (strlen (arg->fallback) + 1 < sizeof *arg->pbuf);
+ strcpy (*arg->pbuf, arg->fallback);
return 0;
}
static bool
-glyph_set (const char **glyph,
- char buf[26], int *width,
+glyph_set (glyph_buffer_t *glyph, int *width,
unsigned code, const char *fallback)
{
- callback_arg_t arg = { glyph, buf, fallback };
+ callback_arg_t arg = { glyph, fallback };
int res = unicode_to_mb (code, on_success, on_failure, &arg);
- *glyph = buf;
*width = mbswidth (*glyph, 0);
return res;
}
@@ -93,11 +83,11 @@ glyph_set (const char **glyph,
void
glyphs_init (void)
{
- glyph_set (&arrow, arrow_buf, &arrow_width, 0x2192, "->");
- glyph_set (&dot, dot_buf, &dot_width, 0x2022, ".");
- glyph_set (&down_arrow, down_arrow_buf, &down_arrow_width, 0x21b3, "`->");
- glyph_set (&empty, empty_buf, &empty_width, 0x03b5, "%empty");
+ glyph_set (&arrow, &arrow_width, 0x2192, "->");
+ glyph_set (&dot, &dot_width, 0x2022, ".");
+ glyph_set (&down_arrow, &down_arrow_width, 0x21b3, "`->");
+ glyph_set (&empty, &empty_width, 0x03b5, "%empty");
- strncat (down_arrow_buf, " ", sizeof down_arrow_buf - strlen (down_arrow_buf) - 1);
+ strncat (down_arrow, " ", sizeof down_arrow - strlen (down_arrow) - 1);
down_arrow_width += 1;
}