xkbcomp: Don't malloc() and free() most scanned tokens Use a constant buffer instead. Sigh. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
diff --git a/src/xkbcomp/parseutils.h b/src/xkbcomp/parseutils.h
index ea04240..0fecb42 100644
--- a/src/xkbcomp/parseutils.h
+++ b/src/xkbcomp/parseutils.h
@@ -33,7 +33,7 @@
#include "xkbcomp.h"
-extern char *scanStr;
+extern char scanBuf[1024];
extern int scanInt;
extern int lineNum;
diff --git a/src/xkbcomp/xkbparse.y b/src/xkbcomp/xkbparse.y
index 68e261f..1c05356 100644
--- a/src/xkbcomp/xkbparse.y
+++ b/src/xkbcomp/xkbparse.y
@@ -313,10 +313,10 @@ Decl : OptMergeMode VarDecl
{
if ($1==MergeAltForm) {
yyerror("cannot use 'alternate' to include other maps");
- $$= &IncludeCreate(scanStr,MergeDefault)->common;
+ $$= &IncludeCreate(scanBuf,MergeDefault)->common;
}
else {
- $$= &IncludeCreate(scanStr,$1)->common;
+ $$= &IncludeCreate(scanBuf,$1)->common;
}
}
;
@@ -723,7 +723,7 @@ KeySymList : KeySymList COMMA KeySym
{ $$= CreateKeysymList($1); }
;
-KeySym : IDENT { $$= scanStr; scanStr= NULL; }
+KeySym : IDENT { $$= strdup(scanBuf); }
| SECTION { $$= strdup("section"); }
| Integer
{
@@ -753,21 +753,21 @@ Float : FLOAT { $$= scanInt; }
Integer : INTEGER { $$= scanInt; }
;
-KeyName : KEYNAME { $$= scanStr; scanStr= NULL; }
+KeyName : KEYNAME { $$= strdup(scanBuf); }
;
-Ident : IDENT { $$= XkbcInternAtom(scanStr,False); }
+Ident : IDENT { $$= XkbcInternAtom(scanBuf,False); }
| DEFAULT { $$= XkbcInternAtom("default",False); }
;
-String : STRING { $$= XkbcInternAtom(scanStr,False); }
+String : STRING { $$= XkbcInternAtom(scanBuf,False); }
;
OptMapName : MapName { $$= $1; }
| { $$= NULL; }
;
-MapName : STRING { $$= scanStr; scanStr= NULL; }
+MapName : STRING { $$= strdup(scanBuf); }
;
%%
void
@@ -776,8 +776,8 @@ yyerror(const char *s)
if (warningLevel>0) {
(void)fprintf(stderr,"%s: line %d of %s\n",s,lineNum,
(scanFile?scanFile:"(unknown)"));
- if ((scanStr)&&(warningLevel>3))
- (void)fprintf(stderr,"last scanned symbol is: %s\n",scanStr);
+ if ((warningLevel>3))
+ (void)fprintf(stderr,"last scanned symbol is: %s\n",scanBuf);
}
return;
}
diff --git a/src/xkbcomp/xkbscan.c b/src/xkbcomp/xkbscan.c
index 1bf363d..e03813c 100644
--- a/src/xkbcomp/xkbscan.c
+++ b/src/xkbcomp/xkbscan.c
@@ -43,11 +43,10 @@ int lineNum = 0;
int scanInt;
-char *scanStr = NULL;
+char scanBuf[1024];
static int scanStrLine = 0;
#define BUFSIZE 4096
-static int nInBuf = 0;
static char readBuf[BUFSIZE];
static int readBufPos = 0;
static int readBufLen = 0;
@@ -218,7 +217,7 @@ tokText(int tok)
break;
case STRING:
- snprintf(buf, sizeof(buf), "STRING (%s)", scanStr);
+ snprintf(buf, sizeof(buf), "STRING (%s)", scanBuf);
break;
case INTEGER:
snprintf(buf, sizeof(buf), "INTEGER (0x%x)", scanInt);
@@ -228,10 +227,10 @@ tokText(int tok)
scanInt / XkbGeomPtsPerMM, scanInt % XkbGeomPtsPerMM);
break;
case IDENT:
- snprintf(buf, sizeof(buf), "IDENT (%s)", scanStr);
+ snprintf(buf, sizeof(buf), "IDENT (%s)", scanBuf);
break;
case KEYNAME:
- snprintf(buf, sizeof(buf), "KEYNAME (%s)", scanStr);
+ snprintf(buf, sizeof(buf), "KEYNAME (%s)", scanBuf);
break;
case PARTIAL:
@@ -308,7 +307,6 @@ static int
yyGetString(void)
{
int ch, i;
- char buf[1024];
i = 0;
while (((ch = scanchar()) != EOF) && (ch != '"'))
@@ -376,15 +374,12 @@ yyGetString(void)
else
return ERROR_TOK;
}
- if (i < sizeof(buf) - 1)
- buf[i++] = ch;
+ if (i < sizeof(scanBuf) - 1)
+ scanBuf[i++] = ch;
}
if (ch == '"')
{
- buf[i++] = '\0';
- if (scanStr)
- uFree(scanStr);
- scanStr = strdup(buf);
+ scanBuf[i++] = '\0';
scanStrLine = lineNum;
return STRING;
}
@@ -395,7 +390,6 @@ static int
yyGetKeyName(void)
{
int ch, i;
- char buf[1024];
i = 0;
while (((ch = scanchar()) != EOF) && (ch != '>'))
@@ -458,15 +452,12 @@ yyGetKeyName(void)
return ERROR_TOK;
}
- if (i < sizeof(buf) - 1)
- buf[i++] = ch;
+ if (i < sizeof(scanBuf) - 1)
+ scanBuf[i++] = ch;
}
if ((ch == '>') && (i < 5))
{
- buf[i++] = '\0';
- if (scanStr)
- uFree(scanStr);
- scanStr = strdup(buf);
+ scanBuf[i++] = '\0';
scanStrLine = lineNum;
return KEYNAME;
}
@@ -577,21 +568,20 @@ yyGetIdent(int first)
{
int ch, i, j, found;
int rtrn = IDENT;
- char buf[1024];
- buf[0] = first;
+ scanBuf[0] = first;
j = 1;
while (((ch = scanchar()) != EOF) && (isalnum(ch) || (ch == '_')))
{
- if (j < sizeof(buf) - 1)
- buf[j++] = ch;
+ if (j < sizeof(scanBuf) - 1)
+ scanBuf[j++] = ch;
}
- buf[j++] = '\0';
+ scanBuf[j++] = '\0';
found = 0;
for (i = 0; (!found) && (i < numKeywords); i++)
{
- if (uStrCaseCmp(buf, keywords[i].keyword) == 0)
+ if (uStrCaseCmp(scanBuf, keywords[i].keyword) == 0)
{
rtrn = keywords[i].token;
found = 1;
@@ -599,9 +589,6 @@ yyGetIdent(int first)
}
if (!found)
{
- if (scanStr)
- uFree(scanStr);
- scanStr = strdup(buf);
scanStrLine = lineNum;
rtrn = IDENT;
}
@@ -619,6 +606,7 @@ yyGetNumber(int ch)
{
int isFloat = 0;
char buf[1024];
+ int nInBuf = 0;
buf[0] = ch;
nInBuf = 1;