rules: reformat input line handling Signed-off-by: Ran Benita <ran234@gmail.com>
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c
index 24e964e..e18189e 100644
--- a/src/xkbcomp/rules.c
+++ b/src/xkbcomp/rules.c
@@ -30,127 +30,142 @@
#include "rules.h"
#include "path.h"
-#define DFLT_LINE_SIZE 128
+#define DFLT_LINE_SIZE 128
-typedef struct {
- size_t sz_line;
- int num_line;
- char buf[DFLT_LINE_SIZE];
- char * line;
-} InputLine;
+struct input_line {
+ size_t size;
+ size_t offset;
+ char buf[DFLT_LINE_SIZE];
+ char *line;
+};
static void
-InitInputLine(InputLine *line)
+input_line_init(struct input_line *line)
{
- line->num_line= 0;
- line->sz_line= DFLT_LINE_SIZE;
- line->line= line->buf;
+ line->size = DFLT_LINE_SIZE;
+ line->offset = 0;
+ line->line = line->buf;
}
static void
-FreeInputLine(InputLine *line)
+input_line_deinit(struct input_line *line)
{
- if (line->line!=line->buf)
- free(line->line);
- line->num_line= 0;
- line->sz_line= DFLT_LINE_SIZE;
- line->line= line->buf;
+ if (line->line != line->buf)
+ free(line->line);
+ line->offset = 0;
+ line->size = DFLT_LINE_SIZE;
+ line->line = line->buf;
}
static int
-InputLineAddChar(InputLine *line,int ch)
+input_line_add_char(struct input_line *line, int ch)
{
- if (line->num_line>=line->sz_line) {
- if (line->line==line->buf) {
- line->line = malloc(line->sz_line * 2);
- memcpy(line->line,line->buf,line->sz_line);
- }
- else {
- line->line = realloc(line->line, line->sz_line * 2);
- }
- line->sz_line*= 2;
+ if (line->offset >= line->size) {
+ if (line->line == line->buf) {
+ line->line = malloc(line->size * 2);
+ memcpy(line->line, line->buf, line->size);
+ }
+ else {
+ line->line = realloc(line->line, line->size * 2);
+ }
+
+ line->size *= 2;
}
- line->line[line->num_line++]= ch;
+
+ line->line[line->offset++] = ch;
return ch;
}
-#define ADD_CHAR(l,c) ((l)->num_line<(l)->sz_line?\
- (int)((l)->line[(l)->num_line++]= (c)):\
- InputLineAddChar(l,c))
-
static bool
-GetInputLine(FILE *file,InputLine *line,bool checkbang)
+input_line_get(FILE *file, struct input_line *line)
{
- int ch;
- bool endOfFile,spacePending,slashPending,inComment;
-
- endOfFile= false;
- while ((!endOfFile)&&(line->num_line==0)) {
- spacePending= slashPending= inComment= false;
- while (((ch=getc(file))!='\n')&&(ch!=EOF)) {
- if (ch=='\\') {
- if ((ch=getc(file))==EOF)
- break;
- if (ch=='\n') {
- inComment= false;
- ch= ' ';
- }
- }
- if (inComment)
- continue;
- if (ch=='/') {
- if (slashPending) {
- inComment= true;
- slashPending= false;
- }
- else {
- slashPending= true;
- }
- continue;
- }
- else if (slashPending) {
- if (spacePending) {
- ADD_CHAR(line,' ');
- spacePending= false;
- }
- ADD_CHAR(line,'/');
- slashPending= false;
- }
- if (isspace(ch)) {
- while (isspace(ch)&&(ch!='\n')&&(ch!=EOF)) {
- ch= getc(file);
- }
- if (ch==EOF)
- break;
- if ((ch!='\n')&&(line->num_line>0))
- spacePending= true;
- ungetc(ch,file);
- }
- else {
- if (spacePending) {
- ADD_CHAR(line,' ');
- spacePending= false;
- }
- if (checkbang && ch=='!') {
- if (line->num_line!=0) {
- WARN("The '!' legal only at start of line\n");
- ACTION("Line containing '!' ignored\n");
- line->num_line= 0;
- break;
- }
+ int ch;
+ bool end_of_file = false;
+ bool space_pending;
+ bool slash_pending;
+ bool in_comment;
- }
- ADD_CHAR(line,ch);
- }
- }
- if (ch==EOF)
- endOfFile= true;
-/* else line->num_line++;*/
- }
- if ((line->num_line==0)&&(endOfFile))
- return false;
- ADD_CHAR(line,'\0');
- return true;
+ while (!end_of_file && line->offset == 0) {
+ space_pending = slash_pending = in_comment = false;
+
+ while ((ch = getc(file)) != '\n' && ch != EOF) {
+ if (ch == '\\') {
+ ch = getc(file);
+
+ if (ch == EOF)
+ break;
+
+ if (ch == '\n') {
+ in_comment = false;
+ ch = ' ';
+ }
+ }
+
+ if (in_comment)
+ continue;
+
+ if (ch == '/') {
+ if (slash_pending) {
+ in_comment = true;
+ slash_pending = false;
+ }
+ else {
+ slash_pending = true;
+ }
+
+ continue;
+ }
+
+ if (slash_pending) {
+ if (space_pending) {
+ input_line_add_char(line, ' ');
+ space_pending = false;
+ }
+
+ input_line_add_char(line, '/');
+ slash_pending = false;
+ }
+
+ if (isspace(ch)) {
+ while (isspace(ch) && ch != '\n' && ch != EOF)
+ ch = getc(file);
+
+ if (ch == EOF)
+ break;
+
+ if (ch != '\n' && line->offset > 0)
+ space_pending = true;
+
+ ungetc(ch, file);
+ }
+ else {
+ if (space_pending) {
+ input_line_add_char(line, ' ');
+ space_pending = false;
+ }
+
+ if (ch == '!') {
+ if (line->offset != 0) {
+ WARN("The '!' is legal only at start of line\n");
+ ACTION("Line containing '!' ignored\n");
+ line->offset = 0;
+ break;
+ }
+ }
+
+ input_line_add_char(line, ch);
+ }
+ }
+
+ if (ch == EOF)
+ end_of_file = true;
+ }
+
+ if (line->offset == 0 && end_of_file)
+ return false;
+
+ input_line_add_char(line, '\0');
+ return true;
}
/***====================================================================***/
@@ -288,7 +303,7 @@ get_index(char *str, int *ndx)
}
static void
-SetUpRemap(InputLine *line,RemapSpec *remap)
+SetUpRemap(struct input_line *line,RemapSpec *remap)
{
char *tok, *str;
unsigned present, l_ndx_present, v_ndx_present;
@@ -409,7 +424,7 @@ MatchOneOf(char *wanted,char *vals_defined)
/***====================================================================***/
static bool
-CheckLine( InputLine * line,
+CheckLine( struct input_line * line,
RemapSpec * remap,
XkbRF_RulePtr rule,
XkbRF_GroupPtr group)
@@ -945,7 +960,7 @@ XkbcRF_AddGroup(XkbRF_RulesPtr rules)
static XkbRF_RulesPtr
XkbcRF_LoadRules(FILE *file)
{
-InputLine line;
+struct input_line line;
RemapSpec remap;
XkbRF_RuleRec trule,*rule;
XkbRF_GroupRec tgroup,*group;
@@ -957,8 +972,8 @@ XkbRF_GroupRec tgroup,*group;
memset(&remap, 0, sizeof(RemapSpec));
memset(&tgroup, 0, sizeof(XkbRF_GroupRec));
- InitInputLine(&line);
- while (GetInputLine(file, &line, true)) {
+ input_line_init(&line);
+ while (input_line_get(file, &line)) {
if (CheckLine(&line,&remap,&trule,&tgroup)) {
if (tgroup.number) {
if ((group= XkbcRF_AddGroup(rules))!=NULL) {
@@ -972,9 +987,9 @@ XkbRF_GroupRec tgroup,*group;
}
}
}
- line.num_line= 0;
+ line.offset = 0;
}
- FreeInputLine(&line);
+ input_line_deinit(&line);
return rules;
}