summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2023-10-08 13:22:13 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-10-08 13:22:13 -0700
commit8a91517ca6ea77633476595b0eb5b213357c60e5 (patch)
treeb6250defde82716184b026b8b85fc2e3923d1dc4
parent4e7bae89a887b189aa4c5e551ce0fba4a763df1a (diff)
xkbtext.c: Add tbGetBufferString helper function
Handles common case of allocating & copying string to temporary buffer As a side effect, clears up gcc warning: xkbtext.c: In function ‘XkbBehaviorText’: xkbtext.c:1476:19: warning: declaration of ‘tmp’ shadows a previous local [-Wshadow] 1476 | char *tmp; | ^~~ xkbtext.c:1457:21: note: shadowed declaration is here 1457 | char buf[256], *tmp; | ^~~ Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--src/xkbtext.c41
1 files changed, 21 insertions, 20 deletions
diff --git a/src/xkbtext.c b/src/xkbtext.c
index fe30d2b..d3a37a1 100644
--- a/src/xkbtext.c
+++ b/src/xkbtext.c
@@ -68,6 +68,20 @@ tbGetBuffer(unsigned size)
/***====================================================================***/
+static inline char *
+tbGetBufferString(const char *str)
+{
+ size_t size = strlen(str) + 1;
+ char *rtrn = tbGetBuffer((unsigned) size);
+
+ if (rtrn != NULL)
+ memcpy(rtrn, str, size);
+
+ return rtrn;
+}
+
+/***====================================================================***/
+
char *
XkbAtomText(Display *dpy, Atom atm, unsigned format)
{
@@ -236,7 +250,6 @@ static const char *modNames[XkbNumModifiers] = {
char *
XkbModIndexText(unsigned ndx, unsigned format)
{
- char *rtrn;
char buf[100];
if (format == XkbCFile) {
@@ -255,9 +268,7 @@ XkbModIndexText(unsigned ndx, unsigned format)
else
snprintf(buf, sizeof(buf), "ILLEGAL_%02x", ndx);
}
- rtrn = tbGetBuffer(strlen(buf) + 1);
- strcpy(rtrn, buf);
- return rtrn;
+ return tbGetBufferString(buf);
}
char *
@@ -548,9 +559,7 @@ XkbNKNDetailMaskText(unsigned detail, unsigned format)
tmp = "0";
else if (format == XkbMessage)
tmp = "none";
- buf = tbGetBuffer(strlen(tmp) + 1);
- strcpy(buf, tmp);
- return buf;
+ return tbGetBufferString(tmp);
}
else if ((detail & XkbAllNewKeyboardEventsMask) ==
XkbAllNewKeyboardEventsMask) {
@@ -560,9 +569,7 @@ XkbNKNDetailMaskText(unsigned detail, unsigned format)
tmp = "XkbAllNewKeyboardEventsMask";
else
tmp = "all";
- buf = tbGetBuffer(strlen(tmp) + 1);
- strcpy(buf, tmp);
- return buf;
+ return tbGetBufferString(tmp);
}
if (format == XkbMessage) {
prefix = "";
@@ -1424,7 +1431,7 @@ static actionCopy copyActionArgs[XkbSA_NumActions] = {
char *
XkbActionText(Display *dpy, XkbDescPtr xkb, XkbAction *action, unsigned format)
{
- char buf[ACTION_SZ], *tmp;
+ char buf[ACTION_SZ];
int sz;
if (format == XkbCFile) {
@@ -1445,16 +1452,13 @@ XkbActionText(Display *dpy, XkbDescPtr xkb, XkbAction *action, unsigned format)
CopyOtherArgs(dpy, xkb, action, buf, &sz);
TryCopyStr(buf, ")", &sz);
}
- tmp = tbGetBuffer(strlen(buf) + 1);
- if (tmp != NULL)
- strcpy(tmp, buf);
- return tmp;
+ return tbGetBufferString(buf);
}
char *
XkbBehaviorText(XkbDescPtr xkb, XkbBehavior * behavior, unsigned format)
{
- char buf[256], *tmp;
+ char buf[256];
if (format == XkbCFile) {
if (behavior->type == XkbKB_Default)
@@ -1509,10 +1513,7 @@ XkbBehaviorText(XkbDescPtr xkb, XkbBehavior * behavior, unsigned format)
snprintf(buf, sizeof(buf), "overlay%d= %s", ndx, kn);
}
}
- tmp = tbGetBuffer(strlen(buf) + 1);
- if (tmp != NULL)
- strcpy(tmp, buf);
- return tmp;
+ return tbGetBufferString(buf);
}
/***====================================================================***/