aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2020-09-30 14:47:14 +0200
committerOmar Polo <op@omarpolo.com>2020-09-30 14:47:14 +0200
commit13cc0916743705e32211da7fcf5499197e36e883 (patch)
treebfdfc16324e32df0697a606d0c21e735c92597fd
parentecc6fa05e1c8a3082eb9612873a27f1ebf8ffcce (diff)
downloadstar-platinum-13cc0916743705e32211da7fcf5499197e36e883.tar.gz
star-platinum-13cc0916743705e32211da7fcf5499197e36e883.tar.bz2
improved error messages
yyerror now is a printf-like variadic function. This helps to generate custom error message like "invalid keysym %s" or "invalid key %s" during parsing.
-rw-r--r--lex.l13
-rw-r--r--parse.y4
-rw-r--r--star-platinum.c18
-rw-r--r--star-platinum.h2
4 files changed, 25 insertions, 12 deletions
diff --git a/lex.l b/lex.l
index 4e5955a..943a39c 100644
--- a/lex.l
+++ b/lex.l
@@ -67,9 +67,6 @@ KeySym key = 0;
<KEY>"\"" {
BEGIN(INITIAL);
- if (key == NoSymbol)
- return TERR;
-
yylval.key = (struct key){ state, key };
state = 0;
key = 0;
@@ -81,7 +78,7 @@ KeySym key = 0;
<KEY>M- { state |= Mod1Mask; }
<KEY>s- { state |= Mod4Mask; }
-<KEY><[_a-zA-Z]+> {
+<KEY><[^>]+> {
char *c;
if ((c = strdup(yytext)) == NULL)
@@ -90,7 +87,8 @@ KeySym key = 0;
c++; /* skip the < */
c[strlen(c)-1] = '\0'; /* trim the > */
- key = XStringToKeysym(c);
+ if ((key = XStringToKeysym(c)) == NoSymbol)
+ yyerror("invalid keysym %s", c);
free(--c);
}
@@ -101,7 +99,10 @@ KeySym key = 0;
<KEY>"(" { key = XK_parenleft; }
<KEY>")" { key = XK_parenright; }
-<KEY>. { key = XStringToKeysym(yytext); }
+<KEY>. {
+ if ((key = XStringToKeysym(yytext)) == NoSymbol)
+ yyerror("invalid key %s", yytext);
+ }
%%
diff --git a/parse.y b/parse.y
index 38d035c..ed7bd95 100644
--- a/parse.y
+++ b/parse.y
@@ -19,12 +19,8 @@
#include <stdio.h>
-#include <X11/Xlib.h>
-
#include "star-platinum.h"
-void yyerror(const char*);
-
/*
* #define YYDEBUG 1
* int yydebug = 1;
diff --git a/star-platinum.c b/star-platinum.c
index 5c6cd54..232d734 100644
--- a/star-platinum.c
+++ b/star-platinum.c
@@ -17,6 +17,7 @@
#include "star-platinum.h"
#include <err.h>
+#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -49,11 +50,24 @@ int ignored_modifiers[] = {
#define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask)
+__attribute__((__format__ (__printf__, 1, 2)))
void
-yyerror(const char *e)
+yyerror(const char *fmt, ...)
{
+ va_list ap;
+ size_t l;
+
+ va_start(ap, fmt);
+
goterror = 1;
- fprintf(stderr, "%s:%d %s\n", fname, yylineno, e);
+ fprintf(stderr, "%s:%d ", fname, yylineno);
+ vfprintf(stderr, fmt, ap);
+
+ l = strlen(fmt);
+ if (l > 0 && fmt[l-1] != '\n')
+ fprintf(stderr, "\n");
+
+ va_end(ap);
}
char *
diff --git a/star-platinum.h b/star-platinum.h
index 4ed031b..88db9ef 100644
--- a/star-platinum.h
+++ b/star-platinum.h
@@ -27,6 +27,8 @@
#include <err.h>
#include <stdlib.h>
+void yyerror(const char*, ...);
+
struct key {
unsigned int modifier;
KeySym key;